From 81e71e7922dae83498beeb1c27d2bf891a201982 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 30 Nov 2016 22:14:23 -0800 Subject: [PATCH] wallet: drop wallet getters. --- lib/http/rpc.js | 2 +- lib/primitives/keyring.js | 80 ++--------- lib/primitives/mtx.js | 6 +- lib/wallet/account.js | 46 +++++++ lib/wallet/wallet.js | 270 ++++---------------------------------- lib/wallet/walletkey.js | 4 +- test/chain-test.js | 8 +- test/mempool-test.js | 6 +- test/wallet-test.js | 74 +++++------ 9 files changed, 137 insertions(+), 359 deletions(-) diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 0a897ff1..83e1de5e 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -3137,7 +3137,7 @@ RPC.prototype.getwalletinfo = co(function* getwalletinfo(args) { walletversion: 6, balance: Amount.btc(balance.unconfirmed, true), unconfirmed_balance: Amount.btc(balance.unconfirmed, true), - txcount: this.wallet.state.tx, + txcount: this.wallet.txdb.state.tx, keypoololdest: 0, keypoolsize: 0, unlocked_until: this.wallet.master.until, diff --git a/lib/primitives/keyring.js b/lib/primitives/keyring.js index ae5fa7ad..c445fe58 100644 --- a/lib/primitives/keyring.js +++ b/lib/primitives/keyring.js @@ -610,14 +610,16 @@ KeyRing.prototype.ownHash = function ownHash(hash) { if (!hash) return false; - if (util.equal(hash, this.keyHash)) + if (util.equal(hash, this.getKeyHash())) return true; - if (util.equal(hash, this.scriptHash)) - return true; + if (this.script) { + if (util.equal(hash, this.getScriptHash())) + return true; + } if (this.witness) { - if (util.equal(hash, this.nestedHash)) + if (util.equal(hash, this.getNestedHash())) return true; } @@ -672,16 +674,16 @@ KeyRing.prototype.ownOutput = function ownOutput(tx, index) { */ KeyRing.prototype.getRedeem = function(hash) { - if (this.program) { - if (util.equal(hash, this.nestedHash)) - return this.program; + if (this.witness) { + if (util.equal(hash, this.getNestedHash())) + return this.getProgram(); } if (this.script) { - if (util.equal(hash, this.scriptHash160)) + if (util.equal(hash, this.getScriptHash160())) return this.script; - if (util.equal(hash, this.scriptHash256)) + if (util.equal(hash, this.getScriptHash256())) return this.script; } @@ -746,62 +748,6 @@ KeyRing.prototype.getType = function getType() { return Script.types.PUBKEYHASH; }; -/* - * Getters - */ - -KeyRing.prototype.__defineGetter__('type', function() { - return this.getType(); -}); - -KeyRing.prototype.__defineGetter__('version', function() { - return this.getVersion(); -}); - -KeyRing.prototype.__defineGetter__('scriptHash', function() { - return this.getScriptHash(); -}); - -KeyRing.prototype.__defineGetter__('scriptHash160', function() { - return this.getScriptHash160(); -}); - -KeyRing.prototype.__defineGetter__('scriptHash256', function() { - return this.getScriptHash256(); -}); - -KeyRing.prototype.__defineGetter__('scriptAddress', function() { - return this.getScriptAddress(); -}); - -KeyRing.prototype.__defineGetter__('program', function() { - return this.getProgram(); -}); - -KeyRing.prototype.__defineGetter__('nestedHash', function() { - return this.getNestedHash(); -}); - -KeyRing.prototype.__defineGetter__('nestedAddress', function() { - return this.getNestedAddress(); -}); - -KeyRing.prototype.__defineGetter__('keyHash', function() { - return this.getKeyHash(); -}); - -KeyRing.prototype.__defineGetter__('keyAddress', function() { - return this.getKeyAddress(); -}); - -KeyRing.prototype.__defineGetter__('hash', function() { - return this.getHash(); -}); - -KeyRing.prototype.__defineGetter__('address', function() { - return this.getAddress(); -}); - /** * Inspect keyring. * @returns {Object} @@ -823,8 +769,8 @@ KeyRing.prototype.toJSON = function toJSON() { nested: this.nested, publicKey: this.publicKey.toString('hex'), script: this.script ? this.script.toRaw().toString('hex') : null, - program: this.program ? this.program.toRaw().toString('hex') : null, - type: constants.scriptTypesByVal[this.type].toLowerCase(), + program: this.witness ? this.getProgram().toRaw().toString('hex') : null, + type: constants.scriptTypesByVal[this.getType()].toLowerCase(), address: this.getAddress('base58') }; }; diff --git a/lib/primitives/mtx.js b/lib/primitives/mtx.js index c6ad383e..f370dfd5 100644 --- a/lib/primitives/mtx.js +++ b/lib/primitives/mtx.js @@ -270,7 +270,7 @@ MTX.prototype.scriptInput = function scriptInput(index, ring) { // P2WPKH nested within pay-to-scripthash. if (redeem.isWitnessPubkeyhash()) { - prev = Script.fromPubkeyhash(ring.keyHash); + prev = Script.fromPubkeyhash(ring.getKeyHash()); if (!this.scriptVector(prev, input.witness, ring)) return false; @@ -357,7 +357,7 @@ MTX.prototype.scriptVector = function scriptVector(prev, vector, ring) { // P2PKH if (prev.isPubkeyhash()) { - if (!util.equal(prev.get(2), ring.keyHash)) + if (!util.equal(prev.get(2), ring.getKeyHash())) return false; vector.set(0, opcodes.OP_0); @@ -504,7 +504,7 @@ MTX.prototype.signVector = function signVector(prev, vector, sig, ring) { // P2PKH if (prev.isPubkeyhash()) { // Make sure the pubkey hash is ours. - if (!util.equal(ring.keyHash, prev.get(2))) + if (!util.equal(ring.getKeyHash(), prev.get(2))) return false; // Already signed. diff --git a/lib/wallet/account.js b/lib/wallet/account.js index 2ccb949d..e11c04ff 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -787,6 +787,52 @@ Account.prototype.setLookahead = co(function* setLookahead(lookahead) { this.save(); }); +/** + * Get current receive address. + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} + */ + +Account.prototype.getAddress = function getAddress(enc) { + return this.getReceive(enc); +}; + +/** + * Get current receive address. + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} + */ + +Account.prototype.getReceive = function getReceive(enc) { + if (!this.receive) + return; + return this.receive.getAddress(enc); +}; + +/** + * Get current change address. + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} + */ + +Account.prototype.getChange = function getChange(enc) { + if (!this.change) + return; + return this.change.getAddress(enc); +}; + +/** + * Get current nested address. + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} + */ + +Account.prototype.getNested = function getNested(enc) { + if (!this.nested) + return; + return this.nested.getAddress(enc); +}; + /** * Convert the account to a more inspection-friendly object. * @returns {Object} diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 18cf1b64..e472bec6 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -2352,258 +2352,44 @@ Wallet.prototype.ensureIndex = co(function* ensureIndex(acct, enforce) { }); /** - * Get public key for current receiving address. - * @param {String?} enc - `"hex"` or `null`. - * @returns {Buffer} - */ - -Wallet.prototype.getPublicKey = function getPublicKey(enc) { - if (!this.receive) - return; - return this.receive.getPublicKey(enc); -}; - -/** - * Get redeem script for current receiving address. - * @returns {Script} - */ - -Wallet.prototype.getScript = function getScript() { - if (!this.receive) - return; - return this.receive.getScript(); -}; - -/** - * Get scripthash for current receiving address. - * @param {String?} enc - `"hex"` or `null`. - * @returns {Buffer} - */ - -Wallet.prototype.getScriptHash = function getScriptHash(enc) { - if (!this.receive) - return; - return this.receive.getScriptHash(enc); -}; - -/** - * Get ripemd160 scripthash for current receiving address. - * @param {String?} enc - `"hex"` or `null`. - * @returns {Buffer} - */ - -Wallet.prototype.getScriptHash160 = function getScriptHash160(enc) { - if (!this.receive) - return; - return this.receive.getScriptHash160(enc); -}; - -/** - * Get sha256 scripthash for current receiving address. - * @param {String?} enc - `"hex"` or `null`. - * @returns {Buffer} - */ - -Wallet.prototype.getScriptHash256 = function getScriptHash256(enc) { - if (!this.receive) - return; - return this.receive.getScriptHash256(enc); -}; - -/** - * Get scripthash address for current receiving address. - * @param {String?} enc - `"base58"` or `null`. - * @returns {Address|Base58Address} - */ - -Wallet.prototype.getScriptAddress = function getScriptAddress(enc) { - if (!this.receive) - return; - return this.receive.getScriptAddress(enc); -}; - -/** - * Get witness program for current receiving address. - * @returns {Buffer} - */ - -Wallet.prototype.getProgram = function getProgram() { - if (!this.receive) - return; - return this.receive.getProgram(); -}; - -/** - * Get current receiving address' ripemd160 program - * scripthash (for witness programs behind a scripthash). - * @param {String?} enc - `"hex"` or `null`. - * @returns {Buffer} - */ - -Wallet.prototype.getNestedHash = function getNestedHash(enc) { - if (!this.nested) - return; - return this.nested.getHash(enc); -}; - -/** - * Get current receiving address' - * scripthash address for witness program. - * @param {String?} enc - `"base58"` or `null`. - * @returns {Address|Base58Address} - */ - -Wallet.prototype.getNestedAddress = function getNestedAddress(enc) { - if (!this.nested) - return; - return this.nested.getAddress(enc); -}; - -/** - * Get public key hash for current receiving address. - * @param {String?} enc - `"hex"` or `null`. - * @returns {Buffer} - */ - -Wallet.prototype.getKeyHash = function getKeyHash(enc) { - if (!this.receive) - return; - return this.receive.getKeyHash(enc); -}; - -/** - * Get pubkeyhash address for current receiving address. - * @param {String?} enc - `"base58"` or `null`. - * @returns {Address|Base58Address} - */ - -Wallet.prototype.getKeyAddress = function getKeyAddress(enc) { - if (!this.receive) - return; - return this.receive.getKeyAddress(enc); -}; - -/** - * Get hash for current receiving address. - * @param {String?} enc - `"hex"` or `null`. - * @returns {Buffer} - */ - -Wallet.prototype.getHash = function getHash(enc) { - if (!this.receive) - return; - return this.receive.getHash(enc); -}; - -/** - * Get base58 address for current receiving address. + * Get current receive address. * @param {String?} enc - `"base58"` or `null`. * @returns {Address|Base58Address} */ Wallet.prototype.getAddress = function getAddress(enc) { - if (!this.receive) - return; - return this.receive.getAddress(enc); + return this.account.getAddress(enc); }; -Wallet.prototype.__defineGetter__('publicKey', function() { - return this.getPublicKey(); -}); +/** + * Get current receive address. + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} + */ -Wallet.prototype.__defineGetter__('script', function() { - return this.getScript(); -}); +Wallet.prototype.getReceive = function getReceive(enc) { + return this.account.getReceive(enc); +}; -Wallet.prototype.__defineGetter__('scriptHash', function() { - return this.getScriptHash(); -}); +/** + * Get current change address. + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} + */ -Wallet.prototype.__defineGetter__('scriptHash160', function() { - return this.getScriptHash160(); -}); +Wallet.prototype.getChange = function getChange(enc) { + return this.account.getChange(enc); +}; -Wallet.prototype.__defineGetter__('scriptHash256', function() { - return this.getScriptHash256(); -}); +/** + * Get current nested address. + * @param {String?} enc - `"base58"` or `null`. + * @returns {Address|Base58Address} + */ -Wallet.prototype.__defineGetter__('scriptAddress', function() { - return this.getScriptAddress(); -}); - -Wallet.prototype.__defineGetter__('program', function() { - return this.getProgram(); -}); - -Wallet.prototype.__defineGetter__('nestedHash', function() { - return this.getNestedHash(); -}); - -Wallet.prototype.__defineGetter__('nestedAddress', function() { - return this.getNestedAddress(); -}); - -Wallet.prototype.__defineGetter__('keyHash', function() { - return this.getKeyHash(); -}); - -Wallet.prototype.__defineGetter__('keyAddress', function() { - return this.getKeyAddress(); -}); - -Wallet.prototype.__defineGetter__('hash', function() { - return this.getHash(); -}); - -Wallet.prototype.__defineGetter__('address', function() { - return this.getAddress(); -}); - -Wallet.prototype.__defineGetter__('receiveDepth', function() { - if (!this.account) - return -1; - return this.account.receiveDepth; -}); - -Wallet.prototype.__defineGetter__('changeDepth', function() { - if (!this.account) - return -1; - return this.account.changeDepth; -}); - -Wallet.prototype.__defineGetter__('nestedDepth', function() { - if (!this.account) - return -1; - return this.account.nestedDepth; -}); - -Wallet.prototype.__defineGetter__('accountKey', function() { - if (!this.account) - return; - return this.account.accountKey; -}); - -Wallet.prototype.__defineGetter__('receive', function() { - if (!this.account) - return; - return this.account.receive; -}); - -Wallet.prototype.__defineGetter__('change', function() { - if (!this.account) - return; - return this.account.change; -}); - -Wallet.prototype.__defineGetter__('nested', function() { - if (!this.account) - return; - return this.account.nested; -}); - -Wallet.prototype.__defineGetter__('state', function() { - return this.txdb.state; -}); +Wallet.prototype.getNested = function getNested(enc) { + return this.account.getNested(enc); +}; /** * Convert the wallet to a more inspection-friendly object. @@ -2619,7 +2405,7 @@ Wallet.prototype.inspect = function inspect() { accountDepth: this.accountDepth, token: this.token.toString('hex'), tokenDepth: this.tokenDepth, - state: this.state ? this.state.toJSON(true) : null, + state: this.txdb.state ? this.txdb.state.toJSON(true) : null, master: this.master, account: this.account }; @@ -2643,7 +2429,7 @@ Wallet.prototype.toJSON = function toJSON(unsafe) { accountDepth: this.accountDepth, token: this.token.toString('hex'), tokenDepth: this.tokenDepth, - state: this.state.toJSON(true), + state: this.txdb.state.toJSON(true), master: this.master.toJSON(unsafe), account: this.account.toJSON(true) }; diff --git a/lib/wallet/walletkey.js b/lib/wallet/walletkey.js index 29931034..1090225e 100644 --- a/lib/wallet/walletkey.js +++ b/lib/wallet/walletkey.js @@ -131,8 +131,8 @@ WalletKey.prototype.toJSON = function toJSON() { nested: this.nested, publicKey: this.publicKey.toString('hex'), script: this.script ? this.script.toRaw().toString('hex') : null, - program: this.program ? this.program.toRaw().toString('hex') : null, - type: constants.scriptTypesByVal[this.type].toLowerCase(), + program: this.witness ? this.getProgram().toRaw().toString('hex') : null, + type: constants.scriptTypesByVal[this.getType()].toLowerCase(), address: this.getAddress('base58') }; }; diff --git a/test/chain-test.js b/test/chain-test.js index 468cfad2..90e39058 100644 --- a/test/chain-test.js +++ b/test/chain-test.js @@ -35,12 +35,12 @@ describe('Chain', function() { redeemer = bcoin.mtx(); redeemer.addOutput({ - address: wallet.receive.getAddress(), + address: wallet.getReceive(), value: 25 * 1e8 }); redeemer.addOutput({ - address: wallet.change.getAddress(), + address: wallet.getChange(), value: 5 * 1e8 }); @@ -64,7 +64,7 @@ describe('Chain', function() { it('should open walletdb', cob(function* () { wallet = yield walletdb.create(); miner.addresses.length = 0; - miner.addAddress(wallet.getAddress()); + miner.addAddress(wallet.getReceive()); })); it('should mine a block', cob(function* () { @@ -447,7 +447,7 @@ describe('Chain', function() { it('should rescan for transactions', cob(function* () { yield walletdb.rescan(0); - assert.equal(wallet.state.confirmed, 1289250000000); + assert.equal(wallet.txdb.state.confirmed, 1289250000000); })); it('should cleanup', cob(function* () { diff --git a/test/mempool-test.js b/test/mempool-test.js index bb63e1bb..1b2fae7d 100644 --- a/test/mempool-test.js +++ b/test/mempool-test.js @@ -241,12 +241,12 @@ describe('Mempool', function() { .addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 10000); - prev = new bcoin.script([0, kp.keyHash]); + prev = new bcoin.script([0, kp.getKeyHash()]); prevHash = crypto.randomBytes(32).toString('hex'); tx.addInput(dummy(prev, prevHash)); - prevs = bcoin.script.fromPubkeyhash(kp.keyHash); + prevs = bcoin.script.fromPubkeyhash(kp.getKeyHash()); sig = tx.signature(0, prevs, kp.privateKey, 'all', 1); sig[sig.length - 1] = 0; @@ -303,7 +303,7 @@ describe('Mempool', function() { .addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 10000); - prev = new bcoin.script([0, kp.keyHash]); + prev = new bcoin.script([0, kp.getKeyHash()]); prevHash = crypto.randomBytes(32).toString('hex'); tx.addInput(dummy(prev, prevHash)); diff --git a/test/wallet-test.js b/test/wallet-test.js index 38480b95..9064b6c4 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -129,7 +129,7 @@ describe('Wallet', function() { outputs: [{ value: 5460 * 2, address: bullshitNesting - ? w.getNestedAddress() + ? w.getNested() : w.getAddress() }, { value: 5460 * 2, @@ -174,7 +174,7 @@ describe('Wallet', function() { yield w.addSharedKey(k); keys = [ - w.getPublicKey(), + w.account.receive.getPublicKey(), k.derive('m/0/0').publicKey ]; @@ -682,15 +682,15 @@ describe('Wallet', function() { w3 = yield walletdb.create(options); receive = yield walletdb.create(); - yield w1.addSharedKey(w2.accountKey); - yield w1.addSharedKey(w3.accountKey); - yield w2.addSharedKey(w1.accountKey); - yield w2.addSharedKey(w3.accountKey); - yield w3.addSharedKey(w1.accountKey); - yield w3.addSharedKey(w2.accountKey); + yield w1.addSharedKey(w2.account.accountKey); + yield w1.addSharedKey(w3.account.accountKey); + yield w2.addSharedKey(w1.account.accountKey); + yield w2.addSharedKey(w3.account.accountKey); + yield w3.addSharedKey(w1.account.accountKey); + yield w3.addSharedKey(w2.account.accountKey); // Our p2sh address - b58 = w1[rec].getAddress('base58'); + b58 = w1.account[rec].getAddress('base58'); addr = bcoin.address.fromBase58(b58); if (witness) { @@ -702,14 +702,14 @@ describe('Wallet', function() { assert.equal(addr.type, scriptTypes.SCRIPTHASH); } - assert.equal(w1[rec].getAddress('base58'), b58); - assert.equal(w2[rec].getAddress('base58'), b58); - assert.equal(w3[rec].getAddress('base58'), b58); + assert.equal(w1.account[rec].getAddress('base58'), b58); + assert.equal(w2.account[rec].getAddress('base58'), b58); + assert.equal(w3.account[rec].getAddress('base58'), b58); - paddr = w1.getNestedAddress('base58'); - assert.equal(w1.getNestedAddress('base58'), paddr); - assert.equal(w2.getNestedAddress('base58'), paddr); - assert.equal(w3.getNestedAddress('base58'), paddr); + paddr = w1.getNested('base58'); + assert.equal(w1.getNested('base58'), paddr); + assert.equal(w2.getNested('base58'), paddr); + assert.equal(w3.getNested('base58'), paddr); // Add a shared unspent transaction to our wallets utx = bcoin.mtx(); @@ -728,19 +728,19 @@ describe('Wallet', function() { utx.ts = block.ts; utx.index = 0; - assert.equal(w1[depth], 1); + assert.equal(w1.account[depth], 1); yield walletdb.addBlock(block, [utx]); - assert.equal(w1[depth], 2); + assert.equal(w1.account[depth], 2); - assert.equal(w1.changeDepth, 1); + assert.equal(w1.account.changeDepth, 1); - assert(w1[rec].getAddress('base58') !== b58); - b58 = w1[rec].getAddress('base58'); - assert.equal(w1[rec].getAddress('base58'), b58); - assert.equal(w2[rec].getAddress('base58'), b58); - assert.equal(w3[rec].getAddress('base58'), b58); + assert(w1.account[rec].getAddress('base58') !== b58); + b58 = w1.account[rec].getAddress('base58'); + assert.equal(w1.account[rec].getAddress('base58'), b58); + assert.equal(w2.account[rec].getAddress('base58'), b58); + assert.equal(w3.account[rec].getAddress('base58'), b58); // Create a tx requiring 2 signatures send = bcoin.mtx(); @@ -757,12 +757,12 @@ describe('Wallet', function() { send = send.toTX(); assert(send.verify(flags)); - assert.equal(w1.changeDepth, 1); + assert.equal(w1.account.changeDepth, 1); - change = w1.change.getAddress('base58'); - assert.equal(w1.change.getAddress('base58'), change); - assert.equal(w2.change.getAddress('base58'), change); - assert.equal(w3.change.getAddress('base58'), change); + change = w1.account.change.getAddress('base58'); + assert.equal(w1.account.change.getAddress('base58'), change); + assert.equal(w2.account.change.getAddress('base58'), change); + assert.equal(w3.account.change.getAddress('base58'), change); // Simulate a confirmation var block = nextBlock(); @@ -773,15 +773,15 @@ describe('Wallet', function() { yield walletdb.addBlock(block, [send]); - assert.equal(w1[depth], 2); - assert.equal(w1.changeDepth, 2); + assert.equal(w1.account[depth], 2); + assert.equal(w1.account.changeDepth, 2); - assert(w1[rec].getAddress('base58') === b58); - assert(w1.change.getAddress('base58') !== change); - change = w1.change.getAddress('base58'); - assert.equal(w1.change.getAddress('base58'), change); - assert.equal(w2.change.getAddress('base58'), change); - assert.equal(w3.change.getAddress('base58'), change); + assert(w1.account[rec].getAddress('base58') === b58); + assert(w1.account.change.getAddress('base58') !== change); + change = w1.account.change.getAddress('base58'); + assert.equal(w1.account.change.getAddress('base58'), change); + assert.equal(w2.account.change.getAddress('base58'), change); + assert.equal(w3.account.change.getAddress('base58'), change); if (witness) { send.inputs[0].witness.set(2, 0);