XRPL Memo PoW tryout

node v10.24.1
version: 5.0.0
endpointsharetweet
/* Based on hashcash */ const crypto = require("crypto") const alphabet = `0123456789_;:. ?!@#$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ` const next = array => { for (i = array.length - 1; i >= 0; i--) { if (array[i] < alphabet.length - 1) { array[i] += 1 return true } else { array[i] = 0 } } return false } const toSuffix = array => { return array.map(v => { return alphabet[v] }).join('') } const hash = data => { const alg = crypto.createHash('sha1') alg.update(data) return alg.digest('hex') } const findHash = obj => { const data = JSON.stringify(obj) const zeroes = Math.min(Math.ceil(data.length / 28), 10) console.log(`Looking for ${zeroes} zeroes...`) for (let l = 0; l < 25; l++) { let array = Array(l) for (let i = 0; i < l; i++) array[i] = 0 do { const appended = toSuffix(array) const challenge = data + appended const cash = hash(challenge) if (cash.match(`^0{${zeroes}}`)) { return { hash: cash, appended } } } while (next(array)) } } const hashFound = findHash({ some: 'data', more: 'things', andOther: 'stuff', for: 'XRP ledger', because: ['Safe', 'Secure', 'Cool'] }) console.log(hashFound)
Loading…

1 comment

  • posted 5 years ago by intelliot
    If this is intended to simulate Bitcoin/Hashcash-style PoW, then the "leading zeros" test should be based on the binary representation of the hash, where each binary digit can only be 1 or 0. If you're looking at the hash as a number, then it should be below (mathematically less than) a certain target. https://bitcoin.stackexchange.com/questions/57809/why-is-difficulty-measured-in-a-hash-s-leading-zeroes

sign in to comment