major cleanup
This commit is contained in:
parent
b05ee032c5
commit
46f1d9b73b
157
lib/bitcoind.js
157
lib/bitcoind.js
@ -299,13 +299,13 @@ function Transaction(data) {
|
|||||||
return new Transaction(data);
|
return new Transaction(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.nMinTxFee = data.nMinTxFee || new bn(0);
|
this.nMinTxFee = data.nMinTxFee || data.minTxFee || 1000;
|
||||||
this.nMinRelayTxFee = data.nMinRelayTxFee || new bn(0);
|
this.nMinRelayTxFee = data.nMinRelayTxFee || data.minRelayTxFee || 1000;
|
||||||
this.CURRENT_VERSION = 1;
|
this.CURRENT_VERSION = 1;
|
||||||
this.nVersion = data.nVersion || -1;
|
this.nVersion = data.nVersion || data.version || this.CURRENT_VERSION;
|
||||||
this.vin = data.vin || [];
|
this.vin = data.vin || [];
|
||||||
this.vout = data.vout || [];
|
this.vout = data.vout || [];
|
||||||
this.nLockTime = data.nLockTime || null;
|
this.nLockTime = data.nLockTime || data.locktime || 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.prototype.getSerializeSize = function() {
|
Transaction.prototype.getSerializeSize = function() {
|
||||||
@ -365,7 +365,7 @@ Transaction.prototype.toHex = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Transaction.toHex = function(tx) {
|
Transaction.toHex = function(tx) {
|
||||||
return new bn(Transaction.binary(tx)).toString('hex');
|
return new bn(Transaction.toBinary(tx)).toString('hex');
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -396,7 +396,7 @@ Bitcoin.prototype._broadcastTx = function(tx, options, callback) {
|
|||||||
callback);
|
callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
Transaction.binary = function(tx) {
|
Transaction.toBinary = function(tx) {
|
||||||
var p = [];
|
var p = [];
|
||||||
var off = utils.writeU32(p, tx.nVersion || tx.version, 0);
|
var off = utils.writeU32(p, tx.nVersion || tx.version, 0);
|
||||||
off += utils.varint(p, tx.vin.length, off);
|
off += utils.varint(p, tx.vin.length, off);
|
||||||
@ -411,7 +411,6 @@ Transaction.binary = function(tx) {
|
|||||||
off += utils.copy(new bn(input.txid, 'hex').toArray(), p, off, true);
|
off += utils.copy(new bn(input.txid, 'hex').toArray(), p, off, true);
|
||||||
off += utils.writeU32(p, input.vout, off);
|
off += utils.writeU32(p, input.vout, off);
|
||||||
|
|
||||||
// var s = script.encode(input.scriptSig.asm.split(' '));
|
|
||||||
var s = script.encode(new bn(input.scriptSig.hex, 'hex').toArray());
|
var s = script.encode(new bn(input.scriptSig.hex, 'hex').toArray());
|
||||||
off += utils.varint(p, s.length, off);
|
off += utils.varint(p, s.length, off);
|
||||||
off += utils.copy(s, p, off, true);
|
off += utils.copy(s, p, off, true);
|
||||||
@ -432,7 +431,6 @@ Transaction.binary = function(tx) {
|
|||||||
p[off] = 0;
|
p[off] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//var s = script.encode(output.scriptPubKey.asm.split(' '));
|
|
||||||
var s = script.encode(new bn(output.scriptPubKey.hex, 'hex').toArray());
|
var s = script.encode(new bn(output.scriptPubKey.hex, 'hex').toArray());
|
||||||
off += utils.varint(p, s.length, off);
|
off += utils.varint(p, s.length, off);
|
||||||
off += utils.copy(s, p, off, true);
|
off += utils.copy(s, p, off, true);
|
||||||
@ -462,15 +460,12 @@ script.encode = function encode(s) {
|
|||||||
} else if (1 <= instr.length && instr.length <= 0x4b) {
|
} else if (1 <= instr.length && instr.length <= 0x4b) {
|
||||||
res = res.concat(instr.length, instr);
|
res = res.concat(instr.length, instr);
|
||||||
} else if (instr.length <= 0xff) {
|
} else if (instr.length <= 0xff) {
|
||||||
// res = res.concat(script.opcodes.pushdata1, instr.length, instr);
|
|
||||||
res = res.concat(0x4c, instr.length, instr);
|
res = res.concat(0x4c, instr.length, instr);
|
||||||
} else if (instr.length <= 0xffff) {
|
} else if (instr.length <= 0xffff) {
|
||||||
// res.push(script.opcodes.pushdata2);
|
|
||||||
res.push(0x4d);
|
res.push(0x4d);
|
||||||
utils.writeU16(res, instr.length, res.length);
|
utils.writeU16(res, instr.length, res.length);
|
||||||
res = res.concat(instr);
|
res = res.concat(instr);
|
||||||
} else {
|
} else {
|
||||||
// res.push(script.opcodes.pushdata4);
|
|
||||||
res.push(0x4e);
|
res.push(0x4e);
|
||||||
utils.writeU32(res, instr.length, res.length);
|
utils.writeU32(res, instr.length, res.length);
|
||||||
res = res.concat(instr);
|
res = res.concat(instr);
|
||||||
@ -478,101 +473,12 @@ script.encode = function encode(s) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// res.push(script.opcodes[instr] || instr);
|
|
||||||
res.push(instr);
|
res.push(instr);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
script.opcodes = {
|
|
||||||
0: 0,
|
|
||||||
pushdata1: 0x4c,
|
|
||||||
pushdata2: 0x4d,
|
|
||||||
pushdata4: 0x4e,
|
|
||||||
negate1: 0x4f,
|
|
||||||
|
|
||||||
nop: 0x61,
|
|
||||||
if_: 0x63,
|
|
||||||
notif: 0x64,
|
|
||||||
else_: 0x67,
|
|
||||||
endif: 0x68,
|
|
||||||
verify: 0x69,
|
|
||||||
ret: 0x6a,
|
|
||||||
|
|
||||||
toaltstack: 0x6b,
|
|
||||||
fromaltstack: 0x6c,
|
|
||||||
ifdup: 0x73,
|
|
||||||
depth: 0x74,
|
|
||||||
drop: 0x75,
|
|
||||||
dup: 0x76,
|
|
||||||
nip: 0x77,
|
|
||||||
over: 0x78,
|
|
||||||
pick: 0x79,
|
|
||||||
roll: 0x7a,
|
|
||||||
rot: 0x7b,
|
|
||||||
swap: 0x7c,
|
|
||||||
tuck: 0x7d,
|
|
||||||
drop2: 0x6d,
|
|
||||||
dup2: 0x6e,
|
|
||||||
dup3: 0x6f,
|
|
||||||
over2: 0x70,
|
|
||||||
rot2: 0x71,
|
|
||||||
swap2: 0x72,
|
|
||||||
|
|
||||||
cat: 0x74,
|
|
||||||
substr: 0x7f,
|
|
||||||
left: 0x80,
|
|
||||||
right: 0x81,
|
|
||||||
size: 0x82,
|
|
||||||
|
|
||||||
invert: 0x83,
|
|
||||||
and: 0x84,
|
|
||||||
or: 0x85,
|
|
||||||
xor: 0x86,
|
|
||||||
eq: 0x87,
|
|
||||||
eqverify: 0x88,
|
|
||||||
|
|
||||||
add1: 0x8b,
|
|
||||||
sub1: 0x8c,
|
|
||||||
mul2: 0x8d,
|
|
||||||
div2: 0x8e,
|
|
||||||
negate: 0x8f,
|
|
||||||
abs: 0x90,
|
|
||||||
not: 0x91,
|
|
||||||
noteq0: 0x92,
|
|
||||||
add: 0x93,
|
|
||||||
sub: 0x94,
|
|
||||||
mul: 0x95,
|
|
||||||
div: 0x96,
|
|
||||||
mod: 0x97,
|
|
||||||
lshift: 0x98,
|
|
||||||
rshift: 0x99,
|
|
||||||
booland: 0x9a,
|
|
||||||
boolor: 0x9b,
|
|
||||||
numeq: 0x9c,
|
|
||||||
numeqverify: 0x9d,
|
|
||||||
numneq: 0x9e,
|
|
||||||
lt: 0x9f,
|
|
||||||
gt: 0xa0,
|
|
||||||
lte: 0xa1,
|
|
||||||
gte: 0xa2,
|
|
||||||
min: 0xa3,
|
|
||||||
max: 0xa4,
|
|
||||||
within: 0xa5,
|
|
||||||
|
|
||||||
ripemd160: 0xa6,
|
|
||||||
sha1: 0xa7,
|
|
||||||
sha256: 0xa8,
|
|
||||||
hash160: 0xa9,
|
|
||||||
hash256: 0xaa,
|
|
||||||
codesep: 0xab,
|
|
||||||
checksig: 0xac,
|
|
||||||
checksigverify: 0xad,
|
|
||||||
checkmultisig: 0xae,
|
|
||||||
checkmultisigverify: 0xaf
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utils
|
* Utils
|
||||||
*/
|
*/
|
||||||
@ -616,57 +522,6 @@ utils.writeU32 = function writeU32(dst, num, off) {
|
|||||||
return 4;
|
return 4;
|
||||||
};
|
};
|
||||||
|
|
||||||
utils.writeU64 = function writeU64(dst, num, off) {
|
|
||||||
if (!off)
|
|
||||||
off = 0;
|
|
||||||
|
|
||||||
num = new bn(num).maskn(64).toArray();
|
|
||||||
while (num.length < 8)
|
|
||||||
num.unshift(0);
|
|
||||||
|
|
||||||
num.reverse().forEach(function(ch) {
|
|
||||||
dst[off++] = ch;
|
|
||||||
});
|
|
||||||
|
|
||||||
var i = num.length;
|
|
||||||
while (i--)
|
|
||||||
dst[off++] = num[i];
|
|
||||||
|
|
||||||
return 8;
|
|
||||||
};
|
|
||||||
|
|
||||||
utils.writeU16BE = function writeU16BE(dst, num, off) {
|
|
||||||
if (!off)
|
|
||||||
off = 0;
|
|
||||||
dst[off] = (num >>> 8) & 0xff;
|
|
||||||
dst[off + 1] = num & 0xff;
|
|
||||||
return 2;
|
|
||||||
};
|
|
||||||
|
|
||||||
utils.writeU32BE = function writeU32BE(dst, num, off) {
|
|
||||||
if (!off)
|
|
||||||
off = 0;
|
|
||||||
dst[off] = (num >>> 24) & 0xff;
|
|
||||||
dst[off + 1] = (num >>> 16) & 0xff;
|
|
||||||
dst[off + 2] = (num >>> 8) & 0xff;
|
|
||||||
dst[off + 3] = num & 0xff;
|
|
||||||
return 4;
|
|
||||||
};
|
|
||||||
|
|
||||||
utils.writeU64BE = function writeU64BE(dst, num, off) {
|
|
||||||
if (!off)
|
|
||||||
off = 0;
|
|
||||||
|
|
||||||
num = new bn(num).maskn(64).toArray();
|
|
||||||
while (num.length < 8)
|
|
||||||
num.unshift(0);
|
|
||||||
|
|
||||||
for (var i = 0; i < num.length; i++)
|
|
||||||
dst[off++] = num[i];
|
|
||||||
|
|
||||||
return 8;
|
|
||||||
};
|
|
||||||
|
|
||||||
utils.varint = function(arr, value, off) {
|
utils.varint = function(arr, value, off) {
|
||||||
if (!off)
|
if (!off)
|
||||||
off = 0;
|
off = 0;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user