hd: drop getters.

This commit is contained in:
Christopher Jeffrey 2016-11-30 22:00:22 -08:00
parent 430a69e3d3
commit bffdd78009
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
8 changed files with 82 additions and 52 deletions

View File

@ -177,7 +177,7 @@ function formatWallet(wallet) {
html += 'Current Address: <b>' + wallet.getAddress() + '</b><br>';
}
html += 'Extended Private Key: <b>' + key.xprivkey + '</b><br>';
html += 'Extended Private Key: <b>' + key.toBase58() + '</b><br>';
html += 'Mnemonic: <b>' + key.mnemonic.phrase + '</b><br>';
wallet.getBalance().then(function(balance) {

View File

@ -40,15 +40,12 @@ var SEED_SALT = new Buffer('Bitcoin seed', 'ascii');
* @param {Buffer?} options.chainCode
* @param {Buffer?} options.privateKey
* @property {Network} network
* @property {Base58String} xprivkey
* @property {Base58String} xpubkey
* @property {Mnemonic?} mnemonic
* @property {Number} depth
* @property {Buffer} parentFingerPrint
* @property {Number} childIndex
* @property {Buffer} chainCode
* @property {Buffer} privateKey
* @property {HDPublicKey} hdPublicKey
*/
function HDPrivateKey(options) {
@ -69,7 +66,6 @@ function HDPrivateKey(options) {
this._xprivkey = null;
this.hdPrivateKey = this;
this._hdPublicKey = null;
if (options)
@ -124,7 +120,12 @@ HDPrivateKey.fromOptions = function fromOptions(options) {
return new HDPrivateKey().fromOptions(options);
};
HDPrivateKey.prototype.__defineGetter__('hdPublicKey', function() {
/**
* Get HD public key.
* @returns {HDPublicKey}
*/
HDPrivateKey.prototype.toPublic = function toPublic() {
var key = this._hdPublicKey;
if (!key) {
@ -139,17 +140,27 @@ HDPrivateKey.prototype.__defineGetter__('hdPublicKey', function() {
}
return key;
});
};
HDPrivateKey.prototype.__defineGetter__('xprivkey', function() {
/**
* Get cached base58 xprivkey.
* @returns {Base58String}
*/
HDPrivateKey.prototype.xprivkey = function xprivkey() {
if (!this._xprivkey)
this._xprivkey = this.toBase58();
return this._xprivkey;
});
};
HDPrivateKey.prototype.__defineGetter__('xpubkey', function() {
return this.hdPublicKey.xpubkey;
});
/**
* Get cached base58 xpubkey.
* @returns {Base58String}
*/
HDPrivateKey.prototype.xpubkey = function xpubkey() {
return this.toPublic().xpubkey();
};
/**
* Destroy the key (zeroes chain code, privkey, and pubkey).
@ -755,7 +766,7 @@ HDPrivateKey.fromRaw = function fromRaw(raw) {
HDPrivateKey.prototype.toJSON = function toJSON() {
return {
xprivkey: this.xprivkey,
xprivkey: this.xprivkey(),
mnemonic: this.mnemonic ? this.mnemonic.toJSON() : null
};
};

View File

@ -36,7 +36,6 @@ var FINGER_PRINT = new Buffer('00000000', 'hex');
* @param {Buffer?} options.chainCode
* @param {Buffer?} options.publicKey
* @property {Network} network
* @property {Base58String} xpubkey
* @property {Number} depth
* @property {Buffer} parentFingerPrint
* @property {Number} childIndex
@ -59,9 +58,6 @@ function HDPublicKey(options) {
this._xpubkey = null;
this.hdPublicKey = this;
this.hdPrivateKey = null;
if (options)
this.fromOptions(options);
}
@ -107,11 +103,34 @@ HDPublicKey.fromOptions = function fromOptions(options) {
return new HDPublicKey().fromOptions(options);
};
HDPublicKey.prototype.__defineGetter__('xpubkey', function() {
/**
* Get HD public key (self).
* @returns {HDPublicKey}
*/
HDPublicKey.prototype.toPublic = function toPublic() {
return this;
};
/**
* Get cached base58 xprivkey (always null here).
* @returns {null}
*/
HDPublicKey.prototype.xprivkey = function xprivkey() {
return null;
};
/**
* Get cached base58 xpubkey.
* @returns {Base58String}
*/
HDPublicKey.prototype.xpubkey = function() {
if (!this._xpubkey)
this._xpubkey = this.toBase58();
return this._xpubkey;
});
};
/**
* Destroy the key (zeroes chain code and pubkey).
@ -376,7 +395,7 @@ HDPublicKey.prototype.compare = function compare(key) {
HDPublicKey.prototype.toJSON = function toJSON() {
return {
xpubkey: this.xpubkey
xpubkey: this.xpubkey()
};
};

View File

@ -814,9 +814,9 @@ Account.prototype.inspect = function inspect() {
nestedAddress: this.initialized && this.nested
? this.nested.getAddress()
: null,
accountKey: this.accountKey.xpubkey,
accountKey: this.accountKey.toBase58(),
keys: this.keys.map(function(key) {
return key.xpubkey;
return key.toBase58();
})
};
};
@ -852,9 +852,9 @@ Account.prototype.toJSON = function toJSON(minimal) {
changeAddress: this.change
? this.change.getAddress('base58')
: null,
accountKey: this.accountKey.xpubkey,
accountKey: this.accountKey.toBase58(),
keys: this.keys.map(function(key) {
return key.xpubkey;
return key.toBase58();
})
};
};

View File

@ -705,7 +705,7 @@ Wallet.prototype._createAccount = co(function* createAccount(options, passphrase
} else {
assert(this.master.key);
key = this.master.key.deriveAccount44(this.accountDepth);
key = key.hdPublicKey;
key = key.toPublic();
}
options = {

View File

@ -99,43 +99,43 @@ describe('HD', function() {
it('should create master private key', function() {
master = bcoin.hd.PrivateKey.fromSeed(new Buffer(seed, 'hex'));
assert.equal(master.xprivkey, master_priv);
assert.equal(master.xpubkey, master_pub);
assert.equal(master.toBase58(), master_priv);
assert.equal(master.toPublic().toBase58(), master_pub);
});
it('should derive(0) child from master', function() {
child1 = master.derive(0);
assert.equal(child1.xprivkey, child1_priv);
assert.equal(child1.xpubkey, child1_pub);
assert.equal(child1.toBase58(), child1_priv);
assert.equal(child1.toPublic().toBase58(), child1_pub);
});
it('should derive(1) child from master public key', function() {
child2 = master.hdPublicKey.derive(1);
assert.equal(child2.xpubkey, child2_pub);
child2 = master.toPublic().derive(1);
assert.equal(child2.toBase58(), child2_pub);
});
it('should derive(1) child from master', function() {
child3 = master.derive(1);
assert.equal(child3.xprivkey, child3_priv);
assert.equal(child3.xpubkey, child3_pub);
assert.equal(child3.toBase58(), child3_priv);
assert.equal(child3.toPublic().toBase58(), child3_pub);
});
it('should derive(2) child from master', function() {
child4 = master.derive(2);
assert.equal(child4.xprivkey, child4_priv);
assert.equal(child4.xpubkey, child4_pub);
assert.equal(child4.toBase58(), child4_priv);
assert.equal(child4.toPublic().toBase58(), child4_pub);
});
it('should derive(0) child from child(2)', function() {
child5 = child4.derive(0);
assert.equal(child5.xprivkey, child5_priv);
assert.equal(child5.xpubkey, child5_pub);
assert.equal(child5.toBase58(), child5_priv);
assert.equal(child5.toPublic().toBase58(), child5_pub);
});
it('should derive(1) child from child(2)', function() {
child6 = child4.derive(1);
assert.equal(child6.xprivkey, child6_priv);
assert.equal(child6.xpubkey, child6_pub);
assert.equal(child6.toBase58(), child6_priv);
assert.equal(child6.toPublic().toBase58(), child6_pub);
});
it('should derive correctly when private key has leading zeros', function() {
@ -147,16 +147,16 @@ describe('HD', function() {
});
it('should deserialize master private key', function() {
bcoin.hd.PrivateKey.fromBase58(master.xprivkey);
bcoin.hd.PrivateKey.fromBase58(master.toBase58());
});
it('should deserialize master public key', function() {
bcoin.hd.PublicKey.fromBase58(master.hdPublicKey.xpubkey);
bcoin.hd.PublicKey.fromBase58(master.toPublic().toBase58());
});
it('should deserialize and reserialize', function() {
var key = bcoin.hd.fromMnemonic();
assert.equal(bcoin.hd.fromJSON(key.toJSON()).xprivkey, key.xprivkey);
assert.equal(bcoin.hd.fromJSON(key.toJSON()).toBase58(), key.toBase58());
});
function ub58(data) {
@ -176,8 +176,8 @@ describe('HD', function() {
delete vector.m;
it('should create from a seed', function() {
master = bcoin.hd.PrivateKey.fromSeed(new Buffer(seed, 'hex'));
equal(master.xprivkey, m.prv);
equal(master.xpubkey, m.pub);
equal(master.toBase58(), m.prv);
equal(master.toPublic().toBase58(), m.pub);
});
Object.keys(vector).forEach(function(path) {
var data = vector[path];
@ -185,8 +185,8 @@ describe('HD', function() {
var xpub = data.pub;
it('should derive ' + path + ' from master', function() {
var key = master.derive(path);
equal(key.xprivkey, xpriv);
equal(key.xpubkey, xpub);
equal(key.toBase58(), xpriv);
equal(key.toPublic().toBase58(), xpub);
});
});
});

View File

@ -23,7 +23,7 @@ describe('Mnemonic', function() {
assert.equal(mnemonic.getPhrase(), phrase);
assert.equal(mnemonic.toSeed().toString('hex'), seed.toString('hex'));
var key = bcoin.hd.fromMnemonic(mnemonic);
assert.equal(key.xprivkey, xpriv);
assert.equal(key.toBase58(), xpriv);
});
});
@ -42,7 +42,7 @@ describe('Mnemonic', function() {
assert.equal(mnemonic.getPhrase(), phrase);
assert.equal(mnemonic.toSeed().toString('hex'), seed.toString('hex'));
var key = bcoin.hd.fromMnemonic(mnemonic);
assert.equal(key.xprivkey, xpriv);
assert.equal(key.toBase58(), xpriv);
});
});

View File

@ -105,8 +105,8 @@ describe('Wallet', function() {
assert(w1 !== w2);
assert(w1.master !== w2.master);
assert.equal(w1.master.key.xprivkey, w2.master.key.xprivkey);
assert.equal(w1.account.accountKey.xpubkey, w2.account.accountKey.xpubkey);
assert.equal(w1.master.key.toBase58(), w2.master.key.toBase58());
assert.equal(w1.account.accountKey.toBase58(), w2.account.accountKey.toBase58());
}));
var p2pkh = co(function* p2pkh(witness, bullshitNesting) {
@ -169,7 +169,7 @@ describe('Wallet', function() {
n: 2
});
k = bcoin.hd.fromMnemonic().deriveAccount44(0).hdPublicKey;
k = bcoin.hd.fromMnemonic().deriveAccount44(0).toPublic();
yield w.addSharedKey(k);
@ -872,7 +872,7 @@ describe('Wallet', function() {
account = yield w.getAccount('foo');
assert.equal(account.name, 'foo');
assert.equal(account.accountIndex, 1);
assert(account.accountKey.xpubkey === acc.accountKey.xpubkey);
assert(account.accountKey.toBase58() === acc.accountKey.toBase58());
assert(w.account.accountIndex === 0);
assert.notEqual(