@libp2p/keychain 6.0.1 → 6.0.2-de2ad9ca5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +2 -2
- package/dist/src/keychain.js +1 -1
- package/dist/src/keychain.js.map +1 -1
- package/package.json +4 -4
- package/src/keychain.ts +1 -1
- package/dist/typedoc-urls.json +0 -14
package/README.md
CHANGED
|
@@ -45,7 +45,7 @@ The key management and naming service API all return a `KeyInfo` object. The `i
|
|
|
45
45
|
|
|
46
46
|
The **key id** is the SHA-256 [multihash](https://github.com/multiformats/multihash) of its public key.
|
|
47
47
|
|
|
48
|
-
The *public key* is a [protobuf encoding](https://github.com/libp2p/js-libp2p/blob/
|
|
48
|
+
The *public key* is a [protobuf encoding](https://github.com/libp2p/js-libp2p/blob/958761163edfc7b1a6231041b199737d72fd6424/packages/crypto/src/keys/keys.proto) containing a type and the [DER encoding](https://en.wikipedia.org/wiki/X.690) of the PKCS [SubjectPublicKeyInfo](https://www.ietf.org/rfc/rfc3279.txt).
|
|
49
49
|
|
|
50
50
|
## Private key storage
|
|
51
51
|
|
package/dist/index.min.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
`));let e=this.idBlock.tagClass===3?`[${this.idBlock.tagNumber}]`:this.constructor.NAME;return t.length?`${e} :
|
|
5
5
|
${t.join(`
|
|
6
6
|
`)}`:`${e} :`}};$c=ye;k.Constructed=$c;ye.NAME="CONSTRUCTED";var qn=class extends lt{fromBER(t,e,n){return e}toBER(t){return Tt}};qn.override="EndOfContentValueBlock";var jc,$n=class extends it{constructor(t={}){super(t,qn),this.idBlock.tagClass=1,this.idBlock.tagNumber=0}};jc=$n;k.EndOfContent=jc;$n.NAME=Gr;var Gc,Re=class extends it{constructor(t={}){super(t,lt),this.idBlock.tagClass=1,this.idBlock.tagNumber=5}fromBER(t,e,n){return this.lenBlock.length>0&&this.warnings.push("Non-zero length of value block for Null type"),this.idBlock.error.length||(this.blockLength+=this.idBlock.blockLength),this.lenBlock.error.length||(this.blockLength+=this.lenBlock.blockLength),this.blockLength+=n,e+n>t.byteLength?(this.error="End of input reached before message was fully decoded (inconsistent offset and length values)",-1):e+n}toBER(t,e){let n=new ArrayBuffer(2);if(!t){let s=new Uint8Array(n);s[0]=5,s[1]=0}return e&&e.write(n),n}onAsciiEncoding(){return`${this.constructor.NAME}`}};Gc=Re;k.Null=Gc;Re.NAME="NULL";var jn=class extends te(lt){get value(){for(let t of this.valueHexView)if(t>0)return!0;return!1}set value(t){this.valueHexView[0]=t?255:0}constructor({value:t,...e}={}){super(e),e.valueHex?this.valueHexView=q.BufferSourceConverter.toUint8Array(e.valueHex):this.valueHexView=new Uint8Array(1),t&&(this.value=t)}fromBER(t,e,n){let s=q.BufferSourceConverter.toUint8Array(t);return Qt(this,s,e,n)?(this.valueHexView=s.subarray(e,e+n),n>1&&this.warnings.push("Boolean value encoded in more then 1 octet"),this.isHexOnly=!0,ti.call(this),this.blockLength=n,e+n):-1}toBER(){return this.valueHexView.slice()}toJSON(){return{...super.toJSON(),value:this.value}}};jn.NAME="BooleanValueBlock";var Zc,Gn=class extends it{getValue(){return this.valueBlock.value}setValue(t){this.valueBlock.value=t}constructor(t={}){super(t,jn),this.idBlock.tagClass=1,this.idBlock.tagNumber=1}onAsciiEncoding(){return`${this.constructor.NAME} : ${this.getValue}`}};Zc=Gn;k.Boolean=Zc;Gn.NAME="BOOLEAN";var Zn=class extends te(Vt){constructor({isConstructed:t=!1,...e}={}){super(e),this.isConstructed=t}fromBER(t,e,n){let s=0;if(this.isConstructed){if(this.isHexOnly=!1,s=Vt.prototype.fromBER.call(this,t,e,n),s===-1)return s;for(let o=0;o<this.value.length;o++){let i=this.value[o].constructor.NAME;if(i===Gr){if(this.isIndefiniteForm)break;return this.error="EndOfContent is unexpected, OCTET STRING may consists of OCTET STRINGs only",-1}if(i!==Mc)return this.error="OCTET STRING may consists of OCTET STRINGs only",-1}}else this.isHexOnly=!0,s=super.fromBER(t,e,n),this.blockLength=n;return s}toBER(t,e){return this.isConstructed?Vt.prototype.toBER.call(this,t,e):t?new ArrayBuffer(this.valueHexView.byteLength):this.valueHexView.slice().buffer}toJSON(){return{...super.toJSON(),isConstructed:this.isConstructed}}};Zn.NAME="OctetStringValueBlock";var ni,Wt=class extends it{constructor({idBlock:t={},lenBlock:e={},...n}={}){var s,o;(s=n.isConstructed)!==null&&s!==void 0||(n.isConstructed=!!(!((o=n.value)===null||o===void 0)&&o.length)),super({idBlock:{isConstructed:n.isConstructed,...t},lenBlock:{...e,isIndefiniteForm:!!n.isIndefiniteForm},...n},Zn),this.idBlock.tagClass=1,this.idBlock.tagNumber=4}fromBER(t,e,n){if(this.valueBlock.isConstructed=this.idBlock.isConstructed,this.valueBlock.isIndefiniteForm=this.lenBlock.isIndefiniteForm,n===0)return this.idBlock.error.length===0&&(this.blockLength+=this.idBlock.blockLength),this.lenBlock.error.length===0&&(this.blockLength+=this.lenBlock.blockLength),e;if(!this.valueBlock.isConstructed){let o=(t instanceof ArrayBuffer?new Uint8Array(t):t).subarray(e,e+n);try{if(o.byteLength){let i=Es(o,0,o.byteLength);i.offset!==-1&&i.offset===n&&(this.valueBlock.value=[i.result])}}catch{}}return super.fromBER(t,e,n)}onAsciiEncoding(){if(this.valueBlock.isConstructed||this.valueBlock.value&&this.valueBlock.value.length)return ye.prototype.onAsciiEncoding.call(this);let t=this.constructor.NAME,e=q.Convert.ToHex(this.valueBlock.valueHexView);return`${t} : ${e}`}getValue(){if(!this.idBlock.isConstructed)return this.valueBlock.valueHexView.slice().buffer;let t=[];for(let e of this.valueBlock.value)e instanceof ni&&t.push(e.valueBlock.valueHexView);return q.BufferSourceConverter.concat(t)}};ni=Wt;k.OctetString=ni;Wt.NAME=Mc;var Yn=class extends te(Vt){constructor({unusedBits:t=0,isConstructed:e=!1,...n}={}){super(n),this.unusedBits=t,this.isConstructed=e,this.blockLength=this.valueHexView.byteLength}fromBER(t,e,n){if(!n)return e;let s=-1;if(this.isConstructed){if(s=Vt.prototype.fromBER.call(this,t,e,n),s===-1)return s;for(let c of this.value){let a=c.constructor.NAME;if(a===Gr){if(this.isIndefiniteForm)break;return this.error="EndOfContent is unexpected, BIT STRING may consists of BIT STRINGs only",-1}if(a!==Pc)return this.error="BIT STRING may consists of BIT STRINGs only",-1;let l=c.valueBlock;if(this.unusedBits>0&&l.unusedBits>0)return this.error='Using of "unused bits" inside constructive BIT STRING allowed for least one only',-1;this.unusedBits=l.unusedBits}return s}let o=q.BufferSourceConverter.toUint8Array(t);if(!Qt(this,o,e,n))return-1;let i=o.subarray(e,e+n);if(this.unusedBits=i[0],this.unusedBits>7)return this.error="Unused bits for BitString must be in range 0-7",-1;if(!this.unusedBits){let c=i.subarray(1);try{if(c.byteLength){let a=Es(c,0,c.byteLength);a.offset!==-1&&a.offset===n-1&&(this.value=[a.result])}}catch{}}return this.valueHexView=i.subarray(1),this.blockLength=i.length,e+n}toBER(t,e){if(this.isConstructed)return Vt.prototype.toBER.call(this,t,e);if(t)return new ArrayBuffer(this.valueHexView.byteLength+1);if(!this.valueHexView.byteLength)return Tt;let n=new Uint8Array(this.valueHexView.length+1);return n[0]=this.unusedBits,n.set(this.valueHexView,1),n.buffer}toJSON(){return{...super.toJSON(),unusedBits:this.unusedBits,isConstructed:this.isConstructed}}};Yn.NAME="BitStringValueBlock";var Yc,zn=class extends it{constructor({idBlock:t={},lenBlock:e={},...n}={}){var s,o;(s=n.isConstructed)!==null&&s!==void 0||(n.isConstructed=!!(!((o=n.value)===null||o===void 0)&&o.length)),super({idBlock:{isConstructed:n.isConstructed,...t},lenBlock:{...e,isIndefiniteForm:!!n.isIndefiniteForm},...n},Yn),this.idBlock.tagClass=1,this.idBlock.tagNumber=3}fromBER(t,e,n){return this.valueBlock.isConstructed=this.idBlock.isConstructed,this.valueBlock.isIndefiniteForm=this.lenBlock.isIndefiniteForm,super.fromBER(t,e,n)}onAsciiEncoding(){if(this.valueBlock.isConstructed||this.valueBlock.value&&this.valueBlock.value.length)return ye.prototype.onAsciiEncoding.call(this);{let t=[],e=this.valueBlock.valueHexView;for(let i of e)t.push(i.toString(2).padStart(8,"0"));let n=t.join(""),s=this.constructor.NAME,o=n.substring(0,n.length-this.valueBlock.unusedBits);return`${s} : ${o}`}}};Yc=zn;k.BitString=Yc;zn.NAME=Pc;var zc;function kh(r,t){let e=new Uint8Array([0]),n=new Uint8Array(r),s=new Uint8Array(t),o=n.slice(0),i=o.length-1,c=s.slice(0),a=c.length-1,l=0,f=a<i?i:a,u=0;for(let d=f;d>=0;d--,u++){switch(!0){case u<c.length:l=o[i-u]+c[a-u]+e[0];break;default:l=o[i-u]+e[0]}switch(e[0]=l/10,!0){case u>=o.length:o=Kn(new Uint8Array([l%10]),o);break;default:o[i-u]=l%10}}return e[0]>0&&(o=Kn(e,o)),o}function Hc(r){if(r>=qr.length)for(let t=qr.length;t<=r;t++){let e=new Uint8Array([0]),n=qr[t-1].slice(0);for(let s=n.length-1;s>=0;s--){let o=new Uint8Array([(n[s]<<1)+e[0]]);e[0]=o[0]/10,n[s]=o[0]%10}e[0]>0&&(n=Kn(e,n)),qr.push(n)}return qr[r]}function Ih(r,t){let e=0,n=new Uint8Array(r),s=new Uint8Array(t),o=n.slice(0),i=o.length-1,c=s.slice(0),a=c.length-1,l,f=0;for(let u=a;u>=0;u--,f++)switch(l=o[i-f]-c[a-f]-e,!0){case l<0:e=1,o[i-f]=l+10;break;default:e=0,o[i-f]=l}if(e>0)for(let u=i-a+1;u>=0;u--,f++)if(l=o[i-f]-e,l<0)e=1,o[i-f]=l+10;else{e=0,o[i-f]=l;break}return o.slice()}var Zr=class extends te(lt){setValueHex(){this.valueHexView.length>=4?(this.warnings.push("Too big Integer for decoding, hex only"),this.isHexOnly=!0,this._valueDec=0):(this.isHexOnly=!1,this.valueHexView.length>0&&(this._valueDec=ti.call(this)))}constructor({value:t,...e}={}){super(e),this._valueDec=0,e.valueHex&&this.setValueHex(),t!==void 0&&(this.valueDec=t)}set valueDec(t){this._valueDec=t,this.isHexOnly=!1,this.valueHexView=new Uint8Array(Kc(t))}get valueDec(){return this._valueDec}fromDER(t,e,n,s=0){let o=this.fromBER(t,e,n);if(o===-1)return o;let i=this.valueHexView;return i[0]===0&&(i[1]&128)!==0?this.valueHexView=i.subarray(1):s!==0&&i.length<s&&(s-i.length>1&&(s=i.length+1),this.valueHexView=i.subarray(s-i.length)),o}toDER(t=!1){let e=this.valueHexView;switch(!0){case(e[0]&128)!==0:{let n=new Uint8Array(this.valueHexView.length+1);n[0]=0,n.set(e,1),this.valueHexView=n}break;case(e[0]===0&&(e[1]&128)===0):this.valueHexView=this.valueHexView.subarray(1);break}return this.toBER(t)}fromBER(t,e,n){let s=super.fromBER(t,e,n);return s===-1||this.setValueHex(),s}toBER(t){return t?new ArrayBuffer(this.valueHexView.length):this.valueHexView.slice().buffer}toJSON(){return{...super.toJSON(),valueDec:this.valueDec}}toString(){let t=this.valueHexView.length*8-1,e=new Uint8Array(this.valueHexView.length*8/3),n=0,s,o=this.valueHexView,i="",c=!1;for(let a=o.byteLength-1;a>=0;a--){s=o[a];for(let l=0;l<8;l++){if((s&1)===1)switch(n){case t:e=Ih(Hc(n),e),i="-";break;default:e=kh(e,Hc(n))}n++,s>>=1}}for(let a=0;a<e.length;a++)e[a]&&(c=!0),c&&(i+=Oc.charAt(e[a]));return c===!1&&(i+=Oc.charAt(0)),i}};zc=Zr;Zr.NAME="IntegerValueBlock";Object.defineProperty(zc.prototype,"valueHex",{set:function(r){this.valueHexView=new Uint8Array(r),this.setValueHex()},get:function(){return this.valueHexView.slice().buffer}});var $r,Jt=class extends it{constructor(t={}){super(t,Zr),this.idBlock.tagClass=1,this.idBlock.tagNumber=2}toBigInt(){return Vn(),BigInt(this.valueBlock.toString())}static fromBigInt(t){Vn();let e=BigInt(t),n=new jr,s=e.toString(16).replace(/^-/,""),o=new Uint8Array(q.Convert.FromHex(s));if(e<0){let c=new Uint8Array(o.length+(o[0]&128?1:0));c[0]|=128;let l=BigInt(`0x${q.Convert.ToHex(c)}`)+e,f=q.BufferSourceConverter.toUint8Array(q.Convert.FromHex(l.toString(16)));f[0]|=128,n.write(f)}else o[0]&128&&n.write(new Uint8Array([0])),n.write(o);return new $r({valueHex:n.final()})}convertToDER(){let t=new $r({valueHex:this.valueBlock.valueHexView});return t.valueBlock.toDER(),t}convertFromDER(){return new $r({valueHex:this.valueBlock.valueHexView[0]===0?this.valueBlock.valueHexView.subarray(1):this.valueBlock.valueHexView})}onAsciiEncoding(){return`${this.constructor.NAME} : ${this.valueBlock.toString()}`}};$r=Jt;k.Integer=$r;Jt.NAME="INTEGER";var Wc,Wn=class extends Jt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=10}};Wc=Wn;k.Enumerated=Wc;Wn.NAME="ENUMERATED";var Yr=class extends te(lt){constructor({valueDec:t=-1,isFirstSid:e=!1,...n}={}){super(n),this.valueDec=t,this.isFirstSid=e}fromBER(t,e,n){if(!n)return e;let s=q.BufferSourceConverter.toUint8Array(t);if(!Qt(this,s,e,n))return-1;let o=s.subarray(e,e+n);this.valueHexView=new Uint8Array(n);for(let c=0;c<n&&(this.valueHexView[c]=o[c]&127,this.blockLength++,(o[c]&128)!==0);c++);let i=new Uint8Array(this.blockLength);for(let c=0;c<this.blockLength;c++)i[c]=this.valueHexView[c];return this.valueHexView=i,(o[this.blockLength-1]&128)!==0?(this.error="End of input reached before message was fully decoded",-1):(this.valueHexView[0]===0&&this.warnings.push("Needlessly long format of SID encoding"),this.blockLength<=8?this.valueDec=Te(this.valueHexView,7):(this.isHexOnly=!0,this.warnings.push("Too big SID for decoding, hex only")),e+this.blockLength)}set valueBigInt(t){Vn();let e=BigInt(t).toString(2);for(;e.length%7;)e="0"+e;let n=new Uint8Array(e.length/7);for(let s=0;s<n.length;s++)n[s]=parseInt(e.slice(s*7,s*7+7),2)+(s+1<n.length?128:0);this.fromBER(n.buffer,0,n.length)}toBER(t){if(this.isHexOnly){if(t)return new ArrayBuffer(this.valueHexView.byteLength);let s=this.valueHexView,o=new Uint8Array(this.blockLength);for(let i=0;i<this.blockLength-1;i++)o[i]=s[i]|128;return o[this.blockLength-1]=s[this.blockLength-1],o.buffer}let e=ge(this.valueDec,7);if(e.byteLength===0)return this.error="Error during encoding SID value",Tt;let n=new Uint8Array(e.byteLength);if(!t){let s=new Uint8Array(e),o=e.byteLength-1;for(let i=0;i<o;i++)n[i]=s[i]|128;n[o]=s[o]}return n}toString(){let t="";if(this.isHexOnly)t=q.Convert.ToHex(this.valueHexView);else if(this.isFirstSid){let e=this.valueDec;this.valueDec<=39?t="0.":this.valueDec<=79?(t="1.",e-=40):(t="2.",e-=80),t+=e.toString()}else t=this.valueDec.toString();return t}toJSON(){return{...super.toJSON(),valueDec:this.valueDec,isFirstSid:this.isFirstSid}}};Yr.NAME="sidBlock";var Jn=class extends lt{constructor({value:t=cr,...e}={}){super(e),this.value=[],t&&this.fromString(t)}fromBER(t,e,n){let s=e;for(;n>0;){let o=new Yr;if(s=o.fromBER(t,s,n),s===-1)return this.blockLength=0,this.error=o.error,s;this.value.length===0&&(o.isFirstSid=!0),this.blockLength+=o.blockLength,n-=o.blockLength,this.value.push(o)}return s}toBER(t){let e=[];for(let n=0;n<this.value.length;n++){let s=this.value[n].toBER(t);if(s.byteLength===0)return this.error=this.value[n].error,Tt;e.push(s)}return ei(e)}fromString(t){this.value=[];let e=0,n=0,s="",o=!1;do if(n=t.indexOf(".",e),n===-1?s=t.substring(e):s=t.substring(e,n),e=n+1,o){let i=this.value[0],c=0;switch(i.valueDec){case 0:break;case 1:c=40;break;case 2:c=80;break;default:this.value=[];return}let a=parseInt(s,10);if(isNaN(a))return;i.valueDec=a+c,o=!1}else{let i=new Yr;if(s>Number.MAX_SAFE_INTEGER){Vn();let c=BigInt(s);i.valueBigInt=c}else if(i.valueDec=parseInt(s,10),isNaN(i.valueDec))return;this.value.length||(i.isFirstSid=!0,o=!0),this.value.push(i)}while(n!==-1)}toString(){let t="",e=!1;for(let n=0;n<this.value.length;n++){e=this.value[n].isHexOnly;let s=this.value[n].toString();n!==0&&(t=`${t}.`),e?(s=`{${s}}`,this.value[n].isFirstSid?t=`2.{${s} - 80}`:t+=s):t+=s}return t}toJSON(){let t={...super.toJSON(),value:this.toString(),sidArray:[]};for(let e=0;e<this.value.length;e++)t.sidArray.push(this.value[e].toJSON());return t}};Jn.NAME="ObjectIdentifierValueBlock";var Jc,Ot=class extends it{getValue(){return this.valueBlock.toString()}setValue(t){this.valueBlock.fromString(t)}constructor(t={}){super(t,Jn),this.idBlock.tagClass=1,this.idBlock.tagNumber=6}onAsciiEncoding(){return`${this.constructor.NAME} : ${this.valueBlock.toString()||"empty"}`}toJSON(){return{...super.toJSON(),value:this.getValue()}}};Jc=Ot;k.ObjectIdentifier=Jc;Ot.NAME="OBJECT IDENTIFIER";var zr=class extends te(zt){constructor({valueDec:t=0,...e}={}){super(e),this.valueDec=t}fromBER(t,e,n){if(n===0)return e;let s=q.BufferSourceConverter.toUint8Array(t);if(!Qt(this,s,e,n))return-1;let o=s.subarray(e,e+n);this.valueHexView=new Uint8Array(n);for(let c=0;c<n&&(this.valueHexView[c]=o[c]&127,this.blockLength++,(o[c]&128)!==0);c++);let i=new Uint8Array(this.blockLength);for(let c=0;c<this.blockLength;c++)i[c]=this.valueHexView[c];return this.valueHexView=i,(o[this.blockLength-1]&128)!==0?(this.error="End of input reached before message was fully decoded",-1):(this.valueHexView[0]===0&&this.warnings.push("Needlessly long format of SID encoding"),this.blockLength<=8?this.valueDec=Te(this.valueHexView,7):(this.isHexOnly=!0,this.warnings.push("Too big SID for decoding, hex only")),e+this.blockLength)}toBER(t){if(this.isHexOnly){if(t)return new ArrayBuffer(this.valueHexView.byteLength);let s=this.valueHexView,o=new Uint8Array(this.blockLength);for(let i=0;i<this.blockLength-1;i++)o[i]=s[i]|128;return o[this.blockLength-1]=s[this.blockLength-1],o.buffer}let e=ge(this.valueDec,7);if(e.byteLength===0)return this.error="Error during encoding SID value",Tt;let n=new Uint8Array(e.byteLength);if(!t){let s=new Uint8Array(e),o=e.byteLength-1;for(let i=0;i<o;i++)n[i]=s[i]|128;n[o]=s[o]}return n.buffer}toString(){let t="";return this.isHexOnly?t=q.Convert.ToHex(this.valueHexView):t=this.valueDec.toString(),t}toJSON(){return{...super.toJSON(),valueDec:this.valueDec}}};zr.NAME="relativeSidBlock";var Xn=class extends lt{constructor({value:t=cr,...e}={}){super(e),this.value=[],t&&this.fromString(t)}fromBER(t,e,n){let s=e;for(;n>0;){let o=new zr;if(s=o.fromBER(t,s,n),s===-1)return this.blockLength=0,this.error=o.error,s;this.blockLength+=o.blockLength,n-=o.blockLength,this.value.push(o)}return s}toBER(t,e){let n=[];for(let s=0;s<this.value.length;s++){let o=this.value[s].toBER(t);if(o.byteLength===0)return this.error=this.value[s].error,Tt;n.push(o)}return ei(n)}fromString(t){this.value=[];let e=0,n=0,s="";do{n=t.indexOf(".",e),n===-1?s=t.substring(e):s=t.substring(e,n),e=n+1;let o=new zr;if(o.valueDec=parseInt(s,10),isNaN(o.valueDec))return!0;this.value.push(o)}while(n!==-1);return!0}toString(){let t="",e=!1;for(let n=0;n<this.value.length;n++){e=this.value[n].isHexOnly;let s=this.value[n].toString();n!==0&&(t=`${t}.`),e&&(s=`{${s}}`),t+=s}return t}toJSON(){let t={...super.toJSON(),value:this.toString(),sidArray:[]};for(let e=0;e<this.value.length;e++)t.sidArray.push(this.value[e].toJSON());return t}};Xn.NAME="RelativeObjectIdentifierValueBlock";var Xc,Qn=class extends it{getValue(){return this.valueBlock.toString()}setValue(t){this.valueBlock.fromString(t)}constructor(t={}){super(t,Xn),this.idBlock.tagClass=1,this.idBlock.tagNumber=13}onAsciiEncoding(){return`${this.constructor.NAME} : ${this.valueBlock.toString()||"empty"}`}toJSON(){return{...super.toJSON(),value:this.getValue()}}};Xc=Qn;k.RelativeObjectIdentifier=Xc;Qn.NAME="RelativeObjectIdentifier";var Qc,Et=class extends ye{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=16}};Qc=Et;k.Sequence=Qc;Et.NAME="SEQUENCE";var tu,ts=class extends ye{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=17}};tu=ts;k.Set=tu;ts.NAME="SET";var es=class extends te(lt){constructor({...t}={}){super(t),this.isHexOnly=!0,this.value=cr}toJSON(){return{...super.toJSON(),value:this.value}}};es.NAME="StringValueBlock";var rs=class extends es{};rs.NAME="SimpleStringValueBlock";var yt=class extends Mn{constructor({...t}={}){super(t,rs)}fromBuffer(t){this.valueBlock.value=String.fromCharCode.apply(null,q.BufferSourceConverter.toUint8Array(t))}fromString(t){let e=t.length,n=this.valueBlock.valueHexView=new Uint8Array(e);for(let s=0;s<e;s++)n[s]=t.charCodeAt(s);this.valueBlock.value=t}};yt.NAME="SIMPLE STRING";var ns=class extends yt{fromBuffer(t){this.valueBlock.valueHexView=q.BufferSourceConverter.toUint8Array(t);try{this.valueBlock.value=q.Convert.ToUtf8String(t)}catch(e){this.warnings.push(`Error during "decodeURIComponent": ${e}, using raw string`),this.valueBlock.value=q.Convert.ToBinary(t)}}fromString(t){this.valueBlock.valueHexView=new Uint8Array(q.Convert.FromUtf8String(t)),this.valueBlock.value=t}};ns.NAME="Utf8StringValueBlock";var eu,Xt=class extends ns{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=12}};eu=Xt;k.Utf8String=eu;Xt.NAME="UTF8String";var ss=class extends yt{fromBuffer(t){this.valueBlock.value=q.Convert.ToUtf16String(t),this.valueBlock.valueHexView=q.BufferSourceConverter.toUint8Array(t)}fromString(t){this.valueBlock.value=t,this.valueBlock.valueHexView=new Uint8Array(q.Convert.FromUtf16String(t))}};ss.NAME="BmpStringValueBlock";var ru,os=class extends ss{constructor({...t}={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=30}};ru=os;k.BmpString=ru;os.NAME="BMPString";var is=class extends yt{fromBuffer(t){let e=ArrayBuffer.isView(t)?t.slice().buffer:t.slice(0),n=new Uint8Array(e);for(let s=0;s<n.length;s+=4)n[s]=n[s+3],n[s+1]=n[s+2],n[s+2]=0,n[s+3]=0;this.valueBlock.value=String.fromCharCode.apply(null,new Uint32Array(e))}fromString(t){let e=t.length,n=this.valueBlock.valueHexView=new Uint8Array(e*4);for(let s=0;s<e;s++){let o=ge(t.charCodeAt(s),8),i=new Uint8Array(o);if(i.length>4)continue;let c=4-i.length;for(let a=i.length-1;a>=0;a--)n[s*4+a+c]=i[a]}this.valueBlock.value=t}};is.NAME="UniversalStringValueBlock";var nu,as=class extends is{constructor({...t}={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=28}};nu=as;k.UniversalString=nu;as.NAME="UniversalString";var su,cs=class extends yt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=18}};su=cs;k.NumericString=su;cs.NAME="NumericString";var ou,us=class extends yt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=19}};ou=us;k.PrintableString=ou;us.NAME="PrintableString";var iu,ls=class extends yt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=20}};iu=ls;k.TeletexString=iu;ls.NAME="TeletexString";var au,fs=class extends yt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=21}};au=fs;k.VideotexString=au;fs.NAME="VideotexString";var cu,hs=class extends yt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=22}};cu=hs;k.IA5String=cu;hs.NAME="IA5String";var uu,ds=class extends yt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=25}};uu=ds;k.GraphicString=uu;ds.NAME="GraphicString";var lu,Wr=class extends yt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=26}};lu=Wr;k.VisibleString=lu;Wr.NAME="VisibleString";var fu,ps=class extends yt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=27}};fu=ps;k.GeneralString=fu;ps.NAME="GeneralString";var hu,gs=class extends yt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=29}};hu=gs;k.CharacterString=hu;gs.NAME="CharacterString";var du,Jr=class extends Wr{constructor({value:t,valueDate:e,...n}={}){if(super(n),this.year=0,this.month=0,this.day=0,this.hour=0,this.minute=0,this.second=0,t){this.fromString(t),this.valueBlock.valueHexView=new Uint8Array(t.length);for(let s=0;s<t.length;s++)this.valueBlock.valueHexView[s]=t.charCodeAt(s)}e&&(this.fromDate(e),this.valueBlock.valueHexView=new Uint8Array(this.toBuffer())),this.idBlock.tagClass=1,this.idBlock.tagNumber=23}fromBuffer(t){this.fromString(String.fromCharCode.apply(null,q.BufferSourceConverter.toUint8Array(t)))}toBuffer(){let t=this.toString(),e=new ArrayBuffer(t.length),n=new Uint8Array(e);for(let s=0;s<t.length;s++)n[s]=t.charCodeAt(s);return e}fromDate(t){this.year=t.getUTCFullYear(),this.month=t.getUTCMonth()+1,this.day=t.getUTCDate(),this.hour=t.getUTCHours(),this.minute=t.getUTCMinutes(),this.second=t.getUTCSeconds()}toDate(){return new Date(Date.UTC(this.year,this.month-1,this.day,this.hour,this.minute,this.second))}fromString(t){let n=/(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z/ig.exec(t);if(n===null){this.error="Wrong input string for conversion";return}let s=parseInt(n[1],10);s>=50?this.year=1900+s:this.year=2e3+s,this.month=parseInt(n[2],10),this.day=parseInt(n[3],10),this.hour=parseInt(n[4],10),this.minute=parseInt(n[5],10),this.second=parseInt(n[6],10)}toString(t="iso"){if(t==="iso"){let e=new Array(7);return e[0]=vt(this.year<2e3?this.year-1900:this.year-2e3,2),e[1]=vt(this.month,2),e[2]=vt(this.day,2),e[3]=vt(this.hour,2),e[4]=vt(this.minute,2),e[5]=vt(this.second,2),e[6]="Z",e.join("")}return super.toString(t)}onAsciiEncoding(){return`${this.constructor.NAME} : ${this.toDate().toISOString()}`}toJSON(){return{...super.toJSON(),year:this.year,month:this.month,day:this.day,hour:this.hour,minute:this.minute,second:this.second}}};du=Jr;k.UTCTime=du;Jr.NAME="UTCTime";var pu,ys=class extends Jr{constructor(t={}){var e;super(t),(e=this.millisecond)!==null&&e!==void 0||(this.millisecond=0),this.idBlock.tagClass=1,this.idBlock.tagNumber=24}fromDate(t){super.fromDate(t),this.millisecond=t.getUTCMilliseconds()}toDate(){let t=Date.UTC(this.year,this.month-1,this.day,this.hour,this.minute,this.second,this.millisecond);return new Date(t)}fromString(t){let e=!1,n="",s="",o=0,i,c=0,a=0;if(t[t.length-1]==="Z")n=t.substring(0,t.length-1),e=!0;else{let u=new Number(t[t.length-1]);if(isNaN(u.valueOf()))throw new Error("Wrong input string for conversion");n=t}if(e){if(n.indexOf("+")!==-1)throw new Error("Wrong input string for conversion");if(n.indexOf("-")!==-1)throw new Error("Wrong input string for conversion")}else{let u=1,d=n.indexOf("+"),y="";if(d===-1&&(d=n.indexOf("-"),u=-1),d!==-1){if(y=n.substring(d+1),n=n.substring(0,d),y.length!==2&&y.length!==4)throw new Error("Wrong input string for conversion");let h=parseInt(y.substring(0,2),10);if(isNaN(h.valueOf()))throw new Error("Wrong input string for conversion");if(c=u*h,y.length===4){if(h=parseInt(y.substring(2,4),10),isNaN(h.valueOf()))throw new Error("Wrong input string for conversion");a=u*h}}}let l=n.indexOf(".");if(l===-1&&(l=n.indexOf(",")),l!==-1){let u=new Number(`0${n.substring(l)}`);if(isNaN(u.valueOf()))throw new Error("Wrong input string for conversion");o=u.valueOf(),s=n.substring(0,l)}else s=n;switch(!0){case s.length===8:if(i=/(\d{4})(\d{2})(\d{2})/ig,l!==-1)throw new Error("Wrong input string for conversion");break;case s.length===10:if(i=/(\d{4})(\d{2})(\d{2})(\d{2})/ig,l!==-1){let u=60*o;this.minute=Math.floor(u),u=60*(u-this.minute),this.second=Math.floor(u),u=1e3*(u-this.second),this.millisecond=Math.floor(u)}break;case s.length===12:if(i=/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/ig,l!==-1){let u=60*o;this.second=Math.floor(u),u=1e3*(u-this.second),this.millisecond=Math.floor(u)}break;case s.length===14:if(i=/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/ig,l!==-1){let u=1e3*o;this.millisecond=Math.floor(u)}break;default:throw new Error("Wrong input string for conversion")}let f=i.exec(s);if(f===null)throw new Error("Wrong input string for conversion");for(let u=1;u<f.length;u++)switch(u){case 1:this.year=parseInt(f[u],10);break;case 2:this.month=parseInt(f[u],10);break;case 3:this.day=parseInt(f[u],10);break;case 4:this.hour=parseInt(f[u],10)+c;break;case 5:this.minute=parseInt(f[u],10)+a;break;case 6:this.second=parseInt(f[u],10);break;default:throw new Error("Wrong input string for conversion")}if(e===!1){let u=new Date(this.year,this.month,this.day,this.hour,this.minute,this.second,this.millisecond);this.year=u.getUTCFullYear(),this.month=u.getUTCMonth(),this.day=u.getUTCDay(),this.hour=u.getUTCHours(),this.minute=u.getUTCMinutes(),this.second=u.getUTCSeconds(),this.millisecond=u.getUTCMilliseconds()}}toString(t="iso"){if(t==="iso"){let e=[];return e.push(vt(this.year,4)),e.push(vt(this.month,2)),e.push(vt(this.day,2)),e.push(vt(this.hour,2)),e.push(vt(this.minute,2)),e.push(vt(this.second,2)),this.millisecond!==0&&(e.push("."),e.push(vt(this.millisecond,3))),e.push("Z"),e.join("")}return super.toString(t)}toJSON(){return{...super.toJSON(),millisecond:this.millisecond}}};pu=ys;k.GeneralizedTime=pu;ys.NAME="GeneralizedTime";var gu,ms=class extends Xt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=31}};gu=ms;k.DATE=gu;ms.NAME="DATE";var yu,bs=class extends Xt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=32}};yu=bs;k.TimeOfDay=yu;bs.NAME="TimeOfDay";var mu,ws=class extends Xt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=33}};mu=ws;k.DateTime=mu;ws.NAME="DateTime";var bu,xs=class extends Xt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=34}};bu=xs;k.Duration=bu;xs.NAME="Duration";var wu,vs=class extends Xt{constructor(t={}){super(t),this.idBlock.tagClass=1,this.idBlock.tagNumber=14}};wu=vs;k.TIME=wu;vs.NAME="TIME";async function As(r,t){let n=await ir.create().encrypt(r,t);return be.encode(n)}async function si(r,t,e){if(r.type==="RSA")return Lh(r,t,e);if(r.type==="Ed25519")return Ch(r,t,e);if(r.type==="secp256k1")return Nh(r,t,e);if(r.type==="ECDSA")return Uh(r,t,e);throw new Ve}async function Ch(r,t,e="libp2p-key"){if(e==="libp2p-key")return As(Le(r),t);throw new R(`export format '${e}' is not supported`)}async function Nh(r,t,e="libp2p-key"){if(e==="libp2p-key")return As(Le(r),t);throw new R("Export format is not supported")}async function Uh(r,t,e="libp2p-key"){if(e==="libp2p-key")return As(Le(r),t);throw new R(`export format '${e}' is not supported`)}async function Lh(r,t,e="pkcs-8"){if(e==="pkcs-8")return Th(r,t);if(e==="libp2p-key")return As(Le(r),t);throw new R("Export format is not supported")}async function Th(r,t){let e=Q.get(),s=new Et({value:[new Jt({value:0}),new Et({value:[new Ot({value:"1.2.840.113549.1.1.1"}),new Re]}),new Wt({valueHex:r.raw})]}).toBER(),o=new Uint8Array(s,0,s.byteLength),i=Ue(16),c=await Ln(ae,t,i,{c:1e4,dkLen:32}),a=Ue(16),l=await e.subtle.importKey("raw",c,"AES-CBC",!1,["encrypt"]),f=await e.subtle.encrypt({name:"AES-CBC",iv:a},l,o),u=new Et({value:[new Wt({valueHex:i}),new Jt({value:1e4}),new Jt({value:32}),new Et({value:[new Ot({value:"1.2.840.113549.2.11"}),new Re]})]}),d=new Et({value:[new Ot({value:"1.2.840.113549.1.5.13"}),new Et({value:[new Et({value:[new Ot({value:"1.2.840.113549.1.5.12"}),u]}),new Et({value:[new Ot({value:"2.16.840.1.101.3.4.1.42"}),new Wt({valueHex:a})]})]})]}),h=new Et({value:[d,new Wt({valueHex:f})]}).toBER(),B=new Uint8Array(h,0,h.byteLength);return["-----BEGIN ENCRYPTED PRIVATE KEY-----",...$(B,"base64pad").split(/(.{64})/).filter(Boolean),"-----END ENCRYPTED PRIVATE KEY-----"].join(`
|
|
7
|
-
`)}async function oi(r,t){try{let e=await Rh(r,t);return bc(e)}catch{}if(!r.includes("BEGIN"))throw new R("Encrypted key was not a libp2p-key or a PEM file");return _h(r,t)}async function Rh(r,t){let e=be.decode(r);return ir.create().decrypt(e,t)}async function _h(r,t){let e=Q.get(),n;if(r.includes("-----BEGIN ENCRYPTED PRIVATE KEY-----")){let o=F(r.replace("-----BEGIN ENCRYPTED PRIVATE KEY-----","").replace("-----END ENCRYPTED PRIVATE KEY-----","").replace(/\n/g,"").trim(),"base64pad"),{result:i}=Bs(o),{iv:c,salt:a,iterations:l,keySize:f,cipherText:u}=Dh(i),d=await Ln(ae,t,a,{c:l,dkLen:f}),y=await e.subtle.importKey("raw",d,"AES-CBC",!1,["decrypt"]),h=Xr(await e.subtle.decrypt({name:"AES-CBC",iv:c},y,u)),{result:B}=Bs(h);n=Bu(B)}else if(r.includes("-----BEGIN PRIVATE KEY-----")){let o=F(r.replace("-----BEGIN PRIVATE KEY-----","").replace("-----END PRIVATE KEY-----","").replace(/\n/g,"").trim(),"base64pad"),{result:i}=Bs(o);n=Bu(i)}else throw new R("Could not parse private key from PEM data");let s=wc(n);if(s.type!=="RSA")throw new R("Could not parse RSA private key from PEM data");return s}function Dh(r){let t=r.valueBlock.value[0];if(t.valueBlock.value[0].toString()!=="OBJECT IDENTIFIER : 1.2.840.113549.1.5.13")throw new R("Only pkcs5PBES2 encrypted private keys are supported");let n=t.valueBlock.value[1].valueBlock.value[0];if(n.valueBlock.value[0].toString()!=="OBJECT IDENTIFIER : 1.2.840.113549.1.5.12")throw new R("Only pkcs5PBKDF2 key derivation functions are supported");let o=n.valueBlock.value[1],i=Xr(o.valueBlock.value[0].getValue()),c=1e4,a=32;if(o.valueBlock.value.length===3)c=Number(o.valueBlock.value[1].toBigInt()),a=Number(o.valueBlock.value[2].toBigInt());else if(o.valueBlock.value.length===2)throw new R("Could not derive key size and iterations from PEM file - please use @libp2p/rsa to re-import your key");let l=t.valueBlock.value[1].valueBlock.value[1],f=l.valueBlock.value[0].toString();if(f!=="OBJECT IDENTIFIER : 1.2.840.113549.3.7"){if(f!=="OBJECT IDENTIFIER : 1.3.14.3.2.7"){if(f!=="OBJECT IDENTIFIER : 2.16.840.1.101.3.4.1.2"){if(f!=="OBJECT IDENTIFIER : 2.16.840.1.101.3.4.1.22"){if(f!=="OBJECT IDENTIFIER : 2.16.840.1.101.3.4.1.42")throw new R("Only AES-CBC encryption schemes are supported")}}}}let u=Xr(l.valueBlock.value[1].getValue());return{cipherText:Xr(r.valueBlock.value[1].getValue()),salt:i,iterations:c,keySize:a,iv:u}}function Bu(r){return Xr(r.valueBlock.value[2].getValue())}function Xr(r){return new Uint8Array(r,0,r.byteLength)}var Kh="/pkcs8/",ii="/info/",Qr=new WeakMap,_e={minKeyLength:112/8,minSaltLength:128/8,minIterationCount:1e3};function ur(r){return r==null||typeof r!="string"?!1:r===(0,Au.default)(r.trim())&&r.length>0}async function at(){let e=Math.random()*800+200;await new Promise(n=>setTimeout(n,e))}function De(r){return new Fr(Kh+r)}function lr(r){return new Fr(ii+r)}async function Vh(r){let t=Le(r),e=await $e.digest(t);return tt.encode(e.bytes).substring(1)}var Ss=class{components;init;log;self;constructor(t,e){if(this.components=t,this.log=t.logger.forComponent("libp2p:keychain"),this.init={...e,dek:{...Xo,...e.dek}},this.self=e.selfKey??"self",this.init.pass!=null&&this.init.pass?.length<20)throw new Error("pass must be least 20 characters");if(this.init.dek?.keyLength!=null&&this.init.dek.keyLength<_e.minKeyLength)throw new Error(`dek.keyLength must be least ${_e.minKeyLength} bytes`);if(this.init.dek?.salt?.length!=null&&this.init.dek.salt.length<_e.minSaltLength)throw new Error(`dek.saltLength must be least ${_e.minSaltLength} bytes`);if(this.init.dek?.iterationCount!=null&&this.init.dek.iterationCount<_e.minIterationCount)throw new Error(`dek.iterationCount must be least ${_e.minIterationCount}`);let n=this.init.pass!=null&&this.init.dek?.salt!=null?Pr(this.init.pass,this.init.dek?.salt,this.init.dek?.iterationCount,this.init.dek?.keyLength,this.init.dek?.hash):"";Qr.set(this,{dek:n})}[Symbol.toStringTag]="@libp2p/keychain";[hi]=["@libp2p/keychain"];static generateOptions(){let t=Object.assign({},this.options),e=Math.ceil(_e.minSaltLength/3)*3;return t.dek!=null&&(t.dek.salt=$(Ue(e),"base64")),t}static get options(){return{dek:{...Xo}}}async findKeyByName(t){if(!ur(t))throw await at(),new R(`Invalid key name '${t}'`);let e=lr(t);try{let n=await this.components.datastore.get(e);return JSON.parse($(n))}catch(n){throw await at(),this.log.error(n),new nn(`Key '${t}' does not exist.`)}}async findKeyById(t){try{let e={prefix:ii};for await(let n of this.components.datastore.query(e)){let s=JSON.parse($(n.value));if(s.id===t)return s}throw new R(`Key with id '${t}' does not exist.`)}catch(e){throw await at(),e}}async importKey(t,e){if(!ur(t))throw await at(),new R(`Invalid key name '${t}'`);if(e==null)throw await at(),new R("Key is required");let n=De(t);if(await this.components.datastore.has(n))throw await at(),new R(`Key '${t}' already exists`);let o,i;try{o=await Vh(e);let l=Qr.get(this);if(l==null)throw new R("dek missing");let f=l.dek;i=await si(e,f,e.type==="RSA"?"pkcs-8":"libp2p-key")}catch(l){throw await at(),l}let c={name:t,id:o},a=this.components.datastore.batch();return a.put(n,F(i)),a.put(lr(t),F(JSON.stringify(c))),await a.commit(),c}async exportKey(t){if(!ur(t))throw await at(),new R(`Invalid key name '${t}'`);let e=De(t);try{let n=await this.components.datastore.get(e),s=$(n),o=Qr.get(this);if(o==null)throw new R("dek missing");let i=o.dek;return await oi(s,i)}catch(n){throw await at(),n}}async removeKey(t){if(!ur(t)||t===this.self)throw await at(),new R(`Invalid key name '${t}'`);let e=De(t),n=await this.findKeyByName(t),s=this.components.datastore.batch();return s.delete(e),s.delete(lr(t)),await s.commit(),n}async listKeys(){let t={prefix:ii},e=[];for await(let n of this.components.datastore.query(t))e.push(JSON.parse($(n.value)));return e}async renameKey(t,e){if(!ur(t)||t===this.self)throw await at(),new R(`Invalid old key name '${t}'`);if(!ur(e)||e===this.self)throw await at(),new R(`Invalid new key name '${e}'`);let n=De(t),s=De(e),o=lr(t),i=lr(e);if(await this.components.datastore.has(s))throw await at(),new R(`Key '${e}' already exists`);try{let a=await this.components.datastore.get(n),l=await this.components.datastore.get(o),f=JSON.parse($(l));f.name=e;let u=this.components.datastore.batch();return u.put(s,a),u.put(i,F(JSON.stringify(f))),u.delete(n),u.delete(o),await u.commit(),f}catch(a){throw await at(),a}}async rotateKeychainPass(t,e){if(typeof t!="string")throw await at(),new R(`Invalid old pass type '${typeof t}'`);if(typeof e!="string")throw await at(),new R(`Invalid new pass type '${typeof e}'`);if(e.length<20)throw await at(),new R(`Invalid pass length ${e.length}`);this.log("recreating keychain");let n=Qr.get(this);if(n==null)throw new R("dek missing");let s=n.dek;this.init.pass=e;let o=e!=null&&this.init.dek?.salt!=null?Pr(e,this.init.dek.salt,this.init.dek?.iterationCount,this.init.dek?.keyLength,this.init.dek?.hash):"";Qr.set(this,{dek:o});let i=await this.listKeys();for(let c of i){let a=await this.components.datastore.get(De(c.name)),l=$(a),f=await oi(l,s),u=o.toString(),d=await si(f,u,f.type==="RSA"?"pkcs-8":"libp2p-key"),y=this.components.datastore.batch(),h={name:c.name,id:c.id};y.put(De(c.name),F(d)),y.put(lr(c.name),F(JSON.stringify(h))),await y.commit()}this.log("keychain reconstructed")}};function Oh(r={}){return t=>new Ss(t,r)}return Uu(Hh);})();
|
|
7
|
+
`)}async function oi(r,t){try{let e=await Rh(r,t);return bc(e)}catch{}if(!r.includes("BEGIN"))throw new R("Encrypted key was not a libp2p-key or a PEM file");return _h(r,t)}async function Rh(r,t){let e=be.decode(r);return ir.create().decrypt(e,t)}async function _h(r,t){let e=Q.get(),n;if(r.includes("-----BEGIN ENCRYPTED PRIVATE KEY-----")){let o=F(r.replace("-----BEGIN ENCRYPTED PRIVATE KEY-----","").replace("-----END ENCRYPTED PRIVATE KEY-----","").replace(/\n/g,"").trim(),"base64pad"),{result:i}=Bs(o),{iv:c,salt:a,iterations:l,keySize:f,cipherText:u}=Dh(i),d=await Ln(ae,t,a,{c:l,dkLen:f}),y=await e.subtle.importKey("raw",d,"AES-CBC",!1,["decrypt"]),h=Xr(await e.subtle.decrypt({name:"AES-CBC",iv:c},y,u)),{result:B}=Bs(h);n=Bu(B)}else if(r.includes("-----BEGIN PRIVATE KEY-----")){let o=F(r.replace("-----BEGIN PRIVATE KEY-----","").replace("-----END PRIVATE KEY-----","").replace(/\n/g,"").trim(),"base64pad"),{result:i}=Bs(o);n=Bu(i)}else throw new R("Could not parse private key from PEM data");let s=wc(n);if(s.type!=="RSA")throw new R("Could not parse RSA private key from PEM data");return s}function Dh(r){let t=r.valueBlock.value[0];if(t.valueBlock.value[0].toString()!=="OBJECT IDENTIFIER : 1.2.840.113549.1.5.13")throw new R("Only pkcs5PBES2 encrypted private keys are supported");let n=t.valueBlock.value[1].valueBlock.value[0];if(n.valueBlock.value[0].toString()!=="OBJECT IDENTIFIER : 1.2.840.113549.1.5.12")throw new R("Only pkcs5PBKDF2 key derivation functions are supported");let o=n.valueBlock.value[1],i=Xr(o.valueBlock.value[0].getValue()),c=1e4,a=32;if(o.valueBlock.value.length===3)c=Number(o.valueBlock.value[1].toBigInt()),a=Number(o.valueBlock.value[2].toBigInt());else if(o.valueBlock.value.length===2)throw new R("Could not derive key size and iterations from PEM file - please use @libp2p/rsa to re-import your key");let l=t.valueBlock.value[1].valueBlock.value[1],f=l.valueBlock.value[0].toString();if(f!=="OBJECT IDENTIFIER : 1.2.840.113549.3.7"){if(f!=="OBJECT IDENTIFIER : 1.3.14.3.2.7"){if(f!=="OBJECT IDENTIFIER : 2.16.840.1.101.3.4.1.2"){if(f!=="OBJECT IDENTIFIER : 2.16.840.1.101.3.4.1.22"){if(f!=="OBJECT IDENTIFIER : 2.16.840.1.101.3.4.1.42")throw new R("Only AES-CBC encryption schemes are supported")}}}}let u=Xr(l.valueBlock.value[1].getValue());return{cipherText:Xr(r.valueBlock.value[1].getValue()),salt:i,iterations:c,keySize:a,iv:u}}function Bu(r){return Xr(r.valueBlock.value[2].getValue())}function Xr(r){return new Uint8Array(r,0,r.byteLength)}var Kh="/pkcs8/",ii="/info/",Qr=new WeakMap,_e={minKeyLength:112/8,minSaltLength:128/8,minIterationCount:1e3};function ur(r){return r==null||typeof r!="string"?!1:r===(0,Au.default)(r.trim())&&r.length>0}async function at(){let e=Math.random()*800+200;await new Promise(n=>setTimeout(n,e))}function De(r){return new Fr(Kh+r)}function lr(r){return new Fr(ii+r)}async function Vh(r){let t=Le(r),e=await $e.digest(t);return tt.encode(e.bytes).substring(1)}var Ss=class{components;init;log;self;constructor(t,e){if(this.components=t,this.log=t.logger.forComponent("libp2p:keychain"),this.init={...e,dek:{...Xo,...e.dek}},this.self=e.selfKey??"self",this.init.pass!=null&&this.init.pass?.length<20)throw new Error("pass must be least 20 characters");if(this.init.dek?.keyLength!=null&&this.init.dek.keyLength<_e.minKeyLength)throw new Error(`dek.keyLength must be least ${_e.minKeyLength} bytes`);if(this.init.dek?.salt?.length!=null&&this.init.dek.salt.length<_e.minSaltLength)throw new Error(`dek.saltLength must be least ${_e.minSaltLength} bytes`);if(this.init.dek?.iterationCount!=null&&this.init.dek.iterationCount<_e.minIterationCount)throw new Error(`dek.iterationCount must be least ${_e.minIterationCount}`);let n=this.init.pass!=null&&this.init.dek?.salt!=null?Pr(this.init.pass,this.init.dek?.salt,this.init.dek?.iterationCount,this.init.dek?.keyLength,this.init.dek?.hash):"";Qr.set(this,{dek:n})}[Symbol.toStringTag]="@libp2p/keychain";[hi]=["@libp2p/keychain"];static generateOptions(){let t=Object.assign({},this.options),e=Math.ceil(_e.minSaltLength/3)*3;return t.dek!=null&&(t.dek.salt=$(Ue(e),"base64")),t}static get options(){return{dek:{...Xo}}}async findKeyByName(t){if(!ur(t))throw await at(),new R(`Invalid key name '${t}'`);let e=lr(t);try{let n=await this.components.datastore.get(e);return JSON.parse($(n))}catch(n){throw await at(),this.log.error("could not read key from datastore - %e",n),new nn(`Key '${t}' does not exist.`)}}async findKeyById(t){try{let e={prefix:ii};for await(let n of this.components.datastore.query(e)){let s=JSON.parse($(n.value));if(s.id===t)return s}throw new R(`Key with id '${t}' does not exist.`)}catch(e){throw await at(),e}}async importKey(t,e){if(!ur(t))throw await at(),new R(`Invalid key name '${t}'`);if(e==null)throw await at(),new R("Key is required");let n=De(t);if(await this.components.datastore.has(n))throw await at(),new R(`Key '${t}' already exists`);let o,i;try{o=await Vh(e);let l=Qr.get(this);if(l==null)throw new R("dek missing");let f=l.dek;i=await si(e,f,e.type==="RSA"?"pkcs-8":"libp2p-key")}catch(l){throw await at(),l}let c={name:t,id:o},a=this.components.datastore.batch();return a.put(n,F(i)),a.put(lr(t),F(JSON.stringify(c))),await a.commit(),c}async exportKey(t){if(!ur(t))throw await at(),new R(`Invalid key name '${t}'`);let e=De(t);try{let n=await this.components.datastore.get(e),s=$(n),o=Qr.get(this);if(o==null)throw new R("dek missing");let i=o.dek;return await oi(s,i)}catch(n){throw await at(),n}}async removeKey(t){if(!ur(t)||t===this.self)throw await at(),new R(`Invalid key name '${t}'`);let e=De(t),n=await this.findKeyByName(t),s=this.components.datastore.batch();return s.delete(e),s.delete(lr(t)),await s.commit(),n}async listKeys(){let t={prefix:ii},e=[];for await(let n of this.components.datastore.query(t))e.push(JSON.parse($(n.value)));return e}async renameKey(t,e){if(!ur(t)||t===this.self)throw await at(),new R(`Invalid old key name '${t}'`);if(!ur(e)||e===this.self)throw await at(),new R(`Invalid new key name '${e}'`);let n=De(t),s=De(e),o=lr(t),i=lr(e);if(await this.components.datastore.has(s))throw await at(),new R(`Key '${e}' already exists`);try{let a=await this.components.datastore.get(n),l=await this.components.datastore.get(o),f=JSON.parse($(l));f.name=e;let u=this.components.datastore.batch();return u.put(s,a),u.put(i,F(JSON.stringify(f))),u.delete(n),u.delete(o),await u.commit(),f}catch(a){throw await at(),a}}async rotateKeychainPass(t,e){if(typeof t!="string")throw await at(),new R(`Invalid old pass type '${typeof t}'`);if(typeof e!="string")throw await at(),new R(`Invalid new pass type '${typeof e}'`);if(e.length<20)throw await at(),new R(`Invalid pass length ${e.length}`);this.log("recreating keychain");let n=Qr.get(this);if(n==null)throw new R("dek missing");let s=n.dek;this.init.pass=e;let o=e!=null&&this.init.dek?.salt!=null?Pr(e,this.init.dek.salt,this.init.dek?.iterationCount,this.init.dek?.keyLength,this.init.dek?.hash):"";Qr.set(this,{dek:o});let i=await this.listKeys();for(let c of i){let a=await this.components.datastore.get(De(c.name)),l=$(a),f=await oi(l,s),u=o.toString(),d=await si(f,u,f.type==="RSA"?"pkcs-8":"libp2p-key"),y=this.components.datastore.batch(),h={name:c.name,id:c.id};y.put(De(c.name),F(d)),y.put(lr(c.name),F(JSON.stringify(h))),await y.commit()}this.log("keychain reconstructed")}};function Oh(r={}){return t=>new Ss(t,r)}return Uu(Hh);})();
|
|
8
8
|
/*! Bundled license information:
|
|
9
9
|
|
|
10
10
|
pvtsutils/build/index.js:
|