Created
June 3, 2024 09:29
-
-
Save WietseWind/a7733799810e1512816ea916caa2f0d7 to your computer and use it in GitHub Desktop.
XRPL Account Generator (ED25519) with 78x 10-face Dice roll
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 accountlib from 'xrpl-accountlib' | |
import elliptic from 'elliptic' | |
import assert from 'assert' | |
const ec = new elliptic.eddsa('ed25519') | |
// Adapted from validator keypair dice tool by RichardAH | |
// https://github.com/RichardAH/validator-keys-from-dice/blob/main/dice.js | |
// Random (BAD RANDOM! BAD BAD BAD RANDOM! DO NOT EVER USE THIS!! TESTING PURPOSES ONLY!) | |
const dicerolls = Array(78).fill().map(r => Math.floor(Math.random() * 10)) | |
// const _dicerolls = [ | |
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10 | |
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 | |
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30 | |
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40 | |
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 50 | |
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60 | |
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 70 | |
// 0, 0, 0, 0, 0, 0, 0, 0 // 78 | |
// ] | |
assert(Math.log2(10) * dicerolls.length > 256, 'Invalid # of dice rolls, not satisfying 256 bits') | |
const bnumber = BigInt(dicerolls.join('')).toString(2).padStart(256, '0').slice(-256) | |
const bnumhex = BigInt(`0b${bnumber}`).toString(16).toUpperCase().padStart(64, '0').slice(-64) | |
const kp = ec.keyFromSecret(bnumhex) | |
const ecKeypair = { | |
secret: 'ED' + Buffer.from(kp.getSecret()).toString('hex').toUpperCase(), | |
public: 'ED' + Buffer.from(kp.getPublic()).toString('hex').toUpperCase(), | |
} | |
assert(ecKeypair.secret.slice(-64) === bnumhex, 'Secret key mismatch (pre accountlib)') | |
const account = accountlib.derive.privatekey(`ED${bnumhex}`) | |
assert(`ED${bnumhex}` === account.keypair.privateKey, 'Secret key mismatch (accountlib)') | |
assert(ecKeypair.public === account.keypair.publicKey, 'Public key derivation mismatch') | |
console.log(account.keypair.privateKey) | |
console.log(account.address) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment