import {Buffer} from 'buffer'; import {blob, Layout} from '@solana/buffer-layout'; import {toBigIntLE, toBufferLE} from 'bigint-buffer'; interface EncodeDecode { decode(buffer: Buffer, offset?: number): T; encode(src: T, buffer: Buffer, offset?: number): number; } const encodeDecode = (layout: Layout): EncodeDecode => { const decode = layout.decode.bind(layout); const encode = layout.encode.bind(layout); return {decode, encode}; }; const bigInt = (length: number) => (property?: string): Layout => { const layout = blob(length, property); const {encode, decode} = encodeDecode(layout); const bigIntLayout = layout as Layout as Layout; bigIntLayout.decode = (buffer: Buffer, offset: number) => { const src = decode(buffer, offset); return toBigIntLE(Buffer.from(src)); }; bigIntLayout.encode = (bigInt: bigint, buffer: Buffer, offset: number) => { const src = toBufferLE(bigInt, length); return encode(src, buffer, offset); }; return bigIntLayout; }; export const u64 = bigInt(8); export const u128 = bigInt(16); export const u192 = bigInt(24); export const u256 = bigInt(32);