Skip to content

Instantly share code, notes, and snippets.

@bitjson
Created February 10, 2020 17:58

Revisions

  1. bitjson created this gist Feb 10, 2020.
    46 changes: 46 additions & 0 deletions example.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    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}?`);
    })();
    26 changes: 26 additions & 0 deletions output
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    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?