Skip to content

Instantly share code, notes, and snippets.

@IwraStudios
Last active October 25, 2019 12:55
Show Gist options
  • Save IwraStudios/eecd07e060cf49145a974ec160c775ec to your computer and use it in GitHub Desktop.
Save IwraStudios/eecd07e060cf49145a974ec160c775ec to your computer and use it in GitHub Desktop.
declare module "libp2p-crypto" {
export function randomBytes(bytes: number): Buffer
export class RsaPublicKey {
public constructor(key: object)
public verify(data: Buffer, sig: Buffer, callback: any): void
public marshal(): any
public bytes(): Buffer
public encrypt(bytes: Buffer): Buffer
public equals(key: RsaPublicKey): boolean
public hash(callback: any): void
}
export class RsaPrivateKey {
// key - Object of the jwk format
// publicKey - Buffer of the spki format
public constructor(key: object, publicKey: Buffer)
public genSecret(): Buffer
public sign(message: string | Buffer, callback: any): void
public public(): RsaPublicKey
public decrypt(msg: string | Buffer, callback: any): void
public marshal(): any
public bytes(): Buffer
public equals(key: any): boolean
public hash(callback: any): void
/**
* Gets the ID of the key.
*
* The key id is the base58 encoding of the SHA-256 multihash of its public key.
* The public key is a protobuf encoding containing a type and the DER encoding
* of the PKCS SubjectPublicKeyInfo.
*
* @param {function(Error, id)} callback
* @returns {undefined}
*/
public id(callback: (err: Error, id: any) => void): void
/**
* Exports the key into a password protected PEM format
*
* @param {string} [format] - Defaults to 'pkcs-8'.
* @param {string} password - The password to read the encrypted PEM
* @param {function(Error, KeyInfo)} callback
* @returns {undefined}
*/
public export(format: string, password: string, callback: (err: Error, key: any) => void): void
}
export let keys: any
}
declare module "libp2p" {
import { EventEmitter } from "events"
import PeerId from "peer-id";
import PeerInfo from "peer-info";
import { MultiAddr } from "multiaddr";
interface ContentRouting {
provide(key: Buffer, callback: () => any): void
findProviders(key: Buffer, opts: {maxTimeout: number, maxNumProviders: number}, callback: () => any): void
}
interface Connection {
getPeerInfo(cb: (err: Error, peer: PeerInfo) => void): void
setPeerInfo(peer: PeerInfo): void
getObservedAddrs(cb: (err: Error, multiaddr: string[]) => void): void
}
interface PeerBook {
has(peerInfo: PeerInfo | PeerId | string): boolean
put(peerInfo: PeerInfo, replace: boolean): void
getAllArray(): PeerInfo[]
getAll(): {[bs58: string]: PeerInfo}
get(peer: PeerInfo | PeerId | string): PeerInfo
remove(peer: PeerInfo | PeerId | string): void
getMultiaddrs(peer: PeerInfo | PeerId | string): string[]
}
type msg = {from: string, seqno: Buffer, data: Buffer, topicIDs: string[] }
// https://github.com/ipfs/interface-js-ipfs-core/blob/master/SPEC/PUBSUB.md
interface PubSub {
subscribe(topic: string, options: {discover?: boolean}, handler: (message: msg) => void, callback: (err?: Error) => void): Promise<void> | void
unsubscribe(topic: string, handler: (message: msg) => void, callback: (err?: Error) => void): Promise<void> | void
publish(topic: string, data: Buffer, callback: (err?: Error) => void): Promise<void> | void
ls(callback: (err: Error | null, ls: string[]) => void): Promise<void> | void
peers(topic: string | ((err: Error | null, ls: string[]) => void), callback?: (err: Error | null, ls: string[]) => void): Promise<void> | void
}
/**
* @fires Node#error Emitted when an error occurs
* @fires Node#peer:connect Emitted when a peer is connected to this node
* @fires Node#peer:disconnect Emitted when a peer disconnects from this node
* @fires Node#peer:discovery Emitted when a peer is discovered
* @fires Node#start Emitted when the node and its services has started
* @fires Node#stop Emitted when the node and its services has stopped
*/
export default class Node extends EventEmitter {
/**
*
* @param _options
*/
public constructor (_options: {peerInfo: PeerInfo, [key: string]: any})
public peerInfo: PeerInfo
public isStarted(): boolean
public pubsub: PubSub
public peerBook: PeerBook
public contentRouting: ContentRouting
/**
* Overrides EventEmitter.emit to conditionally emit errors
* if there is a handler. If not, errors will be logged.
* @param {string} eventName
* @param {...any} args
* @returns {void}
* @param eventName
* @param ...args
* @return
*/
public emit(eventName: string, ...args: any): any
/**
* Starts the libp2p node and all sub services
*
* @param {function(Error)} callback
* @returns {void}
* @param callback
* @return
*/
public start(callback: (err: Error) => void): any
/**
* Stop the libp2p node by closing its listeners and open connections
*
* @param {function(Error)} callback
* @returns {void}
* @param callback
* @return
*/
public stop(callback: (err: Error) => void): any
/**
* Dials to the provided peer. If successful, the `PeerInfo` of the
* peer will be added to the nodes `PeerBook`
*
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
* @param {function(Error)} callback
* @returns {void}
* @param peer
* @param callback
* @return
*/
public dial(peer: MultiAddr | PeerId | PeerInfo | string, callback: (err: Error) => void): void
/**
* Dials to the provided peer and handshakes with the given protocol.
* If successful, the `PeerInfo` of the peer will be added to the nodes `PeerBook`,
* and the `Connection` will be sent in the callback
*
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
* @param {string} protocol
* @param {function(Error, Connection)} callback
* @returns {void}
* @param peer
* @param protocol
* @param callback
*/
public dialProtocol(peer: MultiAddr | PeerId | PeerInfo | string, protocol: string, callback: (err: Error, conn: Connection) => void): void
/**
* Similar to `dial` and `dialProtocol`, but the callback will contain a
* Connection State Machine.
*
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to dial
* @param {string} protocol
* @param {function(Error, ConnectionFSM)} callback
* @returns {void}
* @param peer
* @param protocol
* @param callback
* @return
*/
public dialFSM(peer: MultiAddr | PeerId | PeerInfo | string, protocol: string, callback: (err: Error, conn: any) => void): void
/**
*
* @param peer
* @param callback
*/
public hangUp(peer: MultiAddr | PeerId | PeerInfo | string, callback: (err: Error) => void): void
/**
*
* @param peer
* @param callback
*/
public ping(peer: MultiAddr | PeerId | PeerInfo | string, callback: (err: Error) => void): void
/**
*
* @param protocol
* @param handlerFunc
* @param matchFunc
*/
public handle(protocol: string, handlerFunc: (protocol: string, conn: Connection) => void, matchFunc?: any): void
/**
*
* @param protocol
*/
public unhandle(protocol: string): void
public _onStarting(): void
public _onStopping(): void
}
}
declare module "multiaddr" {
export interface Proto {
code: number
name: string
size: number
}
export interface NodeAddr {
family: string
port: number
address: string
}
export class MultiAddr {
public constructor(addr: string | Buffer | MultiAddr)
public buffer: Buffer
public toString(): string
public protos(): Proto[]
public nodeAddress(): NodeAddr
public encapsulate(addr: MultiAddr | string): MultiAddr
public decapsulate(addr: MultiAddr | string): MultiAddr
public protoCodes(): number[]
public protoNames(): string[]
public tuples(): [number, Buffer][]
public stringTuples(): [number, string][]
public getPeerId(): string | undefined
}
export default function (addr: string | Buffer | MultiAddr): MultiAddr
}
declare module 'peer-id' {
import { RsaPrivateKey, RsaPublicKey } from "libp2p-crypto";
export interface PeerIdData {
id: string,
privKey: string,
pubKey: string
}
export default class PeerId {
public constructor(id: Buffer, privKey?: RsaPrivateKey, pubKey?: RsaPublicKey)
public toHexString(): string
public toBytes(): Buffer
public toB58String(): string
public toJSON(): PeerIdData
public toPrint(): string
public isEqual(id: PeerId | Buffer): boolean
public static create(opts: {bits: number}, callback: (err: Error, id: PeerId) => void): void
public static createFromHexString(str: string): PeerId
public static createFromBytes(buf: Buffer): PeerId
public static createFromB58String(str: string): PeerId
public static createFromPubKey(pubKey: Buffer): PeerId
public static createFromPrivKey(privKey: Buffer): PeerId
public static createFromJSON(obj: PeerIdData): PeerId
}
}
declare module 'peer-info' {
import { MultiAddr } from "multiaddr";
import PeerId, { PeerIdData } from "peer-id";
export function create(id: PeerIdData | PeerId, callback: (err: Error, peerInfo: PeerInfo) => void): void
type MultiAddrIsh = MultiAddr | string
export class MutliAddrSet {
public add(addr: MultiAddr | string): void
public addSafe(addr: MultiAddr): boolean | undefined
public toArray(): MultiAddr[]
public forEach(cb: (addr: MultiAddr) => void): void
public filterBy(regx: RegExp): MultiAddr[]
public has(addr: MultiAddr): boolean
public delete(addr: MultiAddr): boolean | undefined
public replace(existing: MultiAddrIsh | MultiAddrIsh[], fresh: MultiAddrIsh | MultiAddrIsh[]): void
public clear(): void
public distinct(): MultiAddr[]
}
export default class PeerInfo {
public constructor(id?: PeerId)
public multiaddrs: MutliAddrSet
public protocols: Set<string>
public id: PeerId
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment