@ckb-ccc/core 0.1.0 → 0.1.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/CHANGELOG.md +12 -0
- package/dist.commonjs/address/address.advanced.js +28 -23
- package/dist.commonjs/address/advanced.js +17 -1
- package/dist.commonjs/address/index.js +15 -11
- package/dist.commonjs/advanced.js +31 -2
- package/dist.commonjs/advancedBarrel.js +21 -5
- package/dist.commonjs/barrel.js +41 -12
- package/dist.commonjs/bytes/advanced.js +2 -1
- package/dist.commonjs/bytes/index.js +12 -7
- package/dist.commonjs/ckb/advanced.js +32 -3
- package/dist.commonjs/ckb/index.js +19 -3
- package/dist.commonjs/ckb/molecule.advanced/generated.js +131 -64
- package/dist.commonjs/ckb/molecule.advanced/index.js +19 -2
- package/dist.commonjs/ckb/script.advanced.js +6 -3
- package/dist.commonjs/ckb/script.js +53 -23
- package/dist.commonjs/ckb/transaction.advanced.js +6 -3
- package/dist.commonjs/ckb/transaction.js +163 -122
- package/dist.commonjs/ckb/transactionLumos.js +2 -1
- package/dist.commonjs/client/advanced.js +22 -6
- package/dist.commonjs/client/cache/advanced.js +17 -1
- package/dist.commonjs/client/cache/cache.js +7 -3
- package/dist.commonjs/client/cache/index.js +18 -2
- package/dist.commonjs/client/cache/memory.advanced.js +28 -21
- package/dist.commonjs/client/cache/memory.js +23 -19
- package/dist.commonjs/client/client.js +25 -21
- package/dist.commonjs/client/clientPublicMainnet.advanced.js +25 -22
- package/dist.commonjs/client/clientPublicMainnet.js +15 -8
- package/dist.commonjs/client/clientPublicTestnet.advanced.js +25 -22
- package/dist.commonjs/client/clientPublicTestnet.js +15 -8
- package/dist.commonjs/client/clientTypes.advanced.js +6 -3
- package/dist.commonjs/client/clientTypes.js +43 -29
- package/dist.commonjs/client/index.js +22 -6
- package/dist.commonjs/client/jsonRpc/advanced.js +18 -2
- package/dist.commonjs/client/jsonRpc/index.js +41 -37
- package/dist.commonjs/client/jsonRpc/transformers.js +61 -57
- package/dist.commonjs/client/jsonRpc/types.js +2 -1
- package/dist.commonjs/client/transports/advanced.js +25 -8
- package/dist.commonjs/client/transports/http.js +5 -1
- package/dist.commonjs/client/transports/transport.js +2 -1
- package/dist.commonjs/client/transports/webSocket.js +10 -3
- package/dist.commonjs/fixedPoint/index.js +9 -4
- package/dist.commonjs/hasher/advanced.js +4 -1
- package/dist.commonjs/hasher/hasher.js +2 -1
- package/dist.commonjs/hasher/hasherCkb.js +20 -14
- package/dist.commonjs/hasher/hasherKeecak256.js +11 -7
- package/dist.commonjs/hasher/index.js +19 -3
- package/dist.commonjs/hex/index.js +6 -3
- package/dist.commonjs/index.js +31 -2
- package/dist.commonjs/keystore/index.js +25 -21
- package/dist.commonjs/molecule/codec.js +63 -48
- package/dist.commonjs/molecule/index.js +18 -2
- package/dist.commonjs/molecule/predefined.js +103 -77
- package/dist.commonjs/num/index.js +29 -17
- package/dist.commonjs/signer/btc/index.js +19 -3
- package/dist.commonjs/signer/btc/signerBtc.js +24 -20
- package/dist.commonjs/signer/btc/signerBtcPublicKeyReadonly.js +8 -4
- package/dist.commonjs/signer/btc/verify.js +26 -17
- package/dist.commonjs/signer/ckb/index.js +21 -5
- package/dist.commonjs/signer/ckb/signerCkbPrivateKey.js +20 -16
- package/dist.commonjs/signer/ckb/signerCkbPublicKey.js +24 -20
- package/dist.commonjs/signer/ckb/signerCkbScriptReadonly.js +12 -8
- package/dist.commonjs/signer/ckb/verifyCkbSecp256k1.js +16 -12
- package/dist.commonjs/signer/ckb/verifyJoyId.js +8 -5
- package/dist.commonjs/signer/doge/index.js +20 -4
- package/dist.commonjs/signer/doge/signerDoge.js +26 -19
- package/dist.commonjs/signer/doge/signerDogeAddressReadonly.js +6 -2
- package/dist.commonjs/signer/doge/signerDogePrivateKey.js +17 -13
- package/dist.commonjs/signer/doge/verify.js +15 -12
- package/dist.commonjs/signer/dummy/alwaysError.js +6 -2
- package/dist.commonjs/signer/dummy/dummy.js +7 -3
- package/dist.commonjs/signer/dummy/index.js +19 -3
- package/dist.commonjs/signer/dummy/openLink.js +6 -2
- package/dist.commonjs/signer/evm/index.js +19 -3
- package/dist.commonjs/signer/evm/signerEvm.js +35 -31
- package/dist.commonjs/signer/evm/signerEvmAddressReadonly.js +8 -4
- package/dist.commonjs/signer/evm/verify.js +7 -4
- package/dist.commonjs/signer/index.js +23 -7
- package/dist.commonjs/signer/nostr/index.js +18 -2
- package/dist.commonjs/signer/nostr/signerNostr.js +25 -21
- package/dist.commonjs/signer/nostr/verify.js +16 -12
- package/dist.commonjs/signer/signer/index.js +25 -19
- package/dist.commonjs/utils/index.js +12 -6
- package/package.json +6 -2
|
@@ -1,21 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HasherCkb = void 0;
|
|
4
|
+
exports.hashCkb = hashCkb;
|
|
5
|
+
exports.hashTypeId = hashTypeId;
|
|
6
|
+
const blake2b_1 = require("@noble/hashes/blake2b");
|
|
7
|
+
const index_js_1 = require("../bytes/index.js");
|
|
8
|
+
const index_js_2 = require("../ckb/index.js");
|
|
9
|
+
const index_js_3 = require("../hex/index.js");
|
|
10
|
+
const index_js_4 = require("../num/index.js");
|
|
11
|
+
const advanced_js_1 = require("./advanced.js");
|
|
7
12
|
/**
|
|
8
13
|
* @public
|
|
9
14
|
*/
|
|
10
|
-
|
|
15
|
+
class HasherCkb {
|
|
11
16
|
/**
|
|
12
17
|
* Creates an instance of Hasher.
|
|
13
18
|
*
|
|
14
19
|
* @param outLength - The output length of the hash in bytes. Default is 32.
|
|
15
20
|
* @param personal - The personal string for the Blake2b algorithm. Default is CKB_BLAKE2B_PERSONAL.
|
|
16
21
|
*/
|
|
17
|
-
constructor(outLength = 32, personal = CKB_BLAKE2B_PERSONAL) {
|
|
18
|
-
this.hasher = blake2b.create({
|
|
22
|
+
constructor(outLength = 32, personal = advanced_js_1.CKB_BLAKE2B_PERSONAL) {
|
|
23
|
+
this.hasher = blake2b_1.blake2b.create({
|
|
19
24
|
personalization: personal,
|
|
20
25
|
dkLen: outLength,
|
|
21
26
|
});
|
|
@@ -34,7 +39,7 @@ export class HasherCkb {
|
|
|
34
39
|
* ```
|
|
35
40
|
*/
|
|
36
41
|
update(data) {
|
|
37
|
-
this.hasher.update(bytesFrom(data));
|
|
42
|
+
this.hasher.update((0, index_js_1.bytesFrom)(data));
|
|
38
43
|
return this;
|
|
39
44
|
}
|
|
40
45
|
/**
|
|
@@ -50,9 +55,10 @@ export class HasherCkb {
|
|
|
50
55
|
* ```
|
|
51
56
|
*/
|
|
52
57
|
digest() {
|
|
53
|
-
return hexFrom(this.hasher.digest());
|
|
58
|
+
return (0, index_js_3.hexFrom)(this.hasher.digest());
|
|
54
59
|
}
|
|
55
60
|
}
|
|
61
|
+
exports.HasherCkb = HasherCkb;
|
|
56
62
|
/**
|
|
57
63
|
* Computes the CKB hash of the given data using the Blake2b algorithm.
|
|
58
64
|
* @public
|
|
@@ -65,7 +71,7 @@ export class HasherCkb {
|
|
|
65
71
|
* const hash = hashCkb("some data"); // Outputs something like "0x..."
|
|
66
72
|
* ```
|
|
67
73
|
*/
|
|
68
|
-
|
|
74
|
+
function hashCkb(...data) {
|
|
69
75
|
const hasher = new HasherCkb();
|
|
70
76
|
data.forEach((d) => hasher.update(d));
|
|
71
77
|
return hasher.digest();
|
|
@@ -83,6 +89,6 @@ export function hashCkb(...data) {
|
|
|
83
89
|
* const hash = hashTypeId(cellInput, outputIndex); // Outputs something like "0x..."
|
|
84
90
|
* ```
|
|
85
91
|
*/
|
|
86
|
-
|
|
87
|
-
return hashCkb(CellInput.from(cellInputLike).toBytes(), numLeToBytes(outputIndex, 8));
|
|
92
|
+
function hashTypeId(cellInputLike, outputIndex) {
|
|
93
|
+
return hashCkb(index_js_2.CellInput.from(cellInputLike).toBytes(), (0, index_js_4.numLeToBytes)(outputIndex, 8));
|
|
88
94
|
}
|
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HasherKeecak256 = void 0;
|
|
4
|
+
const sha3_1 = require("@noble/hashes/sha3");
|
|
5
|
+
const index_js_1 = require("../bytes/index.js");
|
|
6
|
+
const index_js_2 = require("../hex/index.js");
|
|
4
7
|
/**
|
|
5
8
|
* @public
|
|
6
9
|
*/
|
|
7
|
-
|
|
10
|
+
class HasherKeecak256 {
|
|
8
11
|
/**
|
|
9
12
|
* Creates an instance of Hasher.
|
|
10
13
|
*/
|
|
11
14
|
constructor() {
|
|
12
|
-
this.hasher = keccak_256.create();
|
|
15
|
+
this.hasher = sha3_1.keccak_256.create();
|
|
13
16
|
}
|
|
14
17
|
/**
|
|
15
18
|
* Updates the hash with the given data.
|
|
@@ -25,7 +28,7 @@ export class HasherKeecak256 {
|
|
|
25
28
|
* ```
|
|
26
29
|
*/
|
|
27
30
|
update(data) {
|
|
28
|
-
this.hasher.update(bytesFrom(data));
|
|
31
|
+
this.hasher.update((0, index_js_1.bytesFrom)(data));
|
|
29
32
|
return this;
|
|
30
33
|
}
|
|
31
34
|
/**
|
|
@@ -41,6 +44,7 @@ export class HasherKeecak256 {
|
|
|
41
44
|
* ```
|
|
42
45
|
*/
|
|
43
46
|
digest() {
|
|
44
|
-
return hexFrom(this.hasher.digest());
|
|
47
|
+
return (0, index_js_2.hexFrom)(this.hasher.digest());
|
|
45
48
|
}
|
|
46
49
|
}
|
|
50
|
+
exports.HasherKeecak256 = HasherKeecak256;
|
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./hasher.js"), exports);
|
|
18
|
+
__exportStar(require("./hasherCkb.js"), exports);
|
|
19
|
+
__exportStar(require("./hasherKeecak256.js"), exports);
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hexFrom = hexFrom;
|
|
4
|
+
const index_js_1 = require("../bytes/index.js");
|
|
2
5
|
/**
|
|
3
6
|
* Converts a HexLike value to a Hex string.
|
|
4
7
|
* @public
|
|
@@ -12,6 +15,6 @@ import { bytesFrom, bytesTo } from "../bytes/index.js";
|
|
|
12
15
|
* const hexStringFromBytes = hexFrom(new Uint8Array([104, 101, 108, 108, 111])); // Outputs "0x68656c6c6f"
|
|
13
16
|
* ```
|
|
14
17
|
*/
|
|
15
|
-
|
|
16
|
-
return `0x${bytesTo(bytesFrom(hex), "hex")}`;
|
|
18
|
+
function hexFrom(hex) {
|
|
19
|
+
return `0x${(0, index_js_1.bytesTo)((0, index_js_1.bytesFrom)(hex), "hex")}`;
|
|
17
20
|
}
|
package/dist.commonjs/index.js
CHANGED
|
@@ -1,2 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
19
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
20
|
+
};
|
|
21
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
22
|
+
if (mod && mod.__esModule) return mod;
|
|
23
|
+
var result = {};
|
|
24
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
25
|
+
__setModuleDefault(result, mod);
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.ccc = void 0;
|
|
30
|
+
__exportStar(require("./barrel.js"), exports);
|
|
31
|
+
exports.ccc = __importStar(require("./barrel.js"));
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.keystoreEncrypt = keystoreEncrypt;
|
|
4
|
+
exports.keystoreDecrypt = keystoreDecrypt;
|
|
5
|
+
const aes_1 = require("@noble/ciphers/aes");
|
|
6
|
+
const scrypt_1 = require("@noble/hashes/scrypt");
|
|
7
|
+
const sha3_1 = require("@noble/hashes/sha3");
|
|
8
|
+
const utils_1 = require("@noble/hashes/utils");
|
|
9
|
+
const index_js_1 = require("../bytes/index.js");
|
|
10
|
+
const index_js_2 = require("../hex/index.js");
|
|
7
11
|
// The parameter r ("blockSize")
|
|
8
12
|
const DEFAULT_SCRYPT_PARAM_R = 8;
|
|
9
13
|
// The parallelization parameter p
|
|
@@ -11,35 +15,35 @@ const DEFAULT_SCRYPT_PARAM_P = 1;
|
|
|
11
15
|
// The CPU/Memory cost parameter N
|
|
12
16
|
const DEFAULT_SCRYPT_PARAM_N = 262144;
|
|
13
17
|
function mac(derivedKey, cipherText) {
|
|
14
|
-
return hexFrom(keccak_256(bytesConcat(derivedKey.slice(16, 32), cipherText))).slice(2);
|
|
18
|
+
return (0, index_js_2.hexFrom)((0, sha3_1.keccak_256)((0, index_js_1.bytesConcat)(derivedKey.slice(16, 32), cipherText))).slice(2);
|
|
15
19
|
}
|
|
16
20
|
/**
|
|
17
21
|
* @public
|
|
18
22
|
*/
|
|
19
|
-
|
|
20
|
-
const salt = randomBytes(32);
|
|
21
|
-
const iv = randomBytes(16);
|
|
23
|
+
async function keystoreEncrypt(privateKeyLike, chainCodeLike, password) {
|
|
24
|
+
const salt = (0, utils_1.randomBytes)(32);
|
|
25
|
+
const iv = (0, utils_1.randomBytes)(16);
|
|
22
26
|
const kdfparams = {
|
|
23
27
|
dklen: 32,
|
|
24
|
-
salt: hexFrom(salt).slice(2),
|
|
28
|
+
salt: (0, index_js_2.hexFrom)(salt).slice(2),
|
|
25
29
|
n: DEFAULT_SCRYPT_PARAM_N,
|
|
26
30
|
r: DEFAULT_SCRYPT_PARAM_R,
|
|
27
31
|
p: DEFAULT_SCRYPT_PARAM_P,
|
|
28
32
|
};
|
|
29
|
-
const derivedKey = await scryptAsync(bytesFrom(password, "utf8"), salt, {
|
|
33
|
+
const derivedKey = await (0, scrypt_1.scryptAsync)((0, index_js_1.bytesFrom)(password, "utf8"), salt, {
|
|
30
34
|
N: kdfparams.n,
|
|
31
35
|
r: kdfparams.r,
|
|
32
36
|
p: kdfparams.p,
|
|
33
37
|
dkLen: kdfparams.dklen,
|
|
34
38
|
});
|
|
35
|
-
const cipher = ctr(derivedKey.slice(0, 16), iv.map((v) => v));
|
|
36
|
-
const ciphertext = cipher.encrypt(bytesConcat(bytesFrom(privateKeyLike), bytesFrom(chainCodeLike)));
|
|
39
|
+
const cipher = (0, aes_1.ctr)(derivedKey.slice(0, 16), iv.map((v) => v));
|
|
40
|
+
const ciphertext = cipher.encrypt((0, index_js_1.bytesConcat)((0, index_js_1.bytesFrom)(privateKeyLike), (0, index_js_1.bytesFrom)(chainCodeLike)));
|
|
37
41
|
return {
|
|
38
|
-
id: hexFrom(randomBytes(16)).slice(2),
|
|
42
|
+
id: (0, index_js_2.hexFrom)((0, utils_1.randomBytes)(16)).slice(2),
|
|
39
43
|
crypto: {
|
|
40
|
-
ciphertext: hexFrom(ciphertext).slice(2),
|
|
44
|
+
ciphertext: (0, index_js_2.hexFrom)(ciphertext).slice(2),
|
|
41
45
|
cipherparams: {
|
|
42
|
-
iv: hexFrom(iv).slice(2),
|
|
46
|
+
iv: (0, index_js_2.hexFrom)(iv).slice(2),
|
|
43
47
|
},
|
|
44
48
|
cipher: "aes-128-ctr",
|
|
45
49
|
kdf: "scrypt",
|
|
@@ -51,7 +55,7 @@ export async function keystoreEncrypt(privateKeyLike, chainCodeLike, password) {
|
|
|
51
55
|
/**
|
|
52
56
|
* @public
|
|
53
57
|
*/
|
|
54
|
-
|
|
58
|
+
async function keystoreDecrypt(keystore, password) {
|
|
55
59
|
if (typeof keystore !== "object" ||
|
|
56
60
|
keystore === null ||
|
|
57
61
|
!("crypto" in keystore)) {
|
|
@@ -87,17 +91,17 @@ export async function keystoreDecrypt(keystore, password) {
|
|
|
87
91
|
typeof kdfparams.salt !== "string") {
|
|
88
92
|
throw Error("Invalid kdfparams");
|
|
89
93
|
}
|
|
90
|
-
const derivedKey = await scryptAsync(bytesFrom(password, "utf8"), bytesFrom(kdfparams.salt), {
|
|
94
|
+
const derivedKey = await (0, scrypt_1.scryptAsync)((0, index_js_1.bytesFrom)(password, "utf8"), (0, index_js_1.bytesFrom)(kdfparams.salt), {
|
|
91
95
|
N: kdfparams.n,
|
|
92
96
|
r: kdfparams.r,
|
|
93
97
|
p: kdfparams.p,
|
|
94
98
|
dkLen: kdfparams.dklen,
|
|
95
99
|
});
|
|
96
|
-
const ciphertext = bytesFrom(crypto.ciphertext);
|
|
100
|
+
const ciphertext = (0, index_js_1.bytesFrom)(crypto.ciphertext);
|
|
97
101
|
if (mac(derivedKey, ciphertext) !== crypto.mac) {
|
|
98
102
|
throw Error("Invalid password");
|
|
99
103
|
}
|
|
100
|
-
const cipher = ctr(derivedKey.slice(0, 16), bytesFrom(crypto.cipherparams.iv));
|
|
104
|
+
const cipher = (0, aes_1.ctr)(derivedKey.slice(0, 16), (0, index_js_1.bytesFrom)(crypto.cipherparams.iv));
|
|
101
105
|
const result = cipher.decrypt(ciphertext);
|
|
102
106
|
return {
|
|
103
107
|
privateKey: result.slice(0, 32),
|
|
@@ -1,7 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Codec = void 0;
|
|
5
|
+
exports.fixedItemVec = fixedItemVec;
|
|
6
|
+
exports.dynItemVec = dynItemVec;
|
|
7
|
+
exports.vector = vector;
|
|
8
|
+
exports.option = option;
|
|
9
|
+
exports.byteVec = byteVec;
|
|
10
|
+
exports.table = table;
|
|
11
|
+
exports.union = union;
|
|
12
|
+
exports.struct = struct;
|
|
13
|
+
exports.array = array;
|
|
14
|
+
exports.uint = uint;
|
|
15
|
+
exports.uintNumber = uintNumber;
|
|
16
|
+
const index_js_1 = require("../bytes/index.js");
|
|
17
|
+
const index_js_2 = require("../num/index.js");
|
|
18
|
+
class Codec {
|
|
5
19
|
constructor(encode, decode, byteLength) {
|
|
6
20
|
this.encode = encode;
|
|
7
21
|
this.decode = decode;
|
|
@@ -20,17 +34,18 @@ export class Codec {
|
|
|
20
34
|
});
|
|
21
35
|
}
|
|
22
36
|
}
|
|
37
|
+
exports.Codec = Codec;
|
|
23
38
|
function uint32To(numLike) {
|
|
24
|
-
return numToBytes(numLike, 4);
|
|
39
|
+
return (0, index_js_2.numToBytes)(numLike, 4);
|
|
25
40
|
}
|
|
26
41
|
function uint32From(bytesLike) {
|
|
27
|
-
return Number(numFromBytes(bytesLike));
|
|
42
|
+
return Number((0, index_js_2.numFromBytes)(bytesLike));
|
|
28
43
|
}
|
|
29
44
|
/**
|
|
30
45
|
* Vector with fixed size item codec
|
|
31
46
|
* @param itemCodec fixed-size vector item codec
|
|
32
47
|
*/
|
|
33
|
-
|
|
48
|
+
function fixedItemVec(itemCodec) {
|
|
34
49
|
const itemByteLength = itemCodec.byteLength;
|
|
35
50
|
if (itemByteLength === undefined) {
|
|
36
51
|
throw new Error("fixedItemVec: itemCodec requires a byte length");
|
|
@@ -38,14 +53,14 @@ export function fixedItemVec(itemCodec) {
|
|
|
38
53
|
return Codec.from({
|
|
39
54
|
encode(userDefinedItems) {
|
|
40
55
|
try {
|
|
41
|
-
return userDefinedItems.reduce((concatted, item) => bytesConcat(concatted, itemCodec.encode(item)), uint32To(userDefinedItems.length));
|
|
56
|
+
return userDefinedItems.reduce((concatted, item) => (0, index_js_1.bytesConcat)(concatted, itemCodec.encode(item)), uint32To(userDefinedItems.length));
|
|
42
57
|
}
|
|
43
58
|
catch (e) {
|
|
44
59
|
throw new Error(`fixedItemVec(${e?.toString()})`);
|
|
45
60
|
}
|
|
46
61
|
},
|
|
47
62
|
decode(buffer) {
|
|
48
|
-
const value = bytesFrom(buffer);
|
|
63
|
+
const value = (0, index_js_1.bytesFrom)(buffer);
|
|
49
64
|
if (value.byteLength < 4) {
|
|
50
65
|
throw new Error(`fixedItemVec: too short buffer, expected at least 4 bytes, but got ${value.byteLength}`);
|
|
51
66
|
}
|
|
@@ -71,7 +86,7 @@ export function fixedItemVec(itemCodec) {
|
|
|
71
86
|
* Vector with dynamic size item codec, you can create a recursive vector with this function
|
|
72
87
|
* @param itemCodec the vector item codec. It can be fixed-size or dynamic-size.
|
|
73
88
|
*/
|
|
74
|
-
|
|
89
|
+
function dynItemVec(itemCodec) {
|
|
75
90
|
return Codec.from({
|
|
76
91
|
encode(userDefinedItems) {
|
|
77
92
|
try {
|
|
@@ -79,24 +94,24 @@ export function dynItemVec(itemCodec) {
|
|
|
79
94
|
const encodedItem = itemCodec.encode(item);
|
|
80
95
|
const packedHeader = uint32To(offset);
|
|
81
96
|
return {
|
|
82
|
-
header: bytesConcat(header, packedHeader),
|
|
83
|
-
body: bytesConcat(body, encodedItem),
|
|
84
|
-
offset: offset + bytesFrom(encodedItem).byteLength,
|
|
97
|
+
header: (0, index_js_1.bytesConcat)(header, packedHeader),
|
|
98
|
+
body: (0, index_js_1.bytesConcat)(body, encodedItem),
|
|
99
|
+
offset: offset + (0, index_js_1.bytesFrom)(encodedItem).byteLength,
|
|
85
100
|
};
|
|
86
101
|
}, {
|
|
87
|
-
header: bytesFrom([]),
|
|
88
|
-
body: bytesFrom([]),
|
|
102
|
+
header: (0, index_js_1.bytesFrom)([]),
|
|
103
|
+
body: (0, index_js_1.bytesFrom)([]),
|
|
89
104
|
offset: 4 + userDefinedItems.length * 4,
|
|
90
105
|
});
|
|
91
106
|
const packedTotalSize = uint32To(encoded.header.byteLength + encoded.body.byteLength + 4);
|
|
92
|
-
return bytesConcat(packedTotalSize, encoded.header, encoded.body);
|
|
107
|
+
return (0, index_js_1.bytesConcat)(packedTotalSize, encoded.header, encoded.body);
|
|
93
108
|
}
|
|
94
109
|
catch (e) {
|
|
95
110
|
throw new Error(`dynItemVec(${e?.toString()})`);
|
|
96
111
|
}
|
|
97
112
|
},
|
|
98
113
|
decode(buffer) {
|
|
99
|
-
const value = bytesFrom(buffer);
|
|
114
|
+
const value = (0, index_js_1.bytesFrom)(buffer);
|
|
100
115
|
const byteLength = uint32From(value.slice(0, 4));
|
|
101
116
|
if (byteLength !== value.byteLength) {
|
|
102
117
|
throw new Error(`dynItemVec: invalid buffer size, expected ${byteLength}, but got ${value.byteLength}`);
|
|
@@ -128,7 +143,7 @@ export function dynItemVec(itemCodec) {
|
|
|
128
143
|
* General vector codec, if `itemCodec` is fixed size type, it will create a fixvec codec, otherwise a dynvec codec will be created.
|
|
129
144
|
* @param itemCodec
|
|
130
145
|
*/
|
|
131
|
-
|
|
146
|
+
function vector(itemCodec) {
|
|
132
147
|
if (itemCodec.byteLength !== undefined) {
|
|
133
148
|
return fixedItemVec(itemCodec);
|
|
134
149
|
}
|
|
@@ -141,11 +156,11 @@ export function vector(itemCodec) {
|
|
|
141
156
|
* - if it's not empty, just serialize the inner item (the size is same as the inner item's size).
|
|
142
157
|
* @param innerCodec
|
|
143
158
|
*/
|
|
144
|
-
|
|
159
|
+
function option(innerCodec) {
|
|
145
160
|
return Codec.from({
|
|
146
161
|
encode(userDefinedOrNull) {
|
|
147
162
|
if (!userDefinedOrNull) {
|
|
148
|
-
return bytesFrom([]);
|
|
163
|
+
return (0, index_js_1.bytesFrom)([]);
|
|
149
164
|
}
|
|
150
165
|
try {
|
|
151
166
|
return innerCodec.encode(userDefinedOrNull);
|
|
@@ -155,7 +170,7 @@ export function option(innerCodec) {
|
|
|
155
170
|
}
|
|
156
171
|
},
|
|
157
172
|
decode(buffer) {
|
|
158
|
-
const value = bytesFrom(buffer);
|
|
173
|
+
const value = (0, index_js_1.bytesFrom)(buffer);
|
|
159
174
|
if (value.byteLength === 0) {
|
|
160
175
|
return undefined;
|
|
161
176
|
}
|
|
@@ -172,20 +187,20 @@ export function option(innerCodec) {
|
|
|
172
187
|
* Wrap the encoded value with a fixed-length buffer
|
|
173
188
|
* @param codec
|
|
174
189
|
*/
|
|
175
|
-
|
|
190
|
+
function byteVec(codec) {
|
|
176
191
|
return Codec.from({
|
|
177
192
|
encode(userDefined) {
|
|
178
193
|
try {
|
|
179
|
-
const payload = bytesFrom(codec.encode(userDefined));
|
|
194
|
+
const payload = (0, index_js_1.bytesFrom)(codec.encode(userDefined));
|
|
180
195
|
const byteLength = uint32To(payload.byteLength);
|
|
181
|
-
return bytesConcat(byteLength, payload);
|
|
196
|
+
return (0, index_js_1.bytesConcat)(byteLength, payload);
|
|
182
197
|
}
|
|
183
198
|
catch (e) {
|
|
184
199
|
throw new Error(`byteVec(${e?.toString()})`);
|
|
185
200
|
}
|
|
186
201
|
},
|
|
187
202
|
decode(buffer) {
|
|
188
|
-
const value = bytesFrom(buffer);
|
|
203
|
+
const value = (0, index_js_1.bytesFrom)(buffer);
|
|
189
204
|
if (value.byteLength < 4) {
|
|
190
205
|
throw new Error(`byteVec: too short buffer, expected at least 4 bytes, but got ${value.byteLength}`);
|
|
191
206
|
}
|
|
@@ -206,7 +221,7 @@ export function byteVec(codec) {
|
|
|
206
221
|
* Table is a dynamic-size type. It can be considered as a dynvec but the length is fixed.
|
|
207
222
|
* @param codecLayout
|
|
208
223
|
*/
|
|
209
|
-
|
|
224
|
+
function table(codecLayout) {
|
|
210
225
|
const keys = Object.keys(codecLayout);
|
|
211
226
|
return Codec.from({
|
|
212
227
|
encode(object) {
|
|
@@ -216,24 +231,24 @@ export function table(codecLayout) {
|
|
|
216
231
|
const encodedItem = codecLayout[key].encode(object[key]);
|
|
217
232
|
const packedOffset = uint32To(result.offset);
|
|
218
233
|
return {
|
|
219
|
-
header: bytesConcat(result.header, packedOffset),
|
|
220
|
-
body: bytesConcat(result.body, encodedItem),
|
|
221
|
-
offset: result.offset + bytesFrom(encodedItem).byteLength,
|
|
234
|
+
header: (0, index_js_1.bytesConcat)(result.header, packedOffset),
|
|
235
|
+
body: (0, index_js_1.bytesConcat)(result.body, encodedItem),
|
|
236
|
+
offset: result.offset + (0, index_js_1.bytesFrom)(encodedItem).byteLength,
|
|
222
237
|
};
|
|
223
238
|
}
|
|
224
239
|
catch (e) {
|
|
225
240
|
throw new Error(`table.${key}(${e?.toString()})`);
|
|
226
241
|
}
|
|
227
242
|
}, {
|
|
228
|
-
header: bytesFrom([]),
|
|
229
|
-
body: bytesFrom([]),
|
|
243
|
+
header: (0, index_js_1.bytesFrom)([]),
|
|
244
|
+
body: (0, index_js_1.bytesFrom)([]),
|
|
230
245
|
offset: headerLength,
|
|
231
246
|
});
|
|
232
247
|
const packedTotalSize = uint32To(header.byteLength + body.byteLength + 4);
|
|
233
|
-
return bytesConcat(packedTotalSize, header, body);
|
|
248
|
+
return (0, index_js_1.bytesConcat)(packedTotalSize, header, body);
|
|
234
249
|
},
|
|
235
250
|
decode(buffer) {
|
|
236
|
-
const value = bytesFrom(buffer);
|
|
251
|
+
const value = (0, index_js_1.bytesFrom)(buffer);
|
|
237
252
|
const byteLength = uint32From(value.slice(0, 4));
|
|
238
253
|
if (byteLength !== value.byteLength) {
|
|
239
254
|
throw new Error(`table: invalid buffer size, expected ${byteLength}, but got ${value.byteLength}`);
|
|
@@ -275,7 +290,7 @@ export function table(codecLayout) {
|
|
|
275
290
|
* // with custom id
|
|
276
291
|
* union({ cafe: Uint8, bee: Uint8 }, { cafe: 0xcafe, bee: 0xbee })
|
|
277
292
|
*/
|
|
278
|
-
|
|
293
|
+
function union(codecLayout, fields) {
|
|
279
294
|
const keys = Object.keys(codecLayout);
|
|
280
295
|
return Codec.from({
|
|
281
296
|
encode({ type, value }) {
|
|
@@ -291,14 +306,14 @@ export function union(codecLayout, fields) {
|
|
|
291
306
|
const header = uint32To(fieldId);
|
|
292
307
|
try {
|
|
293
308
|
const body = codec.encode(value);
|
|
294
|
-
return bytesConcat(header, body);
|
|
309
|
+
return (0, index_js_1.bytesConcat)(header, body);
|
|
295
310
|
}
|
|
296
311
|
catch (e) {
|
|
297
312
|
throw new Error(`union.(${typeStr})(${e?.toString()})`);
|
|
298
313
|
}
|
|
299
314
|
},
|
|
300
315
|
decode(buffer) {
|
|
301
|
-
const value = bytesFrom(buffer);
|
|
316
|
+
const value = (0, index_js_1.bytesFrom)(buffer);
|
|
302
317
|
const fieldIndex = uint32From(value.slice(0, 4));
|
|
303
318
|
const keys = Object.keys(codecLayout);
|
|
304
319
|
const field = (() => {
|
|
@@ -328,7 +343,7 @@ export function union(codecLayout, fields) {
|
|
|
328
343
|
* The size of a struct is the sum of all fields' size.
|
|
329
344
|
* @param codecLayout a object contains all fields' codec
|
|
330
345
|
*/
|
|
331
|
-
|
|
346
|
+
function struct(codecLayout) {
|
|
332
347
|
const codecArray = Object.values(codecLayout);
|
|
333
348
|
if (codecArray.some((codec) => codec.byteLength === undefined)) {
|
|
334
349
|
throw new Error("struct: all fields must be fixed-size");
|
|
@@ -340,15 +355,15 @@ export function struct(codecLayout) {
|
|
|
340
355
|
return keys.reduce((result, key) => {
|
|
341
356
|
try {
|
|
342
357
|
const encodedItem = codecLayout[key].encode(object[key]);
|
|
343
|
-
return bytesConcat(result, encodedItem);
|
|
358
|
+
return (0, index_js_1.bytesConcat)(result, encodedItem);
|
|
344
359
|
}
|
|
345
360
|
catch (e) {
|
|
346
361
|
throw new Error(`struct.${key}(${e?.toString()})`);
|
|
347
362
|
}
|
|
348
|
-
}, bytesFrom([]));
|
|
363
|
+
}, (0, index_js_1.bytesFrom)([]));
|
|
349
364
|
},
|
|
350
365
|
decode(buffer) {
|
|
351
|
-
const value = bytesFrom(buffer);
|
|
366
|
+
const value = (0, index_js_1.bytesFrom)(buffer);
|
|
352
367
|
const object = {};
|
|
353
368
|
let offset = 0;
|
|
354
369
|
Object.entries(codecLayout).forEach(([key, codec]) => {
|
|
@@ -372,7 +387,7 @@ export function struct(codecLayout) {
|
|
|
372
387
|
* @param itemCodec the fixed-size array item codec
|
|
373
388
|
* @param itemCount
|
|
374
389
|
*/
|
|
375
|
-
|
|
390
|
+
function array(itemCodec, itemCount) {
|
|
376
391
|
if (itemCodec.byteLength === undefined) {
|
|
377
392
|
throw new Error("array: itemCodec requires a byte length");
|
|
378
393
|
}
|
|
@@ -381,14 +396,14 @@ export function array(itemCodec, itemCount) {
|
|
|
381
396
|
byteLength,
|
|
382
397
|
encode(items) {
|
|
383
398
|
try {
|
|
384
|
-
return items.reduce((concatted, item) => bytesConcat(concatted, itemCodec.encode(item)), bytesFrom([]));
|
|
399
|
+
return items.reduce((concatted, item) => (0, index_js_1.bytesConcat)(concatted, itemCodec.encode(item)), (0, index_js_1.bytesFrom)([]));
|
|
385
400
|
}
|
|
386
401
|
catch (e) {
|
|
387
402
|
throw new Error(`array(${e?.toString()})`);
|
|
388
403
|
}
|
|
389
404
|
},
|
|
390
405
|
decode(buffer) {
|
|
391
|
-
const value = bytesFrom(buffer);
|
|
406
|
+
const value = (0, index_js_1.bytesFrom)(buffer);
|
|
392
407
|
if (value.byteLength != byteLength) {
|
|
393
408
|
throw new Error(`array: invalid buffer size, expected ${byteLength}, but got ${value.byteLength}`);
|
|
394
409
|
}
|
|
@@ -410,23 +425,23 @@ export function array(itemCodec, itemCount) {
|
|
|
410
425
|
* @param byteLength
|
|
411
426
|
* @param littleEndian
|
|
412
427
|
*/
|
|
413
|
-
|
|
428
|
+
function uint(byteLength, littleEndian = false) {
|
|
414
429
|
return Codec.from({
|
|
415
430
|
byteLength,
|
|
416
431
|
encode: (numLike) => {
|
|
417
432
|
if (littleEndian) {
|
|
418
|
-
return numToBytes(numLike, byteLength);
|
|
433
|
+
return (0, index_js_2.numToBytes)(numLike, byteLength);
|
|
419
434
|
}
|
|
420
435
|
else {
|
|
421
|
-
return numBeToBytes(numLike, byteLength);
|
|
436
|
+
return (0, index_js_2.numBeToBytes)(numLike, byteLength);
|
|
422
437
|
}
|
|
423
438
|
},
|
|
424
439
|
decode: (buffer) => {
|
|
425
440
|
if (littleEndian) {
|
|
426
|
-
return numFromBytes(buffer);
|
|
441
|
+
return (0, index_js_2.numFromBytes)(buffer);
|
|
427
442
|
}
|
|
428
443
|
else {
|
|
429
|
-
return numBeFromBytes(buffer);
|
|
444
|
+
return (0, index_js_2.numBeFromBytes)(buffer);
|
|
430
445
|
}
|
|
431
446
|
},
|
|
432
447
|
});
|
|
@@ -436,7 +451,7 @@ export function uint(byteLength, littleEndian = false) {
|
|
|
436
451
|
* @param byteLength
|
|
437
452
|
* @param littleEndian
|
|
438
453
|
*/
|
|
439
|
-
|
|
454
|
+
function uintNumber(byteLength, littleEndian = false) {
|
|
440
455
|
if (byteLength > 4) {
|
|
441
456
|
throw new Error("uintNumber: byteLength must be less than or equal to 4");
|
|
442
457
|
}
|
|
@@ -1,2 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./codec.js"), exports);
|
|
18
|
+
__exportStar(require("./predefined.js"), exports);
|