Skip to content

Instantly share code, notes, and snippets.

@shrugs
Created February 24, 2021 04:30
Show Gist options
  • Save shrugs/4e57fd3f288d02a75c885dd793a85ac0 to your computer and use it in GitHub Desktop.
Save shrugs/4e57fd3f288d02a75c885dd793a85ac0 to your computer and use it in GitHub Desktop.
amulet verification code
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