Skip to content

Commit

Permalink
uint 256
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfirefist committed Jan 26, 2024
1 parent d9a41b3 commit d6296b4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
5 changes: 3 additions & 2 deletions governance/xc_admin/packages/xc_admin_common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@solana/buffer-layout": "^4.0.1",
"@solana/web3.js": "^1.73.0",
"@sqds/mesh": "^1.0.6",
"bigint-buffer": "^1.1.5",
"ethers": "^5.7.2",
"lodash": "^4.17.21",
"typescript": "^4.9.4"
Expand All @@ -34,9 +35,9 @@
"@types/bn.js": "^5.1.1",
"@types/jest": "^29.2.5",
"@types/lodash": "^4.14.191",
"fast-check": "^3.10.0",
"jest": "^29.3.1",
"prettier": "^2.8.1",
"ts-jest": "^29.0.3",
"fast-check": "^3.10.0"
"ts-jest": "^29.0.3"
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import { Layout } from "@solana/buffer-layout";
import { toBigIntBE, toBufferBE } from "bigint-buffer";

export class UInt64BE extends Layout<bigint> {
export class UIntBE extends Layout<bigint> {
// span is the number of bytes to read
constructor(span: number, property?: string) {
super(span, property);
}

// Note: we can not use read/writeBigUInt64BE because it is not supported in the browsers
override decode(b: Uint8Array, offset?: number): bigint {
let o = offset ?? 0;
const buffer = Buffer.from(b.slice(o, o + this.span));
const hi32 = buffer.readUInt32BE();
const lo32 = buffer.readUInt32BE(4);
return BigInt(lo32) + (BigInt(hi32) << BigInt(32));
return toBigIntBE(buffer);
}

override encode(src: bigint, b: Uint8Array, offset?: number): number {
const buffer = Buffer.alloc(this.span);
const hi32 = Number(src >> BigInt(32));
const lo32 = Number(src & BigInt(0xffffffff));
buffer.writeUInt32BE(hi32, 0);
buffer.writeUInt32BE(lo32, 4);
const buffer = toBufferBE(src, this.span);
b.set(buffer, offset);
return this.span;
}
Expand All @@ -45,8 +40,13 @@ export class HexBytes extends Layout<string> {
}

/** A big-endian u64, returned as a bigint. */
export function u64be(property?: string | undefined): UInt64BE {
return new UInt64BE(8, property);
export function u64be(property?: string | undefined): UIntBE {
return new UIntBE(8, property);
}

/** A big-endian u256, returned as a bigint. */
export function u256be(property?: string | undefined): UIntBE {
return new UIntBE(32, property);
}

/** An array of numBytes bytes, returned as a hexadecimal string. */
Expand Down
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d6296b4

Please sign in to comment.