From a7c8cf49b39fab414da0732f1da7e012e87538b8 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Wed, 9 Apr 2014 14:07:56 -0300 Subject: [PATCH] update fromPubKeys to use fromScript This means fewer code-duplication. Also added another test for fromScript to make sure it is thoroughly tested. Also pass through opts to createMultisig so that you can choose to lot let it be sorted if you want. --- Address.js | 9 +++------ test/test.Address.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Address.js b/Address.js index d530bf004..c3bf0eed6 100644 --- a/Address.js +++ b/Address.js @@ -27,7 +27,7 @@ Address.fromPubKey = function(pubKey, network) { }; //create a p2sh m-of-n multisig address -Address.fromPubKeys = function(mReq, pubKeys, network) { +Address.fromPubKeys = function(mReq, pubKeys, network, opts) { if (!network) network = 'livenet'; @@ -37,12 +37,9 @@ Address.fromPubKeys = function(mReq, pubKeys, network) { throw new Error('Invalid public key'); } - var version = networks[network].P2SHVersion; - var script = Script.createMultisig(mReq, pubKeys); - var buf = script.getBuffer(); - var hash = coinUtil.sha256ripe160(buf) + var script = Script.createMultisig(mReq, pubKeys, opts); - return new Address(version, hash); + return Address.fromScript(script, network); }; //create a p2sh address from redeemScript diff --git a/test/test.Address.js b/test/test.Address.js index 9793902ab..bffc08a2e 100644 --- a/test/test.Address.js +++ b/test/test.Address.js @@ -120,6 +120,22 @@ describe('Address', function() { var addr2 = Address.fromPubKeys(mReq, pubKeys); addr.toString().should.equal(addr2.toString()); }); + + it('it should make this hand-crafted address', function() { + var pubkey1 = new Buffer('03e0973263b4e0d5f5f56d25d430e777ab3838ff644db972c0bf32c31da5686c27', 'hex'); + var pubkey2 = new Buffer('0371f94c57cc013507101e30794161f4e6b9efd58a9ea68838daf429b7feac8cb2', 'hex'); + var pubkey3 = new Buffer('032c0d2e394541e2efdc7ac3500e16e7e69df541f38670402e95aa477202fa06bb', 'hex'); + var pubKeys = [pubkey1, pubkey2, pubkey3]; + var mReq = 2; + var script = bitcore.Script.createMultisig(mReq, pubKeys); + var addr = Address.fromScript(script); + + var hash = bitcore.util.sha256ripe160(script.getBuffer()); + var version = bitcore.networks['livenet'].P2SHVersion; + var addr2 = new Address(version, hash); + + addr.toString().should.equal(addr2.toString()); + }); }); });