-
Notifications
You must be signed in to change notification settings - Fork 447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: crypto and pnet #469
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5d28ecf
feat: add initial plaintext 2 module
jacobheun 30eb8e0
refactor: initial refactor of pnet
jacobheun 202c76c
chore: fix lint
jacobheun ee35259
fix: update plaintext api usage
jacobheun e99b52c
test: use plaintext for test crypto
jacobheun d313299
chore: update deps
jacobheun b8bbf51
feat: add connection protection to the upgrader
jacobheun 753390b
refactor: cleanup and lint fix
jacobheun f69b7e8
chore: remove unncessary transforms
jacobheun 49bb3b6
chore: temporarily disable bundlesize
jacobheun 581b39a
chore: add missing dep
jacobheun dc9d91c
fix: use it-handshake to prevent overreading
jacobheun b803efa
chore(fix): PR feedback updates
jacobheun a4b8017
chore: apply suggestions from code review
jacobheun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict' | ||
|
||
const handshake = require('it-handshake') | ||
const lp = require('it-length-prefixed') | ||
const PeerId = require('peer-id') | ||
const debug = require('debug') | ||
const log = debug('libp2p:plaintext') | ||
log.error = debug('libp2p:plaintext:error') | ||
const { UnexpectedPeerError, InvalidCryptoExchangeError } = require('libp2p-interfaces/src/crypto/errors') | ||
|
||
const { Exchange, KeyType } = require('./proto') | ||
const protocol = '/plaintext/2.0.0' | ||
|
||
function lpEncodeExchange (exchange) { | ||
const pb = Exchange.encode(exchange) | ||
return lp.encode.single(pb) | ||
} | ||
|
||
async function encrypt (localId, conn, remoteId) { | ||
const shake = handshake(conn) | ||
|
||
// Encode the public key and write it to the remote peer | ||
shake.write(lpEncodeExchange({ | ||
id: localId.toBytes(), | ||
pubkey: { | ||
Type: KeyType.RSA, // TODO: dont hard code | ||
Data: localId.marshalPubKey() | ||
} | ||
})) | ||
|
||
log('write pubkey exchange to peer %j', remoteId) | ||
|
||
// Get the Exchange message | ||
const response = (await lp.decodeFromReader(shake.reader).next()).value | ||
const id = Exchange.decode(response.slice()) | ||
log('read pubkey exchange from peer %j', remoteId) | ||
|
||
let peerId | ||
try { | ||
peerId = await PeerId.createFromPubKey(id.pubkey.Data) | ||
} catch (err) { | ||
log.error(err) | ||
throw new InvalidCryptoExchangeError('Remote did not provide its public key') | ||
} | ||
|
||
if (remoteId && !peerId.isEqual(remoteId)) { | ||
throw new UnexpectedPeerError() | ||
} | ||
|
||
log('plaintext key exchange completed successfully with peer %j', peerId) | ||
|
||
shake.rest() | ||
return { | ||
conn: shake.stream, | ||
remotePeer: peerId | ||
} | ||
} | ||
|
||
module.exports = { | ||
protocol, | ||
secureInbound: (localId, conn, remoteId) => { | ||
return encrypt(localId, conn, remoteId) | ||
}, | ||
secureOutbound: (localId, conn, remoteId) => { | ||
return encrypt(localId, conn, remoteId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict' | ||
|
||
const protobuf = require('protons') | ||
|
||
module.exports = protobuf(` | ||
message Exchange { | ||
optional bytes id = 1; | ||
optional PublicKey pubkey = 2; | ||
} | ||
|
||
enum KeyType { | ||
RSA = 0; | ||
Ed25519 = 1; | ||
Secp256k1 = 2; | ||
ECDSA = 3; | ||
} | ||
|
||
message PublicKey { | ||
required KeyType Type = 1; | ||
required bytes Data = 2; | ||
} | ||
`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the pubsub PR, I created an util with the base config for pubsub.
What do you think of creating in the root of tests, a util file with the base libp2p config (which would be used here), and in the tests for each component, we would have another utils file, which would get the base libp2p config from the root util and add the specific things for its needs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, in specific tests, we can also use
merge-options
to overwrite what we want to.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created this on libp2p/js-libp2p#470