input tests. function names. getInputKey/getOutputKey.

This commit is contained in:
Christopher Jeffrey 2015-12-17 17:12:49 -08:00
parent 6abfc10a28
commit b74e8de067
2 changed files with 64 additions and 43 deletions

View File

@ -983,7 +983,7 @@ script.isPubkeyhashInput = function isPubkeyhashInput(s, key) {
if (key) if (key)
return utils.isEqual(s[1], key); return utils.isEqual(s[1], key);
return true; return s[1];
}; };
script.isMultisigInput = function isMultisigInput(s, pubs, tx, i) { script.isMultisigInput = function isMultisigInput(s, pubs, tx, i) {
@ -1029,7 +1029,9 @@ script.isScripthashInput = function isScripthashInput(s, redeem) {
if (redeem) if (redeem)
return utils.isEqual(redeem, r); return utils.isEqual(redeem, r);
return true; var keys = script.decode(r).slice(1, -2);
return keys;
}; };
// https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki // https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki

View File

@ -125,7 +125,7 @@ TX.prototype._inputIndex = function _inputIndex(hash, index) {
return -1; return -1;
}; };
TX.prototype.signatureHash = function(i, type) { TX.prototype.signatureHash = function signatureHash(i, type) {
if (typeof i === 'object') if (typeof i === 'object')
i = this.inputs.indexOf(i); i = this.inputs.indexOf(i);
@ -147,7 +147,7 @@ TX.prototype.signatureHash = function(i, type) {
return hash; return hash;
}; };
TX.prototype.signature = function(i, key, type) { TX.prototype.signature = function signature(i, key, type) {
if (typeof i === 'object') if (typeof i === 'object')
i = this.inputs.indexOf(i); i = this.inputs.indexOf(i);
@ -170,7 +170,7 @@ TX.prototype.signature = function(i, key, type) {
}; };
// Build the scriptSigs for inputs, excluding the signatures // Build the scriptSigs for inputs, excluding the signatures
TX.prototype.scriptInput = function(input, pub) { TX.prototype.scriptInput = function scriptInput(input, pub) {
// Get the previous output's subscript // Get the previous output's subscript
var s = input.out.tx.getSubscript(input.out.index); var s = input.out.tx.getSubscript(input.out.index);
@ -230,7 +230,7 @@ TX.prototype.scriptInput = function(input, pub) {
}; };
// Sign the now-built scriptSigs // Sign the now-built scriptSigs
TX.prototype.signInput = function(input, key, type) { TX.prototype.signInput = function signInput(input, key, type) {
if (!type) if (!type)
type = 'all'; type = 'all';
@ -329,7 +329,12 @@ TX.prototype.signInput = function(input, key, type) {
}; };
// Build the scriptSig and sign it // Build the scriptSig and sign it
TX.prototype.scriptSig = function(input, key, pub, type) { TX.prototype.scriptSig = function scriptSig(input, key, pub, type) {
if (!Array.isArray(pub)) {
type = pub;
pub = key.getPublic(true, 'array');
}
// Build script for input // Build script for input
this.scriptInput(input, pub); this.scriptInput(input, pub);
@ -365,7 +370,7 @@ TX.prototype.output = function output(options, value) {
// compat // compat
TX.prototype.out = TX.prototype.output; TX.prototype.out = TX.prototype.output;
TX.prototype.scriptOutput = function(output, options) { TX.prototype.scriptOutput = function scriptOutput(output, options) {
options = options || output; options = options || output;
var script = output.script ? output.script.slice() : []; var script = output.script ? output.script.slice() : [];
@ -805,7 +810,7 @@ TX.prototype.inputAddrs = function inputAddrs() {
}); });
}; };
TX.getInputKey = function(input) { TX.getInputData = function getInputData(input) {
if (!input || !input.script) return; if (!input || !input.script) return;
var script = input.script; var script = input.script;
@ -823,34 +828,38 @@ TX.getInputKey = function(input) {
}; };
} }
if (!input.out.tx) if (bcoin.script.isScripthashInput(script)) {
return false;
var output = input.out.tx.outputs[input.out.index];
if (bcoin.script.isScripthash(script)) {
var pub = script[script.length - 1]; var pub = script[script.length - 1];
var hash = utils.ripesha(pub); var hash = utils.ripesha(pub);
var addr = bcoin.wallet.hash2addr(hash, 'scripthash'); var addr = bcoin.wallet.hash2addr(hash, 'scripthash');
var redeem = bcoin.script.decode(pub); var redeem = bcoin.script.decode(pub);
var keys = TX.getOutputKey({ script: redeem }); var data = TX.getOutputData({ script: redeem });
keys.pub = pub; data.pub = pub;
keys.hash = hash; data.hash = hash;
keys.addr = addr; data.addr = addr;
keys.p2sh = { data.scripthash = {
sig: null,
redeem: redeem, redeem: redeem,
pub: pub, pub: pub,
hash: hash, hash: hash,
addr: addr addr: addr,
m: data.multisig.m,
n: data.multisig.n,
keys: data.multisig.keys,
hashes: data.multisig.hashes,
addrs: data.multisig.addrs
}; };
return keys; return data;
} }
return TX.getOutputKey(output); if (!input.out.tx)
return;
var output = input.out.tx.outputs[input.out.index];
return TX.getOutputData(output);
}; };
TX.getOutputKey = function(output) { TX.getOutputData = function getOutputData(output) {
if (!output || !output.script) return; if (!output || !output.script) return;
var script = output.script; var script = output.script;
@ -878,27 +887,32 @@ TX.getOutputKey = function(output) {
}; };
} }
var pubKeys = bcoin.script.isMultisig(script); var pubs = bcoin.script.isMultisig(script);
if (pubKeys) { if (pubs) {
var keys = pubKeys.map(function(pubKey) { var hash = utils.ripesha(pubs[0]);
var hash = utils.ripesha(pubKey); var addr = bcoin.wallet.hash2addr(hash);
var addr = bcoin.wallet.hash2addr(hash);
return {
pub: pubKey,
hash: hash,
addr: addr
};
});
return { return {
sig: null, sig: null,
pub: keys[0].pub, pub: pubs[0],
hash: keys[0].hash, hash: hash,
addr: keys[0].addr, addr: addr,
multisig: keys keys: pubs,
multisig: {
m: new bn(script[0]).toNumber(),
n: new bn(script[script.length - 2]).toNumber(),
keys: keys,
hashes: keys.map(function(key) {
return utils.ripesha(key);
}),
addrs: keys.map(function(key) {
var hash = utils.ripesha(key);
return bcoin.wallet.hash2addr(hash);
})
}
}; };
} }
if (bcoin.script.isScripthash(script, scriptHash)) { if (bcoin.script.isScripthash(script)) {
var hash = utils.toHex(s[1]); var hash = utils.toHex(s[1]);
var addr = bcoin.wallet.hash2addr(hash, 'scripthash'); var addr = bcoin.wallet.hash2addr(hash, 'scripthash');
return { return {
@ -906,8 +920,7 @@ TX.getOutputKey = function(output) {
pub: null, pub: null,
hash: hash, hash: hash,
addr: addr, addr: addr,
p2sh: { scripthash: {
sig: null,
redeem: null, redeem: null,
pub: null, pub: null,
hash: hash, hash: hash,
@ -915,6 +928,12 @@ TX.getOutputKey = function(output) {
} }
}; };
} }
if (bcoin.script.isColored(script)) {
return {
data: bcoin.script.colored(script)
};
}
}; };
TX.prototype.getFee = function getFee() { TX.prototype.getFee = function getFee() {