@cashscript/utils 0.10.1 → 0.11.0-next.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.
@@ -0,0 +1,7 @@
1
+ interface DecodedSequence {
2
+ blocks?: number;
3
+ seconds?: number;
4
+ }
5
+ export declare function decodeBip68(sequence: number): DecodedSequence;
6
+ export declare function encodeBip68({ blocks, seconds }: DecodedSequence): number;
7
+ export {};
package/dist/bip68.js ADDED
@@ -0,0 +1,51 @@
1
+ // Code taken and adapted from https://github.com/bitcoinjs/bip68
2
+ // If we make significant changes to this code, we should also take and adapt the tests from that repository.
3
+ // see https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki#compatibility
4
+ const SEQUENCE_FINAL = 0xffffffff;
5
+ const SEQUENCE_LOCKTIME_DISABLE_FLAG = (1 << 31);
6
+ const SEQUENCE_LOCKTIME_GRANULARITY = 9;
7
+ const SEQUENCE_LOCKTIME_MASK = 0x0000ffff;
8
+ const SEQUENCE_LOCKTIME_TYPE_FLAG = (1 << 22);
9
+ const BLOCKS_MAX = SEQUENCE_LOCKTIME_MASK;
10
+ const SECONDS_MOD = 1 << SEQUENCE_LOCKTIME_GRANULARITY;
11
+ const SECONDS_MAX = SEQUENCE_LOCKTIME_MASK << SEQUENCE_LOCKTIME_GRANULARITY;
12
+ export function decodeBip68(sequence) {
13
+ // If the disable flag is set, we return an empty object
14
+ if (sequence & SEQUENCE_LOCKTIME_DISABLE_FLAG)
15
+ return {};
16
+ // If the SEQUENCE_LOCKTIME_TYPE_FLAG is set, that means that the sequence is in seconds
17
+ if (sequence & SEQUENCE_LOCKTIME_TYPE_FLAG) {
18
+ // If the sequence is in seconds, we need to shift it by the granularity
19
+ // (because every "unit" of time corresponds to 512 seconds)
20
+ const seconds = (sequence & SEQUENCE_LOCKTIME_MASK) << SEQUENCE_LOCKTIME_GRANULARITY;
21
+ return { seconds };
22
+ }
23
+ // If the disable flag is not set, and the SEQUENCE_LOCKTIME_TYPE_FLAG is not set, the sequence is in blocks
24
+ const blocks = sequence & SEQUENCE_LOCKTIME_MASK;
25
+ return { blocks };
26
+ }
27
+ export function encodeBip68({ blocks, seconds }) {
28
+ if (blocks !== undefined && seconds !== undefined)
29
+ throw new TypeError('Cannot encode blocks AND seconds');
30
+ // If the input is correct, we encode it as a sequence in seconds (using the SEQUENCE_LOCKTIME_TYPE_FLAG)
31
+ if (seconds !== undefined) {
32
+ if (!Number.isFinite(seconds))
33
+ throw new TypeError('Expected Number seconds');
34
+ if (seconds > SECONDS_MAX)
35
+ throw new TypeError('Expected Number seconds <= ' + SECONDS_MAX);
36
+ if (seconds % SECONDS_MOD !== 0)
37
+ throw new TypeError('Expected Number seconds as a multiple of ' + SECONDS_MOD);
38
+ return SEQUENCE_LOCKTIME_TYPE_FLAG | (seconds >> SEQUENCE_LOCKTIME_GRANULARITY);
39
+ }
40
+ // If the input is correct, we return the blocks (no further encoding needed)
41
+ if (blocks !== undefined) {
42
+ if (!Number.isFinite(blocks))
43
+ throw new TypeError('Expected Number blocks');
44
+ if (blocks > SEQUENCE_LOCKTIME_MASK)
45
+ throw new TypeError('Expected Number blocks <= ' + BLOCKS_MAX);
46
+ return blocks;
47
+ }
48
+ // If neither blocks nor seconds are provided, we assume the sequence is final
49
+ return SEQUENCE_FINAL;
50
+ }
51
+ //# sourceMappingURL=bip68.js.map
package/dist/hash.js CHANGED
@@ -1,13 +1,12 @@
1
- // TODO: Replace with libauth
2
- import hash from 'hash.js';
1
+ import { sha256 as sha256Lib, ripemd160 as ripemd160Lib, sha512 as sha512Lib } from '@bitauth/libauth';
3
2
  export function sha512(payload) {
4
- return Uint8Array.from(hash.sha512().update(payload).digest());
3
+ return sha512Lib.hash(payload);
5
4
  }
6
5
  export function sha256(payload) {
7
- return Uint8Array.from(hash.sha256().update(payload).digest());
6
+ return sha256Lib.hash(payload);
8
7
  }
