/* esm.sh - @dfinity/identity@1.4.0 */
import{bufEquals as z}from"/@dfinity/agent@1.4.0/X-ZEBkZmluaXR5L2NhbmRpZEAxLjQuMCxAZGZpbml0eS9wcmluY2lwYWxAMS40LjA/es2022/agent.mjs";import{SignIdentity as Y,uint8ToBuf as $,ED25519_OID as R,unwrapDER as Q,wrapDER as X,fromHex as b,toHex as U,bufFromBufLike as Z}from"/@dfinity/agent@1.4.0/X-ZEBkZmluaXR5L2NhbmRpZEAxLjQuMCxAZGZpbml0eS9wcmluY2lwYWxAMS40LjA/es2022/agent.mjs";import{ed25519 as p}from"/@noble/curves@^1.2.0/ed25519?target=es2022";var _=function(n,e,t,r,i){if(r==="m")throw new TypeError("Private method is not writable");if(r==="a"&&!i)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?n!==e||!i:!e.has(n))throw new TypeError("Cannot write private member to an object whose class did not declare it");return r==="a"?i.call(n,t):i?i.value=t:e.set(n,t),t},c=function(n,e,t,r){if(t==="a"&&!r)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?n!==e||!r:!e.has(n))throw new TypeError("Cannot read private member from an object whose class did not declare it");return t==="m"?r:t==="a"?r.call(n):r?r.value:e.get(n)},g,m,d,f;function N(n){return n!==null&&typeof n=="object"}var l=class n{constructor(e){if(g.set(this,void 0),m.set(this,void 0),e.byteLength!==n.RAW_KEY_LENGTH)throw new Error("An Ed25519 public key must be exactly 32bytes long");_(this,g,e,"f"),_(this,m,n.derEncode(e),"f")}static from(e){if(typeof e=="string"){let t=b(e);return this.fromRaw(t)}else if(N(e)){let t=e;if(N(t)&&Object.hasOwnProperty.call(t,"__derEncodedPublicKey__"))return this.fromDer(t);if(ArrayBuffer.isView(t)){let r=t;return this.fromRaw(Z(r.buffer))}else{if(t instanceof ArrayBuffer)return this.fromRaw(t);if("rawKey"in t)return this.fromRaw(t.rawKey);if("derKey"in t)return this.fromDer(t.derKey);if("toDer"in t)return this.fromDer(t.toDer())}}throw new Error("Cannot construct Ed25519PublicKey from the provided key.")}static fromRaw(e){return new n(e)}static fromDer(e){return new n(this.derDecode(e))}static derEncode(e){let t=X(e,R).buffer;return t.__derEncodedPublicKey__=void 0,t}static derDecode(e){let t=Q(e,R);if(t.length!==this.RAW_KEY_LENGTH)throw new Error("An Ed25519 public key must be exactly 32bytes long");return t}get rawKey(){return c(this,g,"f")}get derKey(){return c(this,m,"f")}toDer(){return this.derKey}toRaw(){return this.rawKey}};g=new WeakMap,m=new WeakMap;l.RAW_KEY_LENGTH=32;var A=class n extends Y{constructor(e,t){super(),d.set(this,void 0),f.set(this,void 0),_(this,d,l.from(e),"f"),_(this,f,new Uint8Array(t),"f")}static generate(e){if(e&&e.length!==32)throw new Error("Ed25519 Seed needs to be 32 bytes long.");e||(e=p.utils.randomPrivateKey()),z(e,new Uint8Array(new Array(32).fill(0)))&&console.warn("Seed is all zeros. This is not a secure seed. Please provide a seed with sufficient entropy if this is a production environment.");let t=new Uint8Array(32);for(let i=0;i<32;i++)t[i]=new Uint8Array(e)[i];let r=p.getPublicKey(t);return n.fromKeyPair(r,t)}static fromParsedJson(e){let[t,r]=e;return new n(l.fromDer(b(t)),b(r))}static fromJSON(e){let t=JSON.parse(e);if(Array.isArray(t)){if(typeof t[0]=="string"&&typeof t[1]=="string")return this.fromParsedJson([t[0],t[1]]);throw new Error("Deserialization error: JSON must have at least 2 items.")}throw new Error(`Deserialization error: Invalid JSON type for string: ${JSON.stringify(e)}`)}static fromKeyPair(e,t){return new n(l.fromRaw(e),t)}static fromSecretKey(e){let t=p.getPublicKey(new Uint8Array(e));return n.fromKeyPair(t,e)}toJSON(){return[U(c(this,d,"f").toDer()),U(c(this,f,"f"))]}getKeyPair(){return{secretKey:c(this,f,"f"),publicKey:c(this,d,"f")}}getPublicKey(){return c(this,d,"f")}async sign(e){let t=new Uint8Array(e),r=$(p.sign(t,c(this,f,"f").slice(0,32)));return Object.defineProperty(r,"__signature__",{enumerable:!1,value:void 0}),r}static verify(e,t,r){let[i,o,s]=[e,t,r].map(a=>(typeof a=="string"&&(a=b(a)),a instanceof Uint8Array&&(a=a.buffer),new Uint8Array(a)));return p.verify(o,i,s)}};d=new WeakMap,f=new WeakMap;import{SignIdentity as ee}from"/@dfinity/agent@1.4.0/X-ZEBkZmluaXR5L2NhbmRpZEAxLjQuMCxAZGZpbml0eS9wcmluY2lwYWxAMS40LjA/es2022/agent.mjs";var D=class n extends Error{constructor(e){super(e),this.message=e,Object.setPrototypeOf(this,n.prototype)}};function H(n){if(typeof globalThis<"u"&&globalThis.crypto&&globalThis.crypto.subtle)return globalThis.crypto.subtle;if(n)return n;if(typeof crypto<"u"&&crypto.subtle)return crypto.subtle;throw new D("Global crypto was not available and none was provided. Please inlcude a SubtleCrypto implementation. See https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto")}var J=class n extends ee{constructor(e,t,r){super(),this._keyPair=e,this._derKey=t,this._subtleCrypto=r}static async generate(e){let{extractable:t=!1,keyUsages:r=["sign","verify"],subtleCrypto:i}=e??{},o=H(i),s=await o.generateKey({name:"ECDSA",namedCurve:"P-256"},t,r),a=await o.exportKey("spki",s.publicKey);return new this(s,a,o)}static async fromKeyPair(e,t){let r=H(t),i=await r.exportKey("spki",e.publicKey);return new n(e,i,r)}getKeyPair(){return this._keyPair}getPublicKey(){let e=this._derKey,t=Object.create(this._keyPair.publicKey);return t.toDer=function(){return e},t}async sign(e){let t={name:"ECDSA",hash:{name:"SHA-256"}};return this._keyPair.privateKey,await this._subtleCrypto.sign(t,this._keyPair.privateKey,e)}};import{fromHex as ae,requestIdOf as G,SignIdentity as se,toHex as E}from"/@dfinity/agent@1.4.0/X-ZEBkZmluaXR5L2NhbmRpZEAxLjQuMCxAZGZpbml0eS9wcmluY2lwYWxAMS40LjA/es2022/agent.mjs";import{Principal as O}from"/@dfinity/principal@1.4.0/es2022/principal.mjs";import*as y from"/simple-cbor?target=es2022";import{Principal as re}from"/@dfinity/principal@1.4.0/es2022/principal.mjs";var te=function(n,e,t,r,i){if(r==="m")throw new TypeError("Private method is not writable");if(r==="a"&&!i)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?n!==e||!i:!e.has(n))throw new TypeError("Cannot write private member to an object whose class did not declare it");return r==="a"?i.call(n,t):i?i.value=t:e.set(n,t),t},h=function(n,e,t,r){if(t==="a"&&!r)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?n!==e||!r:!e.has(n))throw new TypeError("Cannot read private member from an object whose class did not declare it");return t==="m"?r:t==="a"?r.call(n):r?r.value:e.get(n)},u,K=class{constructor(e){u.set(this,void 0),te(this,u,e,"f")}get rawKey(){return h(this,u,"f").rawKey}get derKey(){return h(this,u,"f").derKey}toDer(){return h(this,u,"f").toDer()}getPublicKey(){return h(this,u,"f")}getPrincipal(){return re.from(h(this,u,"f").rawKey)}transformRequest(){return Promise.reject("Not implemented. You are attempting to use a partial identity to sign calls, but this identity only has access to the public key.To sign calls, use a DelegationIdentity instead.")}};u=new WeakMap;var ne=function(n,e,t,r,i){if(r==="m")throw new TypeError("Private method is not writable");if(r==="a"&&!i)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?n!==e||!i:!e.has(n))throw new TypeError("Cannot write private member to an object whose class did not declare it");return r==="a"?i.call(n,t):i?i.value=t:e.set(n,t),t},ie=function(n,e,t,r){if(t==="a"&&!r)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?n!==e||!r:!e.has(n))throw new TypeError("Cannot read private member from an object whose class did not declare it");return t==="m"?r:t==="a"?r.call(n):r?r.value:e.get(n)},oe=function(n,e){var t={};for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&e.indexOf(r)<0&&(t[r]=n[r]);if(n!=null&&typeof Object.getOwnPropertySymbols=="function")for(var i=0,r=Object.getOwnPropertySymbols(n);i<r.length;i++)e.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(n,r[i])&&(t[r[i]]=n[r[i]]);return t},v,ce=new TextEncoder().encode("ic-request-auth-delegation"),ue=new TextEncoder().encode(`
ic-request`);function S(n){if(typeof n!="string"||n.length<64)throw new Error("Invalid public key.");return ae(n)}var P=class{constructor(e,t,r){this.pubkey=e,this.expiration=t,this.targets=r}toCBOR(){return y.value.map(Object.assign({pubkey:y.value.bytes(this.pubkey),expiration:y.value.u64(this.expiration.toString(16),16)},this.targets&&{targets:y.value.array(this.targets.map(e=>y.value.bytes(e.toUint8Array())))}))}toJSON(){return Object.assign({expiration:this.expiration.toString(16),pubkey:E(this.pubkey)},this.targets&&{targets:this.targets.map(e=>e.toHex())})}};async function le(n,e,t,r){let i=new P(e.toDer(),BigInt(+t)*BigInt(1e6),r),o=new Uint8Array([...ce,...new Uint8Array(G(i))]),s=await n.sign(o);return{delegation:i,signature:s}}var B=class n{constructor(e,t){this.delegations=e,this.publicKey=t}static async create(e,t,r=new Date(Date.now()+900*1e3),i={}){var o,s;let a=await le(e,t,r,i.targets);return new n([...((o=i.previous)===null||o===void 0?void 0:o.delegations)||[],a],((s=i.previous)===null||s===void 0?void 0:s.publicKey)||e.getPublicKey().toDer())}static fromJSON(e){let{publicKey:t,delegations:r}=typeof e=="string"?JSON.parse(e):e;if(!Array.isArray(r))throw new Error("Invalid delegations.");let i=r.map(o=>{let{delegation:s,signature:a}=o,{pubkey:x,expiration:V,targets:w}=s;if(w!==void 0&&!Array.isArray(w))throw new Error("Invalid targets.");return{delegation:new P(S(x),BigInt("0x"+V),w&&w.map(j=>{if(typeof j!="string")throw new Error("Invalid target.");return O.fromHex(j)})),signature:S(a)}});return new this(i,S(t))}static fromDelegations(e,t){return new this(e,t)}toJSON(){return{delegations:this.delegations.map(e=>{let{delegation:t,signature:r}=e,{targets:i}=t;return{delegation:Object.assign({expiration:t.expiration.toString(16),pubkey:E(t.pubkey)},i&&{targets:i.map(o=>o.toHex())}),signature:E(r)}}),publicKey:E(this.publicKey)}}},F=class extends se{constructor(e,t){super(),this._inner=e,this._delegation=t}static fromDelegation(e,t){return new this(e,t)}getDelegation(){return this._delegation}getPublicKey(){return{derKey:this._delegation.publicKey,toDer:()=>this._delegation.publicKey}}sign(e){return this._inner.sign(e)}async transformRequest(e){let{body:t}=e,r=oe(e,["body"]),i=await G(t);return Object.assign(Object.assign({},r),{body:{content:t,sender_sig:await this.sign(new Uint8Array([...ue,...new Uint8Array(i)])),sender_delegation:this._delegation.delegations,sender_pubkey:this._delegation.publicKey}})}},W=class n extends K{constructor(e,t){super(e),v.set(this,void 0),ne(this,v,t,"f")}get delegation(){return ie(this,v,"f")}static fromDelegation(e,t){return new n(e,t)}};v=new WeakMap;function Te(n,e){for(let{delegation:i}of n.delegations)if(+new Date(Number(i.expiration/BigInt(1e6)))<=+Date.now())return!1;let t=[],r=e?.scope;r&&(Array.isArray(r)?t.push(...r.map(i=>typeof i=="string"?O.fromText(i):i)):t.push(typeof r=="string"?O.fromText(r):r));for(let i of t){let o=i.toText();for(let{delegation:s}of n.delegations){if(s.targets===void 0)continue;let a=!0;for(let x of s.targets)if(x.toText()===o){a=!1;break}if(a)return!1}}return!0}import{SignIdentity as de,wrapDER as fe,DER_COSE_OID as ye,fromHex as L,toHex as q}from"/@dfinity/agent@1.4.0/X-ZEBkZmluaXR5L2NhbmRpZEAxLjQuMCxAZGZpbml0eS9wcmluY2lwYWxAMS40LjA/es2022/agent.mjs";import I from"/borc@^2.1.1?target=es2022";import{randomBytes as pe}from"/@noble/hashes@^1.3.1/utils?target=es2022";import{bufFromBufLike as he}from"/@dfinity/candid@1.4.0/X-ZEBkZmluaXR5L3ByaW5jaXBhbEAxLjQuMA/es2022/candid.mjs";function we(n){return fe(n,ye).buffer}function ge(n){let e=new DataView(new ArrayBuffer(2)),t=n.slice(53,55);[...new Uint8Array(t)].forEach((i,o)=>e.setUint8(o,i));let r=e.getUint16(0);return n.slice(55+r)}var T=class{constructor(e){this._cose=e,this._encodedKey=we(e)}toDer(){return this._encodedKey}getCose(){return this._cose}};function me(n="<ic0.app>"){return typeof n=="string"?Uint8Array.from(n,e=>e.charCodeAt(0)):n}async function be(n){let e=await navigator.credentials.create(n??{publicKey:{authenticatorSelection:{userVerification:"preferred"},attestation:"direct",challenge:me(),pubKeyCredParams:[{type:"public-key",alg:C.ECDSA_WITH_SHA256}],rp:{name:"Internet Identity Service"},user:{id:pe(16),name:"Internet Identity",displayName:"Internet Identity"}}});return e===null?null:{id:e.id,response:e.response,type:e.type,authenticatorAttachment:e.authenticatorAttachment,getClientExtensionResults:e.getClientExtensionResults,rawId:he(e.rawId)}}var C;(function(n){n[n.ECDSA_WITH_SHA256=-7]="ECDSA_WITH_SHA256"})(C||(C={}));var k=class extends de{constructor(e,t,r){super(),this.rawId=e,this.authenticatorAttachment=r,this._publicKey=new T(t)}static fromJSON(e){let{publicKey:t,rawId:r}=JSON.parse(e);if(typeof t!="string"||typeof r!="string")throw new Error("Invalid JSON string.");return new this(L(r),L(t),void 0)}static async create(e){var t;let r=await be(e);if(!r||r.type!=="public-key")throw new Error("Could not create credentials.");let i=r.response;if(i.attestationObject===void 0)throw new Error("Was expecting an attestation response.");let o=I.decodeFirst(new Uint8Array(i.attestationObject));return new this(r.rawId,ge(o.authData),(t=r.authenticatorAttachment)!==null&&t!==void 0?t:void 0)}getPublicKey(){return this._publicKey}getAuthenticatorAttachment(){return this.authenticatorAttachment}async sign(e){let t=await navigator.credentials.get({publicKey:{allowCredentials:[{type:"public-key",id:this.rawId}],challenge:e,userVerification:"preferred"}});t.authenticatorAttachment!==null&&(this.authenticatorAttachment=t.authenticatorAttachment);let r=t.response,i=I.encode(new I.Tagged(55799,{authenticator_data:new Uint8Array(r.authenticatorData),client_data_json:new TextDecoder().decode(r.clientDataJSON),signature:new Uint8Array(r.signature)}));if(!i)throw new Error("failed to encode cbor");return i.buffer}toJSON(){return{publicKey:q(this._publicKey.getCose()),rawId:q(this.rawId)}}};import{wrapDER as qe,unwrapDER as Me,DER_COSE_OID as Ve,ED25519_OID as ze}from"/@dfinity/agent@1.4.0/X-ZEBkZmluaXR5L2NhbmRpZEAxLjQuMCxAZGZpbml0eS9wcmluY2lwYWxAMS40LjA/es2022/agent.mjs";var M=class{constructor(){throw new Error("Secp256k1KeyIdentity has been moved to a new repo: @dfinity/identity-secp256k1")}};export{D as CryptoError,Ve as DER_COSE_OID,P as Delegation,B as DelegationChain,F as DelegationIdentity,J as ECDSAKeyIdentity,ze as ED25519_OID,A as Ed25519KeyIdentity,l as Ed25519PublicKey,W as PartialDelegationIdentity,K as PartialIdentity,M as Secp256k1KeyIdentity,k as WebAuthnIdentity,Te as isDelegationValid,Me as unwrapDER,qe as wrapDER};
//# sourceMappingURL=identity.mjs.map