Skip to content

Encodes byte arrays into strings of custom alphabets, such as ripple's base58

License

Notifications You must be signed in to change notification settings

ripple/x-address-codec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deprecation Notice

As of September 3, 2019, this library's functionality has been incorporated into ripple-address-codec.

This library is no longer maintained.

x-address-codec NPM Build Status

This is a meta-package that exposes an API factory.

A base-x codec is included.

This library is used to encode/decode cryptocurrency address strings to bytes and back.

Supported alphabets:

  • ripple
  • tipple
  • bitcoin
  • stellar

API

var apiFactory = require('../');
var createHash = require('create-hash');

var api = apiFactory({
  defaultAlphabet: 'stellar',

  // Bring your own hash function:
  sha256: function(bytes) {
    return createHash('sha256').update(new Buffer(bytes)).digest();
  },

  // x-address-codec adds encode* and decode* methods automatically.
  codecMethods : {
    // public keys
    AccountID : {version: 0x00},
    // secrets
    Seed: {version: 0x21}
  },
});

var buf = new Buffer("00000000000000000000000000000000", 'hex');
// It can encode a Buffer
var encoded = api.encodeSeed(buf);
// It returns Array<Number>
var decoded = api.decodeSeed(encoded);
// It can of course encode an Array<Number> too
var reencoded = api.encodeSeed(decoded)

console.log(encoded);
console.log(reencoded);
// ps6JS7f14BuwFY8Mw6bTtLKWauoUp
// ps6JS7f14BuwFY8Mw6bTtLKWauoUp

console.log(decoded);
// [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]

Example: Encode the value as an XRP Ledger seed

console.log(api.encode(decoded, {alphabet: 'ripple', version: 33}));
// sp6JS7f14BuwFY8Mw6bTtLKWauoUs

Example: Create a codec that uses spaceMan as its prefix

var prefix = api.codecs.stellar.findPrefix(16 /* bytes */, 'spaceMan');
var spacey = api.encode(decoded, {version: prefix});
console.log(spacey);
// spaceMan7qBfYEUBHSWDsZjJHctnNQi2pCTn
console.log(api.decode(spacey, {version: prefix}));
// [ 0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0 ]

Example: Export and Publish

module.exports = api;
$ npm publish

Example: Exported functionality

console.log(api)
/*
{ Codec: [Function: AddressCodec],
  codecs:
   { bitcoin:
      { alphabet: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
        codec: [Object],
        base: 58 },
     ripple:
      { alphabet: 'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz',
        codec: [Object],
        base: 58 },
     tipple:
      { alphabet: 'RPShNAF39wBUDnEGHJKLM4pQrsT7VWXYZ2bcdeCg65jkm8ofqi1tuvaxyz',
        codec: [Object],
        base: 58 },
     stellar:
      { alphabet: 'gsphnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCr65jkm8oFqi1tuvAxyz',
        codec: [Object],
        base: 58 } },
  decode: [Function: decode],
  encode: [Function: encode],
  decodeAccountID: [Function],
  encodeAccountID: [Function],
  decodeSeed: [Function],
  encodeSeed: [Function] }
*/

About

Encodes byte arrays into strings of custom alphabets, such as ripple's base58

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published