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