Skip to content

Instantly share code, notes, and snippets.

@lord-max
Last active May 1, 2021 01:24
Show Gist options
  • Save lord-max/46da0c86e0619d26641f1365b591f040 to your computer and use it in GitHub Desktop.
Save lord-max/46da0c86e0619d26641f1365b591f040 to your computer and use it in GitHub Desktop.
BIP39 checksum word via console: when generating BIP39 seed by hand, you will need this script to fix last 4 bits (12th word) with a correct checksum. Minified version with no comments and no error checks. Full version: https://gist.github.com/lord-max/74190d1a211720671e68d4789227b43f
function checksum_12_words_min(data) {
let binstr = (s,l =8) => s.toString(2).padStart(l,'0') // convert to binary `0011...` string
let tohex = (bytes) => bytes.map( x => x.toString(16).padStart(2,0) ).join('')
let bytes = data.map( x => binstr(x - 1, 11)).join('').match(/.{1,8}/g).map( x => parseInt(x, 2))
bytes.pop(); console.log("Entropy is :",tohex(bytes))
window.crypto.subtle.digest("SHA-256", new Uint8Array(bytes).buffer).then( x => {
let hash = new Uint8Array(x)
let cs = binstr(hash[0]).match(/.{1,4}/g)[0]
let bits = [binstr(bytes[15]),cs].join('')
console.log("Your 12th word index is: " + (1+parseInt(bits.substr(1),2)))
}); return "OK"
}; data = [101, 502, 962, 1400, 1607, 1817, 1090, 1827, 820, 1334, 156, 1073]; checksum_12_words_min(data)
// Entropy is : 0c87d5e0d77c8dc6220f226674d44dc3
// Your 12th word index is: 1078
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment