add support code for copay's new tx proposal check

This commit is contained in:
Manuel Araoz 2014-07-24 16:16:47 -03:00
parent b222550dd0
commit c0d51916df
3 changed files with 65 additions and 5 deletions

View File

@ -115,6 +115,13 @@ Script.prototype.isMultiSig = function() {
this.chunks[this.chunks.length - 1] == Opcode.map.OP_CHECKMULTISIG);
};
Script.prototype.isPubkeyHashScript = function() {
// TODO: add more restrictions to chunks
return (this.chunks.length == 2 &&
Buffer.isBuffer(this.chunks[0]) &&
Buffer.isBuffer(this.chunks[1]));
};
Script.prototype.isP2shScriptSig = function() {
if (!isSmallIntOp(this.chunks[0]) || this.chunks[0] !== 0)
return false;
@ -133,20 +140,65 @@ Script.prototype.isMultiSigScriptSig = function() {
Script.prototype.countSignatures = function() {
var ret = 0;
var l = this.chunks.length;
// Multisig?
if (this.isMultiSigScriptSig()) {
ret = l - 1;
} else if (this.isP2shScriptSig()) {
}
// p2sh
else if (this.isP2shScriptSig()) {
ret = l - 2;
}
// p2pubkey or p2pubkeyhash
// p2pubkeyhash
else if (this.isPubkeyHashScript()) {
ret = 1;
}
// p2pubkey
else {
ret = buffertools.compare(this.getBuffer(), util.EMPTY_BUFFER) === 0 ? 0 : 1;
ret = 0;
}
return ret;
};
Script.prototype.getSignatures = function() {
ret = [];
var l = this.chunks.length;
// Multisig?
if (this.isMultiSigScriptSig()) {
for(var i = 1; i<l; i++) {
ret.push(this.chunks[i]);
}
}
// p2sh
else if (this.isP2shScriptSig()) {
for (var i=1; i<l-1; i++) {
ret.push(this.chunks[i]);
}
}
// p2pubkeyhash
else if (this.isPubkeyHashScript()) {
ret.push(this.chunks[0]);
}
// p2pubkey
else {
// no signatures
}
return ret;
};
Script.prototype.getHashType = function() {
var sigs = this.getSignatures();
var hashType = null;
for (var i=0; i<sigs.length; i++) {
var sig = sigs[i];
var hashTypeI = sig[sig.length - 1];
if (hashType !== null && hashType !== hashTypeI) return null;
hashType = hashTypeI;
}
return hashType;
};
Script.prototype.countMissingSignatures = function() {
if (this.isMultiSig()) {
log.debug("Can not count missing signatures on normal Multisig script");

View File

@ -12,6 +12,7 @@ var buffertools = require('buffertools');
var error = require('../util/error');
var WalletKey = require('./WalletKey');
var PrivateKey = require('./PrivateKey');
var preconditions = require('preconditions').singleton();
var COINBASE_OP = Buffer.concat([util.NULL_HASH, new Buffer('FFFFFFFF', 'hex')]);
var FEE_PER_1000B_SAT = parseInt(0.0001 * util.COIN);
@ -400,6 +401,13 @@ var oneBuffer = function() {
return ret; // return 1 bug
};
Transaction.prototype.getHashType = function(inIndex) {
preconditions.checkArgument(inIndex < this.ins.length);
var input = this.ins[inIndex];
var scriptSig = input.getScript();
return scriptSig.getHashType();
};
Transaction.prototype.hashForSignature =
function hashForSignature(script, inIndex, hashType) {

View File

@ -133,7 +133,7 @@ describe('Transaction', function() {
tx2.getNormalizedHash().toString('hex').should.equal('e298bbf3734898581b8e342f2064236abf0acca6ac7e9a3009a16ef7b64d4983');
});
describe('#send and receiving addresses', function() {
describe.only('#send and receiving addresses', function() {
var a1 = 'n1pKARYYUnZwxBuGj3y7WqVDu6VLN7n971';
var a2 = 'mtxYYJXZJmQc2iJRHQ4RZkfxU5K7TE2qMJ';
var utxos = [{