@ckb-ccc/core 0.0.2-alpha.2 → 0.0.2-alpha.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/.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 +69 -22
- package/dist/address/advanced.d.ts +1 -28
- package/dist/address/advanced.d.ts.map +1 -1
- package/dist/address/advanced.js +3 -61
- package/dist/address/index.d.ts +10 -13
- package/dist/address/index.d.ts.map +1 -1
- package/dist/address/index.js +18 -18
- package/dist/advancedBarrel.d.ts +1 -0
- package/dist/advancedBarrel.d.ts.map +1 -1
- package/dist/advancedBarrel.js +1 -0
- package/dist/barrel.d.ts +4 -2
- package/dist/barrel.d.ts.map +1 -1
- package/dist/barrel.js +4 -2
- package/dist/bytes/index.d.ts +7 -0
- package/dist/bytes/index.d.ts.map +1 -0
- package/dist/bytes/index.js +41 -0
- package/dist/ckb/advanced.d.ts +3 -2
- package/dist/ckb/advanced.d.ts.map +1 -1
- package/dist/ckb/advanced.js +16 -2
- package/dist/ckb/index.d.ts +2 -3
- package/dist/ckb/index.d.ts.map +1 -1
- package/dist/ckb/index.js +2 -3
- package/dist/ckb/molecule.advanced/generated.d.ts.map +1 -0
- package/dist/ckb/{types/molecule.advanced → molecule.advanced}/generated.js +378 -97
- package/dist/ckb/molecule.advanced/index.d.ts +6 -0
- package/dist/ckb/molecule.advanced/index.d.ts.map +1 -0
- package/dist/ckb/{types/molecule.advanced → molecule.advanced}/index.js +8 -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/{types/script.advanced.js → script.advanced.js} +2 -1
- package/dist/ckb/script.d.ts +29 -0
- package/dist/ckb/script.d.ts.map +1 -0
- package/dist/ckb/script.js +91 -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/{types/transaction.advanced.js → transaction.advanced.js} +2 -1
- package/dist/ckb/transaction.d.ts +142 -0
- package/dist/ckb/transaction.d.ts.map +1 -0
- package/dist/ckb/transaction.js +262 -0
- package/dist/client/client.d.ts +4 -4
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.advanced.d.ts +2 -2
- package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
- package/dist/client/clientPublicMainnet.d.ts +2 -2
- package/dist/client/clientPublicMainnet.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.advanced.d.ts +2 -2
- package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.d.ts +2 -2
- package/dist/client/clientPublicTestnet.d.ts.map +1 -1
- 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 +11 -11
- package/dist/client/jsonRpc/index.d.ts.map +1 -1
- package/dist/client/jsonRpc/index.js +0 -1
- package/dist/fixedPoint/index.d.ts +7 -0
- package/dist/fixedPoint/index.d.ts.map +1 -0
- package/dist/{fixedPoint.js → fixedPoint/index.js} +9 -12
- 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/{ckb/hasher.js → hasher/index.js} +4 -4
- package/dist/hex/index.d.ts +5 -0
- package/dist/hex/index.d.ts.map +1 -0
- package/dist/hex/index.js +9 -0
- package/dist/num/index.d.ts +13 -0
- package/dist/num/index.d.ts.map +1 -0
- package/dist/num/index.js +47 -0
- package/dist/signer/helpers.d.ts +3 -3
- package/dist/signer/helpers.d.ts.map +1 -1
- package/dist/signer/helpers.js +7 -4
- package/dist/signer/index.d.ts +2 -0
- package/dist/signer/index.d.ts.map +1 -1
- package/dist/signer/index.js +2 -0
- package/dist/signer/signer.d.ts +12 -3
- package/dist/signer/signer.d.ts.map +1 -1
- package/dist/signer/signer.js +13 -2
- package/dist/signer/signerReadonly.d.ts +8 -0
- package/dist/signer/signerReadonly.d.ts.map +1 -0
- package/dist/signer/signerReadonly.js +13 -0
- package/dist/{ckb/viewerCkbScript.d.ts → signer/signerReadonlyCkbScript.d.ts} +6 -5
- package/dist/signer/signerReadonlyCkbScript.d.ts.map +1 -0
- package/dist/{ckb/viewerCkbScript.js → signer/signerReadonlyCkbScript.js} +7 -4
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +10 -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 +4 -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/{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.d.ts.map +0 -1
- 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/script.advanced.d.ts.map +0 -1
- 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.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/fixedPoint.d.ts +0 -9
- package/dist/fixedPoint.d.ts.map +0 -1
- 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/dist/{ckb/hasher.advanced.js → hasher/advanced.js} +0 -0
- /package/src/ckb/{types/molecule.advanced → molecule.advanced}/blockchain.mol +0 -0
- /package/src/{ckb/hasher.advanced.ts → hasher/advanced.ts} +0 -0
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const viewer_1 = require("../viewer");
|
|
3
|
+
exports.SignerReadonlyCkbScript = void 0;
|
|
5
4
|
const address_1 = require("../address");
|
|
6
|
-
|
|
5
|
+
const signerReadonly_1 = require("./signerReadonly");
|
|
6
|
+
class SignerReadonlyCkbScript extends signerReadonly_1.SignerReadonly {
|
|
7
7
|
constructor(script, client) {
|
|
8
8
|
super(client);
|
|
9
9
|
this.script = script;
|
|
10
10
|
}
|
|
11
|
+
async connect() {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
11
14
|
async getInternalAddress() {
|
|
12
15
|
return this.getRecommendedAddress();
|
|
13
16
|
}
|
|
@@ -15,4 +18,4 @@ class ViewerCkbScript extends viewer_1.Viewer {
|
|
|
15
18
|
return [await address_1.Address.fromScript(this.script, this.client)];
|
|
16
19
|
}
|
|
17
20
|
}
|
|
18
|
-
exports.
|
|
21
|
+
exports.SignerReadonlyCkbScript = SignerReadonlyCkbScript;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function apply<T, R>(transformer: (val: T) => R, value: undefined): undefined;
|
|
2
|
+
export declare function apply<T, R>(transformer: (val: T) => R, value: null): null;
|
|
3
|
+
export declare function apply<T, R>(transformer: (val: T) => R, value: T): R;
|
|
4
|
+
export declare function apply<T, R>(transformer: (val: T) => R, value: T | undefined): R | undefined;
|
|
5
|
+
export declare function apply<T, R>(transformer: (val: T) => R, value: T | null): R | null;
|
|
6
|
+
export declare function apply<T, R>(transformer: (val: T) => R, value: undefined | null): undefined | null;
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,SAAS,GACf,SAAS,CAAC;AACb,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,GAAG,IAAI,CAAC;AAC3E,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;AACrE,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,CAAC,GAAG,SAAS,GACnB,CAAC,GAAG,SAAS,CAAC;AACjB,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,CAAC,GAAG,IAAI,GACd,CAAC,GAAG,IAAI,CAAC;AACZ,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EACxB,WAAW,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,EAC1B,KAAK,EAAE,SAAS,GAAG,IAAI,GACtB,SAAS,GAAG,IAAI,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ckb-ccc/core",
|
|
3
|
-
"version": "0.0.2-alpha.
|
|
3
|
+
"version": "0.0.2-alpha.3",
|
|
4
4
|
"description": "Common Chains Connector Core",
|
|
5
5
|
"author": "Hanssen0 <hanssen0@hanssen0.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
16
|
"build": "rimraf ./dist && tsc",
|
|
17
|
-
"lint": "eslint
|
|
17
|
+
"lint": "eslint",
|
|
18
|
+
"format": "prettier --write . && eslint --fix"
|
|
18
19
|
},
|
|
19
20
|
"devDependencies": {
|
|
20
21
|
"@ckb-lumos/helpers": "^0.22.2",
|
|
@@ -24,6 +25,7 @@
|
|
|
24
25
|
"eslint-config-prettier": "^9.1.0",
|
|
25
26
|
"eslint-plugin-prettier": "^5.1.3",
|
|
26
27
|
"prettier": "^3.2.5",
|
|
28
|
+
"prettier-plugin-organize-imports": "^3.2.4",
|
|
27
29
|
"rimraf": "^5.0.5",
|
|
28
30
|
"ts-essentials": "^9.4.2",
|
|
29
31
|
"typescript": "^5.4.5",
|
|
@@ -39,5 +41,5 @@
|
|
|
39
41
|
"buffer": "^6.0.3",
|
|
40
42
|
"cross-fetch": "^4.0.0"
|
|
41
43
|
},
|
|
42
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "32c720f568bc39ba31e0ef00e86fe9173f76e4bf"
|
|
43
45
|
}
|
|
@@ -1,27 +1,60 @@
|
|
|
1
|
+
import { bech32, bech32m } from "bech32";
|
|
2
|
+
import { hashTypeFromBytes } from "../ckb";
|
|
1
3
|
import { Client, KnownScript } from "../client";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { type Address as AddressType } from "./index";
|
|
5
|
-
import { decodeHashType } from "../ckb";
|
|
4
|
+
import { hexFrom } from "../hex";
|
|
5
|
+
import { type AddressLike } from "./index";
|
|
6
6
|
|
|
7
|
-
export
|
|
7
|
+
export function addressPayloadFromString(address: string): {
|
|
8
|
+
prefix: string;
|
|
9
|
+
format: AddressFormat;
|
|
10
|
+
payload: number[];
|
|
11
|
+
} {
|
|
12
|
+
// Try parse full format address
|
|
13
|
+
{
|
|
14
|
+
const { words, prefix } = bech32m.decode(address, ADDRESS_BECH32_LIMIT);
|
|
15
|
+
const [formatType, ...payload] = bech32m.fromWords(words);
|
|
16
|
+
|
|
17
|
+
if (formatType === (AddressFormat.Full as number)) {
|
|
18
|
+
return { prefix, format: AddressFormat.Full, payload };
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Try parse legacy 2019 format address
|
|
23
|
+
{
|
|
24
|
+
const { prefix, words } = bech32.decode(address, ADDRESS_BECH32_LIMIT);
|
|
25
|
+
const [formatType, ...payload] = bech32.fromWords(words);
|
|
26
|
+
if (
|
|
27
|
+
[
|
|
28
|
+
AddressFormat.FullData,
|
|
29
|
+
AddressFormat.FullType,
|
|
30
|
+
AddressFormat.Short,
|
|
31
|
+
].includes(formatType)
|
|
32
|
+
) {
|
|
33
|
+
return { prefix, format: formatType, payload };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
throw Error(`Unknown address format ${address}`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function addressFromPayload(
|
|
8
41
|
prefix: string,
|
|
9
42
|
format: AddressFormat,
|
|
10
43
|
payload: number[],
|
|
11
44
|
client: Client,
|
|
12
|
-
): Promise<
|
|
45
|
+
): Promise<AddressLike> {
|
|
13
46
|
if (format === AddressFormat.Full) {
|
|
14
47
|
if (payload.length < 32 + 1) {
|
|
15
48
|
throw new Error(
|
|
16
|
-
`Invalid full address without enough payload ${
|
|
49
|
+
`Invalid full address without enough payload ${hexFrom(payload)}`,
|
|
17
50
|
);
|
|
18
51
|
}
|
|
19
52
|
|
|
20
53
|
return {
|
|
21
54
|
script: {
|
|
22
|
-
codeHash:
|
|
23
|
-
hashType:
|
|
24
|
-
args:
|
|
55
|
+
codeHash: hexFrom(payload.slice(0, 32)),
|
|
56
|
+
hashType: hashTypeFromBytes(payload.slice(32, 33)),
|
|
57
|
+
args: hexFrom(payload.slice(33)),
|
|
25
58
|
},
|
|
26
59
|
prefix,
|
|
27
60
|
};
|
|
@@ -30,15 +63,15 @@ export async function fromAddressPayload(
|
|
|
30
63
|
if (format === AddressFormat.FullData) {
|
|
31
64
|
if (payload.length < 32) {
|
|
32
65
|
throw new Error(
|
|
33
|
-
`Invalid full data address without enough payload ${
|
|
66
|
+
`Invalid full data address without enough payload ${hexFrom(payload)}`,
|
|
34
67
|
);
|
|
35
68
|
}
|
|
36
69
|
|
|
37
70
|
return {
|
|
38
71
|
script: {
|
|
39
|
-
codeHash:
|
|
72
|
+
codeHash: hexFrom(payload.slice(0, 32)),
|
|
40
73
|
hashType: "data",
|
|
41
|
-
args:
|
|
74
|
+
args: hexFrom(payload.slice(32)),
|
|
42
75
|
},
|
|
43
76
|
prefix,
|
|
44
77
|
};
|
|
@@ -47,15 +80,15 @@ export async function fromAddressPayload(
|
|
|
47
80
|
if (format === AddressFormat.FullType) {
|
|
48
81
|
if (payload.length < 32) {
|
|
49
82
|
throw new Error(
|
|
50
|
-
`Invalid full type address without enough payload ${
|
|
83
|
+
`Invalid full type address without enough payload ${hexFrom(payload)}`,
|
|
51
84
|
);
|
|
52
85
|
}
|
|
53
86
|
|
|
54
87
|
return {
|
|
55
88
|
script: {
|
|
56
|
-
codeHash:
|
|
89
|
+
codeHash: hexFrom(payload.slice(0, 32)),
|
|
57
90
|
hashType: "type",
|
|
58
|
-
args:
|
|
91
|
+
args: hexFrom(payload.slice(32)),
|
|
59
92
|
},
|
|
60
93
|
prefix,
|
|
61
94
|
};
|
|
@@ -64,7 +97,7 @@ export async function fromAddressPayload(
|
|
|
64
97
|
// format === AddressFormat.Short
|
|
65
98
|
if (payload.length !== 21) {
|
|
66
99
|
throw new Error(
|
|
67
|
-
`Invalid short address without enough payload ${
|
|
100
|
+
`Invalid short address without enough payload ${hexFrom(payload)}`,
|
|
68
101
|
);
|
|
69
102
|
}
|
|
70
103
|
const script = [
|
|
@@ -74,15 +107,39 @@ export async function fromAddressPayload(
|
|
|
74
107
|
][payload[0]];
|
|
75
108
|
if (script === undefined) {
|
|
76
109
|
throw new Error(
|
|
77
|
-
`Invalid short address with unknown script ${
|
|
110
|
+
`Invalid short address with unknown script ${hexFrom(payload)}`,
|
|
78
111
|
);
|
|
79
112
|
}
|
|
80
113
|
|
|
81
114
|
return {
|
|
82
115
|
script: {
|
|
83
116
|
...(await client.getKnownScript(script)),
|
|
84
|
-
args:
|
|
117
|
+
args: hexFrom(payload.slice(1)),
|
|
85
118
|
},
|
|
86
119
|
prefix,
|
|
87
120
|
};
|
|
88
121
|
}
|
|
122
|
+
|
|
123
|
+
export enum AddressFormat {
|
|
124
|
+
/**
|
|
125
|
+
* full version identifies the hashType
|
|
126
|
+
*/
|
|
127
|
+
Full = 0x00,
|
|
128
|
+
/**
|
|
129
|
+
* @deprecated
|
|
130
|
+
* short version for locks with Known codeHash, deprecated
|
|
131
|
+
*/
|
|
132
|
+
Short = 0x01,
|
|
133
|
+
/**
|
|
134
|
+
* @deprecated
|
|
135
|
+
* full version with hashType = "Data", deprecated
|
|
136
|
+
*/
|
|
137
|
+
FullData = 0x02,
|
|
138
|
+
/**
|
|
139
|
+
* @deprecated
|
|
140
|
+
* full version with hashType = "Type", deprecated
|
|
141
|
+
*/
|
|
142
|
+
FullType = 0x04,
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export const ADDRESS_BECH32_LIMIT = 1023;
|
package/src/address/advanced.ts
CHANGED
|
@@ -1,59 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export * as Address from "./address.advanced";
|
|
3
|
-
|
|
4
|
-
export function parseAddressToPayload(address: string): {
|
|
5
|
-
prefix: string;
|
|
6
|
-
format: AddressFormat;
|
|
7
|
-
payload: number[];
|
|
8
|
-
} {
|
|
9
|
-
// Try parse full format address
|
|
10
|
-
{
|
|
11
|
-
const { words, prefix } = bech32m.decode(address, BECH32_LIMIT);
|
|
12
|
-
const [formatType, ...payload] = bech32m.fromWords(words);
|
|
13
|
-
|
|
14
|
-
if (formatType === (AddressFormat.Full as number)) {
|
|
15
|
-
return { prefix, format: AddressFormat.Full, payload };
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// Try parse legacy 2019 format address
|
|
20
|
-
{
|
|
21
|
-
const { prefix, words } = bech32.decode(address, BECH32_LIMIT);
|
|
22
|
-
const [formatType, ...payload] = bech32.fromWords(words);
|
|
23
|
-
if (
|
|
24
|
-
[
|
|
25
|
-
AddressFormat.FullData,
|
|
26
|
-
AddressFormat.FullType,
|
|
27
|
-
AddressFormat.Short,
|
|
28
|
-
].includes(formatType)
|
|
29
|
-
) {
|
|
30
|
-
return { prefix, format: formatType, payload };
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
throw Error(`Unknown address format ${address}`);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export enum AddressFormat {
|
|
38
|
-
/**
|
|
39
|
-
* full version identifies the hashType
|
|
40
|
-
*/
|
|
41
|
-
Full = 0x00,
|
|
42
|
-
/**
|
|
43
|
-
* @deprecated
|
|
44
|
-
* short version for locks with Known codeHash, deprecated
|
|
45
|
-
*/
|
|
46
|
-
Short = 0x01,
|
|
47
|
-
/**
|
|
48
|
-
* @deprecated
|
|
49
|
-
* full version with hashType = "Data", deprecated
|
|
50
|
-
*/
|
|
51
|
-
FullData = 0x02,
|
|
52
|
-
/**
|
|
53
|
-
* @deprecated
|
|
54
|
-
* full version with hashType = "Type", deprecated
|
|
55
|
-
*/
|
|
56
|
-
FullType = 0x04,
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export const BECH32_LIMIT = 1023;
|
|
1
|
+
export * from "./address.advanced";
|
package/src/address/index.ts
CHANGED
|
@@ -1,27 +1,38 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { bech32m } from "bech32";
|
|
2
|
+
import { bytesConcat, bytesFrom } from "../bytes";
|
|
3
|
+
import { Script, ScriptLike, hashTypeToBytes } from "../ckb";
|
|
2
4
|
import { Client, KnownScript } from "../client";
|
|
3
|
-
import {
|
|
5
|
+
import { HexLike } from "../hex";
|
|
4
6
|
import {
|
|
7
|
+
ADDRESS_BECH32_LIMIT,
|
|
5
8
|
AddressFormat,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from "./advanced";
|
|
10
|
-
import { bech32m } from "bech32";
|
|
11
|
-
import { Hex } from "../primitive";
|
|
12
|
-
import { concatBytes, toBytes } from "../bytes";
|
|
9
|
+
addressFromPayload,
|
|
10
|
+
addressPayloadFromString,
|
|
11
|
+
} from "./address.advanced";
|
|
13
12
|
|
|
13
|
+
export type AddressLike = {
|
|
14
|
+
script: ScriptLike;
|
|
15
|
+
prefix: string;
|
|
16
|
+
};
|
|
14
17
|
export class Address {
|
|
15
18
|
constructor(
|
|
16
19
|
public script: Script,
|
|
17
20
|
public prefix: string,
|
|
18
21
|
) {}
|
|
19
22
|
|
|
23
|
+
static from(address: AddressLike): Address {
|
|
24
|
+
if (address instanceof Address) {
|
|
25
|
+
return address;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return new Address(Script.from(address.script), address.prefix);
|
|
29
|
+
}
|
|
30
|
+
|
|
20
31
|
static async fromString(
|
|
21
32
|
address: string,
|
|
22
33
|
clients: Client | Record<string, Client>,
|
|
23
34
|
): Promise<Address> {
|
|
24
|
-
const { prefix, format, payload } =
|
|
35
|
+
const { prefix, format, payload } = addressPayloadFromString(address);
|
|
25
36
|
|
|
26
37
|
const client = (clients as Record<string, Client>)[prefix] ?? clients;
|
|
27
38
|
if (!client) {
|
|
@@ -34,37 +45,44 @@ export class Address {
|
|
|
34
45
|
);
|
|
35
46
|
}
|
|
36
47
|
|
|
37
|
-
return
|
|
48
|
+
return Address.from(
|
|
49
|
+
await addressFromPayload(prefix, format, payload, client),
|
|
50
|
+
);
|
|
38
51
|
}
|
|
39
52
|
|
|
40
53
|
static async fromScript(
|
|
41
|
-
script:
|
|
54
|
+
script: ScriptLike,
|
|
42
55
|
client: Client,
|
|
43
56
|
): Promise<Address> {
|
|
44
|
-
return
|
|
45
|
-
script: { ...script },
|
|
46
|
-
prefix: await client.getAddressPrefix(),
|
|
47
|
-
};
|
|
57
|
+
return new Address(Script.from(script), await client.getAddressPrefix());
|
|
48
58
|
}
|
|
49
59
|
|
|
50
|
-
static async fromKnownScript(
|
|
51
|
-
|
|
52
|
-
|
|
60
|
+
static async fromKnownScript(
|
|
61
|
+
script: KnownScript,
|
|
62
|
+
args: HexLike,
|
|
63
|
+
client: Client,
|
|
64
|
+
) {
|
|
65
|
+
return new Address(
|
|
66
|
+
Script.from({
|
|
53
67
|
...(await client.getKnownScript(script)),
|
|
54
68
|
args,
|
|
55
|
-
},
|
|
56
|
-
|
|
57
|
-
|
|
69
|
+
}),
|
|
70
|
+
await client.getAddressPrefix(),
|
|
71
|
+
);
|
|
58
72
|
}
|
|
59
73
|
|
|
60
|
-
|
|
61
|
-
const data =
|
|
74
|
+
toString(): string {
|
|
75
|
+
const data = bytesConcat(
|
|
62
76
|
[AddressFormat.Full],
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
77
|
+
bytesFrom(this.script.codeHash),
|
|
78
|
+
hashTypeToBytes(this.script.hashType),
|
|
79
|
+
bytesFrom(this.script.args),
|
|
66
80
|
);
|
|
67
81
|
|
|
68
|
-
return bech32m.encode(
|
|
82
|
+
return bech32m.encode(
|
|
83
|
+
this.prefix,
|
|
84
|
+
bech32m.toWords(data),
|
|
85
|
+
ADDRESS_BECH32_LIMIT,
|
|
86
|
+
);
|
|
69
87
|
}
|
|
70
88
|
}
|
package/src/advancedBarrel.ts
CHANGED
package/src/barrel.ts
CHANGED
|
@@ -3,6 +3,8 @@ export * from "./bytes";
|
|
|
3
3
|
export * from "./ckb";
|
|
4
4
|
export * from "./client";
|
|
5
5
|
export * from "./fixedPoint";
|
|
6
|
-
export * from "./
|
|
6
|
+
export * from "./hasher";
|
|
7
|
+
export * from "./hex";
|
|
8
|
+
export * from "./num";
|
|
7
9
|
export * from "./signer";
|
|
8
|
-
export * from "./
|
|
10
|
+
export * from "./utils";
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Buffer } from "buffer/";
|
|
2
|
+
|
|
3
|
+
export type Bytes = Uint8Array;
|
|
4
|
+
export type BytesLike = string | Uint8Array | ArrayBuffer | number[];
|
|
5
|
+
|
|
6
|
+
export function bytesConcat(...args: BytesLike[]): Bytes {
|
|
7
|
+
return new Uint8Array(
|
|
8
|
+
args.reduce((acc: number[], v) => {
|
|
9
|
+
acc.push(...bytesFrom(v));
|
|
10
|
+
return acc;
|
|
11
|
+
}, []),
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function bytesFromUtf8(val: string): Bytes {
|
|
16
|
+
return Buffer.from(val, "utf-8");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function bytesToUtf8(val: BytesLike): string {
|
|
20
|
+
return Buffer.from(bytesFrom(val)).toString("utf-8");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function bytesFrom(bytes: BytesLike): Bytes {
|
|
24
|
+
if (bytes instanceof Uint8Array) {
|
|
25
|
+
return bytes;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (bytes instanceof ArrayBuffer) {
|
|
29
|
+
return new Uint8Array(bytes);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (Array.isArray(bytes)) {
|
|
33
|
+
if (bytes.some((v) => v < 0 || 0xff < v)) {
|
|
34
|
+
throw new Error(`Invalid bytes ${JSON.stringify(bytes)}`);
|
|
35
|
+
}
|
|
36
|
+
return new Uint8Array(bytes);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const str = bytes.startsWith("0x") ? bytes.slice(2) : bytes;
|
|
40
|
+
const paddedStr = str.length % 2 === 0 ? str : `0${str}`;
|
|
41
|
+
const data = Buffer.from(paddedStr, "hex");
|
|
42
|
+
if (data.length * 2 !== paddedStr.length) {
|
|
43
|
+
throw new Error(`Invalid bytes ${bytes}`);
|
|
44
|
+
}
|
|
45
|
+
return data;
|
|
46
|
+
}
|
package/src/ckb/advanced.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
1
|
+
export * as moleculeCodecCkb from "./molecule.advanced";
|
|
2
|
+
export * from "./script.advanced";
|
|
3
|
+
export * from "./transaction.advanced";
|
package/src/ckb/index.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./
|
|
3
|
-
export * from "./viewerCkbScript";
|
|
1
|
+
export * from "./script";
|
|
2
|
+
export * from "./transaction";
|
|
@@ -237,7 +237,9 @@ export class ScriptOpt {
|
|
|
237
237
|
hasValue(): boolean;
|
|
238
238
|
}
|
|
239
239
|
|
|
240
|
-
export function SerializeProposalShortId(
|
|
240
|
+
export function SerializeProposalShortId(
|
|
241
|
+
value: CanCastToArrayBuffer,
|
|
242
|
+
): ArrayBuffer;
|
|
241
243
|
export class ProposalShortId {
|
|
242
244
|
constructor(reader: CanCastToArrayBuffer, options?: CreateOptions);
|
|
243
245
|
validate(compatible?: boolean): void;
|
|
@@ -246,7 +248,9 @@ export class ProposalShortId {
|
|
|
246
248
|
static size(): Number;
|
|
247
249
|
}
|
|
248
250
|
|
|
249
|
-
export function SerializeUncleBlockVec(
|
|
251
|
+
export function SerializeUncleBlockVec(
|
|
252
|
+
value: Array<UncleBlockType>,
|
|
253
|
+
): ArrayBuffer;
|
|
250
254
|
export class UncleBlockVec {
|
|
251
255
|
constructor(reader: CanCastToArrayBuffer, options?: CreateOptions);
|
|
252
256
|
validate(compatible?: boolean): void;
|
|
@@ -254,7 +258,9 @@ export class UncleBlockVec {
|
|
|
254
258
|
length(): number;
|
|
255
259
|
}
|
|
256
260
|
|
|
257
|
-
export function SerializeTransactionVec(
|
|
261
|
+
export function SerializeTransactionVec(
|
|
262
|
+
value: Array<TransactionType>,
|
|
263
|
+
): ArrayBuffer;
|
|
258
264
|
export class TransactionVec {
|
|
259
265
|
constructor(reader: CanCastToArrayBuffer, options?: CreateOptions);
|
|
260
266
|
validate(compatible?: boolean): void;
|
|
@@ -262,7 +268,9 @@ export class TransactionVec {
|
|
|
262
268
|
length(): number;
|
|
263
269
|
}
|
|
264
270
|
|
|
265
|
-
export function SerializeProposalShortIdVec(
|
|
271
|
+
export function SerializeProposalShortIdVec(
|
|
272
|
+
value: Array<ProposalShortIdType>,
|
|
273
|
+
): ArrayBuffer;
|
|
266
274
|
export class ProposalShortIdVec {
|
|
267
275
|
constructor(reader: CanCastToArrayBuffer, options?: CreateOptions);
|
|
268
276
|
validate(compatible?: boolean): void;
|
|
@@ -286,7 +294,9 @@ export class CellInputVec {
|
|
|
286
294
|
length(): number;
|
|
287
295
|
}
|
|
288
296
|
|
|
289
|
-
export function SerializeCellOutputVec(
|
|
297
|
+
export function SerializeCellOutputVec(
|
|
298
|
+
value: Array<CellOutputType>,
|
|
299
|
+
): ArrayBuffer;
|
|
290
300
|
export class CellOutputVec {
|
|
291
301
|
constructor(reader: CanCastToArrayBuffer, options?: CreateOptions);
|
|
292
302
|
validate(compatible?: boolean): void;
|
|
@@ -414,7 +424,9 @@ export class BlockV1 {
|
|
|
414
424
|
getExtension(): Bytes;
|
|
415
425
|
}
|
|
416
426
|
|
|
417
|
-
export function SerializeCellbaseWitness(
|
|
427
|
+
export function SerializeCellbaseWitness(
|
|
428
|
+
value: CellbaseWitnessType,
|
|
429
|
+
): ArrayBuffer;
|
|
418
430
|
export class CellbaseWitness {
|
|
419
431
|
constructor(reader: CanCastToArrayBuffer, options?: CreateOptions);
|
|
420
432
|
validate(compatible?: boolean): void;
|
|
@@ -430,4 +442,3 @@ export class WitnessArgs {
|
|
|
430
442
|
getInputType(): BytesOpt;
|
|
431
443
|
getOutputType(): BytesOpt;
|
|
432
444
|
}
|
|
433
|
-
|