Skip to content

Commit 50a2a3b

Browse files
authored
feat(bytes-builder): allow pass bigint as param (#15)
1 parent 936f111 commit 50a2a3b

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/bytes-builder/bytes-builder.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {trim0x} from '../utils'
1010
export class BytesBuilder {
1111
private bytes: string
1212

13-
constructor(init?: string | BN) {
13+
constructor(init?: string | BN | bigint) {
1414
if (init === undefined) {
1515
this.bytes = '0x'
1616

@@ -22,7 +22,8 @@ export class BytesBuilder {
2222

2323
this.bytes = init
2424
} else {
25-
this.bytes = init.toHex()
25+
const initBn = init instanceof BN ? init : new BN(init)
26+
this.bytes = initBn.toHex()
2627
}
2728
}
2829

@@ -33,7 +34,7 @@ export class BytesBuilder {
3334
return Number(this.bytes.length / 2 - 1)
3435
}
3536

36-
public addAddress(address: string | BN): this {
37+
public addAddress(address: string | BN | bigint): this {
3738
if (typeof address === 'string') {
3839
assert(
3940
isHexBytes(address) && address.length === 42,
@@ -42,9 +43,10 @@ export class BytesBuilder {
4243

4344
this.append(address)
4445
} else {
45-
assert(address.value <= UINT_160_MAX, 'Invalid address: too big')
46+
const addressBN = address instanceof BN ? address : new BN(address)
47+
assert(addressBN.value <= UINT_160_MAX, 'Invalid address: too big')
4648

47-
this.append(address.toHex(40))
49+
this.append(addressBN.toHex(40))
4850
}
4951

5052
return this
@@ -58,39 +60,39 @@ export class BytesBuilder {
5860
return this
5961
}
6062

61-
public addByte(byte: string | BN): this {
63+
public addByte(byte: string | BN | bigint): this {
6264
return this.addNBytes(byte, 1)
6365
}
6466

65-
public addUint8(val: string | BN): this {
67+
public addUint8(val: string | BN | bigint): this {
6668
return this.addNBytes(val, 1)
6769
}
6870

69-
public addUint16(val: string | BN): this {
71+
public addUint16(val: string | BN | bigint): this {
7072
return this.addNBytes(val, 2)
7173
}
7274

73-
public addUint24(val: string | BN): this {
75+
public addUint24(val: string | BN | bigint): this {
7476
return this.addNBytes(val, 3)
7577
}
7678

77-
public addUint32(val: string | BN): this {
79+
public addUint32(val: string | BN | bigint): this {
7880
return this.addNBytes(val, 4)
7981
}
8082

81-
public addUint64(val: string | BN): this {
83+
public addUint64(val: string | BN | bigint): this {
8284
return this.addNBytes(val, 8)
8385
}
8486

85-
public addUint128(val: string | BN): this {
87+
public addUint128(val: string | BN | bigint): this {
8688
return this.addNBytes(val, 16)
8789
}
8890

89-
public addUint160(val: string | BN): this {
91+
public addUint160(val: string | BN | bigint): this {
9092
return this.addNBytes(val, 20)
9193
}
9294

93-
public addUint256(val: string | BN): this {
95+
public addUint256(val: string | BN | bigint): this {
9496
return this.addNBytes(val, 32)
9597
}
9698

@@ -114,19 +116,20 @@ export class BytesBuilder {
114116
this.bytes += trim0x(bytes)
115117
}
116118

117-
private addNBytes(bytes: string | BN, n: number): this {
119+
private addNBytes(bytes: string | BN | bigint, n: number): this {
118120
if (typeof bytes === 'string') {
119121
assert(isHexBytes(bytes), 'Invalid value: not bytes hex string')
120122
assert(bytes.length === 2 + n * 2, 'Invalid value: bad length')
121123

122124
this.append(bytes)
123125
} else {
126+
const bytesBn = bytes instanceof BN ? bytes : new BN(bytes)
124127
assert(
125-
bytes.value <= (1n << (8n * BigInt(n))) - 1n,
128+
bytesBn.value <= (1n << (8n * BigInt(n))) - 1n,
126129
'Invalid value: too long'
127130
)
128131

129-
this.append(bytes.toHex(n * 2))
132+
this.append(bytesBn.toHex(n * 2))
130133
}
131134

132135
return this

0 commit comments

Comments
 (0)