@cashscript/utils 0.7.0-next.0 → 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/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) {
@@ -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';
@@ -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.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
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
@@ -1,4 +1,8 @@
1
1
  import { OpcodesBCH } from '@bitauth/libauth';
2
+ export declare const Op: typeof OpcodesBCH;
3
+ export declare type Op = number;
4
+ export declare type OpOrData = Op | Uint8Array;
5
+ export declare type Script = OpOrData[];
2
6
  export declare enum IntrospectionOp {
3
7
  OP_INPUTINDEX = 192,
4
8
  OP_ACTIVEBYTECODE = 193,
@@ -15,10 +19,8 @@ export declare enum IntrospectionOp {
15
19
  OP_OUTPUTVALUE = 204,
16
20
  OP_OUTPUTBYTECODE = 205
17
21
  }
18
- export declare const Op: typeof OpcodesBCH;
19
- export declare type Op = number;
20
- export declare type OpOrData = Op | Uint8Array;
21
- export declare type Script = OpOrData[];
22
+ export declare const introspectionOpMapping: any;
23
+ export declare const reverseIntrospectionOpMapping: any;
22
24
  export declare function scriptToAsm(script: Script): string;
23
25
  export declare function asmToScript(asm: string): Script;
24
26
  export declare function scriptToBytecode(script: Script): Uint8Array;
@@ -3,10 +3,11 @@ 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 = exports.IntrospectionOp = 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 data_1 = require("./data");
9
- const cashproof_optimisations_1 = __importDefault(require("./cashproof-optimisations"));
8
+ const data_js_1 = require("./data.js");
9
+ const cashproof_optimisations_js_1 = __importDefault(require("./cashproof-optimisations.js"));
10
+ exports.Op = libauth_1.OpcodesBCH;
10
11
  // TODO: Replace this when these opcodes are in Libauth
11
12
  var IntrospectionOp;
12
13
  (function (IntrospectionOp) {
@@ -25,7 +26,23 @@ var IntrospectionOp;
25
26
  IntrospectionOp[IntrospectionOp["OP_OUTPUTVALUE"] = 204] = "OP_OUTPUTVALUE";
26
27
  IntrospectionOp[IntrospectionOp["OP_OUTPUTBYTECODE"] = 205] = "OP_OUTPUTBYTECODE";
27
28
  })(IntrospectionOp = exports.IntrospectionOp || (exports.IntrospectionOp = {}));
28
- exports.Op = libauth_1.OpcodesBCH;
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])));
29
46
  function scriptToAsm(script) {
30
47
  return bytecodeToAsm(scriptToBytecode(script));
31
48
  }
@@ -40,15 +57,15 @@ function scriptToBytecode(script) {
40
57
  if (typeof opOrData === 'number') {
41
58
  return { opcode: opOrData };
42
59
  }
43
- return libauth_1.parseBytecode(libauth_1.encodeDataPush(opOrData))[0];
60
+ return (0, libauth_1.parseBytecode)((0, libauth_1.encodeDataPush)(opOrData))[0];
44
61
  });
45
62
  // Convert the AuthenticationInstructions to bytecode
46
- return libauth_1.serializeAuthenticationInstructions(instructions);
63
+ return (0, libauth_1.serializeAuthenticationInstructions)(instructions);
47
64
  }
48
65
  exports.scriptToBytecode = scriptToBytecode;
49
66
  function bytecodeToScript(bytecode) {
50
67
  // Convert the bytecode to AuthenticationInstructions
51
- const instructions = libauth_1.parseBytecode(bytecode);
68
+ const instructions = (0, libauth_1.parseBytecode)(bytecode);
52
69
  // Convert the AuthenticationInstructions to script elements
53
70
  const script = instructions.map((instruction) => ('data' in instruction ? instruction.data : instruction.opcode));
54
71
  return script;
@@ -57,24 +74,28 @@ exports.bytecodeToScript = bytecodeToScript;
57
74
  function asmToBytecode(asm) {
58
75
  // Remove any duplicate whitespace
59
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(' ');
60
79
  // Convert the ASM tokens to AuthenticationInstructions
61
80
  const instructions = asm.split(' ').map((token) => {
62
81
  if (token.startsWith('OP_')) {
63
82
  return { opcode: exports.Op[token] };
64
83
  }
65
- 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];
66
85
  });
67
86
  // Convert the AuthenticationInstructions to bytecode
68
- return libauth_1.serializeAuthenticationInstructions(instructions);
87
+ return (0, libauth_1.serializeAuthenticationInstructions)(instructions);
69
88
  }
