address consistency.

This commit is contained in:
Christopher Jeffrey 2016-06-27 16:51:42 -07:00
parent d11f4deef1
commit 402b47a940
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
5 changed files with 128 additions and 57 deletions

View File

@ -607,6 +607,56 @@ Input.fromExtended = function fromExtended(data, enc) {
return new Input().fromExtended(data); 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. * Test an object to see if it is an Input.
* @param {Object} obj * @param {Object} obj

View File

@ -228,10 +228,11 @@ KeyRing.prototype.getProgramHash = function getProgramHash(enc) {
/** /**
* Get address' scripthash address for witness program. * 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; var hash, address;
if (!this.witness) if (!this.witness)
@ -243,6 +244,9 @@ KeyRing.prototype.getProgramAddress = function getProgramAddress() {
this._programAddress = address; this._programAddress = address;
} }
if (enc === 'base58')
return this._programAddress.toBase58();
return this._programAddress; return this._programAddress;
}; };
@ -296,10 +300,11 @@ KeyRing.prototype.getScriptHash256 = function getScriptHash256(enc) {
/** /**
* Get scripthash address. * 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; var hash, address;
if (this.type !== 'multisig') if (this.type !== 'multisig')
@ -316,6 +321,9 @@ KeyRing.prototype.getScriptAddress = function getScriptAddress() {
this._scriptAddress = address; this._scriptAddress = address;
} }
if (enc === 'base58')
return this._scriptAddress.toBase58();
return this._scriptAddress; return this._scriptAddress;
}; };
@ -336,10 +344,11 @@ KeyRing.prototype.getKeyHash = function getKeyHash(enc) {
/** /**
* Get pubkeyhash address. * 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; var hash, address;
if (!this._keyAddress) { if (!this._keyAddress) {
@ -351,20 +360,24 @@ KeyRing.prototype.getKeyAddress = function getKeyAddress() {
this._keyAddress = address; this._keyAddress = address;
} }
if (enc === 'base58')
return this._keyAddress.toBase58();
return this._keyAddress; return this._keyAddress;
}; };
/** /**
* Compile a hash to an address. * Compile a hash to an address.
* @private
* @param {Hash|Buffer} hash * @param {Hash|Buffer} hash
* @param {AddressType?} type * @param {AddressType?} type
* @param {Number?} version - Witness version. * @param {Number?} version - Witness version.
* @returns {Base58Address} * @returns {Address}
* @throws Error on bad hash/prefix. * @throws Error on bad hash/prefix.
*/ */
KeyRing.prototype.compile = function compile(hash, type, version) { 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. * 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') if (this.type === 'multisig')
return this.getScriptAddress(); return this.getScriptAddress(enc);
return this.getKeyAddress(); return this.getKeyAddress(enc);
}; };
/** /**

View File

@ -156,7 +156,7 @@ MTX.prototype.addInput = function addInput(options, index) {
assert(options.prevout); assert(options.prevout);
input = bcoin.input(options, true); input = new bcoin.input(options, true);
this.inputs.push(input); this.inputs.push(input);
@ -706,7 +706,10 @@ MTX.prototype.addOutput = function addOutput(address, value) {
address = address.getAddress(); address = address.getAddress();
} }
if (typeof address === 'string') { if (typeof address === 'string')
address = bcoin.address.fromBase58(address);
if (address instanceof bcoin.address) {
options = { options = {
address: address, address: address,
value: value value: value
@ -715,7 +718,7 @@ MTX.prototype.addOutput = function addOutput(address, value) {
options = address; options = address;
} }
output = bcoin.output(options, true); output = new bcoin.output(options, true);
if (options.address) if (options.address)
output.script = Script.fromAddress(options.address); output.script = Script.fromAddress(options.address);

View File

@ -1227,13 +1227,14 @@ Wallet.prototype.getScriptHash256 = function getScriptHash256(enc) {
/** /**
* Get scripthash address for current receiving address. * 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) if (!this.receiveAddress)
return; return;
return this.receiveAddress.getScriptAddress(); return this.receiveAddress.getScriptAddress(enc);
}; };
/** /**
@ -1263,13 +1264,14 @@ Wallet.prototype.getProgramHash = function getProgramHash(enc) {
/** /**
* Get current receiving address' * Get current receiving address'
* scripthash address for witness program. * 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) if (!this.receiveAddress)
return; 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. * 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) if (!this.receiveAddress)
return; 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. * 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) if (!this.receiveAddress)
return; return;
return this.receiveAddress.getAddress(); return this.receiveAddress.getAddress(enc);
}; };
Wallet.prototype.__defineGetter__('publicKey', function() { Wallet.prototype.__defineGetter__('publicKey', function() {

View File

@ -65,7 +65,7 @@ describe('Wallet', function() {
it('should generate new key and address', function() { it('should generate new key and address', function() {
walletdb.create(function(err, w) { walletdb.create(function(err, w) {
assert.ifError(err); assert.ifError(err);
var addr = w.getAddress(); var addr = w.getAddress('base58');
assert(addr); assert(addr);
assert(bcoin.address.validate(addr)); assert(bcoin.address.validate(addr));
}); });
@ -105,9 +105,9 @@ describe('Wallet', function() {
assert.ifError(err); assert.ifError(err);
if (witness) if (witness)
assert(bcoin.address.fromBase58(w.getAddress()).type === 'witnesspubkeyhash'); assert(bcoin.address.fromBase58(w.getAddress('base58')).type === 'witnesspubkeyhash');
else else
assert(bcoin.address.fromBase58(w.getAddress()).type === 'pubkeyhash'); assert(bcoin.address.fromBase58(w.getAddress('base58')).type === 'pubkeyhash');
var src = bcoin.mtx({ var src = bcoin.mtx({
outputs: [{ outputs: [{
@ -608,21 +608,21 @@ describe('Wallet', function() {
// w3 = bcoin.wallet.fromJSON(w3.toJSON()); // w3 = bcoin.wallet.fromJSON(w3.toJSON());
// Our p2sh address // Our p2sh address
var addr = w1.getAddress(); var addr = w1.getAddress('base58');
if (witness) if (witness)
assert(bcoin.address.fromBase58(addr).type === 'witnessscripthash'); assert(bcoin.address.fromBase58(addr).type === 'witnessscripthash');
else else
assert(bcoin.address.fromBase58(addr).type === 'scripthash'); assert(bcoin.address.fromBase58(addr).type === 'scripthash');
assert.equal(w1.getAddress(), addr); assert.equal(w1.getAddress('base58'), addr);
assert.equal(w2.getAddress(), addr); assert.equal(w2.getAddress('base58'), addr);
assert.equal(w3.getAddress(), addr); assert.equal(w3.getAddress('base58'), addr);
var paddr = w1.getProgramAddress(); var paddr = w1.getProgramAddress('base58');
assert.equal(w1.getProgramAddress(), paddr); assert.equal(w1.getProgramAddress('base58'), paddr);
assert.equal(w2.getProgramAddress(), paddr); assert.equal(w2.getProgramAddress('base58'), paddr);
assert.equal(w3.getProgramAddress(), paddr); assert.equal(w3.getProgramAddress('base58'), paddr);
// Add a shared unspent transaction to our wallets // Add a shared unspent transaction to our wallets
var utx = bcoin.mtx(); var utx = bcoin.mtx();
@ -650,11 +650,11 @@ describe('Wallet', function() {
assert.equal(w1.receiveDepth, 2); assert.equal(w1.receiveDepth, 2);
assert.equal(w1.changeDepth, 1); assert.equal(w1.changeDepth, 1);
assert(w1.getAddress() !== addr); assert(w1.getAddress('base58') !== addr);
addr = w1.getAddress(); addr = w1.getAddress('base58');
assert.equal(w1.getAddress(), addr); assert.equal(w1.getAddress('base58'), addr);
assert.equal(w2.getAddress(), addr); assert.equal(w2.getAddress('base58'), addr);
assert.equal(w3.getAddress(), addr); assert.equal(w3.getAddress('base58'), addr);
// Create a tx requiring 2 signatures // Create a tx requiring 2 signatures
var send = bcoin.mtx(); var send = bcoin.mtx();
@ -673,10 +673,10 @@ describe('Wallet', function() {
assert(send.verify(flags)); assert(send.verify(flags));
assert.equal(w1.changeDepth, 1); assert.equal(w1.changeDepth, 1);
var change = w1.changeAddress.getAddress(); var change = w1.changeAddress.getAddress('base58');
assert.equal(w1.changeAddress.getAddress(), change); assert.equal(w1.changeAddress.getAddress('base58'), change);
assert.equal(w2.changeAddress.getAddress(), change); assert.equal(w2.changeAddress.getAddress('base58'), change);
assert.equal(w3.changeAddress.getAddress(), change); assert.equal(w3.changeAddress.getAddress('base58'), change);
// Simulate a confirmation // Simulate a confirmation
send.ps = 0; send.ps = 0;
@ -693,12 +693,12 @@ describe('Wallet', function() {
assert.equal(w1.receiveDepth, 2); assert.equal(w1.receiveDepth, 2);
assert.equal(w1.changeDepth, 2); assert.equal(w1.changeDepth, 2);
assert(w1.getAddress() === addr); assert(w1.getAddress('base58') === addr);
assert(w1.changeAddress.getAddress() !== change); assert(w1.changeAddress.getAddress('base58') !== change);
change = w1.changeAddress.getAddress(); change = w1.changeAddress.getAddress('base58');
assert.equal(w1.changeAddress.getAddress(), change); assert.equal(w1.changeAddress.getAddress('base58'), change);
assert.equal(w2.changeAddress.getAddress(), change); assert.equal(w2.changeAddress.getAddress('base58'), change);
assert.equal(w3.changeAddress.getAddress(), change); assert.equal(w3.changeAddress.getAddress('base58'), change);
if (witness) { if (witness) {
send.inputs[0].witness.items[2] = new Buffer([]); send.inputs[0].witness.items[2] = new Buffer([]);
@ -713,8 +713,8 @@ describe('Wallet', function() {
// w3 = bcoin.wallet.fromJSON(w3.toJSON()); // w3 = bcoin.wallet.fromJSON(w3.toJSON());
// assert.equal(w3.receiveDepth, 2); // assert.equal(w3.receiveDepth, 2);
// assert.equal(w3.changeDepth, 2); // assert.equal(w3.changeDepth, 2);
//assert.equal(w3.getAddress(), addr); //assert.equal(w3.getAddress('base58'), addr);
//assert.equal(w3.changeAddress.getAddress(), change); //assert.equal(w3.changeAddress.getAddress('base58'), change);
cb(); cb();
}); });
@ -819,8 +819,8 @@ describe('Wallet', function() {
assert(account !== acc); assert(account !== acc);
assert(account.accountKey.xpubkey === acc.accountKey.xpubkey); assert(account.accountKey.xpubkey === acc.accountKey.xpubkey);
assert(w1.account.accountIndex === 0); assert(w1.account.accountIndex === 0);
assert(account.receiveAddress.getAddress() !== w1.account.receiveAddress.getAddress()); assert(account.receiveAddress.getAddress('base58') !== w1.account.receiveAddress.getAddress('base58'));
assert(w1.getAddress() === w1.account.receiveAddress.getAddress()); assert(w1.getAddress('base58') === w1.account.receiveAddress.getAddress('base58'));
// Coinbase // Coinbase
var t1 = bcoin.mtx() var t1 = bcoin.mtx()