Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add lumos crypto.randomBytes to replace node's #701

Merged
merged 15 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tough-beers-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ckb-lumos/crypto": minor
"@ckb-lumos/lumos": minor
---

feat: add crypto.randomBytes to replace Node's crypto.randomBytes
6 changes: 6 additions & 0 deletions .github/.codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ coverage:
target: auto
threshold: 1%
base: auto
patch:
default:
# basic settings
target: auto
threshold: 1%
base: auto

parsers:
gcov:
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ jobs:
- uses: pnpm/action-setup@v2
with:
version: 8.6.1
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
cache: "pnpm"
node-version: '18'

- name: Install project dependencies and build
run: |
Expand All @@ -38,9 +39,10 @@ jobs:
- uses: pnpm/action-setup@v2
with:
version: 8.6.1
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
cache: "pnpm"
node-version: '18'

- name: Install project dependencies and build
run: |
Expand All @@ -60,7 +62,7 @@ jobs:
- uses: pnpm/action-setup@v2
with:
version: 8.6.1
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
cache: "pnpm"

Expand Down
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const scopeEnumValues = [
"base",
"bi",
"crypto",
"ckb-indexer",
"common-scripts",
"config-manager",
Expand Down
1 change: 1 addition & 0 deletions packages/ckb-indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"events": "^3.3.0"
},
"devDependencies": {
"@ckb-lumos/crypto": "0.23.0",
"@ckb-lumos/testkit": "0.23.0",
"@types/lodash.uniqby": "^4.7.7",
"@types/request": "^2.48.8",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randomBytes } from "crypto";
import test from "ava";
import { randomBytes } from "@ckb-lumos/crypto";
import { Indexer, TransactionCollector } from "../src";
import {
indexerTransactionListThatHaveOneIoTypeInput,
Expand Down
1 change: 1 addition & 0 deletions packages/codec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
]
},
"devDependencies": {
"@ckb-lumos/crypto": "0.23.0",
"escape-string-regexp": "^4.0.0",
"js-yaml": "^4.1.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/codec/tests/bytes-like.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from "ava";
import { molecule, number } from "../src";
import { Byte32 } from "../src/blockchain";
import { randomBytes } from "crypto";
import { randomBytes } from "@ckb-lumos/crypto";
import { equal, concat, hexify } from "../src/bytes";
import { BI } from "@ckb-lumos/bi";
import { bytify } from "../lib/bytes";
Expand Down
1 change: 1 addition & 0 deletions packages/common-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
]
},
"devDependencies": {
"@ckb-lumos/crypto": "0.23.0",
"@ckb-lumos/debugger": "0.23.0",
"@ckb-lumos/hd": "0.23.0",
"@types/keccak": "^3.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/common-scripts/tests/dao-with-custom-lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { dao } from "../src";
import { Config, predefined } from "@ckb-lumos/config-manager";
import { hexify } from "@ckb-lumos/codec/lib/bytes";
import { Uint64 } from "@ckb-lumos/codec/lib/number";
import { randomBytes } from "node:crypto";
import { randomBytes } from "@ckb-lumos/crypto";

const { LINA } = predefined;

Expand Down
3 changes: 3 additions & 0 deletions packages/config-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
"@types/deep-freeze-strict": "^1.1.0",
"deep-freeze-strict": "^1.1.1"
},
"devDependencies": {
"@ckb-lumos/crypto": "0.23.0"
},
"publishConfig": {
"access": "public"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/config-manager/tests/refresh.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import test from "ava";
import { spy } from "sinon";
import {
createLatestTypeIdResolver,
createRpcResolver,
FetchOutputsByTxHashes,
refreshScriptConfigs,
} from "../src/refresh";
import { ScriptConfigs } from "../src";
import { hexify } from "@ckb-lumos/codec/lib/bytes";
import { randomBytes } from "node:crypto";
import { OutPoint, Output, Script } from "@ckb-lumos/base";
import { spy } from "sinon";
import { randomBytes } from "@ckb-lumos/crypto";
import { ScriptConfigs } from "../src";

test("refresh without update", async (t) => {
const scriptConfigs: ScriptConfigs = {
Expand Down
1 change: 1 addition & 0 deletions packages/crypto/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
1 change: 1 addition & 0 deletions packages/crypto/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
1 change: 1 addition & 0 deletions packages/crypto/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @ckb-lumos/crypto
3 changes: 3 additions & 0 deletions packages/crypto/READMD.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `@ckb-lumos/crypto`

Crypto utilities used in Lumos.
52 changes: 52 additions & 0 deletions packages/crypto/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@ckb-lumos/crypto",
"version": "0.23.0",
"description": "Crypto utilities used in Lumos",
"author": "Tom Wang <[email protected]@gmail.com>",
"homepage": "https://github.com/ckb-js/lumos#readme",
"license": "MIT",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"engines": {
"node": ">=18.0.0"
},
"directories": {
"lib": "lib",
"test": "tests"
},
"files": [
"lib",
"index.d.ts"
],
"repository": {
"type": "git",
"url": "git+https://github.com/ckb-js/lumos.git"
},
"scripts": {
"build": "pnpm run build:types && pnpm run build:js",
"build:types": "tsc --declaration --emitDeclarationOnly",
"build:js": "babel --root-mode upward src --out-dir lib --extensions .ts -s",
"fmt": "prettier --write \"{src,tests,examples}/**/*.ts\" package.json",
"lint": "eslint -c ../../.eslintrc.js \"{src,tests,examples}/**/*.ts\"",
"clean": "shx rm -rf lib",
"test": "ava **/*.test.{js,ts} --timeout=2m"
},
"bugs": {
"url": "https://github.com/ckb-js/lumos/issues"
},
"ava": {
"extensions": [
"ts",
"js"
],
"require": [
"ts-node/register"
]
},
"dependencies": {
"@noble/hashes": "^1.4.0"
},
"publishConfig": {
"access": "public"
}
}
1 change: 1 addition & 0 deletions packages/crypto/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { randomBytes } from "@noble/hashes/utils";
10 changes: 10 additions & 0 deletions packages/crypto/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import test from "ava";
import { randomBytes } from "../src";

test("randomBytes", (t) => {
const size = 32;
const bytes = randomBytes(size);
t.is(bytes.length, size);
t.is(bytes.byteLength, size);
t.is(bytes.constructor, Uint8Array);
});
8 changes: 8 additions & 0 deletions packages/crypto/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "lib",
"sourceMap": true
},
"include": ["src"]
}
4 changes: 4 additions & 0 deletions packages/crypto/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["../../typedoc.base.json"],
"entryPoints": ["src/index.ts"]
}
1 change: 1 addition & 0 deletions packages/debugger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@ckb-lumos/base": "0.23.0",
"@ckb-lumos/bi": "0.23.0",
"@ckb-lumos/codec": "0.23.0",
"@ckb-lumos/crypto": "0.23.0",
"@ckb-lumos/config-manager": "0.23.0",
"@ckb-lumos/helpers": "0.23.0",
"@ckb-lumos/rpc": "0.23.0",
Expand Down
7 changes: 3 additions & 4 deletions packages/debugger/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { randomBytes } from "@ckb-lumos/crypto";
import { Header, OutPoint } from "@ckb-lumos/base";
import * as crypto from "crypto";
import { ScriptConfig } from "@ckb-lumos/config-manager";
import { OutputDataLoader } from "./loader";
import { DataLoader, TestContext } from "./types";
Expand All @@ -14,9 +14,8 @@ const TRANSACTION_HASH_LENGTH = 32;

export function mockOutPoint(): OutPoint {
return {
txHash: hexify(crypto.randomBytes(TRANSACTION_HASH_LENGTH)),
index:
"0x" + Uint32.unpack(crypto.randomBytes(Uint32.byteLength)).toString(16),
txHash: hexify(randomBytes(TRANSACTION_HASH_LENGTH)),
index: "0x" + Uint32.unpack(randomBytes(Uint32.byteLength)).toString(16),
};
}

Expand Down
7 changes: 4 additions & 3 deletions packages/debugger/src/executor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { DataLoader, ExecuteResult, Executor } from "./types";
import { TransactionSkeletonType } from "@ckb-lumos/helpers";
import { randomBytes } from "@ckb-lumos/crypto";
import { spawnSync } from "child_process";
import { Hash } from "@ckb-lumos/base";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { parseDebuggerData, parseDebuggerMessage } from "./parse";
import * as crypto from 'crypto';

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

Expand Down
2 changes: 1 addition & 1 deletion packages/debugger/tests/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createScriptRegistry,
} from "@ckb-lumos/experiment-tx-assembler";
import { computeScriptHash } from "@ckb-lumos/base/lib/utils";
import { randomBytes } from "@ckb-lumos/crypto";
import { HexString } from "@ckb-lumos/base";
import { CKBDebugger, CKBDebuggerDownloader, DataLoader } from "../src";
import { TransactionSkeleton } from "@ckb-lumos/helpers";
Expand All @@ -12,7 +13,6 @@ import {
getDefaultConfig,
mockOutPoint,
} from "../src/context";
import { randomBytes } from "crypto";
import { privateKeyToBlake160, signRecoverable } from "@ckb-lumos/hd/lib/key";
import { hexify } from "@ckb-lumos/codec/lib/bytes";
import {
Expand Down
1 change: 1 addition & 0 deletions packages/e2e-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@ckb-lumos/bi": "0.23.0",
"@ckb-lumos/ckb-indexer": "0.23.0",
"@ckb-lumos/codec": "0.23.0",
"@ckb-lumos/crypto": "0.23.0",
"@ckb-lumos/common-scripts": "0.23.0",
"@ckb-lumos/config-manager": "0.23.0",
"@ckb-lumos/hd": "0.23.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/e2e-test/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Script } from "@ckb-lumos/base";
import { randomBytes } from "@ckb-lumos/crypto";
import { encodeToAddress } from "@ckb-lumos/helpers";
import { randomBytes } from "crypto";
import { key } from "@ckb-lumos/hd";
import { getConfig } from "@ckb-lumos/config-manager";
import { hexify } from "@ckb-lumos/codec/lib/bytes";

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

export function asyncSleep(ms: number): Promise<unknown> {
Expand All @@ -30,7 +31,7 @@

const pubKey = key.privateToPublic(_privKey);
const args = key.publicKeyToBlake160(pubKey);
const template = getConfig().SCRIPTS["SECP256K1_BLAKE160"]!;

Check warning on line 34 in packages/e2e-test/src/utils.ts

View workflow job for this annotation

GitHub Actions / lint-staged

Forbidden non-null assertion

Check warning on line 34 in packages/e2e-test/src/utils.ts

View workflow job for this annotation

GitHub Actions / lint-staged

Forbidden non-null assertion
const lockScript = {
codeHash: template.CODE_HASH,
hashType: template.HASH_TYPE,
Expand Down
1 change: 1 addition & 0 deletions packages/hd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"@ckb-lumos/base": "0.23.0",
"@ckb-lumos/bi": "0.23.0",
"@ckb-lumos/crypto": "0.23.0",
"bn.js": "^5.1.3",
"elliptic": "^6.5.4",
"scrypt-js": "^3.0.1",
Expand Down
26 changes: 14 additions & 12 deletions packages/hd/src/keystore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import crypto from "crypto";
import {
Cipher,
ScryptOptions,
createCipheriv,
createDecipheriv,
} from "crypto";
import { Keccak } from "sha3";
import { v4 as uuid } from "uuid";
import { ExtendedPrivateKey } from "./extended_key";
import { randomBytes } from "@ckb-lumos/crypto";
import { HexString } from "@ckb-lumos/base";
import { syncScrypt } from "scrypt-js";

Expand Down Expand Up @@ -94,8 +100,8 @@ export default class Keystore {
// Create an empty keystore object that contains empty private key
static createEmpty(): Keystore {
const saltSize = 32;
const salt: Buffer = crypto.randomBytes(saltSize);
const iv: Buffer = crypto.randomBytes(16);
const salt: Buffer = Buffer.from(randomBytes(saltSize));
const iv: Buffer = Buffer.from(randomBytes(16));
Keith-CY marked this conversation as resolved.
Show resolved Hide resolved
const kdfparams: KdfParams = {
dklen: 32,
salt: salt.toString("hex"),
Expand Down Expand Up @@ -125,8 +131,8 @@ export default class Keystore {
): Keystore {
const saltSize = 32;
const ivSize = 16;
const salt: Buffer = options.salt || crypto.randomBytes(saltSize);
const iv: Buffer = options.iv || crypto.randomBytes(ivSize);
const salt: Buffer = options.salt || Buffer.from(randomBytes(saltSize));
const iv: Buffer = options.iv || Buffer.from(randomBytes(ivSize));
const kdfparams: KdfParams = {
dklen: 32,
salt: salt.toString("hex"),
Expand All @@ -145,11 +151,7 @@ export default class Keystore {
)
);

const cipher: crypto.Cipher = crypto.createCipheriv(
CIPHER,
derivedKey.slice(0, 16),
iv
);
const cipher: Cipher = createCipheriv(CIPHER, derivedKey.slice(0, 16), iv);
if (!cipher) {
throw new UnsupportedCipher();
}
Expand Down Expand Up @@ -190,7 +192,7 @@ export default class Keystore {
if (Keystore.mac(derivedKey, ciphertext) !== this.crypto.mac) {
throw new IncorrectPassword();
}
const decipher = crypto.createDecipheriv(
const decipher = createDecipheriv(
this.crypto.cipher,
derivedKey.slice(0, 16),
Buffer.from(this.crypto.cipherparams.iv, "hex")
Expand Down Expand Up @@ -239,7 +241,7 @@ export default class Keystore {
);
}

static scryptOptions(kdfparams: KdfParams): crypto.ScryptOptions {
static scryptOptions(kdfparams: KdfParams): ScryptOptions {
return {
N: kdfparams.n,
r: kdfparams.r,
Expand Down
Loading
Loading