Skip to content

Commit d4c2cba

Browse files
authored
Merge pull request #701 from ckb-js/replace-node-crypto-randomBytes
feat: add lumos crypto.randomBytes to replace node's
2 parents 8d11c75 + 57977b8 commit d4c2cba

38 files changed

+198
-51
lines changed

.changeset/tough-beers-roll.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@ckb-lumos/crypto": minor
3+
"@ckb-lumos/lumos": minor
4+
---
5+
6+
feat: add crypto.randomBytes to replace Node's crypto.randomBytes

.github/.codecov.yml

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ coverage:
1212
target: auto
1313
threshold: 1%
1414
base: auto
15+
patch:
16+
default:
17+
# basic settings
18+
target: auto
19+
threshold: 1%
20+
base: auto
1521

1622
parsers:
1723
gcov:

.github/workflows/ci.yaml

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ jobs:
99
- uses: pnpm/action-setup@v2
1010
with:
1111
version: 8.6.1
12-
- uses: actions/setup-node@v3
12+
- uses: actions/setup-node@v4
1313
with:
1414
cache: "pnpm"
15+
node-version: '18'
1516

1617
- name: Install project dependencies and build
1718
run: |
@@ -38,9 +39,10 @@ jobs:
3839
- uses: pnpm/action-setup@v2
3940
with:
4041
version: 8.6.1
41-
- uses: actions/setup-node@v3
42+
- uses: actions/setup-node@v4
4243
with:
4344
cache: "pnpm"
45+
node-version: '18'
4446

4547
- name: Install project dependencies and build
4648
run: |
@@ -60,7 +62,7 @@ jobs:
6062
- uses: pnpm/action-setup@v2
6163
with:
6264
version: 8.6.1
63-
- uses: actions/setup-node@v3
65+
- uses: actions/setup-node@v4
6466
with:
6567
cache: "pnpm"
6668

commitlint.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const scopeEnumValues = [
22
"base",
33
"bi",
4+
"crypto",
45
"ckb-indexer",
56
"common-scripts",
67
"config-manager",

packages/ckb-indexer/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"events": "^3.3.0"
2929
},
3030
"devDependencies": {
31+
"@ckb-lumos/crypto": "0.23.0",
3132
"@ckb-lumos/testkit": "0.23.0",
3233
"@types/lodash.uniqby": "^4.7.7",
3334
"@types/request": "^2.48.8",

packages/ckb-indexer/tests/transaction_collector.unit.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { randomBytes } from "crypto";
21
import test from "ava";
2+
import { randomBytes } from "@ckb-lumos/crypto";
33
import { Indexer, TransactionCollector } from "../src";
44
import {
55
indexerTransactionListThatHaveOneIoTypeInput,

packages/codec/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
]
4646
},
4747
"devDependencies": {
48+
"@ckb-lumos/crypto": "0.23.0",
4849
"escape-string-regexp": "^4.0.0",
4950
"js-yaml": "^4.1.0"
5051
}

packages/codec/tests/bytes-like.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from "ava";
22
import { molecule, number } from "../src";
33
import { Byte32 } from "../src/blockchain";
4-
import { randomBytes } from "crypto";
4+
import { randomBytes } from "@ckb-lumos/crypto";
55
import { equal, concat, hexify } from "../src/bytes";
66
import { BI } from "@ckb-lumos/bi";
77
import { bytify } from "../lib/bytes";

packages/common-scripts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
]
5656
},
5757
"devDependencies": {
58+
"@ckb-lumos/crypto": "0.23.0",
5859
"@ckb-lumos/debugger": "0.23.0",
5960
"@ckb-lumos/hd": "0.23.0",
6061
"@types/keccak": "^3.0.1",

packages/common-scripts/tests/dao-with-custom-lock.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { dao } from "../src";
1313
import { Config, predefined } from "@ckb-lumos/config-manager";
1414
import { hexify } from "@ckb-lumos/codec/lib/bytes";
1515
import { Uint64 } from "@ckb-lumos/codec/lib/number";
16-
import { randomBytes } from "node:crypto";
16+
import { randomBytes } from "@ckb-lumos/crypto";
1717

1818
const { LINA } = predefined;
1919

packages/config-manager/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
"@types/deep-freeze-strict": "^1.1.0",
4242
"deep-freeze-strict": "^1.1.1"
4343
},
44+
"devDependencies": {
45+
"@ckb-lumos/crypto": "0.23.0"
46+
},
4447
"publishConfig": {
4548
"access": "public"
4649
},

packages/config-manager/tests/refresh.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import test from "ava";
2+
import { spy } from "sinon";
23
import {
34
createLatestTypeIdResolver,
45
createRpcResolver,
56
FetchOutputsByTxHashes,
67
refreshScriptConfigs,
78
} from "../src/refresh";
8-
import { ScriptConfigs } from "../src";
99
import { hexify } from "@ckb-lumos/codec/lib/bytes";
10-
import { randomBytes } from "node:crypto";
1110
import { OutPoint, Output, Script } from "@ckb-lumos/base";
12-
import { spy } from "sinon";
11+
import { randomBytes } from "@ckb-lumos/crypto";
12+
import { ScriptConfigs } from "../src";
1313

1414
test("refresh without update", async (t) => {
1515
const scriptConfigs: ScriptConfigs = {

packages/crypto/.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib

packages/crypto/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib

packages/crypto/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# @ckb-lumos/crypto

packages/crypto/READMD.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `@ckb-lumos/crypto`
2+
3+
Crypto utilities used in Lumos.

packages/crypto/package.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "@ckb-lumos/crypto",
3+
"version": "0.23.0",
4+
"description": "Crypto utilities used in Lumos",
5+
"author": "Tom Wang <[email protected]@gmail.com>",
6+
"homepage": "https://github.com/ckb-js/lumos#readme",
7+
"license": "MIT",
8+
"main": "lib/index.js",
9+
"types": "lib/index.d.ts",
10+
"engines": {
11+
"node": ">=18.0.0"
12+
},
13+
"directories": {
14+
"lib": "lib",
15+
"test": "tests"
16+
},
17+
"files": [
18+
"lib",
19+
"index.d.ts"
20+
],
21+
"repository": {
22+
"type": "git",
23+
"url": "git+https://github.com/ckb-js/lumos.git"
24+
},
25+
"scripts": {
26+
"build": "pnpm run build:types && pnpm run build:js",
27+
"build:types": "tsc --declaration --emitDeclarationOnly",
28+
"build:js": "babel --root-mode upward src --out-dir lib --extensions .ts -s",
29+
"fmt": "prettier --write \"{src,tests,examples}/**/*.ts\" package.json",
30+
"lint": "eslint -c ../../.eslintrc.js \"{src,tests,examples}/**/*.ts\"",
31+
"clean": "shx rm -rf lib",
32+
"test": "ava **/*.test.{js,ts} --timeout=2m"
33+
},
34+
"bugs": {
35+
"url": "https://github.com/ckb-js/lumos/issues"
36+
},
37+
"ava": {
38+
"extensions": [
39+
"ts",
40+
"js"
41+
],
42+
"require": [
43+
"ts-node/register"
44+
]
45+
},
46+
"dependencies": {
47+
"@noble/hashes": "^1.4.0"
48+
},
49+
"publishConfig": {
50+
"access": "public"
51+
}
52+
}

packages/crypto/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { randomBytes } from "@noble/hashes/utils";

packages/crypto/tests/index.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from "ava";
2+
import { randomBytes } from "../src";
3+
4+
test("randomBytes", (t) => {
5+
const size = 32;
6+
const bytes = randomBytes(size);
7+
t.is(bytes.length, size);
8+
t.is(bytes.byteLength, size);
9+
t.is(bytes.constructor, Uint8Array);
10+
});

packages/crypto/tsconfig.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "lib",
5+
"sourceMap": true
6+
},
7+
"include": ["src"]
8+
}

packages/crypto/typedoc.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": ["../../typedoc.base.json"],
3+
"entryPoints": ["src/index.ts"]
4+
}

packages/debugger/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@ckb-lumos/base": "0.23.0",
2525
"@ckb-lumos/bi": "0.23.0",
2626
"@ckb-lumos/codec": "0.23.0",
27+
"@ckb-lumos/crypto": "0.23.0",
2728
"@ckb-lumos/config-manager": "0.23.0",
2829
"@ckb-lumos/helpers": "0.23.0",
2930
"@ckb-lumos/rpc": "0.23.0",

packages/debugger/src/context.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { randomBytes } from "@ckb-lumos/crypto";
12
import { Header, OutPoint } from "@ckb-lumos/base";
2-
import * as crypto from "crypto";
33
import { ScriptConfig } from "@ckb-lumos/config-manager";
44
import { OutputDataLoader } from "./loader";
55
import { DataLoader, TestContext } from "./types";
@@ -14,9 +14,8 @@ const TRANSACTION_HASH_LENGTH = 32;
1414

1515
export function mockOutPoint(): OutPoint {
1616
return {
17-
txHash: hexify(crypto.randomBytes(TRANSACTION_HASH_LENGTH)),
18-
index:
19-
"0x" + Uint32.unpack(crypto.randomBytes(Uint32.byteLength)).toString(16),
17+
txHash: hexify(randomBytes(TRANSACTION_HASH_LENGTH)),
18+
index: "0x" + Uint32.unpack(randomBytes(Uint32.byteLength)).toString(16),
2019
};
2120
}
2221

packages/debugger/src/executor.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { DataLoader, ExecuteResult, Executor } from "./types";
22
import { TransactionSkeletonType } from "@ckb-lumos/helpers";
3+
import { randomBytes } from "@ckb-lumos/crypto";
34
import { spawnSync } from "child_process";
45
import { Hash } from "@ckb-lumos/base";
56
import * as fs from "fs";
67
import * as os from "os";
78
import * as path from "path";
89
import { parseDebuggerData, parseDebuggerMessage } from "./parse";
9-
import * as crypto from 'crypto';
1010

1111
interface DebuggerOptions {
1212
readonly loader: DataLoader;
@@ -51,8 +51,9 @@ export class CKBDebugger implements Executor {
5151
*/
5252
private saveTmpTxFile(txSkeleton: TransactionSkeletonType): string {
5353
const debuggerData = parseDebuggerData(txSkeleton, this.loader);
54-
const randomHex = crypto.randomBytes(18).toString('hex');
55-
const tempFileName = `lumos-debugger-data-${randomHex}`
54+
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
55+
const randomHex = Buffer.from(randomBytes(18)).toString("hex");
56+
const tempFileName = `lumos-debugger-data-${randomHex}`;
5657
const tmpTxPath = path.join(os.tmpdir(), `${tempFileName}.json`);
5758
fs.writeFileSync(tmpTxPath, JSON.stringify(debuggerData));
5859

packages/debugger/tests/context.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
createScriptRegistry,
55
} from "@ckb-lumos/experiment-tx-assembler";
66
import { computeScriptHash } from "@ckb-lumos/base/lib/utils";
7+
import { randomBytes } from "@ckb-lumos/crypto";
78
import { HexString } from "@ckb-lumos/base";
89
import { CKBDebugger, CKBDebuggerDownloader, DataLoader } from "../src";
910
import { TransactionSkeleton } from "@ckb-lumos/helpers";
@@ -12,7 +13,6 @@ import {
1213
getDefaultConfig,
1314
mockOutPoint,
1415
} from "../src/context";
15-
import { randomBytes } from "crypto";
1616
import { privateKeyToBlake160, signRecoverable } from "@ckb-lumos/hd/lib/key";
1717
import { hexify } from "@ckb-lumos/codec/lib/bytes";
1818
import {

packages/e2e-test/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@ckb-lumos/bi": "0.23.0",
3535
"@ckb-lumos/ckb-indexer": "0.23.0",
3636
"@ckb-lumos/codec": "0.23.0",
37+
"@ckb-lumos/crypto": "0.23.0",
3738
"@ckb-lumos/common-scripts": "0.23.0",
3839
"@ckb-lumos/config-manager": "0.23.0",
3940
"@ckb-lumos/hd": "0.23.0",

packages/e2e-test/src/utils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Script } from "@ckb-lumos/base";
2+
import { randomBytes } from "@ckb-lumos/crypto";
23
import { encodeToAddress } from "@ckb-lumos/helpers";
3-
import { randomBytes } from "crypto";
44
import { key } from "@ckb-lumos/hd";
55
import { getConfig } from "@ckb-lumos/config-manager";
66
import { hexify } from "@ckb-lumos/codec/lib/bytes";
77

88
// secp256k1 private key is 32-bytes length
9+
/* eslint-disable @typescript-eslint/no-magic-numbers */
910
export const generateRandomPrivateKey = (): string => hexify(randomBytes(32));
1011

1112
export function asyncSleep(ms: number): Promise<unknown> {

packages/hd/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"dependencies": {
2222
"@ckb-lumos/base": "0.23.0",
2323
"@ckb-lumos/bi": "0.23.0",
24+
"@ckb-lumos/crypto": "0.23.0",
2425
"bn.js": "^5.1.3",
2526
"elliptic": "^6.5.4",
2627
"scrypt-js": "^3.0.1",

packages/hd/src/keystore.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import crypto from "crypto";
1+
import {
2+
Cipher,
3+
ScryptOptions,
4+
createCipheriv,
5+
createDecipheriv,
6+
} from "crypto";
27
import { Keccak } from "sha3";
38
import { v4 as uuid } from "uuid";
49
import { ExtendedPrivateKey } from "./extended_key";
10+
import { randomBytes } from "@ckb-lumos/crypto";
511
import { HexString } from "@ckb-lumos/base";
612
import { syncScrypt } from "scrypt-js";
713

@@ -94,8 +100,8 @@ export default class Keystore {
94100
// Create an empty keystore object that contains empty private key
95101
static createEmpty(): Keystore {
96102
const saltSize = 32;
97-
const salt: Buffer = crypto.randomBytes(saltSize);
98-
const iv: Buffer = crypto.randomBytes(16);
103+
const salt: Buffer = Buffer.from(randomBytes(saltSize));
104+
const iv: Buffer = Buffer.from(randomBytes(16));
99105
const kdfparams: KdfParams = {
100106
dklen: 32,
101107
salt: salt.toString("hex"),
@@ -125,8 +131,8 @@ export default class Keystore {
125131
): Keystore {
126132
const saltSize = 32;
127133
const ivSize = 16;
128-
const salt: Buffer = options.salt || crypto.randomBytes(saltSize);
129-
const iv: Buffer = options.iv || crypto.randomBytes(ivSize);
134+
const salt: Buffer = options.salt || Buffer.from(randomBytes(saltSize));
135+
const iv: Buffer = options.iv || Buffer.from(randomBytes(ivSize));
130136
const kdfparams: KdfParams = {
131137
dklen: 32,
132138
salt: salt.toString("hex"),
@@ -145,11 +151,7 @@ export default class Keystore {
145151
)
146152
);
147153

148-
const cipher: crypto.Cipher = crypto.createCipheriv(
149-
CIPHER,
150-
derivedKey.slice(0, 16),
151-
iv
152-
);
154+
const cipher: Cipher = createCipheriv(CIPHER, derivedKey.slice(0, 16), iv);
153155
if (!cipher) {
154156
throw new UnsupportedCipher();
155157
}
@@ -190,7 +192,7 @@ export default class Keystore {
190192
if (Keystore.mac(derivedKey, ciphertext) !== this.crypto.mac) {
191193
throw new IncorrectPassword();
192194
}
193-
const decipher = crypto.createDecipheriv(
195+
const decipher = createDecipheriv(
194196
this.crypto.cipher,
195197
derivedKey.slice(0, 16),
196198
Buffer.from(this.crypto.cipherparams.iv, "hex")
@@ -239,7 +241,7 @@ export default class Keystore {
239241
);
240242
}
241243

242-
static scryptOptions(kdfparams: KdfParams): crypto.ScryptOptions {
244+
static scryptOptions(kdfparams: KdfParams): ScryptOptions {
243245
return {
244246
N: kdfparams.n,
245247
r: kdfparams.r,

0 commit comments

Comments
 (0)