Skip to content

Instantly share code, notes, and snippets.

@ltfschoen
Last active April 1, 2020 22:54
Show Gist options
  • Save ltfschoen/f98e5af52dd5ef60e87e87f143d70625 to your computer and use it in GitHub Desktop.
Save ltfschoen/f98e5af52dd5ef60e87e87f143d70625 to your computer and use it in GitHub Desktop.
bs58_util_crypto

Different ways to decode/encode a Substrate public keys (i.e. using bs58 li8brary, and using Polkadot.js util-crypto library)

$ node
const bs58 = require('bs58');
const keyring = require('@polkadot/keyring');
const { decodeAddress, encodeAddress } = require('@polkadot/util-crypto');
const { stringToU8a, u8aToHex } = require('@polkadot/util');

let decoded = u8aToHex(decodeAddress('5HimYxXdszMgAbPQD49kLbgaBb274ubQpRNDJmZD4fA7KrJq'))

'0xfa34ee0f034817963d83845920938c1d23bd7f7d146f588ff0e0f608fd3b6d4e'

keyring.encodeAddress(decoded)

'5HimYxXdszMgAbPQD49kLbgaBb274ubQpRNDJmZD4fA7KrJq'

encodeAddress(decoded)

'5HimYxXdszMgAbPQD49kLbgaBb274ubQpRNDJmZD4fA7KrJq'

bytes = Buffer.from(decoded.slice(2), 'hex');

<Buffer fa 34 ee 0f 03 48 17 96 3d 83 84 59 20 93 8c 1d 23 bd 7f 7d 14 6f 58 8f f0 e0 f6 08 fd 3b 6d 4e>

encoded = keyring.encodeAddress(bytes);

'5HimYxXdszMgAbPQD49kLbgaBb274ubQpRNDJmZD4fA7KrJq'

decoded = bs58.decode('5HimYxXdszMgAbPQD49kLbgaBb274ubQpRNDJmZD4fA7KrJq').toString("hex");

'2afa34ee0f034817963d83845920938c1d23bd7f7d146f588ff0e0f608fd3b6d4ec0be'

bytes = Buffer.from(decoded, 'hex');
bs58.encode(bytes);

'5HimYxXdszMgAbPQD49kLbgaBb274ubQpRNDJmZD4fA7KrJq'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment