diff --git a/js/models/core/TxProposals.js b/js/models/core/TxProposals.js index b02f45791..ead57b190 100644 --- a/js/models/core/TxProposals.js +++ b/js/models/core/TxProposals.js @@ -25,8 +25,7 @@ function TxProposal(opts) { } TxProposal.prototype.getID = function() { - var ntxid = this.builder.build().getNormalizedHash().toString('hex'); - return ntxid; + return this.builder.build().getNormalizedHash().toString('hex'); }; TxProposal.prototype.toObj = function() { @@ -42,16 +41,24 @@ TxProposal.prototype.setSent = function(sentTxid) { this.sentTs = Date.now(); }; -TxProposal.fromObj = function(o) { +TxProposal.fromObj = function(o, forceOpts) { var t = new TxProposal(o); + + // force opts is requested. + for (var k in forceOpts) { + o.builderObj.opts[k] = forceOpts[k]; + } + try { - t.builder = new TransactionBuilder.fromObj(o.builderObj); + t.builder = TransactionBuilder.fromObj(o.builderObj); + } catch (e) { if (!o.version) { t.builder = new BuilderMockV0(o.builderObj); t.readonly = 1; }; } + return t; }; @@ -172,15 +179,16 @@ function TxProposals(opts) { this.txps = {}; } -TxProposals.fromObj = function(o) { +TxProposals.fromObj = function(o, forceOpts) { var ret = new TxProposals({ networkName: o.networkName, walletId: o.walletId, }); + o.txps.forEach(function(o2) { - var t = TxProposal.fromObj(o2); + var t = TxProposal.fromObj(o2, forceOpts); if (t.builder) { - var id = t.builder.build().getNormalizedHash().toString('hex'); + var id = t.getID(); ret.txps[id] = t; } }); diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 7e37feeae..cf5e13839 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -57,6 +57,14 @@ function Wallet(opts) { this.network.setHexNonces(opts.networkNonces); } + +Wallet.builderOpts = { + lockTime: null, + signhash: bitcore.Transaction.SIGNHASH_ALL, + fee: null, + feeSat: null, +}; + Wallet.parent = EventEmitter; Wallet.prototype.log = function() { if (!this.verbose) return; @@ -121,10 +129,11 @@ Wallet.prototype._handlePublicKeyRing = function(senderId, data, isInbound) { }; -Wallet.prototype._handleTxProposal = function(senderId, data) { - this.log('RECV TXPROPOSAL:', data); - var inTxp = TxProposals.TxProposal.fromObj(data.txProposal); +Wallet.prototype._handleTxProposal = function(senderId, data) { + this.log('RECV TXPROPOSAL: ', data); + + var inTxp = TxProposals.TxProposal.fromObj(data.txProposal, Wallet.builderOpts); var valid = inTxp.isValid(); if (!valid) { var corruptEvent = { @@ -378,7 +387,7 @@ Wallet.fromObj = function(o, storage, network, blockchain) { opts.addressBook = o.addressBook; opts.publicKeyRing = PublicKeyRing.fromObj(o.publicKeyRing); - opts.txProposals = TxProposals.fromObj(o.txProposals); + opts.txProposals = TxProposals.fromObj(o.txProposals, Wallet.builderOpts); opts.privateKey = PrivateKey.fromObj(o.privateKey); opts.storage = storage; @@ -497,8 +506,7 @@ Wallet.prototype.getTxProposals = function() { txp.finallyRejected = true; } - if (txp.readonly && !txp.finallyRejected && !txp.sentTs) { - } else { + if (txp.readonly && !txp.finallyRejected && !txp.sentTs) {} else { ret.push(txp); } } @@ -719,6 +727,10 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, comment, utxos }; } + for (var k in Wallet.builderOpts){ + opts[k] = Wallet.builderOpts[k]; + } + var b = new Builder(opts) .setUnspent(utxos) .setOutputs([{ diff --git a/test/test.TxProposals.js b/test/test.TxProposals.js index f42ba5de0..ec7cd7981 100644 --- a/test/test.TxProposals.js +++ b/test/test.TxProposals.js @@ -573,6 +573,13 @@ describe('TxProposals model', function() { }); + it('#fromObj stored (hardcoded) data', function() { + var txp = TxProposals.TxProposal.fromObj(txpv1); + txp.getID().should.equal('5cae6e225335acd2725856c71ef1ca61c42f118967102c5d0ed6710343e4a19f'); + var tx = txp.builder.build(); + tx.countInputSignatures(0).should.equal(2); + tx.countInputMissingSignatures(0).should.equal(0); + }); it('#toObj #fromObj roundtrip', function() { @@ -628,7 +635,7 @@ describe('TxProposals model', function() { Object.keys(w2.txps).length.should.equal(1); }); - describe('TxProposals model', function() { + describe('TxProposal model', function() { var createMockTxp = function(raw) { var tx = new Transaction(); tx.parse(new Buffer(raw, 'hex')); @@ -670,4 +677,51 @@ describe('TxProposals model', function() { txp.isValid().should.equal(false); }); }); + }); + + +var txpv1 = { + "creator": "0361fb4252367715405a0d27f99cc74a671133292e8d725e009536d7257c8c01b0", + "createdTs": 1406310417996, + "seenBy": { + "0361fb4252367715405a0d27f99cc74a671133292e8d725e009536d7257c8c01b0": 1406310417996, + "02ba1599c64da4d80e25985be46c50e944b65f02e2b48c930528ce763d6710158f": 1406310418162 + }, + "signedBy": { + "0361fb4252367715405a0d27f99cc74a671133292e8d725e009536d7257c8c01b0": 1406310417996, + "02ba1599c64da4d80e25985be46c50e944b65f02e2b48c930528ce763d6710158f": 1406310645549 + }, + "rejectedBy": {}, + "sentTs": 1406310645873, + "sentTxid": "87296c50e8601437d63d556afb27c3b8e3819214be0a9d756d401a8286c0ec43", + "inputChainPaths": ["m/45'/0/1/1"], + "comment": "test 6", + "builderObj": { + "version": 1, + "outs": [{ + "address": "mph66bnLvcn9KUSMrpikUBUZZkN2C1Z5tg", + "amountSatStr": 100 + }], + "utxos": [{ + "address": "2NEodmgBa4SH3VwE2asgW34vMYe8VThBZNo", + "txid": "8f8deda12dad6248e655054632a27f6891ebb37e8d2b3dd1bff87e71fd451ac7", + "vout": 1, + "ts": 1406312717, + "scriptPubKey": "a914ec7bce12d0e82a7d2b5431f6d89ca70af317f5a187", + "amount": 0.009798, + "confirmations": 0, + "confirmationsFromCache": false + }], + "opts": { + "spendUnconfirmed": true, + "remainderOut": { + "address": "2N74XAozMH3JB3XgeBkRvRw1J8TtfLTtvny" + } + }, + "scriptSig": ["00483045022100f167ad33b8bef4c65af8d19c1a849d1770cc8d1e35bffebe6b5459dcbe655c7802207b37370b308ba668fe19f8e8bc462c9fbdc6c67f79900670758d228d83ea96da014730440220038ad3f4cc7b0738b593454ec189913ae4b442bc83da153d68d9a0077bd1b09102202b5728a08f302e97de61ea37280b48ccdd575f0d235c22f5e0ecac6a4ab0f46401475221024739614847d5233a46913482c17c6860194ad78abb3bf47de46223047d8a0b5821024c6dc65a52c5eaaa080b96888091544f8ab8712caa7e0b69ea4b45f6f059557452ae"], + "hashToScriptMap": { + "2NEodmgBa4SH3VwE2asgW34vMYe8VThBZNo": "5221024739614847d5233a46913482c17c6860194ad78abb3bf47de46223047d8a0b5821024c6dc65a52c5eaaa080b96888091544f8ab8712caa7e0b69ea4b45f6f059557452ae" + } + } + };