From 402b47a940eea53618363fc5d1c0afef987ba492 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 27 Jun 2016 16:51:42 -0700 Subject: [PATCH] address consistency. --- lib/bcoin/input.js | 50 ++++++++++++++++++++++++++++++++++++ lib/bcoin/keyring.js | 38 +++++++++++++++++++--------- lib/bcoin/mtx.js | 9 ++++--- lib/bcoin/wallet.js | 28 ++++++++++++--------- test/wallet-test.js | 60 ++++++++++++++++++++++---------------------- 5 files changed, 128 insertions(+), 57 deletions(-) diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index dc807c64..a51a200d 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -607,6 +607,56 @@ Input.fromExtended = function fromExtended(data, enc) { return new Input().fromExtended(data); }; +/** + * Inject properties from coin. + * @private + * @param {Coin} coin + */ + +Input.prototype.fromCoin = function fromCoin(coin) { + assert(typeof coin.hash === 'string'); + assert(typeof coin.index === 'number'); + this.prevout = new Outpoint(coin.hash, coin.index); + this.script = new bcoin.script(); + this.sequence = 0xffffffff; + this.witness = new bcoin.witness(); + this.coin = coin; + return this; +}; + +/** + * Instantiate input from coin. + * @param {Coin} + * @returns {Input} + */ + +Input.fromCoin = function fromCoin(coin) { + return new Input().fromCoin(coin); +}; + +/** + * Inject properties from transaction. + * @private + * @param {TX} tx + * @param {Number} index + */ + +Input.prototype.fromTX = function fromTX(tx, index) { + var coin = bcoin.coin.fromTX(tx, index); + return this.fromCoin(coin); +}; + +/** + * Instantiate input from tx. + * @param {TX} tx + * @param {Number} index + * @returns {Input} + */ + +Input.fromTX = function fromTX(tx, index) { + return new Input().fromTX(tx, index); +}; + /** * Test an object to see if it is an Input. * @param {Object} obj diff --git a/lib/bcoin/keyring.js b/lib/bcoin/keyring.js index abbf2584..76d1990f 100644 --- a/lib/bcoin/keyring.js +++ b/lib/bcoin/keyring.js @@ -228,10 +228,11 @@ KeyRing.prototype.getProgramHash = function getProgramHash(enc) { /** * Get address' scripthash address for witness program. - * @returns {Base58Address} + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} */ -KeyRing.prototype.getProgramAddress = function getProgramAddress() { +KeyRing.prototype.getProgramAddress = function getProgramAddress(enc) { var hash, address; if (!this.witness) @@ -243,6 +244,9 @@ KeyRing.prototype.getProgramAddress = function getProgramAddress() { this._programAddress = address; } + if (enc === 'base58') + return this._programAddress.toBase58(); + return this._programAddress; }; @@ -296,10 +300,11 @@ KeyRing.prototype.getScriptHash256 = function getScriptHash256(enc) { /** * Get scripthash address. - * @returns {Base58Address} + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} */ -KeyRing.prototype.getScriptAddress = function getScriptAddress() { +KeyRing.prototype.getScriptAddress = function getScriptAddress(enc) { var hash, address; if (this.type !== 'multisig') @@ -316,6 +321,9 @@ KeyRing.prototype.getScriptAddress = function getScriptAddress() { this._scriptAddress = address; } + if (enc === 'base58') + return this._scriptAddress.toBase58(); + return this._scriptAddress; }; @@ -336,10 +344,11 @@ KeyRing.prototype.getKeyHash = function getKeyHash(enc) { /** * Get pubkeyhash address. - * @returns {Base58Address} + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} */ -KeyRing.prototype.getKeyAddress = function getKeyAddress() { +KeyRing.prototype.getKeyAddress = function getKeyAddress(enc) { var hash, address; if (!this._keyAddress) { @@ -351,20 +360,24 @@ KeyRing.prototype.getKeyAddress = function getKeyAddress() { this._keyAddress = address; } + if (enc === 'base58') + return this._keyAddress.toBase58(); + return this._keyAddress; }; /** * Compile a hash to an address. + * @private * @param {Hash|Buffer} hash * @param {AddressType?} type * @param {Number?} version - Witness version. - * @returns {Base58Address} + * @returns {Address} * @throws Error on bad hash/prefix. */ KeyRing.prototype.compile = function compile(hash, type, version) { - return bcoin.address.fromHash(hash, type, version, this.network).toBase58(); + return bcoin.address.fromHash(hash, type, version, this.network); }; /** @@ -381,13 +394,14 @@ KeyRing.prototype.getHash = function getHash(enc) { /** * Get base58 address. - * @returns {Base58Address} + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} */ -KeyRing.prototype.getAddress = function getAddress() { +KeyRing.prototype.getAddress = function getAddress(enc) { if (this.type === 'multisig') - return this.getScriptAddress(); - return this.getKeyAddress(); + return this.getScriptAddress(enc); + return this.getKeyAddress(enc); }; /** diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index 57a0f4d7..f262c8fb 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -156,7 +156,7 @@ MTX.prototype.addInput = function addInput(options, index) { assert(options.prevout); - input = bcoin.input(options, true); + input = new bcoin.input(options, true); this.inputs.push(input); @@ -706,7 +706,10 @@ MTX.prototype.addOutput = function addOutput(address, value) { address = address.getAddress(); } - if (typeof address === 'string') { + if (typeof address === 'string') + address = bcoin.address.fromBase58(address); + + if (address instanceof bcoin.address) { options = { address: address, value: value @@ -715,7 +718,7 @@ MTX.prototype.addOutput = function addOutput(address, value) { options = address; } - output = bcoin.output(options, true); + output = new bcoin.output(options, true); if (options.address) output.script = Script.fromAddress(options.address); diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 7a8d142d..127d0e20 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -1227,13 +1227,14 @@ Wallet.prototype.getScriptHash256 = function getScriptHash256(enc) { /** * Get scripthash address for current receiving address. - * @returns {Base58Address} + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} */ -Wallet.prototype.getScriptAddress = function getScriptAddress() { +Wallet.prototype.getScriptAddress = function getScriptAddress(enc) { if (!this.receiveAddress) return; - return this.receiveAddress.getScriptAddress(); + return this.receiveAddress.getScriptAddress(enc); }; /** @@ -1263,13 +1264,14 @@ Wallet.prototype.getProgramHash = function getProgramHash(enc) { /** * Get current receiving address' * scripthash address for witness program. - * @returns {Base58Address} + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} */ -Wallet.prototype.getProgramAddress = function getProgramAddress() { +Wallet.prototype.getProgramAddress = function getProgramAddress(enc) { if (!this.receiveAddress) return; - return this.receiveAddress.getProgramAddress(); + return this.receiveAddress.getProgramAddress(enc); }; /** @@ -1286,13 +1288,14 @@ Wallet.prototype.getKeyHash = function getKeyHash(enc) { /** * Get pubkeyhash address for current receiving address. - * @returns {Base58Address} + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} */ -Wallet.prototype.getKeyAddress = function getKeyAddress() { +Wallet.prototype.getKeyAddress = function getKeyAddress(enc) { if (!this.receiveAddress) return; - return this.receiveAddress.getKeyAddress(); + return this.receiveAddress.getKeyAddress(enc); }; /** @@ -1309,13 +1312,14 @@ Wallet.prototype.getHash = function getHash(enc) { /** * Get base58 address for current receiving address. - * @returns {Base58Address} + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} */ -Wallet.prototype.getAddress = function getAddress() { +Wallet.prototype.getAddress = function getAddress(enc) { if (!this.receiveAddress) return; - return this.receiveAddress.getAddress(); + return this.receiveAddress.getAddress(enc); }; Wallet.prototype.__defineGetter__('publicKey', function() { diff --git a/test/wallet-test.js b/test/wallet-test.js index 9d5b4391..eaf7a967 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -65,7 +65,7 @@ describe('Wallet', function() { it('should generate new key and address', function() { walletdb.create(function(err, w) { assert.ifError(err); - var addr = w.getAddress(); + var addr = w.getAddress('base58'); assert(addr); assert(bcoin.address.validate(addr)); }); @@ -105,9 +105,9 @@ describe('Wallet', function() { assert.ifError(err); if (witness) - assert(bcoin.address.fromBase58(w.getAddress()).type === 'witnesspubkeyhash'); + assert(bcoin.address.fromBase58(w.getAddress('base58')).type === 'witnesspubkeyhash'); else - assert(bcoin.address.fromBase58(w.getAddress()).type === 'pubkeyhash'); + assert(bcoin.address.fromBase58(w.getAddress('base58')).type === 'pubkeyhash'); var src = bcoin.mtx({ outputs: [{ @@ -608,21 +608,21 @@ describe('Wallet', function() { // w3 = bcoin.wallet.fromJSON(w3.toJSON()); // Our p2sh address - var addr = w1.getAddress(); + var addr = w1.getAddress('base58'); if (witness) assert(bcoin.address.fromBase58(addr).type === 'witnessscripthash'); else assert(bcoin.address.fromBase58(addr).type === 'scripthash'); - assert.equal(w1.getAddress(), addr); - assert.equal(w2.getAddress(), addr); - assert.equal(w3.getAddress(), addr); + assert.equal(w1.getAddress('base58'), addr); + assert.equal(w2.getAddress('base58'), addr); + assert.equal(w3.getAddress('base58'), addr); - var paddr = w1.getProgramAddress(); - assert.equal(w1.getProgramAddress(), paddr); - assert.equal(w2.getProgramAddress(), paddr); - assert.equal(w3.getProgramAddress(), paddr); + var paddr = w1.getProgramAddress('base58'); + assert.equal(w1.getProgramAddress('base58'), paddr); + assert.equal(w2.getProgramAddress('base58'), paddr); + assert.equal(w3.getProgramAddress('base58'), paddr); // Add a shared unspent transaction to our wallets var utx = bcoin.mtx(); @@ -650,11 +650,11 @@ describe('Wallet', function() { assert.equal(w1.receiveDepth, 2); assert.equal(w1.changeDepth, 1); - assert(w1.getAddress() !== addr); - addr = w1.getAddress(); - assert.equal(w1.getAddress(), addr); - assert.equal(w2.getAddress(), addr); - assert.equal(w3.getAddress(), addr); + assert(w1.getAddress('base58') !== addr); + addr = w1.getAddress('base58'); + assert.equal(w1.getAddress('base58'), addr); + assert.equal(w2.getAddress('base58'), addr); + assert.equal(w3.getAddress('base58'), addr); // Create a tx requiring 2 signatures var send = bcoin.mtx(); @@ -673,10 +673,10 @@ describe('Wallet', function() { assert(send.verify(flags)); assert.equal(w1.changeDepth, 1); - var change = w1.changeAddress.getAddress(); - assert.equal(w1.changeAddress.getAddress(), change); - assert.equal(w2.changeAddress.getAddress(), change); - assert.equal(w3.changeAddress.getAddress(), change); + var change = w1.changeAddress.getAddress('base58'); + assert.equal(w1.changeAddress.getAddress('base58'), change); + assert.equal(w2.changeAddress.getAddress('base58'), change); + assert.equal(w3.changeAddress.getAddress('base58'), change); // Simulate a confirmation send.ps = 0; @@ -693,12 +693,12 @@ describe('Wallet', function() { assert.equal(w1.receiveDepth, 2); assert.equal(w1.changeDepth, 2); - assert(w1.getAddress() === addr); - assert(w1.changeAddress.getAddress() !== change); - change = w1.changeAddress.getAddress(); - assert.equal(w1.changeAddress.getAddress(), change); - assert.equal(w2.changeAddress.getAddress(), change); - assert.equal(w3.changeAddress.getAddress(), change); + assert(w1.getAddress('base58') === addr); + assert(w1.changeAddress.getAddress('base58') !== change); + change = w1.changeAddress.getAddress('base58'); + assert.equal(w1.changeAddress.getAddress('base58'), change); + assert.equal(w2.changeAddress.getAddress('base58'), change); + assert.equal(w3.changeAddress.getAddress('base58'), change); if (witness) { send.inputs[0].witness.items[2] = new Buffer([]); @@ -713,8 +713,8 @@ describe('Wallet', function() { // w3 = bcoin.wallet.fromJSON(w3.toJSON()); // assert.equal(w3.receiveDepth, 2); // assert.equal(w3.changeDepth, 2); - //assert.equal(w3.getAddress(), addr); - //assert.equal(w3.changeAddress.getAddress(), change); + //assert.equal(w3.getAddress('base58'), addr); + //assert.equal(w3.changeAddress.getAddress('base58'), change); cb(); }); @@ -819,8 +819,8 @@ describe('Wallet', function() { assert(account !== acc); assert(account.accountKey.xpubkey === acc.accountKey.xpubkey); assert(w1.account.accountIndex === 0); - assert(account.receiveAddress.getAddress() !== w1.account.receiveAddress.getAddress()); - assert(w1.getAddress() === w1.account.receiveAddress.getAddress()); + assert(account.receiveAddress.getAddress('base58') !== w1.account.receiveAddress.getAddress('base58')); + assert(w1.getAddress('base58') === w1.account.receiveAddress.getAddress('base58')); // Coinbase var t1 = bcoin.mtx()