@ckb-ccc/core 0.0.2-alpha.0 → 0.0.2-alpha.1
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/address/address.advanced.d.ts +5 -0
- package/dist/address/address.advanced.d.ts.map +1 -0
- package/dist/address/address.advanced.js +68 -0
- package/dist/address/advanced.d.ts +7 -9
- package/dist/address/advanced.d.ts.map +1 -1
- package/dist/address/advanced.js +50 -38
- package/dist/address/index.d.ts +20 -5
- package/dist/address/index.d.ts.map +1 -1
- package/dist/address/index.js +43 -32
- package/dist/advanced.d.ts +3 -0
- package/dist/advanced.d.ts.map +1 -0
- package/dist/{types/index.js → advanced.js} +3 -9
- package/dist/advancedBarrel.d.ts +4 -0
- package/dist/advancedBarrel.d.ts.map +1 -0
- package/dist/advancedBarrel.js +19 -0
- package/dist/barrel.d.ts +3 -2
- package/dist/barrel.d.ts.map +1 -1
- package/dist/barrel.js +3 -2
- package/dist/bytes.d.ts +15 -0
- package/dist/bytes.d.ts.map +1 -0
- package/dist/bytes.js +114 -0
- package/dist/ckb/advanced.d.ts +3 -0
- package/dist/ckb/advanced.d.ts.map +1 -0
- package/dist/ckb/advanced.js +18 -0
- package/dist/ckb/hasher.advanced.d.ts +2 -0
- package/dist/ckb/hasher.advanced.d.ts.map +1 -0
- package/dist/ckb/hasher.advanced.js +4 -0
- package/dist/ckb/hasher.d.ts +10 -0
- package/dist/ckb/hasher.d.ts.map +1 -0
- package/dist/ckb/hasher.js +26 -0
- package/dist/ckb/index.d.ts +3 -1
- package/dist/ckb/index.d.ts.map +1 -1
- package/dist/ckb/index.js +3 -1
- package/dist/ckb/types/advanced.d.ts +4 -0
- package/dist/ckb/types/advanced.d.ts.map +1 -0
- package/dist/ckb/types/advanced.js +32 -0
- package/dist/ckb/types/index.d.ts +3 -0
- package/dist/ckb/types/index.d.ts.map +1 -0
- package/dist/{codec → ckb/types}/index.js +1 -2
- package/dist/ckb/types/molecule.advanced/generated.d.ts +355 -0
- package/dist/ckb/types/molecule.advanced/generated.d.ts.map +1 -0
- package/dist/ckb/types/molecule.advanced/generated.js +1255 -0
- package/dist/ckb/types/molecule.advanced/index.d.ts +2 -0
- package/dist/ckb/types/molecule.advanced/index.d.ts.map +1 -0
- package/dist/ckb/types/molecule.advanced/index.js +17 -0
- package/dist/ckb/types/script.advanced.d.ts +4 -0
- package/dist/ckb/types/script.advanced.d.ts.map +1 -0
- package/dist/{types/ckb.advanced.js → ckb/types/script.advanced.js} +3 -3
- package/dist/ckb/types/script.d.ts +26 -0
- package/dist/ckb/types/script.d.ts.map +1 -0
- package/dist/ckb/types/script.js +75 -0
- package/dist/ckb/types/transaction.advanced.d.ts +4 -0
- package/dist/ckb/types/transaction.advanced.d.ts.map +1 -0
- package/dist/ckb/types/transaction.advanced.js +11 -0
- package/dist/ckb/types/transaction.d.ts +127 -0
- package/dist/ckb/types/transaction.d.ts.map +1 -0
- package/dist/ckb/types/transaction.js +266 -0
- package/dist/ckb/viewerCkbScript.d.ts +11 -0
- package/dist/ckb/viewerCkbScript.d.ts.map +1 -0
- package/dist/ckb/viewerCkbScript.js +18 -0
- package/dist/client/advanced.d.ts +3 -0
- package/dist/client/advanced.d.ts.map +1 -0
- package/dist/client/advanced.js +18 -0
- package/dist/client/client.d.ts +7 -4
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +1 -4
- package/dist/client/clientPublicMainnet.advanced.d.ts +1 -1
- package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.d.ts +5 -3
- package/dist/client/clientPublicMainnet.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.js +6 -3
- package/dist/client/clientPublicTestnet.advanced.d.ts +1 -1
- package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.d.ts +5 -3
- package/dist/client/clientPublicTestnet.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.js +6 -3
- package/dist/client/clientTypes.d.ts +2 -0
- package/dist/client/clientTypes.d.ts.map +1 -0
- package/dist/client/index.d.ts +1 -4
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +1 -19
- package/dist/client/jsonRpc/advanced.d.ts +88 -0
- package/dist/client/jsonRpc/advanced.d.ts.map +1 -0
- package/dist/client/jsonRpc/advanced.js +69 -0
- package/dist/client/jsonRpc/index.d.ts +14 -0
- package/dist/client/jsonRpc/index.d.ts.map +1 -0
- package/dist/client/jsonRpc/index.js +63 -0
- package/dist/fixedPoint.d.ts +9 -0
- package/dist/fixedPoint.d.ts.map +1 -0
- package/dist/fixedPoint.js +30 -0
- package/dist/primitive.d.ts +5 -0
- package/dist/primitive.d.ts.map +1 -0
- package/dist/signer/helpers.d.ts +7 -0
- package/dist/signer/helpers.d.ts.map +1 -0
- package/dist/signer/helpers.js +35 -0
- package/dist/signer/index.d.ts +3 -0
- package/dist/signer/index.d.ts.map +1 -0
- package/dist/signer/index.js +18 -0
- package/dist/signer/signer.d.ts +11 -0
- package/dist/signer/signer.d.ts.map +1 -0
- package/dist/signer/signer.js +13 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/viewer.d.ts +8 -5
- package/dist/viewer.d.ts.map +1 -1
- package/dist/viewer.js +13 -0
- package/package.json +13 -5
- package/src/address/address.advanced.ts +88 -0
- package/src/address/advanced.ts +30 -45
- package/src/address/index.ts +70 -5
- package/src/advanced.ts +2 -0
- package/src/advancedBarrel.ts +3 -0
- package/src/barrel.ts +3 -2
- package/src/bytes.ts +127 -0
- package/src/ckb/advanced.ts +2 -0
- package/src/ckb/hasher.advanced.ts +1 -0
- package/src/ckb/hasher.ts +30 -0
- package/src/ckb/index.ts +3 -1
- package/src/ckb/types/advanced.ts +3 -0
- package/src/ckb/types/index.ts +2 -0
- package/src/ckb/types/molecule.advanced/blockchain.mol +118 -0
- package/src/ckb/types/molecule.advanced/generated.d.ts +433 -0
- package/src/ckb/types/molecule.advanced/generated.js +1376 -0
- package/src/ckb/types/molecule.advanced/index.ts +1 -0
- package/src/ckb/types/script.advanced.ts +14 -0
- package/src/ckb/types/script.ts +70 -0
- package/src/ckb/types/transaction.advanced.ts +10 -0
- package/src/ckb/types/transaction.ts +366 -0
- package/src/ckb/viewerCkbScript.ts +21 -0
- package/src/client/advanced.ts +2 -0
- package/src/client/client.ts +11 -4
- package/src/client/clientPublicMainnet.advanced.ts +1 -1
- package/src/client/clientPublicMainnet.ts +9 -4
- package/src/client/clientPublicTestnet.advanced.ts +1 -1
- package/src/client/clientPublicTestnet.ts +9 -4
- package/src/client/clientTypes.ts +1 -0
- package/src/client/index.ts +1 -7
- package/src/client/jsonRpc/advanced.ts +93 -0
- package/src/client/jsonRpc/index.ts +83 -0
- package/src/fixedPoint.ts +53 -0
- package/src/primitive.ts +8 -0
- package/src/signer/helpers.ts +43 -0
- package/src/signer/index.ts +2 -0
- package/src/signer/signer.ts +19 -0
- package/src/viewer.ts +16 -8
- package/dist/address/address.d.ts +0 -8
- package/dist/address/address.d.ts.map +0 -1
- package/dist/address/address.js +0 -6
- package/dist/address/addressKnownScript.d.ts +0 -13
- package/dist/address/addressKnownScript.d.ts.map +0 -1
- package/dist/address/addressKnownScript.js +0 -27
- package/dist/address/addressPlain.d.ts +0 -12
- package/dist/address/addressPlain.d.ts.map +0 -1
- package/dist/address/addressPlain.js +0 -23
- package/dist/address/converter.d.ts +0 -8
- package/dist/address/converter.d.ts.map +0 -1
- package/dist/address/converter.js +0 -65
- package/dist/bundle.d.ts +0 -9
- package/dist/bundle.d.ts.map +0 -1
- package/dist/bundle.js +0 -25
- package/dist/ckb/viewerCkbAddress.d.ts +0 -14
- package/dist/ckb/viewerCkbAddress.d.ts.map +0 -1
- package/dist/ckb/viewerCkbAddress.js +0 -28
- package/dist/codec/bytes.d.ts +0 -4
- package/dist/codec/bytes.d.ts.map +0 -1
- package/dist/codec/bytes.js +0 -29
- package/dist/codec/hashType.d.ts +0 -4
- package/dist/codec/hashType.d.ts.map +0 -1
- package/dist/codec/hashType.js +0 -13
- package/dist/codec/index.d.ts +0 -4
- package/dist/codec/index.d.ts.map +0 -1
- package/dist/codec/script.d.ts +0 -4
- package/dist/codec/script.d.ts.map +0 -1
- package/dist/codec/script.js +0 -22
- package/dist/signer.d.ts +0 -9
- package/dist/signer.d.ts.map +0 -1
- package/dist/signer.js +0 -12
- package/dist/types/cell.d.ts +0 -5
- package/dist/types/cell.d.ts.map +0 -1
- package/dist/types/cell.js +0 -8
- package/dist/types/ckb.advanced.d.ts +0 -4
- package/dist/types/ckb.advanced.d.ts.map +0 -1
- package/dist/types/ckb.d.ts +0 -16
- package/dist/types/ckb.d.ts.map +0 -1
- package/dist/types/ckb.js +0 -36
- package/dist/types/index.d.ts +0 -9
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/lumos.d.ts +0 -90
- package/dist/types/lumos.d.ts.map +0 -1
- package/dist/types/primitive.d.ts +0 -3
- package/dist/types/primitive.d.ts.map +0 -1
- package/src/address/address.ts +0 -9
- package/src/address/addressKnownScript.ts +0 -31
- package/src/address/addressPlain.ts +0 -27
- package/src/address/converter.ts +0 -93
- package/src/ckb/viewerCkbAddress.ts +0 -34
- package/src/codec/bytes.ts +0 -30
- package/src/codec/hashType.ts +0 -10
- package/src/codec/index.ts +0 -3
- package/src/codec/script.ts +0 -21
- package/src/signer.ts +0 -20
- package/src/types/cell.ts +0 -5
- package/src/types/ckb.advanced.ts +0 -14
- package/src/types/ckb.ts +0 -47
- package/src/types/index.ts +0 -9
- package/src/types/lumos.ts +0 -101
- package/src/types/primitive.ts +0 -2
- /package/dist/{types/lumos.js → client/clientTypes.js} +0 -0
- /package/dist/{types/primitive.js → primitive.js} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./generated";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type HashType } from "./script";
|
|
2
|
+
|
|
3
|
+
export const HASH_TYPE_TO_NUM: Record<HashType, number> = {
|
|
4
|
+
type: 0x01,
|
|
5
|
+
data: 0x00,
|
|
6
|
+
data1: 0x02,
|
|
7
|
+
data2: 0x04,
|
|
8
|
+
};
|
|
9
|
+
export const NUM_TO_HASH_TYPE: Record<number, HashType> = {
|
|
10
|
+
0x01: "type",
|
|
11
|
+
0x00: "data",
|
|
12
|
+
0x02: "data1",
|
|
13
|
+
0x04: "data2",
|
|
14
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { DeepReadonly } from "ts-essentials";
|
|
2
|
+
import { toBytes, toHex } from "../../bytes";
|
|
3
|
+
import { BytesLike, Hex } from "../../primitive";
|
|
4
|
+
import { HASH_TYPE_TO_NUM, NUM_TO_HASH_TYPE } from "./script.advanced";
|
|
5
|
+
import * as mol from "./molecule.advanced";
|
|
6
|
+
|
|
7
|
+
export type HashType = "type" | "data" | "data1" | "data2";
|
|
8
|
+
|
|
9
|
+
export function encodeHashType(hashType: HashType): Uint8Array {
|
|
10
|
+
return toBytes([HASH_TYPE_TO_NUM[hashType]]);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function decodeHashType(bytes: BytesLike): HashType {
|
|
14
|
+
return NUM_TO_HASH_TYPE[toBytes(bytes)[0]];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class Script {
|
|
18
|
+
constructor(
|
|
19
|
+
public codeHash: Hex,
|
|
20
|
+
public hashType: HashType,
|
|
21
|
+
public args: Hex,
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
static from({
|
|
25
|
+
codeHash,
|
|
26
|
+
hashType,
|
|
27
|
+
args,
|
|
28
|
+
}: {
|
|
29
|
+
codeHash: BytesLike;
|
|
30
|
+
hashType: string;
|
|
31
|
+
args: BytesLike;
|
|
32
|
+
}): Script {
|
|
33
|
+
return {
|
|
34
|
+
codeHash: toHex(codeHash),
|
|
35
|
+
hashType: hashType as HashType,
|
|
36
|
+
args: toHex(args),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static _toMolData(script: DeepReadonly<Script>) {
|
|
41
|
+
return {
|
|
42
|
+
codeHash: toBytes(script.codeHash),
|
|
43
|
+
hashType: encodeHashType(script.hashType),
|
|
44
|
+
args: toBytes(script.args),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static encode(script: DeepReadonly<Script>): Uint8Array {
|
|
49
|
+
return toBytes(mol.SerializeScript(Script._toMolData(script)));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static decode(bytes: BytesLike | mol.Script): Script {
|
|
53
|
+
const view =
|
|
54
|
+
bytes instanceof mol.Script ? bytes : new mol.Script(toBytes(bytes));
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
codeHash: toHex(view.getCodeHash().raw()),
|
|
58
|
+
hashType: decodeHashType([view.getHashType()]),
|
|
59
|
+
args: toHex(view.getArgs().raw()),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static eq(a: Script, b: Script): boolean {
|
|
64
|
+
return (
|
|
65
|
+
a.codeHash === b.codeHash &&
|
|
66
|
+
a.args === b.args &&
|
|
67
|
+
a.hashType === b.hashType
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { HashType, Script } from "./script";
|
|
2
|
+
import {
|
|
3
|
+
toBytes,
|
|
4
|
+
toBytesFromNumber,
|
|
5
|
+
toHex,
|
|
6
|
+
toHexFromHex,
|
|
7
|
+
toNumber,
|
|
8
|
+
toNumberFromBytes,
|
|
9
|
+
} from "../../bytes";
|
|
10
|
+
import { BytesLike, Hex, NumberLike } from "../../primitive";
|
|
11
|
+
import type { TransactionSkeletonType } from "@ckb-lumos/helpers";
|
|
12
|
+
import { DeepReadonly } from "ts-essentials";
|
|
13
|
+
import * as mol from "./molecule.advanced";
|
|
14
|
+
import { DEP_TYPE_TO_NUM, NUM_TO_DEP_TYPE } from "./transaction.advanced";
|
|
15
|
+
import { Hasher, ckbHash } from "../hasher";
|
|
16
|
+
|
|
17
|
+
export type DepType = "depGroup" | "code";
|
|
18
|
+
|
|
19
|
+
export function encodeDepType(depType: DepType): Uint8Array {
|
|
20
|
+
return toBytes([DEP_TYPE_TO_NUM[depType]]);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function decodeDepType(bytes: BytesLike): DepType {
|
|
24
|
+
return NUM_TO_DEP_TYPE[toBytes(bytes)[0]];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class OutPoint {
|
|
28
|
+
constructor(
|
|
29
|
+
public txHash: Hex,
|
|
30
|
+
public index: string,
|
|
31
|
+
) {}
|
|
32
|
+
|
|
33
|
+
static from({
|
|
34
|
+
txHash,
|
|
35
|
+
index,
|
|
36
|
+
}: {
|
|
37
|
+
txHash: BytesLike;
|
|
38
|
+
index: NumberLike;
|
|
39
|
+
}): OutPoint {
|
|
40
|
+
return new OutPoint(toHex(txHash), toNumber(index));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static _toMolData(outPoint: DeepReadonly<OutPoint>) {
|
|
44
|
+
return {
|
|
45
|
+
txHash: toBytes(outPoint.txHash),
|
|
46
|
+
index: toBytesFromNumber(outPoint.index, 4),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static encode(outPoint: DeepReadonly<OutPoint>): Uint8Array {
|
|
51
|
+
return toBytes(mol.SerializeOutPoint(OutPoint._toMolData(outPoint)));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static decode(bytes: BytesLike | mol.OutPoint): OutPoint {
|
|
55
|
+
const view =
|
|
56
|
+
bytes instanceof mol.OutPoint ? bytes : new mol.OutPoint(toBytes(bytes));
|
|
57
|
+
return {
|
|
58
|
+
txHash: toHex(view.getTxHash().raw()),
|
|
59
|
+
index: toNumberFromBytes(view.getIndex().raw()),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export class CellOutput {
|
|
65
|
+
constructor(
|
|
66
|
+
public capacity: string,
|
|
67
|
+
public lock: Script,
|
|
68
|
+
public type?: Script,
|
|
69
|
+
) {}
|
|
70
|
+
|
|
71
|
+
static from({
|
|
72
|
+
capacity,
|
|
73
|
+
lock,
|
|
74
|
+
type,
|
|
75
|
+
}: {
|
|
76
|
+
capacity: NumberLike;
|
|
77
|
+
lock: Parameters<typeof Script.from>[0];
|
|
78
|
+
type?: Parameters<typeof Script.from>[0];
|
|
79
|
+
}): CellOutput {
|
|
80
|
+
return new CellOutput(
|
|
81
|
+
toNumber(capacity),
|
|
82
|
+
Script.from(lock),
|
|
83
|
+
type ? Script.from(type) : undefined,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static _toMolData(cellOutput: DeepReadonly<CellOutput>) {
|
|
88
|
+
return {
|
|
89
|
+
capacity: toBytesFromNumber(cellOutput.capacity, 8),
|
|
90
|
+
lock: Script._toMolData(cellOutput.lock),
|
|
91
|
+
type: cellOutput.type ? Script._toMolData(cellOutput.type) : undefined,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
static encode(cellOutput: DeepReadonly<CellOutput>): Uint8Array {
|
|
96
|
+
return toBytes(mol.SerializeCellOutput(CellOutput._toMolData(cellOutput)));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
static decode(bytes: BytesLike | mol.CellOutput): CellOutput {
|
|
100
|
+
const view =
|
|
101
|
+
bytes instanceof mol.CellOutput
|
|
102
|
+
? bytes
|
|
103
|
+
: new mol.CellOutput(toBytes(bytes));
|
|
104
|
+
const type = view.getType();
|
|
105
|
+
return {
|
|
106
|
+
capacity: toNumberFromBytes(view.getCapacity().raw()),
|
|
107
|
+
lock: Script.decode(view.getLock()),
|
|
108
|
+
type: type.hasValue() ? Script.decode(type.value()) : undefined,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export class CellInput {
|
|
114
|
+
constructor(
|
|
115
|
+
public previousOutput: OutPoint,
|
|
116
|
+
public since: string,
|
|
117
|
+
public cellOutput?: CellOutput,
|
|
118
|
+
public outputData?: Hex,
|
|
119
|
+
) {}
|
|
120
|
+
|
|
121
|
+
static from({
|
|
122
|
+
previousOutput,
|
|
123
|
+
since,
|
|
124
|
+
cellOutput,
|
|
125
|
+
outputData,
|
|
126
|
+
}: {
|
|
127
|
+
previousOutput: Parameters<typeof OutPoint.from>[0];
|
|
128
|
+
since: NumberLike;
|
|
129
|
+
cellOutput?: Parameters<typeof CellOutput.from>[0];
|
|
130
|
+
outputData?: BytesLike;
|
|
131
|
+
}): CellInput {
|
|
132
|
+
return new CellInput(
|
|
133
|
+
OutPoint.from(previousOutput),
|
|
134
|
+
toNumber(since),
|
|
135
|
+
cellOutput ? CellOutput.from(cellOutput) : undefined,
|
|
136
|
+
outputData ? toHex(outputData) : undefined,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
static _toMolData(cellInput: DeepReadonly<CellInput>) {
|
|
141
|
+
return {
|
|
142
|
+
previousOutput: OutPoint._toMolData(cellInput.previousOutput),
|
|
143
|
+
since: toBytesFromNumber(cellInput.since, 8),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
static encode(cellInput: DeepReadonly<CellInput>): Uint8Array {
|
|
148
|
+
return toBytes(mol.SerializeCellInput(CellInput._toMolData(cellInput)));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static decode(bytes: BytesLike | mol.CellInput): CellInput {
|
|
152
|
+
const view =
|
|
153
|
+
bytes instanceof mol.CellInput
|
|
154
|
+
? bytes
|
|
155
|
+
: new mol.CellInput(toBytes(bytes));
|
|
156
|
+
return {
|
|
157
|
+
previousOutput: OutPoint.decode(view.getPreviousOutput()),
|
|
158
|
+
since: toNumberFromBytes(view.getSince().raw()),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export class CellDep {
|
|
164
|
+
constructor(
|
|
165
|
+
public outPoint: OutPoint,
|
|
166
|
+
public depType: DepType,
|
|
167
|
+
) {}
|
|
168
|
+
|
|
169
|
+
static from({
|
|
170
|
+
outPoint,
|
|
171
|
+
depType,
|
|
172
|
+
}: {
|
|
173
|
+
outPoint: Parameters<typeof OutPoint.from>[0];
|
|
174
|
+
depType: DepType;
|
|
175
|
+
}): CellDep {
|
|
176
|
+
return new CellDep(OutPoint.from(outPoint), depType);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
static _toMolData(cellDep: DeepReadonly<CellDep>) {
|
|
180
|
+
return {
|
|
181
|
+
outPoint: OutPoint._toMolData(cellDep.outPoint),
|
|
182
|
+
depType: encodeDepType(cellDep.depType),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
static encode(cellDep: DeepReadonly<CellDep>): Uint8Array {
|
|
187
|
+
return toBytes(mol.SerializeCellDep(CellDep._toMolData(cellDep)));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
static decode(bytes: BytesLike | mol.CellDep): CellDep {
|
|
191
|
+
const view =
|
|
192
|
+
bytes instanceof mol.CellDep ? bytes : new mol.CellDep(toBytes(bytes));
|
|
193
|
+
return {
|
|
194
|
+
outPoint: OutPoint.decode(view.getOutPoint()),
|
|
195
|
+
depType: decodeDepType([view.getDepType()]),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export class WitnessArgs {
|
|
201
|
+
constructor(
|
|
202
|
+
public lock?: Hex,
|
|
203
|
+
public inputType?: Hex,
|
|
204
|
+
public outputType?: Hex,
|
|
205
|
+
) {}
|
|
206
|
+
|
|
207
|
+
static from({
|
|
208
|
+
lock,
|
|
209
|
+
inputType,
|
|
210
|
+
outputType,
|
|
211
|
+
}: {
|
|
212
|
+
lock?: BytesLike;
|
|
213
|
+
inputType?: BytesLike;
|
|
214
|
+
outputType?: BytesLike;
|
|
215
|
+
}): WitnessArgs {
|
|
216
|
+
return new WitnessArgs(
|
|
217
|
+
lock ? toHex(lock) : undefined,
|
|
218
|
+
inputType ? toHex(inputType) : undefined,
|
|
219
|
+
outputType ? toHex(outputType) : undefined,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
static _toMolData(witnessArgs: DeepReadonly<WitnessArgs>) {
|
|
224
|
+
return {
|
|
225
|
+
lock: witnessArgs.lock ? toBytes(witnessArgs.lock) : undefined,
|
|
226
|
+
inputType: witnessArgs.inputType
|
|
227
|
+
? toBytes(witnessArgs.inputType)
|
|
228
|
+
: undefined,
|
|
229
|
+
outputType: witnessArgs.outputType
|
|
230
|
+
? toBytes(witnessArgs.outputType)
|
|
231
|
+
: undefined,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
static encode(witnessArgs: DeepReadonly<WitnessArgs>): Uint8Array {
|
|
236
|
+
return toBytes(
|
|
237
|
+
mol.SerializeWitnessArgs(WitnessArgs._toMolData(witnessArgs)),
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
static decode(bytes: BytesLike | mol.WitnessArgs): WitnessArgs {
|
|
242
|
+
const view =
|
|
243
|
+
bytes instanceof mol.WitnessArgs
|
|
244
|
+
? bytes
|
|
245
|
+
: new mol.WitnessArgs(toBytes(bytes));
|
|
246
|
+
const lock = view.getLock();
|
|
247
|
+
const inputType = view.getInputType();
|
|
248
|
+
const outputType = view.getOutputType();
|
|
249
|
+
return {
|
|
250
|
+
lock: lock.hasValue() ? toHex(lock.value().raw()) : undefined,
|
|
251
|
+
inputType: inputType.hasValue()
|
|
252
|
+
? toHex(inputType.value().raw())
|
|
253
|
+
: undefined,
|
|
254
|
+
outputType: outputType.hasValue()
|
|
255
|
+
? toHex(outputType.value().raw())
|
|
256
|
+
: undefined,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export class Transaction {
|
|
262
|
+
constructor(
|
|
263
|
+
public version: string,
|
|
264
|
+
public cellDeps: CellDep[],
|
|
265
|
+
public headerDeps: Hex[],
|
|
266
|
+
public inputs: CellInput[],
|
|
267
|
+
public outputs: CellOutput[],
|
|
268
|
+
public outputsData: Hex[],
|
|
269
|
+
public witnesses: Hex[],
|
|
270
|
+
) {}
|
|
271
|
+
|
|
272
|
+
static default(): Transaction {
|
|
273
|
+
return new Transaction("0", [], [], [], [], [], []);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
private static fromLumosOutPoint(outPoint: {
|
|
277
|
+
index: string;
|
|
278
|
+
txHash: string;
|
|
279
|
+
}) {
|
|
280
|
+
return {
|
|
281
|
+
...outPoint,
|
|
282
|
+
txHash: toHexFromHex(outPoint.txHash),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
private static fromLumosScript(script: {
|
|
287
|
+
codeHash: string;
|
|
288
|
+
hashType: HashType;
|
|
289
|
+
args: string;
|
|
290
|
+
}) {
|
|
291
|
+
return {
|
|
292
|
+
codeHash: toHexFromHex(script.codeHash),
|
|
293
|
+
args: toHexFromHex(script.args),
|
|
294
|
+
hashType: script.hashType,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private static fromLumosOutput(output: {
|
|
299
|
+
lock: Parameters<typeof Transaction.fromLumosScript>[0];
|
|
300
|
+
type?: Parameters<typeof Transaction.fromLumosScript>[0];
|
|
301
|
+
capacity: string;
|
|
302
|
+
}) {
|
|
303
|
+
return {
|
|
304
|
+
lock: Transaction.fromLumosScript(output.lock),
|
|
305
|
+
type: output.type ? Transaction.fromLumosScript(output.type) : undefined,
|
|
306
|
+
capacity: toNumber(output.capacity),
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
static fromLumosSkeleton(skeleton: TransactionSkeletonType): Transaction {
|
|
311
|
+
return new Transaction(
|
|
312
|
+
"0",
|
|
313
|
+
skeleton.cellDeps.toArray().map((cellDep) =>
|
|
314
|
+
CellDep.from({
|
|
315
|
+
...cellDep,
|
|
316
|
+
outPoint: Transaction.fromLumosOutPoint(cellDep.outPoint),
|
|
317
|
+
}),
|
|
318
|
+
),
|
|
319
|
+
skeleton.headerDeps.toArray().map(toHex),
|
|
320
|
+
skeleton.inputs.toArray().map((input, i) => {
|
|
321
|
+
if (!input.outPoint) {
|
|
322
|
+
throw new Error("outPoint is required in input");
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return CellInput.from({
|
|
326
|
+
previousOutput: Transaction.fromLumosOutPoint(input.outPoint),
|
|
327
|
+
since: skeleton.inputSinces.get(i, "0x00"),
|
|
328
|
+
cellOutput: Transaction.fromLumosOutput(input.cellOutput),
|
|
329
|
+
outputData: toHexFromHex(input.data),
|
|
330
|
+
});
|
|
331
|
+
}),
|
|
332
|
+
skeleton.outputs
|
|
333
|
+
.toArray()
|
|
334
|
+
.map((output) =>
|
|
335
|
+
CellOutput.from(Transaction.fromLumosOutput(output.cellOutput)),
|
|
336
|
+
),
|
|
337
|
+
skeleton.outputs.toArray().map((output) => toHex(output.data)),
|
|
338
|
+
skeleton.witnesses.toArray().map(toHex),
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
static encodeRaw(
|
|
343
|
+
transaction: Readonly<Omit<Transaction, "witnesses">>,
|
|
344
|
+
): Uint8Array {
|
|
345
|
+
return toBytes(
|
|
346
|
+
mol.SerializeRawTransaction({
|
|
347
|
+
version: toBytesFromNumber(transaction.version, 4),
|
|
348
|
+
cellDeps: transaction.cellDeps.map(CellDep._toMolData),
|
|
349
|
+
headerDeps: transaction.headerDeps.map(toBytes),
|
|
350
|
+
inputs: transaction.inputs.map(CellInput._toMolData),
|
|
351
|
+
outputs: transaction.outputs.map(CellOutput._toMolData),
|
|
352
|
+
outputsData: transaction.outputsData.map(toBytes),
|
|
353
|
+
}),
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
static hashRaw(transaction: Omit<Transaction, "witnesses">) {
|
|
358
|
+
return ckbHash(Transaction.encodeRaw(transaction));
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
static hashWitnessToHasher(witness: Hex, hasher: Hasher) {
|
|
362
|
+
const raw = toBytes(witness);
|
|
363
|
+
hasher.update(toBytesFromNumber(raw.length, 8))
|
|
364
|
+
hasher.update(raw);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Client } from "../client";
|
|
2
|
+
import { Script } from "./types";
|
|
3
|
+
import { Viewer } from "../viewer";
|
|
4
|
+
import { Address } from "../address";
|
|
5
|
+
|
|
6
|
+
export class ViewerCkbScript extends Viewer {
|
|
7
|
+
constructor(
|
|
8
|
+
private readonly script: Script,
|
|
9
|
+
client: Client,
|
|
10
|
+
) {
|
|
11
|
+
super(client);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async getInternalAddress(): Promise<string> {
|
|
15
|
+
return this.getRecommendedAddress();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async getAddressObjs(): Promise<Address[]> {
|
|
19
|
+
return [await Address.fromScript(this.script, this.client)];
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/client/client.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Hex } from "../primitive";
|
|
2
|
+
import { Script, Transaction } from "../ckb";
|
|
3
|
+
import { OutputsValidator } from "./clientTypes";
|
|
2
4
|
|
|
3
5
|
export enum KnownScript {
|
|
4
6
|
Secp256k1Blake160,
|
|
@@ -8,7 +10,12 @@ export enum KnownScript {
|
|
|
8
10
|
OmniLock,
|
|
9
11
|
}
|
|
10
12
|
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
export interface Client {
|
|
14
|
+
getAddressPrefix(): Promise<string>;
|
|
15
|
+
getKnownScript(script: KnownScript): Promise<Omit<Script, "args">>;
|
|
16
|
+
|
|
17
|
+
sendTransaction(
|
|
18
|
+
transaction: Transaction,
|
|
19
|
+
validator?: OutputsValidator,
|
|
20
|
+
): Promise<Hex>;
|
|
14
21
|
}
|
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
import { Script } from "../
|
|
2
|
-
import {
|
|
1
|
+
import { Script } from "../ckb";
|
|
2
|
+
import { KnownScript } from "./client";
|
|
3
3
|
import { MAINNET_SCRIPTS } from "./clientPublicMainnet.advanced";
|
|
4
|
+
import { ClientJsonRpc } from "./jsonRpc";
|
|
5
|
+
|
|
6
|
+
export class ClientPublicMainnet extends ClientJsonRpc {
|
|
7
|
+
constructor(timeout?: number) {
|
|
8
|
+
super("https://mainnet.ckbapp.dev/", timeout);
|
|
9
|
+
}
|
|
4
10
|
|
|
5
|
-
export class ClientPublicMainnet extends Client {
|
|
6
11
|
async getAddressPrefix(): Promise<string> {
|
|
7
12
|
return "ckb";
|
|
8
13
|
}
|
|
9
14
|
|
|
10
15
|
async getKnownScript(script: KnownScript): Promise<Omit<Script, "args">> {
|
|
11
|
-
return MAINNET_SCRIPTS[script];
|
|
16
|
+
return { ...MAINNET_SCRIPTS[script] };
|
|
12
17
|
}
|
|
13
18
|
}
|
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
import { Script } from "../
|
|
2
|
-
import {
|
|
1
|
+
import { Script } from "../ckb";
|
|
2
|
+
import { KnownScript } from "./client";
|
|
3
3
|
import { TESTNET_SCRIPTS } from "./clientPublicTestnet.advanced";
|
|
4
|
+
import { ClientJsonRpc } from "./jsonRpc";
|
|
5
|
+
|
|
6
|
+
export class ClientPublicTestnet extends ClientJsonRpc {
|
|
7
|
+
constructor(timeout?: number) {
|
|
8
|
+
super("https://testnet.ckbapp.dev/", timeout);
|
|
9
|
+
}
|
|
4
10
|
|
|
5
|
-
export class ClientPublicTestnet extends Client {
|
|
6
11
|
async getAddressPrefix(): Promise<string> {
|
|
7
12
|
return "ckt";
|
|
8
13
|
}
|
|
9
14
|
|
|
10
15
|
async getKnownScript(script: KnownScript): Promise<Omit<Script, "args">> {
|
|
11
|
-
return TESTNET_SCRIPTS[script];
|
|
16
|
+
return { ...TESTNET_SCRIPTS[script] };
|
|
12
17
|
}
|
|
13
18
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type OutputsValidator = "passthrough" | "well_known_scripts_only";
|
package/src/client/index.ts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
export * from "./client";
|
|
2
|
-
import * as clientPublicMainnetAdvanced from "./clientPublicMainnet.advanced";
|
|
3
2
|
export * from "./clientPublicMainnet";
|
|
4
|
-
import * as clientPublicTestnetAdvanced from "./clientPublicTestnet.advanced";
|
|
5
3
|
export * from "./clientPublicTestnet";
|
|
6
|
-
|
|
7
|
-
export const AdvancedClient = {
|
|
8
|
-
...clientPublicMainnetAdvanced,
|
|
9
|
-
...clientPublicTestnetAdvanced,
|
|
10
|
-
};
|
|
4
|
+
export * from "./clientTypes";
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CellDep,
|
|
3
|
+
CellInput,
|
|
4
|
+
CellOutput,
|
|
5
|
+
DepType,
|
|
6
|
+
OutPoint,
|
|
7
|
+
Script,
|
|
8
|
+
Transaction,
|
|
9
|
+
} from "../../ckb";
|
|
10
|
+
import { toHex } from "../../bytes";
|
|
11
|
+
import type { Client } from "../client";
|
|
12
|
+
|
|
13
|
+
export type JsonRpcPayload = {
|
|
14
|
+
id: number;
|
|
15
|
+
jsonrpc: "2.0";
|
|
16
|
+
method: string;
|
|
17
|
+
params: unknown[] | Record<string, unknown>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type JsonRpcMethod = {
|
|
21
|
+
method: keyof Client;
|
|
22
|
+
rpcMethod: string;
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
|
+
inTransformers: (((_: any) => unknown) | undefined)[];
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
+
outTransformer?: (_: any) => unknown;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export class JsonRpcTransformers {
|
|
30
|
+
static toDepType(depType: DepType) {
|
|
31
|
+
switch (depType) {
|
|
32
|
+
case "code":
|
|
33
|
+
return "code";
|
|
34
|
+
case "depGroup":
|
|
35
|
+
return "dep_group";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
static toScript(script?: Script) {
|
|
39
|
+
if (!script) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
code_hash: script.codeHash,
|
|
44
|
+
hash_type: script.hashType,
|
|
45
|
+
args: script.args,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
static toOutPoint(outPoint: OutPoint) {
|
|
49
|
+
return {
|
|
50
|
+
index: toHex(outPoint.index),
|
|
51
|
+
tx_hash: outPoint.txHash,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
static toCellInput(cellInput: CellInput) {
|
|
55
|
+
return {
|
|
56
|
+
previous_output: JsonRpcTransformers.toOutPoint(cellInput.previousOutput),
|
|
57
|
+
since: toHex(cellInput.since),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
static toCellOutput(cellOutput: CellOutput) {
|
|
61
|
+
return {
|
|
62
|
+
capacity: toHex(cellOutput.capacity),
|
|
63
|
+
lock: JsonRpcTransformers.toScript(cellOutput.lock),
|
|
64
|
+
type: JsonRpcTransformers.toScript(cellOutput.type),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
static toCellDep(cellDep: CellDep) {
|
|
68
|
+
return {
|
|
69
|
+
out_point: JsonRpcTransformers.toOutPoint(cellDep.outPoint),
|
|
70
|
+
dep_type: JsonRpcTransformers.toDepType(cellDep.depType),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
static toTransaction(tx: Transaction) {
|
|
74
|
+
return {
|
|
75
|
+
version: toHex(tx.version),
|
|
76
|
+
cell_deps: tx.cellDeps.map((c) => JsonRpcTransformers.toCellDep(c)),
|
|
77
|
+
header_deps: tx.headerDeps,
|
|
78
|
+
inputs: tx.inputs.map((i) => JsonRpcTransformers.toCellInput(i)),
|
|
79
|
+
outputs: tx.outputs.map((o) => JsonRpcTransformers.toCellOutput(o)),
|
|
80
|
+
outputs_data: tx.outputsData,
|
|
81
|
+
witnesses: tx.witnesses,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export const CkbRpcMethods: JsonRpcMethod[] = [
|
|
87
|
+
{
|
|
88
|
+
method: "sendTransaction",
|
|
89
|
+
rpcMethod: "send_transaction",
|
|
90
|
+
inTransformers: [JsonRpcTransformers.toTransaction],
|
|
91
|
+
outTransformer: toHex,
|
|
92
|
+
},
|
|
93
|
+
];
|