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

BREAKING CHANGE: replace Buffer with Uint8Array #645

Open
wants to merge 2 commits into
base: browser-compatibility
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .changeset/shiny-falcons-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@ckb-lumos/common-scripts": minor
"@ckb-lumos/base": minor
"@ckb-lumos/hd": minor
---

feat!: replace all `Buffer` with `Uint8Array` for the browser compatibility
37 changes: 6 additions & 31 deletions packages/base/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import * as blockchain from "./blockchain";
import { Script, Input } from "./api";
import { Hash, HexNumber, HexString } from "./primitive";
import { Uint128LE, Uint64LE } from "@ckb-lumos/codec/lib/number";

type CKBHasherOptions = {
outLength?: number;
Expand Down Expand Up @@ -54,7 +55,7 @@
return ckbHash(blockchain.Script.pack(script));
}

function hashCode(buffer: Buffer): number {
function hashCode(buffer: Uint8Array): number {
return xxHash32(buffer, 0);
}

Expand All @@ -69,11 +70,7 @@

function toBigUInt64LECompatible(num: BIish): HexString {
num = BI.from(num);
const buf = Buffer.alloc(8);
buf.writeUInt32LE(num.and("0xffffffff").toNumber(), 0);
num = num.shr(32);
buf.writeUInt32LE(num.and("0xffffffff").toNumber(), 4);
return `0x${buf.toString("hex")}`;
return bytes.hexify(Uint64LE.pack(num));

Check warning on line 73 in packages/base/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/base/src/utils.ts#L73

Added line #L73 was not covered by tests
}

/**
Expand All @@ -90,8 +87,7 @@
* @deprecated please follow the {@link https://lumos-website.vercel.app/migrations/migrate-to-v0.19 migration-guide}
*/
function readBigUInt64LECompatible(hex: HexString): BI {
const buf = Buffer.from(hex.slice(2), "hex");
return BI.from(buf.readUInt32LE()).add(BI.from(buf.readUInt32LE(4)).shl(32));
return Uint64LE.unpack(hex);

Check warning on line 90 in packages/base/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/base/src/utils.ts#L90

Added line #L90 was not covered by tests
}

// const U128_MIN = BigInt(0);
Expand All @@ -117,18 +113,7 @@
if (num.gt(U128_MAX_COMPATIBLE)) {
throw new Error(`u128 ${num} too large`);
}

const buf = Buffer.alloc(16);
buf.writeUInt32LE(num.and(0xffffffff).toNumber(), 0);
num = num.shr(32);
buf.writeUInt32LE(num.and(0xffffffff).toNumber(), 4);

num = num.shr(32);
buf.writeUInt32LE(num.and(0xffffffff).toNumber(), 8);

num = num.shr(32);
buf.writeUInt32LE(num.and(0xffffffff).toNumber(), 12);
return `0x${buf.toString("hex")}`;
return bytes.hexify(Uint128LE.pack(num));

Check warning on line 116 in packages/base/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/base/src/utils.ts#L116

Added line #L116 was not covered by tests
}

/**
Expand All @@ -145,17 +130,7 @@
* @deprecated please follow the {@link https://lumos-website.vercel.app/migrations/migrate-to-v0.19 migration-guide}
*/
function readBigUInt128LECompatible(leHex: HexString): BI {
if (leHex.length < 34 || !leHex.startsWith("0x")) {
throw new Error(`leHex format error`);
}

const buf = Buffer.from(leHex.slice(2, 34), "hex");

return BI.from(buf.readUInt32LE(0))
.shl(0)
.add(BI.from(buf.readUInt32LE(4)).shl(32))
.add(BI.from(buf.readUInt32LE(8)).shl(64))
.add(BI.from(buf.readUInt32LE(12)).shl(96));
return Uint128LE.unpack(bytes.bytify(leHex).slice(0, 16));

Check warning on line 133 in packages/base/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/base/src/utils.ts#L133

Added line #L133 was not covered by tests
}

function assertHexString(debugPath: string, str: string): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/base/src/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}

hashCode(): number {
return xxHash32(Buffer.from(this.buffer), 0);
return xxHash32(this.buffer, 0);

Check warning on line 19 in packages/base/src/values.ts

View check run for this annotation

Codecov / codecov/patch

packages/base/src/values.ts#L19

Added line #L19 was not covered by tests
}

hash(): Hash {
Expand Down
50 changes: 0 additions & 50 deletions packages/common-scripts/examples/pw_lock/config.json

This file was deleted.

Loading
Loading