Added to convert.js and some more multisig methods
This commit is contained in:
parent
e43d23235b
commit
65abb85668
4 changed files with 66 additions and 23 deletions
6
bitcoinjs-min.js
vendored
6
bitcoinjs-min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -2,20 +2,20 @@
|
|||
|
||||
var base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
String.prototype.lpad = function(padString, length) {
|
||||
var str = this;
|
||||
while (str.length < length) str = padString + str;
|
||||
return str;
|
||||
}
|
||||
|
||||
// Convert a byte array to a hex string
|
||||
module.exports.bytesToHex = function(bytes) {
|
||||
for (var hex = [], i = 0; i < bytes.length; i++) {
|
||||
hex.push((bytes[i] >>> 4).toString(16));
|
||||
hex.push((bytes[i] & 0xF).toString(16));
|
||||
}
|
||||
return hex.join("");
|
||||
return bytes.map(function(x) { return x.toString(16).lpad('0',2) }).join('');
|
||||
};
|
||||
|
||||
// Convert a hex string to a byte array
|
||||
module.exports.hexToBytes = function(hex) {
|
||||
for (var bytes = [], c = 0; c < hex.length; c += 2)
|
||||
bytes.push(parseInt(hex.substr(c, 2), 16));
|
||||
return bytes;
|
||||
return hex.match(/../g).map(function(x) { return parseInt(x,16) });
|
||||
}
|
||||
|
||||
// Convert a byte array to a base-64 string
|
||||
|
@ -53,7 +53,26 @@ module.exports.base64ToBytes = function(base64) {
|
|||
return bytes;
|
||||
}
|
||||
|
||||
// utf8 and binary?
|
||||
//stringToBytes
|
||||
//bytesToString
|
||||
// Hex only (allowing bin would be potentially risky, as 01010101 = \x01 * 4 or 85)
|
||||
module.exports.coerceToBytes = function(input) {
|
||||
if (typeof input == "string") return Crypto.util.hexToBytes(input);
|
||||
return input;
|
||||
}
|
||||
|
||||
module.exports.binToBytes = function(bin) {
|
||||
return bin.match(/......../g).map(function(x) { return parseInt(x,2) });
|
||||
}
|
||||
|
||||
module.exports.bytesToBin = function(bytes) {
|
||||
return bytes.map(function(x) { return x.toString(2).lpad('0',8) }).join('');
|
||||
}
|
||||
|
||||
module.exports.bytesToString = function(bytes) {
|
||||
return bytes.map(String.fromCharCode).join('');
|
||||
}
|
||||
|
||||
module.exports.stringToBytes = function(string) {
|
||||
return string.split('').map(function(x) { return x.charCodeAt(0) });
|
||||
}
|
||||
|
||||
// utf8
|
||||
|
|
|
@ -155,6 +155,8 @@ Script.prototype.simpleOutHash = function ()
|
|||
return this.chunks[2];
|
||||
case 'Pubkey':
|
||||
return Bitcoin.Util.sha256ripe160(this.chunks[0]);
|
||||
case 'Multisig':
|
||||
return Bitcoin.Util.sha256ripe160(this.buffer);
|
||||
default:
|
||||
throw new Error("Encountered non-standard scriptPubKey: " + this.getOutType());
|
||||
}
|
||||
|
@ -185,6 +187,9 @@ Script.prototype.simpleOutPubKeyHash = Script.prototype.simpleOutHash;
|
|||
* Pubkey:
|
||||
* Paying to a public key directly.
|
||||
* [sig]
|
||||
*
|
||||
* Multisig:
|
||||
* Paying to M-of-N public keys.
|
||||
*
|
||||
* Strange:
|
||||
* Any other script (no template matched).
|
||||
|
@ -200,6 +205,12 @@ Script.prototype.getInType = function ()
|
|||
util.isArray(this.chunks[0]) &&
|
||||
util.isArray(this.chunks[1])) {
|
||||
return 'Address';
|
||||
} else if (this.chunks[0] == Opcode.map.OP_0 &&
|
||||
this.chunks.slice(1).reduce(function(t,chunk,i) {
|
||||
return t && util.isArray(chunk)
|
||||
&& (chunk[0] == 48 || i == this.chunks.length - 1);
|
||||
},true)) {
|
||||
return 'Multisig';
|
||||
} else {
|
||||
return 'Strange';
|
||||
}
|
||||
|
@ -336,6 +347,8 @@ Script.prototype.extractAddresses = function (addresses)
|
|||
Script.createMultiSigOutputScript = function (m, pubkeys)
|
||||
{
|
||||
var script = new Script();
|
||||
|
||||
pubkeys = pubkeys.sort();
|
||||
|
||||
script.writeOp(Opcode.map.OP_1 + m - 1);
|
||||
|
||||
|
@ -361,6 +374,21 @@ Script.createInputScript = function (signature, pubKey)
|
|||
return script;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a multisig input
|
||||
*/
|
||||
Script.createMultiSigInputScript = function(signatures, script)
|
||||
{
|
||||
script = new Script(script);
|
||||
var k = script.chunks[0][0];
|
||||
if (signatures.length < k) return false; //Not enough sigs
|
||||
var inScript = new Script();
|
||||
inScript.writeOp(Opcode.map.OP_0);
|
||||
signatures.map(inScript.writeBytes.bind(inScript));
|
||||
inScript.writeBytes(script.buffer);
|
||||
return inScript;
|
||||
}
|
||||
|
||||
Script.prototype.clone = function ()
|
||||
{
|
||||
return new Script(this.buffer);
|
||||
|
|
|
@ -496,16 +496,12 @@ Transaction.prototype.p2shsign = function(index, script, key, type) {
|
|||
|
||||
Transaction.prototype.multisign = Transaction.prototype.p2shsign;
|
||||
|
||||
// In progress
|
||||
/*Transaction.prototype.validateInput = function(index,script,sig,pub) {
|
||||
Transaction.prototype.validateSig = function(index,script,sig,pub) {
|
||||
script = new Bitcoin.Script(script);
|
||||
|
||||
scriptBytes = bw.h2b(script),
|
||||
scriptObj = new Bitcoin.Script(scriptBytes),
|
||||
hash = txObj.hashTransactionForSignature(scriptObj,i,1);
|
||||
return Bitcoin.ECDSA.verify(hash, bw.h2b(sig),
|
||||
bw.h2b(pub));
|
||||
}*/
|
||||
var hash = this.hashTransactionForSignature(script,i,1);
|
||||
return Bitcoin.ECDSA.verify(hash, conv.coerceToBytes(sig),
|
||||
conv.coerceToBytes(pub));
|
||||
}
|
||||
|
||||
|
||||
var TransactionIn = function (data)
|
||||
|
|
Loading…
Reference in a new issue