Skip to content
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

chore: add types for @hyperswarm/secret-stream #34

Merged
merged 2 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions lib/discovery/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import net from 'net'

import { TypedEmitter } from 'tiny-typed-emitter'
import SecretStream from '@hyperswarm/secret-stream'
import NoiseSecretStream from '@hyperswarm/secret-stream'
import Hyperswarm from 'hyperswarm'
import { MdnsDiscovery } from 'multicast-service-discovery'
import base32 from 'base32.js'

/**
* @typedef {Object} DiscoveryEvents
* @property {(connection: NoiseSecretStream, info: PeerInfo) => void} connection
* @property {(connection: NoiseSecretStream<RawConnectionStream>, info: PeerInfo) => void} connection
* @property {(topicStatus: { topic: TopicString, mdns: TopicServiceStatus, dht: TopicServiceStatus }) => void} topicStatus
* @property {(peerStatus: { status: String, peer: PeerInfo }) => void} peerStatus
* @property {(error: Error) => void} error
Expand Down Expand Up @@ -247,8 +247,7 @@ export class Discovery extends TypedEmitter {
throw new Error('Socket not connected')
}

/** @type {ConnectionStream} */
const connection = new SecretStream(false, socket)
const connection = new NoiseSecretStream(false, socket)

connection.on('error', (error) => {
console.error('Error on mdns client connection', error)
Expand Down Expand Up @@ -298,7 +297,7 @@ export class Discovery extends TypedEmitter {
* Handle connection that occur via the Hyperswarm DHT
*
* @private
* @param {ConnectionStream} connection
* @param {NoiseSecretStream<RawDHTConnectionStream>} connection
* @param {HyperswarmPeerInfo} info
* @returns {Promise<void>}
*/
Expand Down Expand Up @@ -594,7 +593,7 @@ export class Discovery extends TypedEmitter {
*
* @private
* @param {Address} options
* @returns {ConnectionStream}
* @returns {NoiseSecretStream<net.Socket>}
*/
_connect(options) {
const { host, port } = options
Expand All @@ -604,7 +603,7 @@ export class Discovery extends TypedEmitter {
allowHalfOpen: true,
})

const connection = new SecretStream(true, stream, {
const connection = new NoiseSecretStream(true, stream, {
keyPair: this.#identityKeyPair,
})

Expand Down Expand Up @@ -832,7 +831,7 @@ export class PeerInfo extends TypedEmitter {
* @typedef {Object} PeerInfoOptions
* @property {IdentityPublicKeyString} identityPublicKey
* @property {('dht'|'mdns')} discoveryType
* @property {ConnectionStream} connection
* @property {NoiseSecretStream<RawConnectionStream>} connection
* @property {HyperswarmPeerInfo} [dhtPeerInfo]
* @property {TopicString[]} [topics]
* @property {string} [host]
Expand Down Expand Up @@ -889,7 +888,7 @@ export class PeerInfo extends TypedEmitter {
* @param {Object} options
* @param {IdentityPublicKeyString} [options.identityPublicKey]
* @param {('dht'|'mdns')} [options.discoveryType]
* @param {ConnectionStream} [options.connection]
* @param {NoiseSecretStream<RawConnectionStream>} [options.connection]
* @param {HyperswarmPeerInfo} [options.dhtPeerInfo]
* @param {TopicString[]} [options.topics]
* @param {string} [options.host]
Expand Down
17 changes: 3 additions & 14 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,13 @@
* @typedef {Buffer} Block
*/

// TODO: Figure out where those extra fields come from and find more elegant way to represent this
/**
* @typedef {Object} NoiseSecretStream
* @property {Buffer} remotePublicKey
* @property {number} remotePort
* @property {string} remoteHost
* @property {import('net').Socket} rawStream
* @property {function} end
* @typedef {import('streamx').Duplex & {remoteAddress: string, remotePort:number}} RawDHTConnectionStream
Comment on lines +55 to +57
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seth may have an idea of where these come from, but my guess is that it would require even more work to get the proper typings for that to update this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the approach used here makes sense.

*/

/**
* @typedef {import('streamx').Stream<any, false, false, ConnectionStreamEvents> & NoiseSecretStream} ConnectionStream
*/

/**
* @typedef {Object} ConnectionStreamEvents
* @property {(connect: void) => void} connect
* @property {(close: void) => void} close
* @property {(error: Error) => void} error
* @typedef {import('net').Socket | RawDHTConnectionStream} RawConnectionStream
*/

/**
Expand Down
58 changes: 57 additions & 1 deletion types/modules.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
// TODO: create types for these modules
declare module 'hyperswarm'
declare module '@hyperswarm/secret-stream'
declare module '@hyperswarm/secret-stream' {
import { Duplex as NodeDuplex } from 'stream'
import { Duplex, DuplexEvents } from 'streamx'

interface Opts {
autostart?: boolean
// TODO: Use https://github.com/chm-diederichs/noise-handshake/blob/main/noise.js for specific patterns
pattern?: string
Comment on lines +9 to +10
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh i had no idea how to read those pattern variables in the link provided, so went with string for now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i think we're unlikely to set the pattern option too, so seems lower priority.

remotePublicKey?: Buffer
keyPair?: { publicKey: Buffer; secretKey: Buffer }
handshake?: {
tx: Buffer
rx: Buffer
hash: Buffer
publicKey: Buffer
remotePublicKey: Buffer
}
}

type NoiseStreamEvents = {
connect: () => void
}

class NoiseSecretStream<
RawStream extends NodeDuplex | Duplex = Duplex
> extends Duplex<
any,
any,
any,
any,
true,
true,
DuplexEvents<any, any> & NoiseStreamEvents
> {
Comment on lines +28 to +36
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a more concise way to write this??

readonly publicKey: Buffer
readonly remotePublicKey: Buffer
readonly handshakeHash: Buffer
readonly rawStream: RawStream
readonly isInitiator: boolean
readonly noiseStream: this
readonly opened: Promise<boolean>
readonly userData: any

constructor(isInitiator: boolean, rawStream?: RawStream, opts?: Opts)

static keyPair(seed?: Buffer): {
publicKey: Buffer
secretKey: Buffer
}

start(rawStream?: NodeDuplex, opts?: Opts): void
setTimeout(ms?: number): void
setKeepAlive(ms?: number): void
}

export = NoiseSecretStream
}
declare module '@hyperswarm/testnet'
declare module 'base32.js'
declare module '@mapeo/crypto'
Expand Down