From 5d7c4e407fe8428069b941eb7e4bdbc347e2da7f Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Fri, 10 Feb 2023 00:35:07 +0000 Subject: [PATCH] feat: Identity storage. --- src/identity-storage.js | 32 ++++++++++++++++++++++++++++++++ src/log.js | 3 ++- test/entry.spec.js | 17 ++++++++++++++--- 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 src/identity-storage.js diff --git a/src/identity-storage.js b/src/identity-storage.js new file mode 100644 index 0000000..e545a6e --- /dev/null +++ b/src/identity-storage.js @@ -0,0 +1,32 @@ +import Entry from './entry.js' +import IPFSBlockStorage from './ipfs-block-storage.js' +import * as dagCbor from '@ipld/dag-cbor' +import { sha256 } from 'multiformats/hashes/sha2' +import { base58btc } from 'multiformats/bases/base58' +import * as Block from 'multiformats/block' + +const codec = dagCbor +const hasher = sha256 + +const IdentityStorage = async ({ storage }) => { + const put = async (identity) => { + const { cid, bytes } = await Block.encode({ value: identity.toJSON(), codec, hasher }) + await storage.put(cid.toString(base58btc), bytes) + } + + const get = async (hash) => { + const bytes = await storage.get(hash) + + if (bytes) { + const { value } = await Block.decode({ bytes, codec, hasher }) + return value + } + } + + return { + put, + get + } +} + +export default IdentityStorage diff --git a/src/log.js b/src/log.js index 0d29c2e..c3d9c04 100644 --- a/src/log.js +++ b/src/log.js @@ -8,6 +8,7 @@ import MemoryStorage from './memory-storage.js' import LRUStorage from './lru-storage.js' import LevelStorage from './level-storage.js' import ComposedStorage from './composed-storage.js' +import IdentityStorage from './identity-storage.js' import { isDefined } from './utils/index.js' const { LastWriteWins, NoZeroes } = Sorting @@ -464,4 +465,4 @@ export { Log } export { Sorting } export { Entry } export { DefaultAccessController } -export { IPFSBlockStorage, MemoryStorage, LRUStorage, LevelStorage, ComposedStorage } +export { IPFSBlockStorage, MemoryStorage, LRUStorage, LevelStorage, ComposedStorage, IdentityStorage } diff --git a/test/entry.spec.js b/test/entry.spec.js index a29bb52..64e67cc 100644 --- a/test/entry.spec.js +++ b/test/entry.spec.js @@ -1,10 +1,11 @@ -import { strictEqual } from 'assert' +import { strictEqual, deepStrictEqual } from 'assert' import rimraf from 'rimraf' import { copy } from 'fs-extra' import Entry from '../src/entry.js' import IdentityProvider from 'orbit-db-identity-provider' import Keystore from '../src/Keystore.js' import { config, testAPIs, startIpfs, stopIpfs } from 'orbit-db-test-utils' +import IdentityStorage from '../src/identity-storage.js' import IPFSBlockStorage from '../src/ipfs-block-storage.js' const { sync: rmrf } = rimraf @@ -18,7 +19,7 @@ Object.keys(testAPIs).forEach((IPFS) => { const { identityKeyFixtures, signingKeyFixtures, identityKeysPath, signingKeysPath } = config let testIdentity - let keystore, signingKeystore, identityStore + let keystore, signingKeystore, ipfsBlockStore, identityStore let ipfsd, ipfs before(async () => { @@ -31,7 +32,8 @@ Object.keys(testAPIs).forEach((IPFS) => { keystore = new Keystore(identityKeysPath) signingKeystore = new Keystore(signingKeysPath) - identityStore = await IPFSBlockStorage({ ipfs, pin: true }) + ipfsBlockStore = await IPFSBlockStorage({ ipfs, pin: true }) + identityStore = await IdentityStorage({ storage: ipfsBlockStore }) testIdentity = await createIdentity({ id: 'userA', keystore, signingKeystore, identityStore }) }) @@ -76,6 +78,15 @@ Object.keys(testAPIs).forEach((IPFS) => { strictEqual(entry.refs.length, 0) // strictEqual(entry.hash, expectedHash) }) + + it('retrieves the identity from an entry', async() => { + const expected = testIdentity.toJSON() + const payload = 'hello world' + const entry = await create(testIdentity, 'A', payload) + const entryIdentity = await identityStore.get(entry.identity) + + deepStrictEqual(entryIdentity, expected) + }) it('creates a entry with payload and next', async () => { // const expectedHash = 'zdpuApstRF3DCyuuNhPks8sG2qXPf6BFbMA7EeaGrn9Y6ZEzQ'