70
89
  exports.asmToBytecode = asmToBytecode;
71
90
  function bytecodeToAsm(bytecode) {
72
91
  // Convert the bytecode to libauth's ASM format
73
- let asm = libauth_1.disassembleBytecodeBCH(bytecode);
92
+ let asm = (0, libauth_1.disassembleBytecodeBCH)(bytecode);
74
93
  // COnvert libauth's ASM format to BITBOX's
75
94
  asm = asm.replace(/OP_PUSHBYTES_[^\s]+/g, '');
76
95
  asm = asm.replace(/OP_PUSHDATA[^\s]+ [^\s]+/g, '');
77
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(' ');
78
99
  // Remove any duplicate whitespace
79
100
  asm = asm.replace(/\s+/g, ' ').trim();
80
101
  return asm;
@@ -93,7 +114,7 @@ function calculateBytesize(script) {
93
114
  exports.calculateBytesize = calculateBytesize;
94
115
  // For encoding OP_RETURN data (doesn't require BIP62.3 / MINIMALDATA)
95
116
  function encodeNullDataScript(chunks) {
96
- return libauth_1.flattenBinArray(chunks.map((chunk) => {
117
+ return (0, libauth_1.flattenBinArray)(chunks.map((chunk) => {
97
118
  if (typeof chunk === 'number') {
98
119
  return new Uint8Array([chunk]);
99
120
  }
@@ -132,7 +153,7 @@ function replaceBytecodeNop(script) {
132
153
  // Retrieve size of current OP_SPLIT
133
154
  let oldCut = script[index];
134
155
  if (oldCut instanceof Uint8Array) {
135
- oldCut = data_1.decodeInt(oldCut);
156
+ oldCut = (0, data_js_1.decodeInt)(oldCut);
136
157
  }
137
158
  else if (oldCut === exports.Op.OP_0) {
138
159
  oldCut = 0;
@@ -144,10 +165,10 @@ function replaceBytecodeNop(script) {
144
165
  return script;
145
166
  }
146
167
  // Update the old OP_SPLIT by adding either 1 or 3 to it
147
- script[index] = data_1.encodeInt(oldCut + 1);
168
+ script[index] = (0, data_js_1.encodeInt)(oldCut + 1);
148
169
  const bytecodeSize = calculateBytesize(script);
149
170
  if (bytecodeSize > 252) {
150
- script[index] = data_1.encodeInt(oldCut + 3);
171
+ script[index] = (0, data_js_1.encodeInt)(oldCut + 3);
151
172
  }
152
173
  // Minimally encode
153
174
  return asmToScript(scriptToAsm(script));
@@ -158,7 +179,7 @@ function generateRedeemScript(baseScript, encodedArgs) {
158
179
  }
159
180
  exports.generateRedeemScript = generateRedeemScript;
160
181
  function optimiseBytecode(script, runs = 1000) {
161
- const optimisations = cashproof_optimisations_1.default
182
+ const optimisations = cashproof_optimisations_js_1.default
162
183
  // Split by line and filter all line comments (#)
163
184
  .split('\n')
164
185
  .map((equiv) => equiv.trim())
@@ -12,8 +12,8 @@ export declare class BytesType {
12
12
  toString(): string;
13
13
  }
14
14
  export declare class TupleType {
15
- elementType?: PrimitiveType | ArrayType | TupleType | BytesType | undefined;
16
- constructor(elementType?: PrimitiveType | ArrayType | TupleType | BytesType | undefined);
15
+ elementType?: Type | undefined;
16
+ constructor(elementType?: Type | undefined);
17
17
  toString(): string;
18
18
  }
19
19
  export declare enum PrimitiveType {
@@ -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';
@@ -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
@@ -1,4 +1,8 @@
1
1
  import { OpcodesBCH } from '@bitauth/libauth';
2
+ export declare const Op: typeof OpcodesBCH;
3
+ export declare type Op = number;
4
+ export declare type OpOrData = Op | Uint8Array;
5
+ export declare type Script = OpOrData[];
2
6
  export declare enum IntrospectionOp {
3
7
  OP_INPUTINDEX = 192,
4
8
  OP_ACTIVEBYTECODE = 193,
@@ -15,10 +19,8 @@ export declare enum IntrospectionOp {
15
19
  OP_OUTPUTVALUE = 204,
16
20
  OP_OUTPUTBYTECODE = 205
17
21
  }
18
- export declare const Op: typeof OpcodesBCH;
19
- export declare type Op = number;
20
- export declare type OpOrData = Op | Uint8Array;
21
- export declare type Script = OpOrData[];
22
+ export declare const introspectionOpMapping: any;
23
+ export declare const reverseIntrospectionOpMapping: any;
22
24
  export declare function scriptToAsm(script: Script): string;
23
25
  export declare function asmToScript(asm: string): Script;
24
26
  export declare function scriptToBytecode(script: Script): Uint8Array;
@@ -1,6 +1,7 @@
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
+ export const Op = OpcodesBCH;
4
5
  // TODO: Replace this when these opcodes are in Libauth
5
6
  export var IntrospectionOp;
6
7
  (function (IntrospectionOp) {
@@ -19,7 +20,23 @@ export var IntrospectionOp;
19
20
  IntrospectionOp[IntrospectionOp["OP_OUTPUTVALUE"] = 204] = "OP_OUTPUTVALUE";
20
21
  IntrospectionOp[IntrospectionOp["OP_OUTPUTBYTECODE"] = 205] = "OP_OUTPUTBYTECODE";
21
22
  })(IntrospectionOp || (IntrospectionOp = {}));
22
- export const Op = OpcodesBCH;
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])));
23
40
  export function scriptToAsm(script) {
24
41
  return bytecodeToAsm(scriptToBytecode(script));
25
42
  }
@@ -47,6 +64,8 @@ export function bytecodeToScript(bytecode) {
47
64
  export function asmToBytecode(asm) {
48
65
  // Remove any duplicate whitespace
49
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(' ');
50
69
  // Convert the ASM tokens to AuthenticationInstructions
51
70
  const instructions = asm.split(' ').map((token) => {
52
71
  if (token.startsWith('OP_')) {
@@ -64,6 +83,8 @@ export function bytecodeToAsm(bytecode) {
64
83
  asm = asm.replace(/OP_PUSHBYTES_[^\s]+/g, '');
65
84
  asm = asm.replace(/OP_PUSHDATA[^\s]+ [^\s]+/g, '');
66
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(' ');
67
88
  // Remove any duplicate whitespace
68
89
  asm = asm.replace(/\s+/g, ' ').trim();
69
90
  return asm;
@@ -12,8 +12,8 @@ export declare class BytesType {
12
12
  toString(): string;
13
13
  }
14
14
  export declare class TupleType {
15
- elementType?: PrimitiveType | ArrayType | TupleType | BytesType | undefined;
16
- constructor(elementType?: PrimitiveType | ArrayType | TupleType | BytesType | undefined);
15
+ elementType?: Type | undefined;
16
+ constructor(elementType?: Type | undefined);
17
17
  toString(): string;
18
18
  }
19
19
  export declare enum PrimitiveType {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cashscript/utils",
3
- "version": "0.7.0-next.0",
3
+ "version": "0.7.0",
4
4
  "description": "CashScript utilities and types",
5
5
  "keywords": [
6
6
  "bitcoin cash",