Transaction: restrict Transaction constructor
This commit is contained in:
parent
f85792ba22
commit
7f9711ef9b
1 changed files with 7 additions and 32 deletions
|
@ -17,28 +17,11 @@ var SIGHASH_NONE = 0x02
|
||||||
var SIGHASH_SINGLE = 0x03
|
var SIGHASH_SINGLE = 0x03
|
||||||
var SIGHASH_ANYONECANPAY = 0x80
|
var SIGHASH_ANYONECANPAY = 0x80
|
||||||
|
|
||||||
function Transaction(doc) {
|
function Transaction() {
|
||||||
if (!(this instanceof Transaction)) { return new Transaction(doc) }
|
|
||||||
this.version = 1
|
this.version = 1
|
||||||
this.locktime = 0
|
this.locktime = 0
|
||||||
this.ins = []
|
this.ins = []
|
||||||
this.outs = []
|
this.outs = []
|
||||||
|
|
||||||
if (doc) {
|
|
||||||
if (doc.version) this.version = doc.version;
|
|
||||||
if (doc.locktime) this.locktime = doc.locktime;
|
|
||||||
if (doc.ins && doc.ins.length) {
|
|
||||||
this.ins = doc.ins.map(function(input) {
|
|
||||||
return new TransactionIn(input)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (doc.outs && doc.outs.length) {
|
|
||||||
this.outs = doc.outs.map(function(output) {
|
|
||||||
return new TransactionOut(output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -256,12 +239,10 @@ Transaction.fromBuffer = function(buffer) {
|
||||||
return vi.number
|
return vi.number
|
||||||
}
|
}
|
||||||
|
|
||||||
var ins = []
|
var tx = new Transaction()
|
||||||
var outs = []
|
tx.version = readUInt32()
|
||||||
|
|
||||||
var version = readUInt32()
|
|
||||||
var vinLen = readVarInt()
|
var vinLen = readVarInt()
|
||||||
|
|
||||||
for (var i = 0; i < vinLen; ++i) {
|
for (var i = 0; i < vinLen; ++i) {
|
||||||
var hash = readSlice(32)
|
var hash = readSlice(32)
|
||||||
var vout = readUInt32()
|
var vout = readUInt32()
|
||||||
|
@ -269,7 +250,7 @@ Transaction.fromBuffer = function(buffer) {
|
||||||
var script = readSlice(scriptLen)
|
var script = readSlice(scriptLen)
|
||||||
var sequence = readUInt32()
|
var sequence = readUInt32()
|
||||||
|
|
||||||
ins.push(new TransactionIn({
|
tx.ins.push(new TransactionIn({
|
||||||
outpoint: {
|
outpoint: {
|
||||||
hash: hash,
|
hash: hash,
|
||||||
index: vout
|
index: vout
|
||||||
|
@ -280,27 +261,21 @@ Transaction.fromBuffer = function(buffer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var voutLen = readVarInt()
|
var voutLen = readVarInt()
|
||||||
|
|
||||||
for (i = 0; i < voutLen; ++i) {
|
for (i = 0; i < voutLen; ++i) {
|
||||||
var value = readUInt64()
|
var value = readUInt64()
|
||||||
var scriptLen = readVarInt()
|
var scriptLen = readVarInt()
|
||||||
var script = readSlice(scriptLen)
|
var script = readSlice(scriptLen)
|
||||||
|
|
||||||
outs.push(new TransactionOut({
|
tx.outs.push(new TransactionOut({
|
||||||
value: value,
|
value: value,
|
||||||
script: Script.fromBuffer(script)
|
script: Script.fromBuffer(script)
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
var locktime = readUInt32()
|
tx.locktime = readUInt32()
|
||||||
assert.equal(offset, buffer.length, 'Invalid transaction')
|
assert.equal(offset, buffer.length, 'Invalid transaction')
|
||||||
|
|
||||||
return new Transaction({
|
return tx
|
||||||
version: version,
|
|
||||||
ins: ins,
|
|
||||||
outs: outs,
|
|
||||||
locktime: locktime
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.fromHex = function(hex) {
|
Transaction.fromHex = function(hex) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue