From 595a54322bee681a26ea7ed318e6bb0d32e95f86 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Sat, 5 Sep 2015 18:49:43 -0300 Subject: [PATCH] correctly build tx proposal on P2PKH --- lib/model/address.js | 11 ---------- lib/model/txproposal.js | 2 ++ lib/server.js | 9 +------- test/integration/server.js | 43 +++++++++++++++++++++++++++----------- 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/lib/model/address.js b/lib/model/address.js index 93b6fd9..9f4da97 100644 --- a/lib/model/address.js +++ b/lib/model/address.js @@ -34,15 +34,4 @@ Address.fromObj = function(obj) { return x; }; - -/** - * getScriptPubKey - * - * @param {number} threshold - amount of required signatures to spend the output - * @return {Script} - */ -Address.prototype.getScriptPubKey = function(threshold) { - return Bitcore.Script.buildMultisigOut(this.publicKeys, threshold).toScriptHashOut(); -}; - module.exports = Address; diff --git a/lib/model/txproposal.js b/lib/model/txproposal.js index 668e671..7802d89 100644 --- a/lib/model/txproposal.js +++ b/lib/model/txproposal.js @@ -72,6 +72,7 @@ TxProposal.create = function(opts) { x.proposalSignaturePubKey = opts.proposalSignaturePubKey; x.proposalSignaturePubKeySig = opts.proposalSignaturePubKeySig; x.derivationStrategy = opts.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP45; + x.addressType = opts.addressType || WalletUtils.SCRIPT_TYPES.P2SH; x.customData = opts.customData; if (_.isFunction(TxProposal._create[x.type])) { @@ -120,6 +121,7 @@ TxProposal.fromObj = function(obj) { x.proposalSignaturePubKey = obj.proposalSignaturePubKey; x.proposalSignaturePubKeySig = obj.proposalSignaturePubKeySig; x.derivationStrategy = obj.derivationStrategy || WalletUtils.DERIVATION_STRATEGIES.BIP45; + x.addressType = obj.addressType || WalletUtils.SCRIPT_TYPES.P2SH; x.customData = obj.customData; return x; diff --git a/lib/server.js b/lib/server.js index 379fefc..2b0af0f 100644 --- a/lib/server.js +++ b/lib/server.js @@ -542,14 +542,6 @@ WalletService.prototype._parseClientVersion = function() { return this.parsedClientVersion; }; -WalletService.prototype._clientSupportsBIP44 = function() { - var version = this._parseClientVersion(); - if (!version) return false; - if (version.agent != 'bwc') return true; // Asume 3rd party clients are up-to-date - if (version.major == 0 && version.minor <= 1) return false; - return true; -}; - WalletService.prototype._clientSupportsTXPv2 = function() { var version = this._parseClientVersion(); if (!version) return false; @@ -1216,6 +1208,7 @@ WalletService.prototype.createTx = function(opts, cb) { walletN: wallet.n, excludeUnconfirmedUtxos: !!opts.excludeUnconfirmedUtxos, derivationStrategy: wallet.derivationStrategy, + addressType: wallet.addressType, customData: opts.customData }; diff --git a/test/integration/server.js b/test/integration/server.js index 0b93e6c..9636ad5 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -163,11 +163,23 @@ helpers.stubUtxos = function(server, wallet, amounts, cb) { } else { confirmations = Math.floor(Math.random() * 100 + 1); } + + var scriptPubKey; + switch (wallet.addressType) { + case WalletUtils.SCRIPT_TYPES.P2SH: + scriptPubKey = Bitcore.Script.buildMultisigOut(address.publicKeys, wallet.m).toScriptHashOut(); + break; + case WalletUtils.SCRIPT_TYPES.P2PKH: + scriptPubKey = Bitcore.Script.buildPublicKeyHashOut(address.address); + break; + } + should.exist(scriptPubKey); + return { txid: helpers.randomTXID(), vout: Math.floor(Math.random() * 10 + 1), satoshis: helpers.toSatoshi(amount).toString(), - scriptPubKey: address.getScriptPubKey(wallet.m).toBuffer().toString('hex'), + scriptPubKey: scriptPubKey.toBuffer().toString('hex'), address: address.address, confirmations: confirmations, }; @@ -1158,24 +1170,26 @@ describe('Wallet service', function() { beforeEach(function() { server = WalletService.getInstance(); }); - it('should use BIP44 for 1-of-1 wallet if supported', function(done) { + it('should use BIP44 & P2PKH for 1-of-1 wallet if supported', function(done) { var walletOpts = { name: 'my wallet', m: 1, n: 1, pubKey: TestData.keyPair.pub, supportBIP44: true, + supportP2PKH: true, }; server.createWallet(walletOpts, function(err, wid) { should.not.exist(err); server.storage.fetchWallet(wid, function(err, wallet) { should.not.exist(err); wallet.derivationStrategy.should.equal('BIP44'); + wallet.addressType.should.equal('P2PKH'); done(); }); }); }); - it('should use BIP45 for 1-of-1 wallet if BIP44 not supported', function(done) { + it('should use BIP45 & P2SH for 1-of-1 wallet if not supported', function(done) { var walletOpts = { name: 'my wallet', m: 1, @@ -1187,22 +1201,26 @@ describe('Wallet service', function() { server.storage.fetchWallet(wid, function(err, wallet) { should.not.exist(err); wallet.derivationStrategy.should.equal('BIP45'); + wallet.addressType.should.equal('P2SH'); done(); }); }); }); - it('should always use BIP45 for shared wallets', function(done) { + it('should always use BIP45 & P2SH for shared wallets', function(done) { var walletOpts = { name: 'my wallet', m: 2, n: 3, pubKey: TestData.keyPair.pub, + supportBIP44: true, + supportP2PKH: true, }; server.createWallet(walletOpts, function(err, wid) { should.not.exist(err); server.storage.fetchWallet(wid, function(err, wallet) { should.not.exist(err); wallet.derivationStrategy.should.equal('BIP45'); + wallet.addressType.should.equal('P2SH'); done(); }); }); @@ -1398,7 +1416,7 @@ describe('Wallet service', function() { }); }); - describe('1-of-1 (BIP44)', function() { + describe('1-of-1 (BIP44 & P2PKH)', function() { beforeEach(function(done) { helpers.createAndJoinWallet(1, 1, function(s, w) { server = s; @@ -2799,18 +2817,20 @@ describe('Wallet service', function() { }); describe('#signTx', function() { - describe('1-1', function() { + describe('1-of-1 (BIP44 & P2PKH)', function() { var server, wallet, txid; beforeEach(function(done) { helpers.createAndJoinWallet(1, 1, function(s, w) { server = s; wallet = w; - helpers.stubUtxos(server, wallet, _.range(1, 9), function() { - var txOpts = helpers.createSimpleProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 20, TestData.copayers[0].privKey_1H_0); + helpers.stubUtxos(server, wallet, [1, 2], function() { + var txOpts = helpers.createSimpleProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 2.5, TestData.copayers[0].privKey_1H_0); server.createTx(txOpts, function(err, tx) { should.not.exist(err); should.exist(tx); + tx.derivationStrategy.should.equal('BIP44'); + tx.addressType.should.equal('P2PKH'); txid = tx.id; done(); }); @@ -2844,10 +2864,9 @@ describe('Wallet service', function() { }); }); }); - - }); - describe('Multisign 2-3', function() { + + describe('Multisig', function() { var server, wallet, txid; beforeEach(function(done) { @@ -4360,7 +4379,7 @@ describe('Wallet service', function() { var server, wallet; var scanConfigOld = WalletService.SCAN_CONFIG; - describe('1-of-1 wallet (BIP44)', function() { + describe('1-of-1 wallet (BIP44 & P2PKH)', function() { beforeEach(function(done) { this.timeout(5000); WalletService.SCAN_CONFIG.scanWindow = 2;