add from-to Obj in Transaction Builder
This commit is contained in:
parent
d0f2601512
commit
185ebe8ebb
|
@ -506,12 +506,12 @@ TransactionBuilder.prototype._signPubKeyHash = function(walletKeyMap, input, txS
|
||||||
};
|
};
|
||||||
|
|
||||||
// FOR TESTING
|
// FOR TESTING
|
||||||
// var _dumpChunks = function (scriptSig, label) {
|
var _dumpChunks = function (scriptSig, label) {
|
||||||
// console.log('## DUMP: ' + label + ' ##');
|
console.log('## DUMP: ' + label + ' ##');
|
||||||
// for(var i=0; i<scriptSig.chunks.length; i++) {
|
for(var i=0; i<scriptSig.chunks.length; i++) {
|
||||||
// console.log('\tCHUNK ', i, scriptSig.chunks[i]);
|
console.log('\tCHUNK ', i, scriptSig.chunks[i]);
|
||||||
// }
|
}
|
||||||
// };
|
};
|
||||||
|
|
||||||
TransactionBuilder.prototype._initMultiSig = function(scriptSig, nreq) {
|
TransactionBuilder.prototype._initMultiSig = function(scriptSig, nreq) {
|
||||||
var wasUpdated = false;
|
var wasUpdated = false;
|
||||||
|
@ -526,7 +526,8 @@ TransactionBuilder.prototype._initMultiSig = function(scriptSig, nreq) {
|
||||||
|
|
||||||
|
|
||||||
TransactionBuilder.prototype._isSignedWithKey = function(wk, scriptSig, txSigHash, nreq) {
|
TransactionBuilder.prototype._isSignedWithKey = function(wk, scriptSig, txSigHash, nreq) {
|
||||||
var ret=0;
|
var ret=false;
|
||||||
|
// _dumpChunks(scriptSig);
|
||||||
for(var i=1; i<=nreq; i++) {
|
for(var i=1; i<=nreq; i++) {
|
||||||
var chunk = scriptSig.chunks[i];
|
var chunk = scriptSig.chunks[i];
|
||||||
if (chunk ===0 || chunk.length === 0) continue;
|
if (chunk ===0 || chunk.length === 0) continue;
|
||||||
|
@ -567,6 +568,7 @@ TransactionBuilder.prototype._updateMultiSig = function(wk, scriptSig, txSigHash
|
||||||
wasUpdated=true;
|
wasUpdated=true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// _dumpChunks(scriptSig); // TODO
|
||||||
return wasUpdated ? scriptSig : null;
|
return wasUpdated ? scriptSig : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -683,8 +685,6 @@ TransactionBuilder.prototype.sign = function(keys) {
|
||||||
l = ins.length,
|
l = ins.length,
|
||||||
walletKeyMap = TransactionBuilder._mapKeys(keys);
|
walletKeyMap = TransactionBuilder._mapKeys(keys);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for (var i = 0; i < l; i++) {
|
for (var i = 0; i < l; i++) {
|
||||||
var input = this.inputMap[i];
|
var input = this.inputMap[i];
|
||||||
|
|
||||||
|
@ -717,5 +717,63 @@ TransactionBuilder.prototype.build = function() {
|
||||||
return this.tx;
|
return this.tx;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
TransactionBuilder.prototype.toObj = function() {
|
||||||
|
var data = {
|
||||||
|
valueInSat : this.valueInSat.toString(),
|
||||||
|
valueOutSat : this.valueOutSat.toString(),
|
||||||
|
feeSat : this.feeSat.toString(),
|
||||||
|
remainderSat : this.remainderSat.toString(),
|
||||||
|
|
||||||
|
hashToScriptMap : this.hashToScriptMap,
|
||||||
|
selectedUtxos : this.selectedUtxos,
|
||||||
|
|
||||||
|
inputsSigned : this.inputsSigned,
|
||||||
|
signaturesAdded : this.signaturesAdded,
|
||||||
|
|
||||||
|
//opts :
|
||||||
|
signhash : this.signhash,
|
||||||
|
spendUnconfirmed : this.spendUnconfirmed,
|
||||||
|
|
||||||
|
inputMap : this.inputMap,
|
||||||
|
txobj : this.txobj,
|
||||||
|
};
|
||||||
|
if (this.tx) {
|
||||||
|
data.tx =this.tx.serialize().toString('hex');
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
TransactionBuilder.fromObj = function(data) {
|
||||||
|
var b = new TransactionBuilder();
|
||||||
|
b.valueInSat = data.valueInSat.toString();
|
||||||
|
b.valueOutSat = data.valueOutSat.toString();
|
||||||
|
b.feeSat = data.feeSat.toString();
|
||||||
|
b.remainderSat = data.remainderSat.toString();
|
||||||
|
|
||||||
|
b.hashToScriptMap = data.hashToScriptMap;
|
||||||
|
b.selectedUtxos = data.selectedUtxos;
|
||||||
|
|
||||||
|
b.inputsSigned = data.inputsSigned;
|
||||||
|
b.signaturesAdded = data.signaturesAdded;
|
||||||
|
|
||||||
|
b.signhash = data.signhash;
|
||||||
|
b.spendUnconfirmed = data.spendUnconfirmed;
|
||||||
|
|
||||||
|
b.inputMap = data.inputMap;
|
||||||
|
b.txobj = data.txobj;
|
||||||
|
|
||||||
|
if (data.tx) {
|
||||||
|
var t = new Transaction();
|
||||||
|
t.parse(new Buffer(data.tx,'hex'));
|
||||||
|
b.tx = t;
|
||||||
|
}
|
||||||
|
else if (b.txobj)
|
||||||
|
b.tx = new Transaction(b.txobj);
|
||||||
|
return b;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
module.exports = require('soop')(TransactionBuilder);
|
module.exports = require('soop')(TransactionBuilder);
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ var util = bitcore.util;
|
||||||
var networks = bitcore.networks;
|
var networks = bitcore.networks;
|
||||||
var buffertools = require('buffertools');
|
var buffertools = require('buffertools');
|
||||||
var testdata = testdata || require('./testdata');
|
var testdata = testdata || require('./testdata');
|
||||||
|
var nutil = require('util');
|
||||||
|
|
||||||
describe('TransactionBuilder', function() {
|
describe('TransactionBuilder', function() {
|
||||||
it('should initialze the main object', function() {
|
it('should initialze the main object', function() {
|
||||||
|
@ -644,7 +645,7 @@ describe('TransactionBuilder', function() {
|
||||||
b.signaturesAdded.should.equal(3);
|
b.signaturesAdded.should.equal(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should sign in steps a p2sh/p2pubkeyhash tx', function() {
|
it('should sign a p2sh/p2pubkeyhash tx', function() {
|
||||||
var priv = 'cMpKwGr5oxEacN95WFKNEq6tTcvi11regFwS3muHvGYVxMPJX8JA';
|
var priv = 'cMpKwGr5oxEacN95WFKNEq6tTcvi11regFwS3muHvGYVxMPJX8JA';
|
||||||
var network = 'testnet';
|
var network = 'testnet';
|
||||||
var opts = {
|
var opts = {
|
||||||
|
@ -682,10 +683,79 @@ describe('TransactionBuilder', function() {
|
||||||
b.setHashToScriptMap(hashMap);
|
b.setHashToScriptMap(hashMap);
|
||||||
b.sign([priv]);
|
b.sign([priv]);
|
||||||
b.isFullySigned().should.equal(true);
|
b.isFullySigned().should.equal(true);
|
||||||
|
|
||||||
var tx = b.build();
|
var tx = b.build();
|
||||||
tx.ins.length.should.equal(1);
|
tx.ins.length.should.equal(1);
|
||||||
tx.outs.length.should.equal(2);
|
tx.outs.length.should.equal(2);
|
||||||
tx.isComplete().should.equal(true);
|
tx.isComplete().should.equal(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('#toObj #fromObj roundtrip', function() {
|
||||||
|
var b = getBuilder2();
|
||||||
|
|
||||||
|
b.isFullySigned().should.equal(false);
|
||||||
|
b.getSelectedUnspent().length.should.equal(2);
|
||||||
|
|
||||||
|
var data =b.toObj();
|
||||||
|
|
||||||
|
var b2 = TransactionBuilder.fromObj(data);
|
||||||
|
var tx = b2.build();
|
||||||
|
should.exist(tx);
|
||||||
|
tx.version.should.equal(1);
|
||||||
|
tx.ins.length.should.equal(2);
|
||||||
|
tx.outs.length.should.equal(2);
|
||||||
|
|
||||||
|
util.valueToBigInt(tx.outs[0].v).cmp(8000000).should.equal(0);
|
||||||
|
|
||||||
|
// remainder is 0.0299 here because unspent select utxos in order
|
||||||
|
util.valueToBigInt(tx.outs[1].v).cmp(2990000).should.equal(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('#toObj #fromObj roundtrip, step signatures p2sh/p2pubkeyhash', function() {
|
||||||
|
var b = getP2shBuilder(1);
|
||||||
|
|
||||||
|
var k1 = testdata.dataUnspentSign.keyStringsP2sh.slice(0,1);
|
||||||
|
var k2 = testdata.dataUnspentSign.keyStringsP2sh.slice(1,2);
|
||||||
|
var k5 = testdata.dataUnspentSign.keyStringsP2sh.slice(4,5);
|
||||||
|
b.isFullySigned().should.equal(false);
|
||||||
|
b.signaturesAdded.should.equal(0);
|
||||||
|
|
||||||
|
var b2 = TransactionBuilder.fromObj(b.toObj());
|
||||||
|
|
||||||
|
b2.sign(k1);
|
||||||
|
|
||||||
|
b2.isFullySigned().should.equal(false);
|
||||||
|
b2.signaturesAdded.should.equal(1);
|
||||||
|
|
||||||
|
var tx = b2.build();
|
||||||
|
tx.ins.length.should.equal(1);
|
||||||
|
tx.outs.length.should.equal(2);
|
||||||
|
tx.isComplete().should.equal(false);
|
||||||
|
b2.signaturesAdded.should.equal(1);
|
||||||
|
|
||||||
|
// Sign with the same
|
||||||
|
var b3 = TransactionBuilder.fromObj(b2.toObj());
|
||||||
|
|
||||||
|
b3.sign(k1);
|
||||||
|
b3.isFullySigned().should.equal(false);
|
||||||
|
b3.signaturesAdded.should.equal(1);
|
||||||
|
|
||||||
|
// Sign with k5
|
||||||
|
var b4 = TransactionBuilder.fromObj(b3.toObj());
|
||||||
|
b4.sign(k5);
|
||||||
|
b4.isFullySigned().should.equal(false);
|
||||||
|
b4.signaturesAdded.should.equal(2);
|
||||||
|
|
||||||
|
var b5 = TransactionBuilder.fromObj(b4.toObj());
|
||||||
|
// Sign k2
|
||||||
|
b5.sign(k2);
|
||||||
|
b5.isFullySigned().should.equal(true);
|
||||||
|
var tx2 = b5.build();
|
||||||
|
tx2.isComplete().should.equal(true);
|
||||||
|
b5.signaturesAdded.should.equal(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue