more convenient name

This commit is contained in:
Ryan X. Charles 2014-09-17 15:15:56 -07:00
parent bd16eddf54
commit 9473bdf9b0
2 changed files with 59 additions and 59 deletions

View File

@ -28,8 +28,8 @@ BIP32.prototype.set = function(obj) {
this.keypair = obj.keypair || this.keypair;
this.hasPrivateKey = typeof obj.hasPrivateKey !== 'undefined' ? obj.hasPrivateKey : this.hasPrivateKey;
this.pubKeyHash = obj.pubKeyHash || this.pubKeyHash;
this.extendedPublicKey = obj.extendedPublicKey || this.extendedPublicKey;
this.extendedPrivateKey = obj.extendedPrivateKey || this.extendedPrivateKey;
this.xpubkey = obj.xpubkey || this.xpubkey;
this.xprivkey = obj.xprivkey || this.xprivkey;
return this;
};
@ -124,7 +124,7 @@ BIP32.prototype.initFromBytes = function(bytes) {
}
BIP32.prototype.buildExtendedPublicKey = function() {
this.extendedPublicKey = new Buffer([]);
this.xpubkey = new Buffer([]);
var v = null;
switch (this.version) {
@ -141,7 +141,7 @@ BIP32.prototype.buildExtendedPublicKey = function() {
}
// Version
this.extendedPublicKey = Buffer.concat([
this.xpubkey = Buffer.concat([
new Buffer([v >> 24]),
new Buffer([(v >> 16) & 0xff]),
new Buffer([(v >> 8) & 0xff]),
@ -157,11 +157,11 @@ BIP32.prototype.buildExtendedPublicKey = function() {
]);
}
BIP32.prototype.extendedPublicKeyString = function(format) {
BIP32.prototype.xpubkeyString = function(format) {
if (format === undefined || format === 'base58') {
return Base58Check.encode(this.extendedPublicKey);
return Base58Check.encode(this.xpubkey);
} else if (format === 'hex') {
return this.extendedPublicKey.toString('hex');
return this.xpubkey.toString('hex');
} else {
throw new Error('bad format');
}
@ -169,11 +169,11 @@ BIP32.prototype.extendedPublicKeyString = function(format) {
BIP32.prototype.buildExtendedPrivateKey = function() {
if (!this.hasPrivateKey) return;
this.extendedPrivateKey = new Buffer([]);
this.xprivkey = new Buffer([]);
var v = this.version;
this.extendedPrivateKey = Buffer.concat([
this.xprivkey = Buffer.concat([
new Buffer([v >> 24]),
new Buffer([(v >> 16) & 0xff]),
new Buffer([(v >> 8) & 0xff]),
@ -190,11 +190,11 @@ BIP32.prototype.buildExtendedPrivateKey = function() {
]);
}
BIP32.prototype.extendedPrivateKeyString = function(format) {
BIP32.prototype.xprivkeyString = function(format) {
if (format === undefined || format === 'base58') {
return Base58Check.encode(this.extendedPrivateKey);
return Base58Check.encode(this.xprivkey);
} else if (format === 'hex') {
return this.extendedPrivateKey.toString('hex');
return this.xprivkey.toString('hex');
} else {
throw new Error('bad format');
}
@ -318,9 +318,9 @@ BIP32.prototype.toString = function() {
this.version == constants.testnet.bip32privkey);
if (isPrivate)
return this.extendedPrivateKeyString();
return this.xprivkeyString();
else
return this.extendedPublicKeyString();
return this.xpubkeyString();
};
module.exports = BIP32;

View File

@ -55,104 +55,104 @@ describe('BIP32', function() {
it('should get the extended public key from the extended private key for test vector 1', function() {
var bip32 = new BIP32().fromString(vector1_m_private);
bip32.extendedPublicKeyString().should.equal(vector1_m_public);
bip32.xpubkeyString().should.equal(vector1_m_public);
});
it("should get m/0' ext. private key from test vector 1", function() {
var bip32 = new BIP32().fromString(vector1_m_private);
var child = bip32.derive("m/0'");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector1_m0h_private);
child.xprivkeyString().should.equal(vector1_m0h_private);
});
it("should get m/0' ext. public key from test vector 1", function() {
var bip32 = new BIP32().fromString(vector1_m_private);
var child = bip32.derive("m/0'");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector1_m0h_public);
child.xpubkeyString().should.equal(vector1_m0h_public);
});
it("should get m/0'/1 ext. private key from test vector 1", function() {
var bip32 = new BIP32().fromString(vector1_m_private);
var child = bip32.derive("m/0'/1");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector1_m0h1_private);
child.xprivkeyString().should.equal(vector1_m0h1_private);
});
it("should get m/0'/1 ext. public key from test vector 1", function() {
var bip32 = new BIP32().fromString(vector1_m_private);
var child = bip32.derive("m/0'/1");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector1_m0h1_public);
child.xpubkeyString().should.equal(vector1_m0h1_public);
});
it("should get m/0'/1 ext. public key from m/0' public key from test vector 1", function() {
var bip32 = new BIP32().fromString(vector1_m_private);
var child = bip32.derive("m/0'");
var child_pub = new BIP32().fromString(child.extendedPublicKeyString());
var child_pub = new BIP32().fromString(child.xpubkeyString());
var child2 = child_pub.derive("m/1");
should.exist(child2);
child2.extendedPublicKeyString().should.equal(vector1_m0h1_public);
child2.xpubkeyString().should.equal(vector1_m0h1_public);
});
it("should get m/0'/1/2h ext. private key from test vector 1", function() {
var bip32 = new BIP32().fromString(vector1_m_private);
var child = bip32.derive("m/0'/1/2'");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector1_m0h12h_private);
child.xprivkeyString().should.equal(vector1_m0h12h_private);
});
it("should get m/0'/1/2h ext. public key from test vector 1", function() {
var bip32 = new BIP32().fromString(vector1_m_private);
var child = bip32.derive("m/0'/1/2'");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector1_m0h12h_public);
child.xpubkeyString().should.equal(vector1_m0h12h_public);
});
it("should get m/0'/1/2h/2 ext. private key from test vector 1", function() {
var bip32 = new BIP32().fromString(vector1_m_private);
var child = bip32.derive("m/0'/1/2'/2");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector1_m0h12h2_private);
child.xprivkeyString().should.equal(vector1_m0h12h2_private);
});
it("should get m/0'/1/2'/2 ext. public key from m/0'/1/2' public key from test vector 1", function() {
var bip32 = new BIP32().fromString(vector1_m_private);
var child = bip32.derive("m/0'/1/2'");
var child_pub = new BIP32().fromString(child.extendedPublicKeyString());
var child_pub = new BIP32().fromString(child.xpubkeyString());
var child2 = child_pub.derive("m/2");
should.exist(child2);
child2.extendedPublicKeyString().should.equal(vector1_m0h12h2_public);
child2.xpubkeyString().should.equal(vector1_m0h12h2_public);
});
it("should get m/0'/1/2h/2 ext. public key from test vector 1", function() {
var bip32 = new BIP32().fromString(vector1_m_private);
var child = bip32.derive("m/0'/1/2'/2");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector1_m0h12h2_public);
child.xpubkeyString().should.equal(vector1_m0h12h2_public);
});
it("should get m/0'/1/2h/2/1000000000 ext. private key from test vector 1", function() {
var bip32 = new BIP32().fromString(vector1_m_private);
var child = bip32.derive("m/0'/1/2'/2/1000000000");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector1_m0h12h21000000000_private);
child.xprivkeyString().should.equal(vector1_m0h12h21000000000_private);
});
it("should get m/0'/1/2h/2/1000000000 ext. public key from test vector 1", function() {
var bip32 = new BIP32().fromString(vector1_m_private);
var child = bip32.derive("m/0'/1/2'/2/1000000000");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector1_m0h12h21000000000_public);
child.xpubkeyString().should.equal(vector1_m0h12h21000000000_public);
});
it("should get m/0'/1/2'/2/1000000000 ext. public key from m/0'/1/2'/2 public key from test vector 1", function() {
var bip32 = new BIP32().fromString(vector1_m_private);
var child = bip32.derive("m/0'/1/2'/2");
var child_pub = new BIP32().fromString(child.extendedPublicKeyString());
var child_pub = new BIP32().fromString(child.xpubkeyString());
var child2 = child_pub.derive("m/1000000000");
should.exist(child2);
child2.extendedPublicKeyString().should.equal(vector1_m0h12h21000000000_public);
child2.xpubkeyString().should.equal(vector1_m0h12h21000000000_public);
});
it('should initialize test vector 2 from the extended public key', function() {
@ -167,118 +167,118 @@ describe('BIP32', function() {
it('should get the extended public key from the extended private key for test vector 2', function() {
var bip32 = new BIP32().fromString(vector2_m_private);
bip32.extendedPublicKeyString().should.equal(vector2_m_public);
bip32.xpubkeyString().should.equal(vector2_m_public);
});
it("should get m/0 ext. private key from test vector 2", function() {
var bip32 = new BIP32().fromString(vector2_m_private);
var child = bip32.derive("m/0");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector2_m0_private);
child.xprivkeyString().should.equal(vector2_m0_private);
});
it("should get m/0 ext. public key from test vector 2", function() {
var bip32 = new BIP32().fromString(vector2_m_private);
var child = bip32.derive("m/0");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector2_m0_public);
child.xpubkeyString().should.equal(vector2_m0_public);
});
it("should get m/0 ext. public key from m public key from test vector 2", function() {
var bip32 = new BIP32().fromString(vector2_m_private);
var child = bip32.derive("m");
var child_pub = new BIP32().fromString(child.extendedPublicKeyString());
var child_pub = new BIP32().fromString(child.xpubkeyString());
var child2 = child_pub.derive("m/0");
should.exist(child2);
child2.extendedPublicKeyString().should.equal(vector2_m0_public);
child2.xpubkeyString().should.equal(vector2_m0_public);
});
it("should get m/0/2147483647h ext. private key from test vector 2", function() {
var bip32 = new BIP32().fromString(vector2_m_private);
var child = bip32.derive("m/0/2147483647'");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector2_m02147483647h_private);
child.xprivkeyString().should.equal(vector2_m02147483647h_private);
});
it("should get m/0/2147483647h ext. public key from test vector 2", function() {
var bip32 = new BIP32().fromString(vector2_m_private);
var child = bip32.derive("m/0/2147483647'");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector2_m02147483647h_public);
child.xpubkeyString().should.equal(vector2_m02147483647h_public);
});
it("should get m/0/2147483647h/1 ext. private key from test vector 2", function() {
var bip32 = new BIP32().fromString(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector2_m02147483647h1_private);
child.xprivkeyString().should.equal(vector2_m02147483647h1_private);
});
it("should get m/0/2147483647h/1 ext. public key from test vector 2", function() {
var bip32 = new BIP32().fromString(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector2_m02147483647h1_public);
child.xpubkeyString().should.equal(vector2_m02147483647h1_public);
});
it("should get m/0/2147483647h/1 ext. public key from m/0/2147483647h public key from test vector 2", function() {
var bip32 = new BIP32().fromString(vector2_m_private);
var child = bip32.derive("m/0/2147483647'");
var child_pub = new BIP32().fromString(child.extendedPublicKeyString());
var child_pub = new BIP32().fromString(child.xpubkeyString());
var child2 = child_pub.derive("m/1");
should.exist(child2);
child2.extendedPublicKeyString().should.equal(vector2_m02147483647h1_public);
child2.xpubkeyString().should.equal(vector2_m02147483647h1_public);
});
it("should get m/0/2147483647h/1/2147483646h ext. private key from test vector 2", function() {
var bip32 = new BIP32().fromString(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1/2147483646'");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector2_m02147483647h12147483646h_private);
child.xprivkeyString().should.equal(vector2_m02147483647h12147483646h_private);
});
it("should get m/0/2147483647h/1/2147483646h ext. public key from test vector 2", function() {
var bip32 = new BIP32().fromString(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1/2147483646'");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector2_m02147483647h12147483646h_public);
child.xpubkeyString().should.equal(vector2_m02147483647h12147483646h_public);
});
it("should get m/0/2147483647h/1/2147483646h/2 ext. private key from test vector 2", function() {
var bip32 = new BIP32().fromString(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1/2147483646'/2");
should.exist(child);
child.extendedPrivateKeyString().should.equal(vector2_m02147483647h12147483646h2_private);
child.xprivkeyString().should.equal(vector2_m02147483647h12147483646h2_private);
});
it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from test vector 2", function() {
var bip32 = new BIP32().fromString(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1/2147483646'/2");
should.exist(child);
child.extendedPublicKeyString().should.equal(vector2_m02147483647h12147483646h2_public);
child.xpubkeyString().should.equal(vector2_m02147483647h12147483646h2_public);
});
it("should get m/0/2147483647h/1/2147483646h/2 ext. public key from m/0/2147483647h/2147483646h public key from test vector 2", function() {
var bip32 = new BIP32().fromString(vector2_m_private);
var child = bip32.derive("m/0/2147483647'/1/2147483646'");
var child_pub = new BIP32().fromString(child.extendedPublicKeyString());
var child_pub = new BIP32().fromString(child.xpubkeyString());
var child2 = child_pub.derive("m/2");
should.exist(child2);
child2.extendedPublicKeyString().should.equal(vector2_m02147483647h12147483646h2_public);
child2.xpubkeyString().should.equal(vector2_m02147483647h12147483646h2_public);
});
describe('testnet', function() {
it('should initialize a new BIP32 correctly from a random BIP32', function() {
var b1 = new BIP32();
b1.fromRandom('testnet');
var b2 = new BIP32().fromString(b1.extendedPublicKeyString());
b2.extendedPublicKeyString().should.equal(b1.extendedPublicKeyString());
var b2 = new BIP32().fromString(b1.xpubkeyString());
b2.xpubkeyString().should.equal(b1.xpubkeyString());
});
it('should generate valid ext pub key for testnet', function() {
var b = new BIP32();
b.fromRandom('testnet');
b.extendedPublicKeyString().substring(0,4).should.equal('tpub');
b.xpubkeyString().substring(0,4).should.equal('tpub');
});
});
@ -293,8 +293,8 @@ describe('BIP32', function() {
key: bip32.key,
hasPrivateKey: bip32.hasPrivateKey,
pubKeyHash: bip32.pubKeyhash,
extendedPublicKey: bip32.extendedPublicKey,
extendedPrivateKey: bip32.extendedPrivateKey
xpubkey: bip32.xpubkey,
xprivkey: bip32.xprivkey
});
bip322.toString().should.equal(bip32.toString());
bip322.set({}).toString().should.equal(bip32.toString());
@ -306,16 +306,16 @@ describe('BIP32', function() {
var hex = vector1_master;
var bip32 = (new BIP32()).fromSeed(new Buffer(hex, 'hex'), 'mainnet');
should.exist(bip32);
bip32.extendedPrivateKeyString().should.equal(vector1_m_private);
bip32.extendedPublicKeyString().should.equal(vector1_m_public);
bip32.xprivkeyString().should.equal(vector1_m_private);
bip32.xpubkeyString().should.equal(vector1_m_public);
});
it('should initialize a new BIP32 correctly from test vector 2 seed', function() {
var hex = vector2_master;
var bip32 = (new BIP32()).fromSeed(new Buffer(hex, 'hex'), 'mainnet');
should.exist(bip32);
bip32.extendedPrivateKeyString().should.equal(vector2_m_private);
bip32.extendedPublicKeyString().should.equal(vector2_m_public);
bip32.xprivkeyString().should.equal(vector2_m_private);
bip32.xpubkeyString().should.equal(vector2_m_public);
});
});
@ -330,7 +330,7 @@ describe('BIP32', function() {
});
it('should return an xpub string', function() {
var bip32b = new BIP32().fromString(bip32.extendedPublicKeyString());
var bip32b = new BIP32().fromString(bip32.xpubkeyString());
bip32b.toString().slice(0, 4).should.equal('xpub');
});
@ -339,7 +339,7 @@ describe('BIP32', function() {
});
it('should return a tpub string', function() {
var tip32b = new BIP32().fromString(tip32.extendedPublicKeyString());
var tip32b = new BIP32().fromString(tip32.xpubkeyString());
tip32b.toString().slice(0, 4).should.equal('tpub');
});