mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-10-07 22:57:07 +00:00
feat: Identity storage.
This commit is contained in:
32
src/identity-storage.js
Normal file
32
src/identity-storage.js
Normal file
@@ -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
|
||||
@@ -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 }
|
||||
|
||||
Reference in New Issue
Block a user