-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
220 lines (191 loc) · 3.44 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
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
#!/usr/bin/env node
const yargs = require("yargs")
const fs = require("fs")
const path = require("path")
const opcodes = [
"0",
"false",
"pushdata1",
"pushdata2",
"pushdata4",
"1negate",
"reserved",
"1",
"true",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
// control
"nop",
"ver",
"if",
"notif",
"verif",
"vernotif",
"else",
"endif",
"verify",
"return",
// stack ops
"toaltstack",
"fromaltstack",
"2drop",
"2dup",
"3dup",
"2over",
"2rot",
"2swap",
"ifdup",
"depth",
"drop",
"dup",
"nip",
"over",
"pick",
"roll",
"rot",
"swap",
"tuck",
// splice ops
"cat",
"split", // after monolith upgrade (May 2018)
"num2bin", // after monolith upgrade (May 2018)
"bin2num", // after monolith upgrade (May 2018)
"size",
// bit logic
"invert",
"and",
"or",
"xor",
"equal",
"equalverify",
"reserved1",
"reserved2",
// numeric
"1add",
"1sub",
"2mul",
"2div",
"negate",
"abs",
"not",
"0notequal",
"add",
"sub",
"mul",
"div",
"mod",
"lshift",
"rshift",
"booland",
"boolor",
"numequal",
"numequalverify",
"numnotequal",
"lessthan",
"greaterthan",
"lessthanorequal",
"greaterthanorequal",
"min",
"max",
"within",
// crypto
"ripemd160",
"sha1",
"sha256",
"hash160",
"hash256",
"codeseparator",
"checksig",
"checksigverify",
"checkmultisig",
"checkmultisigverify",
// expansion
"nop1",
"checklocktimeverify",
"nop2",
"checksequenceverify",
"nop3",
"nop4",
"nop5",
"nop6",
"nop7",
"nop8",
"nop9",
"nop10",
// template matching params
"smallinteger",
"pubkeys",
"pubkeyhash",
"pubkey",
"invalidopcode"
]
const argv = yargs.command("$0 <input>", "Compile to Bitcoin Script", yargs => {
yargs.positional("input", {
describe: "Path to file to compile",
type: "string"
})
}).argv
function getSymbols(string) {
return string.split(/\s/).filter(symbol => symbol)
}
function load(path) {
const fileString = fs.readFileSync(path).toString()
return getSymbols(fileString)
}
function compile(symbols) {
const macros = {}
const output = []
function next(symbols) {
const symbol = symbols.shift()
if (symbol === "import") {
const importPath = symbols.shift()
const basePath = path.dirname(argv.input)
const marcoSymbols = load(path.join(basePath, importPath + ".bits"))
const macroName = path.parse(importPath).name
const macroOpCodes = compile(marcoSymbols)
macros[macroName] = macroOpCodes
} else if (symbol === "loop") {
const times = symbols.shift()
const loopSymobls = []
let end = false
while (!end) {
const nextLoopSymbol = symbols.shift()
if (nextLoopSymbol === "end") {
end = true
} else {
loopSymobls.push(nextLoopSymbol)
}
}
const compiledLoop = compile(loopSymobls)
console.log(compiledLoop)
for (let i = 0; i < times; i++) {
output.push(...compiledLoop)
}
} else if (Object.keys(macros).includes(symbol)) {
output.push(...macros[symbol])
} else if (opcodes.includes(symbol)) {
output.push(`OP_${symbol.toUpperCase()}`)
} else {
output.push(symbol)
}
}
while (symbols.length > 0) {
next(symbols, output)
}
return output
}
const symbols = load(argv.input)
console.log(compile(symbols).join(" "))