diff --git a/examples/eth-dm/src/key_pair_handling/key_pair_storage.ts b/examples/eth-dm/src/key_pair_handling/key_pair_storage.ts index d6fc7d2da6..ccbb425515 100644 --- a/examples/eth-dm/src/key_pair_handling/key_pair_storage.ts +++ b/examples/eth-dm/src/key_pair_handling/key_pair_storage.ts @@ -1,4 +1,5 @@ import { KeyPair } from '../crypto'; +import { bufToHex, hexToBuf } from 'js-waku/lib/utils'; /** * Save keypair to storage, encrypted with password @@ -10,9 +11,9 @@ export async function saveKeyPairToStorage( const { salt, iv, cipher } = await encryptKey(ethDmKeyPair, password); const data = { - salt: new Buffer(salt).toString('hex'), - iv: new Buffer(iv).toString('hex'), - cipher: new Buffer(cipher).toString('hex'), + salt: bufToHex(salt), + iv: bufToHex(iv), + cipher: bufToHex(cipher), }; localStorage.setItem('cipherEthDmKeyPair', JSON.stringify(data)); @@ -28,9 +29,9 @@ export async function loadKeyPairFromStorage( if (!str) return; const data = JSON.parse(str); - const salt = new Buffer(data.salt, 'hex'); - const iv = new Buffer(data.iv, 'hex'); - const cipher = new Buffer(data.cipher, 'hex'); + const salt = hexToBuf(data.salt); + const iv = hexToBuf(data.iv); + const cipher = hexToBuf(data.cipher); return await decryptKey(salt, iv, cipher, password); } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 1f0d7ecb7f..145dfb335b 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -2,7 +2,7 @@ export function hexToBuf(str: string): Buffer { return Buffer.from(str.replace(/^0x/i, ''), 'hex'); } -export function bufToHex(buf: Uint8Array | Buffer): string { +export function bufToHex(buf: Uint8Array | Buffer | ArrayBuffer): string { const _buf = Buffer.from(buf); return _buf.toString('hex'); }