From 2fdfdd3c8a6ba36e5e821a5d403a87a81034cd87 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 17 Aug 2016 04:22:47 -0700 Subject: [PATCH] walletdb: store addr type on path. --- lib/bcoin/keyring.js | 21 +++++++++++++++++++++ lib/bcoin/walletdb.js | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/lib/bcoin/keyring.js b/lib/bcoin/keyring.js index c7008209..26155e87 100644 --- a/lib/bcoin/keyring.js +++ b/lib/bcoin/keyring.js @@ -687,6 +687,27 @@ KeyRing.prototype.derive = function derive(key) { return key.derive(this.change).derive(this.index); }; +/** + * Get script type. + * @returns {ScriptType} + */ + +KeyRing.prototype.getScriptType = function getScriptType() { + switch (this.type) { + case KeyRing.types.PUBKEYHASH: + return this.witness + ? scriptTypes.WITNESSPUBKEYHASH + : scriptTypes.PUBKEYHASH; + case KeyRing.types.MULTISIG: + return this.witness + ? scriptTypes.WITNESSSCRIPTHASH + : scriptTypes.SCRIPTHASH; + default: + assert(false, 'Bad keyring type.'); + break; + } +}; + KeyRing.prototype.__defineGetter__('publicKey', function() { return this.getPublicKey(); }); diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index 26fc85b8..dc418436 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -1561,7 +1561,11 @@ function Path() { this.change = 0; this.index = 0; - // NOTE: Passed in by caller. + // Currently unused. + this.type = bcoin.script.types.PUBKEYHASH; + this.version = -1; + + // Passed in by caller. this.id = null; this.hash = null; } @@ -1574,11 +1578,18 @@ function Path() { Path.prototype.fromRaw = function fromRaw(data) { var p = new BufferReader(data); + this.wid = p.readU32(); this.name = p.readVarString('utf8'); this.account = p.readU32(); this.change = p.readU32(); this.index = p.readU32(); + this.version = p.readU8(); + this.type = p.readU8(); + + if (this.version === 0xff) + this.version = -1; + return this; }; @@ -1605,6 +1616,8 @@ Path.prototype.toRaw = function toRaw(writer) { p.writeU32(this.account); p.writeU32(this.change); p.writeU32(this.index); + p.writeU8(this.version === -1 ? 0xff : this.version); + p.writeU8(this.type); if (!writer) p = p.render(); @@ -1621,11 +1634,17 @@ Path.prototype.toRaw = function toRaw(writer) { Path.prototype.fromKeyRing = function fromKeyRing(address) { this.wid = address.wid; - this.id = address.id; this.name = address.name; this.account = address.account; this.change = address.change; this.index = address.index; + + this.version = address.witness ? 0 : -1; + this.type = address.getScriptType(); + + this.id = address.id; + this.hash = address.getHash('hex'); + return this; }; @@ -1645,12 +1664,21 @@ Path.fromKeyRing = function fromKeyRing(address) { * @returns {String} */ -Path.prototype.toPath = function() { +Path.prototype.toPath = function toPath() { return 'm/' + this.account + '\'/' + this.change + '/' + this.index; }; +/** + * Convert path object to an address (currently unused). + * @returns {Address} + */ + +Path.prototype.toAddress = function toAddress() { + return bcoin.address.fromHash(this.hash, this.type, this.version); +}; + /** * Convert path to a json-friendly object. * @returns {Object}