-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction.js
More file actions
336 lines (269 loc) · 8.05 KB
/
transaction.js
File metadata and controls
336 lines (269 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
var assert = require('assert')
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
var typeForce = require('typeforce')
var opcodes = require('./opcodes')
var Script = require('./script')
function Transaction () {
this.version = 1
this.locktime = 0
this.ins = []
this.outs = []
}
Transaction.DEFAULT_SEQUENCE = 0xffffffff
Transaction.SIGHASH_ALL = 0x01
Transaction.SIGHASH_NONE = 0x02
Transaction.SIGHASH_SINGLE = 0x03
Transaction.SIGHASH_ANYONECANPAY = 0x80
Transaction.fromBuffer = function (buffer, __disableAssert) {
var offset = 0
function readSlice (n) {
offset += n
return buffer.slice(offset - n, offset)
}
function readUInt32 () {
var i = buffer.readUInt32LE(offset)
offset += 4
return i
}
function readUInt64 () {
var i = bufferutils.readUInt64LE(buffer, offset)
offset += 8
return i
}
function readVarInt () {
var vi = bufferutils.readVarInt(buffer, offset)
offset += vi.size
return vi.number
}
function readScript () {
return Script.fromBuffer(readSlice(readVarInt()))
}
function readGenerationScript () {
return new Script(readSlice(readVarInt()), [])
}
var tx = new Transaction()
tx.version = readUInt32()
var vinLen = readVarInt()
for (var i = 0; i < vinLen; ++i) {
var hash = readSlice(32)
if (Transaction.isCoinbaseHash(hash)) {
tx.ins.push({
hash: hash,
index: readUInt32(),
script: readGenerationScript(),
sequence: readUInt32()
})
} else {
tx.ins.push({
hash: hash,
index: readUInt32(),
script: readScript(),
sequence: readUInt32()
})
}
}
var voutLen = readVarInt()
for (i = 0; i < voutLen; ++i) {
tx.outs.push({
value: readUInt64(),
script: readScript()
})
}
tx.locktime = readUInt32()
if (!__disableAssert) {
assert.equal(offset, buffer.length, 'Transaction has unexpected data')
}
return tx
}
Transaction.fromHex = function (hex) {
return Transaction.fromBuffer(new Buffer(hex, 'hex'))
}
Transaction.isCoinbaseHash = function (buffer) {
return Array.prototype.every.call(buffer, function (x) {
return x === 0
})
}
Transaction.prototype.addInput = function (hash, index, sequence, script) {
if (sequence === undefined || sequence === null) {
sequence = Transaction.DEFAULT_SEQUENCE
}
script = script || Script.EMPTY
typeForce('Buffer', hash)
typeForce('Number', index)
typeForce('Number', sequence)
typeForce('Script', script)
assert.equal(hash.length, 32, 'Expected hash length of 32, got ' + hash.length)
// Add the input and return the input's index
return (this.ins.push({
hash: hash,
index: index,
script: script,
sequence: sequence
}) - 1)
}
Transaction.prototype.addOutput = function (scriptPubKey, value) {
typeForce('Script', scriptPubKey)
typeForce('Number', value)
// Add the output and return the output's index
return (this.outs.push({
script: scriptPubKey,
value: value
}) - 1)
}
Transaction.prototype.byteLength = function () {
function scriptSize (script) {
var length = script.buffer.length
return bufferutils.varIntSize(length) + length
}
return (
8 +
bufferutils.varIntSize(this.ins.length) +
bufferutils.varIntSize(this.outs.length) +
this.ins.reduce(function (sum, input) { return sum + 40 + scriptSize(input.script) }, 0) +
this.outs.reduce(function (sum, output) { return sum + 8 + scriptSize(output.script) }, 0)
)
}
Transaction.prototype.clone = function () {
var newTx = new Transaction()
newTx.version = this.version
newTx.locktime = this.locktime
newTx.ins = this.ins.map(function (txIn) {
return {
hash: txIn.hash,
index: txIn.index,
script: txIn.script,
sequence: txIn.sequence
}
})
newTx.outs = this.outs.map(function (txOut) {
return {
script: txOut.script,
value: txOut.value
}
})
return newTx
}
var one = new Buffer('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
/**
* Hash transaction for signing a specific input.
*
* Bitcoin uses a different hash for each signed transaction input.
* This method copies the transaction, makes the necessary changes based on the
* hashType, and then hashes the result.
* This hash can then be used to sign the provided transaction input.
*/
Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashType) {
typeForce('Number', inIndex)
typeForce('Script', prevOutScript)
typeForce('Number', hashType)
assert(inIndex >= 0, 'Invalid vin index')
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29
if (inIndex >= this.ins.length) return one
var txTmp = this.clone()
// in case concatenating two scripts ends up with two codeseparators,
// or an extra one at the end, this prevents all those possible incompatibilities.
var hashScript = prevOutScript.without(opcodes.OP_CODESEPARATOR)
var i
// blank out other inputs' signatures
txTmp.ins.forEach(function (input) { input.script = Script.EMPTY })
txTmp.ins[inIndex].script = hashScript
// blank out some of the inputs
if ((hashType & 0x1f) === Transaction.SIGHASH_NONE) {
// wildcard payee
txTmp.outs = []
// let the others update at will
txTmp.ins.forEach(function (input, i) {
if (i !== inIndex) {
input.sequence = 0
}
})
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE) {
var nOut = inIndex
// only lock-in the txOut payee at same index as txIn
// https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L60
if (nOut >= this.outs.length) return one
txTmp.outs = txTmp.outs.slice(0, nOut + 1)
// blank all other outputs (clear scriptPubKey, value === -1)
var stubOut = {
script: Script.EMPTY,
valueBuffer: new Buffer('ffffffffffffffff', 'hex')
}
for (i = 0; i < nOut; i++) {
txTmp.outs[i] = stubOut
}
// let the others update at will
txTmp.ins.forEach(function (input, i) {
if (i !== inIndex) {
input.sequence = 0
}
})
}
// blank out other inputs completely, not recommended for open transactions
if (hashType & Transaction.SIGHASH_ANYONECANPAY) {
txTmp.ins[0] = txTmp.ins[inIndex]
txTmp.ins = txTmp.ins.slice(0, 1)
}
// serialize and hash
var buffer = new Buffer(txTmp.byteLength() + 4)
buffer.writeInt32LE(hashType, buffer.length - 4)
txTmp.toBuffer().copy(buffer, 0)
return crypto.hash256(buffer)
}
Transaction.prototype.getHash = function () {
return crypto.hash256(this.toBuffer())
}
Transaction.prototype.getId = function () {
// TxHash is little-endian, we need big-endian
return bufferutils.reverse(this.getHash()).toString('hex')
}
Transaction.prototype.toBuffer = function () {
var buffer = new Buffer(this.byteLength())
var offset = 0
function writeSlice (slice) {
slice.copy(buffer, offset)
offset += slice.length
}
function writeUInt32 (i) {
buffer.writeUInt32LE(i, offset)
offset += 4
}
function writeUInt64 (i) {
bufferutils.writeUInt64LE(buffer, i, offset)
offset += 8
}
function writeVarInt (i) {
var n = bufferutils.writeVarInt(buffer, i, offset)
offset += n
}
writeUInt32(this.version)
writeVarInt(this.ins.length)
this.ins.forEach(function (txIn) {
writeSlice(txIn.hash)
writeUInt32(txIn.index)
writeVarInt(txIn.script.buffer.length)
writeSlice(txIn.script.buffer)
writeUInt32(txIn.sequence)
})
writeVarInt(this.outs.length)
this.outs.forEach(function (txOut) {
if (!txOut.valueBuffer) {
writeUInt64(txOut.value)
} else {
writeSlice(txOut.valueBuffer)
}
writeVarInt(txOut.script.buffer.length)
writeSlice(txOut.script.buffer)
})
writeUInt32(this.locktime)
return buffer
}
Transaction.prototype.toHex = function () {
return this.toBuffer().toString('hex')
}
Transaction.prototype.setInputScript = function (index, script) {
typeForce('Number', index)
typeForce('Script', script)
this.ins[index].script = script
}
module.exports = Transaction