forked from Frando/hypercore-peer-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
41 lines (37 loc) · 1.14 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const tape = require('tape')
const Protocol = require('@ddatabase/protocol')
const crypto = require('@ddatabase/crypto')
const { pipeline } = require('stream')
const authenticatedProtocol = require('.')
tape('basics', t => {
t.plan(2)
const auth1 = crypto.keyPair()
const auth2 = crypto.keyPair()
const stream1 = init(true, auth1, 'one')
const stream2 = init(false, auth2, 'two')
pipeline(stream1, stream2, stream1, (err) => {
console.log('done', err)
t.end()
})
function init (isInitiator, authKeyPair, name) {
// console.log(name, 'init', authKeyPair.publicKey.toString('hex'))
const protocol = new Protocol(isInitiator)
authenticatedProtocol(protocol, {
authKeyPair,
onauthenticate (publicKey, cb) {
if (name === 'one' && publicKey.equals(auth2.publicKey)) {
return cb(null, true)
}
if (name === 'two' && publicKey.equals(auth1.publicKey)) {
return cb(null, true)
}
cb(null, false)
},
onprotocol (protocol) {
// This is where you'd initialiize the actual connection.
t.ok(protocol)
}
})
return protocol
}
})