Skip to content

Commit b8dce49

Browse files
authored
feat: add identity blockstore (#298)
Add a blockstore implementation that supports identity CIDs.
1 parent bf0b007 commit b8dce49

File tree

7 files changed

+130
-2
lines changed

7 files changed

+130
-2
lines changed

packages/blockstore-core/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ const store = new TieredBlockstore([
7474
])
7575
```
7676

77+
## Example - IdentityBlockstore
78+
79+
An identity blockstore is one that deals exclusively in Identity CIDs - this is a special CID with the codec [0x00](https://github.com/multiformats/multicodec/blob/d06fc6194710e8909bac64273c43f16b56ca4c34/table.csv#L2) where the multihash digest is the data that makes up the block.
80+
81+
```TypeScript
82+
import { IdentityBlockstore } from 'blockstore-core/identity'
83+
import { CID } from 'multiformats/cid'
84+
85+
const blockstore = new IdentityBlockstore()
86+
87+
blockstore.has(CID.parse('QmFoo')) // false
88+
89+
blockstore.has(CID.parse('bafkqac3imvwgy3zao5xxe3de')) // true
90+
```
91+
7792
# Install
7893

7994
```console

packages/blockstore-core/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
"types": "./dist/src/errors.d.ts",
6464
"import": "./dist/src/errors.js"
6565
},
66+
"./identity": {
67+
"types": "./dist/src/identity.d.ts",
68+
"import": "./dist/src/identity.js"
69+
},
6670
"./memory": {
6771
"types": "./dist/src/memory.d.ts",
6872
"import": "./dist/src/memory.js"

packages/blockstore-core/src/base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export class BaseBlockstore implements Blockstore {
3131
}
3232
}
3333

34-
async delete (key: CID, options?: AbortOptions): Promise<void> {
35-
await Promise.reject(new Error('.delete is not implemented'))
34+
delete (key: CID, options?: AbortOptions): Await<void> {
35+
return Promise.reject(new Error('.delete is not implemented'))
3636
}
3737

3838
async * deleteMany (source: AwaitIterable<CID>, options?: AbortOptions): AwaitIterable<CID> {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { BaseBlockstore } from './base.js'
2+
import * as Errors from './errors.js'
3+
import type { Pair } from 'interface-blockstore'
4+
import type { Await, AwaitIterable } from 'interface-store'
5+
import type { CID } from 'multiformats/cid'
6+
7+
// https://github.com/multiformats/multicodec/blob/d06fc6194710e8909bac64273c43f16b56ca4c34/table.csv#L2
8+
const IDENTITY_CODEC = 0x00
9+
10+
export class IdentityBlockstore extends BaseBlockstore {
11+
put (key: CID): CID {
12+
return key
13+
}
14+
15+
get (key: CID): Await<Uint8Array> {
16+
if (key.code === IDENTITY_CODEC) {
17+
return key.multihash.digest
18+
}
19+
20+
throw Errors.notFoundError()
21+
}
22+
23+
has (key: CID): boolean {
24+
return key.code === IDENTITY_CODEC
25+
}
26+
27+
delete (): void {
28+
29+
}
30+
31+
* getAll (): AwaitIterable<Pair> {
32+
33+
}
34+
}

packages/blockstore-core/src/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@
6565
* // ...etc
6666
* ])
6767
* ```
68+
*
69+
* @example IdentityBlockstore
70+
*
71+
* An identity blockstore is one that deals exclusively in Identity CIDs - this is a special CID with the codec [0x00](https://github.com/multiformats/multicodec/blob/d06fc6194710e8909bac64273c43f16b56ca4c34/table.csv#L2) where the multihash digest is the data that makes up the block.
72+
*
73+
* ```TypeScript
74+
* import { IdentityBlockstore } from 'blockstore-core/identity'
75+
* import { CID } from 'multiformats/cid'
76+
*
77+
* const blockstore = new IdentityBlockstore()
78+
*
79+
* blockstore.has(CID.parse('QmFoo')) // false
80+
*
81+
* blockstore.has(CID.parse('bafkqac3imvwgy3zao5xxe3de')) // true
82+
* ```
6883
*/
6984

7085
import * as ErrorsImport from './errors.js'
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* eslint-env mocha */
2+
3+
import { expect } from 'aegir/chai'
4+
import drain from 'it-drain'
5+
import { CID } from 'multiformats/cid'
6+
import * as raw from 'multiformats/codecs/raw'
7+
import { identity } from 'multiformats/hashes/identity'
8+
import { sha256 } from 'multiformats/hashes/sha2'
9+
import { IdentityBlockstore } from '../src/identity.js'
10+
import type { Blockstore } from 'interface-blockstore'
11+
12+
describe('identity', () => {
13+
let blockstore: Blockstore
14+
15+
beforeEach(() => {
16+
blockstore = new IdentityBlockstore()
17+
})
18+
19+
it('has an identity CID', () => {
20+
const block = Uint8Array.from([0, 1, 2, 3, 4])
21+
const multihash = identity.digest(block)
22+
const cid = CID.createV1(identity.code, multihash)
23+
24+
expect(blockstore.has(cid)).to.be.true()
25+
expect(blockstore.get(cid)).to.equalBytes(block)
26+
})
27+
28+
it('does not have a non-identity CID', async () => {
29+
const block = Uint8Array.from([0, 1, 2, 3, 4])
30+
const multihash = await sha256.digest(block)
31+
const cid = CID.createV1(raw.code, multihash)
32+
33+
expect(blockstore.has(cid)).to.be.false()
34+
35+
await blockstore.put(cid, block)
36+
37+
expect(blockstore.has(cid)).to.be.false()
38+
})
39+
40+
it('cannot delete an identity CID', async () => {
41+
const block = Uint8Array.from([0, 1, 2, 3, 4])
42+
const multihash = identity.digest(block)
43+
const cid = CID.createV1(identity.code, multihash)
44+
45+
await blockstore.delete(cid)
46+
47+
expect(blockstore.has(cid)).to.be.true()
48+
})
49+
50+
it('cannot delete many identity CIDs', async () => {
51+
const block = Uint8Array.from([0, 1, 2, 3, 4])
52+
const multihash = identity.digest(block)
53+
const cid = CID.createV1(identity.code, multihash)
54+
55+
await drain(blockstore.deleteMany([cid]))
56+
57+
expect(blockstore.has(cid)).to.be.true()
58+
})
59+
})

packages/blockstore-core/typedoc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"./src/base.ts",
55
"./src/black-hole.ts",
66
"./src/errors.ts",
7+
"./src/identity.ts",
78
"./src/memory.ts",
89
"./src/tiered.ts"
910
]

0 commit comments

Comments
 (0)