@ckb-ccc/core 0.0.2-alpha.0 → 0.0.2-alpha.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/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 +64 -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/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/tsconfig.tsbuildinfo +0 -1
- 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
package/dist/viewer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"viewer.d.ts","sourceRoot":"","sources":["../src/viewer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"viewer.d.ts","sourceRoot":"","sources":["../src/viewer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,8BAAsB,MAAM;aACE,MAAM,EAAE,MAAM;gBAAd,MAAM,EAAE,MAAM;IAE1C,QAAQ,CAAC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAE9C,QAAQ,CAAC,cAAc,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;IACvC,wBAAwB,CAAC,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IAIjE,qBAAqB,CAAC,UAAU,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;IAG5D,YAAY,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;CAKxC"}
|
package/dist/viewer.js
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Viewer = void 0;
|
|
4
|
+
const address_1 = require("./address");
|
|
4
5
|
class Viewer {
|
|
6
|
+
constructor(client) {
|
|
7
|
+
this.client = client;
|
|
8
|
+
}
|
|
9
|
+
async getRecommendedAddressObj(_preference) {
|
|
10
|
+
return (await this.getAddressObjs())[0];
|
|
11
|
+
}
|
|
12
|
+
async getRecommendedAddress(preference) {
|
|
13
|
+
return address_1.Address.toString(await this.getRecommendedAddressObj(preference));
|
|
14
|
+
}
|
|
15
|
+
async getAddresses() {
|
|
16
|
+
return this.getAddressObjs().then((addresses) => addresses.map((address) => address_1.Address.toString(address)));
|
|
17
|
+
}
|
|
5
18
|
}
|
|
6
19
|
exports.Viewer = Viewer;
|
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.2",
|
|
4
4
|
"description": "Common Chains Connector Core",
|
|
5
5
|
"author": "Hanssen0 <hanssen0@hanssen0.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -8,18 +8,24 @@
|
|
|
8
8
|
"main": "dist/index.js",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": "./dist/index.js",
|
|
11
|
-
"./barrel": "./dist/barrel.js"
|
|
11
|
+
"./barrel": "./dist/barrel.js",
|
|
12
|
+
"./advancedBarrel": "./dist/advancedBarrel.js",
|
|
13
|
+
"./advanced": "./dist/advanced.js"
|
|
12
14
|
},
|
|
13
15
|
"scripts": {
|
|
14
|
-
"build": "tsc",
|
|
16
|
+
"build": "rimraf ./dist && tsc",
|
|
15
17
|
"lint": "eslint ."
|
|
16
18
|
},
|
|
17
19
|
"devDependencies": {
|
|
20
|
+
"@ckb-lumos/helpers": "^0.22.2",
|
|
18
21
|
"@eslint/js": "^9.1.1",
|
|
22
|
+
"@types/blake2b": "^2.1.3",
|
|
19
23
|
"eslint": "^9.1.0",
|
|
20
24
|
"eslint-config-prettier": "^9.1.0",
|
|
21
25
|
"eslint-plugin-prettier": "^5.1.3",
|
|
22
26
|
"prettier": "^3.2.5",
|
|
27
|
+
"rimraf": "^5.0.5",
|
|
28
|
+
"ts-essentials": "^9.4.2",
|
|
23
29
|
"typescript": "^5.4.5",
|
|
24
30
|
"typescript-eslint": "^7.7.0"
|
|
25
31
|
},
|
|
@@ -27,9 +33,11 @@
|
|
|
27
33
|
"access": "public"
|
|
28
34
|
},
|
|
29
35
|
"dependencies": {
|
|
36
|
+
"abort-controller": "^3.0.0",
|
|
30
37
|
"bech32": "^2.0.0",
|
|
38
|
+
"blake2b": "^2.1.4",
|
|
31
39
|
"buffer": "^6.0.3",
|
|
32
|
-
"
|
|
40
|
+
"cross-fetch": "^4.0.0"
|
|
33
41
|
},
|
|
34
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "dae8f0bcf3554125cebee4b9497c0045d7195a63"
|
|
35
43
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Client, KnownScript } from "../client";
|
|
2
|
+
import { toHex } from "../bytes";
|
|
3
|
+
import { AddressFormat } from "./advanced";
|
|
4
|
+
import { type Address as AddressType } from "./index";
|
|
5
|
+
import { decodeHashType } from "../ckb";
|
|
6
|
+
|
|
7
|
+
export async function fromAddressPayload(
|
|
8
|
+
prefix: string,
|
|
9
|
+
format: AddressFormat,
|
|
10
|
+
payload: number[],
|
|
11
|
+
client: Client,
|
|
12
|
+
): Promise<AddressType> {
|
|
13
|
+
if (format === AddressFormat.Full) {
|
|
14
|
+
if (payload.length < 32 + 1) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
`Invalid full address without enough payload ${toHex(payload)}`,
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
script: {
|
|
22
|
+
codeHash: toHex(payload.slice(0, 32)),
|
|
23
|
+
hashType: decodeHashType(payload.slice(32, 33)),
|
|
24
|
+
args: toHex(payload.slice(33)),
|
|
25
|
+
},
|
|
26
|
+
prefix,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (format === AddressFormat.FullData) {
|
|
31
|
+
if (payload.length < 32) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`Invalid full data address without enough payload ${toHex(payload)}`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
script: {
|
|
39
|
+
codeHash: toHex(payload.slice(0, 32)),
|
|
40
|
+
hashType: "data",
|
|
41
|
+
args: toHex(payload.slice(32)),
|
|
42
|
+
},
|
|
43
|
+
prefix,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (format === AddressFormat.FullType) {
|
|
48
|
+
if (payload.length < 32) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Invalid full type address without enough payload ${toHex(payload)}`,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
script: {
|
|
56
|
+
codeHash: toHex(payload.slice(0, 32)),
|
|
57
|
+
hashType: "type",
|
|
58
|
+
args: toHex(payload.slice(32)),
|
|
59
|
+
},
|
|
60
|
+
prefix,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// format === AddressFormat.Short
|
|
65
|
+
if (payload.length !== 21) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`Invalid short address without enough payload ${toHex(payload)}`,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
const script = [
|
|
71
|
+
KnownScript.Secp256k1Blake160,
|
|
72
|
+
KnownScript.Secp256k1Multisig,
|
|
73
|
+
KnownScript.AnyoneCanPay,
|
|
74
|
+
][payload[0]];
|
|
75
|
+
if (script === undefined) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`Invalid short address with unknown script ${toHex(payload)}`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
script: {
|
|
83
|
+
...(await client.getKnownScript(script)),
|
|
84
|
+
args: toHex(payload.slice(1)),
|
|
85
|
+
},
|
|
86
|
+
prefix,
|
|
87
|
+
};
|
|
88
|
+
}
|
package/src/address/advanced.ts
CHANGED
|
@@ -1,52 +1,25 @@
|
|
|
1
1
|
import { bech32, bech32m } from "bech32";
|
|
2
|
-
|
|
3
|
-
import { Client } from "../client";
|
|
4
|
-
import { ClientPublicTestnet, ClientPublicMainnet } from "../client";
|
|
5
|
-
import { encodeScript } from "../codec";
|
|
2
|
+
export * as Address from "./address.advanced";
|
|
6
3
|
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
export enum AddressFormat {
|
|
10
|
-
/**
|
|
11
|
-
* full version identifies the hashType
|
|
12
|
-
*/
|
|
13
|
-
Full = 0x00,
|
|
14
|
-
/**
|
|
15
|
-
* @deprecated
|
|
16
|
-
* short version for locks with Known codeHash, deprecated
|
|
17
|
-
*/
|
|
18
|
-
Short = 0x01,
|
|
19
|
-
/**
|
|
20
|
-
* @deprecated
|
|
21
|
-
* full version with hashType = "Data", deprecated
|
|
22
|
-
*/
|
|
23
|
-
FullData = 0x02,
|
|
24
|
-
/**
|
|
25
|
-
* @deprecated
|
|
26
|
-
* full version with hashType = "Type", deprecated
|
|
27
|
-
*/
|
|
28
|
-
FullType = 0x04,
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function parseAddress(address: string): {
|
|
4
|
+
export function parseAddressToPayload(address: string): {
|
|
32
5
|
prefix: string;
|
|
33
6
|
format: AddressFormat;
|
|
34
|
-
|
|
7
|
+
payload: number[];
|
|
35
8
|
} {
|
|
36
9
|
// Try parse full format address
|
|
37
10
|
{
|
|
38
11
|
const { words, prefix } = bech32m.decode(address, BECH32_LIMIT);
|
|
39
|
-
const [formatType, ...
|
|
12
|
+
const [formatType, ...payload] = bech32m.fromWords(words);
|
|
40
13
|
|
|
41
|
-
if (formatType === AddressFormat.Full) {
|
|
42
|
-
return { prefix, format: AddressFormat.Full,
|
|
14
|
+
if (formatType === (AddressFormat.Full as number)) {
|
|
15
|
+
return { prefix, format: AddressFormat.Full, payload };
|
|
43
16
|
}
|
|
44
17
|
}
|
|
45
18
|
|
|
46
19
|
// Try parse legacy 2019 format address
|
|
47
20
|
{
|
|
48
21
|
const { prefix, words } = bech32.decode(address, BECH32_LIMIT);
|
|
49
|
-
const [formatType, ...
|
|
22
|
+
const [formatType, ...payload] = bech32.fromWords(words);
|
|
50
23
|
if (
|
|
51
24
|
[
|
|
52
25
|
AddressFormat.FullData,
|
|
@@ -54,21 +27,33 @@ export function parseAddress(address: string): {
|
|
|
54
27
|
AddressFormat.Short,
|
|
55
28
|
].includes(formatType)
|
|
56
29
|
) {
|
|
57
|
-
return { prefix, format: formatType,
|
|
30
|
+
return { prefix, format: formatType, payload };
|
|
58
31
|
}
|
|
59
32
|
}
|
|
60
33
|
|
|
61
34
|
throw Error(`Unknown address format ${address}`);
|
|
62
35
|
}
|
|
63
36
|
|
|
64
|
-
export
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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,
|
|
74
57
|
}
|
|
58
|
+
|
|
59
|
+
export const BECH32_LIMIT = 1023;
|
package/src/address/index.ts
CHANGED
|
@@ -1,5 +1,70 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { DeepReadonly } from "ts-essentials";
|
|
2
|
+
import { Client, KnownScript } from "../client";
|
|
3
|
+
import { Script, encodeHashType } from "../ckb";
|
|
4
|
+
import {
|
|
5
|
+
AddressFormat,
|
|
6
|
+
BECH32_LIMIT,
|
|
7
|
+
Address as AdvancedAddress,
|
|
8
|
+
parseAddressToPayload,
|
|
9
|
+
} from "./advanced";
|
|
10
|
+
import { bech32m } from "bech32";
|
|
11
|
+
import { Hex } from "../primitive";
|
|
12
|
+
import { concatBytes, toBytes } from "../bytes";
|
|
13
|
+
|
|
14
|
+
export class Address {
|
|
15
|
+
constructor(
|
|
16
|
+
public script: Script,
|
|
17
|
+
public prefix: string,
|
|
18
|
+
) {}
|
|
19
|
+
|
|
20
|
+
static async fromString(
|
|
21
|
+
address: string,
|
|
22
|
+
clients: Client | Record<string, Client>,
|
|
23
|
+
): Promise<Address> {
|
|
24
|
+
const { prefix, format, payload } = parseAddressToPayload(address);
|
|
25
|
+
|
|
26
|
+
const client = (clients as Record<string, Client>)[prefix] ?? clients;
|
|
27
|
+
if (!client) {
|
|
28
|
+
throw new Error(`Unknown address prefix ${prefix}`);
|
|
29
|
+
}
|
|
30
|
+
const expectedPrefix = await client.getAddressPrefix();
|
|
31
|
+
if (expectedPrefix !== prefix) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`Unknown address prefix ${prefix}, expected ${expectedPrefix}`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return AdvancedAddress.fromAddressPayload(prefix, format, payload, client);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static async fromScript(
|
|
41
|
+
script: DeepReadonly<Script>,
|
|
42
|
+
client: Client,
|
|
43
|
+
): Promise<Address> {
|
|
44
|
+
return {
|
|
45
|
+
script: { ...script },
|
|
46
|
+
prefix: await client.getAddressPrefix(),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static async fromKnownScript(script: KnownScript, args: Hex, client: Client) {
|
|
51
|
+
return {
|
|
52
|
+
script: {
|
|
53
|
+
...(await client.getKnownScript(script)),
|
|
54
|
+
args,
|
|
55
|
+
},
|
|
56
|
+
prefix: await client.getAddressPrefix(),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static toString(address: DeepReadonly<Address>): string {
|
|
61
|
+
const data = concatBytes(
|
|
62
|
+
[AddressFormat.Full],
|
|
63
|
+
toBytes(address.script.codeHash),
|
|
64
|
+
encodeHashType(address.script.hashType),
|
|
65
|
+
toBytes(address.script.args),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
return bech32m.encode(address.prefix, bech32m.toWords(data), BECH32_LIMIT);
|
|
69
|
+
}
|
|
70
|
+
}
|
package/src/advanced.ts
ADDED
package/src/barrel.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
export * from "./address";
|
|
2
|
+
export * from "./bytes";
|
|
2
3
|
export * from "./ckb";
|
|
3
4
|
export * from "./client";
|
|
4
|
-
export * from "./
|
|
5
|
-
export * from "./
|
|
5
|
+
export * from "./fixedPoint";
|
|
6
|
+
export * from "./primitive";
|
|
6
7
|
export * from "./signer";
|
|
7
8
|
export * from "./viewer";
|
package/src/bytes.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { BytesLike, Hex, HexLike, NumberLike } from "./primitive";
|
|
2
|
+
import { Buffer } from "buffer/";
|
|
3
|
+
|
|
4
|
+
export function concatBytes(...args: BytesLike[]): Uint8Array {
|
|
5
|
+
return new Uint8Array(
|
|
6
|
+
args.reduce((acc: number[], v) => {
|
|
7
|
+
acc.push(...toBytes(v));
|
|
8
|
+
return acc;
|
|
9
|
+
}, []),
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function encodeUtf8(val: string): Uint8Array {
|
|
14
|
+
return Buffer.from(val, "utf-8");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function decodeUtf8(val: BytesLike): string {
|
|
18
|
+
return Buffer.from(toBytes(val)).toString("utf-8");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function toBytes(bytes: BytesLike): Uint8Array {
|
|
22
|
+
if (bytes instanceof Uint8Array) {
|
|
23
|
+
return bytes;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (bytes instanceof ArrayBuffer) {
|
|
27
|
+
return new Uint8Array(bytes);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (Array.isArray(bytes)) {
|
|
31
|
+
if (bytes.some((v) => v < 0 || 0xff < v)) {
|
|
32
|
+
throw new Error(`Invalid bytes ${JSON.stringify(bytes)}`);
|
|
33
|
+
}
|
|
34
|
+
return new Uint8Array(bytes);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const str = bytes.length % 2 === 0 ? bytes.slice(2) : `0${bytes.slice(2)}`;
|
|
38
|
+
const data = Buffer.from(str, "hex");
|
|
39
|
+
if (data.length * 2 !== str.length) {
|
|
40
|
+
throw new Error(`Invalid bytes ${bytes}`);
|
|
41
|
+
}
|
|
42
|
+
return data;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function toHexFromHex(bytes: HexLike): Hex {
|
|
46
|
+
if (typeof bytes === "string" && !bytes.startsWith("0x")) {
|
|
47
|
+
return `0x${bytes}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return toHex(bytes);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function toHex(bytes: HexLike): Hex {
|
|
54
|
+
if (typeof bytes === "number") {
|
|
55
|
+
return `0x${bytes.toString(16)}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (typeof bytes === "string" && !bytes.startsWith("0x")) {
|
|
59
|
+
return `0x${BigInt(bytes).toString(16)}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return `0x${Buffer.from(toBytes(bytes as BytesLike)).toString("hex")}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function toNumber(val: NumberLike): string {
|
|
66
|
+
return BigInt(val).toString();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function toBytesFromNumber(val: NumberLike, bytes?: number): Uint8Array {
|
|
70
|
+
return toBytesFromLENumber(val, bytes);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function toBytesFromLENumber(
|
|
74
|
+
val: NumberLike,
|
|
75
|
+
bytes?: number,
|
|
76
|
+
): Uint8Array {
|
|
77
|
+
return toBytesFromBENumber(val, bytes).reverse();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function toBytesFromBENumber(
|
|
81
|
+
val: NumberLike,
|
|
82
|
+
bytes?: number,
|
|
83
|
+
): Uint8Array {
|
|
84
|
+
let valStr = val;
|
|
85
|
+
if (typeof valStr === "number" || typeof valStr === "bigint") {
|
|
86
|
+
valStr = valStr.toString(16);
|
|
87
|
+
} else if (typeof valStr === "string") {
|
|
88
|
+
if (valStr.startsWith("0x")) {
|
|
89
|
+
valStr = valStr.slice(2);
|
|
90
|
+
} else {
|
|
91
|
+
valStr = BigInt(valStr).toString(16);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (valStr.length % 2 !== 0) {
|
|
96
|
+
valStr = `0${valStr}`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const res = toBytes(`0x${valStr}`);
|
|
100
|
+
if (bytes == null) {
|
|
101
|
+
return res;
|
|
102
|
+
}
|
|
103
|
+
if (res.length > bytes) {
|
|
104
|
+
return res.slice(res.length - bytes);
|
|
105
|
+
}
|
|
106
|
+
return new Uint8Array([
|
|
107
|
+
...Array.from(Array(bytes - res.length), () => 0),
|
|
108
|
+
...res,
|
|
109
|
+
]);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function toNumberFromBytes(val: BytesLike): string {
|
|
113
|
+
return toLENumberFromBytes(val);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function toLENumberFromBytes(val: BytesLike): string {
|
|
117
|
+
return toBENumberFromBytes(toBytes(val).reverse());
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function toBENumberFromBytes(val: BytesLike): string {
|
|
121
|
+
const str = toHex(val).replace(/0x0*/, "0x");
|
|
122
|
+
if (str.length === 2) {
|
|
123
|
+
return "0";
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return BigInt(str).toString();
|
|
127
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const CKB_BLAKE2B_PERSONAL = "ckb-default-hash";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import blake2b, { Blake2b } from "blake2b";
|
|
2
|
+
import { BytesLike, Hex } from "../primitive";
|
|
3
|
+
import { encodeUtf8, toBytes } from "../bytes";
|
|
4
|
+
import { CKB_BLAKE2B_PERSONAL } from "./hasher.advanced";
|
|
5
|
+
|
|
6
|
+
export class Hasher {
|
|
7
|
+
hasher: Blake2b;
|
|
8
|
+
|
|
9
|
+
constructor(outLength = 32, personal = CKB_BLAKE2B_PERSONAL) {
|
|
10
|
+
this.hasher = blake2b(
|
|
11
|
+
outLength,
|
|
12
|
+
undefined,
|
|
13
|
+
undefined,
|
|
14
|
+
encodeUtf8(personal),
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
update(data: BytesLike): Hasher {
|
|
19
|
+
this.hasher.update(toBytes(data));
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
digest(): Hex {
|
|
24
|
+
return `0x${this.hasher.digest("hex")}`;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function ckbHash(data: BytesLike): Hex {
|
|
29
|
+
return new Hasher().update(data).digest();
|
|
30
|
+
}
|
package/src/ckb/index.ts
CHANGED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/* Basic Types */
|
|
2
|
+
|
|
3
|
+
// The `UintN` is used to store a `N` bits unsigned integer
|
|
4
|
+
// as a byte array in little endian.
|
|
5
|
+
array Uint32 [byte; 4];
|
|
6
|
+
array Uint64 [byte; 8];
|
|
7
|
+
array Uint128 [byte; 16];
|
|
8
|
+
array Byte32 [byte; 32];
|
|
9
|
+
array Uint256 [byte; 32];
|
|
10
|
+
|
|
11
|
+
vector Bytes <byte>;
|
|
12
|
+
option BytesOpt (Bytes);
|
|
13
|
+
vector BytesOptVec <BytesOpt>;
|
|
14
|
+
vector BytesVec <Bytes>;
|
|
15
|
+
vector Byte32Vec <Byte32>;
|
|
16
|
+
|
|
17
|
+
/* Types for Chain */
|
|
18
|
+
|
|
19
|
+
option ScriptOpt (Script);
|
|
20
|
+
|
|
21
|
+
array ProposalShortId [byte; 10];
|
|
22
|
+
|
|
23
|
+
vector UncleBlockVec <UncleBlock>;
|
|
24
|
+
vector TransactionVec <Transaction>;
|
|
25
|
+
vector ProposalShortIdVec <ProposalShortId>;
|
|
26
|
+
vector CellDepVec <CellDep>;
|
|
27
|
+
vector CellInputVec <CellInput>;
|
|
28
|
+
vector CellOutputVec <CellOutput>;
|
|
29
|
+
|
|
30
|
+
table Script {
|
|
31
|
+
codeHash: Byte32,
|
|
32
|
+
hashType: byte,
|
|
33
|
+
args: Bytes,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
struct OutPoint {
|
|
37
|
+
txHash: Byte32,
|
|
38
|
+
index: Uint32,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
struct CellInput {
|
|
42
|
+
since: Uint64,
|
|
43
|
+
previousOutput: OutPoint,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
table CellOutput {
|
|
47
|
+
capacity: Uint64,
|
|
48
|
+
lock: Script,
|
|
49
|
+
type: ScriptOpt,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
struct CellDep {
|
|
53
|
+
outPoint: OutPoint,
|
|
54
|
+
depType: byte,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
table RawTransaction {
|
|
58
|
+
version: Uint32,
|
|
59
|
+
cellDeps: CellDepVec,
|
|
60
|
+
headerDeps: Byte32Vec,
|
|
61
|
+
inputs: CellInputVec,
|
|
62
|
+
outputs: CellOutputVec,
|
|
63
|
+
outputsData: BytesVec,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
table Transaction {
|
|
67
|
+
raw: RawTransaction,
|
|
68
|
+
witnesses: BytesVec,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
struct RawHeader {
|
|
72
|
+
version: Uint32,
|
|
73
|
+
compactTarget: Uint32,
|
|
74
|
+
timestamp: Uint64,
|
|
75
|
+
number: Uint64,
|
|
76
|
+
epoch: Uint64,
|
|
77
|
+
parentHash: Byte32,
|
|
78
|
+
transactionsRoot: Byte32,
|
|
79
|
+
proposalsHash: Byte32,
|
|
80
|
+
extraHash: Byte32,
|
|
81
|
+
dao: Byte32,
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
struct Header {
|
|
85
|
+
raw: RawHeader,
|
|
86
|
+
nonce: Uint128,
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
table UncleBlock {
|
|
90
|
+
header: Header,
|
|
91
|
+
proposals: ProposalShortIdVec,
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
table Block {
|
|
95
|
+
header: Header,
|
|
96
|
+
uncles: UncleBlockVec,
|
|
97
|
+
transactions: TransactionVec,
|
|
98
|
+
proposals: ProposalShortIdVec,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
table BlockV1 {
|
|
102
|
+
header: Header,
|
|
103
|
+
uncles: UncleBlockVec,
|
|
104
|
+
transactions: TransactionVec,
|
|
105
|
+
proposals: ProposalShortIdVec,
|
|
106
|
+
extension: Bytes,
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
table CellbaseWitness {
|
|
110
|
+
lock: Script,
|
|
111
|
+
message: Bytes,
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
table WitnessArgs {
|
|
115
|
+
lock: BytesOpt, // Lock args
|
|
116
|
+
inputType: BytesOpt, // Type args for input
|
|
117
|
+
outputType: BytesOpt, // Type args for output
|
|
118
|
+
}
|