@@ -6,6 +6,7 @@ import { BI, BIish } from "@ckb-lumos/bi";
6
6
import * as blockchain from "./blockchain" ;
7
7
import { Script , Input } from "./api" ;
8
8
import { Hash , HexNumber , HexString } from "./primitive" ;
9
+ import { Uint128LE , Uint64LE } from "@ckb-lumos/codec/lib/number" ;
9
10
10
11
type CKBHasherOptions = {
11
12
outLength ?: number ;
@@ -54,7 +55,7 @@ function computeScriptHash(script: Script): string {
54
55
return ckbHash ( blockchain . Script . pack ( script ) ) ;
55
56
}
56
57
57
- function hashCode ( buffer : Buffer ) : number {
58
+ function hashCode ( buffer : Uint8Array ) : number {
58
59
return xxHash32 ( buffer , 0 ) ;
59
60
}
60
61
@@ -69,11 +70,7 @@ function toBigUInt64LE(num: BIish): HexString {
69
70
70
71
function toBigUInt64LECompatible ( num : BIish ) : HexString {
71
72
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 ) ) ;
77
74
}
78
75
79
76
/**
@@ -90,8 +87,7 @@ function readBigUInt64LE(hex: HexString): bigint {
90
87
* @deprecated please follow the {@link https://lumos-website.vercel.app/migrations/migrate-to-v0.19 migration-guide}
91
88
*/
92
89
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 ) ;
95
91
}
96
92
97
93
// const U128_MIN = BigInt(0);
@@ -117,18 +113,7 @@ function toBigUInt128LECompatible(num: BIish): HexNumber {
117
113
if ( num . gt ( U128_MAX_COMPATIBLE ) ) {
118
114
throw new Error ( `u128 ${ num } too large` ) ;
119
115
}
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 ) ) ;
132
117
}
133
118
134
119
/**
@@ -145,17 +130,7 @@ function readBigUInt128LE(leHex: HexString): bigint {
145
130
* @deprecated please follow the {@link https://lumos-website.vercel.app/migrations/migrate-to-v0.19 migration-guide}
146
131
*/
147
132
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 ) ) ;
159
134
}
160
135
161
136
function assertHexString ( debugPath : string , str : string ) : void {
0 commit comments