Browse Source

Added to convert.js and some more multisig methods

hk-custom-address
vub 11 years ago
parent
commit
65abb85668
  1. 6
      bitcoinjs-min.js
  2. 41
      src/convert.js
  3. 28
      src/script.js
  4. 14
      src/transaction.js

6
bitcoinjs-min.js

File diff suppressed because one or more lines are too long

41
src/convert.js

@ -2,20 +2,20 @@
var base64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 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 // Convert a byte array to a hex string
module.exports.bytesToHex = function(bytes) { module.exports.bytesToHex = function(bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) { return bytes.map(function(x) { return x.toString(16).lpad('0',2) }).join('');
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xF).toString(16));
}
return hex.join("");
}; };
// Convert a hex string to a byte array // Convert a hex string to a byte array
module.exports.hexToBytes = function(hex) { module.exports.hexToBytes = function(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2) return hex.match(/../g).map(function(x) { return parseInt(x,16) });
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
} }
// Convert a byte array to a base-64 string // Convert a byte array to a base-64 string
@ -53,7 +53,26 @@ module.exports.base64ToBytes = function(base64) {
return bytes; return bytes;
} }
// utf8 and binary? // Hex only (allowing bin would be potentially risky, as 01010101 = \x01 * 4 or 85)
//stringToBytes module.exports.coerceToBytes = function(input) {
//bytesToString 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

28
src/script.js

@ -155,6 +155,8 @@ Script.prototype.simpleOutHash = function ()
return this.chunks[2]; return this.chunks[2];
case 'Pubkey': case 'Pubkey':
return Bitcoin.Util.sha256ripe160(this.chunks[0]); return Bitcoin.Util.sha256ripe160(this.chunks[0]);
case 'Multisig':
return Bitcoin.Util.sha256ripe160(this.buffer);
default: default:
throw new Error("Encountered non-standard scriptPubKey: " + this.getOutType()); throw new Error("Encountered non-standard scriptPubKey: " + this.getOutType());
} }
@ -186,6 +188,9 @@ Script.prototype.simpleOutPubKeyHash = Script.prototype.simpleOutHash;
* Paying to a public key directly. * Paying to a public key directly.
* [sig] * [sig]
* *
* Multisig:
* Paying to M-of-N public keys.
*
* Strange: * Strange:
* Any other script (no template matched). * Any other script (no template matched).
*/ */
@ -200,6 +205,12 @@ Script.prototype.getInType = function ()
util.isArray(this.chunks[0]) && util.isArray(this.chunks[0]) &&
util.isArray(this.chunks[1])) { util.isArray(this.chunks[1])) {
return 'Address'; 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 { } else {
return 'Strange'; return 'Strange';
} }
@ -337,6 +348,8 @@ Script.createMultiSigOutputScript = function (m, pubkeys)
{ {
var script = new Script(); var script = new Script();
pubkeys = pubkeys.sort();
script.writeOp(Opcode.map.OP_1 + m - 1); script.writeOp(Opcode.map.OP_1 + m - 1);
for (var i = 0; i < pubkeys.length; ++i) { for (var i = 0; i < pubkeys.length; ++i) {
@ -361,6 +374,21 @@ Script.createInputScript = function (signature, pubKey)
return script; 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 () Script.prototype.clone = function ()
{ {
return new Script(this.buffer); return new Script(this.buffer);

14
src/transaction.js

@ -496,16 +496,12 @@ Transaction.prototype.p2shsign = function(index, script, key, type) {
Transaction.prototype.multisign = Transaction.prototype.p2shsign; Transaction.prototype.multisign = Transaction.prototype.p2shsign;
// In progress Transaction.prototype.validateSig = function(index,script,sig,pub) {
/*Transaction.prototype.validateInput = function(index,script,sig,pub) {
script = new Bitcoin.Script(script); script = new Bitcoin.Script(script);
var hash = this.hashTransactionForSignature(script,i,1);
scriptBytes = bw.h2b(script), return Bitcoin.ECDSA.verify(hash, conv.coerceToBytes(sig),
scriptObj = new Bitcoin.Script(scriptBytes), conv.coerceToBytes(pub));
hash = txObj.hashTransactionForSignature(scriptObj,i,1); }
return Bitcoin.ECDSA.verify(hash, bw.h2b(sig),
bw.h2b(pub));
}*/
var TransactionIn = function (data) var TransactionIn = function (data)

Loading…
Cancel
Save