@cashscript/utils 0.6.4 → 0.7.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/dist/main/artifact.d.ts +1 -1
- package/dist/main/data.d.ts +1 -1
- package/dist/main/data.js +5 -5
- package/dist/main/index.d.ts +5 -5
- package/dist/main/index.js +10 -6
- package/dist/main/script.d.ts +19 -1
- package/dist/main/script.js +53 -14
- package/dist/main/types.d.ts +2 -2
- package/dist/main/types.js +2 -2
- package/dist/module/artifact.d.ts +1 -1
- package/dist/module/data.d.ts +1 -1
- package/dist/module/index.d.ts +5 -5
- package/dist/module/index.js +5 -5
- package/dist/module/script.d.ts +19 -1
- package/dist/module/script.js +41 -2
- package/dist/module/types.d.ts +2 -2
- package/dist/module/types.js +2 -2
- package/package.json +1 -1
package/dist/main/artifact.d.ts
CHANGED
package/dist/main/data.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare function encodeBool(bool: boolean): Uint8Array;
|
|
2
2
|
export declare function decodeBool(encodedBool: Uint8Array): boolean;
|
|
3
|
-
export declare function encodeInt(int: number): Uint8Array;
|
|
3
|
+
export declare function encodeInt(int: number | bigint): Uint8Array;
|
|
4
4
|
export declare function decodeInt(encodedInt: Uint8Array, maxLength?: number): number;
|
|
5
5
|
export declare function encodeString(str: string): Uint8Array;
|
|
6
6
|
export declare function decodeString(encodedString: Uint8Array): string;
|
package/dist/main/data.js
CHANGED
|
@@ -20,24 +20,24 @@ function decodeBool(encodedBool) {
|
|
|
20
20
|
}
|
|
21
21
|
exports.decodeBool = decodeBool;
|
|
22
22
|
function encodeInt(int) {
|
|
23
|
-
return libauth_1.bigIntToScriptNumber(BigInt(int));
|
|
23
|
+
return (0, libauth_1.bigIntToScriptNumber)(BigInt(int));
|
|
24
24
|
}
|
|
25
25
|
exports.encodeInt = encodeInt;
|
|
26
26
|
function decodeInt(encodedInt, maxLength) {
|
|
27
27
|
const options = { maximumScriptNumberByteLength: maxLength };
|
|
28
|
-
const result = libauth_1.parseBytesAsScriptNumber(encodedInt, options);
|
|
29
|
-
if (libauth_1.isScriptNumberError(result)) {
|
|
28
|
+
const result = (0, libauth_1.parseBytesAsScriptNumber)(encodedInt, options);
|
|
29
|
+
if ((0, libauth_1.isScriptNumberError)(result)) {
|
|
30
30
|
throw new Error(result);
|
|
31
31
|
}
|
|
32
32
|
return Number(result);
|
|
33
33
|
}
|
|
34
34
|
exports.decodeInt = decodeInt;
|
|
35
35
|
function encodeString(str) {
|
|
36
|
-
return libauth_1.utf8ToBin(str);
|
|
36
|
+
return (0, libauth_1.utf8ToBin)(str);
|
|
37
37
|
}
|
|
38
38
|
exports.encodeString = encodeString;
|
|
39
39
|
function decodeString(encodedString) {
|
|
40
|
-
return libauth_1.binToUtf8(encodedString);
|
|
40
|
+
return (0, libauth_1.binToUtf8)(encodedString);
|
|
41
41
|
}
|
|
42
42
|
exports.decodeString = decodeString;
|
|
43
43
|
function placeholder(size) {
|
package/dist/main/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from './artifact';
|
|
2
|
-
export * from './data';
|
|
3
|
-
export * from './hash';
|
|
4
|
-
export * from './script';
|
|
5
|
-
export * from './types';
|
|
1
|
+
export * from './artifact.js';
|
|
2
|
+
export * from './data.js';
|
|
3
|
+
export * from './hash.js';
|
|
4
|
+
export * from './script.js';
|
|
5
|
+
export * from './types.js';
|
package/dist/main/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -10,9 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
10
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
15
|
};
|
|
12
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
__exportStar(require("./artifact"), exports);
|
|
14
|
-
__exportStar(require("./data"), exports);
|
|
15
|
-
__exportStar(require("./hash"), exports);
|
|
16
|
-
__exportStar(require("./script"), exports);
|
|
17
|
-
__exportStar(require("./types"), exports);
|
|
17
|
+
__exportStar(require("./artifact.js"), exports);
|
|
18
|
+
__exportStar(require("./data.js"), exports);
|
|
19
|
+
__exportStar(require("./hash.js"), exports);
|
|
20
|
+
__exportStar(require("./script.js"), exports);
|
|
21
|
+
__exportStar(require("./types.js"), exports);
|
|
18
22
|
//# sourceMappingURL=index.js.map
|
package/dist/main/script.d.ts
CHANGED
|
@@ -3,6 +3,24 @@ export declare const Op: typeof OpcodesBCH;
|
|
|
3
3
|
export declare type Op = number;
|
|
4
4
|
export declare type OpOrData = Op | Uint8Array;
|
|
5
5
|
export declare type Script = OpOrData[];
|
|
6
|
+
export declare enum IntrospectionOp {
|
|
7
|
+
OP_INPUTINDEX = 192,
|
|
8
|
+
OP_ACTIVEBYTECODE = 193,
|
|
9
|
+
OP_TXVERSION = 194,
|
|
10
|
+
OP_TXINPUTCOUNT = 195,
|
|
11
|
+
OP_TXOUTPUTCOUNT = 196,
|
|
12
|
+
OP_TXLOCKTIME = 197,
|
|
13
|
+
OP_UTXOVALUE = 198,
|
|
14
|
+
OP_UTXOBYTECODE = 199,
|
|
15
|
+
OP_OUTPOINTTXHASH = 200,
|
|
16
|
+
OP_OUTPOINTINDEX = 201,
|
|
17
|
+
OP_INPUTBYTECODE = 202,
|
|
18
|
+
OP_INPUTSEQUENCENUMBER = 203,
|
|
19
|
+
OP_OUTPUTVALUE = 204,
|
|
20
|
+
OP_OUTPUTBYTECODE = 205
|
|
21
|
+
}
|
|
22
|
+
export declare const introspectionOpMapping: any;
|
|
23
|
+
export declare const reverseIntrospectionOpMapping: any;
|
|
6
24
|
export declare function scriptToAsm(script: Script): string;
|
|
7
25
|
export declare function asmToScript(asm: string): Script;
|
|
8
26
|
export declare function scriptToBytecode(script: Script): Uint8Array;
|
|
@@ -11,7 +29,7 @@ export declare function asmToBytecode(asm: string): Uint8Array;
|
|
|
11
29
|
export declare function bytecodeToAsm(bytecode: Uint8Array): string;
|
|
12
30
|
export declare function countOpcodes(script: Script): number;
|
|
13
31
|
export declare function calculateBytesize(script: Script): number;
|
|
14
|
-
export declare function encodeNullDataScript(chunks:
|
|
32
|
+
export declare function encodeNullDataScript(chunks: OpOrData[]): Uint8Array;
|
|
15
33
|
/**
|
|
16
34
|
* When cutting out the tx.bytecode preimage variable, the compiler does not know
|
|
17
35
|
* the size of the final redeem scrip yet, because the constructor parameters still
|
package/dist/main/script.js
CHANGED
|
@@ -3,11 +3,46 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.optimiseBytecode = exports.generateRedeemScript = exports.replaceBytecodeNop = exports.encodeNullDataScript = exports.calculateBytesize = exports.countOpcodes = exports.bytecodeToAsm = exports.asmToBytecode = exports.bytecodeToScript = exports.scriptToBytecode = exports.asmToScript = exports.scriptToAsm = exports.Op = void 0;
|
|
6
|
+
exports.optimiseBytecode = exports.generateRedeemScript = exports.replaceBytecodeNop = exports.encodeNullDataScript = exports.calculateBytesize = exports.countOpcodes = exports.bytecodeToAsm = exports.asmToBytecode = exports.bytecodeToScript = exports.scriptToBytecode = exports.asmToScript = exports.scriptToAsm = exports.reverseIntrospectionOpMapping = exports.introspectionOpMapping = exports.IntrospectionOp = exports.Op = void 0;
|
|
7
7
|
const libauth_1 = require("@bitauth/libauth");
|
|
8
|
-
const
|
|
9
|
-
const
|
|
8
|
+
const data_js_1 = require("./data.js");
|
|
9
|
+
const cashproof_optimisations_js_1 = __importDefault(require("./cashproof-optimisations.js"));
|
|
10
10
|
exports.Op = libauth_1.OpcodesBCH;
|
|
11
|
+
// TODO: Replace this when these opcodes are in Libauth
|
|
12
|
+
var IntrospectionOp;
|
|
13
|
+
(function (IntrospectionOp) {
|
|
14
|
+
IntrospectionOp[IntrospectionOp["OP_INPUTINDEX"] = 192] = "OP_INPUTINDEX";
|
|
15
|
+
IntrospectionOp[IntrospectionOp["OP_ACTIVEBYTECODE"] = 193] = "OP_ACTIVEBYTECODE";
|
|
16
|
+
IntrospectionOp[IntrospectionOp["OP_TXVERSION"] = 194] = "OP_TXVERSION";
|
|
17
|
+
IntrospectionOp[IntrospectionOp["OP_TXINPUTCOUNT"] = 195] = "OP_TXINPUTCOUNT";
|
|
18
|
+
IntrospectionOp[IntrospectionOp["OP_TXOUTPUTCOUNT"] = 196] = "OP_TXOUTPUTCOUNT";
|
|
19
|
+
IntrospectionOp[IntrospectionOp["OP_TXLOCKTIME"] = 197] = "OP_TXLOCKTIME";
|
|
20
|
+
IntrospectionOp[IntrospectionOp["OP_UTXOVALUE"] = 198] = "OP_UTXOVALUE";
|
|
21
|
+
IntrospectionOp[IntrospectionOp["OP_UTXOBYTECODE"] = 199] = "OP_UTXOBYTECODE";
|
|
22
|
+
IntrospectionOp[IntrospectionOp["OP_OUTPOINTTXHASH"] = 200] = "OP_OUTPOINTTXHASH";
|
|
23
|
+
IntrospectionOp[IntrospectionOp["OP_OUTPOINTINDEX"] = 201] = "OP_OUTPOINTINDEX";
|
|
24
|
+
IntrospectionOp[IntrospectionOp["OP_INPUTBYTECODE"] = 202] = "OP_INPUTBYTECODE";
|
|
25
|
+
IntrospectionOp[IntrospectionOp["OP_INPUTSEQUENCENUMBER"] = 203] = "OP_INPUTSEQUENCENUMBER";
|
|
26
|
+
IntrospectionOp[IntrospectionOp["OP_OUTPUTVALUE"] = 204] = "OP_OUTPUTVALUE";
|
|
27
|
+
IntrospectionOp[IntrospectionOp["OP_OUTPUTBYTECODE"] = 205] = "OP_OUTPUTBYTECODE";
|
|
28
|
+
})(IntrospectionOp = exports.IntrospectionOp || (exports.IntrospectionOp = {}));
|
|
29
|
+
exports.introspectionOpMapping = {
|
|
30
|
+
OP_INPUTINDEX: 'OP_UNKNOWN192',
|
|
31
|
+
OP_ACTIVEBYTECODE: 'OP_UNKNOWN193',
|
|
32
|
+
OP_TXVERSION: 'OP_UNKNOWN194',
|
|
33
|
+
OP_TXINPUTCOUNT: 'OP_UNKNOWN195',
|
|
34
|
+
OP_TXOUTPUTCOUNT: 'OP_UNKNOWN196',
|
|
35
|
+
OP_TXLOCKTIME: 'OP_UNKNOWN197',
|
|
36
|
+
OP_UTXOVALUE: 'OP_UNKNOWN198',
|
|
37
|
+
OP_UTXOBYTECODE: 'OP_UNKNOWN199',
|
|
38
|
+
OP_OUTPOINTTXHASH: 'OP_UNKNOWN200',
|
|
39
|
+
OP_OUTPOINTINDEX: 'OP_UNKNOWN201',
|
|
40
|
+
OP_INPUTBYTECODE: 'OP_UNKNOWN202',
|
|
41
|
+
OP_INPUTSEQUENCENUMBER: 'OP_UNKNOWN203',
|
|
42
|
+
OP_OUTPUTVALUE: 'OP_UNKNOWN204',
|
|
43
|
+
OP_OUTPUTBYTECODE: 'OP_UNKNOWN205',
|
|
44
|
+
};
|
|
45
|
+
exports.reverseIntrospectionOpMapping = Object.fromEntries(Object.entries(exports.introspectionOpMapping).map(([k, v]) => ([v, k])));
|
|
11
46
|
function scriptToAsm(script) {
|
|
12
47
|
return bytecodeToAsm(scriptToBytecode(script));
|
|
13
48
|
}
|
|
@@ -22,15 +57,15 @@ function scriptToBytecode(script) {
|
|
|
22
57
|
if (typeof opOrData === 'number') {
|
|
23
58
|
return { opcode: opOrData };
|
|
24
59
|
}
|
|
25
|
-
return libauth_1.parseBytecode(libauth_1.encodeDataPush(opOrData))[0];
|
|
60
|
+
return (0, libauth_1.parseBytecode)((0, libauth_1.encodeDataPush)(opOrData))[0];
|
|
26
61
|
});
|
|
27
62
|
// Convert the AuthenticationInstructions to bytecode
|
|
28
|
-
return libauth_1.serializeAuthenticationInstructions(instructions);
|
|
63
|
+
return (0, libauth_1.serializeAuthenticationInstructions)(instructions);
|
|
29
64
|
}
|
|
30
65
|
exports.scriptToBytecode = scriptToBytecode;
|
|
31
66
|
function bytecodeToScript(bytecode) {
|
|
32
67
|
// Convert the bytecode to AuthenticationInstructions
|
|
33
|
-
const instructions = libauth_1.parseBytecode(bytecode);
|
|
68
|
+
const instructions = (0, libauth_1.parseBytecode)(bytecode);
|
|
34
69
|
// Convert the AuthenticationInstructions to script elements
|
|
35
70
|
const script = instructions.map((instruction) => ('data' in instruction ? instruction.data : instruction.opcode));
|
|
36
71
|
return script;
|
|
@@ -39,24 +74,28 @@ exports.bytecodeToScript = bytecodeToScript;
|
|
|
39
74
|
function asmToBytecode(asm) {
|
|
40
75
|
// Remove any duplicate whitespace
|
|
41
76
|
asm = asm.replace(/\s+/g, ' ').trim();
|
|
77
|
+
// Replace introspection ops with OP_UNKNOWN... so Libauth gets it
|
|
78
|
+
asm = asm.split(' ').map((token) => { var _a; return (_a = exports.introspectionOpMapping[token]) !== null && _a !== void 0 ? _a : token; }).join(' ');
|
|
42
79
|
// Convert the ASM tokens to AuthenticationInstructions
|
|
43
80
|
const instructions = asm.split(' ').map((token) => {
|
|
44
81
|
if (token.startsWith('OP_')) {
|
|
45
82
|
return { opcode: exports.Op[token] };
|
|
46
83
|
}
|
|
47
|
-
return libauth_1.parseBytecode(libauth_1.encodeDataPush(libauth_1.hexToBin(token)))[0];
|
|
84
|
+
return (0, libauth_1.parseBytecode)((0, libauth_1.encodeDataPush)((0, libauth_1.hexToBin)(token)))[0];
|
|
48
85
|
});
|
|
49
86
|
// Convert the AuthenticationInstructions to bytecode
|
|
50
|
-
return libauth_1.serializeAuthenticationInstructions(instructions);
|
|
87
|
+
return (0, libauth_1.serializeAuthenticationInstructions)(instructions);
|
|
51
88
|
}
|
|
52
89
|
exports.asmToBytecode = asmToBytecode;
|
|
53
90
|
function bytecodeToAsm(bytecode) {
|
|
54
91
|
// Convert the bytecode to libauth's ASM format
|
|
55
|
-
let asm = libauth_1.disassembleBytecodeBCH(bytecode);
|
|
92
|
+
let asm = (0, libauth_1.disassembleBytecodeBCH)(bytecode);
|
|
56
93
|
// COnvert libauth's ASM format to BITBOX's
|
|
57
94
|
asm = asm.replace(/OP_PUSHBYTES_[^\s]+/g, '');
|
|
58
95
|
asm = asm.replace(/OP_PUSHDATA[^\s]+ [^\s]+/g, '');
|
|
59
96
|
asm = asm.replace(/(^|\s)0x/g, ' ');
|
|
97
|
+
// Replace OP_UNKNOWN... with the correct ops
|
|
98
|
+
asm = asm.split(' ').map((token) => { var _a; return (_a = exports.reverseIntrospectionOpMapping[token]) !== null && _a !== void 0 ? _a : token; }).join(' ');
|
|
60
99
|
// Remove any duplicate whitespace
|
|
61
100
|
asm = asm.replace(/\s+/g, ' ').trim();
|
|
62
101
|
return asm;
|
|
@@ -75,7 +114,7 @@ function calculateBytesize(script) {
|
|
|
75
114
|
exports.calculateBytesize = calculateBytesize;
|
|
76
115
|
// For encoding OP_RETURN data (doesn't require BIP62.3 / MINIMALDATA)
|
|
77
116
|
function encodeNullDataScript(chunks) {
|
|
78
|
-
return libauth_1.flattenBinArray(chunks.map((chunk) => {
|
|
117
|
+
return (0, libauth_1.flattenBinArray)(chunks.map((chunk) => {
|
|
79
118
|
if (typeof chunk === 'number') {
|
|
80
119
|
return new Uint8Array([chunk]);
|
|
81
120
|
}
|
|
@@ -114,7 +153,7 @@ function replaceBytecodeNop(script) {
|
|
|
114
153
|
// Retrieve size of current OP_SPLIT
|
|
115
154
|
let oldCut = script[index];
|
|
116
155
|
if (oldCut instanceof Uint8Array) {
|
|
117
|
-
oldCut =
|
|
156
|
+
oldCut = (0, data_js_1.decodeInt)(oldCut);
|
|
118
157
|
}
|
|
119
158
|
else if (oldCut === exports.Op.OP_0) {
|
|
120
159
|
oldCut = 0;
|
|
@@ -126,10 +165,10 @@ function replaceBytecodeNop(script) {
|
|
|
126
165
|
return script;
|
|
127
166
|
}
|
|
128
167
|
// Update the old OP_SPLIT by adding either 1 or 3 to it
|
|
129
|
-
script[index] =
|
|
168
|
+
script[index] = (0, data_js_1.encodeInt)(oldCut + 1);
|
|
130
169
|
const bytecodeSize = calculateBytesize(script);
|
|
131
170
|
if (bytecodeSize > 252) {
|
|
132
|
-
script[index] =
|
|
171
|
+
script[index] = (0, data_js_1.encodeInt)(oldCut + 3);
|
|
133
172
|
}
|
|
134
173
|
// Minimally encode
|
|
135
174
|
return asmToScript(scriptToAsm(script));
|
|
@@ -140,7 +179,7 @@ function generateRedeemScript(baseScript, encodedArgs) {
|
|
|
140
179
|
}
|
|
141
180
|
exports.generateRedeemScript = generateRedeemScript;
|
|
142
181
|
function optimiseBytecode(script, runs = 1000) {
|
|
143
|
-
const optimisations =
|
|
182
|
+
const optimisations = cashproof_optimisations_js_1.default
|
|
144
183
|
// Split by line and filter all line comments (#)
|
|
145
184
|
.split('\n')
|
|
146
185
|
.map((equiv) => equiv.trim())
|
package/dist/main/types.d.ts
CHANGED
|
@@ -12,8 +12,8 @@ export declare class BytesType {
|
|
|
12
12
|
toString(): string;
|
|
13
13
|
}
|
|
14
14
|
export declare class TupleType {
|
|
15
|
-
elementType?:
|
|
16
|
-
constructor(elementType?:
|
|
15
|
+
elementType?: Type | undefined;
|
|
16
|
+
constructor(elementType?: Type | undefined);
|
|
17
17
|
toString(): string;
|
|
18
18
|
}
|
|
19
19
|
export declare enum PrimitiveType {
|
package/dist/main/types.js
CHANGED
|
@@ -53,7 +53,7 @@ const ExplicitlyCastableTo = {
|
|
|
53
53
|
[PrimitiveType.BOOL]: [PrimitiveType.BOOL, PrimitiveType.INT],
|
|
54
54
|
[PrimitiveType.STRING]: [PrimitiveType.STRING],
|
|
55
55
|
[PrimitiveType.PUBKEY]: [PrimitiveType.PUBKEY],
|
|
56
|
-
[PrimitiveType.SIG]: [PrimitiveType.SIG
|
|
56
|
+
[PrimitiveType.SIG]: [PrimitiveType.SIG],
|
|
57
57
|
[PrimitiveType.DATASIG]: [PrimitiveType.DATASIG],
|
|
58
58
|
[PrimitiveType.ANY]: [],
|
|
59
59
|
};
|
|
@@ -93,7 +93,7 @@ function explicitlyCastable(from, to) {
|
|
|
93
93
|
if (from instanceof BytesType) {
|
|
94
94
|
// Can cast unbounded bytes or <=4 bytes to int
|
|
95
95
|
if (to === PrimitiveType.INT)
|
|
96
|
-
return !from.bound || from.bound <=
|
|
96
|
+
return !from.bound || from.bound <= 8;
|
|
97
97
|
// Can't cast bytes to bool or string
|
|
98
98
|
if (to === PrimitiveType.BOOL)
|
|
99
99
|
return false;
|
package/dist/module/data.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare function encodeBool(bool: boolean): Uint8Array;
|
|
2
2
|
export declare function decodeBool(encodedBool: Uint8Array): boolean;
|
|
3
|
-
export declare function encodeInt(int: number): Uint8Array;
|
|
3
|
+
export declare function encodeInt(int: number | bigint): Uint8Array;
|
|
4
4
|
export declare function decodeInt(encodedInt: Uint8Array, maxLength?: number): number;
|
|
5
5
|
export declare function encodeString(str: string): Uint8Array;
|
|
6
6
|
export declare function decodeString(encodedString: Uint8Array): string;
|
package/dist/module/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from './artifact';
|
|
2
|
-
export * from './data';
|
|
3
|
-
export * from './hash';
|
|
4
|
-
export * from './script';
|
|
5
|
-
export * from './types';
|
|
1
|
+
export * from './artifact.js';
|
|
2
|
+
export * from './data.js';
|
|
3
|
+
export * from './hash.js';
|
|
4
|
+
export * from './script.js';
|
|
5
|
+
export * from './types.js';
|
package/dist/module/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from './artifact';
|
|
2
|
-
export * from './data';
|
|
3
|
-
export * from './hash';
|
|
4
|
-
export * from './script';
|
|
5
|
-
export * from './types';
|
|
1
|
+
export * from './artifact.js';
|
|
2
|
+
export * from './data.js';
|
|
3
|
+
export * from './hash.js';
|
|
4
|
+
export * from './script.js';
|
|
5
|
+
export * from './types.js';
|
|
6
6
|
//# sourceMappingURL=index.js.map
|
package/dist/module/script.d.ts
CHANGED
|
@@ -3,6 +3,24 @@ export declare const Op: typeof OpcodesBCH;
|
|
|
3
3
|
export declare type Op = number;
|
|
4
4
|
export declare type OpOrData = Op | Uint8Array;
|
|
5
5
|
export declare type Script = OpOrData[];
|
|
6
|
+
export declare enum IntrospectionOp {
|
|
7
|
+
OP_INPUTINDEX = 192,
|
|
8
|
+
OP_ACTIVEBYTECODE = 193,
|
|
9
|
+
OP_TXVERSION = 194,
|
|
10
|
+
OP_TXINPUTCOUNT = 195,
|
|
11
|
+
OP_TXOUTPUTCOUNT = 196,
|
|
12
|
+
OP_TXLOCKTIME = 197,
|
|
13
|
+
OP_UTXOVALUE = 198,
|
|
14
|
+
OP_UTXOBYTECODE = 199,
|
|
15
|
+
OP_OUTPOINTTXHASH = 200,
|
|
16
|
+
OP_OUTPOINTINDEX = 201,
|
|
17
|
+
OP_INPUTBYTECODE = 202,
|
|
18
|
+
OP_INPUTSEQUENCENUMBER = 203,
|
|
19
|
+
OP_OUTPUTVALUE = 204,
|
|
20
|
+
OP_OUTPUTBYTECODE = 205
|
|
21
|
+
}
|
|
22
|
+
export declare const introspectionOpMapping: any;
|
|
23
|
+
export declare const reverseIntrospectionOpMapping: any;
|
|
6
24
|
export declare function scriptToAsm(script: Script): string;
|
|
7
25
|
export declare function asmToScript(asm: string): Script;
|
|
8
26
|
export declare function scriptToBytecode(script: Script): Uint8Array;
|
|
@@ -11,7 +29,7 @@ export declare function asmToBytecode(asm: string): Uint8Array;
|
|
|
11
29
|
export declare function bytecodeToAsm(bytecode: Uint8Array): string;
|
|
12
30
|
export declare function countOpcodes(script: Script): number;
|
|
13
31
|
export declare function calculateBytesize(script: Script): number;
|
|
14
|
-
export declare function encodeNullDataScript(chunks:
|
|
32
|
+
export declare function encodeNullDataScript(chunks: OpOrData[]): Uint8Array;
|
|
15
33
|
/**
|
|
16
34
|
* When cutting out the tx.bytecode preimage variable, the compiler does not know
|
|
17
35
|
* the size of the final redeem scrip yet, because the constructor parameters still
|
package/dist/module/script.js
CHANGED
|
@@ -1,7 +1,42 @@
|
|
|
1
1
|
import { OpcodesBCH, encodeDataPush, parseBytecode, serializeAuthenticationInstructions, hexToBin, disassembleBytecodeBCH, flattenBinArray, } from '@bitauth/libauth';
|
|
2
|
-
import { decodeInt, encodeInt } from './data';
|
|
3
|
-
import OptimisationsEquivFile from './cashproof-optimisations';
|
|
2
|
+
import { decodeInt, encodeInt } from './data.js';
|
|
3
|
+
import OptimisationsEquivFile from './cashproof-optimisations.js';
|
|
4
4
|
export const Op = OpcodesBCH;
|
|
5
|
+
// TODO: Replace this when these opcodes are in Libauth
|
|
6
|
+
export var IntrospectionOp;
|
|
7
|
+
(function (IntrospectionOp) {
|
|
8
|
+
IntrospectionOp[IntrospectionOp["OP_INPUTINDEX"] = 192] = "OP_INPUTINDEX";
|
|
9
|
+
IntrospectionOp[IntrospectionOp["OP_ACTIVEBYTECODE"] = 193] = "OP_ACTIVEBYTECODE";
|
|
10
|
+
IntrospectionOp[IntrospectionOp["OP_TXVERSION"] = 194] = "OP_TXVERSION";
|
|
11
|
+
IntrospectionOp[IntrospectionOp["OP_TXINPUTCOUNT"] = 195] = "OP_TXINPUTCOUNT";
|
|
12
|
+
IntrospectionOp[IntrospectionOp["OP_TXOUTPUTCOUNT"] = 196] = "OP_TXOUTPUTCOUNT";
|
|
13
|
+
IntrospectionOp[IntrospectionOp["OP_TXLOCKTIME"] = 197] = "OP_TXLOCKTIME";
|
|
14
|
+
IntrospectionOp[IntrospectionOp["OP_UTXOVALUE"] = 198] = "OP_UTXOVALUE";
|
|
15
|
+
IntrospectionOp[IntrospectionOp["OP_UTXOBYTECODE"] = 199] = "OP_UTXOBYTECODE";
|
|
16
|
+
IntrospectionOp[IntrospectionOp["OP_OUTPOINTTXHASH"] = 200] = "OP_OUTPOINTTXHASH";
|
|
17
|
+
IntrospectionOp[IntrospectionOp["OP_OUTPOINTINDEX"] = 201] = "OP_OUTPOINTINDEX";
|
|
18
|
+
IntrospectionOp[IntrospectionOp["OP_INPUTBYTECODE"] = 202] = "OP_INPUTBYTECODE";
|
|
19
|
+
IntrospectionOp[IntrospectionOp["OP_INPUTSEQUENCENUMBER"] = 203] = "OP_INPUTSEQUENCENUMBER";
|
|
20
|
+
IntrospectionOp[IntrospectionOp["OP_OUTPUTVALUE"] = 204] = "OP_OUTPUTVALUE";
|
|
21
|
+
IntrospectionOp[IntrospectionOp["OP_OUTPUTBYTECODE"] = 205] = "OP_OUTPUTBYTECODE";
|
|
22
|
+
})(IntrospectionOp || (IntrospectionOp = {}));
|
|
23
|
+
export const introspectionOpMapping = {
|
|
24
|
+
OP_INPUTINDEX: 'OP_UNKNOWN192',
|
|
25
|
+
OP_ACTIVEBYTECODE: 'OP_UNKNOWN193',
|
|
26
|
+
OP_TXVERSION: 'OP_UNKNOWN194',
|
|
27
|
+
OP_TXINPUTCOUNT: 'OP_UNKNOWN195',
|
|
28
|
+
OP_TXOUTPUTCOUNT: 'OP_UNKNOWN196',
|
|
29
|
+
OP_TXLOCKTIME: 'OP_UNKNOWN197',
|
|
30
|
+
OP_UTXOVALUE: 'OP_UNKNOWN198',
|
|
31
|
+
OP_UTXOBYTECODE: 'OP_UNKNOWN199',
|
|
32
|
+
OP_OUTPOINTTXHASH: 'OP_UNKNOWN200',
|
|
33
|
+
OP_OUTPOINTINDEX: 'OP_UNKNOWN201',
|
|
34
|
+
OP_INPUTBYTECODE: 'OP_UNKNOWN202',
|
|
35
|
+
OP_INPUTSEQUENCENUMBER: 'OP_UNKNOWN203',
|
|
36
|
+
OP_OUTPUTVALUE: 'OP_UNKNOWN204',
|
|
37
|
+
OP_OUTPUTBYTECODE: 'OP_UNKNOWN205',
|
|
38
|
+
};
|
|
39
|
+
export const reverseIntrospectionOpMapping = Object.fromEntries(Object.entries(introspectionOpMapping).map(([k, v]) => ([v, k])));
|
|
5
40
|
export function scriptToAsm(script) {
|
|
6
41
|
return bytecodeToAsm(scriptToBytecode(script));
|
|
7
42
|
}
|
|
@@ -29,6 +64,8 @@ export function bytecodeToScript(bytecode) {
|
|
|
29
64
|
export function asmToBytecode(asm) {
|
|
30
65
|
// Remove any duplicate whitespace
|
|
31
66
|
asm = asm.replace(/\s+/g, ' ').trim();
|
|
67
|
+
// Replace introspection ops with OP_UNKNOWN... so Libauth gets it
|
|
68
|
+
asm = asm.split(' ').map((token) => { var _a; return (_a = introspectionOpMapping[token]) !== null && _a !== void 0 ? _a : token; }).join(' ');
|
|
32
69
|
// Convert the ASM tokens to AuthenticationInstructions
|
|
33
70
|
const instructions = asm.split(' ').map((token) => {
|
|
34
71
|
if (token.startsWith('OP_')) {
|
|
@@ -46,6 +83,8 @@ export function bytecodeToAsm(bytecode) {
|
|
|
46
83
|
asm = asm.replace(/OP_PUSHBYTES_[^\s]+/g, '');
|
|
47
84
|
asm = asm.replace(/OP_PUSHDATA[^\s]+ [^\s]+/g, '');
|
|
48
85
|
asm = asm.replace(/(^|\s)0x/g, ' ');
|
|
86
|
+
// Replace OP_UNKNOWN... with the correct ops
|
|
87
|
+
asm = asm.split(' ').map((token) => { var _a; return (_a = reverseIntrospectionOpMapping[token]) !== null && _a !== void 0 ? _a : token; }).join(' ');
|
|
49
88
|
// Remove any duplicate whitespace
|
|
50
89
|
asm = asm.replace(/\s+/g, ' ').trim();
|
|
51
90
|
return asm;
|
package/dist/module/types.d.ts
CHANGED
|
@@ -12,8 +12,8 @@ export declare class BytesType {
|
|
|
12
12
|
toString(): string;
|
|
13
13
|
}
|
|
14
14
|
export declare class TupleType {
|
|
15
|
-
elementType?:
|
|
16
|
-
constructor(elementType?:
|
|
15
|
+
elementType?: Type | undefined;
|
|
16
|
+
constructor(elementType?: Type | undefined);
|
|
17
17
|
toString(): string;
|
|
18
18
|
}
|
|
19
19
|
export declare enum PrimitiveType {
|
package/dist/module/types.js
CHANGED
|
@@ -47,7 +47,7 @@ const ExplicitlyCastableTo = {
|
|
|
47
47
|
[PrimitiveType.BOOL]: [PrimitiveType.BOOL, PrimitiveType.INT],
|
|
48
48
|
[PrimitiveType.STRING]: [PrimitiveType.STRING],
|
|
49
49
|
[PrimitiveType.PUBKEY]: [PrimitiveType.PUBKEY],
|
|
50
|
-
[PrimitiveType.SIG]: [PrimitiveType.SIG
|
|
50
|
+
[PrimitiveType.SIG]: [PrimitiveType.SIG],
|
|
51
51
|
[PrimitiveType.DATASIG]: [PrimitiveType.DATASIG],
|
|
52
52
|
[PrimitiveType.ANY]: [],
|
|
53
53
|
};
|
|
@@ -87,7 +87,7 @@ export function explicitlyCastable(from, to) {
|
|
|
87
87
|
if (from instanceof BytesType) {
|
|
88
88
|
// Can cast unbounded bytes or <=4 bytes to int
|
|
89
89
|
if (to === PrimitiveType.INT)
|
|
90
|
-
return !from.bound || from.bound <=
|
|
90
|
+
return !from.bound || from.bound <= 8;
|
|
91
91
|
// Can't cast bytes to bool or string
|
|
92
92
|
if (to === PrimitiveType.BOOL)
|
|
93
93
|
return false;
|