Many small improvements

This commit is contained in:
vub 2013-11-02 06:20:09 -04:00
parent 45ec63d2f1
commit a34bc9e2ea
4 changed files with 45 additions and 39 deletions

6
bitcoinjs-min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -42,7 +42,6 @@ var ECKey = function (input) {
this.compressed = true; this.compressed = true;
} else { } else {
// hex string? // hex string?
// Prepend zero byte to prevent interpretation as negative integer
this.priv = BigInteger.fromByteArrayUnsigned(conv.hexToBytes(input)); this.priv = BigInteger.fromByteArrayUnsigned(conv.hexToBytes(input));
} }
} }

View file

@ -121,19 +121,17 @@ Script.prototype.getOutType = function () {
if (this.chunks[this.chunks.length-1] == Opcode.map.OP_CHECKMULTISIG && if (this.chunks[this.chunks.length-1] == Opcode.map.OP_CHECKMULTISIG &&
this.chunks[this.chunks.length-2] <= 3) { this.chunks[this.chunks.length-2] <= 3) {
// Transfer to M-OF-N // Transfer to M-OF-N
return 'Multisig'; return 'P2SH';
} else if (this.chunks.length == 5 && }
else if (this.chunks.length == 5 &&
this.chunks[0] == Opcode.map.OP_DUP && this.chunks[0] == Opcode.map.OP_DUP &&
this.chunks[1] == Opcode.map.OP_HASH160 && this.chunks[1] == Opcode.map.OP_HASH160 &&
this.chunks[3] == Opcode.map.OP_EQUALVERIFY && this.chunks[3] == Opcode.map.OP_EQUALVERIFY &&
this.chunks[4] == Opcode.map.OP_CHECKSIG) { this.chunks[4] == Opcode.map.OP_CHECKSIG) {
// Transfer to Bitcoin address // Transfer to Bitcoin address
return 'Address';
} else if (this.chunks.length == 2 &&
this.chunks[1] == Opcode.map.OP_CHECKSIG) {
// Transfer to IP address
return 'Pubkey'; return 'Pubkey';
} else { }
else {
return 'Strange'; return 'Strange';
} }
} }
@ -145,10 +143,9 @@ Script.prototype.getOutType = function () {
Script.prototype.toScriptHash = function () { Script.prototype.toScriptHash = function () {
var outType = this.getOutType(); var outType = this.getOutType();
return outType == 'Address' ? this.chunks[2] return outType == 'Pubkey' ? this.chunks[2]
: outType == 'Pubkey' ? util.sha256ripe160(this.chunks[0]) : outType == 'P2SH' ? util.sha256ripe160(this.buffer)
: outType == 'Multisig' ? util.sha256ripe160(this.buffer) : util.sha256ripe160(this.buffer)
: util.sha256ripe160(this.buffer)
}; };
Script.prototype.toAddress = function() { Script.prototype.toAddress = function() {
@ -368,7 +365,7 @@ Script.createMultiSigInputScript = function(signatures, script)
if (signatures.length < k) return false; //Not enough sigs if (signatures.length < k) return false; //Not enough sigs
var inScript = new Script(); var inScript = new Script();
inScript.writeOp(Opcode.map.OP_0); inScript.writeOp(Opcode.map.OP_0);
signatures.map(inScript.writeBytes.bind(inScript)); signatures.map(function(sig) { inScript.writeBytes(sig) });
inScript.writeBytes(script.buffer); inScript.writeBytes(script.buffer);
return inScript; return inScript;
} }

View file

@ -9,30 +9,33 @@ var ECDSA = require('./ecdsa');
var Address = require('./address'); var Address = require('./address');
var Transaction = function (doc) { var Transaction = function (doc) {
this.version = 1; this.version = 1;
this.lock_time = 0; this.lock_time = 0;
this.ins = []; this.ins = [];
this.outs = []; this.outs = [];
this.timestamp = null; this.timestamp = null;
this.block = null; this.block = null;
if (doc) { if (doc) {
if (doc.hash) this.hash = doc.hash; if (typeof doc == "string" || util.isArray(doc)) {
if (doc.version) this.version = doc.version; doc = Transaction.deserialize(doc)
if (doc.lock_time) this.lock_time = doc.lock_time; }
if (doc.ins && doc.ins.length) { if (doc.hash) this.hash = doc.hash;
for (var i = 0; i < doc.ins.length; i++) { if (doc.version) this.version = doc.version;
this.addInput(new TransactionIn(doc.ins[i])); if (doc.lock_time) this.lock_time = doc.lock_time;
} if (doc.ins && doc.ins.length) {
for (var i = 0; i < doc.ins.length; i++) {
this.addInput(new TransactionIn(doc.ins[i]));
}
}
if (doc.outs && doc.outs.length) {
for (var i = 0; i < doc.outs.length; i++) {
this.addOutput(new TransactionOut(doc.outs[i]));
}
}
if (doc.timestamp) this.timestamp = doc.timestamp;
if (doc.block) this.block = doc.block;
} }
if (doc.outs && doc.outs.length) {
for (var i = 0; i < doc.outs.length; i++) {
this.addOutput(new TransactionOut(doc.outs[i]));
}
}
if (doc.timestamp) this.timestamp = doc.timestamp;
if (doc.block) this.block = doc.block;
}
}; };
/** /**
@ -412,6 +415,9 @@ Transaction.prototype.calcImpact = function (wallet) {
*/ */
Transaction.deserialize = function(buffer) { Transaction.deserialize = function(buffer) {
if (typeof buffer == "string") {
buffer = conv.hexToBytes(buffer)
}
var pos = 0; var pos = 0;
var readAsInt = function(bytes) { var readAsInt = function(bytes) {
if (bytes == 0) return 0; if (bytes == 0) return 0;
@ -516,6 +522,10 @@ Transaction.prototype.p2shsign = function(index, script, key, type) {
Transaction.prototype.multisign = Transaction.prototype.p2shsign; Transaction.prototype.multisign = Transaction.prototype.p2shsign;
Transaction.prototype.applyMultisigs = function(index, script, sigs, type) {
this.ins[index].script = Script.createMultiSigInputScript(sigs, script);
}
Transaction.prototype.validateSig = function(index, script, sig, pub) { Transaction.prototype.validateSig = function(index, script, sig, pub) {
script = new Script(script); script = new Script(script);
var hash = this.hashTransactionForSignature(script,index,1); var hash = this.hashTransactionForSignature(script,index,1);