Skip to content

Commit 4793ab8

Browse files
martinheideggerandrewosh
authored andcommitted
Added option for onauthenticate
1 parent a6df51e commit 4793ab8

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Creates a new SwarmNetworker that will open replication streams on the `corestor
4848
{
4949
id: crypto.randomBytes(32), // A randomly-generated peer ID,
5050
keyPair: HypercoreProtocol.keyPair(), // A NOISE keypair that's used across all connections.
51+
onauthenticate: (remotePublicKey, cb) => { cb() }, // A NOISE keypair authentication hook
5152
}
5253
```
5354

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class CorestoreNetworker extends Nanoresource {
1717
this._replicationOpts = {
1818
encrypt: true,
1919
live: true,
20-
keyPair: this.keyPair
20+
keyPair: this.keyPair,
21+
onauthenticate: opts.onauthenticate
2122
}
2223

2324
this.streams = new Set()

test/all.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,25 @@ test('bidirectional extension send/receive', async t => {
489489
t.end()
490490
})
491491

492+
test.only('onauthentication hook', async t => {
493+
t.plan(2)
494+
const { networker: networker1 } = await create({
495+
onauthenticate (peerPublicKey, cb) {
496+
t.deepEquals(peerPublicKey, networker2.keyPair.publicKey)
497+
cb()
498+
}
499+
})
500+
const { networker: networker2 } = await create()
501+
502+
const dkey = hypercoreCrypto.randomBytes(32)
503+
await networker1.configure(dkey)
504+
await networker2.configure(dkey)
505+
506+
await new Promise(resolve => setTimeout(resolve, 100))
507+
await cleanup([networker1, networker2])
508+
t.end()
509+
})
510+
492511
async function create (opts = {}) {
493512
if (!bootstrap) {
494513
bootstrap = dht({

0 commit comments

Comments
 (0)