@multiformats/multiaddr
Version:
multiaddr implementation (binary + string representation of network addresses)
5 lines (4 loc) • 38.5 kB
JavaScript
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.MultiformatsMultiaddr = factory()}(typeof self !== 'undefined' ? self : this, function () {
;var MultiformatsMultiaddr=(()=>{var Ne=Object.defineProperty;var Rr=Object.getOwnPropertyDescriptor;var Mr=Object.getOwnPropertyNames;var Lr=Object.prototype.hasOwnProperty;var b=(r,t)=>{for(var e in t)Ne(r,e,{get:t[e],enumerable:!0})},kr=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Mr(t))!Lr.call(r,o)&&o!==e&&Ne(r,o,{get:()=>t[o],enumerable:!(n=Rr(t,o))||n.enumerable});return r};var Vr=r=>kr(Ne({},"__esModule",{value:!0}),r);var Co={};b(Co,{CODE_CERTHASH:()=>ir,CODE_DCCP:()=>Wt,CODE_DNS:()=>De,CODE_DNS4:()=>Se,CODE_DNS6:()=>ue,CODE_DNSADDR:()=>Te,CODE_GARLIC32:()=>Zt,CODE_GARLIC64:()=>Kt,CODE_HTTP:()=>sr,CODE_HTTPS:()=>cr,CODE_HTTP_PATH:()=>ar,CODE_IP4:()=>de,CODE_IP6:()=>Z,CODE_IP6ZONE:()=>Ie,CODE_IPCIDR:()=>jt,CODE_MEMORY:()=>gr,CODE_NOISE:()=>tr,CODE_ONION:()=>Jt,CODE_ONION3:()=>Qt,CODE_P2P:()=>Pe,CODE_P2P_CIRCUIT:()=>Ae,CODE_P2P_STARDUST:()=>ur,CODE_P2P_WEBRTC_DIRECT:()=>hr,CODE_P2P_WEBRTC_STAR:()=>fr,CODE_P2P_WEBSOCKET_STAR:()=>lr,CODE_QUIC:()=>rr,CODE_QUIC_V1:()=>nr,CODE_SCTP:()=>qt,CODE_SNI:()=>er,CODE_TCP:()=>pe,CODE_TLS:()=>Yt,CODE_UDP:()=>le,CODE_UDT:()=>Ht,CODE_UNIX:()=>Xt,CODE_UTP:()=>Gt,CODE_WEBRTC:()=>xr,CODE_WEBRTC_DIRECT:()=>mr,CODE_WEBTRANSPORT:()=>or,CODE_WS:()=>dr,CODE_WSS:()=>pr,MultiaddrFilter:()=>_e,V:()=>f,fromNodeAddress:()=>xo,fromStringTuples:()=>wo,fromTuples:()=>go,isMultiaddr:()=>Oe,isName:()=>bo,multiaddr:()=>R,protocols:()=>yo,registry:()=>x,resolvers:()=>mt});var Re={};b(Re,{base10:()=>Xr});var vo=new Uint8Array(0);function bt(r,t){if(r===t)return!0;if(r.byteLength!==t.byteLength)return!1;for(let e=0;e<r.byteLength;e++)if(r[e]!==t[e])return!1;return!0}function O(r){if(r instanceof Uint8Array&&r.constructor.name==="Uint8Array")return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);if(ArrayBuffer.isView(r))return new Uint8Array(r.buffer,r.byteOffset,r.byteLength);throw new Error("Unknown type, must be binary type")}function yt(r){return new TextEncoder().encode(r)}function Ct(r){return new TextDecoder().decode(r)}function $r(r,t){if(r.length>=255)throw new TypeError("Alphabet too long");for(var e=new Uint8Array(256),n=0;n<e.length;n++)e[n]=255;for(var o=0;o<r.length;o++){var i=r.charAt(o),s=i.charCodeAt(0);if(e[s]!==255)throw new TypeError(i+" is ambiguous");e[s]=o}var a=r.length,c=r.charAt(0),d=Math.log(a)/Math.log(256),h=Math.log(256)/Math.log(a);function P(p){if(p instanceof Uint8Array||(ArrayBuffer.isView(p)?p=new Uint8Array(p.buffer,p.byteOffset,p.byteLength):Array.isArray(p)&&(p=Uint8Array.from(p))),!(p instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(p.length===0)return"";for(var w=0,L=0,v=0,D=p.length;v!==D&&p[v]===0;)v++,w++;for(var S=(D-v)*h+1>>>0,C=new Uint8Array(S);v!==D;){for(var T=p[v],N=0,I=S-1;(T!==0||N<L)&&I!==-1;I--,N++)T+=256*C[I]>>>0,C[I]=T%a>>>0,T=T/a>>>0;if(T!==0)throw new Error("Non-zero carry");L=N,v++}for(var A=S-L;A!==S&&C[A]===0;)A++;for(var xe=c.repeat(w);A<S;++A)xe+=r.charAt(C[A]);return xe}function M(p){if(typeof p!="string")throw new TypeError("Expected String");if(p.length===0)return new Uint8Array;var w=0;if(p[w]!==" "){for(var L=0,v=0;p[w]===c;)L++,w++;for(var D=(p.length-w)*d+1>>>0,S=new Uint8Array(D);p[w];){var C=e[p.charCodeAt(w)];if(C===255)return;for(var T=0,N=D-1;(C!==0||T<v)&&N!==-1;N--,T++)C+=a*S[N]>>>0,S[N]=C%256>>>0,C=C/256>>>0;if(C!==0)throw new Error("Non-zero carry");v=T,w++}if(p[w]!==" "){for(var I=D-v;I!==D&&S[I]===0;)I++;for(var A=new Uint8Array(L+(D-I)),xe=L;I!==D;)A[xe++]=S[I++];return A}}}function Fr(p){var w=M(p);if(w)return w;throw new Error(`Non-${t} character`)}return{encode:P,decodeUnsafe:M,decode:Fr}}var Wr=$r,jr=Wr,vt=jr;var Ue=class{name;prefix;baseEncode;constructor(t,e,n){this.name=t,this.prefix=e,this.baseEncode=n}encode(t){if(t instanceof Uint8Array)return`${this.prefix}${this.baseEncode(t)}`;throw Error("Unknown type, must be binary type")}},Be=class{name;prefix;baseDecode;prefixCodePoint;constructor(t,e,n){this.name=t,this.prefix=e;let o=e.codePointAt(0);if(o===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=o,this.baseDecode=n}decode(t){if(typeof t=="string"){if(t.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(t)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(t.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(t){return It(this,t)}},ze=class{decoders;constructor(t){this.decoders=t}or(t){return It(this,t)}decode(t){let e=t[0],n=this.decoders[e];if(n!=null)return n.decode(t);throw RangeError(`Unable to decode multibase string ${JSON.stringify(t)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}};function It(r,t){return new ze({...r.decoders??{[r.prefix]:r},...t.decoders??{[t.prefix]:t}})}var Fe=class{name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(t,e,n,o){this.name=t,this.prefix=e,this.baseEncode=n,this.baseDecode=o,this.encoder=new Ue(t,e,n),this.decoder=new Be(t,e,o)}encode(t){return this.encoder.encode(t)}decode(t){return this.decoder.decode(t)}};function q({name:r,prefix:t,encode:e,decode:n}){return new Fe(r,t,e,n)}function U({name:r,prefix:t,alphabet:e}){let{encode:n,decode:o}=vt(e,r);return q({prefix:t,name:r,encode:n,decode:i=>O(o(i))})}function qr(r,t,e,n){let o=r.length;for(;r[o-1]==="=";)--o;let i=new Uint8Array(o*e/8|0),s=0,a=0,c=0;for(let d=0;d<o;++d){let h=t[r[d]];if(h===void 0)throw new SyntaxError(`Non-${n} character`);a=a<<e|h,s+=e,s>=8&&(s-=8,i[c++]=255&a>>s)}if(s>=e||(255&a<<8-s)!==0)throw new SyntaxError("Unexpected end of data");return i}function Hr(r,t,e){let n=t[t.length-1]==="=",o=(1<<e)-1,i="",s=0,a=0;for(let c=0;c<r.length;++c)for(a=a<<8|r[c],s+=8;s>e;)s-=e,i+=t[o&a>>s];if(s!==0&&(i+=t[o&a<<e-s]),n)for(;(i.length*e&7)!==0;)i+="=";return i}function Gr(r){let t={};for(let e=0;e<r.length;++e)t[r[e]]=e;return t}function u({name:r,prefix:t,bitsPerChar:e,alphabet:n}){let o=Gr(n);return q({prefix:t,name:r,encode(i){return Hr(i,n,e)},decode(i){return qr(i,o,e,r)}})}var Xr=U({prefix:"9",name:"base10",alphabet:"0123456789"});var Me={};b(Me,{base16:()=>Jr,base16upper:()=>Qr});var Jr=u({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),Qr=u({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var Le={};b(Le,{base2:()=>Kr});var Kr=u({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var ke={};b(ke,{base256emoji:()=>rn});var Dt=Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}"),Zr=Dt.reduce((r,t,e)=>(r[e]=t,r),[]),Yr=Dt.reduce((r,t,e)=>{let n=t.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${t}`);return r[n]=e,r},[]);function en(r){return r.reduce((t,e)=>(t+=Zr[e],t),"")}function tn(r){let t=[];for(let e of r){let n=e.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${e}`);let o=Yr[n];if(o==null)throw new Error(`Non-base256emoji character: ${e}`);t.push(o)}return new Uint8Array(t)}var rn=q({prefix:"\u{1F680}",name:"base256emoji",encode:en,decode:tn});var Ve={};b(Ve,{base32:()=>B,base32hex:()=>an,base32hexpad:()=>dn,base32hexpadupper:()=>pn,base32hexupper:()=>cn,base32pad:()=>on,base32padupper:()=>sn,base32upper:()=>nn,base32z:()=>ln});var B=u({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),nn=u({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),on=u({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),sn=u({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),an=u({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),cn=u({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),dn=u({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),pn=u({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),ln=u({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var $e={};b($e,{base36:()=>te,base36upper:()=>un});var te=U({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),un=U({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var We={};b(We,{base58btc:()=>E,base58flickr:()=>fn});var E=U({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),fn=U({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var qe={};b(qe,{base64:()=>hn,base64pad:()=>mn,base64url:()=>je,base64urlpad:()=>xn});var hn=u({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),mn=u({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),je=u({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),xn=u({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var He={};b(He,{base8:()=>gn});var gn=u({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var Ge={};b(Ge,{identity:()=>wn});var wn=q({prefix:"\0",name:"identity",encode:r=>Ct(r),decode:r=>yt(r)});var Lo=new TextEncoder,ko=new TextDecoder;var Je={};b(Je,{identity:()=>Vn});var Cn=Pt,St=128,En=127,vn=~En,In=Math.pow(2,31);function Pt(r,t,e){t=t||[],e=e||0;for(var n=e;r>=In;)t[e++]=r&255|St,r/=128;for(;r&vn;)t[e++]=r&255|St,r>>>=7;return t[e]=r|0,Pt.bytes=e-n+1,t}var Dn=Xe,Sn=128,Tt=127;function Xe(r,n){var e=0,n=n||0,o=0,i=n,s,a=r.length;do{if(i>=a)throw Xe.bytes=0,new RangeError("Could not decode varint");s=r[i++],e+=o<28?(s&Tt)<<o:(s&Tt)*Math.pow(2,o),o+=7}while(s>=Sn);return Xe.bytes=i-n,e}var Tn=Math.pow(2,7),Pn=Math.pow(2,14),An=Math.pow(2,21),On=Math.pow(2,28),_n=Math.pow(2,35),Nn=Math.pow(2,42),Un=Math.pow(2,49),Bn=Math.pow(2,56),zn=Math.pow(2,63),Fn=function(r){return r<Tn?1:r<Pn?2:r<An?3:r<On?4:r<_n?5:r<Nn?6:r<Un?7:r<Bn?8:r<zn?9:10},Rn={encode:Cn,decode:Dn,encodingLength:Fn},Mn=Rn,re=Mn;function ne(r,t=0){return[re.decode(r,t),re.decode.bytes]}function H(r,t,e=0){return re.encode(r,t,e),t}function G(r){return re.encodingLength(r)}function k(r,t){let e=t.byteLength,n=G(r),o=n+G(e),i=new Uint8Array(o+e);return H(r,i,0),H(e,i,n),i.set(t,o),new X(r,e,t,i)}function At(r){let t=O(r),[e,n]=ne(t),[o,i]=ne(t.subarray(n)),s=t.subarray(n+i);if(s.byteLength!==o)throw new Error("Incorrect length");return new X(e,o,s,t)}function Ot(r,t){if(r===t)return!0;{let e=t;return r.code===e.code&&r.size===e.size&&e.bytes instanceof Uint8Array&&bt(r.bytes,e.bytes)}}var X=class{code;size;digest;bytes;constructor(t,e,n,o){this.code=t,this.size=e,this.digest=n,this.bytes=o}};var _t=0,Ln="identity",Nt=O;function kn(r){return k(_t,Nt(r))}var Vn={code:_t,name:Ln,encode:Nt,digest:kn};var Ze={};b(Ze,{sha256:()=>$n,sha512:()=>Wn});function Ke({name:r,code:t,encode:e}){return new Qe(r,t,e)}var Qe=class{name;code;encode;constructor(t,e,n){this.name=t,this.code=e,this.encode=n}digest(t){if(t instanceof Uint8Array){let e=this.encode(t);return e instanceof Uint8Array?k(this.code,e):e.then(n=>k(this.code,n))}else throw Error("Unknown type, must be binary type")}};function Bt(r){return async t=>new Uint8Array(await crypto.subtle.digest(r,t))}var $n=Ke({name:"sha2-256",code:18,encode:Bt("SHA-256")}),Wn=Ke({name:"sha2-512",code:19,encode:Bt("SHA-512")});function zt(r,t){let{bytes:e,version:n}=r;switch(n){case 0:return qn(e,Ye(r),t??E.encoder);default:return Hn(e,Ye(r),t??B.encoder)}}var Ft=new WeakMap;function Ye(r){let t=Ft.get(r);if(t==null){let e=new Map;return Ft.set(r,e),e}return t}var z=class r{code;version;multihash;bytes;"/";constructor(t,e,n,o){this.code=e,this.version=t,this.multihash=n,this.bytes=o,this["/"]=o}get asCID(){return this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{let{code:t,multihash:e}=this;if(t!==ie)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(e.code!==Gn)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return r.createV0(e)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{let{code:t,digest:e}=this.multihash,n=k(t,e);return r.createV1(this.code,n)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(t){return r.equals(this,t)}static equals(t,e){let n=e;return n!=null&&t.code===n.code&&t.version===n.version&&Ot(t.multihash,n.multihash)}toString(t){return zt(this,t)}toJSON(){return{"/":zt(this)}}link(){return this}[Symbol.toStringTag]="CID";[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(t){if(t==null)return null;let e=t;if(e instanceof r)return e;if(e["/"]!=null&&e["/"]===e.bytes||e.asCID===e){let{version:n,code:o,multihash:i,bytes:s}=e;return new r(n,o,i,s??Rt(n,o,i.bytes))}else if(e[Xn]===!0){let{version:n,multihash:o,code:i}=e,s=At(o);return r.create(n,i,s)}else return null}static create(t,e,n){if(typeof e!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(t){case 0:{if(e!==ie)throw new Error(`Version 0 CID must use dag-pb (code: ${ie}) block encoding`);return new r(t,e,n,n.bytes)}case 1:{let o=Rt(t,e,n.bytes);return new r(t,e,n,o)}default:throw new Error("Invalid version")}}static createV0(t){return r.create(0,ie,t)}static createV1(t,e){return r.create(1,t,e)}static decode(t){let[e,n]=r.decodeFirst(t);if(n.length!==0)throw new Error("Incorrect length");return e}static decodeFirst(t){let e=r.inspectBytes(t),n=e.size-e.multihashSize,o=O(t.subarray(n,n+e.multihashSize));if(o.byteLength!==e.multihashSize)throw new Error("Incorrect length");let i=o.subarray(e.multihashSize-e.digestSize),s=new X(e.multihashCode,e.digestSize,i,o);return[e.version===0?r.createV0(s):r.createV1(e.codec,s),t.subarray(e.size)]}static inspectBytes(t){let e=0,n=()=>{let[P,M]=ne(t.subarray(e));return e+=M,P},o=n(),i=ie;if(o===18?(o=0,e=0):i=n(),o!==0&&o!==1)throw new RangeError(`Invalid CID version ${o}`);let s=e,a=n(),c=n(),d=e+c,h=d-s;return{version:o,codec:i,multihashCode:a,digestSize:c,multihashSize:h,size:d}}static parse(t,e){let[n,o]=jn(t,e),i=r.decode(o);if(i.version===0&&t[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return Ye(i).set(n,t),i}};function jn(r,t){switch(r[0]){case"Q":{let e=t??E;return[E.prefix,e.decode(`${E.prefix}${r}`)]}case E.prefix:{let e=t??E;return[E.prefix,e.decode(r)]}case B.prefix:{let e=t??B;return[B.prefix,e.decode(r)]}case te.prefix:{let e=t??te;return[te.prefix,e.decode(r)]}default:{if(t==null)throw Error("To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided");return[r[0],t.decode(r)]}}}function qn(r,t,e){let{prefix:n}=e;if(n!==E.prefix)throw Error(`Cannot string encode V0 in ${e.name} encoding`);let o=t.get(n);if(o==null){let i=e.encode(r).slice(1);return t.set(n,i),i}else return o}function Hn(r,t,e){let{prefix:n}=e,o=t.get(n);if(o==null){let i=e.encode(r);return t.set(n,i),i}else return o}var ie=112,Gn=18;function Rt(r,t,e){let n=G(r),o=n+G(t),i=new Uint8Array(o+e.byteLength);return H(r,i,0),H(t,i,n),i.set(e,o),i}var Xn=Symbol.for("@ipld/js-cid/CID");var se={...Ge,...Le,...He,...Re,...Me,...Ve,...$e,...We,...qe,...ke},ci={...Ze,...Je};function ae(r=0){return new Uint8Array(r)}function Lt(r,t,e,n){return{name:r,prefix:t,encoder:{name:r,prefix:t,encode:e},decoder:{decode:n}}}var Mt=Lt("utf8","u",r=>"u"+new TextDecoder("utf8").decode(r),r=>new TextEncoder().encode(r.substring(1))),et=Lt("ascii","a",r=>{let t="a";for(let e=0;e<r.length;e++)t+=String.fromCharCode(r[e]);return t},r=>{r=r.substring(1);let t=ae(r.length);for(let e=0;e<r.length;e++)t[e]=r.charCodeAt(e);return t}),Jn={utf8:Mt,"utf-8":Mt,hex:se.base16,latin1:et,ascii:et,binary:et,...se},we=Jn;function y(r,t="utf8"){let e=we[t];if(e==null)throw new Error(`Unsupported encoding "${t}"`);return e.encoder.encode(r).substring(1)}var m=class extends Error{static name="InvalidMultiaddrError";name="InvalidMultiaddrError"},_=class extends Error{static name="ValidationError";name="ValidationError"},V=class extends Error{static name="InvalidParametersError";name="InvalidParametersError"},be=class extends Error{static name="UnknownProtocolError";name="UnknownProtocolError"};function kt(r,t){if(r===t)return!0;if(r.byteLength!==t.byteLength)return!1;for(let e=0;e<r.byteLength;e++)if(r[e]!==t[e])return!1;return!0}function F(r,t="utf8"){let e=we[t];if(e==null)throw new Error(`Unsupported encoding "${t}"`);return e.decoder.decode(`${e.prefix}${r}`)}var Qn=Math.pow(2,7),Kn=Math.pow(2,14),Zn=Math.pow(2,21),tt=Math.pow(2,28),rt=Math.pow(2,35),nt=Math.pow(2,42),ot=Math.pow(2,49),l=128,g=127;function J(r){if(r<Qn)return 1;if(r<Kn)return 2;if(r<Zn)return 3;if(r<tt)return 4;if(r<rt)return 5;if(r<nt)return 6;if(r<ot)return 7;if(Number.MAX_SAFE_INTEGER!=null&&r>Number.MAX_SAFE_INTEGER)throw new RangeError("Could not encode varint");return 8}function it(r,t,e=0){switch(J(r)){case 8:t[e++]=r&255|l,r/=128;case 7:t[e++]=r&255|l,r/=128;case 6:t[e++]=r&255|l,r/=128;case 5:t[e++]=r&255|l,r/=128;case 4:t[e++]=r&255|l,r>>>=7;case 3:t[e++]=r&255|l,r>>>=7;case 2:t[e++]=r&255|l,r>>>=7;case 1:{t[e++]=r&255,r>>>=7;break}default:throw new Error("unreachable")}return t}function Yn(r,t){let e=r[t],n=0;if(n+=e&g,e<l||(e=r[t+1],n+=(e&g)<<7,e<l)||(e=r[t+2],n+=(e&g)<<14,e<l)||(e=r[t+3],n+=(e&g)<<21,e<l)||(e=r[t+4],n+=(e&g)*tt,e<l)||(e=r[t+5],n+=(e&g)*rt,e<l)||(e=r[t+6],n+=(e&g)*nt,e<l)||(e=r[t+7],n+=(e&g)*ot,e<l))return n;throw new RangeError("Could not decode varint")}function eo(r,t){let e=r.get(t),n=0;if(n+=e&g,e<l||(e=r.get(t+1),n+=(e&g)<<7,e<l)||(e=r.get(t+2),n+=(e&g)<<14,e<l)||(e=r.get(t+3),n+=(e&g)<<21,e<l)||(e=r.get(t+4),n+=(e&g)*tt,e<l)||(e=r.get(t+5),n+=(e&g)*rt,e<l)||(e=r.get(t+6),n+=(e&g)*nt,e<l)||(e=r.get(t+7),n+=(e&g)*ot,e<l))return n;throw new RangeError("Could not decode varint")}function st(r,t=0){return r instanceof Uint8Array?Yn(r,t):eo(r,t)}function ce(r,t){t==null&&(t=r.reduce((o,i)=>o+i.length,0));let e=ae(t),n=0;for(let o of r)e.set(o,n),n+=o.length;return e}var ye=class{index=0;input="";new(t){return this.index=0,this.input=t,this}readAtomically(t){let e=this.index,n=t();return n===void 0&&(this.index=e),n}parseWith(t){let e=t();if(this.index===this.input.length)return e}peekChar(){if(!(this.index>=this.input.length))return this.input[this.index]}readChar(){if(!(this.index>=this.input.length))return this.input[this.index++]}readGivenChar(t){return this.readAtomically(()=>{let e=this.readChar();if(e===t)return e})}readSeparator(t,e,n){return this.readAtomically(()=>{if(!(e>0&&this.readGivenChar(t)===void 0))return n()})}readNumber(t,e,n,o){return this.readAtomically(()=>{let i=0,s=0,a=this.peekChar();if(a===void 0)return;let c=a==="0",d=2**(8*o)-1;for(;;){let h=this.readAtomically(()=>{let P=this.readChar();if(P===void 0)return;let M=Number.parseInt(P,t);if(!Number.isNaN(M))return M});if(h===void 0)break;if(i*=t,i+=h,i>d||(s+=1,e!==void 0&&s>e))return}if(s!==0)return!n&&c&&s>1?void 0:i})}readIPv4Addr(){return this.readAtomically(()=>{let t=new Uint8Array(4);for(let e=0;e<t.length;e++){let n=this.readSeparator(".",e,()=>this.readNumber(10,3,!1,1));if(n===void 0)return;t[e]=n}return t})}readIPv6Addr(){let t=e=>{for(let n=0;n<e.length/2;n++){let o=n*2;if(n<e.length-3){let s=this.readSeparator(":",n,()=>this.readIPv4Addr());if(s!==void 0)return e[o]=s[0],e[o+1]=s[1],e[o+2]=s[2],e[o+3]=s[3],[o+4,!0]}let i=this.readSeparator(":",n,()=>this.readNumber(16,4,!0,2));if(i===void 0)return[o,!1];e[o]=i>>8,e[o+1]=i&255}return[e.length,!1]};return this.readAtomically(()=>{let e=new Uint8Array(16),[n,o]=t(e);if(n===16)return e;if(o||this.readGivenChar(":")===void 0||this.readGivenChar(":")===void 0)return;let i=new Uint8Array(14),s=16-(n+2),[a]=t(i.subarray(0,s));return e.set(i.subarray(0,a),16-a),e})}readIPAddr(){return this.readIPv4Addr()??this.readIPv6Addr()}};var Vt=45,ro=15,Q=new ye;function Ce(r){if(!(r.length>ro))return Q.new(r).parseWith(()=>Q.readIPv4Addr())}function Ee(r){if(r.includes("%")&&(r=r.split("%")[0]),!(r.length>Vt))return Q.new(r).parseWith(()=>Q.readIPv6Addr())}function K(r,t=!1){if(r.includes("%")&&(r=r.split("%")[0]),r.length>Vt)return;let e=Q.new(r).parseWith(()=>Q.readIPAddr());if(e)return t&&e.length===4?Uint8Array.from([0,0,0,0,0,0,0,0,0,0,255,255,e[0],e[1],e[2],e[3]]):e}function ve(r){return!!Ce(r)}function $t(r){return!!Ee(r)}var de=4,pe=6,le=273,Wt=33,Z=41,Ie=42,jt=43,De=53,Se=54,ue=55,Te=56,qt=132,Ht=301,Gt=302,Xt=400,Pe=421,Jt=444,Qt=445,Kt=446,Zt=447,Yt=448,er=449,tr=454,rr=460,nr=461,or=465,ir=466,sr=480,ar=481,cr=443,dr=477,pr=478,lr=479,ur=277,fr=275,hr=276,mr=280,xr=281,Ae=290,gr=777;function ct(r){return t=>y(t,r)}function dt(r){return t=>F(t,r)}function Y(r){return new DataView(r.buffer).getUint16(r.byteOffset).toString()}function $(r){let t=new ArrayBuffer(2);return new DataView(t).setUint16(0,typeof r=="string"?parseInt(r):r),new Uint8Array(t)}function wr(r){let t=r.split(":");if(t.length!==2)throw new Error(`failed to parse onion addr: ["'${t.join('", "')}'"]' does not contain a port number`);if(t[0].length!==16)throw new Error(`failed to parse onion addr: ${t[0]} not a Tor onion address.`);let e=F(t[0],"base32"),n=parseInt(t[1],10);if(n<1||n>65536)throw new Error("Port number is not in range(1, 65536)");let o=$(n);return ce([e,o],e.length+o.length)}function br(r){let t=r.split(":");if(t.length!==2)throw new Error(`failed to parse onion addr: ["'${t.join('", "')}'"]' does not contain a port number`);if(t[0].length!==56)throw new Error(`failed to parse onion addr: ${t[0]} not a Tor onion3 address.`);let e=B.decode(`b${t[0]}`),n=parseInt(t[1],10);if(n<1||n>65536)throw new Error("Port number is not in range(1, 65536)");let o=$(n);return ce([e,o],e.length+o.length)}function pt(r){let t=r.subarray(0,r.length-2),e=r.subarray(r.length-2),n=y(t,"base32"),o=Y(e);return`${n}:${o}`}var lt=function(r){r=r.toString().trim();let t=new Uint8Array(4);return r.split(/\./g).forEach((e,n)=>{let o=parseInt(e,10);if(isNaN(o)||o<0||o>255)throw new m("Invalid byte value in IP address");t[n]=o}),t},yr=function(r){let t=0;r=r.toString().trim();let e=r.split(":",8),n;for(n=0;n<e.length;n++){let i=ve(e[n]),s;i&&(s=lt(e[n]),e[n]=y(s.subarray(0,2),"base16")),s!=null&&++n<8&&e.splice(n,0,y(s.subarray(2,4),"base16"))}if(e[0]==="")for(;e.length<8;)e.unshift("0");else if(e[e.length-1]==="")for(;e.length<8;)e.push("0");else if(e.length<8){for(n=0;n<e.length&&e[n]!=="";n++);let i=[n,1];for(n=9-e.length;n>0;n--)i.push("0");e.splice.apply(e,i)}let o=new Uint8Array(t+16);for(n=0;n<e.length;n++){e[n]===""&&(e[n]="0");let i=parseInt(e[n],16);if(isNaN(i)||i<0||i>65535)throw new m("Invalid byte value in IP address");o[t++]=i>>8&255,o[t++]=i&255}return o},Cr=function(r){if(r.byteLength!==4)throw new m("IPv4 address was incorrect length");let t=[];for(let e=0;e<r.byteLength;e++)t.push(r[e]);return t.join(".")},Er=function(r){if(r.byteLength!==16)throw new m("IPv6 address was incorrect length");let t=[];for(let n=0;n<r.byteLength;n+=2){let o=r[n],i=r[n+1],s=`${o.toString(16).padStart(2,"0")}${i.toString(16).padStart(2,"0")}`;t.push(s)}let e=t.join(":");try{let n=new URL(`http://[${e}]`);return n.hostname.substring(1,n.hostname.length-1)}catch{throw new m(`Invalid IPv6 address "${e}"`)}};function vr(r){try{let t=new URL(`http://[${r}]`);return t.hostname.substring(1,t.hostname.length-1)}catch{throw new m(`Invalid IPv6 address "${r}"`)}}var at=Object.values(se).map(r=>r.decoder),no=function(){let r=at[0].or(at[1]);return at.slice(2).forEach(t=>r=r.or(t)),r}();function Ir(r){return no.decode(r)}function Dr(r){return t=>r.encoder.encode(t)}function oo(r){if(parseInt(r).toString()!==r)throw new _("Value must be an integer")}function io(r){if(r<0)throw new _("Value must be a positive integer, or zero")}function so(r){return t=>{if(t>r)throw new _(`Value must be smaller than or equal to ${r}`)}}function ao(...r){return t=>{for(let e of r)e(t)}}var fe=ao(oo,io,so(65535));var f=-1,ut=class{protocolsByCode=new Map;protocolsByName=new Map;getProtocol(t){let e;if(typeof t=="string"?e=this.protocolsByName.get(t):e=this.protocolsByCode.get(t),e==null)throw new be(`Protocol ${t} was unknown`);return e}addProtocol(t){this.protocolsByCode.set(t.code,t),this.protocolsByName.set(t.name,t),t.aliases?.forEach(e=>{this.protocolsByName.set(e,t)})}removeProtocol(t){let e=this.protocolsByCode.get(t);e!=null&&(this.protocolsByCode.delete(e.code),this.protocolsByName.delete(e.name),e.aliases?.forEach(n=>{this.protocolsByName.delete(n)}))}},x=new ut,co=[{code:4,name:"ip4",size:32,valueToBytes:lt,bytesToValue:Cr,validate:r=>{if(!ve(r))throw new _(`Invalid IPv4 address "${r}"`)}},{code:6,name:"tcp",size:16,valueToBytes:$,bytesToValue:Y,validate:fe},{code:273,name:"udp",size:16,valueToBytes:$,bytesToValue:Y,validate:fe},{code:33,name:"dccp",size:16,valueToBytes:$,bytesToValue:Y,validate:fe},{code:41,name:"ip6",size:128,valueToBytes:yr,bytesToValue:Er,stringToValue:vr,validate:r=>{if(!$t(r))throw new _(`Invalid IPv6 address "${r}"`)}},{code:42,name:"ip6zone",size:f},{code:43,name:"ipcidr",size:8,bytesToValue:ct("base10"),valueToBytes:dt("base10")},{code:53,name:"dns",size:f,resolvable:!0},{code:54,name:"dns4",size:f,resolvable:!0},{code:55,name:"dns6",size:f,resolvable:!0},{code:56,name:"dnsaddr",size:f,resolvable:!0},{code:132,name:"sctp",size:16,valueToBytes:$,bytesToValue:Y,validate:fe},{code:301,name:"udt"},{code:302,name:"utp"},{code:400,name:"unix",size:f,path:!0,stringToValue:r=>decodeURIComponent(r),valueToString:r=>encodeURIComponent(r)},{code:421,name:"p2p",aliases:["ipfs"],size:f,bytesToValue:ct("base58btc"),valueToBytes:r=>r.startsWith("Q")||r.startsWith("1")?dt("base58btc")(r):z.parse(r).multihash.bytes},{code:444,name:"onion",size:96,bytesToValue:pt,valueToBytes:wr},{code:445,name:"onion3",size:296,bytesToValue:pt,valueToBytes:br},{code:446,name:"garlic64",size:f},{code:447,name:"garlic32",size:f},{code:448,name:"tls"},{code:449,name:"sni",size:f},{code:454,name:"noise"},{code:460,name:"quic"},{code:461,name:"quic-v1"},{code:465,name:"webtransport"},{code:466,name:"certhash",size:f,bytesToValue:Dr(je),valueToBytes:Ir},{code:480,name:"http"},{code:481,name:"http-path",size:f,stringToValue:r=>`/${decodeURIComponent(r)}`,valueToString:r=>encodeURIComponent(r.substring(1))},{code:443,name:"https"},{code:477,name:"ws"},{code:478,name:"wss"},{code:479,name:"p2p-websocket-star"},{code:277,name:"p2p-stardust"},{code:275,name:"p2p-webrtc-star"},{code:276,name:"p2p-webrtc-direct"},{code:280,name:"webrtc-direct"},{code:281,name:"webrtc"},{code:290,name:"p2p-circuit"},{code:777,name:"memory",size:f}];co.forEach(r=>{x.addProtocol(r)});function Sr(r){let t=[],e=0;for(;e<r.length;){let n=st(r,e),o=x.getProtocol(n),i=J(n),s=po(o,r,e+i),a=0;s>0&&o.size===f&&(a=J(s));let c=i+a+s,d={code:n,name:o.name,bytes:r.subarray(e,e+c)};if(s>0){let h=e+i+a,P=r.subarray(h,h+s);d.value=o.bytesToValue?.(P)??y(P)}t.push(d),e+=c}return t}function Tr(r){let t=0,e=[];for(let n of r){if(n.bytes==null){let o=x.getProtocol(n.code),i=J(n.code),s,a=0,c=0;n.value!=null&&(s=o.valueToBytes?.(n.value)??F(n.value),a=s.byteLength,o.size===f&&(c=J(a)));let d=new Uint8Array(i+c+a),h=0;it(n.code,d,h),h+=i,s!=null&&(o.size===f&&(it(a,d,h),h+=c),d.set(s,h)),n.bytes=d}e.push(n.bytes),t+=n.bytes.byteLength}return ce(e,t)}function Pr(r){if(r.charAt(0)!=="/")throw new m('String multiaddr must start with "/"');let t=[],e="protocol",n="",o="";for(let i=1;i<r.length;i++){let s=r.charAt(i);s!=="/"&&(e==="protocol"?o+=r.charAt(i):n+=r.charAt(i));let a=i===r.length-1;if(s==="/"||a){let c=x.getProtocol(o);if(e==="protocol"){if(c.size==null||c.size===0){t.push({code:c.code,name:c.name}),n="",o="",e="protocol";continue}else if(a)throw new m(`Component ${o} was missing value`);e="value"}else if(e==="value"){let d={code:c.code,name:c.name};if(c.size!=null&&c.size!==0){if(n==="")throw new m(`Component ${o} was missing value`);d.value=c.stringToValue?.(n)??n}t.push(d),n="",o="",e="protocol"}}}if(o!==""&&n!=="")throw new m("Incomplete multiaddr");return t}function Ar(r){return`/${r.flatMap(t=>{if(t.value==null)return t.name;let e=x.getProtocol(t.code);if(e==null)throw new m(`Unknown protocol code ${t.code}`);return[t.name,e.valueToString?.(t.value)??t.value]}).join("/")}`}function po(r,t,e){return r.size==null||r.size===0?0:r.size>0?r.size/8:st(t,e)}var lo=Symbol.for("nodejs.util.inspect.custom"),ht=Symbol.for("@multiformats/multiaddr"),uo=[53,54,55,56],ft=class extends Error{constructor(t="No available resolver"){super(t),this.name="NoAvailableResolverError"}};function fo(r){if(r==null&&(r="/"),Oe(r))return r.getComponents();if(r instanceof Uint8Array)return Sr(r);if(typeof r=="string")return r=r.replace(/\/(\/)+/,"/").replace(/(\/)+$/,""),r===""&&(r="/"),Pr(r);if(Array.isArray(r))return r;throw new m("Must be a string, Uint8Array, Component[], or another Multiaddr")}var he=class r{[ht]=!0;#e;#t;#r;constructor(t="/",e={}){this.#e=fo(t),e.validate!==!1&&ho(this)}get bytes(){return this.#r==null&&(this.#r=Tr(this.#e)),this.#r}toString(){return this.#t==null&&(this.#t=Ar(this.#e)),this.#t}toJSON(){return this.toString()}toOptions(){let t,e,n,o,i="";for(let{code:a,name:c,value:d}of this.#e)a===42&&(i=`%${d??""}`),uo.includes(a)&&(e="tcp",o=443,n=`${d??""}${i}`,t=a===55?6:4),(a===6||a===273)&&(e=c==="tcp"?"tcp":"udp",o=parseInt(d??"")),(a===4||a===41)&&(e="tcp",n=`${d??""}${i}`,t=a===41?6:4);if(t==null||e==null||n==null||o==null)throw new Error('multiaddr must have a valid format: "/{ip4, ip6, dns4, dns6, dnsaddr}/{address}/{tcp, udp}/{port}".');return{family:t,host:n,transport:e,port:o}}getComponents(){return[...this.#e]}protos(){return this.#e.map(({code:t,value:e})=>{let n=x.getProtocol(t);return{code:t,size:n.size??0,name:n.name,resolvable:!!n.resolvable,path:!!n.path}})}protoCodes(){return this.#e.map(({code:t})=>t)}protoNames(){return this.#e.map(({name:t})=>t)}tuples(){return this.#e.map(({code:t,value:e})=>{if(e==null)return[t];let n=x.getProtocol(t),o=[t];return e!=null&&o.push(n.valueToBytes?.(e)??F(e)),o})}stringTuples(){return this.#e.map(({code:t,value:e})=>e==null?[t]:[t,e])}encapsulate(t){let e=new r(t);return new r([...this.#e,...e.getComponents()],{validate:!1})}decapsulate(t){let e=t.toString(),n=this.toString(),o=n.lastIndexOf(e);if(o<0)throw new V(`Address ${this.toString()} does not contain subaddress: ${t.toString()}`);return new r(n.slice(0,o),{validate:!1})}decapsulateCode(t){let e;for(let n=this.#e.length-1;n>-1;n--)if(this.#e[n].code===t){e=n;break}return new r(this.#e.slice(0,e),{validate:!1})}getPeerId(){try{let t=[];this.#e.forEach(({code:n,value:o})=>{n===421&&t.push([n,o]),n===290&&(t=[])});let e=t.pop();if(e?.[1]!=null){let n=e[1];return n[0]==="Q"||n[0]==="1"?y(E.decode(`z${n}`),"base58btc"):y(z.parse(n).multihash.bytes,"base58btc")}return null}catch{return null}}getPath(){for(let t of this.#e)if(x.getProtocol(t.code).path)return t.value??null;return null}equals(t){return kt(this.bytes,t.bytes)}async resolve(t){let e=this.protos().find(i=>i.resolvable);if(e==null)return[this];let n=mt.get(e.name);if(n==null)throw new ft(`no available resolver for ${e.name}`);return(await n(this,t)).map(i=>R(i))}nodeAddress(){let t=this.toOptions();if(t.transport!=="tcp"&&t.transport!=="udp")throw new Error(`multiaddr must have a valid format - no protocol with name: "${t.transport}". Must have a valid transport protocol: "{tcp, udp}"`);return{family:t.family,address:t.host,port:t.port}}isThinWaistAddress(){return!(this.#e.length!==2||this.#e[0].code!==4&&this.#e[0].code!==41||this.#e[1].code!==6&&this.#e[1].code!==273)}[lo](){return`Multiaddr(${this.toString()})`}};function ho(r){r.getComponents().forEach(t=>{let e=x.getProtocol(t.code);t.value!=null&&e.validate?.(t.value)})}function Or(r,t,e){let n=0;for(let o of r)if(!(n<t)){if(n>e)break;if(o!==255)return!1;n++}return!0}function _r(r,t,e,n){let o=0;for(let i of r)if(!(o<e)){if(o>n)break;if(i!==t[o])return!1;o++}return!0}function xt(r){switch(r.length){case W:return r.join(".");case j:{let t=[];for(let e=0;e<r.length;e++)e%2===0&&t.push(r[e].toString(16).padStart(2,"0")+r[e+1].toString(16).padStart(2,"0"));return t.join(":")}default:throw new Error("Invalid ip length")}}function Nr(r){let t=0;for(let[e,n]of r.entries()){if(n===255){t+=8;continue}for(;(n&128)!=0;)t++,n=n<<1;if((n&128)!=0)return-1;for(let o=e+1;o<r.length;o++)if(r[o]!=0)return-1;break}return t}function Ur(r){let t="0x";for(let e of r)t+=(e>>4).toString(16)+(e&15).toString(16);return t}var W=4,j=16,gs=parseInt("0xFFFF",16),mo=new Uint8Array([0,0,0,0,0,0,0,0,0,0,255,255]);function me(r,t){t.length===j&&r.length===W&&Or(t,0,11)&&(t=t.slice(12)),t.length===W&&r.length===j&&_r(r,mo,0,11)&&(r=r.slice(12));let e=r.length;if(e!=t.length)throw new Error("Failed to mask ip");let n=new Uint8Array(e);for(let o=0;o<e;o++)n[o]=r[o]&t[o];return n}function Br(r,t){if(typeof t=="string"&&(t=K(t)),t==null)throw new Error("Invalid ip");if(t.length!==r.network.length)return!1;for(let e=0;e<t.length;e++)if((r.network[e]&r.mask[e])!==(t[e]&r.mask[e]))return!1;return!0}function gt(r){let[t,e]=r.split("/");if(!t||!e)throw new Error("Failed to parse given CIDR: "+r);let n=W,o=Ce(t);if(o==null&&(n=j,o=Ee(t),o==null))throw new Error("Failed to parse given CIDR: "+r);let i=parseInt(e,10);if(Number.isNaN(i)||String(i).length!==e.length||i<0||i>n*8)throw new Error("Failed to parse given CIDR: "+r);let s=wt(i,8*n);return{network:me(o,s),mask:s}}function wt(r,t){if(t!==8*W&&t!==8*j)throw new Error("Invalid CIDR mask");if(r<0||r>t)throw new Error("Invalid CIDR mask");let e=t/8,n=new Uint8Array(e);for(let o=0;o<e;o++){if(r>=8){n[o]=255,r-=8;continue}n[o]=255-(255>>r),r=0}return n}var ee=class{constructor(t,e){if(e==null)({network:this.network,mask:this.mask}=gt(t));else{let n=K(t);if(n==null)throw new Error("Failed to parse network");e=String(e);let o=parseInt(e,10);if(Number.isNaN(o)||String(o).length!==e.length||o<0||o>n.length*8){let i=K(e);if(i==null)throw new Error("Failed to parse mask");this.mask=i}else this.mask=wt(o,8*n.length);this.network=me(n,this.mask)}}contains(t){return Br({network:this.network,mask:this.mask},t)}toString(){let t=Nr(this.mask),e=t!==-1?String(t):Ur(this.mask);return xt(this.network)+"/"+e}};function zr(r){let t,e;if(r.getComponents().forEach(n=>{(n.name==="ip4"||n.name==="ip6")&&(e=n.value),n.name==="ipcidr"&&(t=n.value)}),t==null||e==null)throw new Error("Invalid multiaddr");return new ee(e,t)}var _e=class{multiaddr;netmask;constructor(t){this.multiaddr=R(t),this.netmask=zr(this.multiaddr)}contains(t){if(t==null)return!1;let e=R(t),n;for(let[o,i]of e.stringTuples())if(o===4||o===41){n=i;break}return n===void 0?!1:this.netmask.contains(n)}};var mt=new Map;function xo(r,t){if(r==null)throw new V("requires node address object");if(t==null)throw new V("requires transport protocol");let e,n=r.address;switch(r.family){case 4:e="ip4";break;case 6:if(e="ip6",n.includes("%")){let o=n.split("%");if(o.length!==2)throw Error("Multiple ip6 zones in multiaddr");n=o[0],e=`ip6zone/${o[1]}/ip6`}break;default:throw Error("Invalid addr family, should be 4 or 6.")}return new he("/"+[e,n,t,r.port].join("/"))}function go(r){return R(r.map(([t,e])=>{let n=x.getProtocol(t),o={code:t,name:n.name};return e!=null&&(o.value=n.bytesToValue?.(e)??y(e)),o}))}function wo(r){return R(r.map(([t,e])=>{let n=x.getProtocol(t),o={code:t,name:n.name};return e!=null&&(o.value=e),o}))}function bo(r){return Oe(r)?r.protos().some(t=>t.resolvable):!1}function Oe(r){return!!r?.[ht]}function R(r){return new he(r)}function yo(r){let t=x.getProtocol(r);return{code:t.code,size:t.size??0,name:t.name,resolvable:!!t.resolvable,path:!!t.path}}return Vr(Co);})();
return MultiformatsMultiaddr}));
//# sourceMappingURL=index.min.js.map