@walletconnect/utils
Advanced tools
Comparing version 1.0.0-beta.20 to 1.0.0-beta.21
/// <reference types="node" /> | ||
import { ITxData, IClientMeta, IParseURIResult, IJsonRpcResponseSuccess, IJsonRpcResponseError, IJsonRpcErrorMessage } from '@walletconnect/types'; | ||
export declare function convertArrayBufferToBuffer(arrayBuffer: ArrayBuffer): Buffer; | ||
export declare function convertArrayBufferToUtf8(arrayBuffer: ArrayBuffer): string; | ||
export declare function convertArrayBufferToHex(arrayBuffer: ArrayBuffer, noPrefix?: boolean): string; | ||
export declare function convertArrayBufferToNumber(arrayBuffer: ArrayBuffer): number; | ||
export declare function concatArrayBuffers(...args: ArrayBuffer[]): ArrayBuffer; | ||
export declare function convertBufferToArrayBuffer(buffer: Buffer): ArrayBuffer; | ||
export declare function convertUtf8ToBuffer(utf8: string): Buffer; | ||
export declare function convertBufferToUtf8(buffer: Buffer): string; | ||
export declare function convertBufferToHex(buffer: Buffer, prefix?: boolean): string; | ||
export declare function convertHexToBuffer(hex: string): Buffer; | ||
export declare function convertBufferToHex(buffer: Buffer, noPrefix?: boolean): string; | ||
export declare function convertBufferToNumber(buffer: Buffer): number; | ||
export declare function concatBuffers(...args: Buffer[]): Buffer; | ||
export declare function concatArrayBuffers(...args: ArrayBuffer[]): ArrayBuffer; | ||
export declare function convertArrayBufferToUtf8(arrayBuffer: ArrayBuffer): string; | ||
export declare function convertUtf8ToArrayBuffer(utf8: string): ArrayBuffer; | ||
export declare function convertArrayBufferToHex(arrayBuffer: ArrayBuffer, prefix?: boolean): string; | ||
export declare function convertUtf8ToBuffer(utf8: string): Buffer; | ||
export declare function convertUtf8ToHex(utf8: string, noPrefix?: boolean): string; | ||
export declare function convertUtf8ToNumber(utf8: string): number; | ||
export declare function convertNumberToBuffer(num: number): Buffer; | ||
export declare function convertNumberToArrayBuffer(num: number): ArrayBuffer; | ||
export declare function convertNumberToUtf8(num: number): string; | ||
export declare function convertNumberToHex(num: number, noPrefix?: boolean): string; | ||
export declare function convertHexToBuffer(hex: string): Buffer; | ||
export declare function convertHexToArrayBuffer(hex: string): ArrayBuffer; | ||
export declare function convertUtf8ToHex(utf8: string, prefix?: boolean): string; | ||
export declare function convertHexToUtf8(hex: string): string; | ||
export declare function convertHexToNumber(hex: string): number; | ||
export declare function sanitizeHex(hex: string): string; | ||
export declare function addHexPrefix(hex: string): string; | ||
export declare function removeHexPrefix(hex: string): string; | ||
export declare function payloadId(): number; | ||
export declare function uuid(): string; | ||
export declare const isHexStrict: (hex: string) => boolean; | ||
export declare function keccak256(data?: string): string; | ||
export declare const toChecksumAddress: (address: string) => string; | ||
@@ -25,4 +34,2 @@ export declare const isValidAddress: (address?: string | undefined) => boolean; | ||
export declare function parseWalletConnectUri(str: string): IParseURIResult; | ||
export declare function sanitizeHex(hex: string): string; | ||
export declare function removeHexPrefix(hex: string): string; | ||
export declare function promisify(originalFn: (...args: any[]) => void, thisArg?: any): (...callArgs: any[]) => Promise<IJsonRpcResponseSuccess | IJsonRpcResponseError>; | ||
@@ -29,0 +36,0 @@ export declare function parsePersonalSign(params: string[]): string[]; |
{ | ||
"name": "@walletconnect/utils", | ||
"version": "1.0.0-beta.20", | ||
"version": "1.0.0-beta.21", | ||
"description": "Utility Library for WalletConnect", | ||
@@ -58,7 +58,6 @@ "scripts": { | ||
"dependencies": { | ||
"@walletconnect/types": "^1.0.0-beta.20", | ||
"js-sha3": "^0.8.0", | ||
"lodash.isnumber": "^3.0.3" | ||
"@walletconnect/types": "^1.0.0-beta.21", | ||
"ethers": "^4.0.27" | ||
}, | ||
"gitHead": "165f7993c2acc907c653c02847fb02721052c6e7" | ||
} |
@@ -7,30 +7,49 @@ # WalletConnect Utils | ||
```javascript | ||
// ArrayBuffer | ||
```typescript | ||
function convertArrayBufferToBuffer (arrayBuffer: ArrayBuffer): Buffer | ||
function convertArrayBufferToUtf8 (arrayBuffer: ArrayBuffer): string | ||
function convertArrayBufferToHex (arrayBuffer: ArrayBuffer): string | ||
function convertArrayBufferToHex (arrayBuffer: ArrayBuffer, noPrefix?: boolean): string | ||
function convertArrayBufferToNumber (arrayBuffer: ArrayBuffer): number | ||
function concatArrayBuffers (...args: ArrayBuffer[]): ArrayBuffer | ||
// Buffer | ||
function convertBufferToArrayBuffer (buffer: Buffer): ArrayBuffer | ||
function convertBufferToUtf8 (buffer: Buffer): string | ||
function convertBufferToHex (buffer: Buffer): string | ||
function convertBufferToHex (buffer: Buffer, noPrefix?: boolean): string | ||
function convertBufferToNumber (buffer: Buffer): number | ||
function concatBuffers (...args: Buffer[]): Buffer | ||
// Utf-8 | ||
function convertUtf8ToArrayBuffer (utf8: string): ArrayBuffer | ||
function convertUtf8ToBuffer (utf8: string): Buffer | ||
function convertUtf8ToHex (utf8: string, noPrefix?: boolean): string | ||
function convertUtf8ToNumber (utf8: string): number | ||
// Hex | ||
function convertNumberToBuffer (num: number): Buffer | ||
function convertNumberToArrayBuffer (num: number): ArrayBuffer | ||
function convertNumberToUtf8 (num: number): string | ||
function convertNumberToHex (num: number, noPrefix?: boolean): string | ||
function convertHexToBuffer (hex: string): Buffer | ||
function convertHexToArrayBuffer (hex: string): ArrayBuffer | ||
function convertHexToUtf8 (hex: string): string | ||
function convertHexToNumber (hex: string): number | ||
// Misc | ||
function sanitizeHex (hex: string): string | ||
function addHexPrefix (hex: string): string | ||
function removeHexPrefix (hex: string): string | ||
function payloadId (): number | ||
function uuid (): string | ||
function keccak256 (data?: string): string | ||
function getMeta (): IClientMeta | null | ||
function parseWalletConnectUri (str: string): IParseURIResult | ||
function promisify (originalFn: (...args: any[]) => void, thisArg?: any): (...callArgs: any[]) | ||
function parsePersonalSign (params: string[]): string[] | ||
function parseTransactionData (txData: Partial<ITxData>): Partial<ITxData> | ||
function formatRpcError (error: Partial<IJsonRpcErrorMessage>): { code: number; message: string } | ||
``` |
259
src/index.ts
@@ -1,3 +0,2 @@ | ||
import isNumber from 'lodash.isnumber' | ||
import { keccak_256 } from 'js-sha3' | ||
import { utils } from 'ethers' | ||
@@ -15,2 +14,4 @@ import { | ||
// -- ArrayBuffer ------------------------------------------ // | ||
export function convertArrayBufferToBuffer (arrayBuffer: ArrayBuffer): Buffer { | ||
@@ -22,2 +23,32 @@ const hex = convertArrayBufferToHex(arrayBuffer) | ||
export function convertArrayBufferToUtf8 (arrayBuffer: ArrayBuffer): string { | ||
const utf8 = utils.toUtf8String(new Uint8Array(arrayBuffer)) | ||
return utf8 | ||
} | ||
export function convertArrayBufferToHex ( | ||
arrayBuffer: ArrayBuffer, | ||
noPrefix?: boolean | ||
): string { | ||
let hex = utils.hexlify(new Uint8Array(arrayBuffer)) | ||
if (noPrefix) { | ||
hex = removeHexPrefix(hex) | ||
} | ||
return hex | ||
} | ||
export function convertArrayBufferToNumber (arrayBuffer: ArrayBuffer): number { | ||
const hex = convertArrayBufferToHex(arrayBuffer) | ||
const num = convertHexToNumber(hex) | ||
return num | ||
} | ||
export function concatArrayBuffers (...args: ArrayBuffer[]): ArrayBuffer { | ||
const hex: string = args.map(b => convertArrayBufferToHex(b, true)).join('') | ||
const result: ArrayBuffer = convertHexToArrayBuffer(hex) | ||
return result | ||
} | ||
// -- Buffer ----------------------------------------------- // | ||
export function convertBufferToArrayBuffer (buffer: Buffer): ArrayBuffer { | ||
@@ -29,7 +60,2 @@ const hex = convertBufferToHex(buffer) | ||
export function convertUtf8ToBuffer (utf8: string): Buffer { | ||
const result = new Buffer(utf8, 'utf8') | ||
return result | ||
} | ||
export function convertBufferToUtf8 (buffer: Buffer): string { | ||
@@ -40,18 +66,18 @@ const result = buffer.toString('utf8') | ||
export function convertBufferToHex (buffer: Buffer, prefix?: boolean): string { | ||
let result = buffer.toString('hex') | ||
if (prefix) { | ||
result = '0x' + result | ||
export function convertBufferToHex (buffer: Buffer, noPrefix?: boolean): string { | ||
let hex = buffer.toString('hex') | ||
if (!noPrefix) { | ||
hex = addHexPrefix(hex) | ||
} | ||
return result | ||
return hex | ||
} | ||
export function convertHexToBuffer (hex: string): Buffer { | ||
hex = hex.replace('0x', '') | ||
const result = new Buffer(hex, 'hex') | ||
return result | ||
export function convertBufferToNumber (buffer: Buffer): number { | ||
const hex = convertBufferToHex(buffer) | ||
const num = convertHexToNumber(hex) | ||
return num | ||
} | ||
export function concatBuffers (...args: Buffer[]): Buffer { | ||
const hex: string = args.map(b => convertBufferToHex(b)).join('') | ||
const hex: string = args.map(b => convertBufferToHex(b, true)).join('') | ||
const result: Buffer = convertHexToBuffer(hex) | ||
@@ -61,100 +87,66 @@ return result | ||
export function concatArrayBuffers (...args: ArrayBuffer[]): ArrayBuffer { | ||
const hex: string = args.map(b => convertArrayBufferToHex(b)).join('') | ||
const result: ArrayBuffer = convertHexToArrayBuffer(hex) | ||
// -- Utf8 ------------------------------------------------- // | ||
export function convertUtf8ToArrayBuffer (utf8: string): ArrayBuffer { | ||
const arrayBuffer = utils.toUtf8Bytes(utf8).buffer | ||
return arrayBuffer | ||
} | ||
export function convertUtf8ToBuffer (utf8: string): Buffer { | ||
const result = new Buffer(utf8, 'utf8') | ||
return result | ||
} | ||
export function convertArrayBufferToUtf8 (arrayBuffer: ArrayBuffer): string { | ||
const array: Uint8Array = new Uint8Array(arrayBuffer) | ||
const chars: string[] = [] | ||
let i: number = 0 | ||
export function convertUtf8ToHex (utf8: string, noPrefix?: boolean): string { | ||
const arrayBuffer = convertUtf8ToArrayBuffer(utf8) | ||
const hex = convertArrayBufferToHex(arrayBuffer, noPrefix) | ||
return hex | ||
} | ||
while (i < array.length) { | ||
const byte: number = array[i] | ||
if (byte < 128) { | ||
chars.push(String.fromCharCode(byte)) | ||
i++ | ||
} else if (byte > 191 && byte < 224) { | ||
chars.push( | ||
String.fromCharCode(((byte & 0x1f) << 6) | (array[i + 1] & 0x3f)) | ||
) | ||
i += 2 | ||
} else { | ||
chars.push( | ||
String.fromCharCode( | ||
((byte & 0x0f) << 12) | | ||
((array[i + 1] & 0x3f) << 6) | | ||
(array[i + 2] & 0x3f) | ||
) | ||
) | ||
i += 3 | ||
} | ||
} | ||
const utf8: string = chars.join('') | ||
return utf8 | ||
export function convertUtf8ToNumber (utf8: string): number { | ||
const num = utils.bigNumberify(utf8).toNumber() | ||
return num | ||
} | ||
export function convertUtf8ToArrayBuffer (utf8: string): ArrayBuffer { | ||
const bytes: number[] = [] | ||
// -- Number ----------------------------------------------- // | ||
let i = 0 | ||
utf8 = encodeURI(utf8) | ||
while (i < utf8.length) { | ||
const byte: number = utf8.charCodeAt(i++) | ||
if (byte === 37) { | ||
bytes.push(parseInt(utf8.substr(i, 2), 16)) | ||
i += 2 | ||
} else { | ||
bytes.push(byte) | ||
} | ||
} | ||
export function convertNumberToBuffer (num: number): Buffer { | ||
const hex = convertNumberToHex(num) | ||
const buffer = convertHexToBuffer(hex) | ||
return buffer | ||
} | ||
const array: Uint8Array = new Uint8Array(bytes) | ||
const arrayBuffer: ArrayBuffer = array.buffer | ||
export function convertNumberToArrayBuffer (num: number): ArrayBuffer { | ||
const hex = convertNumberToHex(num) | ||
const arrayBuffer = convertHexToArrayBuffer(hex) | ||
return arrayBuffer | ||
} | ||
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]) | ||
export function convertNumberToUtf8 (num: number): string { | ||
const utf8 = utils.bigNumberify(num).toString() | ||
return utf8 | ||
} | ||
export function convertNumberToHex (num: number, noPrefix?: boolean): string { | ||
let hex = utils.bigNumberify(num).toHexString() | ||
if (noPrefix) { | ||
hex = removeHexPrefix(hex) | ||
} | ||
let hex: string = bytes.join('') | ||
if (prefix) { | ||
hex = '0x' + hex | ||
} | ||
return hex | ||
} | ||
export function convertHexToArrayBuffer (hex: string): ArrayBuffer { | ||
hex = hex.replace('0x', '') | ||
// -- Hex -------------------------------------------------- // | ||
const bytes: number[] = [] | ||
export function convertHexToBuffer (hex: string): Buffer { | ||
hex = removeHexPrefix(hex) | ||
const buffer = new Buffer(hex, 'hex') | ||
return buffer | ||
} | ||
for (let i = 0; i < hex.length; i += 2) { | ||
bytes.push(parseInt(hex.substr(i, 2), 16)) | ||
} | ||
const array: Uint8Array = new Uint8Array(bytes) | ||
const arrayBuffer: ArrayBuffer = array.buffer | ||
export function convertHexToArrayBuffer (hex: string): ArrayBuffer { | ||
hex = addHexPrefix(hex) | ||
const arrayBuffer = utils.arrayify(hex).buffer | ||
return arrayBuffer | ||
} | ||
export function convertUtf8ToHex (utf8: string, prefix?: boolean): string { | ||
const arrayBuffer = convertUtf8ToArrayBuffer(utf8) | ||
let hex = convertArrayBufferToHex(arrayBuffer) | ||
if (prefix) { | ||
hex = '0x' + hex | ||
} | ||
return hex | ||
} | ||
export function convertHexToUtf8 (hex: string): string { | ||
@@ -166,2 +158,30 @@ const arrayBuffer = convertHexToArrayBuffer(hex) | ||
export function convertHexToNumber (hex: string): number { | ||
const num = utils.bigNumberify(hex).toNumber() | ||
return num | ||
} | ||
// -- Misc ------------------------------------------------- // | ||
export function sanitizeHex (hex: string): string { | ||
hex = removeHexPrefix(hex) | ||
hex = hex.length % 2 !== 0 ? '0' + hex : hex | ||
hex = addHexPrefix(hex) | ||
return hex | ||
} | ||
export function addHexPrefix (hex: string): string { | ||
if (hex.toLowerCase().substring(0, 2) === '0x') { | ||
return hex | ||
} | ||
return '0x' + hex | ||
} | ||
export function removeHexPrefix (hex: string): string { | ||
if (hex.toLowerCase().substring(0, 2) === '0x') { | ||
return hex.substring(2) | ||
} | ||
return hex | ||
} | ||
export function payloadId (): number { | ||
@@ -191,32 +211,4 @@ const datePart: number = new Date().getTime() * Math.pow(10, 3) | ||
export const isHexStrict = (hex: string) => { | ||
return ( | ||
(typeof hex === 'string' || isNumber(hex)) && /^(-)?0x[0-9a-f]*$/i.test(hex) | ||
) | ||
} | ||
export function keccak256 (data?: string): string { | ||
if (!data) { | ||
return '' | ||
} | ||
return '0x' + keccak_256(data) | ||
} | ||
export const toChecksumAddress = (address: string) => { | ||
if (typeof address === 'undefined') { | ||
return '' | ||
} | ||
address = address.toLowerCase().replace('0x', '') | ||
const addressHash = keccak256(address).replace('0x', '') | ||
let checksumAddress = '0x' | ||
for (let i = 0; i < address.length; i++) { | ||
if (parseInt(addressHash[i], 16) > 7) { | ||
checksumAddress += address[i].toUpperCase() | ||
} else { | ||
checksumAddress += address[i] | ||
} | ||
} | ||
return checksumAddress | ||
return utils.getAddress(address) | ||
} | ||
@@ -430,15 +422,2 @@ | ||
export function sanitizeHex (hex: string): string { | ||
hex = hex.substring(0, 2) === '0x' ? hex.substring(2) : hex | ||
if (hex === '') { | ||
return '' | ||
} | ||
hex = hex.length % 2 !== 0 ? '0' + hex : hex | ||
return '0x' + hex | ||
} | ||
export function removeHexPrefix (hex: string): string { | ||
return hex.toLowerCase().replace('0x', '') | ||
} | ||
export function promisify ( | ||
@@ -470,4 +449,4 @@ originalFn: (...args: any[]) => void, | ||
export function parsePersonalSign (params: string[]): string[] { | ||
if (!isHexStrict(params[1])) { | ||
params[1] = convertUtf8ToHex(params[1], true) | ||
if (!utils.isHexString(params[1])) { | ||
params[1] = convertUtf8ToHex(params[1]) | ||
} | ||
@@ -485,3 +464,3 @@ return params | ||
function parseHexValues (str: string) { | ||
if (isHexStrict(str)) { | ||
if (utils.isHexString(str)) { | ||
return str | ||
@@ -488,0 +467,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1128129
2
1796
55
1
+ Addedethers@^4.0.27
+ Addedaes-js@3.0.0(transitive)
+ Addedbn.js@4.12.0(transitive)
+ Addedbrorand@1.1.0(transitive)
+ Addedelliptic@6.5.4(transitive)
+ Addedethers@4.0.49(transitive)
+ Addedhash.js@1.1.3(transitive)
+ Addedhmac-drbg@1.0.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedjs-sha3@0.5.7(transitive)
+ Addedminimalistic-assert@1.0.1(transitive)
+ Addedminimalistic-crypto-utils@1.0.1(transitive)
+ Addedscrypt-js@2.0.4(transitive)
+ Addedsetimmediate@1.0.4(transitive)
+ Addeduuid@2.0.1(transitive)
+ Addedxmlhttprequest@1.8.0(transitive)
- Removedjs-sha3@^0.8.0
- Removedlodash.isnumber@^3.0.3
- Removedjs-sha3@0.8.0(transitive)
- Removedlodash.isnumber@3.0.3(transitive)