Skip to content

Commit

Permalink
hex2buf -> hexStringToBuffer && use buffer.from in internals
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiebee committed Aug 19, 2024
1 parent edddedf commit a563eb1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/programs/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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 }
}
}
8 changes: 2 additions & 6 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/programs-dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down

0 comments on commit a563eb1

Please sign in to comment.