-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathscript.ts
250 lines (203 loc) · 6.36 KB
/
script.ts
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
/**
* Script tools, including decompile, compile, toASM, fromASM, toStack, isCanonicalPubKey, isCanonicalScriptSignature
* @packageDocumentation
*/
import * as bip66 from './bip66';
import { Opcodes, OPS, REVERSE_OPS } from './ops';
import { Stack } from './payments';
import * as pushdata from './push_data';
import * as scriptNumber from './script_number';
import * as scriptSignature from './script_signature';
import * as types from './types';
const { typeforce } = types;
const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
export { OPS };
function isOPInt(value: number): boolean {
return (
types.Number(value) &&
(value === OPS.OP_0 ||
(value >= OPS.OP_1 && value <= OPS.OP_16) ||
value === OPS.OP_1NEGATE)
);
}
function isPushOnlyChunk(value: number | Buffer): boolean {
return types.Buffer(value) || isOPInt(value as number);
}
export function isPushOnly(value: Stack): boolean {
return types.Array(value) && value.every(isPushOnlyChunk);
}
export function countNonPushOnlyOPs(value: Stack): number {
return value.length - value.filter(isPushOnlyChunk).length;
}
function asMinimalOP(buffer: Buffer): number | void {
if (buffer.length === 0) return OPS.OP_0;
if (buffer.length !== 1) return;
if (buffer[0] >= 1 && buffer[0] <= 16) return OP_INT_BASE + buffer[0];
if (buffer[0] === 0x81) return OPS.OP_1NEGATE;
}
function chunksIsBuffer(buf: Buffer | Stack): buf is Buffer {
return Buffer.isBuffer(buf);
}
function chunksIsArray(buf: Buffer | Stack): buf is Stack {
return types.Array(buf);
}
function singleChunkIsBuffer(buf: number | Buffer): buf is Buffer {
return Buffer.isBuffer(buf);
}
/**
* Compiles an array of chunks into a Buffer.
*
* @param chunks - The array of chunks to compile.
* @returns The compiled Buffer.
* @throws Error if the compilation fails.
*/
export function compile(chunks: Buffer | Stack): Buffer {
// TODO: remove me
if (chunksIsBuffer(chunks)) return chunks;
typeforce(types.Array, chunks);
const bufferSize = chunks.reduce((accum: number, chunk) => {
// data chunk
if (singleChunkIsBuffer(chunk)) {
// adhere to BIP62.3, minimal push policy
if (chunk.length === 1 && asMinimalOP(chunk) !== undefined) {
return accum + 1;
}
return accum + pushdata.encodingLength(chunk.length) + chunk.length;
}
// opcode
return accum + 1;
}, 0.0);
const buffer = Buffer.allocUnsafe(bufferSize);
let offset = 0;
chunks.forEach(chunk => {
// data chunk
if (singleChunkIsBuffer(chunk)) {
// adhere to BIP62.3, minimal push policy
const opcode = asMinimalOP(chunk);
if (opcode !== undefined) {
buffer.writeUInt8(opcode, offset);
offset += 1;
return;
}
offset += pushdata.encode(buffer, chunk.length, offset);
chunk.copy(buffer, offset);
offset += chunk.length;
// opcode
} else {
buffer.writeUInt8(chunk, offset);
offset += 1;
}
});
if (offset !== buffer.length) throw new Error('Could not decode chunks');
return buffer;
}
export function decompile(
buffer: Buffer | Array<number | Buffer>,
): Array<number | Buffer> | null {
// TODO: remove me
if (chunksIsArray(buffer)) return buffer;
typeforce(types.Buffer, buffer);
const chunks: Array<number | Buffer> = [];
let i = 0;
while (i < buffer.length) {
const opcode = buffer[i];
// data chunk
if (opcode > OPS.OP_0 && opcode <= OPS.OP_PUSHDATA4) {
const d = pushdata.decode(buffer, i);
// did reading a pushDataInt fail?
if (d === null) return null;
i += d.size;
// attempt to read too much data?
if (i + d.number > buffer.length) return null;
const data = buffer.slice(i, i + d.number);
i += d.number;
// decompile minimally
const op = asMinimalOP(data);
if (op !== undefined) {
chunks.push(op);
} else {
chunks.push(data);
}
// opcode
} else {
chunks.push(opcode);
i += 1;
}
}
return chunks;
}
/**
* Converts the given chunks into an ASM (Assembly) string representation.
* If the chunks parameter is a Buffer, it will be decompiled into a Stack before conversion.
* @param chunks - The chunks to convert into ASM.
* @returns The ASM string representation of the chunks.
*/
export function toASM(chunks: Buffer | Array<number | Buffer>): string {
if (chunksIsBuffer(chunks)) {
chunks = decompile(chunks) as Stack;
}
if (!chunks) {
throw new Error('Could not convert invalid chunks to ASM');
}
return chunks
.map(chunk => {
// data?
if (singleChunkIsBuffer(chunk)) {
const op = asMinimalOP(chunk);
if (op === undefined) return chunk.toString('hex');
chunk = op as number;
}
// opcode!
return REVERSE_OPS[chunk];
})
.join(' ');
}
/**
* Converts an ASM string to a Buffer.
* @param asm The ASM string to convert.
* @returns The converted Buffer.
*/
export function fromASM(asm: string): Buffer {
typeforce(types.String, asm);
return compile(
asm.split(' ').map(chunkStr => {
// opcode?
if (OPS[chunkStr as keyof Opcodes] !== undefined) {
return OPS[chunkStr as keyof Opcodes];
}
typeforce(types.Hex, chunkStr);
// data!
return Buffer.from(chunkStr, 'hex');
}),
);
}
/**
* Converts the given chunks into a stack of buffers.
*
* @param chunks - The chunks to convert.
* @returns The stack of buffers.
*/
export function toStack(chunks: Buffer | Array<number | Buffer>): Buffer[] {
chunks = decompile(chunks) as Stack;
typeforce(isPushOnly, chunks);
return chunks.map(op => {
if (singleChunkIsBuffer(op)) return op;
if (op === OPS.OP_0) return Buffer.allocUnsafe(0);
return scriptNumber.encode(op - OP_INT_BASE);
});
}
export function isCanonicalPubKey(buffer: Buffer): boolean {
return types.isPoint(buffer);
}
export function isDefinedHashType(hashType: number): boolean {
const hashTypeMod = hashType & ~0x80;
// return hashTypeMod > SIGHASH_ALL && hashTypeMod < SIGHASH_SINGLE
return hashTypeMod > 0x00 && hashTypeMod < 0x04;
}
export function isCanonicalScriptSignature(buffer: Buffer): boolean {
if (!Buffer.isBuffer(buffer)) return false;
if (!isDefinedHashType(buffer[buffer.length - 1])) return false;
return bip66.check(buffer.slice(0, -1));
}
export const number = scriptNumber;
export const signature = scriptSignature;