Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit d7d0499

Browse files
committed
Merge branch 'master' into fix/rpc-relay
2 parents e624cd5 + 042d555 commit d7d0499

File tree

19 files changed

+1445
-3109
lines changed

19 files changed

+1445
-3109
lines changed

declarations.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ declare module 'hypercore' {
392392
sign?: (message: Uint8Array) => Uint8Array;
393393
verify: (message: Uint8Array, signature: Uint8Array) => boolean;
394394
}
395-
export = class Hypercore extends EventEmitter<'close'> {
395+
class Hypercore extends EventEmitter<'close'> {
396396
constructor(storage: any, key?: Opts | Uint8Array, opts?: Opts);
397397

398398
length: number;
@@ -443,6 +443,8 @@ Populated after ready has been emitted. Will be null before the event.
443443

444444
sessions: Hypercore[];
445445
};
446+
447+
export = Hypercore
446448
}
447449

448450
// file://./node_modules/hyperblobs/index.js

package-lock.json

Lines changed: 1319 additions & 2480 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/drive/README.md

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,3 @@
22

33
[DEPRECATED] use `@synonymdev/slashtags-core-data` instead.
44

5-
Drivestore is a Hyperdrive factory that makes it easier to manage large collections of named Hyperdrives.
6-
7-
## Features
8-
9-
- Public unencrypted drive
10-
- Creates and keep track of all created encrypted private drives
11-
12-
## Installation
13-
14-
```
15-
npm install @synonymdev/slashdrive
16-
```
17-
18-
## Usage
19-
20-
```js
21-
import Corestore from 'corestore'
22-
import Drivestore from '@synonymdev/slashdrive'
23-
24-
const corestore = new Corestore('./corestore_dir')
25-
const store = new Drivestore(corestore, keyPair)
26-
27-
const publicDrive = store.get('public') // or store.get()
28-
29-
const privateDrive = store.get('foo') // returns an encrypted Hyperdrive
30-
```
31-
32-
## API
33-
34-
#### `const drivestore = new Drivestore(corestore, keyPair)`
35-
36-
Create new Drivestore.
37-
38-
- `corestore` must be an instance of [Corestore](https://github.com/hypercore-protocol/corestore).
39-
40-
If the instance is a [namespace](https://github.com/hypercore-protocol/corestore#const-store--storenamespacename), the internal corestore will reset its namespace to the `DEFAULT_NAMESPACE` (32 0-bytes).
41-
42-
- `keyPair` public and secret keys to create the public Hyperdrive, the secret key will be used as the `primaryKey` for the internal corestore.
43-
44-
#### `await drivestore.ready()`
45-
46-
Awaits opening metadata hypercore. Useful before [async iterating](#for-await-let-name-of-drivestore) over all created drives.
47-
48-
#### `const hyperdrive = drivestore.get([name])`
49-
50-
Returns an encrypted [Hyperdrive](https://github.com/hypercore-protocol/hyperdrive-next) for a given name.
51-
52-
If `name` is undefined or equal to `/public` it will return a public unencrypted drive, by the same keypair passed to the contsructor.
53-
54-
#### `const stream = drivestore.replicate(stream)`
55-
56-
Same as [drivestore.corestore.replicate(stream)](https://github.com/hypercore-protocol/corestore#const-stream--storereplicateoptsorstream)

packages/drive/index.js

Lines changed: 11 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,25 @@
1-
const Hyperdrive = require('hyperdrive')
2-
const Hyperbee = require('hyperbee')
3-
const b4a = require('b4a')
4-
const safetyCatch = require('safety-catch')
5-
6-
const METADATA_KEY = 'slashtags-drivestore-metadata'
7-
81
class Drivestore {
92
/**
10-
* @param {import('corestore')} corestore
11-
* @param {import('hyperdht').KeyPair} keyPair
3+
* @param {import('@synonymdev/slashtags-core-data')} coreData
124
*/
13-
constructor (corestore, keyPair) {
14-
this.fava = Math.random()
15-
this.keyPair = keyPair
16-
/** @type {import('corestore')} */
17-
this.corestore = corestore.session({ primaryKey: this.keyPair.secretKey, namespace: null })
18-
19-
const metadataCore = this.corestore.get({
20-
name: METADATA_KEY,
21-
encryptionKey: this.keyPair.secretKey
22-
})
23-
this._metadata = new Hyperbee(metadataCore, { keyEncoding: 'utf8' })
24-
this._drives = this._metadata.sub('drives')
25-
26-
this._opening = this._open().catch(safetyCatch)
5+
constructor (coreData) {
6+
this._coreData = coreData
7+
this.corestore = this._coreData._corestoreSession
278
}
289

29-
/** @returns {import('hyperbee').Iterator<{name: string}>} */
10+
/**
11+
* @deprecated
12+
*/
3013
[Symbol.asyncIterator] () {
31-
if (!this.opened) return emptyIterator
32-
const iterator = this._drives.createReadStream()[Symbol.asyncIterator]()
33-
return {
34-
async next () {
35-
const node = await iterator.next()
36-
const value = node.value
37-
return { done: node.done, value: value && { name: value.key } }
38-
}
39-
}
14+
return new Error('not supported')
4015
}
4116

4217
get closed () {
43-
return this.corestore._root._closing
44-
}
45-
46-
async _open () {
47-
await this._drives.feed.ready()
48-
this.opened = true
18+
return this.corestore._root.closing
4919
}
5020

5121
ready () {
52-
return this._opening
22+
return this._coreData.ready()
5323
}
5424

5525
/** @param {Parameters<import('corestore')['replicate']>} args */
@@ -61,53 +31,8 @@ class Drivestore {
6131
* Get a Hyperdrive by its name.
6232
*/
6333
get (name = 'public') {
64-
validateName(name)
65-
const ns = this.corestore.namespace(name).session({ primaryKey: this.keyPair.secretKey })
66-
const _preload = ns._preload.bind(ns)
67-
ns._preload = (opts) => this._preload.bind(this)(opts, _preload, ns, name)
68-
return new Hyperdrive(ns)
34+
return this._coreData._getLocalDrive(name)
6935
}
70-
71-
/**
72-
* Set the correct and current key and encryption Key (enables future key rotation)
73-
* @param {Parameters<import('corestore')['get']>[0]} opts
74-
* @param {*} preload orginal ns._preload
75-
* @param {import('corestore')} ns
76-
* @param {string} name
77-
* @returns {Promise<any>}
78-
*/
79-
async _preload (opts, preload, ns, name) {
80-
const isPublic = name === 'public'
81-
82-
// Get keyPair programatically from name
83-
const { from } = await preload(opts)
84-
85-
// public drive needs no encryption
86-
// No need currently to save a record about the public drive
87-
if (isPublic) {
88-
if (opts.name !== 'db') return { from }
89-
const session = this.corestore.get({ keyPair: this.keyPair })
90-
await session.ready()
91-
return { from: session }
92-
}
93-
94-
this._drives.ready().then(async () => {
95-
const saved = await this._drives.get(name)
96-
if (!saved) await this._drives.put(name, b4a.from(''))
97-
// TODO enable key rotation, where we overwrite keys, or use saved ones.
98-
// TODO block closing drivestore before this update is flushed
99-
})
100-
101-
// Add encryption keys for non public drives
102-
return { from, encryptionKey: ns._namespace }
103-
}
104-
}
105-
106-
/** @param {string} name */
107-
function validateName (name) {
108-
if (!/^[0-9a-zA-Z-._ ]*$/.test(name)) throw new Error('Invalid drive name')
10936
}
11037

111-
const emptyIterator = { async next () { return { done: true, value: null } } }
112-
11338
module.exports = Drivestore

packages/drive/package.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,12 @@
2828
"types",
2929
"!**/*.tsbuildinfo"
3030
],
31-
"dependencies": {
32-
"b4a": "^1.6.0",
33-
"corestore": "^6.2.1",
34-
"hyperbee": "^2.0.1",
35-
"hyperdrive": "^11.0.0-alpha.5",
36-
"safety-catch": "^1.0.2"
37-
},
3831
"devDependencies": {
32+
"@synonymdev/slashtags-core-data": "^1.0.0-alpha.9",
33+
"b4a": "^1.6.4",
3934
"brittle": "^3.0.2",
4035
"depcheck": "^1.4.3",
4136
"hypercore-crypto": "^3.3.0",
42-
"random-access-memory": "^5.0.1",
4337
"standard": "^17.0.0",
4438
"typescript": "^4.8.2"
4539
}

packages/drive/test/get.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const test = require('brittle')
2-
const Corestore = require('corestore')
32
const crypto = require('hypercore-crypto')
43
const b4a = require('b4a')
4+
const CoreData = require('@synonymdev/slashtags-core-data')
55

66
const Drivestore = require('../index.js')
7-
const { tmpdir } = require('./helpers/index.js')
87

98
test('get - public drive', async (t) => {
109
const keyPair = crypto.keyPair()
11-
const drivestore = new Drivestore(new Corestore(tmpdir()), keyPair)
10+
const coreData = new CoreData({ keyPair })
11+
const drivestore = new Drivestore(coreData)
1212

1313
const publicA = drivestore.get('public')
1414
await publicA.ready()
@@ -17,8 +17,6 @@ test('get - public drive', async (t) => {
1717
const publicB = drivestore.get()
1818
await publicB.ready()
1919
t.alike(publicB.key, keyPair.publicKey)
20-
21-
t.not(publicA, publicB, 'should return a session')
2220
t.alike(publicA.key, publicB.key, 'same public key')
2321
t.absent(publicA.core.encryptionKey, 'do not encrypet public drive')
2422
t.absent(publicB.core.encryptionKey, 'do not encrypet public drive')
@@ -36,11 +34,14 @@ test('get - public drive', async (t) => {
3634
const buf = b4a.from('bar')
3735
await publicA.put('foo', buf)
3836
t.alike(await publicA.get('foo'), buf)
37+
38+
coreData.close()
3939
})
4040

4141
test('get - private drive basic', async (t) => {
4242
const keyPair = crypto.keyPair()
43-
const drivestore = new Drivestore(new Corestore(tmpdir()), keyPair)
43+
const coreData = new CoreData({ keyPair })
44+
const drivestore = new Drivestore(coreData)
4445

4546
const foo = drivestore.get('foo')
4647
const bar = drivestore.get('bar')
@@ -67,4 +68,6 @@ test('get - private drive basic', async (t) => {
6768
t.ok(foo.blobs?.core.writable)
6869
t.unlike(foo.blobs?.core.key, foo.key)
6970
t.unlike(bar.blobs?.core.key, foo.key)
71+
72+
coreData.close()
7073
})

packages/drive/test/keys.js

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,30 @@
11
const test = require('brittle')
2-
const Corestore = require('corestore')
32
const crypto = require('hypercore-crypto')
4-
const path = require('path')
5-
const fs = require('fs')
3+
const CoreData = require('@synonymdev/slashtags-core-data')
64

75
const Drivestore = require('../index.js')
8-
const { tmpdir } = require('./helpers/index.js')
9-
10-
test('dont store secretKey at rest', async (t) => {
11-
const dir = tmpdir()
12-
const corestore = new Corestore(dir)
13-
await corestore.ready()
14-
15-
const drivestore = new Drivestore(corestore, crypto.keyPair())
16-
await drivestore.ready()
17-
18-
t.ok(drivestore.corestore.primaryKey)
19-
20-
await corestore.close()
21-
const stored = fs.readFileSync(path.join(dir, 'primary-key'))
22-
t.unlike(stored, drivestore.corestore.primaryKey)
23-
})
246

257
test('unique private drives for unique keyPairs', async (t) => {
26-
const dir = tmpdir()
27-
const corestore = new Corestore(dir)
28-
await corestore.ready()
29-
308
const kp1 = crypto.keyPair()
31-
const ds1 = new Drivestore(corestore, kp1)
9+
const ds1 = new Drivestore(new CoreData({ keyPair: kp1 }))
3210
await ds1.ready()
3311
const ds1Public = ds1.get()
3412
const ds1Private = ds1.get('contacts')
3513

3614
const kp2 = crypto.keyPair()
37-
const ds2 = new Drivestore(corestore, kp2)
15+
const ds2 = new Drivestore(new CoreData({ keyPair: kp2 }))
3816
await ds2.ready()
3917
const ds2Public = ds2.get('contacts')
4018
const ds2Private = ds2.get('contacts')
4119

4220
await Promise.all([ds1Public, ds2Public, ds1Private, ds2Private].map(d => d.ready()))
4321

44-
t.unlike(ds1.keyPair.secretKey, ds2.keyPair.secretKey)
45-
4622
t.ok(ds1Public.key)
4723
t.unlike(ds2Public.key, ds1Public.key)
4824

4925
t.ok(ds1Private.key)
5026
t.unlike(ds2Private.key, ds1Private.key)
27+
28+
ds1._coreData.close()
29+
ds2._coreData.close()
5130
})

0 commit comments

Comments
 (0)