From a563eb1ec139b38deb490943d650be8aab30c2e1 Mon Sep 17 00:00:00 2001 From: frankie Date: Mon, 19 Aug 2024 10:12:25 -1000 Subject: [PATCH] hex2buf -> hexStringToBuffer && use buffer.from in internals --- src/programs/dev.ts | 4 ++-- src/utils/index.ts | 8 ++------ tests/programs-dev.test.ts | 2 +- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/programs/dev.ts b/src/programs/dev.ts index 7f951354..54a793d8 100644 --- a/src/programs/dev.ts +++ b/src/programs/dev.ts @@ -2,7 +2,7 @@ import ExtrinsicBaseClass from '../extrinsic' import { ApiPromise } from '@polkadot/api' import { Signer } from '../keys/types/internal' import { SubmittableExtrinsic } from '@polkadot/api/types' -import { hex2buf, stripHexPrefix } from '../utils' +import { hexStringToBuffer, stripHexPrefix } from '../utils' import * as util from '@polkadot/util' import { HexString } from '../keys/types/json' @@ -140,7 +140,7 @@ export default class ProgramDev extends ExtrinsicBaseClass { #formatProgramInfo (programInfo): ProgramInfo { const { interfaceDescription, deployer, refCounter } = programInfo - const bytecode = hex2buf(stripHexPrefix(programInfo.bytecode)) // Convert hex string to ArrayBuffer + const bytecode = hexStringToBuffer(stripHexPrefix(programInfo.bytecode)) // Convert hex string to ArrayBuffer return { interfaceDescription, deployer, refCounter, bytecode } } } diff --git a/src/utils/index.ts b/src/utils/index.ts index b094b5fc..6a4d9d11 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -140,12 +140,8 @@ export function toHex (str: any) { * @returns {ArrayBuffer} The ArrayBuffer representation of the hexadecimal string. */ -export function hex2buf (hex: string): ArrayBuffer { - const bytes = new Uint8Array(Math.ceil(hex.length / 2)) - for (let i = 0; i < bytes.length; i++) { - bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16) - } - return bytes.buffer +export function hexStringToBuffer (hex: string): ArrayBuffer { + return Buffer.from(hex, 'hex') } export function hexStringToUint8Array (hex: string): Uint8Array { diff --git a/tests/programs-dev.test.ts b/tests/programs-dev.test.ts index 1dadc61e..64487ced 100644 --- a/tests/programs-dev.test.ts +++ b/tests/programs-dev.test.ts @@ -77,7 +77,7 @@ test('Programs#dev: all methods', async (t) => { ) t.deepEqual( - Buffer.from(noopProgramOnChain.bytecode), + noopProgramOnChain.bytecode, noopProgram, 'Whats on chain should match what was deployed' )