@libp2p/interface
Advanced tools
Comparing version 1.7.0 to 2.0.0-1210884ed
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.Libp2PInterface = factory()}(typeof self !== 'undefined' ? self : this, function () { | ||
"use strict";var Libp2PInterface=(()=>{var n=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var _=(e,t)=>{for(var o in t)n(e,o,{get:t[o],enumerable:!0})},g=(e,t,o,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of I(t))!L.call(e,r)&&r!==o&&n(e,r,{get:()=>t[r],enumerable:!(s=A(t,r))||s.enumerable});return e};var v=e=>g(n({},"__esModule",{value:!0}),e);var z={};_(z,{AbortError:()=>c,AggregateCodeError:()=>l,CodeError:()=>a,CustomEvent:()=>h,ERR_INVALID_MESSAGE:()=>X,ERR_INVALID_PARAMETERS:()=>G,ERR_NOT_FOUND:()=>j,ERR_TIMEOUT:()=>F,Ed25519:()=>w,FaultTolerance:()=>p,InvalidCryptoExchangeError:()=>x,InvalidCryptoTransmissionError:()=>m,KEEP_ALIVE:()=>U,RSA:()=>P,StrictNoSign:()=>k,StrictSign:()=>V,TopicValidatorResult:()=>i,TypedEventEmitter:()=>E,UnexpectedPeerError:()=>f,connectionSymbol:()=>S,contentRoutingSymbol:()=>T,isConnection:()=>N,isPeerId:()=>O,isStartable:()=>d,isTransport:()=>B,peerDiscoverySymbol:()=>M,peerIdSymbol:()=>R,peerRoutingSymbol:()=>C,secp256k1:()=>D,serviceCapabilities:()=>K,serviceDependencies:()=>q,setMaxListeners:()=>u,start:()=>Y,stop:()=>H,transportSymbol:()=>b});var S=Symbol.for("@libp2p/connection");function N(e){return e!=null&&!!e[S]}var T=Symbol.for("@libp2p/content-routing");var w="Ed25519",P="RSA",D="secp256k1";var M=Symbol.for("@libp2p/peer-discovery");var R=Symbol.for("@libp2p/peer-id");function O(e){return e!=null&&!!e[R]}var C=Symbol.for("@libp2p/peer-routing");var U="keep-alive";var V="StrictSign",k="StrictNoSign",i;(function(e){e.Accept="accept",e.Ignore="ignore",e.Reject="reject"})(i||(i={}));var b=Symbol.for("@libp2p/transport");function B(e){return e!=null&&!!e[b]}var p;(function(e){e[e.FATAL_ALL=0]="FATAL_ALL",e[e.NO_FATAL=1]="NO_FATAL"})(p||(p={}));var c=class e extends Error{code;type;constructor(t="The operation was aborted"){super(t),this.name="AbortError",this.code=e.code,this.type=e.type}static code="ABORT_ERR";static type="aborted"},a=class extends Error{code;props;constructor(t,o,s){super(t),this.code=o,this.name=s?.name??"CodeError",this.props=s??{}}},l=class extends AggregateError{code;props;constructor(t,o,s,r){super(t,o),this.code=s,this.name=r?.name??"AggregateCodeError",this.props=r??{}}},f=class e extends Error{code;constructor(t="Unexpected Peer"){super(t),this.name="UnexpectedPeerError",this.code=e.code}static code="ERR_UNEXPECTED_PEER"},x=class e extends Error{code;constructor(t="Invalid crypto exchange"){super(t),this.name="InvalidCryptoExchangeError",this.code=e.code}static code="ERR_INVALID_CRYPTO_EXCHANGE"},m=class e extends Error{code;constructor(t="Invalid crypto transmission"){super(t),this.name="InvalidCryptoTransmissionError",this.code=e.code}static code="ERR_INVALID_CRYPTO_TRANSMISSION"},F="ERR_TIMEOUT",G="ERR_INVALID_PARAMETERS",j="ERR_NOT_FOUND",X="ERR_INVALID_MESSAGE";var u=(e,...t)=>{try{[...t]}catch{}};var E=class extends EventTarget{#t=new Map;constructor(){super(),u(1/0,this)}listenerCount(t){let o=this.#t.get(t);return o==null?0:o.length}addEventListener(t,o,s){super.addEventListener(t,o,s);let r=this.#t.get(t);r==null&&(r=[],this.#t.set(t,r)),r.push({callback:o,once:(s!==!0&&s!==!1&&s?.once)??!1})}removeEventListener(t,o,s){super.removeEventListener(t.toString(),o??null,s);let r=this.#t.get(t);r!=null&&(r=r.filter(({callback:y})=>y!==o),this.#t.set(t,r))}dispatchEvent(t){let o=super.dispatchEvent(t),s=this.#t.get(t.type);return s==null||(s=s.filter(({once:r})=>!r),this.#t.set(t.type,s)),o}safeDispatchEvent(t,o={}){return this.dispatchEvent(new h(t,o))}},h=globalThis.CustomEvent;function d(e){return e!=null&&typeof e.start=="function"&&typeof e.stop=="function"}async function Y(...e){let t=[];for(let o of e)d(o)&&t.push(o);await Promise.all(t.map(async o=>{o.beforeStart!=null&&await o.beforeStart()})),await Promise.all(t.map(async o=>{await o.start()})),await Promise.all(t.map(async o=>{o.afterStart!=null&&await o.afterStart()}))}async function H(...e){let t=[];for(let o of e)d(o)&&t.push(o);await Promise.all(t.map(async o=>{o.beforeStop!=null&&await o.beforeStop()})),await Promise.all(t.map(async o=>{await o.stop()})),await Promise.all(t.map(async o=>{o.afterStop!=null&&await o.afterStop()}))}var K=Symbol.for("@libp2p/service-capabilities"),q=Symbol.for("@libp2p/service-dependencies");return v(z);})(); | ||
"use strict";var Libp2PInterface=(()=>{var n=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var V=(t,r)=>{for(var e in r)n(t,e,{get:r[e],enumerable:!0})},W=(t,r,e,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of J(r))!Q.call(t,o)&&o!==e&&n(t,o,{get:()=>r[o],enumerable:!(s=H(r,o))||s.enumerable});return t};var X=t=>W(n({},"__esModule",{value:!0}),t);var lr={};V(lr,{AbortError:()=>c,AlreadyStartedError:()=>D,ConnectionClosedError:()=>E,ConnectionClosingError:()=>f,ConnectionFailedError:()=>h,DialError:()=>N,FaultTolerance:()=>i,InvalidCIDError:()=>L,InvalidCryptoExchangeError:()=>l,InvalidMessageError:()=>A,InvalidMultiaddrError:()=>g,InvalidMultihashError:()=>P,InvalidParametersError:()=>m,InvalidPeerIdError:()=>I,InvalidPrivateKeyError:()=>x,InvalidPublicKeyError:()=>u,KEEP_ALIVE:()=>er,LimitedConnectionError:()=>k,ListenError:()=>U,MuxerClosedError:()=>b,NotFoundError:()=>v,NotStartedError:()=>M,ProtocolError:()=>C,StreamResetError:()=>y,StreamStateError:()=>S,StrictNoSign:()=>sr,StrictSign:()=>or,TimeoutError:()=>T,TooManyInboundProtocolStreamsError:()=>_,TooManyOutboundProtocolStreamsError:()=>F,TopicValidatorResult:()=>a,TypedEventEmitter:()=>B,UnexpectedPeerError:()=>p,UnsupportedKeyTypeError:()=>K,UnsupportedOperationError:()=>d,UnsupportedProtocolError:()=>w,connectionSymbol:()=>j,contentRoutingSymbol:()=>Z,isConnection:()=>Y,isPeerId:()=>rr,isStartable:()=>R,isTransport:()=>nr,peerDiscoverySymbol:()=>$,peerIdSymbol:()=>q,peerRoutingSymbol:()=>tr,serviceCapabilities:()=>cr,serviceDependencies:()=>pr,setMaxListeners:()=>O,start:()=>ar,stop:()=>ir,transportSymbol:()=>z});var j=Symbol.for("@libp2p/connection");function Y(t){return t!=null&&!!t[j]}var Z=Symbol.for("@libp2p/content-routing");var $=Symbol.for("@libp2p/peer-discovery");var q=Symbol.for("@libp2p/peer-id");function rr(t){return!!t?.[q]}var tr=Symbol.for("@libp2p/peer-routing");var er="keep-alive";var or="StrictSign",sr="StrictNoSign",a;(function(t){t.Accept="accept",t.Ignore="ignore",t.Reject="reject"})(a||(a={}));var z=Symbol.for("@libp2p/transport");function nr(t){return t!=null&&!!t[z]}var i;(function(t){t[t.FATAL_ALL=0]="FATAL_ALL",t[t.NO_FATAL=1]="NO_FATAL"})(i||(i={}));var c=class extends Error{static name="AbortError";constructor(r="The operation was aborted"){super(r),this.name="AbortError"}},p=class extends Error{static name="UnexpectedPeerError";constructor(r="Unexpected Peer"){super(r),this.name="UnexpectedPeerError"}},l=class extends Error{static name="InvalidCryptoExchangeError";constructor(r="Invalid crypto exchange"){super(r),this.name="InvalidCryptoExchangeError"}},m=class extends Error{static name="InvalidParametersError";constructor(r="Invalid parameters"){super(r),this.name="InvalidParametersError"}},u=class extends Error{static name="InvalidPublicKeyError";constructor(r="Invalid public key"){super(r),this.name="InvalidPublicKeyError"}},x=class extends Error{static name="InvalidPrivateKeyError";constructor(r="Invalid private key"){super(r),this.name="InvalidPrivateKeyError"}},d=class extends Error{static name="UnsupportedOperationError";constructor(r="Unsupported operation"){super(r),this.name="UnsupportedOperationError"}},f=class extends Error{static name="ConnectionClosingError";constructor(r="The connection is closing"){super(r),this.name="ConnectionClosingError"}},E=class extends Error{static name="ConnectionClosedError";constructor(r="The connection is closed"){super(r),this.name="ConnectionClosedError"}},h=class extends Error{static name="ConnectionFailedError";constructor(r="Connection failed"){super(r),this.name="ConnectionFailedError"}},b=class extends Error{static name="MuxerClosedError";constructor(r="The muxer is closed"){super(r),this.name="MuxerClosedError"}},y=class extends Error{static name="StreamResetError";constructor(r="The stream has been reset"){super(r),this.name="StreamResetError"}},S=class extends Error{static name="StreamStateError";constructor(r="The stream is in an invalid state"){super(r),this.name="StreamStateError"}},v=class extends Error{static name="NotFoundError";constructor(r="Not found"){super(r),this.name="NotFoundError"}},I=class extends Error{static name="InvalidPeerIdError";constructor(r="Invalid PeerID"){super(r),this.name="InvalidPeerIdError"}},g=class extends Error{static name="InvalidMultiaddrError";constructor(r="Invalid multiaddr"){super(r),this.name="InvalidMultiaddrError"}},L=class extends Error{static name="InvalidCIDError";constructor(r="Invalid CID"){super(r),this.name="InvalidCIDError"}},P=class extends Error{static name="InvalidMultihashError";constructor(r="Invalid Multihash"){super(r),this.name="InvalidMultihashError"}},w=class extends Error{static name="UnsupportedProtocolError";constructor(r="Unsupported protocol error"){super(r),this.name="UnsupportedProtocolError"}},A=class extends Error{static name="InvalidMessageError";constructor(r="Invalid message"){super(r),this.name="InvalidMessageError"}},C=class extends Error{static name="ProtocolError";constructor(r="Protocol error"){super(r),this.name="ProtocolError"}},T=class extends Error{static name="TimeoutError";constructor(r="Timed out"){super(r),this.name="TimeoutError"}},M=class extends Error{static name="NotStartedError";constructor(r="Not started"){super(r),this.name="NotStartedError"}},D=class extends Error{static name="AlreadyStartedError";constructor(r="Already started"){super(r),this.name="AlreadyStartedError"}},N=class extends Error{static name="DialError";constructor(r="Dial error"){super(r),this.name="DialError"}},U=class extends Error{static name="ListenError";constructor(r="Listen error"){super(r),this.name="ListenError"}},k=class extends Error{static name="LimitedConnectionError";constructor(r="Limited connection"){super(r),this.name="LimitedConnectionError"}},_=class extends Error{static name="TooManyInboundProtocolStreamsError";constructor(r="Too many inbound protocol streams"){super(r),this.name="TooManyInboundProtocolStreamsError"}},F=class extends Error{static name="TooManyOutboundProtocolStreamsError";constructor(r="Too many outbound protocol streams"){super(r),this.name="TooManyOutboundProtocolStreamsError"}},K=class extends Error{static name="UnsupportedKeyTypeError";constructor(r="Unsupported key type"){super(r),this.name="UnsupportedKeyTypeError"}};var O=(t,...r)=>{try{[...r]}catch{}};var B=class extends EventTarget{#r=new Map;constructor(){super(),O(1/0,this)}listenerCount(r){let e=this.#r.get(r);return e==null?0:e.length}addEventListener(r,e,s){super.addEventListener(r,e,s);let o=this.#r.get(r);o==null&&(o=[],this.#r.set(r,o)),o.push({callback:e,once:(s!==!0&&s!==!1&&s?.once)??!1})}removeEventListener(r,e,s){super.removeEventListener(r.toString(),e??null,s);let o=this.#r.get(r);o!=null&&(o=o.filter(({callback:G})=>G!==e),this.#r.set(r,o))}dispatchEvent(r){let e=super.dispatchEvent(r),s=this.#r.get(r.type);return s==null||(s=s.filter(({once:o})=>!o),this.#r.set(r.type,s)),e}safeDispatchEvent(r,e={}){return this.dispatchEvent(new CustomEvent(r,e))}};function R(t){return t!=null&&typeof t.start=="function"&&typeof t.stop=="function"}async function ar(...t){let r=[];for(let e of t)R(e)&&r.push(e);await Promise.all(r.map(async e=>{e.beforeStart!=null&&await e.beforeStart()})),await Promise.all(r.map(async e=>{await e.start()})),await Promise.all(r.map(async e=>{e.afterStart!=null&&await e.afterStart()}))}async function ir(...t){let r=[];for(let e of t)R(e)&&r.push(e);await Promise.all(r.map(async e=>{e.beforeStop!=null&&await e.beforeStop()})),await Promise.all(r.map(async e=>{await e.stop()})),await Promise.all(r.map(async e=>{e.afterStop!=null&&await e.afterStop()}))}var cr=Symbol.for("@libp2p/service-capabilities"),pr=Symbol.for("@libp2p/service-dependencies");return X(lr);})(); | ||
return Libp2PInterface})); |
import type { MultiaddrConnection } from '../connection/index.js'; | ||
import type { AbortOptions } from '../index.js'; | ||
import type { PeerId } from '../peer-id/index.js'; | ||
@@ -6,2 +7,10 @@ import type { Duplex } from 'it-stream-types'; | ||
/** | ||
* If the remote PeerId is known and passed as an option, the securing operation | ||
* will throw if the remote peer cannot prove it has the private key that | ||
* corresponds to the public key the remote PeerId is derived from. | ||
*/ | ||
export interface SecureConnectionOptions extends AbortOptions { | ||
remotePeer?: PeerId; | ||
} | ||
/** | ||
* A libp2p connection encrypter module must be compliant to this interface | ||
@@ -17,3 +26,3 @@ * to ensure all exchanged data between two peers is encrypted. | ||
*/ | ||
secureOutbound<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(localPeer: PeerId, connection: Stream, remotePeer?: PeerId): Promise<SecuredConnection<Stream, Extension>>; | ||
secureOutbound<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(connection: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream, Extension>>; | ||
/** | ||
@@ -24,3 +33,3 @@ * Decrypt incoming data. If the remote PeerId is known, | ||
*/ | ||
secureInbound<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(localPeer: PeerId, connection: Stream, remotePeer?: PeerId): Promise<SecuredConnection<Stream, Extension>>; | ||
secureInbound<Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection>(connection: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream, Extension>>; | ||
} | ||
@@ -27,0 +36,0 @@ export interface SecuredConnection<Stream = any, Extension = unknown> { |
@@ -160,8 +160,12 @@ import type { AbortOptions, Logger } from '../index.js'; | ||
/** | ||
* Opt-in to running over a transient connection - one that has time/data limits | ||
* placed on it. | ||
* Opt-in to running over a limited connection - one that has restrictions | ||
* on the amount of data that may be transferred or how long it may be open for. | ||
* | ||
* These limits are typically enforced by a relay server, if the protocol | ||
* will be transferring a lot of data or the stream will be open for a long time | ||
* consider upgrading to a direct connection before opening the stream. | ||
* | ||
* @default false | ||
*/ | ||
runOnTransientConnection?: boolean; | ||
runOnLimitedConnection?: boolean; | ||
/** | ||
@@ -196,2 +200,23 @@ * By default when negotiating a protocol the dialer writes then protocol name | ||
/** | ||
* Connection limits are present on connections that are only allowed to | ||
* transfer a certain amount of bytes or be open for a certain number | ||
* of seconds. | ||
* | ||
* These limits are applied by Circuit Relay v2 servers, for example and | ||
* the connection will normally be closed abruptly if the limits are | ||
* exceeded. | ||
*/ | ||
export interface ConnectionLimits { | ||
/** | ||
* If present this is the number of bytes remaining that may be | ||
* transferred over this connection | ||
*/ | ||
bytes?: bigint; | ||
/** | ||
* If present this is the number of seconds that this connection will | ||
* remain open for | ||
*/ | ||
seconds?: number; | ||
} | ||
/** | ||
* A Connection is a high-level representation of a connection | ||
@@ -244,8 +269,7 @@ * to a remote peer that may have been secured by encryption and | ||
/** | ||
* A transient connection is one that is not expected to be open for very long | ||
* or one that cannot transfer very much data, such as one being used as a | ||
* circuit relay connection. Protocols need to explicitly opt-in to being run | ||
* over transient connections. | ||
* If present, this connection has limits applied to it, perhaps by an | ||
* intermediate relay. Once the limits have been reached the connection will | ||
* be closed by the relay. | ||
*/ | ||
transient: boolean; | ||
limits?: ConnectionLimits; | ||
/** | ||
@@ -252,0 +276,0 @@ * The time in milliseconds it takes to make a round trip to the remote peer. |
@@ -7,37 +7,212 @@ /** | ||
export declare class AbortError extends Error { | ||
readonly code: string; | ||
readonly type: string; | ||
static name: string; | ||
constructor(message?: string); | ||
static readonly code = "ABORT_ERR"; | ||
static readonly type = "aborted"; | ||
} | ||
export declare class CodeError<T extends Record<string, any> = Record<string, never>> extends Error { | ||
readonly code: string; | ||
readonly props: T; | ||
constructor(message: string, code: string, props?: T); | ||
} | ||
export declare class AggregateCodeError<T extends Record<string, any> = Record<string, never>> extends AggregateError { | ||
readonly code: string; | ||
readonly props: T; | ||
constructor(errors: Error[], message: string, code: string, props?: T); | ||
} | ||
/** | ||
* Thrown when a remote Peer ID does not match the expected one | ||
*/ | ||
export declare class UnexpectedPeerError extends Error { | ||
code: string; | ||
static name: string; | ||
constructor(message?: string); | ||
static readonly code = "ERR_UNEXPECTED_PEER"; | ||
} | ||
/** | ||
* Thrown when a crypto exchange fails | ||
*/ | ||
export declare class InvalidCryptoExchangeError extends Error { | ||
code: string; | ||
static name: string; | ||
constructor(message?: string); | ||
static readonly code = "ERR_INVALID_CRYPTO_EXCHANGE"; | ||
} | ||
export declare class InvalidCryptoTransmissionError extends Error { | ||
code: string; | ||
/** | ||
* Thrown when invalid parameters are passed to a function or method call | ||
*/ | ||
export declare class InvalidParametersError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
static readonly code = "ERR_INVALID_CRYPTO_TRANSMISSION"; | ||
} | ||
export declare const ERR_TIMEOUT = "ERR_TIMEOUT"; | ||
export declare const ERR_INVALID_PARAMETERS = "ERR_INVALID_PARAMETERS"; | ||
export declare const ERR_NOT_FOUND = "ERR_NOT_FOUND"; | ||
export declare const ERR_INVALID_MESSAGE = "ERR_INVALID_MESSAGE"; | ||
/** | ||
* Thrown when a public key is invalid | ||
*/ | ||
export declare class InvalidPublicKeyError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when a private key is invalid | ||
*/ | ||
export declare class InvalidPrivateKeyError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when a operation is unsupported | ||
*/ | ||
export declare class UnsupportedOperationError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when a connection is closing | ||
*/ | ||
export declare class ConnectionClosingError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when a connection is closed | ||
*/ | ||
export declare class ConnectionClosedError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when a connection fails | ||
*/ | ||
export declare class ConnectionFailedError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when the muxer is closed and an attempt to open a stream occurs | ||
*/ | ||
export declare class MuxerClosedError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when a protocol stream is reset by the remote muxer | ||
*/ | ||
export declare class StreamResetError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when a stream is in an invalid state | ||
*/ | ||
export declare class StreamStateError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when a value could not be found | ||
*/ | ||
export declare class NotFoundError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when an invalid peer ID is encountered | ||
*/ | ||
export declare class InvalidPeerIdError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when an invalid multiaddr is encountered | ||
*/ | ||
export declare class InvalidMultiaddrError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when an invalid CID is encountered | ||
*/ | ||
export declare class InvalidCIDError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when an invalid multihash is encountered | ||
*/ | ||
export declare class InvalidMultihashError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when a protocol is not supported | ||
*/ | ||
export declare class UnsupportedProtocolError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* An invalid or malformed message was encountered during a protocol exchange | ||
*/ | ||
export declare class InvalidMessageError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when a remote peer sends a structurally valid message that does not | ||
* comply with the protocol | ||
*/ | ||
export declare class ProtocolError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Throw when an operation times out | ||
*/ | ||
export declare class TimeoutError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when a startable component is interacted with but it has not been | ||
* started yet | ||
*/ | ||
export declare class NotStartedError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when a component is started that has already been started | ||
*/ | ||
export declare class AlreadyStartedError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when dialing an address failed | ||
*/ | ||
export declare class DialError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when listening on an address failed | ||
*/ | ||
export declare class ListenError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* This error is thrown when a limited connection is encountered, i.e. if the | ||
* user tried to open a stream on a connection for a protocol that is not | ||
* configured to run over limited connections. | ||
*/ | ||
export declare class LimitedConnectionError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* This error is thrown where there are too many inbound protocols streams open | ||
*/ | ||
export declare class TooManyInboundProtocolStreamsError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* This error is thrown where there are too many outbound protocols streams open | ||
*/ | ||
export declare class TooManyOutboundProtocolStreamsError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
/** | ||
* Thrown when and attempt to operate on an unsupported key was made | ||
*/ | ||
export declare class UnsupportedKeyTypeError extends Error { | ||
static name: string; | ||
constructor(message?: string); | ||
} | ||
//# sourceMappingURL=errors.d.ts.map |
@@ -7,65 +7,302 @@ /** | ||
export class AbortError extends Error { | ||
code; | ||
type; | ||
static name = 'AbortError'; | ||
constructor(message = 'The operation was aborted') { | ||
super(message); | ||
this.name = 'AbortError'; | ||
this.code = AbortError.code; | ||
this.type = AbortError.type; | ||
} | ||
static code = 'ABORT_ERR'; | ||
static type = 'aborted'; | ||
} | ||
export class CodeError extends Error { | ||
code; | ||
props; | ||
constructor(message, code, props) { | ||
super(message); | ||
this.code = code; | ||
this.name = props?.name ?? 'CodeError'; | ||
this.props = props ?? {}; // eslint-disable-line @typescript-eslint/consistent-type-assertions | ||
} | ||
} | ||
export class AggregateCodeError extends AggregateError { | ||
code; | ||
props; | ||
constructor(errors, message, code, props) { | ||
super(errors, message); | ||
this.code = code; | ||
this.name = props?.name ?? 'AggregateCodeError'; | ||
this.props = props ?? {}; // eslint-disable-line @typescript-eslint/consistent-type-assertions | ||
} | ||
} | ||
/** | ||
* Thrown when a remote Peer ID does not match the expected one | ||
*/ | ||
export class UnexpectedPeerError extends Error { | ||
code; | ||
static name = 'UnexpectedPeerError'; | ||
constructor(message = 'Unexpected Peer') { | ||
super(message); | ||
this.name = 'UnexpectedPeerError'; | ||
this.code = UnexpectedPeerError.code; | ||
} | ||
static code = 'ERR_UNEXPECTED_PEER'; | ||
} | ||
/** | ||
* Thrown when a crypto exchange fails | ||
*/ | ||
export class InvalidCryptoExchangeError extends Error { | ||
code; | ||
static name = 'InvalidCryptoExchangeError'; | ||
constructor(message = 'Invalid crypto exchange') { | ||
super(message); | ||
this.name = 'InvalidCryptoExchangeError'; | ||
this.code = InvalidCryptoExchangeError.code; | ||
} | ||
static code = 'ERR_INVALID_CRYPTO_EXCHANGE'; | ||
} | ||
export class InvalidCryptoTransmissionError extends Error { | ||
code; | ||
constructor(message = 'Invalid crypto transmission') { | ||
/** | ||
* Thrown when invalid parameters are passed to a function or method call | ||
*/ | ||
export class InvalidParametersError extends Error { | ||
static name = 'InvalidParametersError'; | ||
constructor(message = 'Invalid parameters') { | ||
super(message); | ||
this.name = 'InvalidCryptoTransmissionError'; | ||
this.code = InvalidCryptoTransmissionError.code; | ||
this.name = 'InvalidParametersError'; | ||
} | ||
static code = 'ERR_INVALID_CRYPTO_TRANSMISSION'; | ||
} | ||
// Error codes | ||
export const ERR_TIMEOUT = 'ERR_TIMEOUT'; | ||
export const ERR_INVALID_PARAMETERS = 'ERR_INVALID_PARAMETERS'; | ||
export const ERR_NOT_FOUND = 'ERR_NOT_FOUND'; | ||
export const ERR_INVALID_MESSAGE = 'ERR_INVALID_MESSAGE'; | ||
/** | ||
* Thrown when a public key is invalid | ||
*/ | ||
export class InvalidPublicKeyError extends Error { | ||
static name = 'InvalidPublicKeyError'; | ||
constructor(message = 'Invalid public key') { | ||
super(message); | ||
this.name = 'InvalidPublicKeyError'; | ||
} | ||
} | ||
/** | ||
* Thrown when a private key is invalid | ||
*/ | ||
export class InvalidPrivateKeyError extends Error { | ||
static name = 'InvalidPrivateKeyError'; | ||
constructor(message = 'Invalid private key') { | ||
super(message); | ||
this.name = 'InvalidPrivateKeyError'; | ||
} | ||
} | ||
/** | ||
* Thrown when a operation is unsupported | ||
*/ | ||
export class UnsupportedOperationError extends Error { | ||
static name = 'UnsupportedOperationError'; | ||
constructor(message = 'Unsupported operation') { | ||
super(message); | ||
this.name = 'UnsupportedOperationError'; | ||
} | ||
} | ||
/** | ||
* Thrown when a connection is closing | ||
*/ | ||
export class ConnectionClosingError extends Error { | ||
static name = 'ConnectionClosingError'; | ||
constructor(message = 'The connection is closing') { | ||
super(message); | ||
this.name = 'ConnectionClosingError'; | ||
} | ||
} | ||
/** | ||
* Thrown when a connection is closed | ||
*/ | ||
export class ConnectionClosedError extends Error { | ||
static name = 'ConnectionClosedError'; | ||
constructor(message = 'The connection is closed') { | ||
super(message); | ||
this.name = 'ConnectionClosedError'; | ||
} | ||
} | ||
/** | ||
* Thrown when a connection fails | ||
*/ | ||
export class ConnectionFailedError extends Error { | ||
static name = 'ConnectionFailedError'; | ||
constructor(message = 'Connection failed') { | ||
super(message); | ||
this.name = 'ConnectionFailedError'; | ||
} | ||
} | ||
/** | ||
* Thrown when the muxer is closed and an attempt to open a stream occurs | ||
*/ | ||
export class MuxerClosedError extends Error { | ||
static name = 'MuxerClosedError'; | ||
constructor(message = 'The muxer is closed') { | ||
super(message); | ||
this.name = 'MuxerClosedError'; | ||
} | ||
} | ||
/** | ||
* Thrown when a protocol stream is reset by the remote muxer | ||
*/ | ||
export class StreamResetError extends Error { | ||
static name = 'StreamResetError'; | ||
constructor(message = 'The stream has been reset') { | ||
super(message); | ||
this.name = 'StreamResetError'; | ||
} | ||
} | ||
/** | ||
* Thrown when a stream is in an invalid state | ||
*/ | ||
export class StreamStateError extends Error { | ||
static name = 'StreamStateError'; | ||
constructor(message = 'The stream is in an invalid state') { | ||
super(message); | ||
this.name = 'StreamStateError'; | ||
} | ||
} | ||
/** | ||
* Thrown when a value could not be found | ||
*/ | ||
export class NotFoundError extends Error { | ||
static name = 'NotFoundError'; | ||
constructor(message = 'Not found') { | ||
super(message); | ||
this.name = 'NotFoundError'; | ||
} | ||
} | ||
/** | ||
* Thrown when an invalid peer ID is encountered | ||
*/ | ||
export class InvalidPeerIdError extends Error { | ||
static name = 'InvalidPeerIdError'; | ||
constructor(message = 'Invalid PeerID') { | ||
super(message); | ||
this.name = 'InvalidPeerIdError'; | ||
} | ||
} | ||
/** | ||
* Thrown when an invalid multiaddr is encountered | ||
*/ | ||
export class InvalidMultiaddrError extends Error { | ||
static name = 'InvalidMultiaddrError'; | ||
constructor(message = 'Invalid multiaddr') { | ||
super(message); | ||
this.name = 'InvalidMultiaddrError'; | ||
} | ||
} | ||
/** | ||
* Thrown when an invalid CID is encountered | ||
*/ | ||
export class InvalidCIDError extends Error { | ||
static name = 'InvalidCIDError'; | ||
constructor(message = 'Invalid CID') { | ||
super(message); | ||
this.name = 'InvalidCIDError'; | ||
} | ||
} | ||
/** | ||
* Thrown when an invalid multihash is encountered | ||
*/ | ||
export class InvalidMultihashError extends Error { | ||
static name = 'InvalidMultihashError'; | ||
constructor(message = 'Invalid Multihash') { | ||
super(message); | ||
this.name = 'InvalidMultihashError'; | ||
} | ||
} | ||
/** | ||
* Thrown when a protocol is not supported | ||
*/ | ||
export class UnsupportedProtocolError extends Error { | ||
static name = 'UnsupportedProtocolError'; | ||
constructor(message = 'Unsupported protocol error') { | ||
super(message); | ||
this.name = 'UnsupportedProtocolError'; | ||
} | ||
} | ||
/** | ||
* An invalid or malformed message was encountered during a protocol exchange | ||
*/ | ||
export class InvalidMessageError extends Error { | ||
static name = 'InvalidMessageError'; | ||
constructor(message = 'Invalid message') { | ||
super(message); | ||
this.name = 'InvalidMessageError'; | ||
} | ||
} | ||
/** | ||
* Thrown when a remote peer sends a structurally valid message that does not | ||
* comply with the protocol | ||
*/ | ||
export class ProtocolError extends Error { | ||
static name = 'ProtocolError'; | ||
constructor(message = 'Protocol error') { | ||
super(message); | ||
this.name = 'ProtocolError'; | ||
} | ||
} | ||
/** | ||
* Throw when an operation times out | ||
*/ | ||
export class TimeoutError extends Error { | ||
static name = 'TimeoutError'; | ||
constructor(message = 'Timed out') { | ||
super(message); | ||
this.name = 'TimeoutError'; | ||
} | ||
} | ||
/** | ||
* Thrown when a startable component is interacted with but it has not been | ||
* started yet | ||
*/ | ||
export class NotStartedError extends Error { | ||
static name = 'NotStartedError'; | ||
constructor(message = 'Not started') { | ||
super(message); | ||
this.name = 'NotStartedError'; | ||
} | ||
} | ||
/** | ||
* Thrown when a component is started that has already been started | ||
*/ | ||
export class AlreadyStartedError extends Error { | ||
static name = 'AlreadyStartedError'; | ||
constructor(message = 'Already started') { | ||
super(message); | ||
this.name = 'AlreadyStartedError'; | ||
} | ||
} | ||
/** | ||
* Thrown when dialing an address failed | ||
*/ | ||
export class DialError extends Error { | ||
static name = 'DialError'; | ||
constructor(message = 'Dial error') { | ||
super(message); | ||
this.name = 'DialError'; | ||
} | ||
} | ||
/** | ||
* Thrown when listening on an address failed | ||
*/ | ||
export class ListenError extends Error { | ||
static name = 'ListenError'; | ||
constructor(message = 'Listen error') { | ||
super(message); | ||
this.name = 'ListenError'; | ||
} | ||
} | ||
/** | ||
* This error is thrown when a limited connection is encountered, i.e. if the | ||
* user tried to open a stream on a connection for a protocol that is not | ||
* configured to run over limited connections. | ||
*/ | ||
export class LimitedConnectionError extends Error { | ||
static name = 'LimitedConnectionError'; | ||
constructor(message = 'Limited connection') { | ||
super(message); | ||
this.name = 'LimitedConnectionError'; | ||
} | ||
} | ||
/** | ||
* This error is thrown where there are too many inbound protocols streams open | ||
*/ | ||
export class TooManyInboundProtocolStreamsError extends Error { | ||
static name = 'TooManyInboundProtocolStreamsError'; | ||
constructor(message = 'Too many inbound protocol streams') { | ||
super(message); | ||
this.name = 'TooManyInboundProtocolStreamsError'; | ||
} | ||
} | ||
/** | ||
* This error is thrown where there are too many outbound protocols streams open | ||
*/ | ||
export class TooManyOutboundProtocolStreamsError extends Error { | ||
static name = 'TooManyOutboundProtocolStreamsError'; | ||
constructor(message = 'Too many outbound protocol streams') { | ||
super(message); | ||
this.name = 'TooManyOutboundProtocolStreamsError'; | ||
} | ||
} | ||
/** | ||
* Thrown when and attempt to operate on an unsupported key was made | ||
*/ | ||
export class UnsupportedKeyTypeError extends Error { | ||
static name = 'UnsupportedKeyTypeError'; | ||
constructor(message = 'Unsupported key type') { | ||
super(message); | ||
this.name = 'UnsupportedKeyTypeError'; | ||
} | ||
} | ||
//# sourceMappingURL=errors.js.map |
@@ -36,6 +36,2 @@ export interface EventCallback<EventType> { | ||
} | ||
export declare const CustomEvent: { | ||
new <T>(type: string, eventInitDict?: CustomEventInit<T> | undefined): CustomEvent<T>; | ||
prototype: CustomEvent<any>; | ||
}; | ||
//# sourceMappingURL=event-target.d.ts.map |
@@ -56,3 +56,2 @@ import { setMaxListeners } from './events.js'; | ||
} | ||
export const CustomEvent = globalThis.CustomEvent; | ||
//# sourceMappingURL=event-target.js.map |
@@ -19,4 +19,5 @@ /** | ||
import type { TypedEventTarget } from './event-target.js'; | ||
import type { Ed25519PublicKey, PublicKey, RSAPublicKey, Secp256k1PublicKey } from './keys/index.js'; | ||
import type { Metrics } from './metrics/index.js'; | ||
import type { PeerId } from './peer-id/index.js'; | ||
import type { Ed25519PeerId, PeerId, RSAPeerId, Secp256k1PeerId, URLPeerId } from './peer-id/index.js'; | ||
import type { PeerInfo } from './peer-info/index.js'; | ||
@@ -299,3 +300,3 @@ import type { PeerRouting } from './peer-routing/index.js'; | ||
*/ | ||
runOnTransientConnection?: boolean; | ||
runOnLimitedConnection?: boolean; | ||
} | ||
@@ -305,2 +306,6 @@ export type TransportManagerDialProgressEvents = ProgressEvent<'transport-manager:selected-transport', string>; | ||
export interface DialOptions extends AbortOptions, ProgressOptions { | ||
/** | ||
* If true, open a new connection to the remote even if one already exists | ||
*/ | ||
force?: boolean; | ||
} | ||
@@ -578,3 +583,7 @@ export interface DialProtocolOptions extends NewStreamOptions { | ||
*/ | ||
getPublicKey(peer: PeerId, options?: AbortOptions): Promise<Uint8Array>; | ||
getPublicKey(peer: Ed25519PeerId, options?: AbortOptions): Promise<Ed25519PublicKey>; | ||
getPublicKey(peer: Secp256k1PeerId, options?: AbortOptions): Promise<Secp256k1PublicKey>; | ||
getPublicKey(peer: RSAPeerId, options?: AbortOptions): Promise<RSAPublicKey>; | ||
getPublicKey(peer: URLPeerId, options?: AbortOptions): Promise<never>; | ||
getPublicKey(peer: PeerId, options?: AbortOptions): Promise<PublicKey>; | ||
/** | ||
@@ -581,0 +590,0 @@ * Given the current node configuration, returns a promise of `true` or |
@@ -0,36 +1,87 @@ | ||
import type { CID } from 'multiformats/cid'; | ||
import type { MultihashDigest } from 'multiformats/hashes/interface'; | ||
import type { Uint8ArrayList } from 'uint8arraylist'; | ||
export interface PublicKey<Type extends KeyType = 'Ed25519'> { | ||
readonly bytes: Uint8Array; | ||
export type KeyType = 'RSA' | 'Ed25519' | 'secp256k1'; | ||
interface PublicKeyBase<KeyType extends string, DigestCode extends number = number> { | ||
/** | ||
* The type of this key | ||
*/ | ||
readonly type: KeyType; | ||
/** | ||
* The raw public key bytes (for Ed25519 and secp256k1 keys) or PKIX in ASN1 | ||
* DER format (for RSA keys) | ||
*/ | ||
readonly raw: Uint8Array; | ||
/** | ||
* Returns `true` if the passed object matches this key | ||
*/ | ||
equals(key?: any): boolean; | ||
/** | ||
* Returns this public key as a Multihash digest. | ||
* | ||
* It contains either an identity hash containing the protobuf version of the | ||
* public key (for Ed25519 and secp256k1 keys) or a sha256 hash of the | ||
* protobuf version of the public key (RSA keys). | ||
*/ | ||
toMultihash(): MultihashDigest<DigestCode>; | ||
/** | ||
* Return this public key as a CID encoded with the `libp2p-key` codec | ||
* | ||
* The digest contains either an identity hash containing the protobuf version | ||
* of the public key (for Ed25519 and secp256k1 keys) or a sha256 hash of the | ||
* protobuf version of the public key (RSA keys). | ||
*/ | ||
toCID(): CID<unknown, 0x72, DigestCode, 1>; | ||
/** | ||
* Verify the passed data was signed by the private key corresponding to this | ||
* public key | ||
*/ | ||
verify(data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean | Promise<boolean>; | ||
marshal(): Uint8Array; | ||
equals(key: PublicKey<Type>): boolean; | ||
hash(): Uint8Array | Promise<Uint8Array>; | ||
/** | ||
* Returns this key as a multihash with base58btc encoding | ||
*/ | ||
toString(): string; | ||
} | ||
export interface RSAPublicKey extends PublicKeyBase<'RSA', 0x12> { | ||
} | ||
export interface Ed25519PublicKey extends PublicKeyBase<'Ed25519', 0x0> { | ||
} | ||
export interface Secp256k1PublicKey extends PublicKeyBase<'secp256k1', 0x0> { | ||
} | ||
export type PublicKey = RSAPublicKey | Ed25519PublicKey | Secp256k1PublicKey; | ||
/** | ||
* Generic private key interface | ||
*/ | ||
export interface PrivateKey<Type extends KeyType = 'Ed25519'> { | ||
readonly public: PublicKey<Type>; | ||
readonly bytes: Uint8Array; | ||
sign(data: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array>; | ||
marshal(): Uint8Array; | ||
equals(key: PrivateKey<Type>): boolean; | ||
hash(): Uint8Array | Promise<Uint8Array>; | ||
interface PrivateKeyBase<KeyType extends string, PublicKeyType extends PublicKeyBase<KeyType>> { | ||
/** | ||
* 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. | ||
* The type of this key | ||
*/ | ||
id(): Promise<string>; | ||
readonly type: KeyType; | ||
/** | ||
* Exports the password protected key in the format specified. | ||
* The public key that corresponds to this private key | ||
*/ | ||
export(password: string, format?: 'pkcs-8' | string): Promise<string>; | ||
readonly publicKey: PublicKeyType; | ||
/** | ||
* The raw public key bytes (for Ed25519 and secp256k1 keys) or PKIX in ASN1 | ||
* DER format (for RSA keys) | ||
*/ | ||
readonly raw: Uint8Array; | ||
/** | ||
* Returns `true` if the passed object matches this key | ||
*/ | ||
equals(key?: any): boolean; | ||
/** | ||
* Sign the passed data with this private key and return the signature for | ||
* later verification | ||
*/ | ||
sign(data: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array>; | ||
} | ||
export declare const Ed25519 = "Ed25519"; | ||
export declare const RSA = "RSA"; | ||
export declare const secp256k1 = "secp256k1"; | ||
export type KeyType = typeof Ed25519 | typeof RSA | typeof secp256k1; | ||
export interface RSAPrivateKey extends PrivateKeyBase<'RSA', RSAPublicKey> { | ||
} | ||
export interface Ed25519PrivateKey extends PrivateKeyBase<'Ed25519', Ed25519PublicKey> { | ||
} | ||
export interface Secp256k1PrivateKey extends PrivateKeyBase<'secp256k1', Secp256k1PublicKey> { | ||
} | ||
export type PrivateKey = RSAPrivateKey | Ed25519PrivateKey | Secp256k1PrivateKey; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,4 +0,2 @@ | ||
export const Ed25519 = 'Ed25519'; | ||
export const RSA = 'RSA'; | ||
export const secp256k1 = 'secp256k1'; | ||
export {}; | ||
//# sourceMappingURL=index.js.map |
@@ -70,17 +70,17 @@ import type { MultiaddrConnection, Stream, Connection } from '../connection/index.js'; | ||
*/ | ||
export interface MetricGroup { | ||
export interface MetricGroup<T extends string = any> { | ||
/** | ||
* Update the stored metric group to the passed value | ||
*/ | ||
update(values: Record<string, number>): void; | ||
update(values: Partial<Record<T, number>>): void; | ||
/** | ||
* Increment the metric group keys by the passed number or | ||
* any non-numeric value to increment by 1 | ||
* `true` to increment by 1 | ||
*/ | ||
increment(values: Record<string, number | unknown>): void; | ||
increment(values: Partial<Record<T, number | true>>): void; | ||
/** | ||
* Decrement the metric group keys by the passed number or | ||
* any non-numeric value to decrement by 1 | ||
* `true` to decrement by 1 | ||
*/ | ||
decrement(values: Record<string, number | unknown>): void; | ||
decrement(values: Partial<Record<T, number | true>>): void; | ||
/** | ||
@@ -116,3 +116,3 @@ * Reset the passed key in this metric group to its default value | ||
*/ | ||
export interface CounterGroup { | ||
export interface CounterGroup<T extends string = any> { | ||
/** | ||
@@ -122,3 +122,3 @@ * Increment the metric group keys by the passed number or | ||
*/ | ||
increment(values: Record<string, number | unknown>): void; | ||
increment(values: Partial<Record<T, number | true>>): void; | ||
/** | ||
@@ -125,0 +125,0 @@ * Reset the passed key in this metric group to its default value |
@@ -1,32 +0,134 @@ | ||
import type { KeyType } from '../keys/index.js'; | ||
import type { Ed25519PublicKey, KeyType, RSAPublicKey, Secp256k1PublicKey } from '../keys/index.js'; | ||
import type { CID } from 'multiformats/cid'; | ||
import type { MultihashDigest } from 'multiformats/hashes/interface'; | ||
export type PeerIdType = KeyType | string; | ||
export interface RSAPeerId extends PeerId { | ||
/** | ||
* A PeerId generated from an RSA public key - it is a base58btc encoded sha-256 | ||
* hash of the public key. | ||
* | ||
* RSA public keys are too large to pass around freely, instead Ed25519 or | ||
* secp256k1 should be preferred as they can embed their public key in the | ||
* PeerId itself. | ||
* | ||
* @deprecated Ed25519 or secp256k1 keys are preferred to RSA | ||
*/ | ||
export interface RSAPeerId { | ||
readonly type: 'RSA'; | ||
readonly publicKey?: Uint8Array; | ||
/** | ||
* RSA public keys are too large to embed in the multihash commonly used to | ||
* refer to peers, so this will only be defined if the public key has | ||
* previously been found through a routing query or during normal protocol | ||
* operations | ||
*/ | ||
readonly publicKey?: RSAPublicKey; | ||
/** | ||
* Returns the multihash from `toMultihash()` as a base58btc encoded string | ||
*/ | ||
toString(): string; | ||
/** | ||
* Returns a multihash, the digest of which is the SHA2-256 hash of the public | ||
* key | ||
*/ | ||
toMultihash(): MultihashDigest<0x12>; | ||
/** | ||
* Returns a CID with the libp2p key code and the same multihash as | ||
* `toMultihash()` | ||
*/ | ||
toCID(): CID<Uint8Array, 0x72, 0x12, 1>; | ||
/** | ||
* Returns true if the passed argument is equivalent to this PeerId | ||
*/ | ||
equals(other?: any): boolean; | ||
} | ||
export interface Ed25519PeerId extends PeerId { | ||
export interface Ed25519PeerId { | ||
readonly type: 'Ed25519'; | ||
readonly publicKey: Uint8Array; | ||
/** | ||
* This will always be defined as the public key is embedded in the multihash | ||
* of this PeerId | ||
*/ | ||
readonly publicKey: Ed25519PublicKey; | ||
/** | ||
* Returns the multihash from `toMultihash()` as a base58btc encoded string | ||
*/ | ||
toString(): string; | ||
/** | ||
* Returns a multihash, the digest of which is the protobuf-encoded public key | ||
* encoded as an identity hash | ||
*/ | ||
toMultihash(): MultihashDigest<0x0>; | ||
/** | ||
* Returns a CID with the libp2p key code and the same multihash as | ||
* `toMultihash()` | ||
*/ | ||
toCID(): CID<Uint8Array, 0x72, 0x0, 1>; | ||
/** | ||
* Returns true if the passed argument is equivalent to this PeerId | ||
*/ | ||
equals(other?: any): boolean; | ||
} | ||
export interface Secp256k1PeerId extends PeerId { | ||
export interface Secp256k1PeerId { | ||
readonly type: 'secp256k1'; | ||
readonly publicKey: Uint8Array; | ||
/** | ||
* This will always be defined as the public key is embedded in the multihash | ||
* of this PeerId | ||
*/ | ||
readonly publicKey: Secp256k1PublicKey; | ||
/** | ||
* Returns the multihash from `toMultihash()` as a base58btc encoded string | ||
*/ | ||
toString(): string; | ||
/** | ||
* Returns a multihash, the digest of which is the protobuf-encoded public key | ||
* encoded as an identity hash | ||
*/ | ||
toMultihash(): MultihashDigest<0x0>; | ||
/** | ||
* Returns a CID with the libp2p key code and the same multihash as | ||
* `toMultihash()` | ||
*/ | ||
toCID(): CID<Uint8Array, 0x72, 0x0, 1>; | ||
/** | ||
* Returns true if the passed argument is equivalent to this PeerId | ||
*/ | ||
equals(other?: any): boolean; | ||
} | ||
export interface URLPeerId extends PeerId { | ||
export interface URLPeerId { | ||
readonly type: 'url'; | ||
} | ||
export interface PeerId { | ||
type: PeerIdType; | ||
multihash: MultihashDigest; | ||
privateKey?: Uint8Array; | ||
publicKey?: Uint8Array; | ||
/** | ||
* This will always be undefined as URL Peers do not have public keys | ||
*/ | ||
readonly publicKey: undefined; | ||
/** | ||
* Returns CID from `toCID()` encoded as a base36 string | ||
*/ | ||
toString(): string; | ||
toCID(): CID; | ||
toBytes(): Uint8Array; | ||
equals(other?: PeerId | Uint8Array | string): boolean; | ||
/** | ||
* Returns a multihash, the digest of which is the URL encoded as an identity | ||
* hash | ||
*/ | ||
toMultihash(): MultihashDigest<0x0>; | ||
/** | ||
* Returns a CID with the Transport IPFS Gateway HTTP code and the same | ||
* multihash as `toMultihash()` | ||
*/ | ||
toCID(): CID<Uint8Array, 0x0920, 0x0, 1>; | ||
/** | ||
* Returns true if the passed argument is equivalent to this PeerId | ||
*/ | ||
equals(other?: any): boolean; | ||
} | ||
/** | ||
* This is a union of all known PeerId types - use the `.type` field to | ||
* disambiguate them | ||
*/ | ||
export type PeerId = RSAPeerId | Ed25519PeerId | Secp256k1PeerId | URLPeerId; | ||
/** | ||
* All PeerId implementations must use this symbol as the name of a property | ||
* with a boolean `true` value | ||
*/ | ||
export declare const peerIdSymbol: unique symbol; | ||
export declare function isPeerId(other: any): other is PeerId; | ||
/** | ||
* Returns true if the passed argument is a PeerId implementation | ||
*/ | ||
export declare function isPeerId(other?: any): other is PeerId; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -0,5 +1,12 @@ | ||
/** | ||
* All PeerId implementations must use this symbol as the name of a property | ||
* with a boolean `true` value | ||
*/ | ||
export const peerIdSymbol = Symbol.for('@libp2p/peer-id'); | ||
/** | ||
* Returns true if the passed argument is a PeerId implementation | ||
*/ | ||
export function isPeerId(other) { | ||
return other != null && Boolean(other[peerIdSymbol]); | ||
return Boolean(other?.[peerIdSymbol]); | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -0,1 +1,2 @@ | ||
import type { PublicKey } from '../keys/index.js'; | ||
import type { PeerId } from '../peer-id/index.js'; | ||
@@ -80,5 +81,7 @@ import type { Multiaddr } from '@multiformats/multiaddr'; | ||
/** | ||
* If this Peer has an RSA key, it's public key can be set with this property | ||
* If this Peer has an RSA key, it's public key can be set with this property. | ||
* | ||
* The byte array should be the protobuf encoded form of the public key. | ||
*/ | ||
publicKey?: Uint8Array; | ||
publicKey?: PublicKey; | ||
/** | ||
@@ -85,0 +88,0 @@ * The last peer record envelope received |
import type { Stream } from '../connection/index.js'; | ||
import type { TypedEventTarget } from '../event-target.js'; | ||
import type { PublicKey } from '../keys/index.js'; | ||
import type { PeerId } from '../peer-id/index.js'; | ||
@@ -34,3 +35,3 @@ import type { Pushable } from 'it-pushable'; | ||
signature: Uint8Array; | ||
key: Uint8Array; | ||
key: PublicKey; | ||
} | ||
@@ -37,0 +38,0 @@ export interface UnsignedMessage { |
@@ -1,2 +0,2 @@ | ||
import type { PeerId } from '../peer-id/index.js'; | ||
import type { PublicKey } from '../keys/index.js'; | ||
import type { Uint8ArrayList } from 'uint8arraylist'; | ||
@@ -25,3 +25,3 @@ /** | ||
export interface Envelope { | ||
peerId: PeerId; | ||
publicKey: PublicKey; | ||
payloadType: Uint8Array | Uint8ArrayList; | ||
@@ -28,0 +28,0 @@ payload: Uint8Array; |
@@ -19,6 +19,6 @@ import type { Connection, Stream } from '../connection/index.js'; | ||
/** | ||
* Opt-in to running over a transient connection - one that has time/data limits | ||
* placed on it. | ||
* Opt-in to running over connections with limits on how much data can be | ||
* transferred or how long it can be open for. | ||
*/ | ||
runOnTransientConnection?: boolean; | ||
runOnLimitedConnection?: boolean; | ||
} | ||
@@ -25,0 +25,0 @@ export interface StreamHandlerRecord { |
@@ -27,8 +27,9 @@ import type { Connection } from '../connection/index.js'; | ||
/** | ||
* If true, invoke `onConnect` for this topology on transient (e.g. short-lived | ||
* and/or data-limited) connections | ||
* If true, invoke `onConnect` for this topology on limited connections, e.g. | ||
* ones with limits on how much data can be transferred or how long they can | ||
* be open for. | ||
* | ||
* @default false | ||
*/ | ||
notifyOnTransient?: boolean; | ||
notifyOnLimitedConnection?: boolean; | ||
/** | ||
@@ -35,0 +36,0 @@ * Invoked when a new connection is opened to a peer that supports the |
@@ -1,2 +0,2 @@ | ||
import type { Connection, MultiaddrConnection } from '../connection/index.js'; | ||
import type { Connection, ConnectionLimits, MultiaddrConnection } from '../connection/index.js'; | ||
import type { TypedEventTarget } from '../event-target.js'; | ||
@@ -88,11 +88,7 @@ import type { AbortOptions } from '../index.js'; | ||
} | ||
export interface UpgraderOptions<ConnectionUpgradeEvents extends ProgressEvent = ProgressEvent> extends ProgressOptions<ConnectionUpgradeEvents> { | ||
export interface UpgraderOptions<ConnectionUpgradeEvents extends ProgressEvent = ProgressEvent> extends ProgressOptions<ConnectionUpgradeEvents>, AbortOptions { | ||
skipEncryption?: boolean; | ||
skipProtection?: boolean; | ||
muxerFactory?: StreamMuxerFactory; | ||
/** | ||
* The passed MultiaddrConnection has limits place on duration and/or data | ||
* transfer amounts so is not expected to be open for very long. | ||
*/ | ||
transient?: boolean; | ||
limits?: ConnectionLimits; | ||
} | ||
@@ -99,0 +95,0 @@ export type InboundConnectionUpgradeEvents = ProgressEvent<'upgrader:encrypt-inbound-connection'> | ProgressEvent<'upgrader:multiplex-inbound-connection'>; |
{ | ||
"name": "@libp2p/interface", | ||
"version": "1.7.0", | ||
"version": "2.0.0-1210884ed", | ||
"description": "The interface implemented by a libp2p node", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0 OR MIT", |
import type { MultiaddrConnection } from '../connection/index.js' | ||
import type { AbortOptions } from '../index.js' | ||
import type { PeerId } from '../peer-id/index.js' | ||
@@ -7,2 +8,11 @@ import type { Duplex } from 'it-stream-types' | ||
/** | ||
* If the remote PeerId is known and passed as an option, the securing operation | ||
* will throw if the remote peer cannot prove it has the private key that | ||
* corresponds to the public key the remote PeerId is derived from. | ||
*/ | ||
export interface SecureConnectionOptions extends AbortOptions { | ||
remotePeer?: PeerId | ||
} | ||
/** | ||
* A libp2p connection encrypter module must be compliant to this interface | ||
@@ -19,3 +29,3 @@ * to ensure all exchanged data between two peers is encrypted. | ||
*/ | ||
secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localPeer: PeerId, connection: Stream, remotePeer?: PeerId): Promise<SecuredConnection<Stream, Extension>> | ||
secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (connection: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream, Extension>> | ||
@@ -27,3 +37,3 @@ /** | ||
*/ | ||
secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localPeer: PeerId, connection: Stream, remotePeer?: PeerId): Promise<SecuredConnection<Stream, Extension>> | ||
secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (connection: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream, Extension>> | ||
} | ||
@@ -30,0 +40,0 @@ |
@@ -188,8 +188,12 @@ import type { AbortOptions, Logger } from '../index.js' | ||
/** | ||
* Opt-in to running over a transient connection - one that has time/data limits | ||
* placed on it. | ||
* Opt-in to running over a limited connection - one that has restrictions | ||
* on the amount of data that may be transferred or how long it may be open for. | ||
* | ||
* These limits are typically enforced by a relay server, if the protocol | ||
* will be transferring a lot of data or the stream will be open for a long time | ||
* consider upgrading to a direct connection before opening the stream. | ||
* | ||
* @default false | ||
*/ | ||
runOnTransientConnection?: boolean | ||
runOnLimitedConnection?: boolean | ||
@@ -227,2 +231,25 @@ /** | ||
/** | ||
* Connection limits are present on connections that are only allowed to | ||
* transfer a certain amount of bytes or be open for a certain number | ||
* of seconds. | ||
* | ||
* These limits are applied by Circuit Relay v2 servers, for example and | ||
* the connection will normally be closed abruptly if the limits are | ||
* exceeded. | ||
*/ | ||
export interface ConnectionLimits { | ||
/** | ||
* If present this is the number of bytes remaining that may be | ||
* transferred over this connection | ||
*/ | ||
bytes?: bigint | ||
/** | ||
* If present this is the number of seconds that this connection will | ||
* remain open for | ||
*/ | ||
seconds?: number | ||
} | ||
/** | ||
* A Connection is a high-level representation of a connection | ||
@@ -285,8 +312,7 @@ * to a remote peer that may have been secured by encryption and | ||
/** | ||
* A transient connection is one that is not expected to be open for very long | ||
* or one that cannot transfer very much data, such as one being used as a | ||
* circuit relay connection. Protocols need to explicitly opt-in to being run | ||
* over transient connections. | ||
* If present, this connection has limits applied to it, perhaps by an | ||
* intermediate relay. Once the limits have been reached the connection will | ||
* be closed by the relay. | ||
*/ | ||
transient: boolean | ||
limits?: ConnectionLimits | ||
@@ -293,0 +319,0 @@ /** |
@@ -7,4 +7,3 @@ /** | ||
export class AbortError extends Error { | ||
public readonly code: string | ||
public readonly type: string | ||
static name = 'AbortError' | ||
@@ -14,83 +13,355 @@ constructor (message: string = 'The operation was aborted') { | ||
this.name = 'AbortError' | ||
this.code = AbortError.code | ||
this.type = AbortError.type | ||
} | ||
} | ||
static readonly code = 'ABORT_ERR' | ||
/** | ||
* Thrown when a remote Peer ID does not match the expected one | ||
*/ | ||
export class UnexpectedPeerError extends Error { | ||
static name = 'UnexpectedPeerError' | ||
static readonly type = 'aborted' | ||
constructor (message = 'Unexpected Peer') { | ||
super(message) | ||
this.name = 'UnexpectedPeerError' | ||
} | ||
} | ||
export class CodeError<T extends Record<string, any> = Record<string, never>> extends Error { | ||
public readonly props: T | ||
/** | ||
* Thrown when a crypto exchange fails | ||
*/ | ||
export class InvalidCryptoExchangeError extends Error { | ||
static name = 'InvalidCryptoExchangeError' | ||
constructor ( | ||
message: string, | ||
public readonly code: string, | ||
props?: T | ||
) { | ||
constructor (message = 'Invalid crypto exchange') { | ||
super(message) | ||
this.name = 'InvalidCryptoExchangeError' | ||
} | ||
} | ||
this.name = props?.name ?? 'CodeError' | ||
this.props = props ?? {} as T // eslint-disable-line @typescript-eslint/consistent-type-assertions | ||
/** | ||
* Thrown when invalid parameters are passed to a function or method call | ||
*/ | ||
export class InvalidParametersError extends Error { | ||
static name = 'InvalidParametersError' | ||
constructor (message = 'Invalid parameters') { | ||
super(message) | ||
this.name = 'InvalidParametersError' | ||
} | ||
} | ||
export class AggregateCodeError<T extends Record<string, any> = Record<string, never>> extends AggregateError { | ||
public readonly props: T | ||
/** | ||
* Thrown when a public key is invalid | ||
*/ | ||
export class InvalidPublicKeyError extends Error { | ||
static name = 'InvalidPublicKeyError' | ||
constructor ( | ||
errors: Error[], | ||
message: string, | ||
public readonly code: string, | ||
props?: T | ||
) { | ||
super(errors, message) | ||
constructor (message = 'Invalid public key') { | ||
super(message) | ||
this.name = 'InvalidPublicKeyError' | ||
} | ||
} | ||
this.name = props?.name ?? 'AggregateCodeError' | ||
this.props = props ?? {} as T // eslint-disable-line @typescript-eslint/consistent-type-assertions | ||
/** | ||
* Thrown when a private key is invalid | ||
*/ | ||
export class InvalidPrivateKeyError extends Error { | ||
static name = 'InvalidPrivateKeyError' | ||
constructor (message = 'Invalid private key') { | ||
super(message) | ||
this.name = 'InvalidPrivateKeyError' | ||
} | ||
} | ||
export class UnexpectedPeerError extends Error { | ||
public code: string | ||
/** | ||
* Thrown when a operation is unsupported | ||
*/ | ||
export class UnsupportedOperationError extends Error { | ||
static name = 'UnsupportedOperationError' | ||
constructor (message = 'Unexpected Peer') { | ||
constructor (message = 'Unsupported operation') { | ||
super(message) | ||
this.name = 'UnexpectedPeerError' | ||
this.code = UnexpectedPeerError.code | ||
this.name = 'UnsupportedOperationError' | ||
} | ||
} | ||
static readonly code = 'ERR_UNEXPECTED_PEER' | ||
/** | ||
* Thrown when a connection is closing | ||
*/ | ||
export class ConnectionClosingError extends Error { | ||
static name = 'ConnectionClosingError' | ||
constructor (message = 'The connection is closing') { | ||
super(message) | ||
this.name = 'ConnectionClosingError' | ||
} | ||
} | ||
export class InvalidCryptoExchangeError extends Error { | ||
public code: string | ||
/** | ||
* Thrown when a connection is closed | ||
*/ | ||
export class ConnectionClosedError extends Error { | ||
static name = 'ConnectionClosedError' | ||
constructor (message = 'Invalid crypto exchange') { | ||
constructor (message = 'The connection is closed') { | ||
super(message) | ||
this.name = 'InvalidCryptoExchangeError' | ||
this.code = InvalidCryptoExchangeError.code | ||
this.name = 'ConnectionClosedError' | ||
} | ||
} | ||
static readonly code = 'ERR_INVALID_CRYPTO_EXCHANGE' | ||
/** | ||
* Thrown when a connection fails | ||
*/ | ||
export class ConnectionFailedError extends Error { | ||
static name = 'ConnectionFailedError' | ||
constructor (message = 'Connection failed') { | ||
super(message) | ||
this.name = 'ConnectionFailedError' | ||
} | ||
} | ||
export class InvalidCryptoTransmissionError extends Error { | ||
public code: string | ||
/** | ||
* Thrown when the muxer is closed and an attempt to open a stream occurs | ||
*/ | ||
export class MuxerClosedError extends Error { | ||
static name = 'MuxerClosedError' | ||
constructor (message = 'Invalid crypto transmission') { | ||
constructor (message = 'The muxer is closed') { | ||
super(message) | ||
this.name = 'InvalidCryptoTransmissionError' | ||
this.code = InvalidCryptoTransmissionError.code | ||
this.name = 'MuxerClosedError' | ||
} | ||
} | ||
static readonly code = 'ERR_INVALID_CRYPTO_TRANSMISSION' | ||
/** | ||
* Thrown when a protocol stream is reset by the remote muxer | ||
*/ | ||
export class StreamResetError extends Error { | ||
static name = 'StreamResetError' | ||
constructor (message = 'The stream has been reset') { | ||
super(message) | ||
this.name = 'StreamResetError' | ||
} | ||
} | ||
// Error codes | ||
/** | ||
* Thrown when a stream is in an invalid state | ||
*/ | ||
export class StreamStateError extends Error { | ||
static name = 'StreamStateError' | ||
export const ERR_TIMEOUT = 'ERR_TIMEOUT' | ||
export const ERR_INVALID_PARAMETERS = 'ERR_INVALID_PARAMETERS' | ||
export const ERR_NOT_FOUND = 'ERR_NOT_FOUND' | ||
export const ERR_INVALID_MESSAGE = 'ERR_INVALID_MESSAGE' | ||
constructor (message = 'The stream is in an invalid state') { | ||
super(message) | ||
this.name = 'StreamStateError' | ||
} | ||
} | ||
/** | ||
* Thrown when a value could not be found | ||
*/ | ||
export class NotFoundError extends Error { | ||
static name = 'NotFoundError' | ||
constructor (message = 'Not found') { | ||
super(message) | ||
this.name = 'NotFoundError' | ||
} | ||
} | ||
/** | ||
* Thrown when an invalid peer ID is encountered | ||
*/ | ||
export class InvalidPeerIdError extends Error { | ||
static name = 'InvalidPeerIdError' | ||
constructor (message = 'Invalid PeerID') { | ||
super(message) | ||
this.name = 'InvalidPeerIdError' | ||
} | ||
} | ||
/** | ||
* Thrown when an invalid multiaddr is encountered | ||
*/ | ||
export class InvalidMultiaddrError extends Error { | ||
static name = 'InvalidMultiaddrError' | ||
constructor (message = 'Invalid multiaddr') { | ||
super(message) | ||
this.name = 'InvalidMultiaddrError' | ||
} | ||
} | ||
/** | ||
* Thrown when an invalid CID is encountered | ||
*/ | ||
export class InvalidCIDError extends Error { | ||
static name = 'InvalidCIDError' | ||
constructor (message = 'Invalid CID') { | ||
super(message) | ||
this.name = 'InvalidCIDError' | ||
} | ||
} | ||
/** | ||
* Thrown when an invalid multihash is encountered | ||
*/ | ||
export class InvalidMultihashError extends Error { | ||
static name = 'InvalidMultihashError' | ||
constructor (message = 'Invalid Multihash') { | ||
super(message) | ||
this.name = 'InvalidMultihashError' | ||
} | ||
} | ||
/** | ||
* Thrown when a protocol is not supported | ||
*/ | ||
export class UnsupportedProtocolError extends Error { | ||
static name = 'UnsupportedProtocolError' | ||
constructor (message = 'Unsupported protocol error') { | ||
super(message) | ||
this.name = 'UnsupportedProtocolError' | ||
} | ||
} | ||
/** | ||
* An invalid or malformed message was encountered during a protocol exchange | ||
*/ | ||
export class InvalidMessageError extends Error { | ||
static name = 'InvalidMessageError' | ||
constructor (message = 'Invalid message') { | ||
super(message) | ||
this.name = 'InvalidMessageError' | ||
} | ||
} | ||
/** | ||
* Thrown when a remote peer sends a structurally valid message that does not | ||
* comply with the protocol | ||
*/ | ||
export class ProtocolError extends Error { | ||
static name = 'ProtocolError' | ||
constructor (message = 'Protocol error') { | ||
super(message) | ||
this.name = 'ProtocolError' | ||
} | ||
} | ||
/** | ||
* Throw when an operation times out | ||
*/ | ||
export class TimeoutError extends Error { | ||
static name = 'TimeoutError' | ||
constructor (message = 'Timed out') { | ||
super(message) | ||
this.name = 'TimeoutError' | ||
} | ||
} | ||
/** | ||
* Thrown when a startable component is interacted with but it has not been | ||
* started yet | ||
*/ | ||
export class NotStartedError extends Error { | ||
static name = 'NotStartedError' | ||
constructor (message = 'Not started') { | ||
super(message) | ||
this.name = 'NotStartedError' | ||
} | ||
} | ||
/** | ||
* Thrown when a component is started that has already been started | ||
*/ | ||
export class AlreadyStartedError extends Error { | ||
static name = 'AlreadyStartedError' | ||
constructor (message = 'Already started') { | ||
super(message) | ||
this.name = 'AlreadyStartedError' | ||
} | ||
} | ||
/** | ||
* Thrown when dialing an address failed | ||
*/ | ||
export class DialError extends Error { | ||
static name = 'DialError' | ||
constructor (message = 'Dial error') { | ||
super(message) | ||
this.name = 'DialError' | ||
} | ||
} | ||
/** | ||
* Thrown when listening on an address failed | ||
*/ | ||
export class ListenError extends Error { | ||
static name = 'ListenError' | ||
constructor (message = 'Listen error') { | ||
super(message) | ||
this.name = 'ListenError' | ||
} | ||
} | ||
/** | ||
* This error is thrown when a limited connection is encountered, i.e. if the | ||
* user tried to open a stream on a connection for a protocol that is not | ||
* configured to run over limited connections. | ||
*/ | ||
export class LimitedConnectionError extends Error { | ||
static name = 'LimitedConnectionError' | ||
constructor (message = 'Limited connection') { | ||
super(message) | ||
this.name = 'LimitedConnectionError' | ||
} | ||
} | ||
/** | ||
* This error is thrown where there are too many inbound protocols streams open | ||
*/ | ||
export class TooManyInboundProtocolStreamsError extends Error { | ||
static name = 'TooManyInboundProtocolStreamsError' | ||
constructor (message = 'Too many inbound protocol streams') { | ||
super(message) | ||
this.name = 'TooManyInboundProtocolStreamsError' | ||
} | ||
} | ||
/** | ||
* This error is thrown where there are too many outbound protocols streams open | ||
*/ | ||
export class TooManyOutboundProtocolStreamsError extends Error { | ||
static name = 'TooManyOutboundProtocolStreamsError' | ||
constructor (message = 'Too many outbound protocol streams') { | ||
super(message) | ||
this.name = 'TooManyOutboundProtocolStreamsError' | ||
} | ||
} | ||
/** | ||
* Thrown when and attempt to operate on an unsupported key was made | ||
*/ | ||
export class UnsupportedKeyTypeError extends Error { | ||
static name = 'UnsupportedKeyTypeError' | ||
constructor (message = 'Unsupported key type') { | ||
super(message) | ||
this.name = 'UnsupportedKeyTypeError' | ||
} | ||
} |
@@ -107,3 +107,1 @@ import { setMaxListeners } from './events.js' | ||
} | ||
export const CustomEvent = globalThis.CustomEvent |
@@ -20,4 +20,5 @@ /** | ||
import type { TypedEventTarget } from './event-target.js' | ||
import type { Ed25519PublicKey, PublicKey, RSAPublicKey, Secp256k1PublicKey } from './keys/index.js' | ||
import type { Metrics } from './metrics/index.js' | ||
import type { PeerId } from './peer-id/index.js' | ||
import type { Ed25519PeerId, PeerId, RSAPeerId, Secp256k1PeerId, URLPeerId } from './peer-id/index.js' | ||
import type { PeerInfo } from './peer-info/index.js' | ||
@@ -335,3 +336,3 @@ import type { PeerRouting } from './peer-routing/index.js' | ||
*/ | ||
runOnTransientConnection?: boolean | ||
runOnLimitedConnection?: boolean | ||
} | ||
@@ -352,3 +353,6 @@ | ||
export interface DialOptions extends AbortOptions, ProgressOptions { | ||
/** | ||
* If true, open a new connection to the remote even if one already exists | ||
*/ | ||
force?: boolean | ||
} | ||
@@ -648,3 +652,7 @@ | ||
*/ | ||
getPublicKey(peer: PeerId, options?: AbortOptions): Promise<Uint8Array> | ||
getPublicKey(peer: Ed25519PeerId, options?: AbortOptions): Promise<Ed25519PublicKey> | ||
getPublicKey(peer: Secp256k1PeerId, options?: AbortOptions): Promise<Secp256k1PublicKey> | ||
getPublicKey(peer: RSAPeerId, options?: AbortOptions): Promise<RSAPublicKey> | ||
getPublicKey(peer: URLPeerId, options?: AbortOptions): Promise<never> | ||
getPublicKey(peer: PeerId, options?: AbortOptions): Promise<PublicKey> | ||
@@ -651,0 +659,0 @@ /** |
@@ -0,39 +1,94 @@ | ||
import type { CID } from 'multiformats/cid' | ||
import type { MultihashDigest } from 'multiformats/hashes/interface' | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
export interface PublicKey<Type extends KeyType = 'Ed25519'> { | ||
readonly bytes: Uint8Array | ||
export type KeyType = 'RSA' | 'Ed25519' | 'secp256k1' | ||
interface PublicKeyBase<KeyType extends string, DigestCode extends number = number> { | ||
/** | ||
* The type of this key | ||
*/ | ||
readonly type: KeyType | ||
/** | ||
* The raw public key bytes (for Ed25519 and secp256k1 keys) or PKIX in ASN1 | ||
* DER format (for RSA keys) | ||
*/ | ||
readonly raw: Uint8Array | ||
/** | ||
* Returns `true` if the passed object matches this key | ||
*/ | ||
equals(key?: any): boolean | ||
/** | ||
* Returns this public key as a Multihash digest. | ||
* | ||
* It contains either an identity hash containing the protobuf version of the | ||
* public key (for Ed25519 and secp256k1 keys) or a sha256 hash of the | ||
* protobuf version of the public key (RSA keys). | ||
*/ | ||
toMultihash(): MultihashDigest<DigestCode> | ||
/** | ||
* Return this public key as a CID encoded with the `libp2p-key` codec | ||
* | ||
* The digest contains either an identity hash containing the protobuf version | ||
* of the public key (for Ed25519 and secp256k1 keys) or a sha256 hash of the | ||
* protobuf version of the public key (RSA keys). | ||
*/ | ||
toCID(): CID<unknown, 0x72, DigestCode, 1> | ||
/** | ||
* Verify the passed data was signed by the private key corresponding to this | ||
* public key | ||
*/ | ||
verify(data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean | Promise<boolean> | ||
marshal(): Uint8Array | ||
equals(key: PublicKey<Type>): boolean | ||
hash(): Uint8Array | Promise<Uint8Array> | ||
/** | ||
* Returns this key as a multihash with base58btc encoding | ||
*/ | ||
toString(): string | ||
} | ||
export interface RSAPublicKey extends PublicKeyBase<'RSA', 0x12> {} | ||
export interface Ed25519PublicKey extends PublicKeyBase<'Ed25519', 0x0> {} | ||
export interface Secp256k1PublicKey extends PublicKeyBase<'secp256k1', 0x0> {} | ||
export type PublicKey = RSAPublicKey | Ed25519PublicKey | Secp256k1PublicKey | ||
/** | ||
* Generic private key interface | ||
*/ | ||
export interface PrivateKey<Type extends KeyType = 'Ed25519'> { | ||
readonly public: PublicKey<Type> | ||
readonly bytes: Uint8Array | ||
sign(data: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> | ||
marshal(): Uint8Array | ||
equals(key: PrivateKey<Type>): boolean | ||
hash(): Uint8Array | Promise<Uint8Array> | ||
interface PrivateKeyBase<KeyType extends string, PublicKeyType extends PublicKeyBase<KeyType>> { | ||
/** | ||
* 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. | ||
* The type of this key | ||
*/ | ||
id(): Promise<string> | ||
readonly type: KeyType | ||
/** | ||
* Exports the password protected key in the format specified. | ||
* The public key that corresponds to this private key | ||
*/ | ||
export(password: string, format?: 'pkcs-8' | string): Promise<string> | ||
readonly publicKey: PublicKeyType | ||
/** | ||
* The raw public key bytes (for Ed25519 and secp256k1 keys) or PKIX in ASN1 | ||
* DER format (for RSA keys) | ||
*/ | ||
readonly raw: Uint8Array | ||
/** | ||
* Returns `true` if the passed object matches this key | ||
*/ | ||
equals(key?: any): boolean | ||
/** | ||
* Sign the passed data with this private key and return the signature for | ||
* later verification | ||
*/ | ||
sign(data: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> | ||
} | ||
export const Ed25519 = 'Ed25519' | ||
export const RSA = 'RSA' | ||
export const secp256k1 = 'secp256k1' | ||
export type KeyType = typeof Ed25519 | typeof RSA | typeof secp256k1 | ||
export interface RSAPrivateKey extends PrivateKeyBase<'RSA', RSAPublicKey> {} | ||
export interface Ed25519PrivateKey extends PrivateKeyBase<'Ed25519', Ed25519PublicKey> {} | ||
export interface Secp256k1PrivateKey extends PrivateKeyBase<'secp256k1', Secp256k1PublicKey> {} | ||
export type PrivateKey = RSAPrivateKey | Ed25519PrivateKey | Secp256k1PrivateKey |
@@ -79,19 +79,19 @@ import type { MultiaddrConnection, Stream, Connection } from '../connection/index.js' | ||
*/ | ||
export interface MetricGroup { | ||
export interface MetricGroup<T extends string = any> { | ||
/** | ||
* Update the stored metric group to the passed value | ||
*/ | ||
update(values: Record<string, number>): void | ||
update(values: Partial<Record<T, number>>): void | ||
/** | ||
* Increment the metric group keys by the passed number or | ||
* any non-numeric value to increment by 1 | ||
* `true` to increment by 1 | ||
*/ | ||
increment(values: Record<string, number | unknown>): void | ||
increment(values: Partial<Record<T, number | true>>): void | ||
/** | ||
* Decrement the metric group keys by the passed number or | ||
* any non-numeric value to decrement by 1 | ||
* `true` to decrement by 1 | ||
*/ | ||
decrement(values: Record<string, number | unknown>): void | ||
decrement(values: Partial<Record<T, number | true>>): void | ||
@@ -132,3 +132,3 @@ /** | ||
*/ | ||
export interface CounterGroup { | ||
export interface CounterGroup<T extends string = any> { | ||
/** | ||
@@ -138,3 +138,3 @@ * Increment the metric group keys by the passed number or | ||
*/ | ||
increment(values: Record<string, number | unknown>): void | ||
increment(values: Partial<Record<T, number | true>>): void | ||
@@ -141,0 +141,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
import type { KeyType } from '../keys/index.js' | ||
import type { Ed25519PublicKey, KeyType, RSAPublicKey, Secp256k1PublicKey } from '../keys/index.js' | ||
import type { CID } from 'multiformats/cid' | ||
@@ -7,37 +7,158 @@ import type { MultihashDigest } from 'multiformats/hashes/interface' | ||
export interface RSAPeerId extends PeerId { | ||
/** | ||
* A PeerId generated from an RSA public key - it is a base58btc encoded sha-256 | ||
* hash of the public key. | ||
* | ||
* RSA public keys are too large to pass around freely, instead Ed25519 or | ||
* secp256k1 should be preferred as they can embed their public key in the | ||
* PeerId itself. | ||
* | ||
* @deprecated Ed25519 or secp256k1 keys are preferred to RSA | ||
*/ | ||
export interface RSAPeerId { | ||
readonly type: 'RSA' | ||
readonly publicKey?: Uint8Array | ||
/** | ||
* RSA public keys are too large to embed in the multihash commonly used to | ||
* refer to peers, so this will only be defined if the public key has | ||
* previously been found through a routing query or during normal protocol | ||
* operations | ||
*/ | ||
readonly publicKey?: RSAPublicKey | ||
/** | ||
* Returns the multihash from `toMultihash()` as a base58btc encoded string | ||
*/ | ||
toString(): string | ||
/** | ||
* Returns a multihash, the digest of which is the SHA2-256 hash of the public | ||
* key | ||
*/ | ||
toMultihash(): MultihashDigest<0x12> | ||
/** | ||
* Returns a CID with the libp2p key code and the same multihash as | ||
* `toMultihash()` | ||
*/ | ||
toCID(): CID<Uint8Array, 0x72, 0x12, 1> | ||
/** | ||
* Returns true if the passed argument is equivalent to this PeerId | ||
*/ | ||
equals(other?: any): boolean | ||
} | ||
export interface Ed25519PeerId extends PeerId { | ||
export interface Ed25519PeerId { | ||
readonly type: 'Ed25519' | ||
readonly publicKey: Uint8Array | ||
/** | ||
* This will always be defined as the public key is embedded in the multihash | ||
* of this PeerId | ||
*/ | ||
readonly publicKey: Ed25519PublicKey | ||
/** | ||
* Returns the multihash from `toMultihash()` as a base58btc encoded string | ||
*/ | ||
toString(): string | ||
/** | ||
* Returns a multihash, the digest of which is the protobuf-encoded public key | ||
* encoded as an identity hash | ||
*/ | ||
toMultihash(): MultihashDigest<0x0> | ||
/** | ||
* Returns a CID with the libp2p key code and the same multihash as | ||
* `toMultihash()` | ||
*/ | ||
toCID(): CID<Uint8Array, 0x72, 0x0, 1> | ||
/** | ||
* Returns true if the passed argument is equivalent to this PeerId | ||
*/ | ||
equals(other?: any): boolean | ||
} | ||
export interface Secp256k1PeerId extends PeerId { | ||
export interface Secp256k1PeerId { | ||
readonly type: 'secp256k1' | ||
readonly publicKey: Uint8Array | ||
/** | ||
* This will always be defined as the public key is embedded in the multihash | ||
* of this PeerId | ||
*/ | ||
readonly publicKey: Secp256k1PublicKey | ||
/** | ||
* Returns the multihash from `toMultihash()` as a base58btc encoded string | ||
*/ | ||
toString(): string | ||
/** | ||
* Returns a multihash, the digest of which is the protobuf-encoded public key | ||
* encoded as an identity hash | ||
*/ | ||
toMultihash(): MultihashDigest<0x0> | ||
/** | ||
* Returns a CID with the libp2p key code and the same multihash as | ||
* `toMultihash()` | ||
*/ | ||
toCID(): CID<Uint8Array, 0x72, 0x0, 1> | ||
/** | ||
* Returns true if the passed argument is equivalent to this PeerId | ||
*/ | ||
equals(other?: any): boolean | ||
} | ||
export interface URLPeerId extends PeerId { | ||
export interface URLPeerId { | ||
readonly type: 'url' | ||
} | ||
export interface PeerId { | ||
type: PeerIdType | ||
multihash: MultihashDigest | ||
privateKey?: Uint8Array | ||
publicKey?: Uint8Array | ||
/** | ||
* This will always be undefined as URL Peers do not have public keys | ||
*/ | ||
readonly publicKey: undefined | ||
/** | ||
* Returns CID from `toCID()` encoded as a base36 string | ||
*/ | ||
toString(): string | ||
toCID(): CID | ||
toBytes(): Uint8Array | ||
equals(other?: PeerId | Uint8Array | string): boolean | ||
/** | ||
* Returns a multihash, the digest of which is the URL encoded as an identity | ||
* hash | ||
*/ | ||
toMultihash(): MultihashDigest<0x0> | ||
/** | ||
* Returns a CID with the Transport IPFS Gateway HTTP code and the same | ||
* multihash as `toMultihash()` | ||
*/ | ||
toCID(): CID<Uint8Array, 0x0920, 0x0, 1> | ||
/** | ||
* Returns true if the passed argument is equivalent to this PeerId | ||
*/ | ||
equals(other?: any): boolean | ||
} | ||
/** | ||
* This is a union of all known PeerId types - use the `.type` field to | ||
* disambiguate them | ||
*/ | ||
export type PeerId = RSAPeerId | Ed25519PeerId | Secp256k1PeerId | URLPeerId | ||
/** | ||
* All PeerId implementations must use this symbol as the name of a property | ||
* with a boolean `true` value | ||
*/ | ||
export const peerIdSymbol = Symbol.for('@libp2p/peer-id') | ||
export function isPeerId (other: any): other is PeerId { | ||
return other != null && Boolean(other[peerIdSymbol]) | ||
/** | ||
* Returns true if the passed argument is a PeerId implementation | ||
*/ | ||
export function isPeerId (other?: any): other is PeerId { | ||
return Boolean(other?.[peerIdSymbol]) | ||
} |
@@ -0,1 +1,2 @@ | ||
import type { PublicKey } from '../keys/index.js' | ||
import type { PeerId } from '../peer-id/index.js' | ||
@@ -94,5 +95,7 @@ import type { Multiaddr } from '@multiformats/multiaddr' | ||
/** | ||
* If this Peer has an RSA key, it's public key can be set with this property | ||
* If this Peer has an RSA key, it's public key can be set with this property. | ||
* | ||
* The byte array should be the protobuf encoded form of the public key. | ||
*/ | ||
publicKey?: Uint8Array | ||
publicKey?: PublicKey | ||
@@ -99,0 +102,0 @@ /** |
import type { Stream } from '../connection/index.js' | ||
import type { TypedEventTarget } from '../event-target.js' | ||
import type { PublicKey } from '../keys/index.js' | ||
import type { PeerId } from '../peer-id/index.js' | ||
@@ -38,3 +39,3 @@ import type { Pushable } from 'it-pushable' | ||
signature: Uint8Array | ||
key: Uint8Array | ||
key: PublicKey | ||
} | ||
@@ -41,0 +42,0 @@ |
@@ -1,2 +0,2 @@ | ||
import type { PeerId } from '../peer-id/index.js' | ||
import type { PublicKey } from '../keys/index.js' | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
@@ -27,3 +27,3 @@ | ||
export interface Envelope { | ||
peerId: PeerId | ||
publicKey: PublicKey | ||
payloadType: Uint8Array | Uint8ArrayList | ||
@@ -30,0 +30,0 @@ payload: Uint8Array |
@@ -24,6 +24,6 @@ import type { Connection, Stream } from '../connection/index.js' | ||
/** | ||
* Opt-in to running over a transient connection - one that has time/data limits | ||
* placed on it. | ||
* Opt-in to running over connections with limits on how much data can be | ||
* transferred or how long it can be open for. | ||
*/ | ||
runOnTransientConnection?: boolean | ||
runOnLimitedConnection?: boolean | ||
} | ||
@@ -30,0 +30,0 @@ |
@@ -30,8 +30,9 @@ import type { Connection } from '../connection/index.js' | ||
/** | ||
* If true, invoke `onConnect` for this topology on transient (e.g. short-lived | ||
* and/or data-limited) connections | ||
* If true, invoke `onConnect` for this topology on limited connections, e.g. | ||
* ones with limits on how much data can be transferred or how long they can | ||
* be open for. | ||
* | ||
* @default false | ||
*/ | ||
notifyOnTransient?: boolean | ||
notifyOnLimitedConnection?: boolean | ||
@@ -38,0 +39,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
import type { Connection, MultiaddrConnection } from '../connection/index.js' | ||
import type { Connection, ConnectionLimits, MultiaddrConnection } from '../connection/index.js' | ||
import type { TypedEventTarget } from '../event-target.js' | ||
@@ -103,12 +103,7 @@ import type { AbortOptions } from '../index.js' | ||
export interface UpgraderOptions<ConnectionUpgradeEvents extends ProgressEvent = ProgressEvent> extends ProgressOptions<ConnectionUpgradeEvents> { | ||
export interface UpgraderOptions<ConnectionUpgradeEvents extends ProgressEvent = ProgressEvent> extends ProgressOptions<ConnectionUpgradeEvents>, AbortOptions { | ||
skipEncryption?: boolean | ||
skipProtection?: boolean | ||
muxerFactory?: StreamMuxerFactory | ||
/** | ||
* The passed MultiaddrConnection has limits place on duration and/or data | ||
* transfer amounts so is not expected to be open for very long. | ||
*/ | ||
transient?: boolean | ||
limits?: ConnectionLimits | ||
} | ||
@@ -115,0 +110,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
275742
6758
124
1