Skip to content

Commit 0c0825d

Browse files
committed
feat!: replace Buffer with Uint8Array
1 parent e34daa1 commit 0c0825d

16 files changed

+191
-594
lines changed

packages/base/src/utils.ts

+6-31
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { BI, BIish } from "@ckb-lumos/bi";
66
import * as blockchain from "./blockchain";
77
import { Script, Input } from "./api";
88
import { Hash, HexNumber, HexString } from "./primitive";
9+
import { Uint128LE, Uint64LE } from "@ckb-lumos/codec/lib/number";
910

1011
type CKBHasherOptions = {
1112
outLength?: number;
@@ -54,7 +55,7 @@ function computeScriptHash(script: Script): string {
5455
return ckbHash(blockchain.Script.pack(script));
5556
}
5657

57-
function hashCode(buffer: Buffer): number {
58+
function hashCode(buffer: Uint8Array): number {
5859
return xxHash32(buffer, 0);
5960
}
6061

@@ -69,11 +70,7 @@ function toBigUInt64LE(num: BIish): HexString {
6970

7071
function toBigUInt64LECompatible(num: BIish): HexString {
7172
num = BI.from(num);
72-
const buf = Buffer.alloc(8);
73-
buf.writeUInt32LE(num.and("0xffffffff").toNumber(), 0);
74-
num = num.shr(32);
75-
buf.writeUInt32LE(num.and("0xffffffff").toNumber(), 4);
76-
return `0x${buf.toString("hex")}`;
73+
return bytes.hexify(Uint64LE.pack(num));
7774
}
7875

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

9793
// const U128_MIN = BigInt(0);
@@ -117,18 +113,7 @@ function toBigUInt128LECompatible(num: BIish): HexNumber {
117113
if (num.gt(U128_MAX_COMPATIBLE)) {
118114
throw new Error(`u128 ${num} too large`);
119115
}
120-
121-
const buf = Buffer.alloc(16);
122-
buf.writeUInt32LE(num.and(0xffffffff).toNumber(), 0);
123-
num = num.shr(32);
124-
buf.writeUInt32LE(num.and(0xffffffff).toNumber(), 4);
125-
126-
num = num.shr(32);
127-
buf.writeUInt32LE(num.and(0xffffffff).toNumber(), 8);
128-
129-
num = num.shr(32);
130-
buf.writeUInt32LE(num.and(0xffffffff).toNumber(), 12);
131-
return `0x${buf.toString("hex")}`;
116+
return bytes.hexify(Uint128LE.pack(num));
132117
}
133118

134119
/**
@@ -145,17 +130,7 @@ function readBigUInt128LE(leHex: HexString): bigint {
145130
* @deprecated please follow the {@link https://lumos-website.vercel.app/migrations/migrate-to-v0.19 migration-guide}
146131
*/
147132
function readBigUInt128LECompatible(leHex: HexString): BI {
148-
if (leHex.length < 34 || !leHex.startsWith("0x")) {
149-
throw new Error(`leHex format error`);
150-
}
151-
152-
const buf = Buffer.from(leHex.slice(2, 34), "hex");
153-
154-
return BI.from(buf.readUInt32LE(0))
155-
.shl(0)
156-
.add(BI.from(buf.readUInt32LE(4)).shl(32))
157-
.add(BI.from(buf.readUInt32LE(8)).shl(64))
158-
.add(BI.from(buf.readUInt32LE(12)).shl(96));
133+
return Uint128LE.unpack(bytes.bytify(leHex).slice(0, 16));
159134
}
160135

161136
function assertHexString(debugPath: string, str: string): void {

packages/base/src/values.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Value {
1616
}
1717

1818
hashCode(): number {
19-
return xxHash32(Buffer.from(this.buffer), 0);
19+
return xxHash32(this.buffer, 0);
2020
}
2121

2222
hash(): Hash {

packages/common-scripts/examples/pw_lock/config.json

-50
This file was deleted.

0 commit comments

Comments
 (0)