Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ swarm.join(...)
swarm.on('connection', (connection) => store.replicate(connection))
```

#### `const staticCore = await store.staticify(core, opts = {})`

Create a static version of the passed Hypercore `core`. Static Hypercores are read only and have no signers so cannot be updated by anyone. This is useful for creating content addressed cores that are immutable. `opts` are the same opts as `store.get()` with only `discoveryKey` & `key` options being ignored as they are derived when creating the static core.

#### `const storeB = storeA.session()`

Create a new Corestore session. Closing a session will close all cores made from this session.
Expand Down
42 changes: 42 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,48 @@ test('open readOnly', async function (t) {
t.ok(storeReadOnly.storage.readOnly)
})

test('staticify', async (t) => {
const dir = await tmp(t)
const store = new Corestore(dir)
t.teardown(() => store.close())

const noDataCore = store.get({ name: 'no-data' })
await noDataCore.ready()
await t.exception(store.staticify(noDataCore), /must have data/, 'throw if core has no data')

const core = store.get({ name: 'yolo' })
await core.ready()
while (core.length < 10) await core.append('tick')

const s = await store.staticify(core)

await t.exception(s.append('shouldnt work'), 'cant append to static core')

t.unlike(s.key, core.key, 'different key')
t.is(s.length, core.length)
t.alike(await s.treeHash(), await core.treeHash(), 'tree hashes match')

const staticHash = await s.treeHash()
const staticKey = s.key

await core.append('diverge')

t.not(s.length, core.length, 'static is unchanged')
t.unlike(staticHash, await core.treeHash(), 'tree hashes dont match')
t.alike(s.core.header.manifest.signers, [], 'static core has no singers')

await core.close()
await s.close()
await store.close()

const store2 = new Corestore(dir)
t.teardown(() => store2.close())

const s2 = await store2.get(staticKey)
t.alike(staticHash, await s2.treeHash(), 'tree hashes still matches')
t.alike(s.core.header.manifest.signers, [], 'static core still has no singers')
})

function toArray(stream) {
return new Promise((resolve, reject) => {
const all = []
Expand Down
Loading