@cashscript/utils 0.10.1 → 0.10.3
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/artifact.d.ts +5 -4
- package/dist/artifact.js +35 -2
- package/dist/bip68.d.ts +7 -0
- package/dist/bip68.js +51 -0
- package/dist/hash.js +4 -5
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/package.json +3 -4
package/dist/artifact.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface AbiInput {
|
|
|
6
6
|
export interface AbiFunction {
|
|
7
7
|
name: string;
|
|
8
8
|
covenant?: boolean;
|
|
9
|
-
inputs: AbiInput[];
|
|
9
|
+
inputs: readonly AbiInput[];
|
|
10
10
|
}
|
|
11
11
|
export interface DebugInformation {
|
|
12
12
|
bytecode: string;
|
|
@@ -32,8 +32,8 @@ export interface RequireStatement {
|
|
|
32
32
|
}
|
|
33
33
|
export interface Artifact {
|
|
34
34
|
contractName: string;
|
|
35
|
-
constructorInputs: AbiInput[];
|
|
36
|
-
abi: AbiFunction[];
|
|
35
|
+
constructorInputs: readonly AbiInput[];
|
|
36
|
+
abi: readonly AbiFunction[];
|
|
37
37
|
bytecode: string;
|
|
38
38
|
source: string;
|
|
39
39
|
debug?: DebugInformation;
|
|
@@ -44,4 +44,5 @@ export interface Artifact {
|
|
|
44
44
|
updatedAt: string;
|
|
45
45
|
}
|
|
46
46
|
export declare function importArtifact(artifactFile: PathLike): Artifact;
|
|
47
|
-
export declare function exportArtifact(artifact: Artifact, targetFile: string): void;
|
|
47
|
+
export declare function exportArtifact(artifact: Artifact, targetFile: string, format: 'json' | 'ts'): void;
|
|
48
|
+
export declare function formatArtifact(artifact: Artifact, format: 'json' | 'ts'): string;
|
package/dist/artifact.js
CHANGED
|
@@ -2,8 +2,41 @@ import fs from 'fs';
|
|
|
2
2
|
export function importArtifact(artifactFile) {
|
|
3
3
|
return JSON.parse(fs.readFileSync(artifactFile, { encoding: 'utf-8' }));
|
|
4
4
|
}
|
|
5
|
-
export function exportArtifact(artifact, targetFile) {
|
|
6
|
-
const jsonString =
|
|
5
|
+
export function exportArtifact(artifact, targetFile, format) {
|
|
6
|
+
const jsonString = formatArtifact(artifact, format);
|
|
7
7
|
fs.writeFileSync(targetFile, jsonString);
|
|
8
8
|
}
|
|
9
|
+
export function formatArtifact(artifact, format) {
|
|
10
|
+
if (format === 'ts') {
|
|
11
|
+
return `export default ${stringifyAsTs(artifact)} as const;\n`;
|
|
12
|
+
}
|
|
13
|
+
return JSON.stringify(artifact, null, 2);
|
|
14
|
+
}
|
|
15
|
+
const indent = (level) => ' '.repeat(level);
|
|
16
|
+
function stringifyAsTs(obj, indentationLevel = 1) {
|
|
17
|
+
// For strings, we use JSON.stringify, but we convert double quotes to single quotes
|
|
18
|
+
if (typeof obj === 'string') {
|
|
19
|
+
return JSON.stringify(obj).replace(/'/g, "\\'").replace(/"/g, "'");
|
|
20
|
+
}
|
|
21
|
+
// Numbers and booleans are just converted to strings
|
|
22
|
+
if (typeof obj === 'number' || typeof obj === 'boolean') {
|
|
23
|
+
return JSON.stringify(obj);
|
|
24
|
+
}
|
|
25
|
+
// Arrays are recursively formatted with indentation
|
|
26
|
+
if (Array.isArray(obj)) {
|
|
27
|
+
if (obj.length === 0)
|
|
28
|
+
return '[]';
|
|
29
|
+
const formattedItems = obj.map((item) => `${indent(indentationLevel)}${stringifyAsTs(item, indentationLevel + 1)}`);
|
|
30
|
+
return `[\n${formattedItems.join(',\n')},\n${indent(indentationLevel - 1)}]`;
|
|
31
|
+
}
|
|
32
|
+
// Objects are recursively formatted with indentation
|
|
33
|
+
if (typeof obj === 'object') {
|
|
34
|
+
const entries = Object.entries(obj);
|
|
35
|
+
if (entries.length === 0)
|
|
36
|
+
return '{}';
|
|
37
|
+
const formattedEntries = entries.map(([key, value]) => (`${indent(indentationLevel)}${key}: ${stringifyAsTs(value, indentationLevel + 1)}`));
|
|
38
|
+
return `{\n${formattedEntries.join(',\n')},\n${indent(indentationLevel - 1)}}`;
|
|
39
|
+
}
|
|
40
|
+
throw new Error(`Unsupported type: ${typeof obj}`);
|
|
41
|
+
}
|
|
9
42
|
//# sourceMappingURL=artifact.js.map
|
package/dist/bip68.d.ts
ADDED
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
|
-
|
|
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
|
|
3
|
+
return sha512Lib.hash(payload);
|
|
5
4
|
}
|
|
6
5
|
export function sha256(payload) {
|
|
7
|
-
return
|
|
6
|
+
return sha256Lib.hash(payload);
|
|
8
7
|
}
|
|
9
8
|
export function ripemd160(payload) {
|
|
10
|
-
return
|
|
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 './
|
|
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 './
|
|
8
|
+
export * from './types.js';
|
|
8
9
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cashscript/utils",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.3",
|
|
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.0.0"
|
|
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": "
|
|
51
|
+
"gitHead": "1aa7c6b26ee8add3ff8cf42227141d9bea8aba66"
|
|
53
52
|
}
|