-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
285 lines (230 loc) · 7.29 KB
/
scripts.js
File metadata and controls
285 lines (230 loc) · 7.29 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
var assert = require('assert')
var ops = require('./opcodes')
var typeForce = require('typeforce')
var ecurve = require('ecurve')
var curve = ecurve.getCurveByName('secp256k1')
var ECSignature = require('./ecsignature')
var Script = require('./script')
function isCanonicalPubKey (buffer) {
if (!Buffer.isBuffer(buffer)) return false
try {
ecurve.Point.decodeFrom(curve, buffer)
} catch (e) {
if (!(e.message.match(/Invalid sequence (length|tag)/))) {
throw e
}
return false
}
return true
}
function isCanonicalSignature (buffer) {
if (!Buffer.isBuffer(buffer)) return false
try {
ECSignature.parseScriptSignature(buffer)
} catch (e) {
if (!(e.message.match(/Not a DER sequence|Invalid sequence length|Expected a DER integer|R length is zero|S length is zero|R value excessively padded|S value excessively padded|R value is negative|S value is negative|Invalid hashType/))) {
throw e
}
return false
}
return true
}
function isPubKeyHashInput (script) {
return script.chunks.length === 2 &&
isCanonicalSignature(script.chunks[0]) &&
isCanonicalPubKey(script.chunks[1])
}
function isPubKeyHashOutput (script) {
return script.chunks.length === 5 &&
script.chunks[0] === ops.OP_DUP &&
script.chunks[1] === ops.OP_HASH160 &&
Buffer.isBuffer(script.chunks[2]) &&
script.chunks[2].length === 20 &&
script.chunks[3] === ops.OP_EQUALVERIFY &&
script.chunks[4] === ops.OP_CHECKSIG
}
function isPubKeyInput (script) {
return script.chunks.length === 1 &&
isCanonicalSignature(script.chunks[0])
}
function isPubKeyOutput (script) {
return script.chunks.length === 2 &&
isCanonicalPubKey(script.chunks[0]) &&
script.chunks[1] === ops.OP_CHECKSIG
}
function isScriptHashInput (script, allowIncomplete) {
if (script.chunks.length < 2) return false
var lastChunk = script.chunks[script.chunks.length - 1]
if (!Buffer.isBuffer(lastChunk)) return false
var scriptSig = Script.fromChunks(script.chunks.slice(0, -1))
var redeemScript = Script.fromBuffer(lastChunk)
// is redeemScript a valid script?
if (redeemScript.chunks.length === 0) return false
return classifyInput(scriptSig, allowIncomplete) === classifyOutput(redeemScript)
}
function isScriptHashOutput (script) {
return script.chunks.length === 3 &&
script.chunks[0] === ops.OP_HASH160 &&
Buffer.isBuffer(script.chunks[1]) &&
script.chunks[1].length === 20 &&
script.chunks[2] === ops.OP_EQUAL
}
// allowIncomplete is to account for combining signatures
// See https://github.com/bitcoin/bitcoin/blob/f425050546644a36b0b8e0eb2f6934a3e0f6f80f/src/script/sign.cpp#L195-L197
function isMultisigInput (script, allowIncomplete) {
if (script.chunks.length < 2) return false
if (script.chunks[0] !== ops.OP_0) return false
if (allowIncomplete) {
return script.chunks.slice(1).every(function (chunk) {
return chunk === ops.OP_0 || isCanonicalSignature(chunk)
})
}
return script.chunks.slice(1).every(isCanonicalSignature)
}
function isMultisigOutput (script) {
if (script.chunks.length < 4) return false
if (script.chunks[script.chunks.length - 1] !== ops.OP_CHECKMULTISIG) return false
var mOp = script.chunks[0]
if (mOp === ops.OP_0) return false
if (mOp < ops.OP_1) return false
if (mOp > ops.OP_16) return false
var nOp = script.chunks[script.chunks.length - 2]
if (nOp === ops.OP_0) return false
if (nOp < ops.OP_1) return false
if (nOp > ops.OP_16) return false
var m = mOp - (ops.OP_1 - 1)
var n = nOp - (ops.OP_1 - 1)
if (n < m) return false
var pubKeys = script.chunks.slice(1, -2)
if (n < pubKeys.length) return false
return pubKeys.every(isCanonicalPubKey)
}
function isNullDataOutput (script) {
return script.chunks[0] === ops.OP_RETURN
}
function classifyOutput (script) {
typeForce('Script', script)
if (isPubKeyHashOutput(script)) {
return 'pubkeyhash'
} else if (isScriptHashOutput(script)) {
return 'scripthash'
} else if (isMultisigOutput(script)) {
return 'multisig'
} else if (isPubKeyOutput(script)) {
return 'pubkey'
} else if (isNullDataOutput(script)) {
return 'nulldata'
}
return 'nonstandard'
}
function classifyInput (script, allowIncomplete) {
typeForce('Script', script)
if (isPubKeyHashInput(script)) {
return 'pubkeyhash'
} else if (isMultisigInput(script, allowIncomplete)) {
return 'multisig'
} else if (isScriptHashInput(script, allowIncomplete)) {
return 'scripthash'
} else if (isPubKeyInput(script)) {
return 'pubkey'
}
return 'nonstandard'
}
// Standard Script Templates
// {pubKey} OP_CHECKSIG
function pubKeyOutput (pubKey) {
return Script.fromChunks([
pubKey,
ops.OP_CHECKSIG
])
}
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
function pubKeyHashOutput (hash) {
typeForce('Buffer', hash)
return Script.fromChunks([
ops.OP_DUP,
ops.OP_HASH160,
hash,
ops.OP_EQUALVERIFY,
ops.OP_CHECKSIG
])
}
// OP_HASH160 {scriptHash} OP_EQUAL
function scriptHashOutput (hash) {
typeForce('Buffer', hash)
return Script.fromChunks([
ops.OP_HASH160,
hash,
ops.OP_EQUAL
])
}
// m [pubKeys ...] n OP_CHECKMULTISIG
function multisigOutput (m, pubKeys) {
typeForce(['Buffer'], pubKeys)
var n = pubKeys.length
assert(n >= m, 'Not enough pubKeys provided')
return Script.fromChunks([].concat(
(ops.OP_1 - 1) + m,
pubKeys,
(ops.OP_1 - 1) + n,
ops.OP_CHECKMULTISIG
))
}
// {signature}
function pubKeyInput (signature) {
typeForce('Buffer', signature)
return Script.fromChunks([signature])
}
// {signature} {pubKey}
function pubKeyHashInput (signature, pubKey) {
typeForce('Buffer', signature)
typeForce('Buffer', pubKey)
return Script.fromChunks([signature, pubKey])
}
// <scriptSig> {serialized scriptPubKey script}
function scriptHashInput (scriptSig, scriptPubKey) {
return Script.fromChunks([].concat(
scriptSig.chunks,
scriptPubKey.toBuffer()
))
}
// OP_0 [signatures ...]
function multisigInput (signatures, scriptPubKey) {
if (scriptPubKey) {
assert(isMultisigOutput(scriptPubKey))
var mOp = scriptPubKey.chunks[0]
var nOp = scriptPubKey.chunks[scriptPubKey.chunks.length - 2]
var m = mOp - (ops.OP_1 - 1)
var n = nOp - (ops.OP_1 - 1)
assert(signatures.length >= m, 'Not enough signatures provided')
assert(signatures.length <= n, 'Too many signatures provided')
}
return Script.fromChunks([].concat(ops.OP_0, signatures))
}
function nullDataOutput (data) {
return Script.fromChunks([ops.OP_RETURN, data])
}
module.exports = {
isCanonicalPubKey: isCanonicalPubKey,
isCanonicalSignature: isCanonicalSignature,
isPubKeyHashInput: isPubKeyHashInput,
isPubKeyHashOutput: isPubKeyHashOutput,
isPubKeyInput: isPubKeyInput,
isPubKeyOutput: isPubKeyOutput,
isScriptHashInput: isScriptHashInput,
isScriptHashOutput: isScriptHashOutput,
isMultisigInput: isMultisigInput,
isMultisigOutput: isMultisigOutput,
isNullDataOutput: isNullDataOutput,
classifyOutput: classifyOutput,
classifyInput: classifyInput,
pubKeyOutput: pubKeyOutput,
pubKeyHashOutput: pubKeyHashOutput,
scriptHashOutput: scriptHashOutput,
multisigOutput: multisigOutput,
pubKeyInput: pubKeyInput,
pubKeyHashInput: pubKeyHashInput,
scriptHashInput: scriptHashInput,
multisigInput: multisigInput,
nullDataOutput: nullDataOutput
}