@bitcoinerlab/miniscript-policies 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +138 -10
- package/dist/index.js +63 -47
- package/package.json +6 -1
- package/types/index.d.ts +7 -4
- package/Makefile +0 -14
- package/example.js +0 -34
- package/js_bindings.cpp +0 -154
- package/patches/uppercase-h.patch +0 -11
- package/rollup.config.js +0 -13
- package/src/index.js +0 -5
- package/src/miniscript.js +0 -149
- package/test/fixtures.js +0 -898
- package/test/miniscript.test.js +0 -21
- package/test/policy.test.js +0 -20
package/src/miniscript.js
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
// Initial author: Pieter Wuille ( https://github.com/sipa/miniscript/blob/master/index.html)
|
|
2
|
-
// Adapted by Jose-Luis Landabaso - https://bitcoinerlab.com:
|
|
3
|
-
// compilePolicy, compileMiniscript with issane, issanesublevel and cleanAsm
|
|
4
|
-
|
|
5
|
-
import bindings from './bindings.js';
|
|
6
|
-
|
|
7
|
-
const cleanAsm = asm =>
|
|
8
|
-
asm
|
|
9
|
-
.trim()
|
|
10
|
-
.replace(/\n/g, ' ')
|
|
11
|
-
.replace(/ +(?= )/g, '');
|
|
12
|
-
|
|
13
|
-
let Module;
|
|
14
|
-
let em_miniscript_compile;
|
|
15
|
-
let em_miniscript_analyze;
|
|
16
|
-
let modulePromise;
|
|
17
|
-
|
|
18
|
-
const initModule = () => {
|
|
19
|
-
if (!modulePromise) {
|
|
20
|
-
modulePromise = Promise.resolve(bindings()).then(resolved => {
|
|
21
|
-
Module = resolved;
|
|
22
|
-
em_miniscript_compile = Module.cwrap('miniscript_compile', 'none', [
|
|
23
|
-
'string',
|
|
24
|
-
'number',
|
|
25
|
-
'number',
|
|
26
|
-
'number',
|
|
27
|
-
'number',
|
|
28
|
-
'number',
|
|
29
|
-
'number'
|
|
30
|
-
]);
|
|
31
|
-
em_miniscript_analyze = Module.cwrap('miniscript_analyze', 'none', [
|
|
32
|
-
'string',
|
|
33
|
-
'number',
|
|
34
|
-
'number',
|
|
35
|
-
'number',
|
|
36
|
-
'number'
|
|
37
|
-
]);
|
|
38
|
-
return Module;
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return modulePromise;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export const ready = initModule();
|
|
46
|
-
|
|
47
|
-
const ensureReady = () => {
|
|
48
|
-
if (!Module || !em_miniscript_compile || !em_miniscript_analyze) {
|
|
49
|
-
throw new Error('Miniscript bindings not ready. Await ready before calling compile functions.');
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @typedef {Object} CompilePolicyResult
|
|
55
|
-
* @property {string} miniscript - The compiled miniscript expression.
|
|
56
|
-
* @property {string} asm - The compiled miniscript as Bitcoin asm code.
|
|
57
|
-
* @property {boolean} issane - Whether the miniscript is sane at the top level.
|
|
58
|
-
* @property {boolean} issanesublevel - Whether the miniscript is sane at the sublevel.
|
|
59
|
-
*/
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* @typedef {Object} CompileMiniscriptResult
|
|
63
|
-
* @property {string} asm - The Bitcoin asm code of the compiled miniscript expression.
|
|
64
|
-
* @property {boolean} issane - Whether the miniscript is sane at the top level.
|
|
65
|
-
* @property {boolean} issanesublevel - Whether the miniscript is sane at the sublevel.
|
|
66
|
-
*/
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Compiles a miniscript policy into a miniscript expression (if possible).
|
|
71
|
-
* @function
|
|
72
|
-
*
|
|
73
|
-
* @param {string} policy - The miniscript policy to compile.
|
|
74
|
-
* @returns {CompilePolicyResult}
|
|
75
|
-
*/
|
|
76
|
-
export const compilePolicy = policy => {
|
|
77
|
-
ensureReady();
|
|
78
|
-
const miniscript = Module._malloc(10000);
|
|
79
|
-
const cost = Module._malloc(500);
|
|
80
|
-
const asm = Module._malloc(100000);
|
|
81
|
-
const issane = Module._malloc(10);
|
|
82
|
-
const issanesublevel = Module._malloc(10);
|
|
83
|
-
em_miniscript_compile(
|
|
84
|
-
policy,
|
|
85
|
-
miniscript,
|
|
86
|
-
10000,
|
|
87
|
-
cost,
|
|
88
|
-
500,
|
|
89
|
-
asm,
|
|
90
|
-
100000,
|
|
91
|
-
issane,
|
|
92
|
-
10,
|
|
93
|
-
issanesublevel,
|
|
94
|
-
10
|
|
95
|
-
);
|
|
96
|
-
const result = {
|
|
97
|
-
miniscript: Module.UTF8ToString(miniscript),
|
|
98
|
-
asm: cleanAsm(Module.UTF8ToString(asm)),
|
|
99
|
-
issane: Module.UTF8ToString(issane) === 'true' ? true : false,
|
|
100
|
-
issanesublevel:
|
|
101
|
-
Module.UTF8ToString(issanesublevel) === 'true' ? true : false
|
|
102
|
-
};
|
|
103
|
-
Module._free(miniscript);
|
|
104
|
-
Module._free(cost);
|
|
105
|
-
Module._free(asm);
|
|
106
|
-
Module._free(issane);
|
|
107
|
-
Module._free(issanesublevel);
|
|
108
|
-
|
|
109
|
-
return result;
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Compiles a miniscript expression and returns its asm code.
|
|
114
|
-
* @function
|
|
115
|
-
*
|
|
116
|
-
* @param {string} miniscript - A miniscript expression.
|
|
117
|
-
* @returns {CompileMiniscriptResult}
|
|
118
|
-
*/
|
|
119
|
-
export const compileMiniscript = miniscript => {
|
|
120
|
-
ensureReady();
|
|
121
|
-
const analysis = Module._malloc(50000);
|
|
122
|
-
const asm = Module._malloc(100000);
|
|
123
|
-
const issane = Module._malloc(10);
|
|
124
|
-
const issanesublevel = Module._malloc(10);
|
|
125
|
-
em_miniscript_analyze(
|
|
126
|
-
miniscript,
|
|
127
|
-
analysis,
|
|
128
|
-
50000,
|
|
129
|
-
asm,
|
|
130
|
-
100000,
|
|
131
|
-
issane,
|
|
132
|
-
10,
|
|
133
|
-
issanesublevel,
|
|
134
|
-
10
|
|
135
|
-
);
|
|
136
|
-
const result_asm = Module.UTF8ToString(asm);
|
|
137
|
-
const result_issane = Module.UTF8ToString(issane);
|
|
138
|
-
const result_issanesublebel = Module.UTF8ToString(issanesublevel);
|
|
139
|
-
Module._free(analysis);
|
|
140
|
-
Module._free(asm);
|
|
141
|
-
Module._free(issane);
|
|
142
|
-
Module._free(issanesublevel);
|
|
143
|
-
|
|
144
|
-
return {
|
|
145
|
-
asm: cleanAsm(result_asm),
|
|
146
|
-
issane: result_issane === 'true' ? true : false,
|
|
147
|
-
issanesublevel: result_issanesublebel === 'true' ? true : false
|
|
148
|
-
};
|
|
149
|
-
};
|