mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-06-14 16:41:33 +00:00
feat: crypto interface (#2)
* docs: initial crypto readme * feat: add basic crypto interface test suite * feat: add optional remotepeer for inbound feat: add errors export * docs(fix): update src/crypto/README.md Co-Authored-By: Vasco Santos <vasco.santos@moxy.studio>
This commit is contained in:
13
test/crypto/compliance.spec.js
Normal file
13
test/crypto/compliance.spec.js
Normal file
@ -0,0 +1,13 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const tests = require('../../src/crypto/tests')
|
||||
const mockCrypto = require('./mock-crypto')
|
||||
|
||||
describe('compliance tests', () => {
|
||||
tests({
|
||||
setup () {
|
||||
return mockCrypto
|
||||
}
|
||||
})
|
||||
})
|
75
test/crypto/mock-crypto.js
Normal file
75
test/crypto/mock-crypto.js
Normal file
@ -0,0 +1,75 @@
|
||||
'use strict'
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const handshake = require('it-handshake')
|
||||
const duplexPair = require('it-pair/duplex')
|
||||
const pipe = require('it-pipe')
|
||||
const { UnexpectedPeerError } = require('../../src/crypto/errors')
|
||||
|
||||
// A basic transform that does nothing to the data
|
||||
const transform = () => {
|
||||
return (source) => (async function * () {
|
||||
for await (const chunk of source) {
|
||||
yield chunk
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
protocol: 'insecure',
|
||||
secureInbound: async (localPeer, duplex, expectedPeer) => {
|
||||
// 1. Perform a basic handshake.
|
||||
const shake = handshake(duplex)
|
||||
shake.write(localPeer.id)
|
||||
const remoteId = await shake.read()
|
||||
const remotePeer = new PeerId(remoteId.slice())
|
||||
shake.rest()
|
||||
|
||||
if (expectedPeer && expectedPeer.id !== remotePeer.id) {
|
||||
throw new UnexpectedPeerError()
|
||||
}
|
||||
|
||||
// 2. Create your encryption box/unbox wrapper
|
||||
const wrapper = duplexPair()
|
||||
const encrypt = transform() // Use transform iterables to modify data
|
||||
const decrypt = transform()
|
||||
|
||||
pipe(
|
||||
wrapper[0], // We write to wrapper
|
||||
encrypt, // The data is encrypted
|
||||
shake.stream, // It goes to the remote peer
|
||||
decrypt, // Decrypt the incoming data
|
||||
wrapper[0] // Pipe to the wrapper
|
||||
)
|
||||
|
||||
return {
|
||||
conn: wrapper[1],
|
||||
remotePeer
|
||||
}
|
||||
},
|
||||
secureOutbound: async (localPeer, duplex, remotePeer) => {
|
||||
// 1. Perform a basic handshake.
|
||||
const shake = handshake(duplex)
|
||||
shake.write(localPeer.id)
|
||||
const remoteId = await shake.read()
|
||||
shake.rest()
|
||||
|
||||
// 2. Create your encryption box/unbox wrapper
|
||||
const wrapper = duplexPair()
|
||||
const encrypt = transform()
|
||||
const decrypt = transform()
|
||||
|
||||
pipe(
|
||||
wrapper[0], // We write to wrapper
|
||||
encrypt, // The data is encrypted
|
||||
shake.stream, // It goes to the remote peer
|
||||
decrypt, // Decrypt the incoming data
|
||||
wrapper[0] // Pipe to the wrapper
|
||||
)
|
||||
|
||||
return {
|
||||
conn: wrapper[1],
|
||||
remotePeer: new PeerId(remoteId.slice())
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user