From 82fcc830ec97f1a026192a08dec33a6b30fdfc94 Mon Sep 17 00:00:00 2001 From: saul Date: Mon, 27 Mar 2023 11:23:36 +1300 Subject: [PATCH] Replace usage of Buffer with Uint8Array. --- src/identities/providers/orbitdb.js | 3 ++- src/key-store.js | 31 ++++++++++++++++------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/identities/providers/orbitdb.js b/src/identities/providers/orbitdb.js index 4c7af6a..b46cfe7 100644 --- a/src/identities/providers/orbitdb.js +++ b/src/identities/providers/orbitdb.js @@ -1,3 +1,4 @@ +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import IdentityProvider from './interface.js' import { signMessage, verifyMessage } from '../../key-store.js' @@ -22,7 +23,7 @@ class OrbitDBIdentityProvider extends IdentityProvider { throw new Error('id is required') } const key = await this._keystore.getKey(id) || await this._keystore.createKey(id) - return Buffer.from(key.public.marshal()).toString('hex') + return uint8ArrayToString(key.public.marshal(), 'base16') } async signIdentity (data, { id } = {}) { diff --git a/src/key-store.js b/src/key-store.js index f749027..c73e91b 100644 --- a/src/key-store.js +++ b/src/key-store.js @@ -1,5 +1,7 @@ import * as crypto from '@libp2p/crypto' -import { Buffer } from 'safe-buffer' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' +import { compare as uint8ArrayCompare } from 'uint8arrays/compare' import ComposedStorage from './storage/composed.js' import LevelStorage from './storage/level.js' import LRUStorage from './storage/lru.js' @@ -18,16 +20,16 @@ const verifySignature = async (signature, publicKey, data) => { throw new Error('Given input data was undefined') } - if (!Buffer.isBuffer(data)) { - data = Buffer.from(data) + if (!(data instanceof Uint8Array)) { + data = typeof data === "string" ? uint8ArrayFromString(data) : new Uint8Array(data) } const isValid = (key, msg, sig) => key.verify(msg, sig) let res = false try { - const pubKey = unmarshalPubKey(Buffer.from(publicKey, 'hex')) - res = await isValid(pubKey, data, Buffer.from(signature, 'hex')) + const pubKey = unmarshalPubKey(uint8ArrayFromString(publicKey, 'base16')) + res = await isValid(pubKey, data, uint8ArrayFromString(signature, 'base16')) } catch (e) { // Catch error: sig length wrong } @@ -44,11 +46,11 @@ const signMessage = async (key, data) => { throw new Error('Given input data was undefined') } - if (!Buffer.isBuffer(data)) { - data = Buffer.from(data) + if (!(data instanceof Uint8Array)) { + data = typeof data === "string" ? uint8ArrayFromString(data) : new Uint8Array(data) } - return Buffer.from(await key.sign(data)).toString('hex') + return uint8ArrayToString(await key.sign(data), 'base16') } const verifiedCache = await LRUStorage({ size: 1000 }) @@ -66,7 +68,7 @@ const verifyMessage = async (signature, publicKey, data) => { } } else { const compare = (cached, data) => { - const match = Buffer.isBuffer(data) ? Buffer.compare(cached, data) === 0 : cached.toString() === data.toString() + const match = data instanceof Uint8Array ? uint8ArrayCompare(cached, data) === 0 : cached.toString() === data.toString() return match } res = cached.publicKey === publicKey && compare(cached.data, data) @@ -127,8 +129,8 @@ const KeyStore = async ({ storage, path } = {}) => { const pubKey = keys.public.marshal() const key = { - publicKey: Buffer.from(pubKey), - privateKey: Buffer.from(keys.marshal()) + publicKey: pubKey, + privateKey: keys.marshal() } await addKey(id, key) @@ -161,9 +163,10 @@ const KeyStore = async ({ storage, path } = {}) => { if (formats.indexOf(format) === -1) { throw new Error('Supported formats are `hex` and `buffer`') } - let pubKey = keys.public.marshal() - pubKey = Buffer.from(pubKey) - return format === 'buffer' ? pubKey : pubKey.toString('hex') + + const pubKey = keys.public.marshal() + + return format === 'buffer' ? pubKey : uint8ArrayToString(pubKey, 'base16') } return {