Prefer usage of utils

This commit is contained in:
Franck Royer 2021-07-09 15:57:47 +10:00
parent b70f7c5a95
commit 63923f4368
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 8 additions and 7 deletions

View File

@ -1,4 +1,5 @@
import { KeyPair } from '../crypto'; import { KeyPair } from '../crypto';
import { bufToHex, hexToBuf } from 'js-waku/lib/utils';
/** /**
* Save keypair to storage, encrypted with password * Save keypair to storage, encrypted with password
@ -10,9 +11,9 @@ export async function saveKeyPairToStorage(
const { salt, iv, cipher } = await encryptKey(ethDmKeyPair, password); const { salt, iv, cipher } = await encryptKey(ethDmKeyPair, password);
const data = { const data = {
salt: new Buffer(salt).toString('hex'), salt: bufToHex(salt),
iv: new Buffer(iv).toString('hex'), iv: bufToHex(iv),
cipher: new Buffer(cipher).toString('hex'), cipher: bufToHex(cipher),
}; };
localStorage.setItem('cipherEthDmKeyPair', JSON.stringify(data)); localStorage.setItem('cipherEthDmKeyPair', JSON.stringify(data));
@ -28,9 +29,9 @@ export async function loadKeyPairFromStorage(
if (!str) return; if (!str) return;
const data = JSON.parse(str); const data = JSON.parse(str);
const salt = new Buffer(data.salt, 'hex'); const salt = hexToBuf(data.salt);
const iv = new Buffer(data.iv, 'hex'); const iv = hexToBuf(data.iv);
const cipher = new Buffer(data.cipher, 'hex'); const cipher = hexToBuf(data.cipher);
return await decryptKey(salt, iv, cipher, password); return await decryptKey(salt, iv, cipher, password);
} }

View File

@ -2,7 +2,7 @@ export function hexToBuf(str: string): Buffer {
return Buffer.from(str.replace(/^0x/i, ''), 'hex'); 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); const _buf = Buffer.from(buf);
return _buf.toString('hex'); return _buf.toString('hex');
} }