-
-
Save mrbrucespringsteen/bc75ce01bfc2b339d09e0316b4931762 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} | |
} | |
// 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