forked from bitcoinjs/bitcoinjs-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
157 lines (127 loc) · 4.79 KB
/
script.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
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
/* global describe, it */
var assert = require('assert')
var bscript = require('../src/script')
var minimalData = require('minimaldata')
var fixtures = require('./fixtures/script.json')
var fixtures2 = require('./fixtures/templates.json')
describe('script', function () {
// TODO
describe('isCanonicalPubKey', function () {
it('rejects if not provided a Buffer', function () {
assert.strictEqual(false, bscript.isCanonicalPubKey(0))
})
it('rejects smaller than 33', function () {
for (var i = 0; i < 33; i++) {
assert.strictEqual(false, bscript.isCanonicalPubKey(Buffer.from('', i)))
}
})
})
describe.skip('isCanonicalSignature', function () {})
describe('fromASM/toASM', function () {
fixtures.valid.forEach(function (f) {
it('encodes/decodes ' + f.asm, function () {
var script = bscript.fromASM(f.asm)
assert.strictEqual(bscript.toASM(script), f.asm)
})
})
fixtures.invalid.fromASM.forEach(function (f) {
it('throws ' + f.description, function () {
assert.throws(function () {
bscript.fromASM(f.script)
}, new RegExp(f.description))
})
})
})
describe('fromASM/toASM (templates)', function () {
fixtures2.valid.forEach(function (f) {
if (f.inputHex) {
var ih = bscript.toASM(Buffer.from(f.inputHex, 'hex'))
it('encodes/decodes ' + ih, function () {
var script = bscript.fromASM(f.input)
assert.strictEqual(script.toString('hex'), f.inputHex)
assert.strictEqual(bscript.toASM(script), f.input)
})
}
if (f.outputHex) {
it('encodes/decodes ' + f.output, function () {
var script = bscript.fromASM(f.output)
assert.strictEqual(script.toString('hex'), f.outputHex)
assert.strictEqual(bscript.toASM(script), f.output)
})
}
})
})
describe('isPushOnly', function () {
fixtures.valid.forEach(function (f) {
it('returns ' + !!f.stack + ' for ' + f.asm, function () {
var script = bscript.fromASM(f.asm)
var chunks = bscript.decompile(script)
assert.strictEqual(bscript.isPushOnly(chunks), !!f.stack)
})
})
})
describe('toStack', function () {
fixtures.valid.forEach(function (f) {
it('returns ' + !!f.stack + ' for ' + f.asm, function () {
if (!f.stack || !f.asm) return
var script = bscript.fromASM(f.asm)
var stack = bscript.toStack(script)
assert.deepEqual(stack.map(function (x) {
return x.toString('hex')
}), f.stack)
assert.equal(bscript.toASM(bscript.compile(stack)), f.asm, 'should rebuild same script from stack')
})
})
})
describe('compile (via fromASM)', function () {
fixtures.valid.forEach(function (f) {
it('(' + f.type + ') compiles ' + f.asm, function () {
var scriptSig = bscript.fromASM(f.asm)
assert.strictEqual(scriptSig.toString('hex'), f.script)
if (f.nonstandard) {
var scriptSigNS = bscript.fromASM(f.nonstandard.scriptSig)
assert.strictEqual(scriptSigNS.toString('hex'), f.script)
}
})
})
})
describe('decompile', function () {
fixtures.valid.forEach(function (f) {
it('decompiles ' + f.asm, function () {
var chunks = bscript.decompile(Buffer.from(f.script, 'hex'))
assert.strictEqual(bscript.compile(chunks).toString('hex'), f.script)
assert.strictEqual(bscript.toASM(chunks), f.asm)
if (f.nonstandard) {
var chunksNS = bscript.decompile(Buffer.from(f.nonstandard.scriptSigHex, 'hex'))
assert.strictEqual(bscript.compile(chunksNS).toString('hex'), f.script)
// toASM converts verbatim, only `compile` transforms the script to a minimalpush compliant script
assert.strictEqual(bscript.toASM(chunksNS), f.nonstandard.scriptSig)
}
})
})
fixtures.invalid.decompile.forEach(function (f) {
it('decompiles ' + f.script + ' to [] because of "' + f.description + '"', function () {
var chunks = bscript.decompile(Buffer.from(f.script, 'hex'))
assert.strictEqual(chunks.length, 0)
})
})
})
describe('SCRIPT_VERIFY_MINIMALDATA policy', function () {
fixtures.valid.forEach(function (f) {
it('compliant for ' + f.type + ' scriptSig ' + f.asm, function () {
var script = Buffer.from(f.script, 'hex')
assert(minimalData(script))
})
})
function testEncodingForSize (i) {
it('compliant for data PUSH of length ' + i, function () {
var buffer = Buffer.alloc(i)
var script = bscript.compile([buffer])
assert(minimalData(script), 'Failed for ' + i + ' length script: ' + script.toString('hex'))
})
}
for (var i = 0; i < 520; ++i) {
testEncodingForSize(i)
}
})
})