Skip to content

Commit f5ba9a8

Browse files
Add test for core.setKeyPair() which shows signer error (#726)
Test shows that the key changes and that attempting to append will cause an error. This is to cover the fact that using `core.setKeyPair()` can be a footgun.
1 parent 31afceb commit f5ba9a8

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

test/basic.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const test = require('brittle')
22
const b4a = require('b4a')
33
const HypercoreStorage = require('hypercore-storage')
4+
const crypto = require('hypercore-crypto')
45

56
const Hypercore = require('../')
67
const { create, createStorage, eventFlush } = require('./helpers')
@@ -762,6 +763,20 @@ test('append alignment to bitfield boundary', async function (t) {
762763
}
763764
})
764765

766+
test('setKeyPair', async function (t) {
767+
const core = await create(t)
768+
769+
await core.append('hello')
770+
t.is(core.length, 1)
771+
772+
const keyPair = crypto.keyPair()
773+
t.unlike(core.keyPair, keyPair, 'generate new keyPair')
774+
core.setKeyPair(keyPair)
775+
t.alike(core.keyPair, keyPair, 'keyPair updated')
776+
777+
await t.exception(core.append('world'), /Public key is not a declared signer/)
778+
})
779+
765780
function getBitfields (hypercore, start = 0, end = null) {
766781
if (!end) end = hypercore.length
767782

0 commit comments

Comments
 (0)