From d6296b482e7a506719099efe022731e47f172bc9 Mon Sep 17 00:00:00 2001 From: 0xfirefist Date: Fri, 26 Jan 2024 14:27:48 +0530 Subject: [PATCH] uint 256 --- .../packages/xc_admin_common/package.json | 5 ++-- .../src/governance_payload/BufferLayoutExt.ts | 24 +++++++++---------- package-lock.json | 2 ++ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/governance/xc_admin/packages/xc_admin_common/package.json b/governance/xc_admin/packages/xc_admin_common/package.json index 6b33118602..db6aeccd98 100644 --- a/governance/xc_admin/packages/xc_admin_common/package.json +++ b/governance/xc_admin/packages/xc_admin_common/package.json @@ -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" @@ -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" } } diff --git a/governance/xc_admin/packages/xc_admin_common/src/governance_payload/BufferLayoutExt.ts b/governance/xc_admin/packages/xc_admin_common/src/governance_payload/BufferLayoutExt.ts index 3b137420ca..c95ce6b385 100644 --- a/governance/xc_admin/packages/xc_admin_common/src/governance_payload/BufferLayoutExt.ts +++ b/governance/xc_admin/packages/xc_admin_common/src/governance_payload/BufferLayoutExt.ts @@ -1,25 +1,20 @@ import { Layout } from "@solana/buffer-layout"; +import { toBigIntBE, toBufferBE } from "bigint-buffer"; -export class UInt64BE extends Layout { +export class UIntBE extends Layout { + // 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; } @@ -45,8 +40,13 @@ export class HexBytes extends Layout { } /** 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. */ diff --git a/package-lock.json b/package-lock.json index 0b8e200062..d1aa23b9c5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1918,6 +1918,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" @@ -104649,6 +104650,7 @@ "@types/bn.js": "^5.1.1", "@types/jest": "^29.2.5", "@types/lodash": "^4.14.191", + "bigint-buffer": "*", "ethers": "^5.7.2", "fast-check": "^3.10.0", "jest": "^29.3.1",