Skip to content
Permalink
Browse files

Merge pull request #117 from WalletConnect/hex-prefix

Add utils hex prefix argument
  • Loading branch information...
pedrouid committed May 2, 2019
2 parents 8cb41fe + dbcfbdb commit 9d32989e55937a26b764470d01f14a5411e39002

Large diffs are not rendered by default.

Large diffs are not rendered by default.

@@ -571,7 +571,7 @@ class Connector {
}

if (!isHexStrict(params[1])) {
params[1] = convertUtf8ToHex(params[1])
params[1] = convertUtf8ToHex(params[1], true)
}

const request = this._formatRequest({

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

@@ -34,8 +34,11 @@ export function convertBufferToUtf8 (buffer: Buffer): string {
return result
}

export function convertBufferToHex (buffer: Buffer): string {
const result = buffer.toString('hex')
export function convertBufferToHex (buffer: Buffer, prefix?: boolean): string {
let result = buffer.toString('hex')
if (prefix) {
result = '0x' + result
}
return result
}

@@ -108,15 +111,21 @@ export function convertUtf8ToArrayBuffer (utf8: string): ArrayBuffer {
return arrayBuffer
}

export function convertArrayBufferToHex (arrayBuffer: ArrayBuffer): string {
export function convertArrayBufferToHex (
arrayBuffer: ArrayBuffer,
prefix?: boolean
): string {
const array: Uint8Array = new Uint8Array(arrayBuffer)
const HEX_CHARS: string = '0123456789abcdef'
const bytes: string[] = []
for (let i = 0; i < array.length; i++) {
const byte = array[i]
bytes.push(HEX_CHARS[(byte & 0xf0) >> 4] + HEX_CHARS[byte & 0x0f])
}
const hex: string = bytes.join('')
let hex: string = bytes.join('')
if (prefix) {
hex = '0x' + hex
}
return hex
}

@@ -134,9 +143,12 @@ export function convertHexToArrayBuffer (hex: string): ArrayBuffer {
return arrayBuffer
}

export function convertUtf8ToHex (utf8: string): string {
export function convertUtf8ToHex (utf8: string, prefix?: boolean): string {
const arrayBuffer = convertUtf8ToArrayBuffer(utf8)
const hex = convertArrayBufferToHex(arrayBuffer)
let hex = convertArrayBufferToHex(arrayBuffer)
if (prefix) {
hex = '0x' + hex
}
return hex
}

0 comments on commit 9d32989

Please sign in to comment.
You can’t perform that action at this time.