Created
February 10, 2020 17:58
-
-
Save bitjson/23489db79a9950c51c3e8e233235444f to your computer and use it in GitHub Desktop.
CashAddress Encoding, Decoding, and Error Correction in Javascript
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 { instantiateSecp256k1, hexToBin, instantiateRipemd160, encodeCashAddress, CashAddressType, instantiateSha256, encodeCashAddressFormat, attemptCashAddressFormatErrorCorrection, decodeCashAddress, decodeCashAddressFormat, stringify } from 'bitcoin-ts'; | |
(async () => { | |
const secp256k1 = await instantiateSecp256k1(); | |
const sha256 = await instantiateSha256(); | |
const ripemd160 = await instantiateRipemd160(); | |
// Create an address from a private key | |
const privateKey = hexToBin('f85d4bd8a03ca106c9deb47b791803dac7f0333809e3f1dd04d182e0aba6e553'); | |
const publicKey = secp256k1.derivePublicKeyCompressed(privateKey); | |
const publicKeyHash = ripemd160.hash(sha256.hash(publicKey)); | |
// Encode a P2PKH CashAddress | |
const cashAddress = encodeCashAddress('bchtest', CashAddressType.P2PKH, publicKeyHash); | |
// Decode a CashAddress | |
const cashAddressDecoded = decodeCashAddress('bchtest:qq2azmyyv6dtgczexyalqar70q036yund53jvfde0x'); | |
// Encode a P2PKH SLP Address (uses the CashAddress format) | |
const slpAddress = encodeCashAddressFormat('slptest', 0, publicKeyHash); | |
// Decode an SLP Address (uses the CashAddress format) | |
const slpDecoded = decodeCashAddressFormat('slptest:qq2azmyyv6dtgczexyalqar70q036yund52xtjhwam'); | |
// Fix up to 2 errors in a CashAddress-formatted string | |
const broken = 'bitauth:qwtcxp42fcp06phz2xec6t5krau0ftewxefy50j9xyfxwa38df40zp5xz6t5w'; | |
const result = attemptCashAddressFormatErrorCorrection(broken); | |
// Example prompt to correct a Bitauth ID | |
const pointToCorrections = (c: number[]) => Array.from({ length: c[c.length - 1] + 1 }, (_, i) => c.includes(i) ? '^' : '-').join(''); | |
console.log('Cash Address:'); | |
console.log(cashAddress); | |
console.log(stringify(cashAddressDecoded), '\n'); | |
console.log('SLP Address:'); | |
console.log(slpAddress); | |
console.log(stringify(slpDecoded), '\n'); | |
console.log('Error Correction:'); | |
console.log(result); | |
console.log(typeof result === 'string' ? '[no correction]' : ` | |
You entered: ${broken} | |
${pointToCorrections(result.corrections)} | |
Did you mean: ${result.address}?`); | |
})(); |
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
Cash Address: | |
bchtest:qq2azmyyv6dtgczexyalqar70q036yund53jvfde0x | |
{ | |
"hash": "<Uint8Array: 0x15d16c84669ab46059313bf0747e781f1d13936d>", | |
"prefix": "bchtest", | |
"type": 0 | |
} | |
SLP Address: | |
slptest:qq2azmyyv6dtgczexyalqar70q036yund52xtjhwam | |
{ | |
"hash": "<Uint8Array: 0x15d16c84669ab46059313bf0747e781f1d13936d>", | |
"prefix": "slptest", | |
"version": 0 | |
} | |
Error Correction: | |
{ | |
address: 'bitauth:qwtcxp42fcp06phz2xec6t5krau0ftew5efy50j9xyfxwa38df40zp58z6t5w', | |
corrections: [ 40, 63 ] | |
} | |
You entered: bitauth:qwtcxp42fcp06phz2xec6t5krau0ftewxefy50j9xyfxwa38df40zp5xz6t5w | |
----------------------------------------^----------------------^ | |
Did you mean: bitauth:qwtcxp42fcp06phz2xec6t5krau0ftew5efy50j9xyfxwa38df40zp58z6t5w? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment