-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add identity blockstore (#298)
Add a blockstore implementation that supports identity CIDs.
- Loading branch information
1 parent
bf0b007
commit b8dce49
Showing
7 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { BaseBlockstore } from './base.js' | ||
import * as Errors from './errors.js' | ||
import type { Pair } from 'interface-blockstore' | ||
import type { Await, AwaitIterable } from 'interface-store' | ||
import type { CID } from 'multiformats/cid' | ||
|
||
// https://github.com/multiformats/multicodec/blob/d06fc6194710e8909bac64273c43f16b56ca4c34/table.csv#L2 | ||
const IDENTITY_CODEC = 0x00 | ||
|
||
export class IdentityBlockstore extends BaseBlockstore { | ||
put (key: CID): CID { | ||
return key | ||
} | ||
|
||
get (key: CID): Await<Uint8Array> { | ||
if (key.code === IDENTITY_CODEC) { | ||
return key.multihash.digest | ||
} | ||
|
||
throw Errors.notFoundError() | ||
} | ||
|
||
has (key: CID): boolean { | ||
return key.code === IDENTITY_CODEC | ||
} | ||
|
||
delete (): void { | ||
|
||
} | ||
|
||
* getAll (): AwaitIterable<Pair> { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* eslint-env mocha */ | ||
|
||
import { expect } from 'aegir/chai' | ||
import drain from 'it-drain' | ||
import { CID } from 'multiformats/cid' | ||
import * as raw from 'multiformats/codecs/raw' | ||
import { identity } from 'multiformats/hashes/identity' | ||
import { sha256 } from 'multiformats/hashes/sha2' | ||
import { IdentityBlockstore } from '../src/identity.js' | ||
import type { Blockstore } from 'interface-blockstore' | ||
|
||
describe('identity', () => { | ||
let blockstore: Blockstore | ||
|
||
beforeEach(() => { | ||
blockstore = new IdentityBlockstore() | ||
}) | ||
|
||
it('has an identity CID', () => { | ||
const block = Uint8Array.from([0, 1, 2, 3, 4]) | ||
const multihash = identity.digest(block) | ||
const cid = CID.createV1(identity.code, multihash) | ||
|
||
expect(blockstore.has(cid)).to.be.true() | ||
expect(blockstore.get(cid)).to.equalBytes(block) | ||
}) | ||
|
||
it('does not have a non-identity CID', async () => { | ||
const block = Uint8Array.from([0, 1, 2, 3, 4]) | ||
const multihash = await sha256.digest(block) | ||
const cid = CID.createV1(raw.code, multihash) | ||
|
||
expect(blockstore.has(cid)).to.be.false() | ||
|
||
await blockstore.put(cid, block) | ||
|
||
expect(blockstore.has(cid)).to.be.false() | ||
}) | ||
|
||
it('cannot delete an identity CID', async () => { | ||
const block = Uint8Array.from([0, 1, 2, 3, 4]) | ||
const multihash = identity.digest(block) | ||
const cid = CID.createV1(identity.code, multihash) | ||
|
||
await blockstore.delete(cid) | ||
|
||
expect(blockstore.has(cid)).to.be.true() | ||
}) | ||
|
||
it('cannot delete many identity CIDs', async () => { | ||
const block = Uint8Array.from([0, 1, 2, 3, 4]) | ||
const multihash = identity.digest(block) | ||
const cid = CID.createV1(identity.code, multihash) | ||
|
||
await drain(blockstore.deleteMany([cid])) | ||
|
||
expect(blockstore.has(cid)).to.be.true() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters