add toString method for bip32

...so that it is consistent with the rest of the library
This commit is contained in:
Ryan X. Charles 2014-08-09 23:29:05 -07:00
parent 43f288d205
commit 7def2609ec
2 changed files with 38 additions and 0 deletions

View File

@ -308,6 +308,16 @@ BIP32.prototype.deriveChild = function(i) {
return ret;
}
BIP32.prototype.toString = function() {
var isPrivate =
(this.version == constants.mainnet.bip32privkey ||
this.version == constants.testnet.bip32privkey);
if (isPrivate)
return this.extendedPrivateKeyString();
else
return this.extendedPublicKeyString();
};
function uint(f, size) {
if (f.length < size)

View File

@ -1,4 +1,5 @@
var should = require('chai').should();
var constants = require('../lib/constants');
var BIP32 = require('../lib/bip32');
describe('BIP32', function() {
@ -299,4 +300,31 @@ describe('BIP32', function() {
});
});
describe('#toString', function() {
var bip32 = new BIP32();
bip32.fromRandom('mainnet');
var tip32 = new BIP32();
tip32.fromRandom('testnet');
it('should return an xprv string', function() {
bip32.toString().slice(0, 4).should.equal('xprv');
});
it('should return an xpub string', function() {
var bip32b = new BIP32(bip32.extendedPublicKeyString());
bip32b.toString().slice(0, 4).should.equal('xpub');
});
it('should return a tprv string', function() {
tip32.toString().slice(0, 4).should.equal('tprv');
});
it('should return a tpub string', function() {
var tip32b = new BIP32(tip32.extendedPublicKeyString());
tip32b.toString().slice(0, 4).should.equal('tpub');
});
});
});