Skip to content

Instantly share code, notes, and snippets.

@stonegao
Forked from mrbrucespringsteen/amulet.ts
Created September 9, 2021 16:54
Show Gist options
  • Save stonegao/3a4c61b7a56361e570b4c06757f4e28d to your computer and use it in GitHub Desktop.
Save stonegao/3a4c61b7a56361e570b4c06757f4e28d 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}
}
// The following adds the option to upload a .txt and scan it for amulets using the classic @shrugs code
// Any amulets found will be saved in a csv.
// You can change the "64" in the code "data.match(/.{1,64}/g)" to find shorter amulets
const fs = require('fs')
function writeCsv(data){
fs.writeFile("amulets.csv",data, function(err){
if (err) return console.error(err)
console.log ("amulets saved")
})
}
fs.readFile("yourfilehere.txt","utf8", function(err,data){
if (err) throw err
const strings = data.match(/.{1,64}/g)
// const strings = data.split (".")
const amulets = strings.filter(result => toValidAmulet(result)).reduce((acc, result) => acc + `${result}\n`,"")
writeCsv(amulets)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment