@cashscript/utils 0.10.0-next.0 → 0.10.0-next.2
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/bitauth-script.d.ts +6 -0
- package/dist/bitauth-script.js +31 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/script.d.ts +3 -0
- package/dist/script.js +24 -1
- package/dist/{sourceMap.js → source-map.js} +1 -1
- package/package.json +3 -3
- package/dist/libauth.d.ts +0 -5
- package/dist/libauth.js +0 -50
- /package/dist/{sourceMap.d.ts → source-map.d.ts} +0 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Script } from './script.js';
|
|
2
|
+
export declare type LineToOpcodesMap = Record<string, Script>;
|
|
3
|
+
export declare type LineToAsmMap = Record<string, string>;
|
|
4
|
+
export declare function buildLineToOpcodesMap(bytecode: Script, souceMap: string): LineToOpcodesMap;
|
|
5
|
+
export declare function buildLineToAsmMap(bytecode: Script, souceMap: string): LineToAsmMap;
|
|
6
|
+
export declare function formatBitAuthScript(bytecode: Script, souceMap: string, sourceCode: string): string;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { scriptToBitAuthAsm } from './script.js';
|
|
2
|
+
import { sourceMapToLocationData } from './source-map.js';
|
|
3
|
+
export function buildLineToOpcodesMap(bytecode, souceMap) {
|
|
4
|
+
const locationData = sourceMapToLocationData(souceMap);
|
|
5
|
+
return locationData.reduce((lineToOpcodeMap, [location, positionHint], index) => {
|
|
6
|
+
const opcode = bytecode[index];
|
|
7
|
+
const line = positionHint ? location?.end.line : location?.start.line;
|
|
8
|
+
return {
|
|
9
|
+
...lineToOpcodeMap,
|
|
10
|
+
[line]: [...(lineToOpcodeMap[line] || []), opcode],
|
|
11
|
+
};
|
|
12
|
+
}, {});
|
|
13
|
+
}
|
|
14
|
+
export function buildLineToAsmMap(bytecode, souceMap) {
|
|
15
|
+
const lineToOpcodesMap = buildLineToOpcodesMap(bytecode, souceMap);
|
|
16
|
+
return Object.fromEntries(Object.entries(lineToOpcodesMap).map(([lineNumber, opcodeList]) => [lineNumber, scriptToBitAuthAsm(opcodeList)]));
|
|
17
|
+
}
|
|
18
|
+
export function formatBitAuthScript(bytecode, souceMap, sourceCode) {
|
|
19
|
+
const lineToAsmMap = buildLineToAsmMap(bytecode, souceMap);
|
|
20
|
+
const sourceCodeLines = sourceCode.split('\n');
|
|
21
|
+
const sourceCodeLineLengths = sourceCodeLines.map((line) => line.length);
|
|
22
|
+
const bytecodeLineLengths = Object.values(lineToAsmMap).map((line) => line.length);
|
|
23
|
+
const maxSourceCodeLength = Math.max(...sourceCodeLineLengths);
|
|
24
|
+
const maxBytecodeLength = Math.max(...bytecodeLineLengths);
|
|
25
|
+
const annotatedAsmLines = sourceCodeLines.map((line, index) => {
|
|
26
|
+
const lineAsm = lineToAsmMap[index + 1];
|
|
27
|
+
return `${(lineAsm || '').padEnd(maxBytecodeLength)} /* ${line.padEnd(maxSourceCodeLength)} */`;
|
|
28
|
+
});
|
|
29
|
+
return annotatedAsmLines.join('\n');
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=bitauth-script.js.map
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -3,6 +3,6 @@ export * from './data.js';
|
|
|
3
3
|
export * from './hash.js';
|
|
4
4
|
export * from './script.js';
|
|
5
5
|
export * from './types.js';
|
|
6
|
-
export * from './
|
|
7
|
-
export * from './
|
|
6
|
+
export * from './source-map.js';
|
|
7
|
+
export * from './bitauth-script.js';
|
|
8
8
|
//# sourceMappingURL=index.js.map
|
package/dist/script.d.ts
CHANGED
|
@@ -3,11 +3,14 @@ export declare type Op = number;
|
|
|
3
3
|
export declare type OpOrData = Op | Uint8Array;
|
|
4
4
|
export declare type Script = OpOrData[];
|
|
5
5
|
export declare function scriptToAsm(script: Script): string;
|
|
6
|
+
export declare function scriptToBitAuthAsm(script: Script): string;
|
|
6
7
|
export declare function asmToScript(asm: string): Script;
|
|
8
|
+
export declare function bitAuthAsmToScript(asm: string): Script;
|
|
7
9
|
export declare function scriptToBytecode(script: Script): Uint8Array;
|
|
8
10
|
export declare function bytecodeToScript(bytecode: Uint8Array): Script;
|
|
9
11
|
export declare function asmToBytecode(asm: string): Uint8Array;
|
|
10
12
|
export declare function bytecodeToAsm(bytecode: Uint8Array): string;
|
|
13
|
+
export declare function bytecodeToBitAuthAsm(bytecode: Uint8Array): string;
|
|
11
14
|
export declare function countOpcodes(script: Script): number;
|
|
12
15
|
export declare function calculateBytesize(script: Script): number;
|
|
13
16
|
export declare function encodeNullDataScript(chunks: OpOrData[]): Uint8Array;
|
package/dist/script.js
CHANGED
|
@@ -5,9 +5,18 @@ export const Op = OpcodesBCH;
|
|
|
5
5
|
export function scriptToAsm(script) {
|
|
6
6
|
return bytecodeToAsm(scriptToBytecode(script));
|
|
7
7
|
}
|
|
8
|
+
// TODO: See note at bytecodeToAsm
|
|
9
|
+
export function scriptToBitAuthAsm(script) {
|
|
10
|
+
return bytecodeToBitAuthAsm(scriptToBytecode(script));
|
|
11
|
+
}
|
|
12
|
+
// TODO: Figure out why OP_0 is decoded to an empty Uint8Array (while e.g. OP_3 is just OP_3)
|
|
8
13
|
export function asmToScript(asm) {
|
|
9
14
|
return bytecodeToScript(asmToBytecode(asm));
|
|
10
15
|
}
|
|
16
|
+
// asmToBytecode also works for BitAuth ASM
|
|
17
|
+
export function bitAuthAsmToScript(asm) {
|
|
18
|
+
return asmToScript(asm);
|
|
19
|
+
}
|
|
11
20
|
export function scriptToBytecode(script) {
|
|
12
21
|
// Convert the script elements to AuthenticationInstructions
|
|
13
22
|
const instructions = script.map((opOrData) => {
|
|
@@ -34,7 +43,8 @@ export function asmToBytecode(asm) {
|
|
|
34
43
|
if (token.startsWith('OP_')) {
|
|
35
44
|
return { opcode: Op[token] };
|
|
36
45
|
}
|
|
37
|
-
|
|
46
|
+
const data = token.replace(/<|>/g, '').replace(/^0x/, '');
|
|
47
|
+
return decodeAuthenticationInstructions(encodeDataPush(hexToBin(data)))[0];
|
|
38
48
|
});
|
|
39
49
|
// Convert the AuthenticationInstructions to bytecode
|
|
40
50
|
return encodeAuthenticationInstructions(instructions);
|
|
@@ -50,6 +60,19 @@ export function bytecodeToAsm(bytecode) {
|
|
|
50
60
|
asm = asm.replace(/\s+/g, ' ').trim();
|
|
51
61
|
return asm;
|
|
52
62
|
}
|
|
63
|
+
// TODO: If we decide to change the ASM / artifact format, we can remove this function and merge it
|
|
64
|
+
// with bytecodeToAsm
|
|
65
|
+
export function bytecodeToBitAuthAsm(bytecode) {
|
|
66
|
+
// Convert the bytecode to libauth's ASM format
|
|
67
|
+
let asm = disassembleBytecodeBCH(bytecode);
|
|
68
|
+
// COnvert libauth's ASM format to BITBOX's
|
|
69
|
+
asm = asm.replace(/OP_PUSHBYTES_[^\s]+/g, '');
|
|
70
|
+
asm = asm.replace(/OP_PUSHDATA[^\s]+ [^\s]+/g, '');
|
|
71
|
+
asm = asm.replace(/(^|\s)(0x\w*)/g, ' \<$2\>');
|
|
72
|
+
// Remove any duplicate whitespace
|
|
73
|
+
asm = asm.replace(/\s+/g, ' ').trim();
|
|
74
|
+
return asm;
|
|
75
|
+
}
|
|
53
76
|
export function countOpcodes(script) {
|
|
54
77
|
return script
|
|
55
78
|
.filter((opOrData) => typeof opOrData === 'number')
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cashscript/utils",
|
|
3
|
-
"version": "0.10.0-next.
|
|
3
|
+
"version": "0.10.0-next.2",
|
|
4
4
|
"description": "CashScript utilities and types",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bitcoin cash",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"compile:test": "tsc -p tsconfig.test.json",
|
|
36
36
|
"lint": "eslint . --ext .ts --ignore-path ../../.eslintignore",
|
|
37
37
|
"prepare": "yarn build",
|
|
38
|
-
"prepublishOnly": "yarn test",
|
|
38
|
+
"prepublishOnly": "yarn test && yarn lint",
|
|
39
39
|
"pretest": "yarn build:test",
|
|
40
40
|
"test": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest"
|
|
41
41
|
},
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
"jest": "^29.4.1",
|
|
50
50
|
"typescript": "^4.1.5"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "23f2f6fc78f6a911dfe559404be7741aa824e791"
|
|
53
53
|
}
|
package/dist/libauth.d.ts
DELETED
package/dist/libauth.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { binToHex } from '@bitauth/libauth';
|
|
2
|
-
import { Op } from './script.js';
|
|
3
|
-
import { sourceMapToLocationData } from './sourceMap.js';
|
|
4
|
-
function buildLineMap(bytecode, souceMap) {
|
|
5
|
-
const lineMap = {};
|
|
6
|
-
const locationData = sourceMapToLocationData(souceMap);
|
|
7
|
-
const map = locationData.map(([location, positionHint], index) => {
|
|
8
|
-
const op = bytecode[index];
|
|
9
|
-
return [
|
|
10
|
-
op,
|
|
11
|
-
Object.keys(Op)[Object.values(Op).indexOf(op)],
|
|
12
|
-
positionHint ? location?.end.line : location?.start.line,
|
|
13
|
-
];
|
|
14
|
-
});
|
|
15
|
-
map.forEach(([op, , line]) => {
|
|
16
|
-
if (!lineMap[line]) {
|
|
17
|
-
lineMap[line] = [];
|
|
18
|
-
}
|
|
19
|
-
lineMap[line].push(op);
|
|
20
|
-
});
|
|
21
|
-
return lineMap;
|
|
22
|
-
}
|
|
23
|
-
export function buildOpCodeMap(bytecode, souceMap) {
|
|
24
|
-
const lineMap = buildLineMap(bytecode, souceMap);
|
|
25
|
-
const opCodeMap = {};
|
|
26
|
-
for (const [key, value] of Object.entries(lineMap)) {
|
|
27
|
-
opCodeMap[key] = value.map((asmElement) => {
|
|
28
|
-
if (asmElement instanceof Uint8Array) {
|
|
29
|
-
if (asmElement.length === 0) {
|
|
30
|
-
return 'OP_0';
|
|
31
|
-
}
|
|
32
|
-
return `<0x${binToHex(asmElement)}>`;
|
|
33
|
-
}
|
|
34
|
-
return Object.keys(Op)[Object.values(Op).indexOf(asmElement)];
|
|
35
|
-
}).join(' ');
|
|
36
|
-
}
|
|
37
|
-
return opCodeMap;
|
|
38
|
-
}
|
|
39
|
-
export function formatLibauthScript(bytecode, souceMap, sourceCode) {
|
|
40
|
-
const opCodeMap = buildOpCodeMap(bytecode, souceMap);
|
|
41
|
-
const split = sourceCode.split('\n');
|
|
42
|
-
const maxCodeLength = Math.max(...split.map((val) => val.length));
|
|
43
|
-
const maxBytecodeLength = Math.max(...Object.values(opCodeMap).map((val) => val.length));
|
|
44
|
-
const result = split.map((line, index) => {
|
|
45
|
-
const opCodes = opCodeMap[index + 1];
|
|
46
|
-
return `${(opCodes || '').padEnd(maxBytecodeLength)} /* ${line.padEnd(maxCodeLength)} */`;
|
|
47
|
-
}).join('\n');
|
|
48
|
-
return result;
|
|
49
|
-
}
|
|
50
|
-
//# sourceMappingURL=libauth.js.map
|
|
File without changes
|