9
8
  export function ripemd160(payload) {
10
- return Uint8Array.from(hash.ripemd160().update(payload).digest());
9
+ return ripemd160Lib.hash(payload);
11
10
  }
12
11
  export function hash160(payload) {
13
12
  return ripemd160(sha256(payload));
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  export * from './artifact.js';
2
+ export * from './bip68.js';
3
+ export * from './bitauth-script.js';
2
4
  export * from './data.js';
3
5
  export * from './hash.js';
4
6
  export * from './script.js';
5
- export * from './types.js';
6
7
  export * from './source-map.js';
7
- export * from './bitauth-script.js';
8
+ export * from './types.js';
package/dist/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  export * from './artifact.js';
2
+ export * from './bip68.js';
3
+ export * from './bitauth-script.js';
2
4
  export * from './data.js';
3
5
  export * from './hash.js';
4
6
  export * from './script.js';
5
- export * from './types.js';
6
7
  export * from './source-map.js';
7
- export * from './bitauth-script.js';
8
+ export * from './types.js';
8
9
  //# sourceMappingURL=index.js.map
package/dist/script.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export declare const Op: typeof import("@bitauth/libauth").OpcodesBCH2023;
1
+ import { OpcodesBch2023 } from '@bitauth/libauth';
2
+ export declare const Op: typeof OpcodesBch2023;
2
3
  export type Op = number;
3
4
  export type OpOrData = Op | Uint8Array;
4
5
  export type Script = OpOrData[];
package/dist/script.js CHANGED
@@ -1,7 +1,7 @@
1
- import { OpcodesBCH, encodeDataPush, hexToBin, disassembleBytecodeBCH, flattenBinArray, encodeAuthenticationInstructions, decodeAuthenticationInstructions, } from '@bitauth/libauth';
1
+ import { OpcodesBch2023, encodeDataPush, hexToBin, disassembleBytecodeBch, flattenBinArray, encodeAuthenticationInstructions, decodeAuthenticationInstructions, } from '@bitauth/libauth';
2
2
  import { decodeInt, encodeInt } from './data.js';
3
3
  import OptimisationsEquivFile from './cashproof-optimisations.js';
4
- export const Op = OpcodesBCH;
4
+ export const Op = OpcodesBch2023;
5
5
  export function scriptToAsm(script) {
6
6
  return bytecodeToAsm(scriptToBytecode(script));
7
7
  }
@@ -49,7 +49,7 @@ export function asmToBytecode(asm) {
49
49
  }
50
50
  export function bytecodeToAsm(bytecode) {
51
51
  // Convert the bytecode to libauth's ASM format
52
- let asm = disassembleBytecodeBCH(bytecode);
52
+ let asm = disassembleBytecodeBch(bytecode);
53
53
  // COnvert libauth's ASM format to BITBOX's
54
54
  asm = asm.replace(/OP_PUSHBYTES_[^\s]+/g, '');
55
55
  asm = asm.replace(/OP_PUSHDATA[^\s]+ [^\s]+/g, '');
@@ -61,7 +61,7 @@ export function bytecodeToAsm(bytecode) {
61
61
  // TODO: If we decide to change the ASM / artifact format, we can remove this function and merge it with bytecodeToAsm
62
62
  export function bytecodeToBitAuthAsm(bytecode) {
63
63
  // Convert the bytecode to libauth's ASM format
64
- let asm = disassembleBytecodeBCH(bytecode);
64
+ let asm = disassembleBytecodeBch(bytecode);
65
65
  // COnvert libauth's ASM format to BitAuth Script ASM
66
66
  asm = asm.replace(/OP_PUSHBYTES_[^\s]+/g, '');
67
67
  asm = asm.replace(/OP_PUSHDATA[^\s]+ [^\s]+/g, '');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cashscript/utils",
3
- "version": "0.10.1",
3
+ "version": "0.11.0-next.0",
4
4
  "description": "CashScript utilities and types",
5
5
  "keywords": [
6
6
  "bitcoin cash",
@@ -40,8 +40,7 @@
40
40
  "test": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest"
41
41
  },
42
42
  "dependencies": {
43
- "@bitauth/libauth": "^3.0.0",
44
- "hash.js": "^1.1.7"
43
+ "@bitauth/libauth": "^3.1.0-next.2"
45
44
  },
46
45
  "devDependencies": {
47
46
  "@jest/globals": "^29.4.1",
@@ -49,5 +48,5 @@
49
48
  "jest": "^29.4.1",
50
49
  "typescript": "^5.5.4"
51
50
  },
52
- "gitHead": "0dd95d5b05a438ac1884bdb2252aec6a4c77e5b2"
51
+ "gitHead": "18df35aa3276e64fa64e92386106374d1d35b28d"
53
52
  }