Various stylistic changes
This commit is contained in:
parent
f580516c06
commit
ed137a3b9d
4 changed files with 35 additions and 20 deletions
|
@ -36,7 +36,7 @@ module.exports.encode = function (input) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return chars.reverse().join('');
|
return chars.reverse().join('');
|
||||||
},
|
}
|
||||||
|
|
||||||
// decode a base58 string into a byte array
|
// decode a base58 string into a byte array
|
||||||
// input should be a base58 encoded string
|
// input should be a base58 encoded string
|
||||||
|
@ -88,10 +88,13 @@ module.exports.checkDecode = function(input) {
|
||||||
var bytes = module.exports.decode(input),
|
var bytes = module.exports.decode(input),
|
||||||
front = bytes.slice(0,bytes.length-4),
|
front = bytes.slice(0,bytes.length-4),
|
||||||
back = bytes.slice(bytes.length-4);
|
back = bytes.slice(bytes.length-4);
|
||||||
|
|
||||||
var checksum = getChecksum(front)
|
var checksum = getChecksum(front)
|
||||||
|
|
||||||
if (""+checksum != ""+back) {
|
if (""+checksum != ""+back) {
|
||||||
throw new Error("Checksum failed");
|
throw new Error("Checksum failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
var o = front.slice(1);
|
var o = front.slice(1);
|
||||||
o.version = front[0];
|
o.version = front[0];
|
||||||
return o;
|
return o;
|
||||||
|
@ -103,4 +106,3 @@ function getChecksum(bytes) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.getChecksum = getChecksum
|
module.exports.getChecksum = getChecksum
|
||||||
|
|
||||||
|
|
|
@ -53,8 +53,8 @@ exports.base64ToBytes = function(base64) {
|
||||||
// Remove non-base-64 characters
|
// Remove non-base-64 characters
|
||||||
base64 = base64.replace(/[^A-Z0-9+\/]/ig, '');
|
base64 = base64.replace(/[^A-Z0-9+\/]/ig, '');
|
||||||
|
|
||||||
var bytes = []
|
var bytes = [];
|
||||||
, imod4 = 0
|
var imod4 = 0;
|
||||||
|
|
||||||
for (var i = 0; i < base64.length; imod4 = ++i % 4) {
|
for (var i = 0; i < base64.length; imod4 = ++i % 4) {
|
||||||
if (!imod4) continue
|
if (!imod4) continue
|
||||||
|
|
|
@ -144,4 +144,4 @@ for(var i in Opcode.map) {
|
||||||
Opcode.reverseMap[Opcode.map[i]] = i
|
Opcode.reverseMap[Opcode.map[i]] = i
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Opcode
|
module.exports = Opcode;
|
||||||
|
|
|
@ -13,12 +13,13 @@ var Transaction = function (doc) {
|
||||||
this.locktime = 0;
|
this.locktime = 0;
|
||||||
this.ins = [];
|
this.ins = [];
|
||||||
this.outs = [];
|
this.outs = [];
|
||||||
this.defaultSequence = [255, 255, 255, 255] // 0xFFFFFFFF
|
this.defaultSequence = [255, 255, 255, 255]; // 0xFFFFFFFF
|
||||||
|
|
||||||
if (doc) {
|
if (doc) {
|
||||||
if (typeof doc == "string" || Array.isArray(doc)) {
|
if (typeof doc == "string" || Array.isArray(doc)) {
|
||||||
doc = Transaction.deserialize(doc)
|
doc = Transaction.deserialize(doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doc.hash) this.hash = doc.hash;
|
if (doc.hash) this.hash = doc.hash;
|
||||||
if (doc.version) this.version = doc.version;
|
if (doc.version) this.version = doc.version;
|
||||||
if (doc.locktime) this.locktime = doc.locktime;
|
if (doc.locktime) this.locktime = doc.locktime;
|
||||||
|
@ -27,13 +28,14 @@ var Transaction = function (doc) {
|
||||||
this.addInput(new TransactionIn(doc.ins[i]));
|
this.addInput(new TransactionIn(doc.ins[i]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doc.outs && doc.outs.length) {
|
if (doc.outs && doc.outs.length) {
|
||||||
for (var i = 0; i < doc.outs.length; i++) {
|
for (var i = 0; i < doc.outs.length; i++) {
|
||||||
this.addOutput(new TransactionOut(doc.outs[i]));
|
this.addOutput(new TransactionOut(doc.outs[i]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.hash = this.hash || this.getHash()
|
this.hash = this.hash || this.getHash();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -86,11 +88,13 @@ Transaction.prototype.addOutput = function (address, value) {
|
||||||
this.outs.push(arguments[0]);
|
this.outs.push(arguments[0]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arguments[0].indexOf(':') >= 0) {
|
if (arguments[0].indexOf(':') >= 0) {
|
||||||
var args = arguments[0].split(':');
|
var args = arguments[0].split(':');
|
||||||
address = args[0];
|
address = args[0];
|
||||||
value = parseInt(args[1]);
|
value = parseInt(args[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.outs.push(new TransactionOut({
|
this.outs.push(new TransactionOut({
|
||||||
value: value,
|
value: value,
|
||||||
script: Script.createOutputScript(address)
|
script: Script.createOutputScript(address)
|
||||||
|
@ -106,7 +110,7 @@ Transaction.prototype.addOutput = function (address, value) {
|
||||||
*/
|
*/
|
||||||
Transaction.prototype.serialize = function () {
|
Transaction.prototype.serialize = function () {
|
||||||
var buffer = [];
|
var buffer = [];
|
||||||
buffer = buffer.concat(convert.numToBytes(parseInt(this.version),4));
|
buffer = buffer.concat(convert.numToBytes(parseInt(this.version), 4));
|
||||||
buffer = buffer.concat(convert.numToVarInt(this.ins.length));
|
buffer = buffer.concat(convert.numToVarInt(this.ins.length));
|
||||||
for (var i = 0; i < this.ins.length; i++) {
|
for (var i = 0; i < this.ins.length; i++) {
|
||||||
var txin = this.ins[i];
|
var txin = this.ins[i];
|
||||||
|
@ -115,7 +119,8 @@ Transaction.prototype.serialize = function () {
|
||||||
// else use little-endian hashes? No idea...
|
// else use little-endian hashes? No idea...
|
||||||
buffer = buffer.concat(convert.hexToBytes(txin.outpoint.hash).reverse());
|
buffer = buffer.concat(convert.hexToBytes(txin.outpoint.hash).reverse());
|
||||||
|
|
||||||
buffer = buffer.concat(convert.numToBytes(parseInt(txin.outpoint.index),4));
|
buffer = buffer.concat(convert.numToBytes(parseInt(txin.outpoint.index), 4));
|
||||||
|
|
||||||
var scriptBytes = txin.script.buffer;
|
var scriptBytes = txin.script.buffer;
|
||||||
buffer = buffer.concat(convert.numToVarInt(scriptBytes.length));
|
buffer = buffer.concat(convert.numToVarInt(scriptBytes.length));
|
||||||
buffer = buffer.concat(scriptBytes);
|
buffer = buffer.concat(scriptBytes);
|
||||||
|
@ -125,11 +130,13 @@ Transaction.prototype.serialize = function () {
|
||||||
for (var i = 0; i < this.outs.length; i++) {
|
for (var i = 0; i < this.outs.length; i++) {
|
||||||
var txout = this.outs[i];
|
var txout = this.outs[i];
|
||||||
buffer = buffer.concat(convert.numToBytes(txout.value,8));
|
buffer = buffer.concat(convert.numToBytes(txout.value,8));
|
||||||
|
|
||||||
var scriptBytes = txout.script.buffer;
|
var scriptBytes = txout.script.buffer;
|
||||||
buffer = buffer.concat(convert.numToVarInt(scriptBytes.length));
|
buffer = buffer.concat(convert.numToVarInt(scriptBytes.length));
|
||||||
buffer = buffer.concat(scriptBytes);
|
buffer = buffer.concat(scriptBytes);
|
||||||
}
|
});
|
||||||
buffer = buffer.concat(convert.numToBytes(parseInt(this.locktime),4));
|
|
||||||
|
buffer = buffer.concat(convert.numToBytes(parseInt(this.locktime), 4));
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
};
|
};
|
||||||
|
@ -191,9 +198,9 @@ function (connectedScript, inIndex, hashType)
|
||||||
|
|
||||||
var buffer = txTmp.serialize();
|
var buffer = txTmp.serialize();
|
||||||
|
|
||||||
buffer = buffer.concat(convert.numToBytes(parseInt(hashType),4));
|
buffer = buffer.concat(convert.numToBytes(parseInt(hashType), 4));
|
||||||
|
|
||||||
buffer = convert.bytesToWordArray(buffer);
|
buffer = convert.bytesToWordArray(buffer);
|
||||||
|
|
||||||
return convert.wordArrayToBytes(SHA256(SHA256(buffer)));
|
return convert.wordArrayToBytes(SHA256(SHA256(buffer)));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -291,7 +298,7 @@ Transaction.deserialize = function(buffer) {
|
||||||
Transaction.prototype.sign = function(index, key, type) {
|
Transaction.prototype.sign = function(index, key, type) {
|
||||||
type = type || SIGHASH_ALL;
|
type = type || SIGHASH_ALL;
|
||||||
key = new ECKey(key);
|
key = new ECKey(key);
|
||||||
|
|
||||||
// TODO: getPub is slow, sha256ripe160 probably is too.
|
// TODO: getPub is slow, sha256ripe160 probably is too.
|
||||||
// This could be sped up a lot by providing these as inputs.
|
// This could be sped up a lot by providing these as inputs.
|
||||||
var pub = key.getPub().export('bytes'),
|
var pub = key.getPub().export('bytes'),
|
||||||
|
@ -305,6 +312,7 @@ Transaction.prototype.sign = function(index, key, type) {
|
||||||
// Takes outputs of the form [{ output: 'txhash:index', address: 'address' },...]
|
// Takes outputs of the form [{ output: 'txhash:index', address: 'address' },...]
|
||||||
Transaction.prototype.signWithKeys = function(keys, outputs, type) {
|
Transaction.prototype.signWithKeys = function(keys, outputs, type) {
|
||||||
type = type || SIGHASH_ALL;
|
type = type || SIGHASH_ALL;
|
||||||
|
|
||||||
var addrdata = keys.map(function(key) {
|
var addrdata = keys.map(function(key) {
|
||||||
key = new ECKey(key);
|
key = new ECKey(key);
|
||||||
return {
|
return {
|
||||||
|
@ -312,14 +320,17 @@ Transaction.prototype.signWithKeys = function(keys, outputs, type) {
|
||||||
address: key.getAddress().toString()
|
address: key.getAddress().toString()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var hmap = {};
|
var hmap = {};
|
||||||
for (var o in outputs) {
|
for (var o in outputs) {
|
||||||
hmap[outputs[o].output] = outputs[o];
|
hmap[outputs[o].output] = outputs[o];
|
||||||
}
|
}
|
||||||
for (var i = 0; i < this.ins.length; i++) {
|
for (var i = 0; i < this.ins.length; i++) {
|
||||||
var outpoint = this.ins[i].outpoint.hash+':'+this.ins[i].outpoint.index,
|
var outpoint = this.ins[i].outpoint.hash + ':' + this.ins[i].outpoint.index;
|
||||||
histItem = hmap[outpoint];
|
var histItem = hmap[outpoint];
|
||||||
|
|
||||||
if (!histItem) continue;
|
if (!histItem) continue;
|
||||||
|
|
||||||
var thisInputAddrdata = addrdata.filter(function(a) {
|
var thisInputAddrdata = addrdata.filter(function(a) {
|
||||||
return a.address == histItem.address;
|
return a.address == histItem.address;
|
||||||
});
|
});
|
||||||
|
@ -410,6 +421,8 @@ TransactionOut.prototype.clone = function ()
|
||||||
return newTxout;
|
return newTxout;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.Transaction = Transaction;
|
module.exports = {
|
||||||
module.exports.TransactionIn = TransactionIn;
|
Transaction: Transaction,
|
||||||
module.exports.TransactionOut = TransactionOut;
|
TransactionIn: TransactionIn,
|
||||||
|
TransactionOut: TransactionOut
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue