Created
February 24, 2021 04:30
-
-
Save shrugs/4e57fd3f288d02a75c885dd793a85ac0 to your computer and use it in GitHub Desktop.
amulet verification code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { utils } from 'ethers' | |
export interface Amulet { | |
value: string; | |
hash: string; | |
count: number; | |
} | |
const getAmuletCount = (hash: string): number => { | |
const matches = hash.match(/(8)\1*/g); | |
if (!matches) return 0; | |
return Math.max(...matches.map(match => match.length)); | |
} | |
const isValidAmuletCount = (count: number) => count >= 4; | |
export const toValidAmulet = (value: string): Amulet | undefined => { | |
const bytes = utils.toUtf8Bytes(value); | |
if (bytes.length > 64) return; | |
const hash = utils.sha256(bytes); | |
const count = getAmuletCount(hash); | |
if (!isValidAmuletCount(count)) return; | |
return {value, hash, count} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment