Browse Source

Resolves #373

Move TransactionBuilder.scriptForAddress to Address#getScriptPubKey (instance method)
patch-2
Manan Patel 11 years ago
parent
commit
d83fc6e4b5
  1. 17
      lib/Address.js
  2. 27
      lib/TransactionBuilder.js
  3. 16
      test/test.Address.js
  4. 3
      test/test.TransactionBuilder.js

17
lib/Address.js

@ -168,5 +168,22 @@ Address.prototype.isScript = function() {
return this.isValid() && this.version() === this.network().P2SHVersion;
};
// returns the scriptPubKey
Address.prototype.getScriptPubKey = function() {
var version = this.version();
var livenet = networks.livenet;
var testnet = networks.testnet;
var script;
if (version === livenet.addressVersion || version === testnet.addressVersion)
script = Script.createPubKeyHashOut(this.payload());
else if (version === livenet.P2SHVersion || version === testnet.P2SHVersion)
script = Script.createP2SH(this.payload());
else
throw new Error('invalid address - unknown version');
return script;
};
module.exports = require('soop')(Address);

27
lib/TransactionBuilder.js

@ -116,31 +116,6 @@ function TransactionBuilder(opts) {
TransactionBuilder.FEE_PER_1000B_SAT = FEE_PER_1000B_SAT;
/*
* scriptForAddress
*
* Returns a scriptPubKey for the given address type
*/
TransactionBuilder.scriptForAddress = function(addressString) {
var livenet = networks.livenet;
var testnet = networks.testnet;
var address = new Address(addressString);
var version = address.version();
var script;
if (version === livenet.addressVersion || version === testnet.addressVersion)
script = Script.createPubKeyHashOut(address.payload());
else if (version === livenet.P2SHVersion || version === testnet.P2SHVersion)
script = Script.createP2SH(address.payload());
else
throw new Error('invalid output address');
return script;
};
TransactionBuilder._scriptForPubkeys = function(out) {
var l = out.pubkeys.length;
@ -156,7 +131,7 @@ TransactionBuilder._scriptForPubkeys = function(out) {
TransactionBuilder._scriptForOut = function(out) {
var ret;
if (out.address)
ret = this.scriptForAddress(out.address);
ret = new Address(out.address).getScriptPubKey();
else if (out.pubkeys || out.nreq || out.nreq > 1)
ret = this._scriptForPubkeys(out);
else

16
test/test.Address.js

@ -174,6 +174,22 @@ describe('Address', function() {
});
});
describe('#getScriptPubKey', function() {
var data = [
['76a91423b7530a00dd7951e11791c529389421c0b8d83b88ac', 'mimoZNLcP2rrMRgdeX5PSnR7AjCqQveZZ4'],
['a9147049be48e74a660157da3ed64569981592f7fa0587','2N3Ux1YTnt1ixofYvJfaabqZSj2MBF3jsmv'],
['76a914774e603bafb717bd3f070e68bbcccfd907c77d1388ac', 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE'],
['76a914b00127584485a7cff0949ef0f6bc5575f06ce00d88ac', 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd']
];
it('validate scriptPubKey for a given address', function() {
for(var i in data) {
var d = data[i];
var b = new Address(d[1]).getScriptPubKey().getBuffer();
b.toString('hex').should.equal(d[0]);
}
});
});
describe('#fromScriptPubKey', function() {

3
test/test.TransactionBuilder.js

@ -7,6 +7,7 @@ var bitcore = bitcore || require('../bitcore');
var should = chai.should();
var TransactionBuilder = bitcore.TransactionBuilder;
var Address = bitcore.Address;
var WalletKey = bitcore.WalletKey;
var Script = bitcore.Script;
var util = bitcore.util;
@ -818,7 +819,7 @@ describe('TransactionBuilder', function() {
}];
//info.scriptBufHex,
var s = TransactionBuilder.scriptForAddress(info.address)
var s = new Address(info.address).getScriptPubKey()
.getBuffer().toString('hex');
var b = new TransactionBuilder(opts)

Loading…
Cancel
Save