Skip to content

Commit 39a9066

Browse files
Add manifest signature and verification
1 parent 1c1b2eb commit 39a9066

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

src/OrbitDB.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const Keystore = require('orbit-db-keystore')
1212
const IdentityProvider = require('orbit-db-identity-provider')
1313
const IPFSAccessController = require('./ipfs-access-controller')
1414
const OrbitDBAddress = require('./orbit-db-address')
15-
const createDBManifest = require('./db-manifest')
15+
const { createDBManifest, uploadDBManifest, signDBManifest, verifyDBManifest } = require('./db-manifest')
1616
const exchangeHeads = require('./exchange-heads')
1717
const isDefined = require('./utils/is-defined')
1818
const Logger = require('logplease')
@@ -268,12 +268,14 @@ class OrbitDB {
268268
const accessControllerAddress = await accessController.save()
269269

270270
// Save the manifest to IPFS
271-
const manifestHash = await createDBManifest(this._ipfs, name, type, accessControllerAddress)
271+
const manifest = createDBManifest(name, type, accessControllerAddress, this.identity)
272+
const manifestHash = await uploadDBManifest(this._ipfs, manifest)
272273

273274
// Create the database address
274-
const dbAddress = OrbitDBAddress.parse(path.join('/orbitdb', manifestHash, name))
275+
const manifestSignature = await signDBManifest(manifest, this.identity, this.identity.provider)
276+
const dbAddress = OrbitDBAddress.parse(path.join('/orbitdb', manifestHash, name, manifestSignature))
275277

276-
// // Load local cache
278+
// Load local cache
277279
const haveDB = await this._loadCache(directory, dbAddress)
278280
.then(cache => cache ? cache.get(path.join(dbAddress.toString(), '_manifest')) : null)
279281
.then(data => data !== undefined && data !== null)
@@ -346,6 +348,11 @@ class OrbitDB {
346348
const manifest = JSON.parse(dag.toJSON().data)
347349
logger.debug(`Manifest for '${dbAddress}':\n${JSON.stringify(manifest, null, 2)}`)
348350

351+
const isValid = await verifyDBManifest(manifest, dbAddress.signature, this.identity.provider)
352+
if (!isValid) {
353+
throw new Error(`Could not verify ${dbAddress}`)
354+
}
355+
349356
// Make sure the type from the manifest matches the type that was given as an option
350357
if (options.type && manifest.type !== options.type)
351358
throw new Error(`Database '${dbAddress}' is type '${manifest.type}' but was opened as '${options.type}'`)

src/db-manifest.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
const path = require('path')
22

3+
const encodeManifest = manifest => Buffer.from(JSON.stringify(manifest))
4+
35
// Creates a DB manifest file and saves it in IPFS
4-
const createDBManifest = async (ipfs, name, type, accessControllerAddress) => {
5-
const manifest = {
6-
name: name,
7-
type: type,
6+
const createDBManifest = (name, type, accessControllerAddress, identity) => {
7+
const { orbitKey } = identity
8+
const owner = orbitKey.getPublic('hex')
9+
return {
10+
name,
11+
type,
12+
owner,
813
accessController: path.join('/ipfs', accessControllerAddress),
914
}
10-
const dag = await ipfs.object.put(Buffer.from(JSON.stringify(manifest)))
15+
}
16+
17+
const uploadDBManifest = async (ipfs, manifest) => {
18+
console.log('uploading manifest:', manifest)
19+
const dag = await ipfs.object.put(encodeManifest(manifest))
1120
return dag.toJSON().multihash.toString()
1221
}
1322

14-
module.exports = createDBManifest
23+
const signDBManifest = async (manifest, identity, identityProvider) => {
24+
console.log('signing manifest:', manifest)
25+
return identityProvider.sign(identity, encodeManifest(manifest))
26+
}
27+
28+
const verifyDBManifest = async (manifest, signature, identityProvider) => {
29+
console.log('verifying manifest:', manifest)
30+
const { owner } = manifest
31+
return identityProvider.verify(signature, owner, encodeManifest(manifest))
32+
}
33+
34+
module.exports.createDBManifest = createDBManifest
35+
module.exports.uploadDBManifest = uploadDBManifest
36+
module.exports.signDBManifest = signDBManifest
37+
module.exports.verifyDBManifest = verifyDBManifest

0 commit comments

Comments
 (0)