Merge pull request #451 from maraoz/add/support-methods

Add copay support methods
This commit is contained in:
Ryan X. Charles 2014-07-24 17:13:11 -04:00
commit 29cbf24d93
4 changed files with 69 additions and 8 deletions

File diff suppressed because one or more lines are too long

View File

@ -17,6 +17,7 @@ var NetworkMonitor = function(peerman) {
util.inherits(NetworkMonitor, EventEmitter);
NetworkMonitor.create = function(config) {
this.config = config;
var peerman = new PeerManager({
network: config.networkName
});

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) {