-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
144 lines (111 loc) · 3.06 KB
/
script.js
File metadata and controls
144 lines (111 loc) · 3.06 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
var assert = require('assert')
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
var typeForce = require('typeforce')
var opcodes = require('./opcodes')
function Script (buffer, chunks) {
typeForce('Buffer', buffer)
typeForce('Array', chunks)
this.buffer = buffer
this.chunks = chunks
}
Script.fromASM = function (asm) {
var strChunks = asm.split(' ')
var chunks = strChunks.map(function (strChunk) {
// opcode
if (strChunk in opcodes) {
return opcodes[strChunk]
// data chunk
} else {
return new Buffer(strChunk, 'hex')
}
})
return Script.fromChunks(chunks)
}
Script.fromBuffer = function (buffer) {
var chunks = []
var i = 0
while (i < buffer.length) {
var opcode = buffer.readUInt8(i)
// data chunk
if ((opcode > opcodes.OP_0) && (opcode <= opcodes.OP_PUSHDATA4)) {
var d = bufferutils.readPushDataInt(buffer, i)
// did reading a pushDataInt fail? return non-chunked script
if (d === null) return new Script(buffer, [])
i += d.size
// attempt to read too much data?
if (i + d.number > buffer.length) return new Script(buffer, [])
var data = buffer.slice(i, i + d.number)
i += d.number
chunks.push(data)
// opcode
} else {
chunks.push(opcode)
i += 1
}
}
return new Script(buffer, chunks)
}
Script.fromChunks = function (chunks) {
typeForce('Array', chunks)
var bufferSize = chunks.reduce(function (accum, chunk) {
// data chunk
if (Buffer.isBuffer(chunk)) {
return accum + bufferutils.pushDataSize(chunk.length) + chunk.length
}
// opcode
return accum + 1
}, 0.0)
var buffer = new Buffer(bufferSize)
var offset = 0
chunks.forEach(function (chunk) {
// data chunk
if (Buffer.isBuffer(chunk)) {
offset += bufferutils.writePushDataInt(buffer, chunk.length, offset)
chunk.copy(buffer, offset)
offset += chunk.length
// opcode
} else {
buffer.writeUInt8(chunk, offset)
offset += 1
}
})
assert.equal(offset, buffer.length, 'Could not decode chunks')
return new Script(buffer, chunks)
}
Script.fromHex = function (hex) {
return Script.fromBuffer(new Buffer(hex, 'hex'))
}
Script.EMPTY = Script.fromChunks([])
Script.prototype.getHash = function () {
return crypto.hash160(this.buffer)
}
// FIXME: doesn't work for data chunks, maybe time to use buffertools.compare...
Script.prototype.without = function (needle) {
return Script.fromChunks(this.chunks.filter(function (op) {
return op !== needle
}))
}
var reverseOps = []
for (var op in opcodes) {
var code = opcodes[op]
reverseOps[code] = op
}
Script.prototype.toASM = function () {
return this.chunks.map(function (chunk) {
// data chunk
if (Buffer.isBuffer(chunk)) {
return chunk.toString('hex')
// opcode
} else {
return reverseOps[chunk]
}
}).join(' ')
}
Script.prototype.toBuffer = function () {
return this.buffer
}
Script.prototype.toHex = function () {
return this.toBuffer().toString('hex')
}
module.exports = Script