forked from bitcoinjs/minimaldata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (51 loc) · 1.84 KB
/
index.js
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
var OPS = require('ravencoin-ops')
var pushData = require('pushdata-ravencoin')
// https://github.com/ravenproject/ravencoin/blob/master/src/script/script.h#L26
var MAX_SCRIPT_ELEMENT_SIZE = 520
// https://github.com/bitcoin/bitcoin/blob/d612837814020ae832499d18e6ee5eb919a87907/src/script/interpreter.cpp#L209
function checkMinimalPush (opcode, data) {
// Could have used OP_0.
if (data.length === 0) {
return opcode === OPS.OP_0
// Could have used OP_1 .. OP_16.
} else if (data.length === 1 && data[0] >= 1 && data[0] <= 16) {
return opcode === OPS.OP_1 + (opcode - 1)
// Could have used OP_1NEGATE.
} else if (data.length === 1 && opcode === 0x81) {
return opcode === OPS.OP_1NEGATE
// Could have used a direct push (opcode indicating number of bytes pushed + those bytes).
} else if (data.length <= 75) {
return opcode === data.length
// Could have used OP_PUSHDATA1.
} else if (data.length <= 255) {
return opcode === OPS.OP_PUSHDATA1
// Could have used OP_PUSHDATA2.
} else if (data.length <= 65535) {
return opcode === OPS.OP_PUSHDATA2
}
return false
}
// https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki
module.exports = function bip62 (buffer) {
var i = 0
while (i < buffer.length) {
var opcode = buffer[i]
// is this a data PUSH?
if (opcode >= 0 && opcode <= OPS.OP_PUSHDATA4) {
var d = pushData.decode(buffer, i)
// did reading a pushDataInt fail? empty script
if (d === null) return false
i += d.size
// attempt to read too much data? empty script
if (i + d.number > buffer.length) return false
var data = buffer.slice(i, i + d.number)
i += d.number
if (d.number > MAX_SCRIPT_ELEMENT_SIZE) return false
if (!checkMinimalPush(opcode, data)) return false
// opcode
} else {
++i
}
}
return true
}