test: Load keys into storage and retrieve from keystore.

This commit is contained in:
Hayden Young 2023-03-06 22:21:17 +00:00
parent 1fb02ea4cb
commit 885fc5f02c
2 changed files with 38 additions and 10 deletions

View File

@ -74,6 +74,8 @@ const verifyMessage = async (signature, publicKey, data) => {
return res return res
} }
const defaultPath = './keystore'
/** /**
* Creates an instance of KeyStore. * Creates an instance of KeyStore.
* @param {Object} options Various options to use when instantiating KeyStore. * @param {Object} options Various options to use when instantiating KeyStore.
@ -82,7 +84,6 @@ const verifyMessage = async (signature, publicKey, data) => {
* @return {KeyStore} An instance of KeyStore. * @return {KeyStore} An instance of KeyStore.
*/ */
const KeyStore = async ({ storage, path } = {}) => { const KeyStore = async ({ storage, path } = {}) => {
const defaultPath = './keystore'
storage = storage || await ComposedStorage(await LevelStorage({ path: path || defaultPath }), await LRUStorage({ size: 1000 })) storage = storage || await ComposedStorage(await LevelStorage({ path: path || defaultPath }), await LRUStorage({ size: 1000 }))
const close = async () => { const close = async () => {
@ -172,9 +173,7 @@ const KeyStore = async ({ storage, path } = {}) => {
addKey, addKey,
createKey, createKey,
getKey, getKey,
getPublic, getPublic
defaultPath,
storage
} }
} }

View File

@ -1,10 +1,13 @@
import { strictEqual, deepStrictEqual } from 'assert' import { strictEqual, deepStrictEqual } from 'assert'
import * as crypto from '@libp2p/crypto'
import { Buffer } from 'safe-buffer'
import rmrf from 'rimraf' import rmrf from 'rimraf'
import { copy, pathExists } from 'fs-extra' import { copy } from 'fs-extra'
import KeyStore, { signMessage, verifyMessage } from '../src/key-store.js' import KeyStore, { signMessage, verifyMessage } from '../src/key-store.js'
import LevelStorage from '../src/storage/level.js' import LevelStorage from '../src/storage/level.js'
import testKeysPath from './fixtures/test-keys-path.js ' import testKeysPath from './fixtures/test-keys-path.js '
const defaultPath = './keystore'
const keysPath = './testkeys' const keysPath = './testkeys'
describe('KeyStore', () => { describe('KeyStore', () => {
@ -23,7 +26,7 @@ describe('KeyStore', () => {
afterEach(async () => { afterEach(async () => {
if (keystore) { if (keystore) {
await keystore.close() await keystore.close()
await rmrf(keystore.defaultPath) await rmrf(defaultPath)
} }
}) })
@ -83,6 +86,7 @@ describe('KeyStore', () => {
it('gets a key', async () => { it('gets a key', async () => {
const id = 'key1' const id = 'key1'
const keys = await keystore.createKey(id) const keys = await keystore.createKey(id)
deepStrictEqual(await keystore.getKey(id), keys) deepStrictEqual(await keystore.getKey(id), keys)
}) })
@ -139,20 +143,36 @@ describe('KeyStore', () => {
}) })
describe('Options', () => { describe('Options', () => {
const unmarshal = crypto.keys.supportedKeys.secp256k1.unmarshalSecp256k1PrivateKey
const privateKey = '198594a8de39fd97017d11996d619b3746211605a9d290964badf58bc79bdb33'
const publicKey = '0260baeaffa1de1e4135e5b395e0380563a622b9599d1b8e012a0f7603f516bdaa'
let privateKeyBuffer, publicKeyBuffer, unmarshalledPrivateKey
before(async () => {
privateKeyBuffer = Buffer.from(privateKey, 'hex')
publicKeyBuffer = Buffer.from(publicKey, 'hex')
unmarshalledPrivateKey = await unmarshal(privateKeyBuffer)
})
describe('Using default options', () => { describe('Using default options', () => {
beforeEach(async () => { beforeEach(async () => {
const storage = await LevelStorage({ path: defaultPath })
await storage.put('private_key1', privateKeyBuffer)
await storage.put('public_key1', publicKeyBuffer)
await storage.close()
keystore = await KeyStore() keystore = await KeyStore()
}) })
afterEach(async () => { afterEach(async () => {
if (keystore) { if (keystore) {
await keystore.close() await keystore.close()
await rmrf(keystore.defaultPath) await rmrf(defaultPath)
} }
}) })
it('loads default storage using default path', async () => { it('loads default storage using default path', async () => {
strictEqual(await pathExists(keystore.defaultPath), true) deepStrictEqual(await keystore.getKey('key1'), unmarshalledPrivateKey)
}) })
}) })
@ -161,6 +181,9 @@ describe('KeyStore', () => {
beforeEach(async () => { beforeEach(async () => {
const storage = await LevelStorage({ path }) const storage = await LevelStorage({ path })
await storage.put('private_key2', privateKeyBuffer)
await storage.put('public_key2', publicKeyBuffer)
keystore = await KeyStore({ storage }) keystore = await KeyStore({ storage })
}) })
@ -172,13 +195,19 @@ describe('KeyStore', () => {
}) })
it('loads custom storage', async () => { it('loads custom storage', async () => {
strictEqual(await pathExists(path), true) deepStrictEqual(await keystore.getKey('key2'), unmarshalledPrivateKey)
}) })
}) })
describe('Setting options.path', () => { describe('Setting options.path', () => {
beforeEach(async () => { beforeEach(async () => {
await copy(testKeysPath, keysPath) await copy(testKeysPath, keysPath)
const storage = await LevelStorage({ path: keysPath })
await storage.put('private_key3', privateKeyBuffer)
await storage.put('public_key3', publicKeyBuffer)
await storage.close()
keystore = await KeyStore({ path: keysPath }) keystore = await KeyStore({ path: keysPath })
}) })
@ -191,7 +220,7 @@ describe('KeyStore', () => {
}) })
it('loads default storage using custom path', async () => { it('loads default storage using custom path', async () => {
strictEqual(await pathExists(keysPath), true) deepStrictEqual(await keystore.getKey('key3'), unmarshalledPrivateKey)
}) })
}) })
}) })