wallet: drop wallet getters.

This commit is contained in:
Christopher Jeffrey 2016-11-30 22:14:23 -08:00
parent bffdd78009
commit 81e71e7922
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
9 changed files with 137 additions and 359 deletions

View File

@ -3137,7 +3137,7 @@ RPC.prototype.getwalletinfo = co(function* getwalletinfo(args) {
walletversion: 6, walletversion: 6,
balance: Amount.btc(balance.unconfirmed, true), balance: Amount.btc(balance.unconfirmed, true),
unconfirmed_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, keypoololdest: 0,
keypoolsize: 0, keypoolsize: 0,
unlocked_until: this.wallet.master.until, unlocked_until: this.wallet.master.until,

View File

@ -610,14 +610,16 @@ KeyRing.prototype.ownHash = function ownHash(hash) {
if (!hash) if (!hash)
return false; return false;
if (util.equal(hash, this.keyHash)) if (util.equal(hash, this.getKeyHash()))
return true; return true;
if (util.equal(hash, this.scriptHash)) if (this.script) {
return true; if (util.equal(hash, this.getScriptHash()))
return true;
}
if (this.witness) { if (this.witness) {
if (util.equal(hash, this.nestedHash)) if (util.equal(hash, this.getNestedHash()))
return true; return true;
} }
@ -672,16 +674,16 @@ KeyRing.prototype.ownOutput = function ownOutput(tx, index) {
*/ */
KeyRing.prototype.getRedeem = function(hash) { KeyRing.prototype.getRedeem = function(hash) {
if (this.program) { if (this.witness) {
if (util.equal(hash, this.nestedHash)) if (util.equal(hash, this.getNestedHash()))
return this.program; return this.getProgram();
} }
if (this.script) { if (this.script) {
if (util.equal(hash, this.scriptHash160)) if (util.equal(hash, this.getScriptHash160()))
return this.script; return this.script;
if (util.equal(hash, this.scriptHash256)) if (util.equal(hash, this.getScriptHash256()))
return this.script; return this.script;
} }
@ -746,62 +748,6 @@ KeyRing.prototype.getType = function getType() {
return Script.types.PUBKEYHASH; 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. * Inspect keyring.
* @returns {Object} * @returns {Object}
@ -823,8 +769,8 @@ KeyRing.prototype.toJSON = function toJSON() {
nested: this.nested, nested: this.nested,
publicKey: this.publicKey.toString('hex'), publicKey: this.publicKey.toString('hex'),
script: this.script ? this.script.toRaw().toString('hex') : null, script: this.script ? this.script.toRaw().toString('hex') : null,
program: this.program ? this.program.toRaw().toString('hex') : null, program: this.witness ? this.getProgram().toRaw().toString('hex') : null,
type: constants.scriptTypesByVal[this.type].toLowerCase(), type: constants.scriptTypesByVal[this.getType()].toLowerCase(),
address: this.getAddress('base58') address: this.getAddress('base58')
}; };
}; };

View File

@ -270,7 +270,7 @@ MTX.prototype.scriptInput = function scriptInput(index, ring) {
// P2WPKH nested within pay-to-scripthash. // P2WPKH nested within pay-to-scripthash.
if (redeem.isWitnessPubkeyhash()) { if (redeem.isWitnessPubkeyhash()) {
prev = Script.fromPubkeyhash(ring.keyHash); prev = Script.fromPubkeyhash(ring.getKeyHash());
if (!this.scriptVector(prev, input.witness, ring)) if (!this.scriptVector(prev, input.witness, ring))
return false; return false;
@ -357,7 +357,7 @@ MTX.prototype.scriptVector = function scriptVector(prev, vector, ring) {
// P2PKH // P2PKH
if (prev.isPubkeyhash()) { if (prev.isPubkeyhash()) {
if (!util.equal(prev.get(2), ring.keyHash)) if (!util.equal(prev.get(2), ring.getKeyHash()))
return false; return false;
vector.set(0, opcodes.OP_0); vector.set(0, opcodes.OP_0);
@ -504,7 +504,7 @@ MTX.prototype.signVector = function signVector(prev, vector, sig, ring) {
// P2PKH // P2PKH
if (prev.isPubkeyhash()) { if (prev.isPubkeyhash()) {
// Make sure the pubkey hash is ours. // 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; return false;
// Already signed. // Already signed.

View File

@ -787,6 +787,52 @@ Account.prototype.setLookahead = co(function* setLookahead(lookahead) {
this.save(); 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. * Convert the account to a more inspection-friendly object.
* @returns {Object} * @returns {Object}

View File

@ -2352,258 +2352,44 @@ Wallet.prototype.ensureIndex = co(function* ensureIndex(acct, enforce) {
}); });
/** /**
* Get public key for current receiving address. * Get current receive 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.
* @param {String?} enc - `"base58"` or `null`. * @param {String?} enc - `"base58"` or `null`.
* @returns {Address|Base58Address} * @returns {Address|Base58Address}
*/ */
Wallet.prototype.getAddress = function getAddress(enc) { Wallet.prototype.getAddress = function getAddress(enc) {
if (!this.receive) return this.account.getAddress(enc);
return;
return this.receive.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() { Wallet.prototype.getReceive = function getReceive(enc) {
return this.getScript(); 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() { Wallet.prototype.getChange = function getChange(enc) {
return this.getScriptHash160(); 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() { Wallet.prototype.getNested = function getNested(enc) {
return this.getScriptAddress(); return this.account.getNested(enc);
}); };
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;
});
/** /**
* Convert the wallet to a more inspection-friendly object. * Convert the wallet to a more inspection-friendly object.
@ -2619,7 +2405,7 @@ Wallet.prototype.inspect = function inspect() {
accountDepth: this.accountDepth, accountDepth: this.accountDepth,
token: this.token.toString('hex'), token: this.token.toString('hex'),
tokenDepth: this.tokenDepth, tokenDepth: this.tokenDepth,
state: this.state ? this.state.toJSON(true) : null, state: this.txdb.state ? this.txdb.state.toJSON(true) : null,
master: this.master, master: this.master,
account: this.account account: this.account
}; };
@ -2643,7 +2429,7 @@ Wallet.prototype.toJSON = function toJSON(unsafe) {
accountDepth: this.accountDepth, accountDepth: this.accountDepth,
token: this.token.toString('hex'), token: this.token.toString('hex'),
tokenDepth: this.tokenDepth, tokenDepth: this.tokenDepth,
state: this.state.toJSON(true), state: this.txdb.state.toJSON(true),
master: this.master.toJSON(unsafe), master: this.master.toJSON(unsafe),
account: this.account.toJSON(true) account: this.account.toJSON(true)
}; };

View File

@ -131,8 +131,8 @@ WalletKey.prototype.toJSON = function toJSON() {
nested: this.nested, nested: this.nested,
publicKey: this.publicKey.toString('hex'), publicKey: this.publicKey.toString('hex'),
script: this.script ? this.script.toRaw().toString('hex') : null, script: this.script ? this.script.toRaw().toString('hex') : null,
program: this.program ? this.program.toRaw().toString('hex') : null, program: this.witness ? this.getProgram().toRaw().toString('hex') : null,
type: constants.scriptTypesByVal[this.type].toLowerCase(), type: constants.scriptTypesByVal[this.getType()].toLowerCase(),
address: this.getAddress('base58') address: this.getAddress('base58')
}; };
}; };

View File

@ -35,12 +35,12 @@ describe('Chain', function() {
redeemer = bcoin.mtx(); redeemer = bcoin.mtx();
redeemer.addOutput({ redeemer.addOutput({
address: wallet.receive.getAddress(), address: wallet.getReceive(),
value: 25 * 1e8 value: 25 * 1e8
}); });
redeemer.addOutput({ redeemer.addOutput({
address: wallet.change.getAddress(), address: wallet.getChange(),
value: 5 * 1e8 value: 5 * 1e8
}); });
@ -64,7 +64,7 @@ describe('Chain', function() {
it('should open walletdb', cob(function* () { it('should open walletdb', cob(function* () {
wallet = yield walletdb.create(); wallet = yield walletdb.create();
miner.addresses.length = 0; miner.addresses.length = 0;
miner.addAddress(wallet.getAddress()); miner.addAddress(wallet.getReceive());
})); }));
it('should mine a block', cob(function* () { it('should mine a block', cob(function* () {
@ -447,7 +447,7 @@ describe('Chain', function() {
it('should rescan for transactions', cob(function* () { it('should rescan for transactions', cob(function* () {
yield walletdb.rescan(0); yield walletdb.rescan(0);
assert.equal(wallet.state.confirmed, 1289250000000); assert.equal(wallet.txdb.state.confirmed, 1289250000000);
})); }));
it('should cleanup', cob(function* () { it('should cleanup', cob(function* () {

View File

@ -241,12 +241,12 @@ describe('Mempool', function() {
.addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 50000)
.addOutput(w.getAddress(), 10000); .addOutput(w.getAddress(), 10000);
prev = new bcoin.script([0, kp.keyHash]); prev = new bcoin.script([0, kp.getKeyHash()]);
prevHash = crypto.randomBytes(32).toString('hex'); prevHash = crypto.randomBytes(32).toString('hex');
tx.addInput(dummy(prev, prevHash)); 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 = tx.signature(0, prevs, kp.privateKey, 'all', 1);
sig[sig.length - 1] = 0; sig[sig.length - 1] = 0;
@ -303,7 +303,7 @@ describe('Mempool', function() {
.addOutput(w.getAddress(), 50000) .addOutput(w.getAddress(), 50000)
.addOutput(w.getAddress(), 10000); .addOutput(w.getAddress(), 10000);
prev = new bcoin.script([0, kp.keyHash]); prev = new bcoin.script([0, kp.getKeyHash()]);
prevHash = crypto.randomBytes(32).toString('hex'); prevHash = crypto.randomBytes(32).toString('hex');
tx.addInput(dummy(prev, prevHash)); tx.addInput(dummy(prev, prevHash));

View File

@ -129,7 +129,7 @@ describe('Wallet', function() {
outputs: [{ outputs: [{
value: 5460 * 2, value: 5460 * 2,
address: bullshitNesting address: bullshitNesting
? w.getNestedAddress() ? w.getNested()
: w.getAddress() : w.getAddress()
}, { }, {
value: 5460 * 2, value: 5460 * 2,
@ -174,7 +174,7 @@ describe('Wallet', function() {
yield w.addSharedKey(k); yield w.addSharedKey(k);
keys = [ keys = [
w.getPublicKey(), w.account.receive.getPublicKey(),
k.derive('m/0/0').publicKey k.derive('m/0/0').publicKey
]; ];
@ -682,15 +682,15 @@ describe('Wallet', function() {
w3 = yield walletdb.create(options); w3 = yield walletdb.create(options);
receive = yield walletdb.create(); receive = yield walletdb.create();
yield w1.addSharedKey(w2.accountKey); yield w1.addSharedKey(w2.account.accountKey);
yield w1.addSharedKey(w3.accountKey); yield w1.addSharedKey(w3.account.accountKey);
yield w2.addSharedKey(w1.accountKey); yield w2.addSharedKey(w1.account.accountKey);
yield w2.addSharedKey(w3.accountKey); yield w2.addSharedKey(w3.account.accountKey);
yield w3.addSharedKey(w1.accountKey); yield w3.addSharedKey(w1.account.accountKey);
yield w3.addSharedKey(w2.accountKey); yield w3.addSharedKey(w2.account.accountKey);
// Our p2sh address // Our p2sh address
b58 = w1[rec].getAddress('base58'); b58 = w1.account[rec].getAddress('base58');
addr = bcoin.address.fromBase58(b58); addr = bcoin.address.fromBase58(b58);
if (witness) { if (witness) {
@ -702,14 +702,14 @@ describe('Wallet', function() {
assert.equal(addr.type, scriptTypes.SCRIPTHASH); assert.equal(addr.type, scriptTypes.SCRIPTHASH);
} }
assert.equal(w1[rec].getAddress('base58'), b58); assert.equal(w1.account[rec].getAddress('base58'), b58);
assert.equal(w2[rec].getAddress('base58'), b58); assert.equal(w2.account[rec].getAddress('base58'), b58);
assert.equal(w3[rec].getAddress('base58'), b58); assert.equal(w3.account[rec].getAddress('base58'), b58);
paddr = w1.getNestedAddress('base58'); paddr = w1.getNested('base58');
assert.equal(w1.getNestedAddress('base58'), paddr); assert.equal(w1.getNested('base58'), paddr);
assert.equal(w2.getNestedAddress('base58'), paddr); assert.equal(w2.getNested('base58'), paddr);
assert.equal(w3.getNestedAddress('base58'), paddr); assert.equal(w3.getNested('base58'), paddr);
// Add a shared unspent transaction to our wallets // Add a shared unspent transaction to our wallets
utx = bcoin.mtx(); utx = bcoin.mtx();
@ -728,19 +728,19 @@ describe('Wallet', function() {
utx.ts = block.ts; utx.ts = block.ts;
utx.index = 0; utx.index = 0;
assert.equal(w1[depth], 1); assert.equal(w1.account[depth], 1);
yield walletdb.addBlock(block, [utx]); 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); assert(w1.account[rec].getAddress('base58') !== b58);
b58 = w1[rec].getAddress('base58'); b58 = w1.account[rec].getAddress('base58');
assert.equal(w1[rec].getAddress('base58'), b58); assert.equal(w1.account[rec].getAddress('base58'), b58);
assert.equal(w2[rec].getAddress('base58'), b58); assert.equal(w2.account[rec].getAddress('base58'), b58);
assert.equal(w3[rec].getAddress('base58'), b58); assert.equal(w3.account[rec].getAddress('base58'), b58);
// Create a tx requiring 2 signatures // Create a tx requiring 2 signatures
send = bcoin.mtx(); send = bcoin.mtx();
@ -757,12 +757,12 @@ describe('Wallet', function() {
send = send.toTX(); send = send.toTX();
assert(send.verify(flags)); assert(send.verify(flags));
assert.equal(w1.changeDepth, 1); assert.equal(w1.account.changeDepth, 1);
change = w1.change.getAddress('base58'); change = w1.account.change.getAddress('base58');
assert.equal(w1.change.getAddress('base58'), change); assert.equal(w1.account.change.getAddress('base58'), change);
assert.equal(w2.change.getAddress('base58'), change); assert.equal(w2.account.change.getAddress('base58'), change);
assert.equal(w3.change.getAddress('base58'), change); assert.equal(w3.account.change.getAddress('base58'), change);
// Simulate a confirmation // Simulate a confirmation
var block = nextBlock(); var block = nextBlock();
@ -773,15 +773,15 @@ describe('Wallet', function() {
yield walletdb.addBlock(block, [send]); yield walletdb.addBlock(block, [send]);
assert.equal(w1[depth], 2); assert.equal(w1.account[depth], 2);
assert.equal(w1.changeDepth, 2); assert.equal(w1.account.changeDepth, 2);
assert(w1[rec].getAddress('base58') === b58); assert(w1.account[rec].getAddress('base58') === b58);
assert(w1.change.getAddress('base58') !== change); assert(w1.account.change.getAddress('base58') !== change);
change = w1.change.getAddress('base58'); change = w1.account.change.getAddress('base58');
assert.equal(w1.change.getAddress('base58'), change); assert.equal(w1.account.change.getAddress('base58'), change);
assert.equal(w2.change.getAddress('base58'), change); assert.equal(w2.account.change.getAddress('base58'), change);
assert.equal(w3.change.getAddress('base58'), change); assert.equal(w3.account.change.getAddress('base58'), change);
if (witness) { if (witness) {
send.inputs[0].witness.set(2, 0); send.inputs[0].witness.set(2, 0);