Merge pull request #53 from saul-jb/feat/buffer-refactor

feat: remove dependence on buffer/safe-buffer
This commit is contained in:
Haad 2023-03-27 07:05:42 +03:00 committed by GitHub
commit 893e34451d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 33 deletions

6
package-lock.json generated
View File

@ -17,7 +17,7 @@
"lru": "^3.1.0",
"multiformats": "^11.0.1",
"p-queue": "^7.3.4",
"safe-buffer": "^5.2.1"
"uint8arrays": "^4.0.3"
},
"devDependencies": {
"assert": "^2.0.0",
@ -11640,6 +11640,7 @@
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
"dev": true,
"funding": [
{
"type": "github",
@ -22433,7 +22434,8 @@
"safe-buffer": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
"dev": true
},
"safe-regex-test": {
"version": "1.0.0",

View File

@ -26,7 +26,7 @@
"lru": "^3.1.0",
"multiformats": "^11.0.1",
"p-queue": "^7.3.4",
"safe-buffer": "^5.2.1"
"uint8arrays": "^4.0.3"
},
"devDependencies": {
"assert": "^2.0.0",

View File

@ -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 } = {}) {

View File

@ -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 {

View File

@ -1,4 +1,5 @@
import * as crypto from '@libp2p/crypto'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { Identities, KeyStore } from '../../src/index.js'
const unmarshal = crypto.keys.supportedKeys.secp256k1.unmarshalSecp256k1PrivateKey
@ -51,10 +52,10 @@ before(async () => {
]
for (let user of users) {
const privateKey1 = unmarshal(Buffer.from(user.privateKey, 'hex'))
const privateKey2 = unmarshal(Buffer.from(user.identity.privateKey, 'hex'))
await keystore.addKey(user.id, { privateKey: Buffer.from(privateKey1.marshal()) })
await keystore.addKey(user.identity.id, { privateKey: Buffer.from(privateKey2.marshal()) })
const privateKey1 = unmarshal(uint8ArrayFromString(user.privateKey, 'base16'))
const privateKey2 = unmarshal(uint8ArrayFromString(user.identity.privateKey, 'base16'))
await keystore.addKey(user.id, { privateKey: privateKey1.marshal() })
await keystore.addKey(user.identity.id, { privateKey: privateKey2.marshal() })
}
await keystore.close()

View File

@ -1,6 +1,7 @@
import assert from 'assert'
import rmrf from 'rimraf'
import { copy } from 'fs-extra'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import KeyStore, { signMessage, verifyMessage } from '../../src/key-store.js'
import Identities, { addIdentityProvider } from '../../src/identities/identities.js'
import Identity from '../../src/identities/identity.js'
@ -34,7 +35,7 @@ describe('Identities', function () {
identities = await Identities({ path: keysPath })
identity = await identities.createIdentity({ id })
const key = await identities.keystore.getKey(id)
const externalId = Buffer.from(key.public.marshal()).toString('hex')
const externalId = uint8ArrayToString(key.public.marshal(), 'base16')
assert.strictEqual(identity.id, externalId)
})
})
@ -87,7 +88,7 @@ describe('Identities', function () {
identity = await identities.createIdentity({ id })
keystore = identities.keystore
const key = await keystore.getKey(id)
const externalId = Buffer.from(key.public.marshal()).toString('hex')
const externalId = uint8ArrayToString(key.public.marshal(), 'base16')
assert.strictEqual(identity.id, externalId)
})
@ -98,7 +99,7 @@ describe('Identities', function () {
it('has the correct public key', async () => {
const key = await keystore.getKey(id)
const externalId = Buffer.from(key.public.marshal()).toString('hex')
const externalId = uint8ArrayToString(key.public.marshal(), 'base16')
const signingKey = await keystore.getKey(externalId)
assert.notStrictEqual(signingKey, undefined)
assert.strictEqual(identity.publicKey, keystore.getPublic(signingKey))
@ -106,10 +107,10 @@ describe('Identities', function () {
it('has a signature for the id', async () => {
const key = await keystore.getKey(id)
const externalId = Buffer.from(key.public.marshal()).toString('hex')
const externalId = uint8ArrayToString(key.public.marshal(), 'base16')
const signingKey = await keystore.getKey(externalId)
const idSignature = await signMessage(signingKey, externalId)
const publicKey = Buffer.from(signingKey.public.marshal()).toString('hex')
const publicKey = uint8ArrayToString(signingKey.public.marshal(), 'base16')
const verifies = await verifyMessage(idSignature, publicKey, externalId)
assert.strictEqual(verifies, true)
assert.strictEqual(identity.signatures.id, idSignature)
@ -117,7 +118,7 @@ describe('Identities', function () {
it('has a signature for the publicKey', async () => {
const key = await keystore.getKey(id)
const externalId = Buffer.from(key.public.marshal()).toString('hex')
const externalId = uint8ArrayToString(key.public.marshal(), 'base16')
const signingKey = await keystore.getKey(externalId)
const idSignature = await signMessage(signingKey, externalId)
const externalKey = await keystore.getKey(id)
@ -152,7 +153,7 @@ describe('Identities', function () {
it('has the correct id', async () => {
const key = await savedKeysKeyStore.getKey(id)
assert.strictEqual(identity.id, Buffer.from(key.public.marshal()).toString('hex'))
assert.strictEqual(identity.id, uint8ArrayToString(key.public.marshal(), 'base16'))
})
it('has the correct public key', async () => {

View File

@ -1,6 +1,7 @@
import { strictEqual, deepStrictEqual, deepEqual } from 'assert'
import * as crypto from '@libp2p/crypto'
import { Buffer } from 'safe-buffer'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import rmrf from 'rimraf'
import { copy } from 'fs-extra'
import KeyStore, { signMessage, verifyMessage } from '../src/key-store.js'
@ -150,8 +151,8 @@ describe('KeyStore', () => {
let privateKeyBuffer, publicKeyBuffer, unmarshalledPrivateKey
before(async () => {
privateKeyBuffer = Buffer.from(privateKey, 'hex')
publicKeyBuffer = Buffer.from(publicKey, 'hex')
privateKeyBuffer = uint8ArrayFromString(privateKey, 'base16')
publicKeyBuffer = uint8ArrayFromString(publicKey, 'base16')
unmarshalledPrivateKey = await unmarshal(privateKeyBuffer)
})
@ -296,7 +297,7 @@ describe('KeyStore', () => {
const expected = '02e7247a4c155b63d182a23c70cb6fe8ba2e44bc9e9d62dc45d4c4167ccde95944'
const publicKey = await keystore.getPublic(key, { format: 'buffer' })
deepStrictEqual(publicKey.toString('hex'), expected)
deepStrictEqual(uint8ArrayToString(publicKey, 'base16'), expected)
})
it('throws an error if no keys are passed', async () => {