@nexus-cross/crossx-sdk-core 2.1.2-beta.2 → 2.1.3
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/dist/crossx.global +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +2 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
보안을 위해 동일한 Google/Apple 계정으로만 이어서 사용할 수 있습니다.`,sessionAlert_title:"세션 만료",sessionAlert_accountLabel:"계정",sessionAlert_appleAccount:"Apple 계정",sessionAlert_signOutButton:"로그아웃",sessionAlert_signInAgainButton:"다시 로그인"},_r={confirm:"Confirm",cancel:"Cancel",close:"Close",signature_requesting:" is requesting a Signature",signature_warning:"After you sign, changes or cancellations are not possible.",signature_signButton:"Sign",signMessage_title:"Signature Request",signTypedData_title:"Signature Request",signTransaction_title:"Signature Request",sendTransaction_title:"Approve transaction",sendTx_permissionText:" wants your permission to approve the following transaction.",sendTx_approveButton:"Approve",tx_pending:"Processing transaction...",tx_success:"Transaction complete",tx_failed:"Transaction failed",tx_timeout:"Transaction timeout",txProgress_waitingTitle:"Waiting for confirmation",txProgress_waitingText:"Waiting for transaction receipt...",txComplete_doneButton:"All Done",label_to:"To",label_from:"From",label_network:"Network",label_value:"Value",label_estimatedFee:"Estimated fee",label_estTxFee:"Est. Tx Fee",label_maxPriorityFee:"Max Priority Fee",label_maxGasFee:"Max Gas Fee",label_gasPrice:"Gas Price",label_gasLimit:"Gas Limit",label_transfer:"Transfer",label_txFee:"Tx Fee",label_txHash:"Tx Hash",label_total:"Total",label_includingFees:"(including fees)",label_primaryType:"Primary Type",loginSelector_title:"Choose a sign-in method",loginSelector_subtitle:"Sign in instantly with your social account",loginSelector_google:"Continue with Google",loginSelector_apple:"Continue with Apple",loginSelector_or:"or",loginSelector_termsTemplate:"By continuing, you agree to NEXUS {tos} and consent to its {pp}",loginSelector_termsTos:"Terms of Service",loginSelector_termsPp:"Privacy Policy.",loginSelector_connectOtherWallets:"Connect with Other Wallets",loginSelector_connectOtherWalletsDesc:"Connect your existing Web3 wallet to manage your assets.",migration_foundTitle:"Wallet Found on Social Account",migration_foundDescription:`We found a wallet linked to your social account.
|
|
5
5
|
Enter your 4-digit PIN to restore your assets.`,migration_recoverButton:"Import from Social Backup",migration_skipButton:"Skip for Now",recoveryPinInput_title:"Recover My Wallet",recoveryPinInput_placeholder:"Enter your 4-digit PIN to recover your wallet.",recoveryPinInput_error:"Incorrect PIN. Please try again.",walletSelector_title:"Select Connected Wallet",walletSelector_description:"Please select the wallet you previously used for this game to continue.",walletSelector_infoTitle:"Why do I need to select a wallet?",walletSelector_infoDesc:"Identifying your previously linked wallet ensures your data and assets are synced correctly.",walletSelector_addButton:"add a wallet",walletSelector_selected:"Selected",walletSelector_addError_limitExceeded:"Account limit reached. Maximum {limit} accounts allowed.",walletSelector_addError_generic:"Failed to add a new account. Please try again.",pinNotice_headerSubtitle:"Required for transactions, PK/mnemonic export & account deletion.",pinNotice_title:"PIN Setup Notice",pinNotice_description:"For your security, this PIN cannot be reset or recovered. Please make sure you remember it or store it in a safe place.",pinNotice_check1:"I understand that losing this PIN will result in permanent loss of access to my wallet and assets.",pinNotice_check2:"I understand that no one, including the support team, can recover or reset this PIN on my behalf.",pinNotice_nextButton:"Next",pinNotice_submitButton:"I Understand",pinSetup_title:"Create PIN",pinSetup_headerSubtitle:"This PIN is used to authorize your transactions.",pinSetup_subtitle:"Set a 6-digit PIN to authorize transactions.",pinSetup_confirmTitle:"Confirm PIN",pinSetup_confirmSubtitle:"Enter your PIN again to confirm.",pinSetup_mismatchError:"PINs do not match. Please try again.",pinValidation_tooShort:"Please enter a 6-digit PIN.",pinValidation_numbersOnly:"Only numbers (0-9) are allowed.",pinValidation_repeatingDigit:"Cannot use the same number 3+ times in a row.",pinValidation_sequential:"Sequential numbers (e.g., 123456) are not allowed.",pinValidation_alternatingPattern:"Repeating patterns (e.g., 121212) are not allowed.",pinInput_title:"Enter PIN",pinInput_subtitle:"Enter your 6-digit PIN to continue.",pinInput_error:"Incorrect PIN. Please try again.",verifyPin_title:"Verify Your PIN",verifyPin_subtitle:"To continue, please confirm your PIN.",pinLocked_message:"Please try again after the lock expires.",pinLocked_availableFrom:"Available from",pinLocked_failureWarning:"Your account will be locked after further failures.",pinLocked_permanent:"Your account has been permanently locked due to too many failed attempts.",alert_sessionExpired:"Your session has expired. Please sign in again.",alert_accountWithdrawn:"This account is no longer available or has been withdrawn. Please sign in again.",alert_walletNotFound:"Wallet data could not be found. Please sign in again.",alert_differentAccount:`You signed in with a different Google/Apple account than before.
|
|
6
6
|
|
|
7
|
-
For security, you must continue with the same account.`,sessionAlert_title:"Session expired",sessionAlert_accountLabel:"Account",sessionAlert_appleAccount:"Apple account",sessionAlert_signOutButton:"Sign out",sessionAlert_signInAgainButton:"Sign in again"},ds={ko:ls,en:_r};function xe(s="en",e){return ds[s]??_r}const Mt="crossx_access_token",Bt="crossx_refresh_token",Ft="crossx_user_info",Qe=class Qe{constructor(e,r,t,n,o,i,a){this.config=e,this.storage=r,this.crypto=t,this.oauth=n,this.transport=o,this.walletProvider=i,this.tokenStore=a,this._refreshPromise=null,this._migrated=!1;const c=e.projectId;this.STORAGE_KEY_ACCESS_TOKEN=`crossx_${c}_access_token`,this.STORAGE_KEY_REFRESH_TOKEN=`crossx_${c}_refresh_token`,this.STORAGE_KEY_USER=`crossx_${c}_user_info`}get useCookieAuth(){return this.config.authMode==="cookie"}extractResponseErrorCode(e){if(e!=null&&e.code&&e.code!==200&&e.code>0)return{code:e.code,message:e.message??""};const r=e==null?void 0:e.data;if(typeof r=="object"&&r!==null&&"code"in r){const t=r;if(t.code&&t.code!==200&&t.code>0)return{code:t.code,message:t.message??""}}return null}checkResponseError(e,r){const t=this.extractResponseErrorCode(e);if(t)throw new g(h.AUTH_FAILED,`${r} 실패 (코드 ${t.code}): ${t.message}`)}async execute(e){let r,t;try{const n=e==null?void 0:e.provider;let o="/login";n==="google"?o="/google":n==="apple"&&(o="/apple");const{oauthServiceUrl:i}=this.config,a=`${i}${o}`;u.log(`[CROSSx] OAuth 팝업 열기 (${n||"일반"} 로그인)`);const c=await this.oauth.openAuth({authUrl:a,expectedOrigin:new URL(i).origin});r=c.token,t=c.email,u.log("[CROSSx] OAuth Firebase 토큰 받음 (length:",r.length,")"),t&&u.log("[CROSSx] OAuth 콜백 email:",t)}catch(n){u.error("[CROSSx] SignIn 에러 (OAuth 단계):",n);const o=n instanceof Error?n.message:"Sign in failed";throw/팝업|popup/i.test(o)?new g(h.OAUTH_POPUP_BLOCKED,o):new g(h.AUTH_FAILED,o)}return this.processFirebaseToken(r,t)}async executeWithOAuthToken(e){return u.log("[CROSSx] signInWithOAuthToken — Firebase 토큰 주입 (length:",e.length,")"),this.processFirebaseToken(e)}async processFirebaseToken(e,r){var o,i,a,c,l;let t,n=!1;try{const{authApiUrl:d}=this.config,{accessToken:p,refreshToken:_}=await this.exchangeFirebaseToken(e,d);let f,x,w;try{const k=this.crypto.decodeJWT(e);u.log("[CROSSx] Firebase JWT 필드:",Object.keys(k).join(", ")),x=(o=k.firebase)==null?void 0:o.sign_in_provider;const O=((i=k.firebase)==null?void 0:i.identities)??{};w=k.email??((a=O.email)==null?void 0:a[0]),x==="google.com"?f=(c=O["google.com"])==null?void 0:c[0]:x==="apple.com"&&(f=(l=O["apple.com"])==null?void 0:l[0]),u.log("[CROSSx] OAuth provider sub 추출 — provider:",x,"hasProviderSub:",!!f,"email:",w??"(없음)")}catch{u.warn("[CROSSx] firebaseToken에서 providerSub 추출 실패")}if(p){const k=this.crypto.decodeJWT(p);u.log("[CROSSx] access_token 디코딩 — sub:",k.sub,"exp:",k.exp,"필드:",Object.keys(k).join(", "));const O=await this.crypto.verifyJWT(p);if(n=O.signatureVerified??!1,!O.valid)throw u.error("[CROSSx] access_token 검증 실패"),new Error("유효하지 않은 access token");const P=O.payload,L=P.email??w??r;u.log("[CROSSx] email 소스 — CROSSx JWT:",P.email??"(없음)","/ Firebase JWT:",w??"(없음)","/ OAuth 콜백:",r??"(없음)","→",L??"(없음)"),t={id:P.sub,email:L,signInProvider:x,providerSub:f},this.tokenStore.set(p),this.useCookieAuth||(await this.storage.set(this.STORAGE_KEY_ACCESS_TOKEN,p),_&&this.config.secureStorageAvailable!==!1?await this.storage.set(this.STORAGE_KEY_REFRESH_TOKEN,_):_&&u.warn("[CROSSx] 안전한 스토리지 미사용 — refresh_token 영속 저장을 건너뜁니다"))}else{const k=this.crypto.decodeJWT(e);t={id:k.sub,email:k.email??r,signInProvider:x,providerSub:f},u.log("[CROSSx] Cookie 모드 — Firebase 토큰에서 사용자 정보 추출 — id:",t.id)}u.log("[CROSSx] 사용자 정보 — id:",t.id,"email:",t.email??"(없음)");const E=this.useCookieAuth?{id:t.id,email:t.email,signInProvider:t.signInProvider,providerSub:t.providerSub}:t;await this.storage.set(this.STORAGE_KEY_USER,E),u.log("[CROSSx] 사용자 정보 저장 완료 (authMode:",this.useCookieAuth?"cookie":"token",")")}catch(d){return u.error("[CROSSx] SignIn 에러 (토큰 교환 단계):",d),{success:!1,error:d instanceof Error?d.message:"Sign in failed"}}return u.log("[CROSSx][Migration Phase 1] 로그인 완료, 지갑 로드 시작 — userId:",t.id),this.loadWallet(t,n)}async exchangeFirebaseToken(e,r){const t=this.useCookieAuth,n=t?`${r}/cross-auth/social/login/cookie`:`${r}/cross-auth/social/login`;u.log("[CROSSx] Firebase 토큰 교환 요청");const o=await this.transport.request({url:n,method:"POST",headers:{"Content-Type":"application/json"},body:{auth_code:e,login_type:"firebase"},...t?{credentials:"include"}:{}});u.log("[CROSSx] 토큰 교환 응답 — status:",o.status);const i=o.data;this.checkResponseError(i,"Token exchange");const a=this.extractAccessToken(i);if(t&&!a)return u.log("[CROSSx] Cookie 모드 — 로그인 성공 (JWT는 HttpOnly 쿠키)"),{};if(!a)throw new g(h.AUTH_FAILED,"토큰 교환 응답에서 access_token을 찾을 수 없습니다");u.log("[CROSSx] access_token 교환 성공");let c;return t||(c=this.extractRefreshToken(i)),{accessToken:a,refreshToken:c}}extractAccessToken(e){const r=(e==null?void 0:e.data)??e;if(this.isJwtString(r))return r;if(typeof r=="object"&&r!==null){const t=r;if(this.isJwtString(t.data))return t.data;if(typeof t.data=="object"&&t.data!==null){const o=t.data,i=o.access_token??o.token;if(typeof i=="string")return i}const n=t.access_token??t.token;if(typeof n=="string")return n}}extractRefreshToken(e){const r=(e==null?void 0:e.data)??e;if(typeof r!="object"||r===null)return;const t=r;if(typeof t.data=="object"&&t.data!==null){const o=t.data,i=o.refresh_token??o.refresh;if(typeof i=="string")return i}const n=t.refresh_token??t.refresh;if(typeof n=="string")return n}isJwtString(e){return typeof e=="string"&&e.split(".").length===3}async restoreSession(){try{await this.migrateStorageKeys();const e=this.tokenStore.get();if(e){const t=await this.crypto.verifyJWT(e);if(t.valid){const n=await this.storage.get(this.STORAGE_KEY_USER);if(n)return u.log("[CROSSx] restoreSession — 메모리 토큰 유효, 세션 복원"),this.loadWallet(n,t.signatureVerified)}this.tokenStore.clear()}if(this.useCookieAuth){u.log("[CROSSx] restoreSession — 쿠키 기반 silentRefresh 시도");const t=await this.silentRefresh();t&&this.tokenStore.set(t)}else{const t=await this.storage.get(this.STORAGE_KEY_ACCESS_TOKEN),n=await this.storage.get(this.STORAGE_KEY_REFRESH_TOKEN);if(u.log("[CROSSx] restoreSession — access_token:",t?"있음":"없음","refresh_token:",n?"있음":"없음"),!n)return u.log("[CROSSx] restoreSession — refresh_token 없음, 세션 복원 생략"),null;t&&this.tokenStore.set(t);const o=await this.silentRefresh(n);u.log("[CROSSx] restoreSession — silentRefresh 결과:",o?"토큰 발급 성공":"토큰 없음"),o&&this.tokenStore.set(o)}const r=await this.storage.get(this.STORAGE_KEY_USER);if(u.log("[CROSSx] restoreSession — userInfo 조회:",r?`있음 (id: ${r.id})`:"없음"),!r)return null;u.log("[CROSSx] restoreSession — silentRefresh 성공, 세션 복원 — userId:",r.id);try{return await this.loadWallet(r,!1)}catch(t){return u.warn("[CROSSx] restoreSession — 지갑 로드 실패 (세션은 유지):",t),{success:!0,user:r,tokenSignatureVerified:!1}}}catch(e){const r=e instanceof g&&e.code===h.SESSION_EXPIRED;return u.log("[CROSSx] restoreSession —",r?"세션 만료 (재로그인 필요)":"세션 복원 실패:",e),this.tokenStore.clear(),r&&(this.useCookieAuth||await this.storage.remove(this.STORAGE_KEY_REFRESH_TOKEN),await this.storage.remove(this.STORAGE_KEY_USER)),null}}silentRefresh(e){return this._refreshPromise&&this._lastRefreshToken===e?this._refreshPromise:(this._lastRefreshToken=e,this._refreshPromise=this._doSilentRefresh(e).finally(()=>{this._refreshPromise=null,this._lastRefreshToken=void 0}),this._refreshPromise)}async _doSilentRefresh(e){const{authApiUrl:r}=this.config,t=this.useCookieAuth,n=`${r}/cross-auth/social/refresh/simple`,o={};if(!t){const d=this.tokenStore.get()??"";d&&(o.access_token=d),e&&(o.refresh_token=e)}const i=await this.transport.request({url:n,method:"POST",headers:{"Content-Type":"application/json"},body:o,...t?{credentials:"include"}:{}});u.log("[CROSSx] silentRefresh 응답 — status:",i.status);const a=i.data,c=this.extractResponseErrorCode(a);if(c){const d=Qe.REFRESH_RELOGIN_CODES.has(c.code);throw new g(d?h.SESSION_EXPIRED:h.AUTH_FAILED,`토큰 갱신 실패 (코드 ${c.code}): ${c.message}`)}const l=this.extractAccessToken(a);if(t&&!l){u.log("[CROSSx] silentRefresh 성공 (cookie 갱신)");return}if(!l)throw new g(h.AUTH_FAILED,"토큰 자동 갱신 실패: 응답에 토큰이 없습니다");if(this.tokenStore.set(l),!t){await this.storage.set(this.STORAGE_KEY_ACCESS_TOKEN,l);const d=this.extractRefreshToken(a);d&&this.config.secureStorageAvailable!==!1&&await this.storage.set(this.STORAGE_KEY_REFRESH_TOKEN,d)}return u.log("[CROSSx] silentRefresh 성공"),l}async refreshAccessToken(){try{if(this.useCookieAuth)return!!await this.silentRefresh()||this.tokenStore.has();const e=await this.storage.get(this.STORAGE_KEY_REFRESH_TOKEN);return e?!!await this.silentRefresh(e):(u.warn("[CROSSx] refreshAccessToken: refresh_token 없음 — 갱신 불가"),!1)}catch(e){return e instanceof g&&e.code===h.SESSION_EXPIRED?(u.warn("[CROSSx] refreshAccessToken: 세션 만료 — 재로그인 필요"),this.tokenStore.clear(),this.useCookieAuth||await this.storage.remove(this.STORAGE_KEY_REFRESH_TOKEN)):u.warn("[CROSSx] refreshAccessToken 실패:",e),!1}}async executeWithJWT(e,r){let t,n=!1;try{let o=await this.crypto.verifyJWT(e);if(n=o.signatureVerified??!1,!o.valid){if(!r)return u.error("[CROSSx] signInWithJWT: access_token 검증 실패, refreshToken 없음"),{success:!1,error:"유효하지 않은 access token"};u.log("[CROSSx] signInWithJWT: access_token 만료, refreshToken으로 갱신 시도");const a=await this.silentRefresh(r);if(!a)return u.error("[CROSSx] signInWithJWT: silentRefresh 실패"),{success:!1,error:"access token이 만료되었고 갱신에 실패했습니다"};if(e=a,o=await this.crypto.verifyJWT(e),n=o.signatureVerified??!1,!o.valid)return u.error("[CROSSx] signInWithJWT: 갱신된 access_token도 유효하지 않음"),{success:!1,error:"갱신된 access token이 유효하지 않습니다"};u.log("[CROSSx] signInWithJWT: silentRefresh 성공, 새 access_token 사용")}const i=o.payload;u.log("[CROSSx] signInWithJWT — sub:",i.sub,"signatureVerified:",n),t={id:i.sub,email:i.email},this.tokenStore.set(e),this.useCookieAuth||(await this.storage.set(this.STORAGE_KEY_ACCESS_TOKEN,e),r&&this.config.secureStorageAvailable!==!1&&await this.storage.set(this.STORAGE_KEY_REFRESH_TOKEN,r)),await this.storage.set(this.STORAGE_KEY_USER,t),u.log("[CROSSx] signInWithJWT — 토큰 및 사용자 정보 저장 완료")}catch(o){return u.error("[CROSSx] signInWithJWT 에러:",o),{success:!1,error:o instanceof Error?o.message:"JWT sign in failed"}}return this.loadWallet(t,n)}async migrateStorageKeys(){if(!this._migrated){this._migrated=!0;try{const e=await this.storage.get(Ft);if(!e||await this.storage.get(this.STORAGE_KEY_USER))return;await this.storage.set(this.STORAGE_KEY_USER,e);const t=await this.storage.get(Mt);t&&await this.storage.set(this.STORAGE_KEY_ACCESS_TOKEN,t);const n=await this.storage.get(Bt);n&&await this.storage.set(this.STORAGE_KEY_REFRESH_TOKEN,n),await this.storage.remove(Ft),await this.storage.remove(Mt),await this.storage.remove(Bt),u.log("[CROSSx] 스토리지 키 마이그레이션 완료 (projectId 스코프)")}catch(e){u.warn("[CROSSx] 스토리지 키 마이그레이션 실패:",e)}}}async loadWallet(e,r){let t,n=!1;try{if(typeof this.walletProvider.checkWallet=="function"){u.log("[CROSSx] GET /mnemonic/check — 지갑 상태 확인 (비밀번호 불필요)");const o=await this.walletProvider.checkWallet();if(u.log("[CROSSx] 지갑 상태:",o),o==="migration_required")u.log("[CROSSx] migration_required → needsMigration = true"),n=!0;else if(o==="exists")try{const i=await this.walletProvider.getAddresses(e.id);i.length>0?(t=i[0].address,u.log("[CROSSx] 캐시된 주소 로드 완료 (비밀번호 불필요):",t)):u.log("[CROSSx] 주소 캐시 없음 — createWallet 단계에서 비밀번호 입력 후 로드")}catch(i){u.warn("[CROSSx] getAddresses 실패, createWallet 단계에서 재시도:",i)}}else u.log("[CROSSx] getOrCreateWallet 직접 호출 (폴백)"),t=(await this.walletProvider.getOrCreateWallet(e.id)).address,u.log("[CROSSx] 지갑 로드 완료 — address:",t)}catch(o){if(o instanceof g&&o.code===h.MIGRATION_BACKUP_EXISTS)u.log("[CROSSx] MIGRATION_BACKUP_EXISTS 감지 → needsMigration = true"),n=!0;else{if(o instanceof g&&(o.code===h.PROJECT_NOT_REGISTERED||o.code===h.PROJECT_ID_MISSING||o.code===h.ORIGIN_NOT_ALLOWED))throw u.error("[CROSSx] 프로젝트 설정 에러:",o.message),o;u.warn("[CROSSx] 지갑 상태 확인 실패 (로그인은 유지):",o)}}return u.log("[CROSSx] loadWallet 결과 — address:",t,"needsMigration:",n),{success:!0,address:t,user:e,needsMigration:n,tokenSignatureVerified:r}}};Qe.REFRESH_RELOGIN_CODES=new Set([1007,1008]);let xt=Qe;class us{constructor(e,r,t){this.config=e,this.storage=r,this.tokenStore=t;const n=e.projectId;this.STORAGE_KEY_ACCESS_TOKEN=`crossx_${n}_access_token`,this.STORAGE_KEY_REFRESH_TOKEN=`crossx_${n}_refresh_token`,this.STORAGE_KEY_USER=`crossx_${n}_user_info`}async execute(){this.tokenStore.clear(),this.config.authMode!=="cookie"&&(await this.storage.remove(this.STORAGE_KEY_ACCESS_TOKEN),await this.storage.remove(this.STORAGE_KEY_REFRESH_TOKEN)),await this.storage.remove(this.STORAGE_KEY_USER),await this.storage.clear()}}class hs{constructor(e,r){this.storage=e,this.walletProvider=r}async execute(e,r){if(!this.walletProvider.migrateWallet)throw new g(h.NOT_IMPLEMENTED,"현재 환경에서는 마이그레이션이 지원되지 않습니다");u.log("[CROSSx][Migration Phase 4] MigrateWalletUseCase.execute() — pin 길이:",e.length,"sub:",r);const t=await this.walletProvider.migrateWallet(e,r);return u.log("[CROSSx][Migration Phase 5] MigrateWalletUseCase 완료 — address:",t.address),{address:t.address}}}class vt{constructor(){this.encryptedBytes=null,this.xorKey=null}set(e){this.clear();const r=new TextEncoder().encode(e),t=new Uint8Array(r.length);crypto.getRandomValues(t);const n=new Uint8Array(r.length);for(let o=0;o<r.length;o++)n[o]=r[o]^t[o];r.fill(0),this.xorKey=t,this.encryptedBytes=n}get(){if(!this.encryptedBytes||!this.xorKey)return null;const e=new Uint8Array(this.encryptedBytes.length);for(let t=0;t<this.encryptedBytes.length;t++)e[t]=this.encryptedBytes[t]^this.xorKey[t];const r=new TextDecoder().decode(e);return e.fill(0),r}clear(){var e,r;(e=this.encryptedBytes)==null||e.fill(0),(r=this.xorKey)==null||r.fill(0),this.encryptedBytes=null,this.xorKey=null}has(){return this.encryptedBytes!==null}}class ps{constructor(e,r){this.chainRegistry=e,this.transport=r,this._nextId=1}async call(e,r,t){const n=await this.chainRegistry.getChain(t),o={jsonrpc:"2.0",method:e,params:r,id:this._nextId++},a=(await this.transport.request({url:n.rpcUrl,method:"POST",headers:{"Content-Type":"application/json"},body:o})).data;if(a!=null&&a.error)throw new g(h.UNKNOWN_ERROR,`RPC 오류 [${e}] (${t}): ${a.error.message} (코드: ${a.error.code})`);return a==null?void 0:a.result}}class fs{constructor(){this.listeners=new Map}on(e,r){return this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e).add(r),()=>this.off(e,r)}off(e,r){var t;(t=this.listeners.get(e))==null||t.delete(r)}emit(e,r){var t;(t=this.listeners.get(e))==null||t.forEach(n=>n(r))}removeAllListeners(){this.listeners.clear()}}const Ne={production:{oauthServiceUrl:"https://cross-wallet-oauth.crosstoken.io",authApiUrl:"https://cross-auth.crosstoken.io",walletGatewayUrl:"https://embedded-wallet-gateway.crosstoken.io/api/v1",portraitBaseUrl:"https://portrait.crosstoken.io"},staging:{oauthServiceUrl:"https://stg-cross-wallet-oauth.crosstoken.io",authApiUrl:"https://stg-cross-auth.crosstoken.io",walletGatewayUrl:"https://stg-embedded-wallet-gateway.crosstoken.io/api/v1",portraitBaseUrl:"https://dev-portrait.crosstoken.io"},development:{oauthServiceUrl:"https://dev-cross-wallet-oauth.crosstoken.io",authApiUrl:"https://dev-cross-auth.crosstoken.io",walletGatewayUrl:"https://dev-embedded-wallet-gateway.crosstoken.io/api/v1",portraitBaseUrl:"https://dev-portrait.crosstoken.io"}};function _s(s){const e=s.environment;return e&&e in Ne?Ne[e]:null}function xr(){try{if(typeof __CROSSX_CONFIG__<"u"){const s=typeof __CROSSX_CONFIG__=="string"?JSON.parse(__CROSSX_CONFIG__):__CROSSX_CONFIG__,e=_s(s);if(e)return e}}catch{}try{const s=process.env.NEXT_PUBLIC_CROSSX_ENVIRONMENT;if(s&&s in Ne)return Ne[s]}catch{}return Ne.production}const xs=2e3,Ut=6e4,gs=1e3,ws=1e4,ms="0x77359400",Ht="0x3B9ACA00",Wt=130,Et=6,Gt=18,qt=3e4,ys=5*60*1e3,bs=30*1e3,je=100;function gr(s){let e=s.length;for(;e>0&&s.charCodeAt(e-1)===48;)e--;return s.slice(0,e)}function Ss(s,e=Et){if(!s||s==="0x0"||s==="0x")return"0";try{const r=BigInt(s);if(r===0n)return"0";const t=10n**BigInt(Gt),n=r/t,i=(r%t).toString().padStart(Gt,"0"),a=gr(i).slice(0,e);return a?`${n}.${a}`:`${n}`}catch{return"?"}}function We(s,e,r,t=Et){if(!(!s||s==="0x"||s==="0x0"))try{const n=BigInt(s);if(n===0n)return;const o=10n**BigInt(r),i=n/o,c=(n%o).toString().padStart(r,"0"),l=gr(c.slice(0,t));return`${l?`${i}.${l}`:`${i}`} ${e}`}catch{return}}function vs(s){const e=s.startsWith("0x")?s.slice(2):s;if(!/^[0-9a-fA-F]+$/.test(e))throw new g(h.SIGNATURE_FAILED,"유효하지 않은 서명: 올바른 hex 문자열이 아닙니다");if(e.length!==Wt)throw new g(h.SIGNATURE_FAILED,`서명 길이가 유효하지 않습니다: ${Wt} hex 문자(65 바이트) 예상, 현재 ${e.length}`)}function Es(s){const e=s.startsWith("0x")?s.slice(2):s;if(!/^[0-9a-fA-F]+$/.test(e))throw new g(h.SIGNATURE_FAILED,"유효하지 않은 서명된 트랜잭션: 올바른 hex 문자열이 아닙니다");if(e.length<2)throw new g(h.SIGNATURE_FAILED,"유효하지 않은 서명된 트랜잭션: 너무 짧습니다")}function As(s,e){const r=Is(e);if(s==="0"){if(r!==void 0&&r!==0)throw new g(h.TYPED_DATA_CHAIN_ID_MISMATCH,`오프체인 서명(chainId=0)에서는 typedData.domain.chainId가 없거나 0이어야 합니다. 현재 값: ${r}`);return}const t=s.match(/^eip155:(\d+)$/);if(t){const n=Number(t[1]);if(r===void 0)throw new g(h.TYPED_DATA_CHAIN_ID_MISMATCH,`온체인 서명(${s})에서는 typedData.domain.chainId가 반드시 있어야 합니다`);if(r!==n)throw new g(h.TYPED_DATA_CHAIN_ID_MISMATCH,`typedData.domain.chainId (${r})가 chainId (${s}, 예상값: ${n})와 일치하지 않습니다`)}}function Is(s){if(s==null||typeof s!="object")return;const e=s.domain;if(e==null||typeof e!="object")return;const r=e.chainId;if(r==null)return;const t=Number(r);return Number.isFinite(t)?t:void 0}class Pe{constructor(e){this.deps=e,this.verifyPinMutex=null}async ensurePinSetup(){if(this.deps.pinStore.has())return;const e=await this.deps.confirmation.showPinSetupPrompt();if(!e)throw new g(h.PIN_CANCELLED,"사용자가 PIN 설정을 취소했습니다");this.deps.pinStore.set(e),u.log("[CROSSx] PIN 설정 완료 (메모리 캐시)")}async ensurePinForSigning(e){if(this.deps.pinStore.has()&&!e)return;e&&this.deps.pinStore.clear();const r=await this.deps.confirmation.showPinInputPrompt({errorMessage:e});if(!r)throw new g(h.PIN_CANCELLED,"사용자가 PIN 입력을 취소했습니다");this.deps.pinStore.set(r),u.log("[CROSSx] PIN 입력 완료 (메모리 캐시)")}async ensureVerifiedPin(e,r,t,n){if(e)this.deps.pinStore.clear();else if(this.deps.pinStore.has())return;if(this.verifyPinMutex&&!e){u.log("[CROSSx] ensureVerifiedPin: 진행 중인 검증 대기"),await this.verifyPinMutex;return}let o,i;this.verifyPinMutex=new Promise((c,l)=>{o=c,i=l});const a=xe(this.deps.getLocale()??"en");try{if(typeof this.deps.walletProvider.verifyPin!="function"){const l=await this.deps.confirmation.showPinInputPrompt({verifyMode:!0,errorMessage:e,lockExpiresAt:r,attemptCount:t,maxAttempts:n});if(!l)throw new g(h.PIN_CANCELLED,"사용자가 PIN 입력을 취소했습니다");this.deps.pinStore.set(l),u.log("[CROSSx] PIN 캐시 완료 (verifyPin 미지원, 로컬 전용)"),o();return}const c=await this.deps.confirmation.showPinInputPrompt({verifyMode:!0,errorMessage:e,lockExpiresAt:r,attemptCount:t,maxAttempts:n,onSubmit:async l=>{this.deps.pinStore.set(l);try{return await this.deps.walletProvider.verifyPin(l)?(u.log("[CROSSx] PIN 서버 검증 완료 (verify-password)"),{ok:!0}):(this.deps.pinStore.clear(),{ok:!1,error:a.pinInput_error})}catch(d){if(this.deps.pinStore.clear(),d instanceof g){if(d.code===h.PIN_WRONG)return{ok:!1,error:a.pinInput_error};if(d.code===h.PIN_INVALID)return{ok:!1,error:d.message};if(d.code===h.PIN_LOCKED)return Pe.buildPinLockedResult(d,a)}throw d}}});if(!c)throw new g(h.PIN_CANCELLED,"사용자가 PIN 입력을 취소했습니다");this.deps.pinStore.set(c),u.log("[CROSSx] PIN 갱신 완료 (verify 모달 닫힘)"),o()}catch(c){if(c instanceof g&&(c.code===h.AUTH_NOT_AUTHENTICATED||c.code===h.AUTH_TOKEN_EXPIRED||c.code===h.SESSION_EXPIRED)&&c.code!==h.SESSION_EXPIRED){const l=xe(this.deps.getLocale()??"en"),d=this.deps.getUserEmail();if(await this.deps.confirmation.showSessionAlert({title:l.sessionAlert_title,message:l.alert_sessionExpired,email:d?this.deps.maskEmail(d,l.sessionAlert_appleAccount):void 0})==="signin-again"&&(await this.deps.signInAgain()).success)throw i(c),c}throw i(c),c}finally{this.verifyPinMutex=null}}async withPinRetry(e){const r=xe(this.deps.getLocale()??"en");let t=null;try{return await e()}catch(n){if(n instanceof g){if(t=Pe.parsePinError(n,r),!t)throw n}else throw n}for(;;){u.warn("[CROSSx] PIN 불일치/잠금 — 재입력 요청:",t.errorMessage),this.deps.pinStore.clear(),await this.ensureVerifiedPin(t.errorMessage,t.lockExpiresAt,t.attemptCount,t.maxAttempts);try{return await e()}catch(n){if(n instanceof g&&(t=Pe.parsePinError(n,r),t))continue;throw n}}}static parsePinError(e,r){if(e.code===h.PIN_WRONG)return{errorMessage:r.pinInput_error};if(e.code===h.PIN_INVALID)return{errorMessage:e.message};if(e.code===h.PIN_LOCKED){const t=e.details;return t!=null&&t.permanent?{errorMessage:r.pinLocked_permanent}:(t==null?void 0:t.remainingAttempts)!=null&&t.remainingAttempts>0&&t.maxAttempts?{errorMessage:r.pinInput_error,attemptCount:t.maxAttempts-t.remainingAttempts,maxAttempts:t.maxAttempts}:{errorMessage:"Too many failed attempts. Your account is temporarily locked.",lockExpiresAt:t==null?void 0:t.lockExpiresAt}}return null}static buildPinLockedResult(e,r){const t=Pe.parsePinError(e,r);if(!t)return{ok:!1,error:e.message};const{errorMessage:n,...o}=t;return{ok:!1,error:n,...o}}}class Ts{constructor(e){this.deps=e}async confirmAndExecuteWithPreparedPin(e){if(!await this.deps.confirmation.requestConfirmation(e.confirmation))throw new g(h.USER_REJECTED,e.rejectedMessage);return this.deps.pinOrchestrator.withPinRetry(async()=>{let t;return this.deps.walletProvider.prepare&&(t=(await this.deps.walletProvider.prepare(e.prepareAction,e.prepareContext)).uuid),await this.deps.pinOrchestrator.ensureVerifiedPin(),e.execute(t)})}}function ks(s){return s==="google"||s==="apple"}class Rs{constructor(e){this.deps=e}async signInAgain(){var r,t;this.deps.ensureInitialized();const e=this.deps.getSessionSnapshot();this.deps.setRecoveringSession(!0);try{this.deps.setAuthenticated(!1),this.deps.clearTokenStore();const n=ks(e.loginType)?{provider:e.loginType}:void 0;let o;try{o=await this.deps.executeSignIn(n)}catch(l){throw this.deps.clearAuthState(),l}if(!o.success)return this.deps.clearAuthState(),o;const i=((r=o.user)==null?void 0:r.providerSub)??null,a=((t=o.user)==null?void 0:t.id)??null;if(!(e.providerSub?!!i&&i===e.providerSub:e.userId?!!a&&e.userId===a:!0)){u.warn("[CROSSx] signInAgain: providerSub/userId 불일치 — 잘못된 계정으로 로그인됨"),await this.deps.executeSignOut().catch(()=>{});const l=xe(this.deps.getLocale()??"en");return await this.deps.showSessionAlert({title:l.sessionAlert_title,message:l.alert_differentAccount,email:e.userEmail?this.deps.maskEmail(e.userEmail,l.sessionAlert_appleAccount):void 0})==="signin-again"?(this.deps.setRecoveringSession(!1),this.signInAgain()):(this.deps.clearAuthState(),{success:!1,error:"Different account signed in"})}this.deps.applyAuthResult(o);try{await this.deps.loadWalletAfterAuth()}catch{u.warn("[CROSSx] signInAgain: loadWalletAfterAuth 실패 (로그인은 유지)")}return u.log("[CROSSx] signInAgain: 세션 복구 성공"),o}finally{this.deps.setRecoveringSession(!1)}}async withSessionRecovery(e){try{return await e()}catch(r){const t=xe(this.deps.getLocale()??"en"),{userEmail:n}=this.deps.getSessionSnapshot(),o=n?this.deps.maskEmail(n,t.sessionAlert_appleAccount):void 0;if(r instanceof g&&r.code===h.WITHDRAW_FAILED){if(u.warn("[CROSSx] WITHDRAW_FAILED — 로컬 인증 초기화"),await this.deps.showSessionAlert({title:t.sessionAlert_title,message:t.alert_accountWithdrawn,email:o})==="signin-again"&&(await this.signInAgain()).success)return e();throw this.deps.clearAuthState(),r}if(r instanceof g&&r.code===h.WALLET_NOT_FOUND){if(u.warn("[CROSSx] WALLET_NOT_FOUND — 로컬 인증 초기화"),await this.deps.showSessionAlert({title:t.sessionAlert_title,message:t.alert_walletNotFound,email:o})==="signin-again"&&(await this.signInAgain()).success)return e();throw this.deps.clearAuthState(),r}if(!(r instanceof g)||r.code!==h.SESSION_EXPIRED)throw r;if(u.warn("[CROSSx] SESSION_EXPIRED 감지 — 자동 재인증 시도"),await this.deps.showSessionAlert({title:t.sessionAlert_title,message:t.alert_sessionExpired,email:o})==="signout")throw this.deps.clearAuthState(),new g(h.SESSION_EXPIRED,"세션이 만료되었습니다. 다시 로그인해 주세요.");const a=await this.signInAgain();if(!a.success)throw new g(h.SESSION_EXPIRED,a.error??"세션이 만료되었습니다. 다시 로그인해 주세요.");return e()}}}class Os{constructor(e){this.deps=e}async fetchWalletStatus(){if(typeof this.deps.walletProvider.checkWallet=="function")try{return await this.deps.walletProvider.checkWallet()}catch(e){return u.warn("[CROSSx] checkWallet 실패 (폴백: not_found 처리):",e),"not_found"}return null}async loadWalletAfterAuth(e,r){const t=this.deps.getUserId();if(!t)return;const n=await this.fetchWalletStatus();if(u.log("[CROSSx] loadWalletAfterAuth 지갑 상태:",n),n!=="exists")return;const o=await this.deps.walletProvider.getAddresses(t);if(o.length>0){const a=r?o.find(d=>d.address.toLowerCase()===r.toLowerCase()):void 0,c=e!==void 0?o.find(d=>d.index===e):void 0,l=a??c??o[0];u.log("[CROSSx] 캐시된 주소 로드 완료 (비밀번호 불필요):",l.address),this.deps.setActiveWallet(l.address,l.index);return}u.log("[CROSSx] 주소 캐시 없음 — 비밀번호 확인 후 address(0) 조회"),await this.deps.pinOrchestrator.ensureVerifiedPin();const i=await this.deps.walletProvider.getAddress(t,0);u.log("[CROSSx] 세션 복원 후 지갑 주소 로드 완료:",i.address),this.deps.setActiveWallet(i.address,0)}}class Ps{constructor(e){this.deps=e}async handleMigrationFlow(e){var c,l;u.log('[CROSSx][Migration Phase 3] "Wallet Found" 팝업 표시');const r=this.deps.getAllowSkip(),t=await this.deps.confirmation.showMigrationFoundPrompt({allowSkip:r});if(u.log("[CROSSx][Migration Phase 3] 사용자 선택:",t),t==="skip")return u.log("[CROSSx][Migration Phase 3] 사용자가 마이그레이션을 건너뜀 → 종료"),null;let n,o=0,i=5,a=null;for(;;){o++,u.log(`[CROSSx][Migration Phase 4] PIN 입력 팝업 표시 (시도 #${o}/${i})`,n?`— 이전 메시지: ${n}`:"");const d=await this.deps.confirmation.showRecoveryPinInputPrompt({errorMessage:n,attemptCount:o>1?o-1:void 0,maxAttempts:i});if(d===null)return u.log("[CROSSx][Migration Phase 4] 사용자가 PIN 입력을 취소함 → 종료"),null;u.log("[CROSSx][Migration Phase 4] PIN 입력 완료 — verify-recovery-pin API 호출"),n=void 0;try{const p=await((l=(c=this.deps.walletProvider).verifyRecoveryPin)==null?void 0:l.call(c,d,e));if(!p){u.log("[CROSSx][Migration Phase 4] verifyRecoveryPin 미지원 — PIN 검증 생략"),a=d;break}if(p.valid){u.log("[CROSSx][Migration Phase 4] PIN 검증 성공"),a=d;break}const _=p.pinStatus;if(i=_.maxAttempts,o=i-_.remainingAttempts,_.remainingAttempts===0&&_.lockExpiresAt){const f=_.lockExpiresAt*1e3,x=Math.max(1,Math.round((f-Date.now())/1e3)),w=x<=1800?"Too many failed attempts. Please try again in 30 minutes.":"Too many failed attempts. Please try again in 24 hours.";u.warn(`[CROSSx][Migration Phase 4] verify-recovery-pin 잠금 — ${x}초, 메시지: ${w}`),await this.deps.confirmation.showRecoveryPinLockedPrompt(x,w),o=0,n="Your account lock has been lifted. You may try again."}else u.warn(`[CROSSx][Migration Phase 4] PIN 불일치 (시도 ${o}/${i})`),n="Incorrect PIN."}catch(p){if(!(p instanceof g))throw p;if(p.code===h.SESSION_EXPIRED){u.warn("[CROSSx][Migration Phase 4] 세션 만료 — 재로그인 후 PIN 입력 재시도");const _=xe(this.deps.getLocale()??"en"),f=this.deps.getUserEmail();if(await this.deps.confirmation.showSessionAlert({title:_.sessionAlert_title,message:_.alert_sessionExpired,email:f?this.deps.maskEmail(f,_.sessionAlert_appleAccount):void 0})==="signin-again"&&(await this.deps.signInAgain()).success){u.log("[CROSSx][Migration Phase 4] 재로그인 성공 — PIN 입력 루프 계속"),o--;continue}throw this.deps.clearAuthState(),p}if(p.code===h.MIGRATION_PIN_LOCKED){const _=p.details;if(i=(_==null?void 0:_.maxAttempts)??5,(_==null?void 0:_.permanent)===!0)return u.warn("[CROSSx][Migration Phase 4] PIN 영구 잠금 (verify-recovery-pin)"),await this.deps.confirmation.showRecoveryPinLockedPrompt(0,"Your account has been permanently locked due to too many failed attempts."),null;const f=((_==null?void 0:_.lockExpiresAt)??0)*1e3,x=Math.max(1,Math.round((f-Date.now())/1e3)),w=x<=1800?"Too many failed attempts. Please try again in 30 minutes.":"Too many failed attempts. Please try again in 24 hours.";u.warn(`[CROSSx][Migration Phase 4] verify-recovery-pin 이미 잠금 — ${x}초`),await this.deps.confirmation.showRecoveryPinLockedPrompt(x,w),o=0,n="Your account lock has been lifted. You may try again."}else throw u.error("[CROSSx][Migration Phase 4] verify-recovery-pin 실패 (복구 불가):",p),p}}u.log("[CROSSx][Migration Phase 5] PIN 검증 완료 — 비밀번호 설정 및 마이그레이션 진행");try{await this.deps.pinOrchestrator.ensurePinSetup();const d=await this.deps.executeMigrate(a,e);return u.log("[CROSSx][Migration Phase 5] 마이그레이션 성공 — address:",d.address),d}catch(d){if(d instanceof g&&d.code===h.MIGRATION_FAILED)return u.warn("[CROSSx][Migration Phase 5] migrate PIN 불일치 (경합) — 처음부터 재시도"),this.handleMigrationFlow(e);if(d instanceof g&&d.code===h.SESSION_EXPIRED){u.warn("[CROSSx][Migration Phase 5] 세션 만료 — 재로그인 후 마이그레이션 재시도");const p=xe(this.deps.getLocale()??"en"),_=this.deps.getUserEmail();if(await this.deps.confirmation.showSessionAlert({title:p.sessionAlert_title,message:p.alert_sessionExpired,email:_?this.deps.maskEmail(_,p.sessionAlert_appleAccount):void 0})==="signin-again"&&(await this.deps.signInAgain()).success){u.log("[CROSSx][Migration Phase 5] 재로그인 성공 — 마이그레이션 재시도 (검증된 PIN 유지)");try{const w=await this.deps.executeMigrate(a,e);return u.log("[CROSSx][Migration Phase 5] 마이그레이션 재시도 성공 — address:",w.address),w}catch(w){throw u.error("[CROSSx][Migration Phase 5] 마이그레이션 재시도 실패:",w),w}}throw this.deps.clearAuthState(),d}throw u.error("[CROSSx][Migration Phase 5] 마이그레이션 실패 (복구 불가):",d),d}}}class Cs{constructor(e){this.deps=e}async withResolvedGasAndFee(e,r){const t=f=>!(f!=null&&f.trim()),n=e.nonce===void 0||e.nonce===null,o=t(e.gasLimit),i=t(e.gasPrice)&&t(e.maxFeePerGas),a=!t(e.maxFeePerGas)&&t(e.maxPriorityFeePerGas);if(!n&&!o&&!i&&!a)return e;const c={...e},l=n?c.from??this.deps.getActiveAddress()??void 0:void 0;l&&u.log("[CROSSx] nonce 비어있음 → eth_getTransactionCount 호출 (from:",l,")"),o&&u.log("[CROSSx] gasLimit 비어있음 → eth_estimateGas 호출"),i&&u.log("[CROSSx] gasPrice & maxFeePerGas 비어있음 → baseFee 조회로 Dynamic/Legacy 판별");const[d,p,_]=await Promise.all([l?this.deps.jsonRpcCall("eth_getTransactionCount",[l,"pending"],r):null,o?this.deps.estimateGas(e,r):null,i?this.deps.getBaseFeePerGas(r):null]);if(l&&(c.nonce=parseInt(d??"0x0",16),u.log("[CROSSx] nonce 결과:",c.nonce)),o&&(c.gasLimit=p,u.log("[CROSSx] estimateGas 결과:",c.gasLimit)),i){const f=_;if(f){const x=Ht;c.maxFeePerGas=`0x${(BigInt(f)+BigInt(x)).toString(16)}`,c.maxPriorityFeePerGas=x,u.log("[CROSSx] Dynamic 체인 감지 — baseFee:",f,"maxFeePerGas:",c.maxFeePerGas,"maxPriorityFeePerGas: 1 Gwei")}else c.gasPrice=ms,u.log("[CROSSx] Legacy 체인 감지 — gasPrice: 2 Gwei")}return!i&&a&&(c.maxPriorityFeePerGas=Ht,u.log("[CROSSx] maxPriorityFeePerGas 비어있음 → 1 Gwei 기본값 적용")),c}}class Ns{constructor(e){this.crypto=e}verifySignatureSigner(e,r,t){if(this.crypto.recoverPersonalSignSigner)try{const n=this.crypto.recoverPersonalSignSigner(e,r);if(n.toLowerCase()!==t.toLowerCase())throw u.error("[CROSSx] 서명 검증 실패: 서명자 주소 불일치",{expected:t,recovered:n}),new g(h.SIGNATURE_SIGNER_MISMATCH,`서명자 주소가 일치하지 않습니다: 예상 ${t}, 복원된 주소 ${n}`);u.log("[CROSSx] 서명 ecrecover 검증 성공")}catch(n){if(n instanceof g)throw n;u.warn("[CROSSx] ecrecover 검증 중 예외 (무시):",n)}}}function Ls(s){return s?s.includes("google")?"google":s.includes("apple")?"apple":s:null}class Ds{constructor(e){this.deps=e}setActiveWallet(e,r){this.deps.setState({address:e,activeWalletIndex:r}),this.deps.emitAddressChanged({address:e,index:r}),this.deps.shouldPersistWalletPreference()&&this.deps.storage.set(this.deps.walletPreferenceKey,{index:r,address:e}).catch(()=>{})}applyAuthResult(e){var t,n,o,i,a;const r={authenticated:e.success,userId:((t=e.user)==null?void 0:t.id)??null,address:e.address??null,activeWalletIndex:0,userEmail:((n=e.user)==null?void 0:n.email)??null,providerSub:((o=e.user)==null?void 0:o.providerSub)??null,loginType:Ls((i=e.user)==null?void 0:i.signInProvider),tokenSignatureVerified:e.tokenSignatureVerified??!1};this.deps.setState(r),this.deps.getIsRecoveringSession()||this.deps.emitAuthChanged({isAuthenticated:e.success,address:e.address??null,userId:((a=e.user)==null?void 0:a.id)??null})}clearAuthState(){this.deps.setState({authenticated:!1,userId:null,address:null,activeWalletIndex:0,userEmail:null,providerSub:null,loginType:null,tokenSignatureVerified:!1}),this.deps.pinStore.clear(),this.deps.shouldPersistWalletPreference()&&this.deps.storage.remove(this.deps.walletPreferenceKey).catch(()=>{}),this.deps.getIsRecoveringSession()||this.deps.emitAuthChanged({isAuthenticated:!1,address:null,userId:null})}}class $s{constructor(e){this.deps=e}async resolveAddress(e){const r=this.deps.getActiveWalletIndex(),t=this.deps.getActiveAddress();if(e===void 0||e===r){if(!t)throw new g(h.AUTH_NOT_AUTHENTICATED,"활성 지갑 주소를 찾을 수 없습니다");return{address:t,index:r}}return{address:(await this.deps.pinOrchestrator.withPinRetry(()=>this.deps.walletProvider.getAddress(this.deps.getUserId(),e))).address,index:e}}}class Ms{constructor(e){this.deps=e}ensureInitialized(){if(!this.deps.isInitialized())throw new g(h.AUTH_NOT_INITIALIZED,"SDK가 초기화되지 않았습니다. initialize()를 먼저 호출하세요.")}ensureAuthenticated(){if(this.ensureInitialized(),!this.deps.isAuthenticated())throw new g(h.AUTH_NOT_AUTHENTICATED,"인증되지 않은 상태입니다. signIn()를 먼저 호출하세요.")}}class Bs{constructor(e){this.deps=e}async createWallet(){const e=this.deps.getUserId();if(!e)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");u.log("[CROSSx] createWallet 시작");const r=await this.deps.walletLifecycleService.fetchWalletStatus();if(u.log("[CROSSx] 지갑 상태:",r),r==="migration_required"){const t=this.deps.getProviderSub()??e;u.log("[CROSSx] migration_required → 마이그레이션 UI 시작 — sub:",t);const n=await this.deps.migrationOrchestrator.handleMigrationFlow(t);if(n)return this.deps.setActiveWallet(n.address,0),{address:n.address};throw new g(h.MIGRATION_FAILED,"사용자가 마이그레이션을 건너뛰었습니다")}return r==="exists"?(await this.deps.pinOrchestrator.ensureVerifiedPin(),this.tryGetOrCreateWalletWithMigrationFallback(e)):(await this.deps.pinOrchestrator.ensurePinSetup(),this.tryGetOrCreateWalletWithMigrationFallback(e))}async tryGetOrCreateWalletWithMigrationFallback(e){try{const r=await this.deps.pinOrchestrator.withPinRetry(()=>this.deps.walletProvider.getOrCreateWallet(e));return this.deps.setActiveWallet(r.address,0),u.log("[CROSSx] 지갑 준비 완료:",r.address),{address:r.address}}catch(r){if(r instanceof g&&r.code===h.MIGRATION_BACKUP_EXISTS){const t=this.deps.getProviderSub()??e;u.log("[CROSSx] MIGRATION_BACKUP_EXISTS (폴백) → 마이그레이션 UI 시작");const n=await this.deps.migrationOrchestrator.handleMigrationFlow(t);if(n)return this.deps.setActiveWallet(n.address,0),{address:n.address};throw new g(h.MIGRATION_FAILED,"사용자가 마이그레이션을 건너뛰었습니다")}throw r}}}function it(s,e="Apple account"){if(s.endsWith("@privaterelay.appleid.com"))return e;const r=s.indexOf("@");if(r<0)return"*".repeat(s.length||3);const t=s.substring(0,r),n=s.substring(r);return t.length<=1?`${t}${"*".repeat(Math.max(t.length,1))}${n}`:`${t[0]}${"*".repeat(t.length-1)}${n}`}const Ie=class Ie extends fs{constructor(e,r,t,n,o,i,a,c,l,d,p){var _,f;super(),this.storage=t,this.crypto=n,this.transport=o,this.oauth=i,this.walletProvider=a,this.tokenStore=c,this.initialized=!1,this._initPromise=null,this.authenticated=!1,this.userId=null,this.address=null,this.activeWalletIndex=0,this.userEmail=null,this.loginType=null,this.providerSub=null,this.tokenSignatureVerified=!1,this._themeMediaCleanup=null,this._isRecoveringSession=!1,this._config=Object.freeze({...e}),this.internalConfig=e,this.adapterConfig=r,e.logger&&$t(e.logger),this.confirmation=l,this.pinStore=p??new vt,this.stateManager=new Ds({setState:x=>{x.authenticated!==void 0&&(this.authenticated=x.authenticated),x.userId!==void 0&&(this.userId=x.userId),x.address!==void 0&&(this.address=x.address),x.activeWalletIndex!==void 0&&(this.activeWalletIndex=x.activeWalletIndex),x.userEmail!==void 0&&(this.userEmail=x.userEmail),x.providerSub!==void 0&&(this.providerSub=x.providerSub),x.loginType!==void 0&&(this.loginType=x.loginType),x.tokenSignatureVerified!==void 0&&(this.tokenSignatureVerified=x.tokenSignatureVerified)},getIsRecoveringSession:()=>this._isRecoveringSession,emitAddressChanged:({address:x,index:w})=>{this.emit("addressChanged",{address:x,index:w})},emitAuthChanged:({isAuthenticated:x,address:w,userId:E})=>{this.emit("authChanged",{isAuthenticated:x,address:w,userId:E})},shouldPersistWalletPreference:()=>this._config.persistWalletPreference!==!1,walletPreferenceKey:Ie.WALLET_PREF_KEY,storage:this.storage,pinStore:this.pinStore}),this.pinOrchestrator=new Pe({confirmation:this.confirmation,walletProvider:this.walletProvider,pinStore:this.pinStore,getLocale:()=>this._config.locale,getUserEmail:()=>this.userEmail,maskEmail:it,signInAgain:()=>this.signInAgain()}),this.signingOrchestrator=new Ts({confirmation:this.confirmation,walletProvider:this.walletProvider,pinOrchestrator:this.pinOrchestrator}),this.walletLifecycleService=new Os({walletProvider:this.walletProvider,pinOrchestrator:this.pinOrchestrator,getUserId:()=>this.userId,setActiveWallet:(x,w)=>this.setActiveWallet(x,w)}),this.migrationOrchestrator=new Ps({confirmation:this.confirmation,walletProvider:this.walletProvider,pinOrchestrator:this.pinOrchestrator,executeMigrate:(x,w)=>this.migrateWalletUseCase.execute(x,w),getLocale:()=>this._config.locale,getUserEmail:()=>this.userEmail,maskEmail:it,signInAgain:()=>this.signInAgain(),clearAuthState:()=>this.clearAuthState(),getAllowSkip:()=>{var x;return((x=this._config.migration)==null?void 0:x.allowSkip)??!0}}),this.transactionLifecycleService=new Cs({getActiveAddress:()=>this.address,estimateGas:(x,w)=>this.estimateGas(x,w),getBaseFeePerGas:x=>this.getBaseFeePerGas(x),jsonRpcCall:(x,w,E)=>this.jsonRpc.call(x,w,E)}),this.signatureVerifier=new Ns(this.crypto),this.addressResolverService=new $s({walletProvider:this.walletProvider,pinOrchestrator:this.pinOrchestrator,getActiveAddress:()=>this.address,getActiveWalletIndex:()=>this.activeWalletIndex,getUserId:()=>this.userId}),this.authGuardService=new Ms({isInitialized:()=>this.initialized,isAuthenticated:()=>this.authenticated}),this.walletCreationService=new Bs({walletProvider:this.walletProvider,pinOrchestrator:this.pinOrchestrator,migrationOrchestrator:this.migrationOrchestrator,walletLifecycleService:this.walletLifecycleService,getUserId:()=>this.userId,getProviderSub:()=>this.providerSub,setActiveWallet:(x,w)=>this.setActiveWallet(x,w)}),this.sessionOrchestrator=new Rs({ensureInitialized:()=>this.ensureInitialized(),getSessionSnapshot:()=>({providerSub:this.providerSub,userId:this.userId,loginType:this.loginType,userEmail:this.userEmail}),setRecoveringSession:x=>{this._isRecoveringSession=x},setAuthenticated:x=>{this.authenticated=x},clearTokenStore:()=>this.tokenStore.clear(),executeSignIn:x=>this.signInUseCase.execute(x),executeSignOut:()=>this.signOutUseCase.execute(),showSessionAlert:x=>this.confirmation.showSessionAlert(x),clearAuthState:()=>this.clearAuthState(),applyAuthResult:x=>this.applyAuthResult(x),loadWalletAfterAuth:()=>this.walletLifecycleService.loadWalletAfterAuth(),getLocale:()=>this._config.locale,maskEmail:it}),this.chainRegistry=d,this.jsonRpc=new ps(d,o),this.signInUseCase=new xt(this.internalConfig,t,n,i,o,a,c),this.signOutUseCase=new us(this.internalConfig,t,c),this.migrateWalletUseCase=new hs(t,a),(_=a.setOnUnauthorized)==null||_.call(a,()=>this.forceLogout()),(f=a.setTokenRefresher)==null||f.call(a,()=>this.signInUseCase.refreshAccessToken()),e.autoDetectTheme&&this._setupAutoDetectTheme()}get config(){return this._config}async initialize(e){return this.initialized?this.authenticated?{success:!0,address:this.address??void 0}:null:this._initPromise?this._initPromise:(this._initPromise=this._doInitialize(e).finally(()=>{this._initPromise=null}),this._initPromise)}async _doInitialize(e){var r,t;u.log("[CROSSx SDK] v2.1.2-beta.2 초기화 중..."),this.confirmation.setMessages(xe(this._config.locale));try{const n=xr();this.internalConfig.oauthServiceUrl=n.oauthServiceUrl,this.internalConfig.authApiUrl=n.authApiUrl,this.internalConfig.walletGatewayUrl=n.walletGatewayUrl,this.adapterConfig.gatewayUrl=n.walletGatewayUrl,(t=(r=this.crypto).setJWKSEndpoint)==null||t.call(r,`${n.authApiUrl}/.well-known/jwks.json`);const o=this._config.persistWalletPreference!==!1?await this.storage.get(Ie.WALLET_PREF_KEY).catch(()=>null):null,i=(e==null?void 0:e.preferredWalletIndex)??(o==null?void 0:o.index),a=(e==null?void 0:e.preferredWalletAddress)??(o==null?void 0:o.address),c=await this.signInUseCase.restoreSession();if(c!=null&&c.success)if(this.applyAuthResult(c),this.address&&(!a||this.address.toLowerCase()===a.toLowerCase()))this.address&&this.setActiveWallet(this.address,i??this.activeWalletIndex);else try{await this.loadWalletAfterAuth(i,a)}catch(d){if(!(d instanceof g&&d.code===h.PIN_CANCELLED))throw d;u.log("[CROSSx] initialize: 비밀번호 입력 취소 — 지갑 미로드 상태로 계속")}return this.initialized=!0,u.info("[CROSSx SDK] v2.1.2-beta.2 초기화 완료"),this.emit("initialized",{restored:!!(c!=null&&c.success)}),c??null}catch(n){throw new g(h.AUTH_NOT_INITIALIZED,"SDK 초기화에 실패했습니다",n)}}async signIn(e){if(this.ensureInitialized(),this.authenticated)throw new g(h.ALREADY_AUTHENTICATED,"이미 로그인된 상태입니다. signOut()을 먼저 호출하세요.");let r=e;if(!(e!=null&&e.provider)){const t=await this.confirmation.showLoginSelector({connectOtherWallets:os(this._config)});if(t===null)return{success:!1,error:"User cancelled login"};if(t.type==="external")throw this.emit("connectExternalWallet",{walletId:t.walletId}),new g(h.EXTERNAL_WALLET_REQUESTED,"User requested external wallet connection");r={...e,provider:t.provider}}try{const t=await this.signInUseCase.execute(r);return t.success&&this.applyAuthResult(t),t}catch(t){throw t instanceof g?t:new g(h.AUTH_FAILED,"로그인에 실패했습니다",t)}}async signInWithCreate(e){const{preferredWalletAddress:r,...t}=e??{},n=await this.signIn(t);if(!n.success)return{...n,addresses:[]};try{if(n.needsMigration||!n.address){const{address:a}=await this.createWallet(),c=await this.getAddresses(),l=await this.selectWalletIfMultiple(c,r);return{...n,address:(l==null?void 0:l.address)??a,needsMigration:!1,addresses:c}}const o=await this.getAddresses(),i=await this.selectWalletIfMultiple(o,r);return{...n,address:(i==null?void 0:i.address)??n.address,addresses:o}}catch(o){throw o instanceof g&&o.code===h.PIN_CANCELLED&&(u.log("[CROSSx] signInWithCreate: 비밀번호 설정 취소 — 자동 로그아웃 진행"),await this.signOut().catch(i=>u.warn("[CROSSx] signInWithCreate: 자동 로그아웃 실패",i))),o}}async signInWithOAuthToken(e){if(this.ensureInitialized(),this.authenticated)throw new g(h.ALREADY_AUTHENTICATED,"이미 로그인된 상태입니다. signOut()을 먼저 호출하세요.");try{const r=await this.signInUseCase.executeWithOAuthToken(e);return r.success&&this.applyAuthResult(r),r}catch(r){throw r instanceof g?r:new g(h.AUTH_FAILED,"OAuth 토큰 로그인에 실패했습니다",r)}}async signInWithJWT(e,r){if(this.ensureInitialized(),this.authenticated)throw new g(h.ALREADY_AUTHENTICATED,"이미 로그인된 상태입니다. signOut()을 먼저 호출하세요.");try{const t=await this.signInUseCase.executeWithJWT(e,r);return t.success&&this.applyAuthResult(t),t}catch(t){throw t instanceof g?t:new g(h.AUTH_FAILED,"JWT 로그인에 실패했습니다",t)}}async signOut(){this.ensureInitialized();try{await this.signOutUseCase.execute(),this.clearAuthState()}catch(e){throw new g(h.UNKNOWN_ERROR,"로그아웃에 실패했습니다",e)}}isAuthenticated(){return this.authenticated}async whenReady(){if(this.initialized)return!0;if(this._initPromise)try{return await this._initPromise,this.initialized}catch{return!1}return!1}get currentAddress(){return this.address}get currentUserId(){return this.userId}isLoggedIn(){return this.isAuthenticated()}async ensureLoggedIn(){if(!this.initialized)return!1;if(this.authenticated)return!0;try{const e=await this.signInUseCase.restoreSession();if(e!=null&&e.success){this.applyAuthResult(e);try{await this.loadWalletAfterAuth()}catch(r){r instanceof g&&r.code===h.PIN_CANCELLED||u.warn("[CROSSx] ensureLoggedIn: loadWalletAfterAuth 실패:",r)}return!0}return!1}catch{return!1}}async getUserInfo(){return this.ensureAuthenticated(),{id:this.userId,email:this.userEmail??void 0,loginType:this.loginType??void 0,addresses:this.address?[this.address]:[],tokenSignatureVerified:this.tokenSignatureVerified}}async migrateWallet(e){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");try{const r=await this.migrateWalletUseCase.execute(e,this.userId);return this.setActiveWallet(r.address,0),u.log("[CROSSx] migrateWallet 완료 — address:",r.address),r}catch(r){throw r instanceof g?r:new g(h.MIGRATION_FAILED,"지갑 마이그레이션에 실패했습니다",r)}}async getAddress(e){if(e!==void 0){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");return{address:(await this.walletProvider.getAddress(this.userId,e)).address,index:e}}return!this.authenticated||!this.address?null:{address:this.address,index:this.activeWalletIndex}}async getAddresses(){if(this.ensureAuthenticated(),!this.userId)return[];const e=await this.withSessionRecovery(()=>this.walletProvider.getAddresses(this.userId));return e.length===0&&this.address?[{address:this.address,index:0}]:e}async selectWallet(e){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");let r=await this.withSessionRecovery(()=>this.walletProvider.getAddresses(this.userId));r.length===0&&this.address&&(r=[{address:this.address,index:0}]);const t=await this.confirmation.showWalletSelector(r,async()=>{await this.pinOrchestrator.ensurePinForSigning();const n=r.length,i={address:(await this.pinOrchestrator.withPinRetry(()=>this.withSessionRecovery(()=>this.walletProvider.getAddress(this.userId,n)))).address,index:n};return r.push(i),i},e);return t&&this.setActiveWallet(t.address,t.index),t}async selectWalletIfMultiple(e,r){if(e.length<2)return null;if(r){const t=e.find(n=>n.address.toLowerCase()===r.toLowerCase());if(t)return this.setActiveWallet(t.address,t.index),t}return this.selectWallet()}async getChains(){this.ensureInitialized();try{return await this.chainRegistry.getChains()}catch(e){throw e instanceof g?e:new g(h.UNKNOWN_ERROR,"체인 목록 조회에 실패했습니다",e)}}async getChain(e){this.ensureInitialized();try{return await this.chainRegistry.getChain(e)}catch(r){throw r instanceof g?r:new g(h.CHAIN_NOT_SUPPORTED,`체인 조회에 실패했습니다: ${e}`,r)}}_setupAutoDetectTheme(){if(typeof window>"u"||!window.matchMedia)return;const e=window.matchMedia("(prefers-color-scheme: dark)"),r=n=>{const o=n?"dark":"light";this.applyTheme(o,this._config.themeTokens??{})};r(e.matches);const t=n=>r(n.matches);e.addEventListener("change",t),this._themeMediaCleanup=()=>e.removeEventListener("change",t)}applyTheme(e=this._config.theme??"light",r=this._config.themeTokens??{}){this._config=Object.freeze({...this._config,theme:e,themeTokens:r}),this.confirmation.setTheme(e,r)}applyLocale(e=this._config.locale??"en"){this._config=Object.freeze({...this._config,locale:e}),this.confirmation.setMessages(xe(e))}async createWallet(){return this.withSessionRecovery(()=>this._createWallet())}async _createWallet(){return this.ensureAuthenticated(),this.walletCreationService.createWallet()}async signMessage(e,r,t){return this.withSessionRecovery(()=>this._signMessage(e,r,t))}async _signMessage(e,r,t){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");const n=await this.resolveAddress(t==null?void 0:t.index);try{const o=await this.signingOrchestrator.confirmAndExecuteWithPreparedPin({confirmation:{type:"sign-message",chainId:e,from:n.address,message:r,dappName:(t==null?void 0:t.dappName)??this._config.appName,accountName:t==null?void 0:t.accountName},rejectedMessage:"User rejected the message signing request",prepareAction:"sign-message",prepareContext:{message:r,from:n.address},execute:i=>this.walletProvider.signMessage(this.userId,e,r,t==null?void 0:t.index,i,n.address)});return this.signatureVerifier.verifySignatureSigner(r,o.signature,n.address),{chainId:e,signature:o.signature,message:r,address:n.address}}catch(o){throw o instanceof g?o:new g(h.SIGNATURE_FAILED,`메시지 서명에 실패했습니다 (${e})`,o)}}async signTypedData(e,r,t){return this.withSessionRecovery(()=>this._signTypedData(e,r,t))}async _signTypedData(e,r,t){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");if(!this.walletProvider.signTypedData)throw new g(h.NOT_IMPLEMENTED,"signTypedData가 구현되어 있지 않습니다");As(e,r);const n=await this.resolveAddress(t==null?void 0:t.index);try{const o=await this.signingOrchestrator.confirmAndExecuteWithPreparedPin({confirmation:{type:"sign-typed-data",chainId:e,from:n.address,typedData:r,dappName:(t==null?void 0:t.dappName)??this._config.appName,accountName:t==null?void 0:t.accountName},rejectedMessage:"User rejected the typed data signing request",prepareAction:"sign-typed-data",prepareContext:{typedData:r,from:n.address},execute:i=>this.walletProvider.signTypedData(this.userId,e,r,t==null?void 0:t.index,i,n.address)});return vs(o.signature),{chainId:e,signature:o.signature,address:n.address}}catch(o){throw o instanceof g?o:new g(h.SIGNATURE_FAILED,`타입 데이터 서명에 실패했습니다 (${e})`,o)}}async signTypedDataOffchain(e,r){return this.signTypedData(Ie.OFFCHAIN_CHAIN_ID,e,r)}async signTransaction(e,r,t){return this.withSessionRecovery(()=>this._signTransaction(e,r,t))}async _signTransaction(e,r,t){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");const n=await this.transactionLifecycleService.withResolvedGasAndFee(r,e);n.from||(n.from=(await this.resolveAddress(t==null?void 0:t.index)).address);const o=ot(e);try{const i=await this.signingOrchestrator.confirmAndExecuteWithPreparedPin({confirmation:{type:"sign",chainId:e,from:n.from,to:n.to,value:n.value,data:n.data,gasLimit:n.gasLimit,gasPrice:n.gasPrice,maxFeePerGas:n.maxFeePerGas,maxPriorityFeePerGas:n.maxPriorityFeePerGas,nativeSymbol:o.symbol,nativeDecimals:o.decimals,dappName:(t==null?void 0:t.dappName)??this._config.appName,accountName:t==null?void 0:t.accountName},rejectedMessage:"User rejected the transaction signing request",prepareAction:"sign",prepareContext:{tx:n},execute:a=>this.walletProvider.signTransaction(this.userId,e,n,t==null?void 0:t.index,a)});return Es(i.signature),{chainId:e,signedTx:i.signature,txHash:i.txHash??""}}catch(i){if(i instanceof g)throw i;const a=i instanceof Error?i.message:String(i);throw new g(h.SIGNATURE_FAILED,`트랜잭션 서명에 실패했습니다 (${e}): ${a}`,i)}}async sendTransaction(e,r,t){return this.withSessionRecovery(()=>this._sendTransaction(e,r,t))}async _sendTransaction(e,r,t){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");const n=await this.transactionLifecycleService.withResolvedGasAndFee(r,e);n.from||(n.from=(await this.resolveAddress(t==null?void 0:t.index)).address);const o=ot(e);try{const i=await this.signingOrchestrator.confirmAndExecuteWithPreparedPin({confirmation:{type:"send",chainId:e,from:n.from,to:n.to,value:n.value,data:n.data,gasLimit:n.gasLimit,gasPrice:n.gasPrice,maxFeePerGas:n.maxFeePerGas,maxPriorityFeePerGas:n.maxPriorityFeePerGas,nativeSymbol:o.symbol,nativeDecimals:o.decimals,dappName:(t==null?void 0:t.dappName)??this._config.appName,accountName:t==null?void 0:t.accountName},rejectedMessage:"User rejected the transaction request",prepareAction:"send",prepareContext:{tx:n},execute:async a=>{if(this.walletProvider.sendTransaction)return(await this.walletProvider.sendTransaction(this.userId,e,n,a)).txHash;const c=await this.walletProvider.signTransaction(this.userId,e,n,t==null?void 0:t.index,a);return c.txHash??c.signature}});return{chainId:e,txHash:i,status:"pending"}}catch(i){if(i instanceof g)throw i;const a=i instanceof Error?i.message:String(i);throw new g(h.TRANSACTION_FAILED,`트랜잭션 전송에 실패했습니다 (${e}): ${a}`,i)}}async getTransactionReceipt(e,r){try{return await this.jsonRpc.call("eth_getTransactionReceipt",[e],r)??null}catch{return null}}async waitForTxAndGetReceipt(e,r,t={}){const n=t.intervalMs??gs,o=ws,i=t.timeoutMs??Ut,a=Date.now()+i;let c=n;for(;Date.now()<a;){const l=await this.getTransactionReceipt(e,r);if(l)return l;await new Promise(d=>setTimeout(d,c)),c=Math.min(c*2,o)}throw new g(h.UNKNOWN_ERROR,`트랜잭션 영수증 조회 시간이 초과되었습니다 (${e})`)}async sendTransactionWithWaitForReceipt(e,r,t={}){var w,E;const{intervalMs:n,timeoutMs:o,...i}=t,{txHash:a}=await this.sendTransaction(e,r,i),c=n??((w=this._config.receiptPolling)==null?void 0:w.intervalMs)??xs,l=o??((E=this._config.receiptPolling)==null?void 0:E.timeoutMs)??Ut,d=ot(e),p=r.from??"";let _,f;const x=this.waitForTxAndGetReceipt(a,e,{intervalMs:c,timeoutMs:l}).then(k=>{_=k;const O=BigInt(k.gasUsed)*BigInt(k.effectiveGasPrice),P=r.value?BigInt(r.value):0n,L=We(r.value,d.symbol,d.decimals,this._config.displayDecimals),W=We("0x"+O.toString(16),d.symbol,d.decimals,this._config.displayDecimals),q=We("0x"+(P+O).toString(16),d.symbol,d.decimals,this._config.displayDecimals);return{chainId:e,txHash:a,from:k.from,to:k.to??r.to,amount:L,fees:W,total:q,nativeSymbol:d.symbol,status:k.status==="0x1"?"success":"reverted"}}).catch(k=>(f=k instanceof Error?k:new Error(String(k)),{chainId:e,txHash:a,from:p,to:r.to,amount:We(r.value,d.symbol,d.decimals,this._config.displayDecimals),nativeSymbol:d.symbol,status:"timeout"}));if(await this.confirmation.showTransactionProgress({chainId:e,txHash:a,from:p,to:r.to},x),f)throw f;return{chainId:e,txHash:a,receipt:_}}setPin(e){this.pinStore.set(e)}clearPin(){this.pinStore.clear()}hasPin(){return this.pinStore.has()}async changePin(e,r){this.ensureAuthenticated();const t=this.walletProvider;if(typeof t.changePin!="function")throw new g(h.NOT_IMPLEMENTED,"changePin이 구현되어 있지 않습니다");this.pinStore.set(e);try{await t.changePin(e,r),this.pinStore.set(r),u.log("[CROSSx] PIN 변경 완료")}catch(n){throw n instanceof g?n:new g(h.UNKNOWN_ERROR,"PIN 변경에 실패했습니다",n)}}async getGasPrice(e){this.ensureAuthenticated();try{return await this.walletRpc("eth_gasPrice",[],e)}catch(r){const t=r instanceof Error?r.message:String(r);throw new g(h.GAS_ESTIMATION_FAILED,`가스 가격 조회에 실패했습니다 (${e}): ${t}`,r)}}async estimateGas(e,r){this.ensureAuthenticated();const t={};e.from&&(t.from=e.from),e.to&&(t.to=e.to),e.value&&(t.value=e.value),e.data&&(t.data=e.data),e.gasPrice&&(t.gasPrice=e.gasPrice),e.maxFeePerGas&&(t.maxFeePerGas=e.maxFeePerGas),e.maxPriorityFeePerGas&&(t.maxPriorityFeePerGas=e.maxPriorityFeePerGas),e.nonce!==void 0&&(t.nonce="0x"+e.nonce.toString(16));try{return await this.walletRpc("eth_estimateGas",[t],r)}catch(n){const o=n instanceof Error?n.message:String(n);throw new g(h.GAS_ESTIMATION_FAILED,`가스 추정에 실패했습니다 (${r}): ${o}`,n)}}async getBaseFeePerGas(e){this.ensureAuthenticated();try{const r=await this.walletRpc("eth_getBlockByNumber",["latest",!1],e);return(r==null?void 0:r.baseFeePerGas)??null}catch(r){const t=r instanceof Error?r.message:String(r);throw new g(h.GAS_ESTIMATION_FAILED,`baseFeePerGas 조회에 실패했습니다 (${e}): ${t}`,r)}}async getMaxPriorityFeePerGas(e){this.ensureAuthenticated();try{return await this.walletRpc("eth_maxPriorityFeePerGas",[],e)}catch(r){const t=r instanceof Error?r.message:String(r);throw new g(h.GAS_ESTIMATION_FAILED,`maxPriorityFeePerGas 조회에 실패했습니다 (${e}): ${t}`,r)}}async getNonce(e){this.ensureAuthenticated();const r=this.address;if(!r)return 0;try{const t=await this.jsonRpc.call("eth_getTransactionCount",[r,"pending"],e);return parseInt(t??"0x0",16)}catch(t){if(t instanceof g)throw t;const n=t instanceof Error?t.message:String(t);throw new g(h.UNKNOWN_ERROR,`Nonce 조회에 실패했습니다: ${n}`,t)}}async getBalance(e){this.ensureAuthenticated();const r=this.address;if(!r)return{wei:"0x0",formatted:"0",chainId:e};try{const n=await this.jsonRpc.call("eth_getBalance",[r,"latest"],e)??"0x0";return{wei:n,formatted:Ss(n,this._config.displayDecimals),chainId:e}}catch(t){if(t instanceof g)throw t;const n=t instanceof Error?t.message:String(t);throw new g(h.UNKNOWN_ERROR,`잔액 조회에 실패했습니다 (${e}): ${n}`,t)}}getProvider(e){return this.ensureAuthenticated(),new fr(this,e)}async walletRpc(e,r,t){this.ensureAuthenticated();try{return await this.jsonRpc.call(e,r,t)}catch(n){if(n instanceof g)throw n;const o=n instanceof Error?n.message:String(n);throw new g(h.UNKNOWN_ERROR,`walletRpc 호출에 실패했습니다 [${e}] (${t}): ${o}`,n)}}setActiveWallet(e,r){this.stateManager.setActiveWallet(e,r)}applyAuthResult(e){this.stateManager.applyAuthResult(e)}clearAuthState(){this.stateManager.clearAuthState()}forceLogout(){this.authenticated&&(u.warn("[CROSSx] 강제 로그아웃 (Gateway -10002/-10033)"),this.clearAuthState())}async signInAgain(){return this.sessionOrchestrator.signInAgain()}async withSessionRecovery(e){return this.sessionOrchestrator.withSessionRecovery(e)}async loadWalletAfterAuth(e,r){return this.walletLifecycleService.loadWalletAfterAuth(e,r)}async resolveAddress(e){return this.addressResolverService.resolveAddress(e)}ensureInitialized(){this.authGuardService.ensureInitialized()}ensureAuthenticated(){this.authGuardService.ensureAuthenticated()}dispose(){var e;(e=this._themeMediaCleanup)==null||e.call(this),this._themeMediaCleanup=null,this.clearAuthState(),this.tokenStore.clear(),this.pinStore.clear(),this.initialized=!1,this.removeAllListeners(),$t(null)}_getInternalContext(){return{transport:this.transport,storage:this.storage,walletProvider:this.walletProvider,confirmation:this.confirmation,tokenStore:this.tokenStore,pinStore:this.pinStore,adapterConfig:this.adapterConfig}}};Ie.WALLET_PREF_KEY="wallet_preference",Ie.OFFCHAIN_CHAIN_ID="0";let Ye=Ie;class Fs{constructor(){this.prefix="crossx_"}async set(e,r){try{const t=JSON.stringify(r);localStorage.setItem(this.prefix+e,t)}catch(t){throw u.error("Storage set error:",t),t}}async get(e){try{const r=localStorage.getItem(this.prefix+e);return r?JSON.parse(r):null}catch(r){return u.error("Storage get error:",r),null}}async remove(e){try{localStorage.removeItem(this.prefix+e)}catch(r){throw u.error("Storage remove error:",r),r}}async clear(){try{Object.keys(localStorage).forEach(r=>{r.startsWith(this.prefix)&&localStorage.removeItem(r)})}catch(e){throw u.error("Storage clear error:",e),e}}}const at="crossx-sdk",Us=1,ue="data",ve="keys",Ge="aes-primary",Hs=12;class Kt{constructor(e){this.db=null,this.cryptoKey=null,this.initPromise=null,this.dbName=`crossx-sdk-${e}`}async ensureReady(){this.db&&this.cryptoKey||(this.initPromise||(this.initPromise=this.init()),await this.initPromise)}openDB(e=this.dbName){return new Promise((r,t)=>{const n=indexedDB.open(e,Us);n.onupgradeneeded=()=>{const o=n.result;o.objectStoreNames.contains(ue)||o.createObjectStore(ue),o.objectStoreNames.contains(ve)||o.createObjectStore(ve)},n.onsuccess=()=>r(n.result),n.onerror=()=>t(n.error)})}idbGet(e,r){return new Promise((t,n)=>{const i=this.db.transaction(e,"readonly").objectStore(e).get(r);i.onsuccess=()=>t(i.result),i.onerror=()=>n(i.error)})}idbPut(e,r,t){return new Promise((n,o)=>{const a=this.db.transaction(e,"readwrite").objectStore(e).put(t,r);a.onsuccess=()=>n(),a.onerror=()=>o(a.error)})}idbDelete(e,r){return new Promise((t,n)=>{const i=this.db.transaction(e,"readwrite").objectStore(e).delete(r);i.onsuccess=()=>t(),i.onerror=()=>n(i.error)})}idbClear(e){return new Promise((r,t)=>{const o=this.db.transaction(e,"readwrite").objectStore(e).clear();o.onsuccess=()=>r(),o.onerror=()=>t(o.error)})}async init(){this.db=await this.openDB();const e=await this.idbGet(ve,Ge);if(e){this.cryptoKey=e;return}const r=await this.migrateFromLegacyDB();if(r){this.cryptoKey=r;return}this.cryptoKey=await crypto.subtle.generateKey({name:"AES-GCM",length:256},!1,["encrypt","decrypt"]),await this.idbPut(ve,Ge,this.cryptoKey)}async migrateFromLegacyDB(){if(this.dbName===at)return null;try{const e=await this.openDB(at),r=e.transaction([ve,ue],"readonly"),t=r.objectStore(ve).get(Ge),n=await new Promise((l,d)=>{t.onsuccess=()=>l(t.result),t.onerror=()=>d(t.error)});if(!n)return e.close(),null;const o=r.objectStore(ue).getAll(),i=r.objectStore(ue).getAllKeys(),[a,c]=await Promise.all([new Promise((l,d)=>{o.onsuccess=()=>l(o.result),o.onerror=()=>d(o.error)}),new Promise((l,d)=>{i.onsuccess=()=>l(i.result),i.onerror=()=>d(i.error)})]);e.close(),await this.idbPut(ve,Ge,n);for(let l=0;l<c.length;l++)await this.idbPut(ue,String(c[l]),a[l]);return indexedDB.deleteDatabase(at),u.log("[CROSSx] IndexedDB 레거시 DB 마이그레이션 완료"),n}catch(e){return u.warn("[CROSSx] IndexedDB 레거시 DB 마이그레이션 실패:",e),null}}async encrypt(e){const r=new Uint8Array(Hs);crypto.getRandomValues(r);const t=new TextEncoder().encode(e),n=await crypto.subtle.encrypt({name:"AES-GCM",iv:r},this.cryptoKey,t);return{iv:r,ciphertext:n}}async decrypt(e){const r=new Uint8Array(e.iv),t=await crypto.subtle.decrypt({name:"AES-GCM",iv:r},this.cryptoKey,e.ciphertext);return new TextDecoder().decode(t)}async set(e,r){try{await this.ensureReady();const t=JSON.stringify(r),n=await this.encrypt(t);await this.idbPut(ue,e,n)}catch(t){throw u.error("[CROSSx] IndexedDB set error:",t),t}}async get(e){try{await this.ensureReady();const r=await this.idbGet(ue,e);if(!r)return null;const t=await this.decrypt(r);return JSON.parse(t)}catch(r){return u.error("[CROSSx] IndexedDB get error:",r),null}}async remove(e){try{await this.ensureReady(),await this.idbDelete(ue,e)}catch(r){throw u.error("[CROSSx] IndexedDB remove error:",r),r}}async clear(){try{await this.ensureReady(),await this.idbClear(ue)}catch(e){throw u.error("[CROSSx] IndexedDB clear error:",e),e}}static isAvailable(){return typeof indexedDB<"u"&&typeof crypto<"u"&&typeof crypto.subtle<"u"}}/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */function At(s){return s instanceof Uint8Array||ArrayBuffer.isView(s)&&s.constructor.name==="Uint8Array"}function we(s,e=""){if(!Number.isSafeInteger(s)||s<0){const r=e&&`"${e}" `;throw new Error(`${r}expected integer >= 0, got ${s}`)}}function V(s,e,r=""){const t=At(s),n=s==null?void 0:s.length,o=e!==void 0;if(!t||o&&n!==e){const i=r&&`"${r}" `,a=o?` of length ${e}`:"",c=t?`length=${n}`:`type=${typeof s}`;throw new Error(i+"expected Uint8Array"+a+", got "+c)}return s}function wr(s){if(typeof s!="function"||typeof s.create!="function")throw new Error("Hash must wrapped by utils.createHasher");we(s.outputLen),we(s.blockLen)}function De(s,e=!0){if(s.destroyed)throw new Error("Hash instance has been destroyed");if(e&&s.finished)throw new Error("Hash#digest() has already been called")}function mr(s,e){V(s,void 0,"digestInto() output");const r=e.outputLen;if(s.length<r)throw new Error('"digestInto() output" expected to be of length >='+r)}function Ws(s){return new Uint32Array(s.buffer,s.byteOffset,Math.floor(s.byteLength/4))}function $e(...s){for(let e=0;e<s.length;e++)s[e].fill(0)}function ct(s){return new DataView(s.buffer,s.byteOffset,s.byteLength)}function he(s,e){return s<<32-e|s>>>e}const Gs=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function qs(s){return s<<24&4278190080|s<<8&16711680|s>>>8&65280|s>>>24&255}function Ks(s){for(let e=0;e<s.length;e++)s[e]=qs(s[e]);return s}const Vt=Gs?s=>s:Ks,yr=typeof Uint8Array.from([]).toHex=="function"&&typeof Uint8Array.fromHex=="function",Vs=Array.from({length:256},(s,e)=>e.toString(16).padStart(2,"0"));function He(s){if(V(s),yr)return s.toHex();let e="";for(let r=0;r<s.length;r++)e+=Vs[s[r]];return e}const pe={_0:48,_9:57,A:65,F:70,a:97,f:102};function zt(s){if(s>=pe._0&&s<=pe._9)return s-pe._0;if(s>=pe.A&&s<=pe.F)return s-(pe.A-10);if(s>=pe.a&&s<=pe.f)return s-(pe.a-10)}function Xe(s){if(typeof s!="string")throw new Error("hex string expected, got "+typeof s);if(yr)return Uint8Array.fromHex(s);const e=s.length,r=e/2;if(e%2)throw new Error("hex string expected, got unpadded hex of length "+e);const t=new Uint8Array(r);for(let n=0,o=0;n<r;n++,o+=2){const i=zt(s.charCodeAt(o)),a=zt(s.charCodeAt(o+1));if(i===void 0||a===void 0){const c=s[o]+s[o+1];throw new Error('hex string expected, got non-hex character "'+c+'" at index '+o)}t[n]=i*16+a}return t}function Se(...s){let e=0;for(let t=0;t<s.length;t++){const n=s[t];V(n),e+=n.length}const r=new Uint8Array(e);for(let t=0,n=0;t<s.length;t++){const o=s[t];r.set(o,n),n+=o.length}return r}function br(s,e={}){const r=(n,o)=>s(o).update(n).digest(),t=s(void 0);return r.outputLen=t.outputLen,r.blockLen=t.blockLen,r.create=n=>s(n),Object.assign(r,e),Object.freeze(r)}function Sr(s=32){const e=typeof globalThis=="object"?globalThis.crypto:null;if(typeof(e==null?void 0:e.getRandomValues)!="function")throw new Error("crypto.getRandomValues must be defined");return e.getRandomValues(new Uint8Array(s))}const zs=s=>({oid:Uint8Array.from([6,9,96,134,72,1,101,3,4,2,s])});function js(s,e,r){return s&e^~s&r}function Ys(s,e,r){return s&e^s&r^e&r}class Xs{constructor(e,r,t,n){N(this,"blockLen");N(this,"outputLen");N(this,"padOffset");N(this,"isLE");N(this,"buffer");N(this,"view");N(this,"finished",!1);N(this,"length",0);N(this,"pos",0);N(this,"destroyed",!1);this.blockLen=e,this.outputLen=r,this.padOffset=t,this.isLE=n,this.buffer=new Uint8Array(e),this.view=ct(this.buffer)}update(e){De(this),V(e);const{view:r,buffer:t,blockLen:n}=this,o=e.length;for(let i=0;i<o;){const a=Math.min(n-this.pos,o-i);if(a===n){const c=ct(e);for(;n<=o-i;i+=n)this.process(c,i);continue}t.set(e.subarray(i,i+a),this.pos),this.pos+=a,i+=a,this.pos===n&&(this.process(r,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){De(this),mr(e,this),this.finished=!0;const{buffer:r,view:t,blockLen:n,isLE:o}=this;let{pos:i}=this;r[i++]=128,$e(this.buffer.subarray(i)),this.padOffset>n-i&&(this.process(t,0),i=0);for(let p=i;p<n;p++)r[p]=0;t.setBigUint64(n-8,BigInt(this.length*8),o),this.process(t,0);const a=ct(e),c=this.outputLen;if(c%4)throw new Error("_sha2: outputLen must be aligned to 32bit");const l=c/4,d=this.get();if(l>d.length)throw new Error("_sha2: outputLen bigger than state");for(let p=0;p<l;p++)a.setUint32(4*p,d[p],o)}digest(){const{buffer:e,outputLen:r}=this;this.digestInto(e);const t=e.slice(0,r);return this.destroy(),t}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());const{blockLen:r,buffer:t,length:n,finished:o,destroyed:i,pos:a}=this;return e.destroyed=i,e.finished=o,e.length=n,e.pos=a,n%r&&e.buffer.set(t),e}clone(){return this._cloneInto()}}const me=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),qe=BigInt(2**32-1),jt=BigInt(32);function Js(s,e=!1){return e?{h:Number(s&qe),l:Number(s>>jt&qe)}:{h:Number(s>>jt&qe)|0,l:Number(s&qe)|0}}function Zs(s,e=!1){const r=s.length;let t=new Uint32Array(r),n=new Uint32Array(r);for(let o=0;o<r;o++){const{h:i,l:a}=Js(s[o],e);[t[o],n[o]]=[i,a]}return[t,n]}const Qs=(s,e,r)=>s<<r|e>>>32-r,en=(s,e,r)=>e<<r|s>>>32-r,tn=(s,e,r)=>e<<r-32|s>>>64-r,rn=(s,e,r)=>s<<r-32|e>>>64-r,sn=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),ye=new Uint32Array(64);class nn extends Xs{constructor(e){super(64,e,8,!1)}get(){const{A:e,B:r,C:t,D:n,E:o,F:i,G:a,H:c}=this;return[e,r,t,n,o,i,a,c]}set(e,r,t,n,o,i,a,c){this.A=e|0,this.B=r|0,this.C=t|0,this.D=n|0,this.E=o|0,this.F=i|0,this.G=a|0,this.H=c|0}process(e,r){for(let p=0;p<16;p++,r+=4)ye[p]=e.getUint32(r,!1);for(let p=16;p<64;p++){const _=ye[p-15],f=ye[p-2],x=he(_,7)^he(_,18)^_>>>3,w=he(f,17)^he(f,19)^f>>>10;ye[p]=w+ye[p-7]+x+ye[p-16]|0}let{A:t,B:n,C:o,D:i,E:a,F:c,G:l,H:d}=this;for(let p=0;p<64;p++){const _=he(a,6)^he(a,11)^he(a,25),f=d+_+js(a,c,l)+sn[p]+ye[p]|0,w=(he(t,2)^he(t,13)^he(t,22))+Ys(t,n,o)|0;d=l,l=c,c=a,a=i+f|0,i=o,o=n,n=t,t=f+w|0}t=t+this.A|0,n=n+this.B|0,o=o+this.C|0,i=i+this.D|0,a=a+this.E|0,c=c+this.F|0,l=l+this.G|0,d=d+this.H|0,this.set(t,n,o,i,a,c,l,d)}roundClean(){$e(ye)}destroy(){this.set(0,0,0,0,0,0,0,0),$e(this.buffer)}}class on extends nn{constructor(){super(32);N(this,"A",me[0]|0);N(this,"B",me[1]|0);N(this,"C",me[2]|0);N(this,"D",me[3]|0);N(this,"E",me[4]|0);N(this,"F",me[5]|0);N(this,"G",me[6]|0);N(this,"H",me[7]|0)}}const an=br(()=>new on,zs(1));/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const It=BigInt(0),gt=BigInt(1);function Je(s,e=""){if(typeof s!="boolean"){const r=e&&`"${e}" `;throw new Error(r+"expected boolean, got type="+typeof s)}return s}function vr(s){if(typeof s=="bigint"){if(!ze(s))throw new Error("positive bigint expected, got "+s)}else we(s);return s}function Ke(s){const e=vr(s).toString(16);return e.length&1?"0"+e:e}function Er(s){if(typeof s!="string")throw new Error("hex string expected, got "+typeof s);return s===""?It:BigInt("0x"+s)}function tt(s){return Er(He(s))}function Ar(s){return Er(He(cn(V(s)).reverse()))}function Tt(s,e){we(e),s=vr(s);const r=Xe(s.toString(16).padStart(e*2,"0"));if(r.length!==e)throw new Error("number too large");return r}function Ir(s,e){return Tt(s,e).reverse()}function cn(s){return Uint8Array.from(s)}const ze=s=>typeof s=="bigint"&&It<=s;function ln(s,e,r){return ze(s)&&ze(e)&&ze(r)&&e<=s&&s<r}function dn(s,e,r,t){if(!ln(e,r,t))throw new Error("expected valid "+s+": "+r+" <= n < "+t+", got "+e)}function un(s){let e;for(e=0;s>It;s>>=gt,e+=1);return e}const kt=s=>(gt<<BigInt(s))-gt;function hn(s,e,r){if(we(s,"hashLen"),we(e,"qByteLen"),typeof r!="function")throw new Error("hmacFn must be a function");const t=E=>new Uint8Array(E),n=Uint8Array.of(),o=Uint8Array.of(0),i=Uint8Array.of(1),a=1e3;let c=t(s),l=t(s),d=0;const p=()=>{c.fill(1),l.fill(0),d=0},_=(...E)=>r(l,Se(c,...E)),f=(E=n)=>{l=_(o,E),c=_(),E.length!==0&&(l=_(i,E),c=_())},x=()=>{if(d++>=a)throw new Error("drbg: tried max amount of iterations");let E=0;const k=[];for(;E<e;){c=_();const O=c.slice();k.push(O),E+=c.length}return Se(...k)};return(E,k)=>{p(),f(E);let O;for(;!(O=k(x()));)f();return p(),O}}function Rt(s,e={},r={}){if(!s||typeof s!="object")throw new Error("expected valid options object");function t(o,i,a){const c=s[o];if(a&&c===void 0)return;const l=typeof c;if(l!==i||c===null)throw new Error(`param "${o}" is invalid: expected ${i}, got ${l}`)}const n=(o,i)=>Object.entries(o).forEach(([a,c])=>t(a,c,i));n(e,!1),n(r,!0)}function Yt(s){const e=new WeakMap;return(r,...t)=>{const n=e.get(r);if(n!==void 0)return n;const o=s(r,...t);return e.set(r,o),o}}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const se=BigInt(0),te=BigInt(1),Te=BigInt(2),Tr=BigInt(3),kr=BigInt(4),Rr=BigInt(5),pn=BigInt(7),Or=BigInt(8),fn=BigInt(9),Pr=BigInt(16);function de(s,e){const r=s%e;return r>=se?r:e+r}function ae(s,e,r){let t=s;for(;e-- >se;)t*=t,t%=r;return t}function Xt(s,e){if(s===se)throw new Error("invert: expected non-zero number");if(e<=se)throw new Error("invert: expected positive modulus, got "+e);let r=de(s,e),t=e,n=se,o=te;for(;r!==se;){const a=t/r,c=t%r,l=n-o*a;t=r,r=c,n=o,o=l}if(t!==te)throw new Error("invert: does not exist");return de(n,e)}function Ot(s,e,r){if(!s.eql(s.sqr(e),r))throw new Error("Cannot find square root")}function Cr(s,e){const r=(s.ORDER+te)/kr,t=s.pow(e,r);return Ot(s,t,e),t}function _n(s,e){const r=(s.ORDER-Rr)/Or,t=s.mul(e,Te),n=s.pow(t,r),o=s.mul(e,n),i=s.mul(s.mul(o,Te),n),a=s.mul(o,s.sub(i,s.ONE));return Ot(s,a,e),a}function xn(s){const e=rt(s),r=Nr(s),t=r(e,e.neg(e.ONE)),n=r(e,t),o=r(e,e.neg(t)),i=(s+pn)/Pr;return(a,c)=>{let l=a.pow(c,i),d=a.mul(l,t);const p=a.mul(l,n),_=a.mul(l,o),f=a.eql(a.sqr(d),c),x=a.eql(a.sqr(p),c);l=a.cmov(l,d,f),d=a.cmov(_,p,x);const w=a.eql(a.sqr(d),c),E=a.cmov(l,d,w);return Ot(a,E,c),E}}function Nr(s){if(s<Tr)throw new Error("sqrt is not defined for small field");let e=s-te,r=0;for(;e%Te===se;)e/=Te,r++;let t=Te;const n=rt(s);for(;Jt(n,t)===1;)if(t++>1e3)throw new Error("Cannot find square root: probably non-prime P");if(r===1)return Cr;let o=n.pow(t,e);const i=(e+te)/Te;return function(c,l){if(c.is0(l))return l;if(Jt(c,l)!==1)throw new Error("Cannot find square root");let d=r,p=c.mul(c.ONE,o),_=c.pow(l,e),f=c.pow(l,i);for(;!c.eql(_,c.ONE);){if(c.is0(_))return c.ZERO;let x=1,w=c.sqr(_);for(;!c.eql(w,c.ONE);)if(x++,w=c.sqr(w),x===d)throw new Error("Cannot find square root");const E=te<<BigInt(d-x-1),k=c.pow(p,E);d=x,p=c.sqr(k),_=c.mul(_,p),f=c.mul(f,k)}return f}}function gn(s){return s%kr===Tr?Cr:s%Or===Rr?_n:s%Pr===fn?xn(s):Nr(s)}const wn=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function mn(s){const e={ORDER:"bigint",BYTES:"number",BITS:"number"},r=wn.reduce((t,n)=>(t[n]="function",t),e);return Rt(s,r),s}function yn(s,e,r){if(r<se)throw new Error("invalid exponent, negatives unsupported");if(r===se)return s.ONE;if(r===te)return e;let t=s.ONE,n=e;for(;r>se;)r&te&&(t=s.mul(t,n)),n=s.sqr(n),r>>=te;return t}function Lr(s,e,r=!1){const t=new Array(e.length).fill(r?s.ZERO:void 0),n=e.reduce((i,a,c)=>s.is0(a)?i:(t[c]=i,s.mul(i,a)),s.ONE),o=s.inv(n);return e.reduceRight((i,a,c)=>s.is0(a)?i:(t[c]=s.mul(i,t[c]),s.mul(i,a)),o),t}function Jt(s,e){const r=(s.ORDER-te)/Te,t=s.pow(e,r),n=s.eql(t,s.ONE),o=s.eql(t,s.ZERO),i=s.eql(t,s.neg(s.ONE));if(!n&&!o&&!i)throw new Error("invalid Legendre symbol result");return n?1:o?0:-1}function bn(s,e){e!==void 0&&we(e);const r=e!==void 0?e:s.toString(2).length,t=Math.ceil(r/8);return{nBitLength:r,nByteLength:t}}class Sn{constructor(e,r={}){N(this,"ORDER");N(this,"BITS");N(this,"BYTES");N(this,"isLE");N(this,"ZERO",se);N(this,"ONE",te);N(this,"_lengths");N(this,"_sqrt");N(this,"_mod");var i;if(e<=se)throw new Error("invalid field: expected ORDER > 0, got "+e);let t;this.isLE=!1,r!=null&&typeof r=="object"&&(typeof r.BITS=="number"&&(t=r.BITS),typeof r.sqrt=="function"&&(this.sqrt=r.sqrt),typeof r.isLE=="boolean"&&(this.isLE=r.isLE),r.allowedLengths&&(this._lengths=(i=r.allowedLengths)==null?void 0:i.slice()),typeof r.modFromBytes=="boolean"&&(this._mod=r.modFromBytes));const{nBitLength:n,nByteLength:o}=bn(e,t);if(o>2048)throw new Error("invalid field: expected ORDER of <= 2048 bytes");this.ORDER=e,this.BITS=n,this.BYTES=o,this._sqrt=void 0,Object.preventExtensions(this)}create(e){return de(e,this.ORDER)}isValid(e){if(typeof e!="bigint")throw new Error("invalid field element: expected bigint, got "+typeof e);return se<=e&&e<this.ORDER}is0(e){return e===se}isValidNot0(e){return!this.is0(e)&&this.isValid(e)}isOdd(e){return(e&te)===te}neg(e){return de(-e,this.ORDER)}eql(e,r){return e===r}sqr(e){return de(e*e,this.ORDER)}add(e,r){return de(e+r,this.ORDER)}sub(e,r){return de(e-r,this.ORDER)}mul(e,r){return de(e*r,this.ORDER)}pow(e,r){return yn(this,e,r)}div(e,r){return de(e*Xt(r,this.ORDER),this.ORDER)}sqrN(e){return e*e}addN(e,r){return e+r}subN(e,r){return e-r}mulN(e,r){return e*r}inv(e){return Xt(e,this.ORDER)}sqrt(e){return this._sqrt||(this._sqrt=gn(this.ORDER)),this._sqrt(this,e)}toBytes(e){return this.isLE?Ir(e,this.BYTES):Tt(e,this.BYTES)}fromBytes(e,r=!1){V(e);const{_lengths:t,BYTES:n,isLE:o,ORDER:i,_mod:a}=this;if(t){if(!t.includes(e.length)||e.length>n)throw new Error("Field.fromBytes: expected "+t+" bytes, got "+e.length);const l=new Uint8Array(n);l.set(e,o?0:l.length-e.length),e=l}if(e.length!==n)throw new Error("Field.fromBytes: expected "+n+" bytes, got "+e.length);let c=o?Ar(e):tt(e);if(a&&(c=de(c,i)),!r&&!this.isValid(c))throw new Error("invalid field element: outside of range 0..ORDER");return c}invertBatch(e){return Lr(this,e)}cmov(e,r,t){return t?r:e}}function rt(s,e={}){return new Sn(s,e)}function Dr(s){if(typeof s!="bigint")throw new Error("field order must be bigint");const e=s.toString(2).length;return Math.ceil(e/8)}function $r(s){const e=Dr(s);return e+Math.ceil(e/2)}function vn(s,e,r=!1){V(s);const t=s.length,n=Dr(e),o=$r(e);if(t<16||t<o||t>1024)throw new Error("expected "+o+"-1024 bytes of input, got "+t);const i=r?Ar(s):tt(s),a=de(i,e-te)+te;return r?Ir(a,n):Tt(a,n)}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const Me=BigInt(0),ke=BigInt(1);function Ze(s,e){const r=e.negate();return s?r:e}function Zt(s,e){const r=Lr(s.Fp,e.map(t=>t.Z));return e.map((t,n)=>s.fromAffine(t.toAffine(r[n])))}function Mr(s,e){if(!Number.isSafeInteger(s)||s<=0||s>e)throw new Error("invalid window size, expected [1.."+e+"], got W="+s)}function lt(s,e){Mr(s,e);const r=Math.ceil(e/s)+1,t=2**(s-1),n=2**s,o=kt(s),i=BigInt(s);return{windows:r,windowSize:t,mask:o,maxNumber:n,shiftBy:i}}function Qt(s,e,r){const{windowSize:t,mask:n,maxNumber:o,shiftBy:i}=r;let a=Number(s&n),c=s>>i;a>t&&(a-=o,c+=ke);const l=e*t,d=l+Math.abs(a)-1,p=a===0,_=a<0,f=e%2!==0;return{nextN:c,offset:d,isZero:p,isNeg:_,isNegF:f,offsetF:l}}const dt=new WeakMap,Br=new WeakMap;function ut(s){return Br.get(s)||1}function er(s){if(s!==Me)throw new Error("invalid wNAF")}class En{constructor(e,r){N(this,"BASE");N(this,"ZERO");N(this,"Fn");N(this,"bits");this.BASE=e.BASE,this.ZERO=e.ZERO,this.Fn=e.Fn,this.bits=r}_unsafeLadder(e,r,t=this.ZERO){let n=e;for(;r>Me;)r&ke&&(t=t.add(n)),n=n.double(),r>>=ke;return t}precomputeWindow(e,r){const{windows:t,windowSize:n}=lt(r,this.bits),o=[];let i=e,a=i;for(let c=0;c<t;c++){a=i,o.push(a);for(let l=1;l<n;l++)a=a.add(i),o.push(a);i=a.double()}return o}wNAF(e,r,t){if(!this.Fn.isValid(t))throw new Error("invalid scalar");let n=this.ZERO,o=this.BASE;const i=lt(e,this.bits);for(let a=0;a<i.windows;a++){const{nextN:c,offset:l,isZero:d,isNeg:p,isNegF:_,offsetF:f}=Qt(t,a,i);t=c,d?o=o.add(Ze(_,r[f])):n=n.add(Ze(p,r[l]))}return er(t),{p:n,f:o}}wNAFUnsafe(e,r,t,n=this.ZERO){const o=lt(e,this.bits);for(let i=0;i<o.windows&&t!==Me;i++){const{nextN:a,offset:c,isZero:l,isNeg:d}=Qt(t,i,o);if(t=a,!l){const p=r[c];n=n.add(d?p.negate():p)}}return er(t),n}getPrecomputes(e,r,t){let n=dt.get(r);return n||(n=this.precomputeWindow(r,e),e!==1&&(typeof t=="function"&&(n=t(n)),dt.set(r,n))),n}cached(e,r,t){const n=ut(e);return this.wNAF(n,this.getPrecomputes(n,e,t),r)}unsafe(e,r,t,n){const o=ut(e);return o===1?this._unsafeLadder(e,r,n):this.wNAFUnsafe(o,this.getPrecomputes(o,e,t),r,n)}createCache(e,r){Mr(r,this.bits),Br.set(e,r),dt.delete(e)}hasCache(e){return ut(e)!==1}}function An(s,e,r,t){let n=e,o=s.ZERO,i=s.ZERO;for(;r>Me||t>Me;)r&ke&&(o=o.add(n)),t&ke&&(i=i.add(n)),n=n.double(),r>>=ke,t>>=ke;return{p1:o,p2:i}}function tr(s,e,r){if(e){if(e.ORDER!==s)throw new Error("Field.ORDER must match order: Fp == p, Fn == n");return mn(e),e}else return rt(s,{isLE:r})}function In(s,e,r={},t){if(t===void 0&&(t=s==="edwards"),!e||typeof e!="object")throw new Error(`expected valid ${s} CURVE object`);for(const c of["p","n","h"]){const l=e[c];if(!(typeof l=="bigint"&&l>Me))throw new Error(`CURVE.${c} must be positive bigint`)}const n=tr(e.p,r.Fp,t),o=tr(e.n,r.Fn,t),a=["Gx","Gy","a","b"];for(const c of a)if(!n.isValid(e[c]))throw new Error(`CURVE.${c} must be valid field element of CURVE.Fp`);return e=Object.freeze(Object.assign({},e)),{CURVE:e,Fp:n,Fn:o}}function Tn(s,e){return function(t){const n=s(t);return{secretKey:n,publicKey:e(n)}}}class Fr{constructor(e,r){N(this,"oHash");N(this,"iHash");N(this,"blockLen");N(this,"outputLen");N(this,"finished",!1);N(this,"destroyed",!1);if(wr(e),V(r,void 0,"key"),this.iHash=e.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;const t=this.blockLen,n=new Uint8Array(t);n.set(r.length>t?e.create().update(r).digest():r);for(let o=0;o<n.length;o++)n[o]^=54;this.iHash.update(n),this.oHash=e.create();for(let o=0;o<n.length;o++)n[o]^=106;this.oHash.update(n),$e(n)}update(e){return De(this),this.iHash.update(e),this}digestInto(e){De(this),V(e,this.outputLen,"output"),this.finished=!0,this.iHash.digestInto(e),this.oHash.update(e),this.oHash.digestInto(e),this.destroy()}digest(){const e=new Uint8Array(this.oHash.outputLen);return this.digestInto(e),e}_cloneInto(e){e||(e=Object.create(Object.getPrototypeOf(this),{}));const{oHash:r,iHash:t,finished:n,destroyed:o,blockLen:i,outputLen:a}=this;return e=e,e.finished=n,e.destroyed=o,e.blockLen=i,e.outputLen=a,e.oHash=r._cloneInto(e.oHash),e.iHash=t._cloneInto(e.iHash),e}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}}const Ur=(s,e,r)=>new Fr(s,e).update(r).digest();Ur.create=(s,e)=>new Fr(s,e);/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const rr=(s,e)=>(s+(s>=0?e:-e)/Hr)/e;function kn(s,e,r){const[[t,n],[o,i]]=e,a=rr(i*s,r),c=rr(-n*s,r);let l=s-a*t-c*o,d=-a*n-c*i;const p=l<_e,_=d<_e;p&&(l=-l),_&&(d=-d);const f=kt(Math.ceil(un(r)/2))+Le;if(l<_e||l>=f||d<_e||d>=f)throw new Error("splitScalar (endomorphism): failed, k="+s);return{k1neg:p,k1:l,k2neg:_,k2:d}}function wt(s){if(!["compact","recovered","der"].includes(s))throw new Error('Signature format must be "compact", "recovered", or "der"');return s}function ht(s,e){const r={};for(let t of Object.keys(e))r[t]=s[t]===void 0?e[t]:s[t];return Je(r.lowS,"lowS"),Je(r.prehash,"prehash"),r.format!==void 0&&wt(r.format),r}class Rn extends Error{constructor(e=""){super(e)}}const be={Err:Rn,_tlv:{encode:(s,e)=>{const{Err:r}=be;if(s<0||s>256)throw new r("tlv.encode: wrong tag");if(e.length&1)throw new r("tlv.encode: unpadded data");const t=e.length/2,n=Ke(t);if(n.length/2&128)throw new r("tlv.encode: long form length too big");const o=t>127?Ke(n.length/2|128):"";return Ke(s)+o+n+e},decode(s,e){const{Err:r}=be;let t=0;if(s<0||s>256)throw new r("tlv.encode: wrong tag");if(e.length<2||e[t++]!==s)throw new r("tlv.decode: wrong tlv");const n=e[t++],o=!!(n&128);let i=0;if(!o)i=n;else{const c=n&127;if(!c)throw new r("tlv.decode(long): indefinite length not supported");if(c>4)throw new r("tlv.decode(long): byte length is too big");const l=e.subarray(t,t+c);if(l.length!==c)throw new r("tlv.decode: length bytes not complete");if(l[0]===0)throw new r("tlv.decode(long): zero leftmost byte");for(const d of l)i=i<<8|d;if(t+=c,i<128)throw new r("tlv.decode(long): not minimal encoding")}const a=e.subarray(t,t+i);if(a.length!==i)throw new r("tlv.decode: wrong value length");return{v:a,l:e.subarray(t+i)}}},_int:{encode(s){const{Err:e}=be;if(s<_e)throw new e("integer: negative integers are not allowed");let r=Ke(s);if(Number.parseInt(r[0],16)&8&&(r="00"+r),r.length&1)throw new e("unexpected DER parsing assertion: unpadded hex");return r},decode(s){const{Err:e}=be;if(s[0]&128)throw new e("invalid signature integer: negative");if(s[0]===0&&!(s[1]&128))throw new e("invalid signature integer: unnecessary leading zero");return tt(s)}},toSig(s){const{Err:e,_int:r,_tlv:t}=be,n=V(s,void 0,"signature"),{v:o,l:i}=t.decode(48,n);if(i.length)throw new e("invalid signature: left bytes after parsing");const{v:a,l:c}=t.decode(2,o),{v:l,l:d}=t.decode(2,c);if(d.length)throw new e("invalid signature: left bytes after parsing");return{r:r.decode(a),s:r.decode(l)}},hexFromSig(s){const{_tlv:e,_int:r}=be,t=e.encode(2,r.encode(s.r)),n=e.encode(2,r.encode(s.s)),o=t+n;return e.encode(48,o)}},_e=BigInt(0),Le=BigInt(1),Hr=BigInt(2),Ve=BigInt(3),On=BigInt(4);function Pn(s,e={}){const r=In("weierstrass",s,e),{Fp:t,Fn:n}=r;let o=r.CURVE;const{h:i,n:a}=o;Rt(e,{},{allowInfinityPoint:"boolean",clearCofactor:"function",isTorsionFree:"function",fromBytes:"function",toBytes:"function",endo:"object"});const{endo:c}=e;if(c&&(!t.is0(o.a)||typeof c.beta!="bigint"||!Array.isArray(c.basises)))throw new Error('invalid endo: expected "beta": bigint and "basises": array');const l=Gr(t,n);function d(){if(!t.isOdd)throw new Error("compression is not supported: Field does not have .isOdd()")}function p(B,m,y){const{x:T,y:C}=m.toAffine(),M=t.toBytes(T);if(Je(y,"isCompressed"),y){d();const b=!t.isOdd(C);return Se(Wr(b),M)}else return Se(Uint8Array.of(4),M,t.toBytes(C))}function _(B){V(B,void 0,"Point");const{publicKey:m,publicKeyUncompressed:y}=l,T=B.length,C=B[0],M=B.subarray(1);if(T===m&&(C===2||C===3)){const b=t.fromBytes(M);if(!t.isValid(b))throw new Error("bad point: is not on curve, wrong x");const S=w(b);let v;try{v=t.sqrt(S)}catch(H){const F=H instanceof Error?": "+H.message:"";throw new Error("bad point: is not on curve, sqrt error"+F)}d();const I=t.isOdd(v);return(C&1)===1!==I&&(v=t.neg(v)),{x:b,y:v}}else if(T===y&&C===4){const b=t.BYTES,S=t.fromBytes(M.subarray(0,b)),v=t.fromBytes(M.subarray(b,b*2));if(!E(S,v))throw new Error("bad point: is not on curve");return{x:S,y:v}}else throw new Error(`bad point: got length ${T}, expected compressed=${m} or uncompressed=${y}`)}const f=e.toBytes||p,x=e.fromBytes||_;function w(B){const m=t.sqr(B),y=t.mul(m,B);return t.add(t.add(y,t.mul(B,o.a)),o.b)}function E(B,m){const y=t.sqr(m),T=w(B);return t.eql(y,T)}if(!E(o.Gx,o.Gy))throw new Error("bad curve params: generator point");const k=t.mul(t.pow(o.a,Ve),On),O=t.mul(t.sqr(o.b),BigInt(27));if(t.is0(t.add(k,O)))throw new Error("bad curve params: a or b");function P(B,m,y=!1){if(!t.isValid(m)||y&&t.is0(m))throw new Error(`bad point coordinate ${B}`);return m}function L(B){if(!(B instanceof X))throw new Error("Weierstrass Point expected")}function W(B){if(!c||!c.basises)throw new Error("no endo");return kn(B,c.basises,n.ORDER)}const q=Yt((B,m)=>{const{X:y,Y:T,Z:C}=B;if(t.eql(C,t.ONE))return{x:y,y:T};const M=B.is0();m==null&&(m=M?t.ONE:t.inv(C));const b=t.mul(y,m),S=t.mul(T,m),v=t.mul(C,m);if(M)return{x:t.ZERO,y:t.ZERO};if(!t.eql(v,t.ONE))throw new Error("invZ was invalid");return{x:b,y:S}}),J=Yt(B=>{if(B.is0()){if(e.allowInfinityPoint&&!t.is0(B.Y))return;throw new Error("bad point: ZERO")}const{x:m,y}=B.toAffine();if(!t.isValid(m)||!t.isValid(y))throw new Error("bad point: x or y not field elements");if(!E(m,y))throw new Error("bad point: equation left != right");if(!B.isTorsionFree())throw new Error("bad point: not in prime-order subgroup");return!0});function G(B,m,y,T,C){return y=new X(t.mul(y.X,B),y.Y,y.Z),m=Ze(T,m),y=Ze(C,y),m.add(y)}const D=class D{constructor(m,y,T){N(this,"X");N(this,"Y");N(this,"Z");this.X=P("x",m),this.Y=P("y",y,!0),this.Z=P("z",T),Object.freeze(this)}static CURVE(){return o}static fromAffine(m){const{x:y,y:T}=m||{};if(!m||!t.isValid(y)||!t.isValid(T))throw new Error("invalid affine point");if(m instanceof D)throw new Error("projective point not allowed");return t.is0(y)&&t.is0(T)?D.ZERO:new D(y,T,t.ONE)}static fromBytes(m){const y=D.fromAffine(x(V(m,void 0,"point")));return y.assertValidity(),y}static fromHex(m){return D.fromBytes(Xe(m))}get x(){return this.toAffine().x}get y(){return this.toAffine().y}precompute(m=8,y=!0){return U.createCache(this,m),y||this.multiply(Ve),this}assertValidity(){J(this)}hasEvenY(){const{y:m}=this.toAffine();if(!t.isOdd)throw new Error("Field doesn't support isOdd");return!t.isOdd(m)}equals(m){L(m);const{X:y,Y:T,Z:C}=this,{X:M,Y:b,Z:S}=m,v=t.eql(t.mul(y,S),t.mul(M,C)),I=t.eql(t.mul(T,S),t.mul(b,C));return v&&I}negate(){return new D(this.X,t.neg(this.Y),this.Z)}double(){const{a:m,b:y}=o,T=t.mul(y,Ve),{X:C,Y:M,Z:b}=this;let S=t.ZERO,v=t.ZERO,I=t.ZERO,R=t.mul(C,C),H=t.mul(M,M),F=t.mul(b,b),$=t.mul(C,M);return $=t.add($,$),I=t.mul(C,b),I=t.add(I,I),S=t.mul(m,I),v=t.mul(T,F),v=t.add(S,v),S=t.sub(H,v),v=t.add(H,v),v=t.mul(S,v),S=t.mul($,S),I=t.mul(T,I),F=t.mul(m,F),$=t.sub(R,F),$=t.mul(m,$),$=t.add($,I),I=t.add(R,R),R=t.add(I,R),R=t.add(R,F),R=t.mul(R,$),v=t.add(v,R),F=t.mul(M,b),F=t.add(F,F),R=t.mul(F,$),S=t.sub(S,R),I=t.mul(F,H),I=t.add(I,I),I=t.add(I,I),new D(S,v,I)}add(m){L(m);const{X:y,Y:T,Z:C}=this,{X:M,Y:b,Z:S}=m;let v=t.ZERO,I=t.ZERO,R=t.ZERO;const H=o.a,F=t.mul(o.b,Ve);let $=t.mul(y,M),K=t.mul(T,b),Y=t.mul(C,S),oe=t.add(y,T),z=t.add(M,b);oe=t.mul(oe,z),z=t.add($,K),oe=t.sub(oe,z),z=t.add(y,C);let ee=t.add(M,S);return z=t.mul(z,ee),ee=t.add($,Y),z=t.sub(z,ee),ee=t.add(T,C),v=t.add(b,S),ee=t.mul(ee,v),v=t.add(K,Y),ee=t.sub(ee,v),R=t.mul(H,z),v=t.mul(F,Y),R=t.add(v,R),v=t.sub(K,R),R=t.add(K,R),I=t.mul(v,R),K=t.add($,$),K=t.add(K,$),Y=t.mul(H,Y),z=t.mul(F,z),K=t.add(K,Y),Y=t.sub($,Y),Y=t.mul(H,Y),z=t.add(z,Y),$=t.mul(K,z),I=t.add(I,$),$=t.mul(ee,z),v=t.mul(oe,v),v=t.sub(v,$),$=t.mul(oe,K),R=t.mul(ee,R),R=t.add(R,$),new D(v,I,R)}subtract(m){return this.add(m.negate())}is0(){return this.equals(D.ZERO)}multiply(m){const{endo:y}=e;if(!n.isValidNot0(m))throw new Error("invalid scalar: out of range");let T,C;const M=b=>U.cached(this,b,S=>Zt(D,S));if(y){const{k1neg:b,k1:S,k2neg:v,k2:I}=W(m),{p:R,f:H}=M(S),{p:F,f:$}=M(I);C=H.add($),T=G(y.beta,R,F,b,v)}else{const{p:b,f:S}=M(m);T=b,C=S}return Zt(D,[T,C])[0]}multiplyUnsafe(m){const{endo:y}=e,T=this;if(!n.isValid(m))throw new Error("invalid scalar: out of range");if(m===_e||T.is0())return D.ZERO;if(m===Le)return T;if(U.hasCache(this))return this.multiply(m);if(y){const{k1neg:C,k1:M,k2neg:b,k2:S}=W(m),{p1:v,p2:I}=An(D,T,M,S);return G(y.beta,v,I,C,b)}else return U.unsafe(T,m)}toAffine(m){return q(this,m)}isTorsionFree(){const{isTorsionFree:m}=e;return i===Le?!0:m?m(D,this):U.unsafe(this,a).is0()}clearCofactor(){const{clearCofactor:m}=e;return i===Le?this:m?m(D,this):this.multiplyUnsafe(i)}isSmallOrder(){return this.multiplyUnsafe(i).is0()}toBytes(m=!0){return Je(m,"isCompressed"),this.assertValidity(),f(D,this,m)}toHex(m=!0){return He(this.toBytes(m))}toString(){return`<Point ${this.is0()?"ZERO":this.toHex()}>`}};N(D,"BASE",new D(o.Gx,o.Gy,t.ONE)),N(D,"ZERO",new D(t.ZERO,t.ONE,t.ZERO)),N(D,"Fp",t),N(D,"Fn",n);let X=D;const Q=n.BITS,U=new En(X,e.endo?Math.ceil(Q/2):Q);return X.BASE.precompute(8),X}function Wr(s){return Uint8Array.of(s?2:3)}function Gr(s,e){return{secretKey:e.BYTES,publicKey:1+s.BYTES,publicKeyUncompressed:1+2*s.BYTES,publicKeyHasPrefix:!0,signature:2*e.BYTES}}function Cn(s,e={}){const{Fn:r}=s,t=e.randomBytes||Sr,n=Object.assign(Gr(s.Fp,r),{seed:$r(r.ORDER)});function o(f){try{const x=r.fromBytes(f);return r.isValidNot0(x)}catch{return!1}}function i(f,x){const{publicKey:w,publicKeyUncompressed:E}=n;try{const k=f.length;return x===!0&&k!==w||x===!1&&k!==E?!1:!!s.fromBytes(f)}catch{return!1}}function a(f=t(n.seed)){return vn(V(f,n.seed,"seed"),r.ORDER)}function c(f,x=!0){return s.BASE.multiply(r.fromBytes(f)).toBytes(x)}function l(f){const{secretKey:x,publicKey:w,publicKeyUncompressed:E}=n;if(!At(f)||"_lengths"in r&&r._lengths||x===w)return;const k=V(f,void 0,"key").length;return k===w||k===E}function d(f,x,w=!0){if(l(f)===!0)throw new Error("first arg must be private key");if(l(x)===!1)throw new Error("second arg must be public key");const E=r.fromBytes(f);return s.fromBytes(x).multiply(E).toBytes(w)}const p={isValidSecretKey:o,isValidPublicKey:i,randomSecretKey:a},_=Tn(a,c);return Object.freeze({getPublicKey:c,getSharedSecret:d,keygen:_,Point:s,utils:p,lengths:n})}function Nn(s,e,r={}){wr(e),Rt(r,{},{hmac:"function",lowS:"boolean",randomBytes:"function",bits2int:"function",bits2int_modN:"function"}),r=Object.assign({},r);const t=r.randomBytes||Sr,n=r.hmac||((m,y)=>Ur(e,m,y)),{Fp:o,Fn:i}=s,{ORDER:a,BITS:c}=i,{keygen:l,getPublicKey:d,getSharedSecret:p,utils:_,lengths:f}=Cn(s,r),x={prehash:!0,lowS:typeof r.lowS=="boolean"?r.lowS:!0,format:"compact",extraEntropy:!1},w=a*Hr<o.ORDER;function E(m){const y=a>>Le;return m>y}function k(m,y){if(!i.isValidNot0(y))throw new Error(`invalid signature ${m}: out of range 1..Point.Fn.ORDER`);return y}function O(){if(w)throw new Error('"recovered" sig type is not supported for cofactor >2 curves')}function P(m,y){wt(y);const T=f.signature,C=y==="compact"?T:y==="recovered"?T+1:void 0;return V(m,C)}class L{constructor(y,T,C){N(this,"r");N(this,"s");N(this,"recovery");if(this.r=k("r",y),this.s=k("s",T),C!=null){if(O(),![0,1,2,3].includes(C))throw new Error("invalid recovery id");this.recovery=C}Object.freeze(this)}static fromBytes(y,T=x.format){P(y,T);let C;if(T==="der"){const{r:v,s:I}=be.toSig(V(y));return new L(v,I)}T==="recovered"&&(C=y[0],T="compact",y=y.subarray(1));const M=f.signature/2,b=y.subarray(0,M),S=y.subarray(M,M*2);return new L(i.fromBytes(b),i.fromBytes(S),C)}static fromHex(y,T){return this.fromBytes(Xe(y),T)}assertRecovery(){const{recovery:y}=this;if(y==null)throw new Error("invalid recovery id: must be present");return y}addRecoveryBit(y){return new L(this.r,this.s,y)}recoverPublicKey(y){const{r:T,s:C}=this,M=this.assertRecovery(),b=M===2||M===3?T+a:T;if(!o.isValid(b))throw new Error("invalid recovery id: sig.r+curve.n != R.x");const S=o.toBytes(b),v=s.fromBytes(Se(Wr((M&1)===0),S)),I=i.inv(b),R=q(V(y,void 0,"msgHash")),H=i.create(-R*I),F=i.create(C*I),$=s.BASE.multiplyUnsafe(H).add(v.multiplyUnsafe(F));if($.is0())throw new Error("invalid recovery: point at infinify");return $.assertValidity(),$}hasHighS(){return E(this.s)}toBytes(y=x.format){if(wt(y),y==="der")return Xe(be.hexFromSig(this));const{r:T,s:C}=this,M=i.toBytes(T),b=i.toBytes(C);return y==="recovered"?(O(),Se(Uint8Array.of(this.assertRecovery()),M,b)):Se(M,b)}toHex(y){return He(this.toBytes(y))}}const W=r.bits2int||function(y){if(y.length>8192)throw new Error("input is too large");const T=tt(y),C=y.length*8-c;return C>0?T>>BigInt(C):T},q=r.bits2int_modN||function(y){return i.create(W(y))},J=kt(c);function G(m){return dn("num < 2^"+c,m,_e,J),i.toBytes(m)}function X(m,y){return V(m,void 0,"message"),y?V(e(m),void 0,"prehashed message"):m}function Q(m,y,T){const{lowS:C,prehash:M,extraEntropy:b}=ht(T,x);m=X(m,M);const S=q(m),v=i.fromBytes(y);if(!i.isValidNot0(v))throw new Error("invalid private key");const I=[G(v),G(S)];if(b!=null&&b!==!1){const $=b===!0?t(f.secretKey):b;I.push(V($,void 0,"extraEntropy"))}const R=Se(...I),H=S;function F($){const K=W($);if(!i.isValidNot0(K))return;const Y=i.inv(K),oe=s.BASE.multiply(K).toAffine(),z=i.create(oe.x);if(z===_e)return;const ee=i.create(Y*i.create(H+z*v));if(ee===_e)return;let Lt=(oe.x===z?0:2)|Number(oe.y&Le),Dt=ee;return C&&E(ee)&&(Dt=i.neg(ee),Lt^=1),new L(z,Dt,w?void 0:Lt)}return{seed:R,k2sig:F}}function U(m,y,T={}){const{seed:C,k2sig:M}=Q(m,y,T);return hn(e.outputLen,i.BYTES,n)(C,M).toBytes(T.format)}function D(m,y,T,C={}){const{lowS:M,prehash:b,format:S}=ht(C,x);if(T=V(T,void 0,"publicKey"),y=X(y,b),!At(m)){const v=m instanceof L?", use sig.toBytes()":"";throw new Error("verify expects Uint8Array signature"+v)}P(m,S);try{const v=L.fromBytes(m,S),I=s.fromBytes(T);if(M&&v.hasHighS())return!1;const{r:R,s:H}=v,F=q(y),$=i.inv(H),K=i.create(F*$),Y=i.create(R*$),oe=s.BASE.multiplyUnsafe(K).add(I.multiplyUnsafe(Y));return oe.is0()?!1:i.create(oe.x)===R}catch{return!1}}function B(m,y,T={}){const{prehash:C}=ht(T,x);return y=X(y,C),L.fromBytes(m,"recovered").recoverPublicKey(y).toBytes()}return Object.freeze({keygen:l,getPublicKey:d,getSharedSecret:p,utils:_,lengths:f,Point:s,sign:U,verify:D,recoverPublicKey:B,Signature:L,hash:e})}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const Pt={p:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),n:BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),h:BigInt(1),a:BigInt(0),b:BigInt(7),Gx:BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),Gy:BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")},Ln={beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),basises:[[BigInt("0x3086d221a7d46bcde86c90e49284eb15"),-BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),BigInt("0x3086d221a7d46bcde86c90e49284eb15")]]},sr=BigInt(2);function Dn(s){const e=Pt.p,r=BigInt(3),t=BigInt(6),n=BigInt(11),o=BigInt(22),i=BigInt(23),a=BigInt(44),c=BigInt(88),l=s*s*s%e,d=l*l*s%e,p=ae(d,r,e)*d%e,_=ae(p,r,e)*d%e,f=ae(_,sr,e)*l%e,x=ae(f,n,e)*f%e,w=ae(x,o,e)*x%e,E=ae(w,a,e)*w%e,k=ae(E,c,e)*E%e,O=ae(k,a,e)*w%e,P=ae(O,r,e)*d%e,L=ae(P,i,e)*x%e,W=ae(L,t,e)*l%e,q=ae(W,sr,e);if(!mt.eql(mt.sqr(q),s))throw new Error("Cannot find square root");return q}const mt=rt(Pt.p,{sqrt:Dn}),$n=Pn(Pt,{Fp:mt,endo:Ln}),Mn=Nn($n,an),Bn=BigInt(0),Be=BigInt(1),Fn=BigInt(2),Un=BigInt(7),Hn=BigInt(256),Wn=BigInt(113),qr=[],Kr=[],Vr=[];for(let s=0,e=Be,r=1,t=0;s<24;s++){[r,t]=[t,(2*r+3*t)%5],qr.push(2*(5*t+r)),Kr.push((s+1)*(s+2)/2%64);let n=Bn;for(let o=0;o<7;o++)e=(e<<Be^(e>>Un)*Wn)%Hn,e&Fn&&(n^=Be<<(Be<<BigInt(o))-Be);Vr.push(n)}const zr=Zs(Vr,!0),Gn=zr[0],qn=zr[1],nr=(s,e,r)=>r>32?tn(s,e,r):Qs(s,e,r),or=(s,e,r)=>r>32?rn(s,e,r):en(s,e,r);function Kn(s,e=24){const r=new Uint32Array(10);for(let t=24-e;t<24;t++){for(let i=0;i<10;i++)r[i]=s[i]^s[i+10]^s[i+20]^s[i+30]^s[i+40];for(let i=0;i<10;i+=2){const a=(i+8)%10,c=(i+2)%10,l=r[c],d=r[c+1],p=nr(l,d,1)^r[a],_=or(l,d,1)^r[a+1];for(let f=0;f<50;f+=10)s[i+f]^=p,s[i+f+1]^=_}let n=s[2],o=s[3];for(let i=0;i<24;i++){const a=Kr[i],c=nr(n,o,a),l=or(n,o,a),d=qr[i];n=s[d],o=s[d+1],s[d]=c,s[d+1]=l}for(let i=0;i<50;i+=10){for(let a=0;a<10;a++)r[a]=s[i+a];for(let a=0;a<10;a++)s[i+a]^=~r[(a+2)%10]&r[(a+4)%10]}s[0]^=Gn[t],s[1]^=qn[t]}$e(r)}class Ct{constructor(e,r,t,n=!1,o=24){N(this,"state");N(this,"pos",0);N(this,"posOut",0);N(this,"finished",!1);N(this,"state32");N(this,"destroyed",!1);N(this,"blockLen");N(this,"suffix");N(this,"outputLen");N(this,"enableXOF",!1);N(this,"rounds");if(this.blockLen=e,this.suffix=r,this.outputLen=t,this.enableXOF=n,this.rounds=o,we(t,"outputLen"),!(0<e&&e<200))throw new Error("only keccak-f1600 function is supported");this.state=new Uint8Array(200),this.state32=Ws(this.state)}clone(){return this._cloneInto()}keccak(){Vt(this.state32),Kn(this.state32,this.rounds),Vt(this.state32),this.posOut=0,this.pos=0}update(e){De(this),V(e);const{blockLen:r,state:t}=this,n=e.length;for(let o=0;o<n;){const i=Math.min(r-this.pos,n-o);for(let a=0;a<i;a++)t[this.pos++]^=e[o++];this.pos===r&&this.keccak()}return this}finish(){if(this.finished)return;this.finished=!0;const{state:e,suffix:r,pos:t,blockLen:n}=this;e[t]^=r,r&128&&t===n-1&&this.keccak(),e[n-1]^=128,this.keccak()}writeInto(e){De(this,!1),V(e),this.finish();const r=this.state,{blockLen:t}=this;for(let n=0,o=e.length;n<o;){this.posOut>=t&&this.keccak();const i=Math.min(t-this.posOut,o-n);e.set(r.subarray(this.posOut,this.posOut+i),n),this.posOut+=i,n+=i}return e}xofInto(e){if(!this.enableXOF)throw new Error("XOF is not possible for this instance");return this.writeInto(e)}xof(e){return we(e),this.xofInto(new Uint8Array(e))}digestInto(e){if(mr(e,this),this.finished)throw new Error("digest() was already called");return this.writeInto(e),this.destroy(),e}digest(){return this.digestInto(new Uint8Array(this.outputLen))}destroy(){this.destroyed=!0,$e(this.state)}_cloneInto(e){const{blockLen:r,suffix:t,outputLen:n,rounds:o,enableXOF:i}=this;return e||(e=new Ct(r,t,n,i,o)),e.state32.set(this.state32),e.pos=this.pos,e.posOut=this.posOut,e.finished=this.finished,e.rounds=o,e.suffix=t,e.outputLen=n,e.enableXOF=i,e.destroyed=this.destroyed,e}}const Vn=(s,e,r,t={})=>br(()=>new Ct(e,s,r),t),ir=Vn(1,136,32),zn=60;class jn{constructor(){this.jwks=null}setJWKSEndpoint(e){try{this.jwks=nt.createRemoteJWKSet(new URL(e)),u.log("[CROSSx] JWKS 엔드포인트 설정:",e)}catch(r){u.warn("[CROSSx] JWKS 엔드포인트 URL 파싱 실패:",r)}}async verifyJWT(e,r){try{const t=this.decodeJWT(e);u.log("[CROSSx] JWT 디코딩 성공:",{sub:t.sub,exp:t.exp,iat:t.iat});const n=Math.floor(Date.now()/1e3);if(t.exp&&t.exp+zn<n)return u.warn("[CROSSx] 토큰 만료:",{exp:t.exp,now:n,만료시간:new Date(t.exp*1e3).toISOString()}),{payload:t,valid:!1,signatureVerified:!1};if(!t.sub)return u.warn("[CROSSx] JWT에 sub(사용자ID) 없음"),{payload:t,valid:!1,signatureVerified:!1};if(this.jwks)try{const{payload:o}=await nt.jwtVerify(e,this.jwks,{algorithms:["RS256","ES256"]});return u.log("[CROSSx] JWT 서명 검증 성공"),{payload:o,valid:!0,signatureVerified:!0}}catch(o){if(o instanceof Error&&(o.name==="JWSSignatureVerificationFailed"||o.name==="JWTClaimValidationFailed"))return u.error("[CROSSx] JWT 서명 검증 실패:",o),{payload:t,valid:!1,signatureVerified:!1};u.warn("[CROSSx] JWKS 엔드포인트 접근 불가 — 서명 미검증 모드로 전환")}return u.log("[CROSSx] JWT 검증 성공 (서명 미검증 — JWKS 미설정 또는 접근 불가)"),{payload:t,valid:!0,signatureVerified:!1}}catch(t){throw u.error("[CROSSx] JWT 검증 중 에러:",t),t}}decodeJWT(e){return nt.decodeJwt(e)}recoverPersonalSignSigner(e,r){const t=new TextEncoder().encode(e),n=new TextEncoder().encode(`Ethereum Signed Message:
|
|
7
|
+
For security, you must continue with the same account.`,sessionAlert_title:"Session expired",sessionAlert_accountLabel:"Account",sessionAlert_appleAccount:"Apple account",sessionAlert_signOutButton:"Sign out",sessionAlert_signInAgainButton:"Sign in again"},ds={ko:ls,en:_r};function xe(s="en",e){return ds[s]??_r}const Mt="crossx_access_token",Bt="crossx_refresh_token",Ft="crossx_user_info",Qe=class Qe{constructor(e,r,t,n,o,i,a){this.config=e,this.storage=r,this.crypto=t,this.oauth=n,this.transport=o,this.walletProvider=i,this.tokenStore=a,this._refreshPromise=null,this._migrated=!1;const c=e.projectId;this.STORAGE_KEY_ACCESS_TOKEN=`crossx_${c}_access_token`,this.STORAGE_KEY_REFRESH_TOKEN=`crossx_${c}_refresh_token`,this.STORAGE_KEY_USER=`crossx_${c}_user_info`}get useCookieAuth(){return this.config.authMode==="cookie"}extractResponseErrorCode(e){if(e!=null&&e.code&&e.code!==200&&e.code>0)return{code:e.code,message:e.message??""};const r=e==null?void 0:e.data;if(typeof r=="object"&&r!==null&&"code"in r){const t=r;if(t.code&&t.code!==200&&t.code>0)return{code:t.code,message:t.message??""}}return null}checkResponseError(e,r){const t=this.extractResponseErrorCode(e);if(t)throw new g(h.AUTH_FAILED,`${r} 실패 (코드 ${t.code}): ${t.message}`)}async execute(e){let r,t;try{const n=e==null?void 0:e.provider;let o="/login";n==="google"?o="/google":n==="apple"&&(o="/apple");const{oauthServiceUrl:i}=this.config,a=`${i}${o}`;u.log(`[CROSSx] OAuth 팝업 열기 (${n||"일반"} 로그인)`);const c=await this.oauth.openAuth({authUrl:a,expectedOrigin:new URL(i).origin});r=c.token,t=c.email,u.log("[CROSSx] OAuth Firebase 토큰 받음 (length:",r.length,")"),t&&u.log("[CROSSx] OAuth 콜백 email:",t)}catch(n){u.error("[CROSSx] SignIn 에러 (OAuth 단계):",n);const o=n instanceof Error?n.message:"Sign in failed";throw/팝업|popup/i.test(o)?new g(h.OAUTH_POPUP_BLOCKED,o):new g(h.AUTH_FAILED,o)}return this.processFirebaseToken(r,t)}async executeWithOAuthToken(e){return u.log("[CROSSx] signInWithOAuthToken — Firebase 토큰 주입 (length:",e.length,")"),this.processFirebaseToken(e)}async processFirebaseToken(e,r){var o,i,a,c,l;let t,n=!1;try{const{authApiUrl:d}=this.config,{accessToken:p,refreshToken:_}=await this.exchangeFirebaseToken(e,d);let f,x,w;try{const k=this.crypto.decodeJWT(e);u.log("[CROSSx] Firebase JWT 필드:",Object.keys(k).join(", ")),x=(o=k.firebase)==null?void 0:o.sign_in_provider;const O=((i=k.firebase)==null?void 0:i.identities)??{};w=k.email??((a=O.email)==null?void 0:a[0]),x==="google.com"?f=(c=O["google.com"])==null?void 0:c[0]:x==="apple.com"&&(f=(l=O["apple.com"])==null?void 0:l[0]),u.log("[CROSSx] OAuth provider sub 추출 — provider:",x,"hasProviderSub:",!!f,"email:",w??"(없음)")}catch{u.warn("[CROSSx] firebaseToken에서 providerSub 추출 실패")}if(p){const k=this.crypto.decodeJWT(p);u.log("[CROSSx] access_token 디코딩 — sub:",k.sub,"exp:",k.exp,"필드:",Object.keys(k).join(", "));const O=await this.crypto.verifyJWT(p);if(n=O.signatureVerified??!1,!O.valid)throw u.error("[CROSSx] access_token 검증 실패"),new Error("유효하지 않은 access token");const P=O.payload,L=P.email??w??r;u.log("[CROSSx] email 소스 — CROSSx JWT:",P.email??"(없음)","/ Firebase JWT:",w??"(없음)","/ OAuth 콜백:",r??"(없음)","→",L??"(없음)"),t={id:P.sub,email:L,signInProvider:x,providerSub:f},this.tokenStore.set(p),this.useCookieAuth||(await this.storage.set(this.STORAGE_KEY_ACCESS_TOKEN,p),_&&this.config.secureStorageAvailable!==!1?await this.storage.set(this.STORAGE_KEY_REFRESH_TOKEN,_):_&&u.warn("[CROSSx] 안전한 스토리지 미사용 — refresh_token 영속 저장을 건너뜁니다"))}else{const k=this.crypto.decodeJWT(e);t={id:k.sub,email:k.email??r,signInProvider:x,providerSub:f},u.log("[CROSSx] Cookie 모드 — Firebase 토큰에서 사용자 정보 추출 — id:",t.id)}u.log("[CROSSx] 사용자 정보 — id:",t.id,"email:",t.email??"(없음)");const E=this.useCookieAuth?{id:t.id,email:t.email,signInProvider:t.signInProvider,providerSub:t.providerSub}:t;await this.storage.set(this.STORAGE_KEY_USER,E),u.log("[CROSSx] 사용자 정보 저장 완료 (authMode:",this.useCookieAuth?"cookie":"token",")")}catch(d){return u.error("[CROSSx] SignIn 에러 (토큰 교환 단계):",d),{success:!1,error:d instanceof Error?d.message:"Sign in failed"}}return u.log("[CROSSx][Migration Phase 1] 로그인 완료, 지갑 로드 시작 — userId:",t.id),this.loadWallet(t,n)}async exchangeFirebaseToken(e,r){const t=this.useCookieAuth,n=t?`${r}/cross-auth/social/login/cookie`:`${r}/cross-auth/social/login`;u.log("[CROSSx] Firebase 토큰 교환 요청");const o=await this.transport.request({url:n,method:"POST",headers:{"Content-Type":"application/json"},body:{auth_code:e,login_type:"firebase"},...t?{credentials:"include"}:{}});u.log("[CROSSx] 토큰 교환 응답 — status:",o.status);const i=o.data;this.checkResponseError(i,"Token exchange");const a=this.extractAccessToken(i);if(t&&!a)return u.log("[CROSSx] Cookie 모드 — 로그인 성공 (JWT는 HttpOnly 쿠키)"),{};if(!a)throw new g(h.AUTH_FAILED,"토큰 교환 응답에서 access_token을 찾을 수 없습니다");u.log("[CROSSx] access_token 교환 성공");let c;return t||(c=this.extractRefreshToken(i)),{accessToken:a,refreshToken:c}}extractAccessToken(e){const r=(e==null?void 0:e.data)??e;if(this.isJwtString(r))return r;if(typeof r=="object"&&r!==null){const t=r;if(this.isJwtString(t.data))return t.data;if(typeof t.data=="object"&&t.data!==null){const o=t.data,i=o.access_token??o.token;if(typeof i=="string")return i}const n=t.access_token??t.token;if(typeof n=="string")return n}}extractRefreshToken(e){const r=(e==null?void 0:e.data)??e;if(typeof r!="object"||r===null)return;const t=r;if(typeof t.data=="object"&&t.data!==null){const o=t.data,i=o.refresh_token??o.refresh;if(typeof i=="string")return i}const n=t.refresh_token??t.refresh;if(typeof n=="string")return n}isJwtString(e){return typeof e=="string"&&e.split(".").length===3}async restoreSession(){try{await this.migrateStorageKeys();const e=this.tokenStore.get();if(e){const t=await this.crypto.verifyJWT(e);if(t.valid){const n=await this.storage.get(this.STORAGE_KEY_USER);if(n)return u.log("[CROSSx] restoreSession — 메모리 토큰 유효, 세션 복원"),this.loadWallet(n,t.signatureVerified)}this.tokenStore.clear()}if(this.useCookieAuth){u.log("[CROSSx] restoreSession — 쿠키 기반 silentRefresh 시도");const t=await this.silentRefresh();t&&this.tokenStore.set(t)}else{const t=await this.storage.get(this.STORAGE_KEY_ACCESS_TOKEN),n=await this.storage.get(this.STORAGE_KEY_REFRESH_TOKEN);if(u.log("[CROSSx] restoreSession — access_token:",t?"있음":"없음","refresh_token:",n?"있음":"없음"),!n)return u.log("[CROSSx] restoreSession — refresh_token 없음, 세션 복원 생략"),null;t&&this.tokenStore.set(t);const o=await this.silentRefresh(n);u.log("[CROSSx] restoreSession — silentRefresh 결과:",o?"토큰 발급 성공":"토큰 없음"),o&&this.tokenStore.set(o)}const r=await this.storage.get(this.STORAGE_KEY_USER);if(u.log("[CROSSx] restoreSession — userInfo 조회:",r?`있음 (id: ${r.id})`:"없음"),!r)return null;u.log("[CROSSx] restoreSession — silentRefresh 성공, 세션 복원 — userId:",r.id);try{return await this.loadWallet(r,!1)}catch(t){return u.warn("[CROSSx] restoreSession — 지갑 로드 실패 (세션은 유지):",t),{success:!0,user:r,tokenSignatureVerified:!1}}}catch(e){const r=e instanceof g&&e.code===h.SESSION_EXPIRED;return u.log("[CROSSx] restoreSession —",r?"세션 만료 (재로그인 필요)":"세션 복원 실패:",e),this.tokenStore.clear(),r&&(this.useCookieAuth||await this.storage.remove(this.STORAGE_KEY_REFRESH_TOKEN),await this.storage.remove(this.STORAGE_KEY_USER)),null}}silentRefresh(e){return this._refreshPromise&&this._lastRefreshToken===e?this._refreshPromise:(this._lastRefreshToken=e,this._refreshPromise=this._doSilentRefresh(e).finally(()=>{this._refreshPromise=null,this._lastRefreshToken=void 0}),this._refreshPromise)}async _doSilentRefresh(e){const{authApiUrl:r}=this.config,t=this.useCookieAuth,n=`${r}/cross-auth/social/refresh/simple`,o={};if(!t){const d=this.tokenStore.get()??"";d&&(o.access_token=d),e&&(o.refresh_token=e)}const i=await this.transport.request({url:n,method:"POST",headers:{"Content-Type":"application/json"},body:o,...t?{credentials:"include"}:{}});u.log("[CROSSx] silentRefresh 응답 — status:",i.status);const a=i.data,c=this.extractResponseErrorCode(a);if(c){const d=Qe.REFRESH_RELOGIN_CODES.has(c.code);throw new g(d?h.SESSION_EXPIRED:h.AUTH_FAILED,`토큰 갱신 실패 (코드 ${c.code}): ${c.message}`)}const l=this.extractAccessToken(a);if(t&&!l){u.log("[CROSSx] silentRefresh 성공 (cookie 갱신)");return}if(!l)throw new g(h.AUTH_FAILED,"토큰 자동 갱신 실패: 응답에 토큰이 없습니다");if(this.tokenStore.set(l),!t){await this.storage.set(this.STORAGE_KEY_ACCESS_TOKEN,l);const d=this.extractRefreshToken(a);d&&this.config.secureStorageAvailable!==!1&&await this.storage.set(this.STORAGE_KEY_REFRESH_TOKEN,d)}return u.log("[CROSSx] silentRefresh 성공"),l}async refreshAccessToken(){try{if(this.useCookieAuth)return!!await this.silentRefresh()||this.tokenStore.has();const e=await this.storage.get(this.STORAGE_KEY_REFRESH_TOKEN);return e?!!await this.silentRefresh(e):(u.warn("[CROSSx] refreshAccessToken: refresh_token 없음 — 갱신 불가"),!1)}catch(e){return e instanceof g&&e.code===h.SESSION_EXPIRED?(u.warn("[CROSSx] refreshAccessToken: 세션 만료 — 재로그인 필요"),this.tokenStore.clear(),this.useCookieAuth||await this.storage.remove(this.STORAGE_KEY_REFRESH_TOKEN)):u.warn("[CROSSx] refreshAccessToken 실패:",e),!1}}async executeWithJWT(e,r){let t,n=!1;try{let o=await this.crypto.verifyJWT(e);if(n=o.signatureVerified??!1,!o.valid){if(!r)return u.error("[CROSSx] signInWithJWT: access_token 검증 실패, refreshToken 없음"),{success:!1,error:"유효하지 않은 access token"};u.log("[CROSSx] signInWithJWT: access_token 만료, refreshToken으로 갱신 시도");const a=await this.silentRefresh(r);if(!a)return u.error("[CROSSx] signInWithJWT: silentRefresh 실패"),{success:!1,error:"access token이 만료되었고 갱신에 실패했습니다"};if(e=a,o=await this.crypto.verifyJWT(e),n=o.signatureVerified??!1,!o.valid)return u.error("[CROSSx] signInWithJWT: 갱신된 access_token도 유효하지 않음"),{success:!1,error:"갱신된 access token이 유효하지 않습니다"};u.log("[CROSSx] signInWithJWT: silentRefresh 성공, 새 access_token 사용")}const i=o.payload;u.log("[CROSSx] signInWithJWT — sub:",i.sub,"signatureVerified:",n),t={id:i.sub,email:i.email},this.tokenStore.set(e),this.useCookieAuth||(await this.storage.set(this.STORAGE_KEY_ACCESS_TOKEN,e),r&&this.config.secureStorageAvailable!==!1&&await this.storage.set(this.STORAGE_KEY_REFRESH_TOKEN,r)),await this.storage.set(this.STORAGE_KEY_USER,t),u.log("[CROSSx] signInWithJWT — 토큰 및 사용자 정보 저장 완료")}catch(o){return u.error("[CROSSx] signInWithJWT 에러:",o),{success:!1,error:o instanceof Error?o.message:"JWT sign in failed"}}return this.loadWallet(t,n)}async migrateStorageKeys(){if(!this._migrated){this._migrated=!0;try{const e=await this.storage.get(Ft);if(!e||await this.storage.get(this.STORAGE_KEY_USER))return;await this.storage.set(this.STORAGE_KEY_USER,e);const t=await this.storage.get(Mt);t&&await this.storage.set(this.STORAGE_KEY_ACCESS_TOKEN,t);const n=await this.storage.get(Bt);n&&await this.storage.set(this.STORAGE_KEY_REFRESH_TOKEN,n),await this.storage.remove(Ft),await this.storage.remove(Mt),await this.storage.remove(Bt),u.log("[CROSSx] 스토리지 키 마이그레이션 완료 (projectId 스코프)")}catch(e){u.warn("[CROSSx] 스토리지 키 마이그레이션 실패:",e)}}}async loadWallet(e,r){let t,n=!1;try{if(typeof this.walletProvider.checkWallet=="function"){u.log("[CROSSx] GET /mnemonic/check — 지갑 상태 확인 (비밀번호 불필요)");const o=await this.walletProvider.checkWallet();if(u.log("[CROSSx] 지갑 상태:",o),o==="migration_required")u.log("[CROSSx] migration_required → needsMigration = true"),n=!0;else if(o==="exists")try{const i=await this.walletProvider.getAddresses(e.id);i.length>0?(t=i[0].address,u.log("[CROSSx] 캐시된 주소 로드 완료 (비밀번호 불필요):",t)):u.log("[CROSSx] 주소 캐시 없음 — createWallet 단계에서 비밀번호 입력 후 로드")}catch(i){u.warn("[CROSSx] getAddresses 실패, createWallet 단계에서 재시도:",i)}}else u.log("[CROSSx] getOrCreateWallet 직접 호출 (폴백)"),t=(await this.walletProvider.getOrCreateWallet(e.id)).address,u.log("[CROSSx] 지갑 로드 완료 — address:",t)}catch(o){if(o instanceof g&&o.code===h.MIGRATION_BACKUP_EXISTS)u.log("[CROSSx] MIGRATION_BACKUP_EXISTS 감지 → needsMigration = true"),n=!0;else{if(o instanceof g&&(o.code===h.PROJECT_NOT_REGISTERED||o.code===h.PROJECT_ID_MISSING||o.code===h.ORIGIN_NOT_ALLOWED))throw u.error("[CROSSx] 프로젝트 설정 에러:",o.message),o;u.warn("[CROSSx] 지갑 상태 확인 실패 (로그인은 유지):",o)}}return u.log("[CROSSx] loadWallet 결과 — address:",t,"needsMigration:",n),{success:!0,address:t,user:e,needsMigration:n,tokenSignatureVerified:r}}};Qe.REFRESH_RELOGIN_CODES=new Set([1007,1008]);let xt=Qe;class us{constructor(e,r,t){this.config=e,this.storage=r,this.tokenStore=t;const n=e.projectId;this.STORAGE_KEY_ACCESS_TOKEN=`crossx_${n}_access_token`,this.STORAGE_KEY_REFRESH_TOKEN=`crossx_${n}_refresh_token`,this.STORAGE_KEY_USER=`crossx_${n}_user_info`}async execute(){this.tokenStore.clear(),this.config.authMode!=="cookie"&&(await this.storage.remove(this.STORAGE_KEY_ACCESS_TOKEN),await this.storage.remove(this.STORAGE_KEY_REFRESH_TOKEN)),await this.storage.remove(this.STORAGE_KEY_USER),await this.storage.clear()}}class hs{constructor(e,r){this.storage=e,this.walletProvider=r}async execute(e,r){if(!this.walletProvider.migrateWallet)throw new g(h.NOT_IMPLEMENTED,"현재 환경에서는 마이그레이션이 지원되지 않습니다");u.log("[CROSSx][Migration Phase 4] MigrateWalletUseCase.execute() — pin 길이:",e.length,"sub:",r);const t=await this.walletProvider.migrateWallet(e,r);return u.log("[CROSSx][Migration Phase 5] MigrateWalletUseCase 완료 — address:",t.address),{address:t.address}}}class vt{constructor(){this.encryptedBytes=null,this.xorKey=null}set(e){this.clear();const r=new TextEncoder().encode(e),t=new Uint8Array(r.length);crypto.getRandomValues(t);const n=new Uint8Array(r.length);for(let o=0;o<r.length;o++)n[o]=r[o]^t[o];r.fill(0),this.xorKey=t,this.encryptedBytes=n}get(){if(!this.encryptedBytes||!this.xorKey)return null;const e=new Uint8Array(this.encryptedBytes.length);for(let t=0;t<this.encryptedBytes.length;t++)e[t]=this.encryptedBytes[t]^this.xorKey[t];const r=new TextDecoder().decode(e);return e.fill(0),r}clear(){var e,r;(e=this.encryptedBytes)==null||e.fill(0),(r=this.xorKey)==null||r.fill(0),this.encryptedBytes=null,this.xorKey=null}has(){return this.encryptedBytes!==null}}class ps{constructor(e,r){this.chainRegistry=e,this.transport=r,this._nextId=1}async call(e,r,t){const n=await this.chainRegistry.getChain(t),o={jsonrpc:"2.0",method:e,params:r,id:this._nextId++},a=(await this.transport.request({url:n.rpcUrl,method:"POST",headers:{"Content-Type":"application/json"},body:o})).data;if(a!=null&&a.error)throw new g(h.UNKNOWN_ERROR,`RPC 오류 [${e}] (${t}): ${a.error.message} (코드: ${a.error.code})`);return a==null?void 0:a.result}}class fs{constructor(){this.listeners=new Map}on(e,r){return this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e).add(r),()=>this.off(e,r)}off(e,r){var t;(t=this.listeners.get(e))==null||t.delete(r)}emit(e,r){var t;(t=this.listeners.get(e))==null||t.forEach(n=>n(r))}removeAllListeners(){this.listeners.clear()}}const Ne={production:{oauthServiceUrl:"https://cross-wallet-oauth.crosstoken.io",authApiUrl:"https://cross-auth.crosstoken.io",walletGatewayUrl:"https://embedded-wallet-gateway.crosstoken.io/api/v1",portraitBaseUrl:"https://portrait.crosstoken.io"},staging:{oauthServiceUrl:"https://stg-cross-wallet-oauth.crosstoken.io",authApiUrl:"https://stg-cross-auth.crosstoken.io",walletGatewayUrl:"https://stg-embedded-wallet-gateway.crosstoken.io/api/v1",portraitBaseUrl:"https://dev-portrait.crosstoken.io"},development:{oauthServiceUrl:"https://dev-cross-wallet-oauth.crosstoken.io",authApiUrl:"https://dev-cross-auth.crosstoken.io",walletGatewayUrl:"https://dev-embedded-wallet-gateway.crosstoken.io/api/v1",portraitBaseUrl:"https://dev-portrait.crosstoken.io"}};function _s(s){const e=s.environment;return e&&e in Ne?Ne[e]:null}function xr(){try{if(typeof __CROSSX_CONFIG__<"u"){const s=typeof __CROSSX_CONFIG__=="string"?JSON.parse(__CROSSX_CONFIG__):__CROSSX_CONFIG__,e=_s(s);if(e)return e}}catch{}try{const s=process.env.NEXT_PUBLIC_CROSSX_ENVIRONMENT;if(s&&s in Ne)return Ne[s]}catch{}return Ne.production}const xs=2e3,Ut=6e4,gs=1e3,ws=1e4,ms="0x77359400",Ht="0x3B9ACA00",Wt=130,Et=6,Gt=18,qt=3e4,ys=5*60*1e3,bs=30*1e3,je=100;function gr(s){let e=s.length;for(;e>0&&s.charCodeAt(e-1)===48;)e--;return s.slice(0,e)}function Ss(s,e=Et){if(!s||s==="0x0"||s==="0x")return"0";try{const r=BigInt(s);if(r===0n)return"0";const t=10n**BigInt(Gt),n=r/t,i=(r%t).toString().padStart(Gt,"0"),a=gr(i).slice(0,e);return a?`${n}.${a}`:`${n}`}catch{return"?"}}function We(s,e,r,t=Et){if(!(!s||s==="0x"||s==="0x0"))try{const n=BigInt(s);if(n===0n)return;const o=10n**BigInt(r),i=n/o,c=(n%o).toString().padStart(r,"0"),l=gr(c.slice(0,t));return`${l?`${i}.${l}`:`${i}`} ${e}`}catch{return}}function vs(s){const e=s.startsWith("0x")?s.slice(2):s;if(!/^[0-9a-fA-F]+$/.test(e))throw new g(h.SIGNATURE_FAILED,"유효하지 않은 서명: 올바른 hex 문자열이 아닙니다");if(e.length!==Wt)throw new g(h.SIGNATURE_FAILED,`서명 길이가 유효하지 않습니다: ${Wt} hex 문자(65 바이트) 예상, 현재 ${e.length}`)}function Es(s){const e=s.startsWith("0x")?s.slice(2):s;if(!/^[0-9a-fA-F]+$/.test(e))throw new g(h.SIGNATURE_FAILED,"유효하지 않은 서명된 트랜잭션: 올바른 hex 문자열이 아닙니다");if(e.length<2)throw new g(h.SIGNATURE_FAILED,"유효하지 않은 서명된 트랜잭션: 너무 짧습니다")}function As(s,e){const r=Is(e);if(s==="0"){if(r!==void 0&&r!==0)throw new g(h.TYPED_DATA_CHAIN_ID_MISMATCH,`오프체인 서명(chainId=0)에서는 typedData.domain.chainId가 없거나 0이어야 합니다. 현재 값: ${r}`);return}const t=s.match(/^eip155:(\d+)$/);if(t){const n=Number(t[1]);if(r===void 0)throw new g(h.TYPED_DATA_CHAIN_ID_MISMATCH,`온체인 서명(${s})에서는 typedData.domain.chainId가 반드시 있어야 합니다`);if(r!==n)throw new g(h.TYPED_DATA_CHAIN_ID_MISMATCH,`typedData.domain.chainId (${r})가 chainId (${s}, 예상값: ${n})와 일치하지 않습니다`)}}function Is(s){if(s==null||typeof s!="object")return;const e=s.domain;if(e==null||typeof e!="object")return;const r=e.chainId;if(r==null)return;const t=Number(r);return Number.isFinite(t)?t:void 0}class Pe{constructor(e){this.deps=e,this.verifyPinMutex=null}async ensurePinSetup(){if(this.deps.pinStore.has())return;const e=await this.deps.confirmation.showPinSetupPrompt();if(!e)throw new g(h.PIN_CANCELLED,"사용자가 PIN 설정을 취소했습니다");this.deps.pinStore.set(e),u.log("[CROSSx] PIN 설정 완료 (메모리 캐시)")}async ensurePinForSigning(e){if(this.deps.pinStore.has()&&!e)return;e&&this.deps.pinStore.clear();const r=await this.deps.confirmation.showPinInputPrompt({errorMessage:e});if(!r)throw new g(h.PIN_CANCELLED,"사용자가 PIN 입력을 취소했습니다");this.deps.pinStore.set(r),u.log("[CROSSx] PIN 입력 완료 (메모리 캐시)")}async ensureVerifiedPin(e,r,t,n){if(e)this.deps.pinStore.clear();else if(this.deps.pinStore.has())return;if(this.verifyPinMutex&&!e){u.log("[CROSSx] ensureVerifiedPin: 진행 중인 검증 대기"),await this.verifyPinMutex;return}let o,i;this.verifyPinMutex=new Promise((c,l)=>{o=c,i=l});const a=xe(this.deps.getLocale()??"en");try{if(typeof this.deps.walletProvider.verifyPin!="function"){const l=await this.deps.confirmation.showPinInputPrompt({verifyMode:!0,errorMessage:e,lockExpiresAt:r,attemptCount:t,maxAttempts:n});if(!l)throw new g(h.PIN_CANCELLED,"사용자가 PIN 입력을 취소했습니다");this.deps.pinStore.set(l),u.log("[CROSSx] PIN 캐시 완료 (verifyPin 미지원, 로컬 전용)"),o();return}const c=await this.deps.confirmation.showPinInputPrompt({verifyMode:!0,errorMessage:e,lockExpiresAt:r,attemptCount:t,maxAttempts:n,onSubmit:async l=>{this.deps.pinStore.set(l);try{return await this.deps.walletProvider.verifyPin(l)?(u.log("[CROSSx] PIN 서버 검증 완료 (verify-password)"),{ok:!0}):(this.deps.pinStore.clear(),{ok:!1,error:a.pinInput_error})}catch(d){if(this.deps.pinStore.clear(),d instanceof g){if(d.code===h.PIN_WRONG)return{ok:!1,error:a.pinInput_error};if(d.code===h.PIN_INVALID)return{ok:!1,error:d.message};if(d.code===h.PIN_LOCKED)return Pe.buildPinLockedResult(d,a)}throw d}}});if(!c)throw new g(h.PIN_CANCELLED,"사용자가 PIN 입력을 취소했습니다");this.deps.pinStore.set(c),u.log("[CROSSx] PIN 갱신 완료 (verify 모달 닫힘)"),o()}catch(c){if(c instanceof g&&(c.code===h.AUTH_NOT_AUTHENTICATED||c.code===h.AUTH_TOKEN_EXPIRED||c.code===h.SESSION_EXPIRED)&&c.code!==h.SESSION_EXPIRED){const l=xe(this.deps.getLocale()??"en"),d=this.deps.getUserEmail();if(await this.deps.confirmation.showSessionAlert({title:l.sessionAlert_title,message:l.alert_sessionExpired,email:d?this.deps.maskEmail(d,l.sessionAlert_appleAccount):void 0})==="signin-again"&&(await this.deps.signInAgain()).success)throw i(c),c}throw i(c),c}finally{this.verifyPinMutex=null}}async withPinRetry(e){const r=xe(this.deps.getLocale()??"en");let t=null;try{return await e()}catch(n){if(n instanceof g){if(t=Pe.parsePinError(n,r),!t)throw n}else throw n}for(;;){u.warn("[CROSSx] PIN 불일치/잠금 — 재입력 요청:",t.errorMessage),this.deps.pinStore.clear(),await this.ensureVerifiedPin(t.errorMessage,t.lockExpiresAt,t.attemptCount,t.maxAttempts);try{return await e()}catch(n){if(n instanceof g&&(t=Pe.parsePinError(n,r),t))continue;throw n}}}static parsePinError(e,r){if(e.code===h.PIN_WRONG)return{errorMessage:r.pinInput_error};if(e.code===h.PIN_INVALID)return{errorMessage:e.message};if(e.code===h.PIN_LOCKED){const t=e.details;return t!=null&&t.permanent?{errorMessage:r.pinLocked_permanent}:(t==null?void 0:t.remainingAttempts)!=null&&t.remainingAttempts>0&&t.maxAttempts?{errorMessage:r.pinInput_error,attemptCount:t.maxAttempts-t.remainingAttempts,maxAttempts:t.maxAttempts}:{errorMessage:"Too many failed attempts. Your account is temporarily locked.",lockExpiresAt:t==null?void 0:t.lockExpiresAt}}return null}static buildPinLockedResult(e,r){const t=Pe.parsePinError(e,r);if(!t)return{ok:!1,error:e.message};const{errorMessage:n,...o}=t;return{ok:!1,error:n,...o}}}class Ts{constructor(e){this.deps=e}async confirmAndExecuteWithPreparedPin(e){if(!await this.deps.confirmation.requestConfirmation(e.confirmation))throw new g(h.USER_REJECTED,e.rejectedMessage);return this.deps.pinOrchestrator.withPinRetry(async()=>{let t;return this.deps.walletProvider.prepare&&(t=(await this.deps.walletProvider.prepare(e.prepareAction,e.prepareContext)).uuid),await this.deps.pinOrchestrator.ensureVerifiedPin(),e.execute(t)})}}function ks(s){return s==="google"||s==="apple"}class Rs{constructor(e){this.deps=e}async signInAgain(){var r,t;this.deps.ensureInitialized();const e=this.deps.getSessionSnapshot();this.deps.setRecoveringSession(!0);try{this.deps.setAuthenticated(!1),this.deps.clearTokenStore();const n=ks(e.loginType)?{provider:e.loginType}:void 0;let o;try{o=await this.deps.executeSignIn(n)}catch(l){throw this.deps.clearAuthState(),l}if(!o.success)return this.deps.clearAuthState(),o;const i=((r=o.user)==null?void 0:r.providerSub)??null,a=((t=o.user)==null?void 0:t.id)??null;if(!(e.providerSub?!!i&&i===e.providerSub:e.userId?!!a&&e.userId===a:!0)){u.warn("[CROSSx] signInAgain: providerSub/userId 불일치 — 잘못된 계정으로 로그인됨"),await this.deps.executeSignOut().catch(()=>{});const l=xe(this.deps.getLocale()??"en");return await this.deps.showSessionAlert({title:l.sessionAlert_title,message:l.alert_differentAccount,email:e.userEmail?this.deps.maskEmail(e.userEmail,l.sessionAlert_appleAccount):void 0})==="signin-again"?(this.deps.setRecoveringSession(!1),this.signInAgain()):(this.deps.clearAuthState(),{success:!1,error:"Different account signed in"})}this.deps.applyAuthResult(o);try{await this.deps.loadWalletAfterAuth()}catch{u.warn("[CROSSx] signInAgain: loadWalletAfterAuth 실패 (로그인은 유지)")}return u.log("[CROSSx] signInAgain: 세션 복구 성공"),o}finally{this.deps.setRecoveringSession(!1)}}async withSessionRecovery(e){try{return await e()}catch(r){const t=xe(this.deps.getLocale()??"en"),{userEmail:n}=this.deps.getSessionSnapshot(),o=n?this.deps.maskEmail(n,t.sessionAlert_appleAccount):void 0;if(r instanceof g&&r.code===h.WITHDRAW_FAILED){if(u.warn("[CROSSx] WITHDRAW_FAILED — 로컬 인증 초기화"),await this.deps.showSessionAlert({title:t.sessionAlert_title,message:t.alert_accountWithdrawn,email:o})==="signin-again"&&(await this.signInAgain()).success)return e();throw this.deps.clearAuthState(),r}if(r instanceof g&&r.code===h.WALLET_NOT_FOUND){if(u.warn("[CROSSx] WALLET_NOT_FOUND — 로컬 인증 초기화"),await this.deps.showSessionAlert({title:t.sessionAlert_title,message:t.alert_walletNotFound,email:o})==="signin-again"&&(await this.signInAgain()).success)return e();throw this.deps.clearAuthState(),r}if(!(r instanceof g)||r.code!==h.SESSION_EXPIRED)throw r;if(u.warn("[CROSSx] SESSION_EXPIRED 감지 — 자동 재인증 시도"),await this.deps.showSessionAlert({title:t.sessionAlert_title,message:t.alert_sessionExpired,email:o})==="signout")throw this.deps.clearAuthState(),new g(h.SESSION_EXPIRED,"세션이 만료되었습니다. 다시 로그인해 주세요.");const a=await this.signInAgain();if(!a.success)throw new g(h.SESSION_EXPIRED,a.error??"세션이 만료되었습니다. 다시 로그인해 주세요.");return e()}}}class Os{constructor(e){this.deps=e}async fetchWalletStatus(){if(typeof this.deps.walletProvider.checkWallet=="function")try{return await this.deps.walletProvider.checkWallet()}catch(e){return u.warn("[CROSSx] checkWallet 실패 (폴백: not_found 처리):",e),"not_found"}return null}async loadWalletAfterAuth(e,r){const t=this.deps.getUserId();if(!t)return;const n=await this.fetchWalletStatus();if(u.log("[CROSSx] loadWalletAfterAuth 지갑 상태:",n),n!=="exists")return;const o=await this.deps.walletProvider.getAddresses(t);if(o.length>0){const a=r?o.find(d=>d.address.toLowerCase()===r.toLowerCase()):void 0,c=e!==void 0?o.find(d=>d.index===e):void 0,l=a??c??o[0];u.log("[CROSSx] 캐시된 주소 로드 완료 (비밀번호 불필요):",l.address),this.deps.setActiveWallet(l.address,l.index);return}u.log("[CROSSx] 주소 캐시 없음 — 비밀번호 확인 후 address(0) 조회"),await this.deps.pinOrchestrator.ensureVerifiedPin();const i=await this.deps.walletProvider.getAddress(t,0);u.log("[CROSSx] 세션 복원 후 지갑 주소 로드 완료:",i.address),this.deps.setActiveWallet(i.address,0)}}class Ps{constructor(e){this.deps=e}async handleMigrationFlow(e){var c,l;u.log('[CROSSx][Migration Phase 3] "Wallet Found" 팝업 표시');const r=this.deps.getAllowSkip(),t=await this.deps.confirmation.showMigrationFoundPrompt({allowSkip:r});if(u.log("[CROSSx][Migration Phase 3] 사용자 선택:",t),t==="skip")return u.log("[CROSSx][Migration Phase 3] 사용자가 마이그레이션을 건너뜀 → 종료"),null;let n,o=0,i=5,a=null;for(;;){o++,u.log(`[CROSSx][Migration Phase 4] PIN 입력 팝업 표시 (시도 #${o}/${i})`,n?`— 이전 메시지: ${n}`:"");const d=await this.deps.confirmation.showRecoveryPinInputPrompt({errorMessage:n,attemptCount:o>1?o-1:void 0,maxAttempts:i});if(d===null)return u.log("[CROSSx][Migration Phase 4] 사용자가 PIN 입력을 취소함 → 종료"),null;u.log("[CROSSx][Migration Phase 4] PIN 입력 완료 — verify-recovery-pin API 호출"),n=void 0;try{const p=await((l=(c=this.deps.walletProvider).verifyRecoveryPin)==null?void 0:l.call(c,d,e));if(!p){u.log("[CROSSx][Migration Phase 4] verifyRecoveryPin 미지원 — PIN 검증 생략"),a=d;break}if(p.valid){u.log("[CROSSx][Migration Phase 4] PIN 검증 성공"),a=d;break}const _=p.pinStatus;if(i=_.maxAttempts,o=i-_.remainingAttempts,_.remainingAttempts===0&&_.lockExpiresAt){const f=_.lockExpiresAt*1e3,x=Math.max(1,Math.round((f-Date.now())/1e3)),w=x<=1800?"Too many failed attempts. Please try again in 30 minutes.":"Too many failed attempts. Please try again in 24 hours.";u.warn(`[CROSSx][Migration Phase 4] verify-recovery-pin 잠금 — ${x}초, 메시지: ${w}`),await this.deps.confirmation.showRecoveryPinLockedPrompt(x,w),o=0,n="Your account lock has been lifted. You may try again."}else u.warn(`[CROSSx][Migration Phase 4] PIN 불일치 (시도 ${o}/${i})`),n="Incorrect PIN."}catch(p){if(!(p instanceof g))throw p;if(p.code===h.SESSION_EXPIRED){u.warn("[CROSSx][Migration Phase 4] 세션 만료 — 재로그인 후 PIN 입력 재시도");const _=xe(this.deps.getLocale()??"en"),f=this.deps.getUserEmail();if(await this.deps.confirmation.showSessionAlert({title:_.sessionAlert_title,message:_.alert_sessionExpired,email:f?this.deps.maskEmail(f,_.sessionAlert_appleAccount):void 0})==="signin-again"&&(await this.deps.signInAgain()).success){u.log("[CROSSx][Migration Phase 4] 재로그인 성공 — PIN 입력 루프 계속"),o--;continue}throw this.deps.clearAuthState(),p}if(p.code===h.MIGRATION_PIN_LOCKED){const _=p.details;if(i=(_==null?void 0:_.maxAttempts)??5,(_==null?void 0:_.permanent)===!0)return u.warn("[CROSSx][Migration Phase 4] PIN 영구 잠금 (verify-recovery-pin)"),await this.deps.confirmation.showRecoveryPinLockedPrompt(0,"Your account has been permanently locked due to too many failed attempts."),null;const f=((_==null?void 0:_.lockExpiresAt)??0)*1e3,x=Math.max(1,Math.round((f-Date.now())/1e3)),w=x<=1800?"Too many failed attempts. Please try again in 30 minutes.":"Too many failed attempts. Please try again in 24 hours.";u.warn(`[CROSSx][Migration Phase 4] verify-recovery-pin 이미 잠금 — ${x}초`),await this.deps.confirmation.showRecoveryPinLockedPrompt(x,w),o=0,n="Your account lock has been lifted. You may try again."}else throw u.error("[CROSSx][Migration Phase 4] verify-recovery-pin 실패 (복구 불가):",p),p}}u.log("[CROSSx][Migration Phase 5] PIN 검증 완료 — 비밀번호 설정 및 마이그레이션 진행");try{await this.deps.pinOrchestrator.ensurePinSetup();const d=await this.deps.executeMigrate(a,e);return u.log("[CROSSx][Migration Phase 5] 마이그레이션 성공 — address:",d.address),d}catch(d){if(d instanceof g&&d.code===h.MIGRATION_FAILED)return u.warn("[CROSSx][Migration Phase 5] migrate PIN 불일치 (경합) — 처음부터 재시도"),this.handleMigrationFlow(e);if(d instanceof g&&d.code===h.SESSION_EXPIRED){u.warn("[CROSSx][Migration Phase 5] 세션 만료 — 재로그인 후 마이그레이션 재시도");const p=xe(this.deps.getLocale()??"en"),_=this.deps.getUserEmail();if(await this.deps.confirmation.showSessionAlert({title:p.sessionAlert_title,message:p.alert_sessionExpired,email:_?this.deps.maskEmail(_,p.sessionAlert_appleAccount):void 0})==="signin-again"&&(await this.deps.signInAgain()).success){u.log("[CROSSx][Migration Phase 5] 재로그인 성공 — 마이그레이션 재시도 (검증된 PIN 유지)");try{const w=await this.deps.executeMigrate(a,e);return u.log("[CROSSx][Migration Phase 5] 마이그레이션 재시도 성공 — address:",w.address),w}catch(w){throw u.error("[CROSSx][Migration Phase 5] 마이그레이션 재시도 실패:",w),w}}throw this.deps.clearAuthState(),d}throw u.error("[CROSSx][Migration Phase 5] 마이그레이션 실패 (복구 불가):",d),d}}}class Cs{constructor(e){this.deps=e}async withResolvedGasAndFee(e,r){const t=f=>!(f!=null&&f.trim()),n=e.nonce===void 0||e.nonce===null,o=t(e.gasLimit),i=t(e.gasPrice)&&t(e.maxFeePerGas),a=!t(e.maxFeePerGas)&&t(e.maxPriorityFeePerGas);if(!n&&!o&&!i&&!a)return e;const c={...e},l=n?c.from??this.deps.getActiveAddress()??void 0:void 0;l&&u.log("[CROSSx] nonce 비어있음 → eth_getTransactionCount 호출 (from:",l,")"),o&&u.log("[CROSSx] gasLimit 비어있음 → eth_estimateGas 호출"),i&&u.log("[CROSSx] gasPrice & maxFeePerGas 비어있음 → baseFee 조회로 Dynamic/Legacy 판별");const[d,p,_]=await Promise.all([l?this.deps.jsonRpcCall("eth_getTransactionCount",[l,"pending"],r):null,o?this.deps.estimateGas(e,r):null,i?this.deps.getBaseFeePerGas(r):null]);if(l&&(c.nonce=parseInt(d??"0x0",16),u.log("[CROSSx] nonce 결과:",c.nonce)),o&&(c.gasLimit=p,u.log("[CROSSx] estimateGas 결과:",c.gasLimit)),i){const f=_;if(f){const x=Ht;c.maxFeePerGas=`0x${(BigInt(f)+BigInt(x)).toString(16)}`,c.maxPriorityFeePerGas=x,u.log("[CROSSx] Dynamic 체인 감지 — baseFee:",f,"maxFeePerGas:",c.maxFeePerGas,"maxPriorityFeePerGas: 1 Gwei")}else c.gasPrice=ms,u.log("[CROSSx] Legacy 체인 감지 — gasPrice: 2 Gwei")}return!i&&a&&(c.maxPriorityFeePerGas=Ht,u.log("[CROSSx] maxPriorityFeePerGas 비어있음 → 1 Gwei 기본값 적용")),c}}class Ns{constructor(e){this.crypto=e}verifySignatureSigner(e,r,t){if(this.crypto.recoverPersonalSignSigner)try{const n=this.crypto.recoverPersonalSignSigner(e,r);if(n.toLowerCase()!==t.toLowerCase())throw u.error("[CROSSx] 서명 검증 실패: 서명자 주소 불일치",{expected:t,recovered:n}),new g(h.SIGNATURE_SIGNER_MISMATCH,`서명자 주소가 일치하지 않습니다: 예상 ${t}, 복원된 주소 ${n}`);u.log("[CROSSx] 서명 ecrecover 검증 성공")}catch(n){if(n instanceof g)throw n;u.warn("[CROSSx] ecrecover 검증 중 예외 (무시):",n)}}}function Ls(s){return s?s.includes("google")?"google":s.includes("apple")?"apple":s:null}class Ds{constructor(e){this.deps=e}setActiveWallet(e,r){this.deps.setState({address:e,activeWalletIndex:r}),this.deps.emitAddressChanged({address:e,index:r}),this.deps.shouldPersistWalletPreference()&&this.deps.storage.set(this.deps.walletPreferenceKey,{index:r,address:e}).catch(()=>{})}applyAuthResult(e){var t,n,o,i,a;const r={authenticated:e.success,userId:((t=e.user)==null?void 0:t.id)??null,address:e.address??null,activeWalletIndex:0,userEmail:((n=e.user)==null?void 0:n.email)??null,providerSub:((o=e.user)==null?void 0:o.providerSub)??null,loginType:Ls((i=e.user)==null?void 0:i.signInProvider),tokenSignatureVerified:e.tokenSignatureVerified??!1};this.deps.setState(r),this.deps.getIsRecoveringSession()||this.deps.emitAuthChanged({isAuthenticated:e.success,address:e.address??null,userId:((a=e.user)==null?void 0:a.id)??null})}clearAuthState(){this.deps.setState({authenticated:!1,userId:null,address:null,activeWalletIndex:0,userEmail:null,providerSub:null,loginType:null,tokenSignatureVerified:!1}),this.deps.pinStore.clear(),this.deps.shouldPersistWalletPreference()&&this.deps.storage.remove(this.deps.walletPreferenceKey).catch(()=>{}),this.deps.getIsRecoveringSession()||this.deps.emitAuthChanged({isAuthenticated:!1,address:null,userId:null})}}class $s{constructor(e){this.deps=e}async resolveAddress(e){const r=this.deps.getActiveWalletIndex(),t=this.deps.getActiveAddress();if(e===void 0||e===r){if(!t)throw new g(h.AUTH_NOT_AUTHENTICATED,"활성 지갑 주소를 찾을 수 없습니다");return{address:t,index:r}}return{address:(await this.deps.pinOrchestrator.withPinRetry(()=>this.deps.walletProvider.getAddress(this.deps.getUserId(),e))).address,index:e}}}class Ms{constructor(e){this.deps=e}ensureInitialized(){if(!this.deps.isInitialized())throw new g(h.AUTH_NOT_INITIALIZED,"SDK가 초기화되지 않았습니다. initialize()를 먼저 호출하세요.")}ensureAuthenticated(){if(this.ensureInitialized(),!this.deps.isAuthenticated())throw new g(h.AUTH_NOT_AUTHENTICATED,"인증되지 않은 상태입니다. signIn()를 먼저 호출하세요.")}}class Bs{constructor(e){this.deps=e}async createWallet(){const e=this.deps.getUserId();if(!e)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");u.log("[CROSSx] createWallet 시작");const r=await this.deps.walletLifecycleService.fetchWalletStatus();if(u.log("[CROSSx] 지갑 상태:",r),r==="migration_required"){const t=this.deps.getProviderSub()??e;u.log("[CROSSx] migration_required → 마이그레이션 UI 시작 — sub:",t);const n=await this.deps.migrationOrchestrator.handleMigrationFlow(t);if(n)return this.deps.setActiveWallet(n.address,0),{address:n.address};throw new g(h.MIGRATION_FAILED,"사용자가 마이그레이션을 건너뛰었습니다")}return r==="exists"?(await this.deps.pinOrchestrator.ensureVerifiedPin(),this.tryGetOrCreateWalletWithMigrationFallback(e)):(await this.deps.pinOrchestrator.ensurePinSetup(),this.tryGetOrCreateWalletWithMigrationFallback(e))}async tryGetOrCreateWalletWithMigrationFallback(e){try{const r=await this.deps.pinOrchestrator.withPinRetry(()=>this.deps.walletProvider.getOrCreateWallet(e));return this.deps.setActiveWallet(r.address,0),u.log("[CROSSx] 지갑 준비 완료:",r.address),{address:r.address}}catch(r){if(r instanceof g&&r.code===h.MIGRATION_BACKUP_EXISTS){const t=this.deps.getProviderSub()??e;u.log("[CROSSx] MIGRATION_BACKUP_EXISTS (폴백) → 마이그레이션 UI 시작");const n=await this.deps.migrationOrchestrator.handleMigrationFlow(t);if(n)return this.deps.setActiveWallet(n.address,0),{address:n.address};throw new g(h.MIGRATION_FAILED,"사용자가 마이그레이션을 건너뛰었습니다")}throw r}}}function it(s,e="Apple account"){if(s.endsWith("@privaterelay.appleid.com"))return e;const r=s.indexOf("@");if(r<0)return"*".repeat(s.length||3);const t=s.substring(0,r),n=s.substring(r);return t.length<=1?`${t}${"*".repeat(Math.max(t.length,1))}${n}`:`${t[0]}${"*".repeat(t.length-1)}${n}`}const Ie=class Ie extends fs{constructor(e,r,t,n,o,i,a,c,l,d,p){var _,f;super(),this.storage=t,this.crypto=n,this.transport=o,this.oauth=i,this.walletProvider=a,this.tokenStore=c,this.initialized=!1,this._initPromise=null,this.authenticated=!1,this.userId=null,this.address=null,this.activeWalletIndex=0,this.userEmail=null,this.loginType=null,this.providerSub=null,this.tokenSignatureVerified=!1,this._themeMediaCleanup=null,this._isRecoveringSession=!1,this._config=Object.freeze({...e}),this.internalConfig=e,this.adapterConfig=r,e.logger&&$t(e.logger),this.confirmation=l,this.pinStore=p??new vt,this.stateManager=new Ds({setState:x=>{x.authenticated!==void 0&&(this.authenticated=x.authenticated),x.userId!==void 0&&(this.userId=x.userId),x.address!==void 0&&(this.address=x.address),x.activeWalletIndex!==void 0&&(this.activeWalletIndex=x.activeWalletIndex),x.userEmail!==void 0&&(this.userEmail=x.userEmail),x.providerSub!==void 0&&(this.providerSub=x.providerSub),x.loginType!==void 0&&(this.loginType=x.loginType),x.tokenSignatureVerified!==void 0&&(this.tokenSignatureVerified=x.tokenSignatureVerified)},getIsRecoveringSession:()=>this._isRecoveringSession,emitAddressChanged:({address:x,index:w})=>{this.emit("addressChanged",{address:x,index:w})},emitAuthChanged:({isAuthenticated:x,address:w,userId:E})=>{this.emit("authChanged",{isAuthenticated:x,address:w,userId:E})},shouldPersistWalletPreference:()=>this._config.persistWalletPreference!==!1,walletPreferenceKey:Ie.WALLET_PREF_KEY,storage:this.storage,pinStore:this.pinStore}),this.pinOrchestrator=new Pe({confirmation:this.confirmation,walletProvider:this.walletProvider,pinStore:this.pinStore,getLocale:()=>this._config.locale,getUserEmail:()=>this.userEmail,maskEmail:it,signInAgain:()=>this.signInAgain()}),this.signingOrchestrator=new Ts({confirmation:this.confirmation,walletProvider:this.walletProvider,pinOrchestrator:this.pinOrchestrator}),this.walletLifecycleService=new Os({walletProvider:this.walletProvider,pinOrchestrator:this.pinOrchestrator,getUserId:()=>this.userId,setActiveWallet:(x,w)=>this.setActiveWallet(x,w)}),this.migrationOrchestrator=new Ps({confirmation:this.confirmation,walletProvider:this.walletProvider,pinOrchestrator:this.pinOrchestrator,executeMigrate:(x,w)=>this.migrateWalletUseCase.execute(x,w),getLocale:()=>this._config.locale,getUserEmail:()=>this.userEmail,maskEmail:it,signInAgain:()=>this.signInAgain(),clearAuthState:()=>this.clearAuthState(),getAllowSkip:()=>{var x;return((x=this._config.migration)==null?void 0:x.allowSkip)??!0}}),this.transactionLifecycleService=new Cs({getActiveAddress:()=>this.address,estimateGas:(x,w)=>this.estimateGas(x,w),getBaseFeePerGas:x=>this.getBaseFeePerGas(x),jsonRpcCall:(x,w,E)=>this.jsonRpc.call(x,w,E)}),this.signatureVerifier=new Ns(this.crypto),this.addressResolverService=new $s({walletProvider:this.walletProvider,pinOrchestrator:this.pinOrchestrator,getActiveAddress:()=>this.address,getActiveWalletIndex:()=>this.activeWalletIndex,getUserId:()=>this.userId}),this.authGuardService=new Ms({isInitialized:()=>this.initialized,isAuthenticated:()=>this.authenticated}),this.walletCreationService=new Bs({walletProvider:this.walletProvider,pinOrchestrator:this.pinOrchestrator,migrationOrchestrator:this.migrationOrchestrator,walletLifecycleService:this.walletLifecycleService,getUserId:()=>this.userId,getProviderSub:()=>this.providerSub,setActiveWallet:(x,w)=>this.setActiveWallet(x,w)}),this.sessionOrchestrator=new Rs({ensureInitialized:()=>this.ensureInitialized(),getSessionSnapshot:()=>({providerSub:this.providerSub,userId:this.userId,loginType:this.loginType,userEmail:this.userEmail}),setRecoveringSession:x=>{this._isRecoveringSession=x},setAuthenticated:x=>{this.authenticated=x},clearTokenStore:()=>this.tokenStore.clear(),executeSignIn:x=>this.signInUseCase.execute(x),executeSignOut:()=>this.signOutUseCase.execute(),showSessionAlert:x=>this.confirmation.showSessionAlert(x),clearAuthState:()=>this.clearAuthState(),applyAuthResult:x=>this.applyAuthResult(x),loadWalletAfterAuth:()=>this.walletLifecycleService.loadWalletAfterAuth(),getLocale:()=>this._config.locale,maskEmail:it}),this.chainRegistry=d,this.jsonRpc=new ps(d,o),this.signInUseCase=new xt(this.internalConfig,t,n,i,o,a,c),this.signOutUseCase=new us(this.internalConfig,t,c),this.migrateWalletUseCase=new hs(t,a),(_=a.setOnUnauthorized)==null||_.call(a,()=>this.forceLogout()),(f=a.setTokenRefresher)==null||f.call(a,()=>this.signInUseCase.refreshAccessToken()),e.autoDetectTheme&&this._setupAutoDetectTheme()}get config(){return this._config}async initialize(e){return this.initialized?this.authenticated?{success:!0,address:this.address??void 0}:null:this._initPromise?this._initPromise:(this._initPromise=this._doInitialize(e).finally(()=>{this._initPromise=null}),this._initPromise)}async _doInitialize(e){var r,t;u.log("[CROSSx SDK] v2.1.3 초기화 중..."),this.confirmation.setMessages(xe(this._config.locale));try{const n=xr();this.internalConfig.oauthServiceUrl=n.oauthServiceUrl,this.internalConfig.authApiUrl=n.authApiUrl,this.internalConfig.walletGatewayUrl=n.walletGatewayUrl,this.adapterConfig.gatewayUrl=n.walletGatewayUrl,(t=(r=this.crypto).setJWKSEndpoint)==null||t.call(r,`${n.authApiUrl}/.well-known/jwks.json`);const o=this._config.persistWalletPreference!==!1?await this.storage.get(Ie.WALLET_PREF_KEY).catch(()=>null):null,i=(e==null?void 0:e.preferredWalletIndex)??(o==null?void 0:o.index),a=(e==null?void 0:e.preferredWalletAddress)??(o==null?void 0:o.address),c=await this.signInUseCase.restoreSession();if(c!=null&&c.success)if(this.applyAuthResult(c),this.address&&(!a||this.address.toLowerCase()===a.toLowerCase()))this.address&&this.setActiveWallet(this.address,i??this.activeWalletIndex);else try{await this.loadWalletAfterAuth(i,a)}catch(d){if(!(d instanceof g&&d.code===h.PIN_CANCELLED))throw d;u.log("[CROSSx] initialize: 비밀번호 입력 취소 — 지갑 미로드 상태로 계속")}return this.initialized=!0,u.info("[CROSSx SDK] v2.1.3 초기화 완료"),this.emit("initialized",{restored:!!(c!=null&&c.success)}),c??null}catch(n){throw new g(h.AUTH_NOT_INITIALIZED,"SDK 초기화에 실패했습니다",n)}}async signIn(e){if(this.ensureInitialized(),this.authenticated)throw new g(h.ALREADY_AUTHENTICATED,"이미 로그인된 상태입니다. signOut()을 먼저 호출하세요.");let r=e;if(!(e!=null&&e.provider)){const t=await this.confirmation.showLoginSelector({connectOtherWallets:os(this._config)});if(t===null)return{success:!1,error:"User cancelled login"};if(t.type==="external")throw this.emit("connectExternalWallet",{walletId:t.walletId}),new g(h.EXTERNAL_WALLET_REQUESTED,"User requested external wallet connection");r={...e,provider:t.provider}}try{const t=await this.signInUseCase.execute(r);return t.success&&this.applyAuthResult(t),t}catch(t){throw t instanceof g?t:new g(h.AUTH_FAILED,"로그인에 실패했습니다",t)}}async signInWithCreate(e){const{preferredWalletAddress:r,...t}=e??{},n=await this.signIn(t);if(!n.success)return{...n,addresses:[]};try{if(n.needsMigration||!n.address){const{address:a}=await this.createWallet(),c=await this.getAddresses(),l=await this.selectWalletIfMultiple(c,r);return{...n,address:(l==null?void 0:l.address)??a,needsMigration:!1,addresses:c}}const o=await this.getAddresses(),i=await this.selectWalletIfMultiple(o,r);return{...n,address:(i==null?void 0:i.address)??n.address,addresses:o}}catch(o){throw o instanceof g&&o.code===h.PIN_CANCELLED&&(u.log("[CROSSx] signInWithCreate: 비밀번호 설정 취소 — 자동 로그아웃 진행"),await this.signOut().catch(i=>u.warn("[CROSSx] signInWithCreate: 자동 로그아웃 실패",i))),o}}async signInWithOAuthToken(e){if(this.ensureInitialized(),this.authenticated)throw new g(h.ALREADY_AUTHENTICATED,"이미 로그인된 상태입니다. signOut()을 먼저 호출하세요.");try{const r=await this.signInUseCase.executeWithOAuthToken(e);return r.success&&this.applyAuthResult(r),r}catch(r){throw r instanceof g?r:new g(h.AUTH_FAILED,"OAuth 토큰 로그인에 실패했습니다",r)}}async signInWithJWT(e,r){if(this.ensureInitialized(),this.authenticated)throw new g(h.ALREADY_AUTHENTICATED,"이미 로그인된 상태입니다. signOut()을 먼저 호출하세요.");try{const t=await this.signInUseCase.executeWithJWT(e,r);return t.success&&this.applyAuthResult(t),t}catch(t){throw t instanceof g?t:new g(h.AUTH_FAILED,"JWT 로그인에 실패했습니다",t)}}async signOut(){this.ensureInitialized();try{await this.signOutUseCase.execute(),this.clearAuthState()}catch(e){throw new g(h.UNKNOWN_ERROR,"로그아웃에 실패했습니다",e)}}isAuthenticated(){return this.authenticated}async whenReady(){if(this.initialized)return!0;if(this._initPromise)try{return await this._initPromise,this.initialized}catch{return!1}return!1}get currentAddress(){return this.address}get currentUserId(){return this.userId}isLoggedIn(){return this.isAuthenticated()}async ensureLoggedIn(){if(!this.initialized)return!1;if(this.authenticated)return!0;try{const e=await this.signInUseCase.restoreSession();if(e!=null&&e.success){this.applyAuthResult(e);try{await this.loadWalletAfterAuth()}catch(r){r instanceof g&&r.code===h.PIN_CANCELLED||u.warn("[CROSSx] ensureLoggedIn: loadWalletAfterAuth 실패:",r)}return!0}return!1}catch{return!1}}async getUserInfo(){return this.ensureAuthenticated(),{id:this.userId,email:this.userEmail??void 0,loginType:this.loginType??void 0,addresses:this.address?[this.address]:[],tokenSignatureVerified:this.tokenSignatureVerified}}async migrateWallet(e){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");try{const r=await this.migrateWalletUseCase.execute(e,this.userId);return this.setActiveWallet(r.address,0),u.log("[CROSSx] migrateWallet 완료 — address:",r.address),r}catch(r){throw r instanceof g?r:new g(h.MIGRATION_FAILED,"지갑 마이그레이션에 실패했습니다",r)}}async getAddress(e){if(e!==void 0){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");return{address:(await this.walletProvider.getAddress(this.userId,e)).address,index:e}}return!this.authenticated||!this.address?null:{address:this.address,index:this.activeWalletIndex}}async getAddresses(){if(this.ensureAuthenticated(),!this.userId)return[];const e=await this.withSessionRecovery(()=>this.walletProvider.getAddresses(this.userId));return e.length===0&&this.address?[{address:this.address,index:0}]:e}async selectWallet(e){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");let r=await this.withSessionRecovery(()=>this.walletProvider.getAddresses(this.userId));r.length===0&&this.address&&(r=[{address:this.address,index:0}]);const t=await this.confirmation.showWalletSelector(r,async()=>{await this.pinOrchestrator.ensurePinForSigning();const n=r.length,i={address:(await this.pinOrchestrator.withPinRetry(()=>this.withSessionRecovery(()=>this.walletProvider.getAddress(this.userId,n)))).address,index:n};return r.push(i),i},e);return t&&this.setActiveWallet(t.address,t.index),t}async selectWalletIfMultiple(e,r){if(e.length<2)return null;if(r){const t=e.find(n=>n.address.toLowerCase()===r.toLowerCase());if(t)return this.setActiveWallet(t.address,t.index),t}return this.selectWallet()}async getChains(){this.ensureInitialized();try{return await this.chainRegistry.getChains()}catch(e){throw e instanceof g?e:new g(h.UNKNOWN_ERROR,"체인 목록 조회에 실패했습니다",e)}}async getChain(e){this.ensureInitialized();try{return await this.chainRegistry.getChain(e)}catch(r){throw r instanceof g?r:new g(h.CHAIN_NOT_SUPPORTED,`체인 조회에 실패했습니다: ${e}`,r)}}_setupAutoDetectTheme(){if(typeof window>"u"||!window.matchMedia)return;const e=window.matchMedia("(prefers-color-scheme: dark)"),r=n=>{const o=n?"dark":"light";this.applyTheme(o,this._config.themeTokens??{})};r(e.matches);const t=n=>r(n.matches);e.addEventListener("change",t),this._themeMediaCleanup=()=>e.removeEventListener("change",t)}applyTheme(e=this._config.theme??"light",r=this._config.themeTokens??{}){this._config=Object.freeze({...this._config,theme:e,themeTokens:r}),this.confirmation.setTheme(e,r)}applyLocale(e=this._config.locale??"en"){this._config=Object.freeze({...this._config,locale:e}),this.confirmation.setMessages(xe(e))}async createWallet(){return this.withSessionRecovery(()=>this._createWallet())}async _createWallet(){return this.ensureAuthenticated(),this.walletCreationService.createWallet()}async signMessage(e,r,t){return this.withSessionRecovery(()=>this._signMessage(e,r,t))}async _signMessage(e,r,t){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");const n=await this.resolveAddress(t==null?void 0:t.index);try{const o=await this.signingOrchestrator.confirmAndExecuteWithPreparedPin({confirmation:{type:"sign-message",chainId:e,from:n.address,message:r,dappName:(t==null?void 0:t.dappName)??this._config.appName,accountName:t==null?void 0:t.accountName},rejectedMessage:"User rejected the message signing request",prepareAction:"sign-message",prepareContext:{message:r,from:n.address},execute:i=>this.walletProvider.signMessage(this.userId,e,r,t==null?void 0:t.index,i,n.address)});return this.signatureVerifier.verifySignatureSigner(r,o.signature,n.address),{chainId:e,signature:o.signature,message:r,address:n.address}}catch(o){throw o instanceof g?o:new g(h.SIGNATURE_FAILED,`메시지 서명에 실패했습니다 (${e})`,o)}}async signTypedData(e,r,t){return this.withSessionRecovery(()=>this._signTypedData(e,r,t))}async _signTypedData(e,r,t){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");if(!this.walletProvider.signTypedData)throw new g(h.NOT_IMPLEMENTED,"signTypedData가 구현되어 있지 않습니다");As(e,r);const n=await this.resolveAddress(t==null?void 0:t.index);try{const o=await this.signingOrchestrator.confirmAndExecuteWithPreparedPin({confirmation:{type:"sign-typed-data",chainId:e,from:n.address,typedData:r,dappName:(t==null?void 0:t.dappName)??this._config.appName,accountName:t==null?void 0:t.accountName},rejectedMessage:"User rejected the typed data signing request",prepareAction:"sign-typed-data",prepareContext:{typedData:r,from:n.address},execute:i=>this.walletProvider.signTypedData(this.userId,e,r,t==null?void 0:t.index,i,n.address)});return vs(o.signature),{chainId:e,signature:o.signature,address:n.address}}catch(o){throw o instanceof g?o:new g(h.SIGNATURE_FAILED,`타입 데이터 서명에 실패했습니다 (${e})`,o)}}async signTypedDataOffchain(e,r){return this.signTypedData(Ie.OFFCHAIN_CHAIN_ID,e,r)}async signTransaction(e,r,t){return this.withSessionRecovery(()=>this._signTransaction(e,r,t))}async _signTransaction(e,r,t){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");const n=await this.transactionLifecycleService.withResolvedGasAndFee(r,e);n.from||(n.from=(await this.resolveAddress(t==null?void 0:t.index)).address);const o=ot(e);try{const i=await this.signingOrchestrator.confirmAndExecuteWithPreparedPin({confirmation:{type:"sign",chainId:e,from:n.from,to:n.to,value:n.value,data:n.data,gasLimit:n.gasLimit,gasPrice:n.gasPrice,maxFeePerGas:n.maxFeePerGas,maxPriorityFeePerGas:n.maxPriorityFeePerGas,nativeSymbol:o.symbol,nativeDecimals:o.decimals,dappName:(t==null?void 0:t.dappName)??this._config.appName,accountName:t==null?void 0:t.accountName},rejectedMessage:"User rejected the transaction signing request",prepareAction:"sign",prepareContext:{tx:n},execute:a=>this.walletProvider.signTransaction(this.userId,e,n,t==null?void 0:t.index,a)});return Es(i.signature),{chainId:e,signedTx:i.signature,txHash:i.txHash??""}}catch(i){if(i instanceof g)throw i;const a=i instanceof Error?i.message:String(i);throw new g(h.SIGNATURE_FAILED,`트랜잭션 서명에 실패했습니다 (${e}): ${a}`,i)}}async sendTransaction(e,r,t){return this.withSessionRecovery(()=>this._sendTransaction(e,r,t))}async _sendTransaction(e,r,t){if(this.ensureAuthenticated(),!this.userId)throw new g(h.AUTH_NOT_AUTHENTICATED,"사용자 ID를 찾을 수 없습니다");const n=await this.transactionLifecycleService.withResolvedGasAndFee(r,e);n.from||(n.from=(await this.resolveAddress(t==null?void 0:t.index)).address);const o=ot(e);try{const i=await this.signingOrchestrator.confirmAndExecuteWithPreparedPin({confirmation:{type:"send",chainId:e,from:n.from,to:n.to,value:n.value,data:n.data,gasLimit:n.gasLimit,gasPrice:n.gasPrice,maxFeePerGas:n.maxFeePerGas,maxPriorityFeePerGas:n.maxPriorityFeePerGas,nativeSymbol:o.symbol,nativeDecimals:o.decimals,dappName:(t==null?void 0:t.dappName)??this._config.appName,accountName:t==null?void 0:t.accountName},rejectedMessage:"User rejected the transaction request",prepareAction:"send",prepareContext:{tx:n},execute:async a=>{if(this.walletProvider.sendTransaction)return(await this.walletProvider.sendTransaction(this.userId,e,n,a)).txHash;const c=await this.walletProvider.signTransaction(this.userId,e,n,t==null?void 0:t.index,a);return c.txHash??c.signature}});return{chainId:e,txHash:i,status:"pending"}}catch(i){if(i instanceof g)throw i;const a=i instanceof Error?i.message:String(i);throw new g(h.TRANSACTION_FAILED,`트랜잭션 전송에 실패했습니다 (${e}): ${a}`,i)}}async getTransactionReceipt(e,r){try{return await this.jsonRpc.call("eth_getTransactionReceipt",[e],r)??null}catch{return null}}async waitForTxAndGetReceipt(e,r,t={}){const n=t.intervalMs??gs,o=ws,i=t.timeoutMs??Ut,a=Date.now()+i;let c=n;for(;Date.now()<a;){const l=await this.getTransactionReceipt(e,r);if(l)return l;await new Promise(d=>setTimeout(d,c)),c=Math.min(c*2,o)}throw new g(h.UNKNOWN_ERROR,`트랜잭션 영수증 조회 시간이 초과되었습니다 (${e})`)}async sendTransactionWithWaitForReceipt(e,r,t={}){var w,E;const{intervalMs:n,timeoutMs:o,...i}=t,{txHash:a}=await this.sendTransaction(e,r,i),c=n??((w=this._config.receiptPolling)==null?void 0:w.intervalMs)??xs,l=o??((E=this._config.receiptPolling)==null?void 0:E.timeoutMs)??Ut,d=ot(e),p=r.from??"";let _,f;const x=this.waitForTxAndGetReceipt(a,e,{intervalMs:c,timeoutMs:l}).then(k=>{_=k;const O=BigInt(k.gasUsed)*BigInt(k.effectiveGasPrice),P=r.value?BigInt(r.value):0n,L=We(r.value,d.symbol,d.decimals,this._config.displayDecimals),W=We("0x"+O.toString(16),d.symbol,d.decimals,this._config.displayDecimals),q=We("0x"+(P+O).toString(16),d.symbol,d.decimals,this._config.displayDecimals);return{chainId:e,txHash:a,from:k.from,to:k.to??r.to,amount:L,fees:W,total:q,nativeSymbol:d.symbol,status:k.status==="0x1"?"success":"reverted"}}).catch(k=>(f=k instanceof Error?k:new Error(String(k)),{chainId:e,txHash:a,from:p,to:r.to,amount:We(r.value,d.symbol,d.decimals,this._config.displayDecimals),nativeSymbol:d.symbol,status:"timeout"}));if(await this.confirmation.showTransactionProgress({chainId:e,txHash:a,from:p,to:r.to},x),f)throw f;return{chainId:e,txHash:a,receipt:_}}setPin(e){this.pinStore.set(e)}clearPin(){this.pinStore.clear()}hasPin(){return this.pinStore.has()}async changePin(e,r){this.ensureAuthenticated();const t=this.walletProvider;if(typeof t.changePin!="function")throw new g(h.NOT_IMPLEMENTED,"changePin이 구현되어 있지 않습니다");this.pinStore.set(e);try{await t.changePin(e,r),this.pinStore.set(r),u.log("[CROSSx] PIN 변경 완료")}catch(n){throw n instanceof g?n:new g(h.UNKNOWN_ERROR,"PIN 변경에 실패했습니다",n)}}async getGasPrice(e){this.ensureAuthenticated();try{return await this.walletRpc("eth_gasPrice",[],e)}catch(r){const t=r instanceof Error?r.message:String(r);throw new g(h.GAS_ESTIMATION_FAILED,`가스 가격 조회에 실패했습니다 (${e}): ${t}`,r)}}async estimateGas(e,r){this.ensureAuthenticated();const t={};e.from&&(t.from=e.from),e.to&&(t.to=e.to),e.value&&(t.value=e.value),e.data&&(t.data=e.data),e.gasPrice&&(t.gasPrice=e.gasPrice),e.maxFeePerGas&&(t.maxFeePerGas=e.maxFeePerGas),e.maxPriorityFeePerGas&&(t.maxPriorityFeePerGas=e.maxPriorityFeePerGas),e.nonce!==void 0&&(t.nonce="0x"+e.nonce.toString(16));try{return await this.walletRpc("eth_estimateGas",[t],r)}catch(n){const o=n instanceof Error?n.message:String(n);throw new g(h.GAS_ESTIMATION_FAILED,`가스 추정에 실패했습니다 (${r}): ${o}`,n)}}async getBaseFeePerGas(e){this.ensureAuthenticated();try{const r=await this.walletRpc("eth_getBlockByNumber",["latest",!1],e);return(r==null?void 0:r.baseFeePerGas)??null}catch(r){const t=r instanceof Error?r.message:String(r);throw new g(h.GAS_ESTIMATION_FAILED,`baseFeePerGas 조회에 실패했습니다 (${e}): ${t}`,r)}}async getMaxPriorityFeePerGas(e){this.ensureAuthenticated();try{return await this.walletRpc("eth_maxPriorityFeePerGas",[],e)}catch(r){const t=r instanceof Error?r.message:String(r);throw new g(h.GAS_ESTIMATION_FAILED,`maxPriorityFeePerGas 조회에 실패했습니다 (${e}): ${t}`,r)}}async getNonce(e){this.ensureAuthenticated();const r=this.address;if(!r)return 0;try{const t=await this.jsonRpc.call("eth_getTransactionCount",[r,"pending"],e);return parseInt(t??"0x0",16)}catch(t){if(t instanceof g)throw t;const n=t instanceof Error?t.message:String(t);throw new g(h.UNKNOWN_ERROR,`Nonce 조회에 실패했습니다: ${n}`,t)}}async getBalance(e){this.ensureAuthenticated();const r=this.address;if(!r)return{wei:"0x0",formatted:"0",chainId:e};try{const n=await this.jsonRpc.call("eth_getBalance",[r,"latest"],e)??"0x0";return{wei:n,formatted:Ss(n,this._config.displayDecimals),chainId:e}}catch(t){if(t instanceof g)throw t;const n=t instanceof Error?t.message:String(t);throw new g(h.UNKNOWN_ERROR,`잔액 조회에 실패했습니다 (${e}): ${n}`,t)}}getProvider(e){return this.ensureAuthenticated(),new fr(this,e)}async walletRpc(e,r,t){this.ensureAuthenticated();try{return await this.jsonRpc.call(e,r,t)}catch(n){if(n instanceof g)throw n;const o=n instanceof Error?n.message:String(n);throw new g(h.UNKNOWN_ERROR,`walletRpc 호출에 실패했습니다 [${e}] (${t}): ${o}`,n)}}setActiveWallet(e,r){this.stateManager.setActiveWallet(e,r)}applyAuthResult(e){this.stateManager.applyAuthResult(e)}clearAuthState(){this.stateManager.clearAuthState()}forceLogout(){this.authenticated&&(u.warn("[CROSSx] 강제 로그아웃 (Gateway -10002/-10033)"),this.clearAuthState())}async signInAgain(){return this.sessionOrchestrator.signInAgain()}async withSessionRecovery(e){return this.sessionOrchestrator.withSessionRecovery(e)}async loadWalletAfterAuth(e,r){return this.walletLifecycleService.loadWalletAfterAuth(e,r)}async resolveAddress(e){return this.addressResolverService.resolveAddress(e)}ensureInitialized(){this.authGuardService.ensureInitialized()}ensureAuthenticated(){this.authGuardService.ensureAuthenticated()}dispose(){var e;(e=this._themeMediaCleanup)==null||e.call(this),this._themeMediaCleanup=null,this.clearAuthState(),this.tokenStore.clear(),this.pinStore.clear(),this.initialized=!1,this.removeAllListeners(),$t(null)}_getInternalContext(){return{transport:this.transport,storage:this.storage,walletProvider:this.walletProvider,confirmation:this.confirmation,tokenStore:this.tokenStore,pinStore:this.pinStore,adapterConfig:this.adapterConfig}}};Ie.WALLET_PREF_KEY="wallet_preference",Ie.OFFCHAIN_CHAIN_ID="0";let Ye=Ie;class Fs{constructor(){this.prefix="crossx_"}async set(e,r){try{const t=JSON.stringify(r);localStorage.setItem(this.prefix+e,t)}catch(t){throw u.error("Storage set error:",t),t}}async get(e){try{const r=localStorage.getItem(this.prefix+e);return r?JSON.parse(r):null}catch(r){return u.error("Storage get error:",r),null}}async remove(e){try{localStorage.removeItem(this.prefix+e)}catch(r){throw u.error("Storage remove error:",r),r}}async clear(){try{Object.keys(localStorage).forEach(r=>{r.startsWith(this.prefix)&&localStorage.removeItem(r)})}catch(e){throw u.error("Storage clear error:",e),e}}}const at="crossx-sdk",Us=1,ue="data",ve="keys",Ge="aes-primary",Hs=12;class Kt{constructor(e){this.db=null,this.cryptoKey=null,this.initPromise=null,this.dbName=`crossx-sdk-${e}`}async ensureReady(){this.db&&this.cryptoKey||(this.initPromise||(this.initPromise=this.init()),await this.initPromise)}openDB(e=this.dbName){return new Promise((r,t)=>{const n=indexedDB.open(e,Us);n.onupgradeneeded=()=>{const o=n.result;o.objectStoreNames.contains(ue)||o.createObjectStore(ue),o.objectStoreNames.contains(ve)||o.createObjectStore(ve)},n.onsuccess=()=>r(n.result),n.onerror=()=>t(n.error)})}idbGet(e,r){return new Promise((t,n)=>{const i=this.db.transaction(e,"readonly").objectStore(e).get(r);i.onsuccess=()=>t(i.result),i.onerror=()=>n(i.error)})}idbPut(e,r,t){return new Promise((n,o)=>{const a=this.db.transaction(e,"readwrite").objectStore(e).put(t,r);a.onsuccess=()=>n(),a.onerror=()=>o(a.error)})}idbDelete(e,r){return new Promise((t,n)=>{const i=this.db.transaction(e,"readwrite").objectStore(e).delete(r);i.onsuccess=()=>t(),i.onerror=()=>n(i.error)})}idbClear(e){return new Promise((r,t)=>{const o=this.db.transaction(e,"readwrite").objectStore(e).clear();o.onsuccess=()=>r(),o.onerror=()=>t(o.error)})}async init(){this.db=await this.openDB();const e=await this.idbGet(ve,Ge);if(e){this.cryptoKey=e;return}const r=await this.migrateFromLegacyDB();if(r){this.cryptoKey=r;return}this.cryptoKey=await crypto.subtle.generateKey({name:"AES-GCM",length:256},!1,["encrypt","decrypt"]),await this.idbPut(ve,Ge,this.cryptoKey)}async migrateFromLegacyDB(){if(this.dbName===at)return null;try{const e=await this.openDB(at),r=e.transaction([ve,ue],"readonly"),t=r.objectStore(ve).get(Ge),n=await new Promise((l,d)=>{t.onsuccess=()=>l(t.result),t.onerror=()=>d(t.error)});if(!n)return e.close(),null;const o=r.objectStore(ue).getAll(),i=r.objectStore(ue).getAllKeys(),[a,c]=await Promise.all([new Promise((l,d)=>{o.onsuccess=()=>l(o.result),o.onerror=()=>d(o.error)}),new Promise((l,d)=>{i.onsuccess=()=>l(i.result),i.onerror=()=>d(i.error)})]);e.close(),await this.idbPut(ve,Ge,n);for(let l=0;l<c.length;l++)await this.idbPut(ue,String(c[l]),a[l]);return indexedDB.deleteDatabase(at),u.log("[CROSSx] IndexedDB 레거시 DB 마이그레이션 완료"),n}catch(e){return u.warn("[CROSSx] IndexedDB 레거시 DB 마이그레이션 실패:",e),null}}async encrypt(e){const r=new Uint8Array(Hs);crypto.getRandomValues(r);const t=new TextEncoder().encode(e),n=await crypto.subtle.encrypt({name:"AES-GCM",iv:r},this.cryptoKey,t);return{iv:r,ciphertext:n}}async decrypt(e){const r=new Uint8Array(e.iv),t=await crypto.subtle.decrypt({name:"AES-GCM",iv:r},this.cryptoKey,e.ciphertext);return new TextDecoder().decode(t)}async set(e,r){try{await this.ensureReady();const t=JSON.stringify(r),n=await this.encrypt(t);await this.idbPut(ue,e,n)}catch(t){throw u.error("[CROSSx] IndexedDB set error:",t),t}}async get(e){try{await this.ensureReady();const r=await this.idbGet(ue,e);if(!r)return null;const t=await this.decrypt(r);return JSON.parse(t)}catch(r){return u.error("[CROSSx] IndexedDB get error:",r),null}}async remove(e){try{await this.ensureReady(),await this.idbDelete(ue,e)}catch(r){throw u.error("[CROSSx] IndexedDB remove error:",r),r}}async clear(){try{await this.ensureReady(),await this.idbClear(ue)}catch(e){throw u.error("[CROSSx] IndexedDB clear error:",e),e}}static isAvailable(){return typeof indexedDB<"u"&&typeof crypto<"u"&&typeof crypto.subtle<"u"}}/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */function At(s){return s instanceof Uint8Array||ArrayBuffer.isView(s)&&s.constructor.name==="Uint8Array"}function we(s,e=""){if(!Number.isSafeInteger(s)||s<0){const r=e&&`"${e}" `;throw new Error(`${r}expected integer >= 0, got ${s}`)}}function V(s,e,r=""){const t=At(s),n=s==null?void 0:s.length,o=e!==void 0;if(!t||o&&n!==e){const i=r&&`"${r}" `,a=o?` of length ${e}`:"",c=t?`length=${n}`:`type=${typeof s}`;throw new Error(i+"expected Uint8Array"+a+", got "+c)}return s}function wr(s){if(typeof s!="function"||typeof s.create!="function")throw new Error("Hash must wrapped by utils.createHasher");we(s.outputLen),we(s.blockLen)}function De(s,e=!0){if(s.destroyed)throw new Error("Hash instance has been destroyed");if(e&&s.finished)throw new Error("Hash#digest() has already been called")}function mr(s,e){V(s,void 0,"digestInto() output");const r=e.outputLen;if(s.length<r)throw new Error('"digestInto() output" expected to be of length >='+r)}function Ws(s){return new Uint32Array(s.buffer,s.byteOffset,Math.floor(s.byteLength/4))}function $e(...s){for(let e=0;e<s.length;e++)s[e].fill(0)}function ct(s){return new DataView(s.buffer,s.byteOffset,s.byteLength)}function he(s,e){return s<<32-e|s>>>e}const Gs=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function qs(s){return s<<24&4278190080|s<<8&16711680|s>>>8&65280|s>>>24&255}function Ks(s){for(let e=0;e<s.length;e++)s[e]=qs(s[e]);return s}const Vt=Gs?s=>s:Ks,yr=typeof Uint8Array.from([]).toHex=="function"&&typeof Uint8Array.fromHex=="function",Vs=Array.from({length:256},(s,e)=>e.toString(16).padStart(2,"0"));function He(s){if(V(s),yr)return s.toHex();let e="";for(let r=0;r<s.length;r++)e+=Vs[s[r]];return e}const pe={_0:48,_9:57,A:65,F:70,a:97,f:102};function zt(s){if(s>=pe._0&&s<=pe._9)return s-pe._0;if(s>=pe.A&&s<=pe.F)return s-(pe.A-10);if(s>=pe.a&&s<=pe.f)return s-(pe.a-10)}function Xe(s){if(typeof s!="string")throw new Error("hex string expected, got "+typeof s);if(yr)return Uint8Array.fromHex(s);const e=s.length,r=e/2;if(e%2)throw new Error("hex string expected, got unpadded hex of length "+e);const t=new Uint8Array(r);for(let n=0,o=0;n<r;n++,o+=2){const i=zt(s.charCodeAt(o)),a=zt(s.charCodeAt(o+1));if(i===void 0||a===void 0){const c=s[o]+s[o+1];throw new Error('hex string expected, got non-hex character "'+c+'" at index '+o)}t[n]=i*16+a}return t}function Se(...s){let e=0;for(let t=0;t<s.length;t++){const n=s[t];V(n),e+=n.length}const r=new Uint8Array(e);for(let t=0,n=0;t<s.length;t++){const o=s[t];r.set(o,n),n+=o.length}return r}function br(s,e={}){const r=(n,o)=>s(o).update(n).digest(),t=s(void 0);return r.outputLen=t.outputLen,r.blockLen=t.blockLen,r.create=n=>s(n),Object.assign(r,e),Object.freeze(r)}function Sr(s=32){const e=typeof globalThis=="object"?globalThis.crypto:null;if(typeof(e==null?void 0:e.getRandomValues)!="function")throw new Error("crypto.getRandomValues must be defined");return e.getRandomValues(new Uint8Array(s))}const zs=s=>({oid:Uint8Array.from([6,9,96,134,72,1,101,3,4,2,s])});function js(s,e,r){return s&e^~s&r}function Ys(s,e,r){return s&e^s&r^e&r}class Xs{constructor(e,r,t,n){N(this,"blockLen");N(this,"outputLen");N(this,"padOffset");N(this,"isLE");N(this,"buffer");N(this,"view");N(this,"finished",!1);N(this,"length",0);N(this,"pos",0);N(this,"destroyed",!1);this.blockLen=e,this.outputLen=r,this.padOffset=t,this.isLE=n,this.buffer=new Uint8Array(e),this.view=ct(this.buffer)}update(e){De(this),V(e);const{view:r,buffer:t,blockLen:n}=this,o=e.length;for(let i=0;i<o;){const a=Math.min(n-this.pos,o-i);if(a===n){const c=ct(e);for(;n<=o-i;i+=n)this.process(c,i);continue}t.set(e.subarray(i,i+a),this.pos),this.pos+=a,i+=a,this.pos===n&&(this.process(r,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){De(this),mr(e,this),this.finished=!0;const{buffer:r,view:t,blockLen:n,isLE:o}=this;let{pos:i}=this;r[i++]=128,$e(this.buffer.subarray(i)),this.padOffset>n-i&&(this.process(t,0),i=0);for(let p=i;p<n;p++)r[p]=0;t.setBigUint64(n-8,BigInt(this.length*8),o),this.process(t,0);const a=ct(e),c=this.outputLen;if(c%4)throw new Error("_sha2: outputLen must be aligned to 32bit");const l=c/4,d=this.get();if(l>d.length)throw new Error("_sha2: outputLen bigger than state");for(let p=0;p<l;p++)a.setUint32(4*p,d[p],o)}digest(){const{buffer:e,outputLen:r}=this;this.digestInto(e);const t=e.slice(0,r);return this.destroy(),t}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());const{blockLen:r,buffer:t,length:n,finished:o,destroyed:i,pos:a}=this;return e.destroyed=i,e.finished=o,e.length=n,e.pos=a,n%r&&e.buffer.set(t),e}clone(){return this._cloneInto()}}const me=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),qe=BigInt(2**32-1),jt=BigInt(32);function Js(s,e=!1){return e?{h:Number(s&qe),l:Number(s>>jt&qe)}:{h:Number(s>>jt&qe)|0,l:Number(s&qe)|0}}function Zs(s,e=!1){const r=s.length;let t=new Uint32Array(r),n=new Uint32Array(r);for(let o=0;o<r;o++){const{h:i,l:a}=Js(s[o],e);[t[o],n[o]]=[i,a]}return[t,n]}const Qs=(s,e,r)=>s<<r|e>>>32-r,en=(s,e,r)=>e<<r|s>>>32-r,tn=(s,e,r)=>e<<r-32|s>>>64-r,rn=(s,e,r)=>s<<r-32|e>>>64-r,sn=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),ye=new Uint32Array(64);class nn extends Xs{constructor(e){super(64,e,8,!1)}get(){const{A:e,B:r,C:t,D:n,E:o,F:i,G:a,H:c}=this;return[e,r,t,n,o,i,a,c]}set(e,r,t,n,o,i,a,c){this.A=e|0,this.B=r|0,this.C=t|0,this.D=n|0,this.E=o|0,this.F=i|0,this.G=a|0,this.H=c|0}process(e,r){for(let p=0;p<16;p++,r+=4)ye[p]=e.getUint32(r,!1);for(let p=16;p<64;p++){const _=ye[p-15],f=ye[p-2],x=he(_,7)^he(_,18)^_>>>3,w=he(f,17)^he(f,19)^f>>>10;ye[p]=w+ye[p-7]+x+ye[p-16]|0}let{A:t,B:n,C:o,D:i,E:a,F:c,G:l,H:d}=this;for(let p=0;p<64;p++){const _=he(a,6)^he(a,11)^he(a,25),f=d+_+js(a,c,l)+sn[p]+ye[p]|0,w=(he(t,2)^he(t,13)^he(t,22))+Ys(t,n,o)|0;d=l,l=c,c=a,a=i+f|0,i=o,o=n,n=t,t=f+w|0}t=t+this.A|0,n=n+this.B|0,o=o+this.C|0,i=i+this.D|0,a=a+this.E|0,c=c+this.F|0,l=l+this.G|0,d=d+this.H|0,this.set(t,n,o,i,a,c,l,d)}roundClean(){$e(ye)}destroy(){this.set(0,0,0,0,0,0,0,0),$e(this.buffer)}}class on extends nn{constructor(){super(32);N(this,"A",me[0]|0);N(this,"B",me[1]|0);N(this,"C",me[2]|0);N(this,"D",me[3]|0);N(this,"E",me[4]|0);N(this,"F",me[5]|0);N(this,"G",me[6]|0);N(this,"H",me[7]|0)}}const an=br(()=>new on,zs(1));/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const It=BigInt(0),gt=BigInt(1);function Je(s,e=""){if(typeof s!="boolean"){const r=e&&`"${e}" `;throw new Error(r+"expected boolean, got type="+typeof s)}return s}function vr(s){if(typeof s=="bigint"){if(!ze(s))throw new Error("positive bigint expected, got "+s)}else we(s);return s}function Ke(s){const e=vr(s).toString(16);return e.length&1?"0"+e:e}function Er(s){if(typeof s!="string")throw new Error("hex string expected, got "+typeof s);return s===""?It:BigInt("0x"+s)}function tt(s){return Er(He(s))}function Ar(s){return Er(He(cn(V(s)).reverse()))}function Tt(s,e){we(e),s=vr(s);const r=Xe(s.toString(16).padStart(e*2,"0"));if(r.length!==e)throw new Error("number too large");return r}function Ir(s,e){return Tt(s,e).reverse()}function cn(s){return Uint8Array.from(s)}const ze=s=>typeof s=="bigint"&&It<=s;function ln(s,e,r){return ze(s)&&ze(e)&&ze(r)&&e<=s&&s<r}function dn(s,e,r,t){if(!ln(e,r,t))throw new Error("expected valid "+s+": "+r+" <= n < "+t+", got "+e)}function un(s){let e;for(e=0;s>It;s>>=gt,e+=1);return e}const kt=s=>(gt<<BigInt(s))-gt;function hn(s,e,r){if(we(s,"hashLen"),we(e,"qByteLen"),typeof r!="function")throw new Error("hmacFn must be a function");const t=E=>new Uint8Array(E),n=Uint8Array.of(),o=Uint8Array.of(0),i=Uint8Array.of(1),a=1e3;let c=t(s),l=t(s),d=0;const p=()=>{c.fill(1),l.fill(0),d=0},_=(...E)=>r(l,Se(c,...E)),f=(E=n)=>{l=_(o,E),c=_(),E.length!==0&&(l=_(i,E),c=_())},x=()=>{if(d++>=a)throw new Error("drbg: tried max amount of iterations");let E=0;const k=[];for(;E<e;){c=_();const O=c.slice();k.push(O),E+=c.length}return Se(...k)};return(E,k)=>{p(),f(E);let O;for(;!(O=k(x()));)f();return p(),O}}function Rt(s,e={},r={}){if(!s||typeof s!="object")throw new Error("expected valid options object");function t(o,i,a){const c=s[o];if(a&&c===void 0)return;const l=typeof c;if(l!==i||c===null)throw new Error(`param "${o}" is invalid: expected ${i}, got ${l}`)}const n=(o,i)=>Object.entries(o).forEach(([a,c])=>t(a,c,i));n(e,!1),n(r,!0)}function Yt(s){const e=new WeakMap;return(r,...t)=>{const n=e.get(r);if(n!==void 0)return n;const o=s(r,...t);return e.set(r,o),o}}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const se=BigInt(0),te=BigInt(1),Te=BigInt(2),Tr=BigInt(3),kr=BigInt(4),Rr=BigInt(5),pn=BigInt(7),Or=BigInt(8),fn=BigInt(9),Pr=BigInt(16);function de(s,e){const r=s%e;return r>=se?r:e+r}function ae(s,e,r){let t=s;for(;e-- >se;)t*=t,t%=r;return t}function Xt(s,e){if(s===se)throw new Error("invert: expected non-zero number");if(e<=se)throw new Error("invert: expected positive modulus, got "+e);let r=de(s,e),t=e,n=se,o=te;for(;r!==se;){const a=t/r,c=t%r,l=n-o*a;t=r,r=c,n=o,o=l}if(t!==te)throw new Error("invert: does not exist");return de(n,e)}function Ot(s,e,r){if(!s.eql(s.sqr(e),r))throw new Error("Cannot find square root")}function Cr(s,e){const r=(s.ORDER+te)/kr,t=s.pow(e,r);return Ot(s,t,e),t}function _n(s,e){const r=(s.ORDER-Rr)/Or,t=s.mul(e,Te),n=s.pow(t,r),o=s.mul(e,n),i=s.mul(s.mul(o,Te),n),a=s.mul(o,s.sub(i,s.ONE));return Ot(s,a,e),a}function xn(s){const e=rt(s),r=Nr(s),t=r(e,e.neg(e.ONE)),n=r(e,t),o=r(e,e.neg(t)),i=(s+pn)/Pr;return(a,c)=>{let l=a.pow(c,i),d=a.mul(l,t);const p=a.mul(l,n),_=a.mul(l,o),f=a.eql(a.sqr(d),c),x=a.eql(a.sqr(p),c);l=a.cmov(l,d,f),d=a.cmov(_,p,x);const w=a.eql(a.sqr(d),c),E=a.cmov(l,d,w);return Ot(a,E,c),E}}function Nr(s){if(s<Tr)throw new Error("sqrt is not defined for small field");let e=s-te,r=0;for(;e%Te===se;)e/=Te,r++;let t=Te;const n=rt(s);for(;Jt(n,t)===1;)if(t++>1e3)throw new Error("Cannot find square root: probably non-prime P");if(r===1)return Cr;let o=n.pow(t,e);const i=(e+te)/Te;return function(c,l){if(c.is0(l))return l;if(Jt(c,l)!==1)throw new Error("Cannot find square root");let d=r,p=c.mul(c.ONE,o),_=c.pow(l,e),f=c.pow(l,i);for(;!c.eql(_,c.ONE);){if(c.is0(_))return c.ZERO;let x=1,w=c.sqr(_);for(;!c.eql(w,c.ONE);)if(x++,w=c.sqr(w),x===d)throw new Error("Cannot find square root");const E=te<<BigInt(d-x-1),k=c.pow(p,E);d=x,p=c.sqr(k),_=c.mul(_,p),f=c.mul(f,k)}return f}}function gn(s){return s%kr===Tr?Cr:s%Or===Rr?_n:s%Pr===fn?xn(s):Nr(s)}const wn=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function mn(s){const e={ORDER:"bigint",BYTES:"number",BITS:"number"},r=wn.reduce((t,n)=>(t[n]="function",t),e);return Rt(s,r),s}function yn(s,e,r){if(r<se)throw new Error("invalid exponent, negatives unsupported");if(r===se)return s.ONE;if(r===te)return e;let t=s.ONE,n=e;for(;r>se;)r&te&&(t=s.mul(t,n)),n=s.sqr(n),r>>=te;return t}function Lr(s,e,r=!1){const t=new Array(e.length).fill(r?s.ZERO:void 0),n=e.reduce((i,a,c)=>s.is0(a)?i:(t[c]=i,s.mul(i,a)),s.ONE),o=s.inv(n);return e.reduceRight((i,a,c)=>s.is0(a)?i:(t[c]=s.mul(i,t[c]),s.mul(i,a)),o),t}function Jt(s,e){const r=(s.ORDER-te)/Te,t=s.pow(e,r),n=s.eql(t,s.ONE),o=s.eql(t,s.ZERO),i=s.eql(t,s.neg(s.ONE));if(!n&&!o&&!i)throw new Error("invalid Legendre symbol result");return n?1:o?0:-1}function bn(s,e){e!==void 0&&we(e);const r=e!==void 0?e:s.toString(2).length,t=Math.ceil(r/8);return{nBitLength:r,nByteLength:t}}class Sn{constructor(e,r={}){N(this,"ORDER");N(this,"BITS");N(this,"BYTES");N(this,"isLE");N(this,"ZERO",se);N(this,"ONE",te);N(this,"_lengths");N(this,"_sqrt");N(this,"_mod");var i;if(e<=se)throw new Error("invalid field: expected ORDER > 0, got "+e);let t;this.isLE=!1,r!=null&&typeof r=="object"&&(typeof r.BITS=="number"&&(t=r.BITS),typeof r.sqrt=="function"&&(this.sqrt=r.sqrt),typeof r.isLE=="boolean"&&(this.isLE=r.isLE),r.allowedLengths&&(this._lengths=(i=r.allowedLengths)==null?void 0:i.slice()),typeof r.modFromBytes=="boolean"&&(this._mod=r.modFromBytes));const{nBitLength:n,nByteLength:o}=bn(e,t);if(o>2048)throw new Error("invalid field: expected ORDER of <= 2048 bytes");this.ORDER=e,this.BITS=n,this.BYTES=o,this._sqrt=void 0,Object.preventExtensions(this)}create(e){return de(e,this.ORDER)}isValid(e){if(typeof e!="bigint")throw new Error("invalid field element: expected bigint, got "+typeof e);return se<=e&&e<this.ORDER}is0(e){return e===se}isValidNot0(e){return!this.is0(e)&&this.isValid(e)}isOdd(e){return(e&te)===te}neg(e){return de(-e,this.ORDER)}eql(e,r){return e===r}sqr(e){return de(e*e,this.ORDER)}add(e,r){return de(e+r,this.ORDER)}sub(e,r){return de(e-r,this.ORDER)}mul(e,r){return de(e*r,this.ORDER)}pow(e,r){return yn(this,e,r)}div(e,r){return de(e*Xt(r,this.ORDER),this.ORDER)}sqrN(e){return e*e}addN(e,r){return e+r}subN(e,r){return e-r}mulN(e,r){return e*r}inv(e){return Xt(e,this.ORDER)}sqrt(e){return this._sqrt||(this._sqrt=gn(this.ORDER)),this._sqrt(this,e)}toBytes(e){return this.isLE?Ir(e,this.BYTES):Tt(e,this.BYTES)}fromBytes(e,r=!1){V(e);const{_lengths:t,BYTES:n,isLE:o,ORDER:i,_mod:a}=this;if(t){if(!t.includes(e.length)||e.length>n)throw new Error("Field.fromBytes: expected "+t+" bytes, got "+e.length);const l=new Uint8Array(n);l.set(e,o?0:l.length-e.length),e=l}if(e.length!==n)throw new Error("Field.fromBytes: expected "+n+" bytes, got "+e.length);let c=o?Ar(e):tt(e);if(a&&(c=de(c,i)),!r&&!this.isValid(c))throw new Error("invalid field element: outside of range 0..ORDER");return c}invertBatch(e){return Lr(this,e)}cmov(e,r,t){return t?r:e}}function rt(s,e={}){return new Sn(s,e)}function Dr(s){if(typeof s!="bigint")throw new Error("field order must be bigint");const e=s.toString(2).length;return Math.ceil(e/8)}function $r(s){const e=Dr(s);return e+Math.ceil(e/2)}function vn(s,e,r=!1){V(s);const t=s.length,n=Dr(e),o=$r(e);if(t<16||t<o||t>1024)throw new Error("expected "+o+"-1024 bytes of input, got "+t);const i=r?Ar(s):tt(s),a=de(i,e-te)+te;return r?Ir(a,n):Tt(a,n)}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const Me=BigInt(0),ke=BigInt(1);function Ze(s,e){const r=e.negate();return s?r:e}function Zt(s,e){const r=Lr(s.Fp,e.map(t=>t.Z));return e.map((t,n)=>s.fromAffine(t.toAffine(r[n])))}function Mr(s,e){if(!Number.isSafeInteger(s)||s<=0||s>e)throw new Error("invalid window size, expected [1.."+e+"], got W="+s)}function lt(s,e){Mr(s,e);const r=Math.ceil(e/s)+1,t=2**(s-1),n=2**s,o=kt(s),i=BigInt(s);return{windows:r,windowSize:t,mask:o,maxNumber:n,shiftBy:i}}function Qt(s,e,r){const{windowSize:t,mask:n,maxNumber:o,shiftBy:i}=r;let a=Number(s&n),c=s>>i;a>t&&(a-=o,c+=ke);const l=e*t,d=l+Math.abs(a)-1,p=a===0,_=a<0,f=e%2!==0;return{nextN:c,offset:d,isZero:p,isNeg:_,isNegF:f,offsetF:l}}const dt=new WeakMap,Br=new WeakMap;function ut(s){return Br.get(s)||1}function er(s){if(s!==Me)throw new Error("invalid wNAF")}class En{constructor(e,r){N(this,"BASE");N(this,"ZERO");N(this,"Fn");N(this,"bits");this.BASE=e.BASE,this.ZERO=e.ZERO,this.Fn=e.Fn,this.bits=r}_unsafeLadder(e,r,t=this.ZERO){let n=e;for(;r>Me;)r&ke&&(t=t.add(n)),n=n.double(),r>>=ke;return t}precomputeWindow(e,r){const{windows:t,windowSize:n}=lt(r,this.bits),o=[];let i=e,a=i;for(let c=0;c<t;c++){a=i,o.push(a);for(let l=1;l<n;l++)a=a.add(i),o.push(a);i=a.double()}return o}wNAF(e,r,t){if(!this.Fn.isValid(t))throw new Error("invalid scalar");let n=this.ZERO,o=this.BASE;const i=lt(e,this.bits);for(let a=0;a<i.windows;a++){const{nextN:c,offset:l,isZero:d,isNeg:p,isNegF:_,offsetF:f}=Qt(t,a,i);t=c,d?o=o.add(Ze(_,r[f])):n=n.add(Ze(p,r[l]))}return er(t),{p:n,f:o}}wNAFUnsafe(e,r,t,n=this.ZERO){const o=lt(e,this.bits);for(let i=0;i<o.windows&&t!==Me;i++){const{nextN:a,offset:c,isZero:l,isNeg:d}=Qt(t,i,o);if(t=a,!l){const p=r[c];n=n.add(d?p.negate():p)}}return er(t),n}getPrecomputes(e,r,t){let n=dt.get(r);return n||(n=this.precomputeWindow(r,e),e!==1&&(typeof t=="function"&&(n=t(n)),dt.set(r,n))),n}cached(e,r,t){const n=ut(e);return this.wNAF(n,this.getPrecomputes(n,e,t),r)}unsafe(e,r,t,n){const o=ut(e);return o===1?this._unsafeLadder(e,r,n):this.wNAFUnsafe(o,this.getPrecomputes(o,e,t),r,n)}createCache(e,r){Mr(r,this.bits),Br.set(e,r),dt.delete(e)}hasCache(e){return ut(e)!==1}}function An(s,e,r,t){let n=e,o=s.ZERO,i=s.ZERO;for(;r>Me||t>Me;)r&ke&&(o=o.add(n)),t&ke&&(i=i.add(n)),n=n.double(),r>>=ke,t>>=ke;return{p1:o,p2:i}}function tr(s,e,r){if(e){if(e.ORDER!==s)throw new Error("Field.ORDER must match order: Fp == p, Fn == n");return mn(e),e}else return rt(s,{isLE:r})}function In(s,e,r={},t){if(t===void 0&&(t=s==="edwards"),!e||typeof e!="object")throw new Error(`expected valid ${s} CURVE object`);for(const c of["p","n","h"]){const l=e[c];if(!(typeof l=="bigint"&&l>Me))throw new Error(`CURVE.${c} must be positive bigint`)}const n=tr(e.p,r.Fp,t),o=tr(e.n,r.Fn,t),a=["Gx","Gy","a","b"];for(const c of a)if(!n.isValid(e[c]))throw new Error(`CURVE.${c} must be valid field element of CURVE.Fp`);return e=Object.freeze(Object.assign({},e)),{CURVE:e,Fp:n,Fn:o}}function Tn(s,e){return function(t){const n=s(t);return{secretKey:n,publicKey:e(n)}}}class Fr{constructor(e,r){N(this,"oHash");N(this,"iHash");N(this,"blockLen");N(this,"outputLen");N(this,"finished",!1);N(this,"destroyed",!1);if(wr(e),V(r,void 0,"key"),this.iHash=e.create(),typeof this.iHash.update!="function")throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;const t=this.blockLen,n=new Uint8Array(t);n.set(r.length>t?e.create().update(r).digest():r);for(let o=0;o<n.length;o++)n[o]^=54;this.iHash.update(n),this.oHash=e.create();for(let o=0;o<n.length;o++)n[o]^=106;this.oHash.update(n),$e(n)}update(e){return De(this),this.iHash.update(e),this}digestInto(e){De(this),V(e,this.outputLen,"output"),this.finished=!0,this.iHash.digestInto(e),this.oHash.update(e),this.oHash.digestInto(e),this.destroy()}digest(){const e=new Uint8Array(this.oHash.outputLen);return this.digestInto(e),e}_cloneInto(e){e||(e=Object.create(Object.getPrototypeOf(this),{}));const{oHash:r,iHash:t,finished:n,destroyed:o,blockLen:i,outputLen:a}=this;return e=e,e.finished=n,e.destroyed=o,e.blockLen=i,e.outputLen=a,e.oHash=r._cloneInto(e.oHash),e.iHash=t._cloneInto(e.iHash),e}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}}const Ur=(s,e,r)=>new Fr(s,e).update(r).digest();Ur.create=(s,e)=>new Fr(s,e);/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const rr=(s,e)=>(s+(s>=0?e:-e)/Hr)/e;function kn(s,e,r){const[[t,n],[o,i]]=e,a=rr(i*s,r),c=rr(-n*s,r);let l=s-a*t-c*o,d=-a*n-c*i;const p=l<_e,_=d<_e;p&&(l=-l),_&&(d=-d);const f=kt(Math.ceil(un(r)/2))+Le;if(l<_e||l>=f||d<_e||d>=f)throw new Error("splitScalar (endomorphism): failed, k="+s);return{k1neg:p,k1:l,k2neg:_,k2:d}}function wt(s){if(!["compact","recovered","der"].includes(s))throw new Error('Signature format must be "compact", "recovered", or "der"');return s}function ht(s,e){const r={};for(let t of Object.keys(e))r[t]=s[t]===void 0?e[t]:s[t];return Je(r.lowS,"lowS"),Je(r.prehash,"prehash"),r.format!==void 0&&wt(r.format),r}class Rn extends Error{constructor(e=""){super(e)}}const be={Err:Rn,_tlv:{encode:(s,e)=>{const{Err:r}=be;if(s<0||s>256)throw new r("tlv.encode: wrong tag");if(e.length&1)throw new r("tlv.encode: unpadded data");const t=e.length/2,n=Ke(t);if(n.length/2&128)throw new r("tlv.encode: long form length too big");const o=t>127?Ke(n.length/2|128):"";return Ke(s)+o+n+e},decode(s,e){const{Err:r}=be;let t=0;if(s<0||s>256)throw new r("tlv.encode: wrong tag");if(e.length<2||e[t++]!==s)throw new r("tlv.decode: wrong tlv");const n=e[t++],o=!!(n&128);let i=0;if(!o)i=n;else{const c=n&127;if(!c)throw new r("tlv.decode(long): indefinite length not supported");if(c>4)throw new r("tlv.decode(long): byte length is too big");const l=e.subarray(t,t+c);if(l.length!==c)throw new r("tlv.decode: length bytes not complete");if(l[0]===0)throw new r("tlv.decode(long): zero leftmost byte");for(const d of l)i=i<<8|d;if(t+=c,i<128)throw new r("tlv.decode(long): not minimal encoding")}const a=e.subarray(t,t+i);if(a.length!==i)throw new r("tlv.decode: wrong value length");return{v:a,l:e.subarray(t+i)}}},_int:{encode(s){const{Err:e}=be;if(s<_e)throw new e("integer: negative integers are not allowed");let r=Ke(s);if(Number.parseInt(r[0],16)&8&&(r="00"+r),r.length&1)throw new e("unexpected DER parsing assertion: unpadded hex");return r},decode(s){const{Err:e}=be;if(s[0]&128)throw new e("invalid signature integer: negative");if(s[0]===0&&!(s[1]&128))throw new e("invalid signature integer: unnecessary leading zero");return tt(s)}},toSig(s){const{Err:e,_int:r,_tlv:t}=be,n=V(s,void 0,"signature"),{v:o,l:i}=t.decode(48,n);if(i.length)throw new e("invalid signature: left bytes after parsing");const{v:a,l:c}=t.decode(2,o),{v:l,l:d}=t.decode(2,c);if(d.length)throw new e("invalid signature: left bytes after parsing");return{r:r.decode(a),s:r.decode(l)}},hexFromSig(s){const{_tlv:e,_int:r}=be,t=e.encode(2,r.encode(s.r)),n=e.encode(2,r.encode(s.s)),o=t+n;return e.encode(48,o)}},_e=BigInt(0),Le=BigInt(1),Hr=BigInt(2),Ve=BigInt(3),On=BigInt(4);function Pn(s,e={}){const r=In("weierstrass",s,e),{Fp:t,Fn:n}=r;let o=r.CURVE;const{h:i,n:a}=o;Rt(e,{},{allowInfinityPoint:"boolean",clearCofactor:"function",isTorsionFree:"function",fromBytes:"function",toBytes:"function",endo:"object"});const{endo:c}=e;if(c&&(!t.is0(o.a)||typeof c.beta!="bigint"||!Array.isArray(c.basises)))throw new Error('invalid endo: expected "beta": bigint and "basises": array');const l=Gr(t,n);function d(){if(!t.isOdd)throw new Error("compression is not supported: Field does not have .isOdd()")}function p(B,m,y){const{x:T,y:C}=m.toAffine(),M=t.toBytes(T);if(Je(y,"isCompressed"),y){d();const b=!t.isOdd(C);return Se(Wr(b),M)}else return Se(Uint8Array.of(4),M,t.toBytes(C))}function _(B){V(B,void 0,"Point");const{publicKey:m,publicKeyUncompressed:y}=l,T=B.length,C=B[0],M=B.subarray(1);if(T===m&&(C===2||C===3)){const b=t.fromBytes(M);if(!t.isValid(b))throw new Error("bad point: is not on curve, wrong x");const S=w(b);let v;try{v=t.sqrt(S)}catch(H){const F=H instanceof Error?": "+H.message:"";throw new Error("bad point: is not on curve, sqrt error"+F)}d();const I=t.isOdd(v);return(C&1)===1!==I&&(v=t.neg(v)),{x:b,y:v}}else if(T===y&&C===4){const b=t.BYTES,S=t.fromBytes(M.subarray(0,b)),v=t.fromBytes(M.subarray(b,b*2));if(!E(S,v))throw new Error("bad point: is not on curve");return{x:S,y:v}}else throw new Error(`bad point: got length ${T}, expected compressed=${m} or uncompressed=${y}`)}const f=e.toBytes||p,x=e.fromBytes||_;function w(B){const m=t.sqr(B),y=t.mul(m,B);return t.add(t.add(y,t.mul(B,o.a)),o.b)}function E(B,m){const y=t.sqr(m),T=w(B);return t.eql(y,T)}if(!E(o.Gx,o.Gy))throw new Error("bad curve params: generator point");const k=t.mul(t.pow(o.a,Ve),On),O=t.mul(t.sqr(o.b),BigInt(27));if(t.is0(t.add(k,O)))throw new Error("bad curve params: a or b");function P(B,m,y=!1){if(!t.isValid(m)||y&&t.is0(m))throw new Error(`bad point coordinate ${B}`);return m}function L(B){if(!(B instanceof X))throw new Error("Weierstrass Point expected")}function W(B){if(!c||!c.basises)throw new Error("no endo");return kn(B,c.basises,n.ORDER)}const q=Yt((B,m)=>{const{X:y,Y:T,Z:C}=B;if(t.eql(C,t.ONE))return{x:y,y:T};const M=B.is0();m==null&&(m=M?t.ONE:t.inv(C));const b=t.mul(y,m),S=t.mul(T,m),v=t.mul(C,m);if(M)return{x:t.ZERO,y:t.ZERO};if(!t.eql(v,t.ONE))throw new Error("invZ was invalid");return{x:b,y:S}}),J=Yt(B=>{if(B.is0()){if(e.allowInfinityPoint&&!t.is0(B.Y))return;throw new Error("bad point: ZERO")}const{x:m,y}=B.toAffine();if(!t.isValid(m)||!t.isValid(y))throw new Error("bad point: x or y not field elements");if(!E(m,y))throw new Error("bad point: equation left != right");if(!B.isTorsionFree())throw new Error("bad point: not in prime-order subgroup");return!0});function G(B,m,y,T,C){return y=new X(t.mul(y.X,B),y.Y,y.Z),m=Ze(T,m),y=Ze(C,y),m.add(y)}const D=class D{constructor(m,y,T){N(this,"X");N(this,"Y");N(this,"Z");this.X=P("x",m),this.Y=P("y",y,!0),this.Z=P("z",T),Object.freeze(this)}static CURVE(){return o}static fromAffine(m){const{x:y,y:T}=m||{};if(!m||!t.isValid(y)||!t.isValid(T))throw new Error("invalid affine point");if(m instanceof D)throw new Error("projective point not allowed");return t.is0(y)&&t.is0(T)?D.ZERO:new D(y,T,t.ONE)}static fromBytes(m){const y=D.fromAffine(x(V(m,void 0,"point")));return y.assertValidity(),y}static fromHex(m){return D.fromBytes(Xe(m))}get x(){return this.toAffine().x}get y(){return this.toAffine().y}precompute(m=8,y=!0){return U.createCache(this,m),y||this.multiply(Ve),this}assertValidity(){J(this)}hasEvenY(){const{y:m}=this.toAffine();if(!t.isOdd)throw new Error("Field doesn't support isOdd");return!t.isOdd(m)}equals(m){L(m);const{X:y,Y:T,Z:C}=this,{X:M,Y:b,Z:S}=m,v=t.eql(t.mul(y,S),t.mul(M,C)),I=t.eql(t.mul(T,S),t.mul(b,C));return v&&I}negate(){return new D(this.X,t.neg(this.Y),this.Z)}double(){const{a:m,b:y}=o,T=t.mul(y,Ve),{X:C,Y:M,Z:b}=this;let S=t.ZERO,v=t.ZERO,I=t.ZERO,R=t.mul(C,C),H=t.mul(M,M),F=t.mul(b,b),$=t.mul(C,M);return $=t.add($,$),I=t.mul(C,b),I=t.add(I,I),S=t.mul(m,I),v=t.mul(T,F),v=t.add(S,v),S=t.sub(H,v),v=t.add(H,v),v=t.mul(S,v),S=t.mul($,S),I=t.mul(T,I),F=t.mul(m,F),$=t.sub(R,F),$=t.mul(m,$),$=t.add($,I),I=t.add(R,R),R=t.add(I,R),R=t.add(R,F),R=t.mul(R,$),v=t.add(v,R),F=t.mul(M,b),F=t.add(F,F),R=t.mul(F,$),S=t.sub(S,R),I=t.mul(F,H),I=t.add(I,I),I=t.add(I,I),new D(S,v,I)}add(m){L(m);const{X:y,Y:T,Z:C}=this,{X:M,Y:b,Z:S}=m;let v=t.ZERO,I=t.ZERO,R=t.ZERO;const H=o.a,F=t.mul(o.b,Ve);let $=t.mul(y,M),K=t.mul(T,b),Y=t.mul(C,S),oe=t.add(y,T),z=t.add(M,b);oe=t.mul(oe,z),z=t.add($,K),oe=t.sub(oe,z),z=t.add(y,C);let ee=t.add(M,S);return z=t.mul(z,ee),ee=t.add($,Y),z=t.sub(z,ee),ee=t.add(T,C),v=t.add(b,S),ee=t.mul(ee,v),v=t.add(K,Y),ee=t.sub(ee,v),R=t.mul(H,z),v=t.mul(F,Y),R=t.add(v,R),v=t.sub(K,R),R=t.add(K,R),I=t.mul(v,R),K=t.add($,$),K=t.add(K,$),Y=t.mul(H,Y),z=t.mul(F,z),K=t.add(K,Y),Y=t.sub($,Y),Y=t.mul(H,Y),z=t.add(z,Y),$=t.mul(K,z),I=t.add(I,$),$=t.mul(ee,z),v=t.mul(oe,v),v=t.sub(v,$),$=t.mul(oe,K),R=t.mul(ee,R),R=t.add(R,$),new D(v,I,R)}subtract(m){return this.add(m.negate())}is0(){return this.equals(D.ZERO)}multiply(m){const{endo:y}=e;if(!n.isValidNot0(m))throw new Error("invalid scalar: out of range");let T,C;const M=b=>U.cached(this,b,S=>Zt(D,S));if(y){const{k1neg:b,k1:S,k2neg:v,k2:I}=W(m),{p:R,f:H}=M(S),{p:F,f:$}=M(I);C=H.add($),T=G(y.beta,R,F,b,v)}else{const{p:b,f:S}=M(m);T=b,C=S}return Zt(D,[T,C])[0]}multiplyUnsafe(m){const{endo:y}=e,T=this;if(!n.isValid(m))throw new Error("invalid scalar: out of range");if(m===_e||T.is0())return D.ZERO;if(m===Le)return T;if(U.hasCache(this))return this.multiply(m);if(y){const{k1neg:C,k1:M,k2neg:b,k2:S}=W(m),{p1:v,p2:I}=An(D,T,M,S);return G(y.beta,v,I,C,b)}else return U.unsafe(T,m)}toAffine(m){return q(this,m)}isTorsionFree(){const{isTorsionFree:m}=e;return i===Le?!0:m?m(D,this):U.unsafe(this,a).is0()}clearCofactor(){const{clearCofactor:m}=e;return i===Le?this:m?m(D,this):this.multiplyUnsafe(i)}isSmallOrder(){return this.multiplyUnsafe(i).is0()}toBytes(m=!0){return Je(m,"isCompressed"),this.assertValidity(),f(D,this,m)}toHex(m=!0){return He(this.toBytes(m))}toString(){return`<Point ${this.is0()?"ZERO":this.toHex()}>`}};N(D,"BASE",new D(o.Gx,o.Gy,t.ONE)),N(D,"ZERO",new D(t.ZERO,t.ONE,t.ZERO)),N(D,"Fp",t),N(D,"Fn",n);let X=D;const Q=n.BITS,U=new En(X,e.endo?Math.ceil(Q/2):Q);return X.BASE.precompute(8),X}function Wr(s){return Uint8Array.of(s?2:3)}function Gr(s,e){return{secretKey:e.BYTES,publicKey:1+s.BYTES,publicKeyUncompressed:1+2*s.BYTES,publicKeyHasPrefix:!0,signature:2*e.BYTES}}function Cn(s,e={}){const{Fn:r}=s,t=e.randomBytes||Sr,n=Object.assign(Gr(s.Fp,r),{seed:$r(r.ORDER)});function o(f){try{const x=r.fromBytes(f);return r.isValidNot0(x)}catch{return!1}}function i(f,x){const{publicKey:w,publicKeyUncompressed:E}=n;try{const k=f.length;return x===!0&&k!==w||x===!1&&k!==E?!1:!!s.fromBytes(f)}catch{return!1}}function a(f=t(n.seed)){return vn(V(f,n.seed,"seed"),r.ORDER)}function c(f,x=!0){return s.BASE.multiply(r.fromBytes(f)).toBytes(x)}function l(f){const{secretKey:x,publicKey:w,publicKeyUncompressed:E}=n;if(!At(f)||"_lengths"in r&&r._lengths||x===w)return;const k=V(f,void 0,"key").length;return k===w||k===E}function d(f,x,w=!0){if(l(f)===!0)throw new Error("first arg must be private key");if(l(x)===!1)throw new Error("second arg must be public key");const E=r.fromBytes(f);return s.fromBytes(x).multiply(E).toBytes(w)}const p={isValidSecretKey:o,isValidPublicKey:i,randomSecretKey:a},_=Tn(a,c);return Object.freeze({getPublicKey:c,getSharedSecret:d,keygen:_,Point:s,utils:p,lengths:n})}function Nn(s,e,r={}){wr(e),Rt(r,{},{hmac:"function",lowS:"boolean",randomBytes:"function",bits2int:"function",bits2int_modN:"function"}),r=Object.assign({},r);const t=r.randomBytes||Sr,n=r.hmac||((m,y)=>Ur(e,m,y)),{Fp:o,Fn:i}=s,{ORDER:a,BITS:c}=i,{keygen:l,getPublicKey:d,getSharedSecret:p,utils:_,lengths:f}=Cn(s,r),x={prehash:!0,lowS:typeof r.lowS=="boolean"?r.lowS:!0,format:"compact",extraEntropy:!1},w=a*Hr<o.ORDER;function E(m){const y=a>>Le;return m>y}function k(m,y){if(!i.isValidNot0(y))throw new Error(`invalid signature ${m}: out of range 1..Point.Fn.ORDER`);return y}function O(){if(w)throw new Error('"recovered" sig type is not supported for cofactor >2 curves')}function P(m,y){wt(y);const T=f.signature,C=y==="compact"?T:y==="recovered"?T+1:void 0;return V(m,C)}class L{constructor(y,T,C){N(this,"r");N(this,"s");N(this,"recovery");if(this.r=k("r",y),this.s=k("s",T),C!=null){if(O(),![0,1,2,3].includes(C))throw new Error("invalid recovery id");this.recovery=C}Object.freeze(this)}static fromBytes(y,T=x.format){P(y,T);let C;if(T==="der"){const{r:v,s:I}=be.toSig(V(y));return new L(v,I)}T==="recovered"&&(C=y[0],T="compact",y=y.subarray(1));const M=f.signature/2,b=y.subarray(0,M),S=y.subarray(M,M*2);return new L(i.fromBytes(b),i.fromBytes(S),C)}static fromHex(y,T){return this.fromBytes(Xe(y),T)}assertRecovery(){const{recovery:y}=this;if(y==null)throw new Error("invalid recovery id: must be present");return y}addRecoveryBit(y){return new L(this.r,this.s,y)}recoverPublicKey(y){const{r:T,s:C}=this,M=this.assertRecovery(),b=M===2||M===3?T+a:T;if(!o.isValid(b))throw new Error("invalid recovery id: sig.r+curve.n != R.x");const S=o.toBytes(b),v=s.fromBytes(Se(Wr((M&1)===0),S)),I=i.inv(b),R=q(V(y,void 0,"msgHash")),H=i.create(-R*I),F=i.create(C*I),$=s.BASE.multiplyUnsafe(H).add(v.multiplyUnsafe(F));if($.is0())throw new Error("invalid recovery: point at infinify");return $.assertValidity(),$}hasHighS(){return E(this.s)}toBytes(y=x.format){if(wt(y),y==="der")return Xe(be.hexFromSig(this));const{r:T,s:C}=this,M=i.toBytes(T),b=i.toBytes(C);return y==="recovered"?(O(),Se(Uint8Array.of(this.assertRecovery()),M,b)):Se(M,b)}toHex(y){return He(this.toBytes(y))}}const W=r.bits2int||function(y){if(y.length>8192)throw new Error("input is too large");const T=tt(y),C=y.length*8-c;return C>0?T>>BigInt(C):T},q=r.bits2int_modN||function(y){return i.create(W(y))},J=kt(c);function G(m){return dn("num < 2^"+c,m,_e,J),i.toBytes(m)}function X(m,y){return V(m,void 0,"message"),y?V(e(m),void 0,"prehashed message"):m}function Q(m,y,T){const{lowS:C,prehash:M,extraEntropy:b}=ht(T,x);m=X(m,M);const S=q(m),v=i.fromBytes(y);if(!i.isValidNot0(v))throw new Error("invalid private key");const I=[G(v),G(S)];if(b!=null&&b!==!1){const $=b===!0?t(f.secretKey):b;I.push(V($,void 0,"extraEntropy"))}const R=Se(...I),H=S;function F($){const K=W($);if(!i.isValidNot0(K))return;const Y=i.inv(K),oe=s.BASE.multiply(K).toAffine(),z=i.create(oe.x);if(z===_e)return;const ee=i.create(Y*i.create(H+z*v));if(ee===_e)return;let Lt=(oe.x===z?0:2)|Number(oe.y&Le),Dt=ee;return C&&E(ee)&&(Dt=i.neg(ee),Lt^=1),new L(z,Dt,w?void 0:Lt)}return{seed:R,k2sig:F}}function U(m,y,T={}){const{seed:C,k2sig:M}=Q(m,y,T);return hn(e.outputLen,i.BYTES,n)(C,M).toBytes(T.format)}function D(m,y,T,C={}){const{lowS:M,prehash:b,format:S}=ht(C,x);if(T=V(T,void 0,"publicKey"),y=X(y,b),!At(m)){const v=m instanceof L?", use sig.toBytes()":"";throw new Error("verify expects Uint8Array signature"+v)}P(m,S);try{const v=L.fromBytes(m,S),I=s.fromBytes(T);if(M&&v.hasHighS())return!1;const{r:R,s:H}=v,F=q(y),$=i.inv(H),K=i.create(F*$),Y=i.create(R*$),oe=s.BASE.multiplyUnsafe(K).add(I.multiplyUnsafe(Y));return oe.is0()?!1:i.create(oe.x)===R}catch{return!1}}function B(m,y,T={}){const{prehash:C}=ht(T,x);return y=X(y,C),L.fromBytes(m,"recovered").recoverPublicKey(y).toBytes()}return Object.freeze({keygen:l,getPublicKey:d,getSharedSecret:p,utils:_,lengths:f,Point:s,sign:U,verify:D,recoverPublicKey:B,Signature:L,hash:e})}/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const Pt={p:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),n:BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),h:BigInt(1),a:BigInt(0),b:BigInt(7),Gx:BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),Gy:BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")},Ln={beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),basises:[[BigInt("0x3086d221a7d46bcde86c90e49284eb15"),-BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),BigInt("0x3086d221a7d46bcde86c90e49284eb15")]]},sr=BigInt(2);function Dn(s){const e=Pt.p,r=BigInt(3),t=BigInt(6),n=BigInt(11),o=BigInt(22),i=BigInt(23),a=BigInt(44),c=BigInt(88),l=s*s*s%e,d=l*l*s%e,p=ae(d,r,e)*d%e,_=ae(p,r,e)*d%e,f=ae(_,sr,e)*l%e,x=ae(f,n,e)*f%e,w=ae(x,o,e)*x%e,E=ae(w,a,e)*w%e,k=ae(E,c,e)*E%e,O=ae(k,a,e)*w%e,P=ae(O,r,e)*d%e,L=ae(P,i,e)*x%e,W=ae(L,t,e)*l%e,q=ae(W,sr,e);if(!mt.eql(mt.sqr(q),s))throw new Error("Cannot find square root");return q}const mt=rt(Pt.p,{sqrt:Dn}),$n=Pn(Pt,{Fp:mt,endo:Ln}),Mn=Nn($n,an),Bn=BigInt(0),Be=BigInt(1),Fn=BigInt(2),Un=BigInt(7),Hn=BigInt(256),Wn=BigInt(113),qr=[],Kr=[],Vr=[];for(let s=0,e=Be,r=1,t=0;s<24;s++){[r,t]=[t,(2*r+3*t)%5],qr.push(2*(5*t+r)),Kr.push((s+1)*(s+2)/2%64);let n=Bn;for(let o=0;o<7;o++)e=(e<<Be^(e>>Un)*Wn)%Hn,e&Fn&&(n^=Be<<(Be<<BigInt(o))-Be);Vr.push(n)}const zr=Zs(Vr,!0),Gn=zr[0],qn=zr[1],nr=(s,e,r)=>r>32?tn(s,e,r):Qs(s,e,r),or=(s,e,r)=>r>32?rn(s,e,r):en(s,e,r);function Kn(s,e=24){const r=new Uint32Array(10);for(let t=24-e;t<24;t++){for(let i=0;i<10;i++)r[i]=s[i]^s[i+10]^s[i+20]^s[i+30]^s[i+40];for(let i=0;i<10;i+=2){const a=(i+8)%10,c=(i+2)%10,l=r[c],d=r[c+1],p=nr(l,d,1)^r[a],_=or(l,d,1)^r[a+1];for(let f=0;f<50;f+=10)s[i+f]^=p,s[i+f+1]^=_}let n=s[2],o=s[3];for(let i=0;i<24;i++){const a=Kr[i],c=nr(n,o,a),l=or(n,o,a),d=qr[i];n=s[d],o=s[d+1],s[d]=c,s[d+1]=l}for(let i=0;i<50;i+=10){for(let a=0;a<10;a++)r[a]=s[i+a];for(let a=0;a<10;a++)s[i+a]^=~r[(a+2)%10]&r[(a+4)%10]}s[0]^=Gn[t],s[1]^=qn[t]}$e(r)}class Ct{constructor(e,r,t,n=!1,o=24){N(this,"state");N(this,"pos",0);N(this,"posOut",0);N(this,"finished",!1);N(this,"state32");N(this,"destroyed",!1);N(this,"blockLen");N(this,"suffix");N(this,"outputLen");N(this,"enableXOF",!1);N(this,"rounds");if(this.blockLen=e,this.suffix=r,this.outputLen=t,this.enableXOF=n,this.rounds=o,we(t,"outputLen"),!(0<e&&e<200))throw new Error("only keccak-f1600 function is supported");this.state=new Uint8Array(200),this.state32=Ws(this.state)}clone(){return this._cloneInto()}keccak(){Vt(this.state32),Kn(this.state32,this.rounds),Vt(this.state32),this.posOut=0,this.pos=0}update(e){De(this),V(e);const{blockLen:r,state:t}=this,n=e.length;for(let o=0;o<n;){const i=Math.min(r-this.pos,n-o);for(let a=0;a<i;a++)t[this.pos++]^=e[o++];this.pos===r&&this.keccak()}return this}finish(){if(this.finished)return;this.finished=!0;const{state:e,suffix:r,pos:t,blockLen:n}=this;e[t]^=r,r&128&&t===n-1&&this.keccak(),e[n-1]^=128,this.keccak()}writeInto(e){De(this,!1),V(e),this.finish();const r=this.state,{blockLen:t}=this;for(let n=0,o=e.length;n<o;){this.posOut>=t&&this.keccak();const i=Math.min(t-this.posOut,o-n);e.set(r.subarray(this.posOut,this.posOut+i),n),this.posOut+=i,n+=i}return e}xofInto(e){if(!this.enableXOF)throw new Error("XOF is not possible for this instance");return this.writeInto(e)}xof(e){return we(e),this.xofInto(new Uint8Array(e))}digestInto(e){if(mr(e,this),this.finished)throw new Error("digest() was already called");return this.writeInto(e),this.destroy(),e}digest(){return this.digestInto(new Uint8Array(this.outputLen))}destroy(){this.destroyed=!0,$e(this.state)}_cloneInto(e){const{blockLen:r,suffix:t,outputLen:n,rounds:o,enableXOF:i}=this;return e||(e=new Ct(r,t,n,i,o)),e.state32.set(this.state32),e.pos=this.pos,e.posOut=this.posOut,e.finished=this.finished,e.rounds=o,e.suffix=t,e.outputLen=n,e.enableXOF=i,e.destroyed=this.destroyed,e}}const Vn=(s,e,r,t={})=>br(()=>new Ct(e,s,r),t),ir=Vn(1,136,32),zn=60;class jn{constructor(){this.jwks=null}setJWKSEndpoint(e){try{this.jwks=nt.createRemoteJWKSet(new URL(e)),u.log("[CROSSx] JWKS 엔드포인트 설정:",e)}catch(r){u.warn("[CROSSx] JWKS 엔드포인트 URL 파싱 실패:",r)}}async verifyJWT(e,r){try{const t=this.decodeJWT(e);u.log("[CROSSx] JWT 디코딩 성공:",{sub:t.sub,exp:t.exp,iat:t.iat});const n=Math.floor(Date.now()/1e3);if(t.exp&&t.exp+zn<n)return u.warn("[CROSSx] 토큰 만료:",{exp:t.exp,now:n,만료시간:new Date(t.exp*1e3).toISOString()}),{payload:t,valid:!1,signatureVerified:!1};if(!t.sub)return u.warn("[CROSSx] JWT에 sub(사용자ID) 없음"),{payload:t,valid:!1,signatureVerified:!1};if(this.jwks)try{const{payload:o}=await nt.jwtVerify(e,this.jwks,{algorithms:["RS256","ES256"]});return u.log("[CROSSx] JWT 서명 검증 성공"),{payload:o,valid:!0,signatureVerified:!0}}catch(o){if(o instanceof Error&&(o.name==="JWSSignatureVerificationFailed"||o.name==="JWTClaimValidationFailed"))return u.error("[CROSSx] JWT 서명 검증 실패:",o),{payload:t,valid:!1,signatureVerified:!1};u.warn("[CROSSx] JWKS 엔드포인트 접근 불가 — 서명 미검증 모드로 전환")}return u.log("[CROSSx] JWT 검증 성공 (서명 미검증 — JWKS 미설정 또는 접근 불가)"),{payload:t,valid:!0,signatureVerified:!1}}catch(t){throw u.error("[CROSSx] JWT 검증 중 에러:",t),t}}decodeJWT(e){return nt.decodeJwt(e)}recoverPersonalSignSigner(e,r){const t=new TextEncoder().encode(e),n=new TextEncoder().encode(`Ethereum Signed Message:
|
|
8
8
|
${t.length}`),o=new Uint8Array(n.length+t.length);o.set(n,0),o.set(t,n.length);const i=ir(o),a=r.startsWith("0x")?r.slice(2):r;if(a.length!==130)throw new Error(`서명 길이가 유효하지 않습니다: 130 hex 문자 예상, 현재 ${a.length}`);const c=Yn(a),l=c.slice(0,32),d=c.slice(32,64),p=c[64],_=p>=27?p-27:p,w=new Mn.Signature(ar(l),ar(d)).addRecoveryBit(_).recoverPublicKey(i).toBytes(!1).slice(1),E=ir(w);return"0x"+He(E.slice(12))}}function Yn(s){const e=new Uint8Array(s.length/2);for(let r=0;r<e.length;r++)e[r]=parseInt(s.substring(r*2,r*2+2),16);return e}function ar(s){let e="0x";for(const r of s)e+=r.toString(16).padStart(2,"0");return BigInt(e)}const Xn=3e4,Jn=1e3;class Zn{async request(e){const r=e.retries??0;for(let t=0;t<=r;t++){try{const n=await this._doRequest(e);if(n.status<500||t===r)return n}catch(n){if(t===r)throw n}await new Promise(n=>setTimeout(n,Jn*2**t))}throw new Error("Unexpected: retry loop exited without result")}async _doRequest(e){const r=new AbortController,t=setTimeout(()=>r.abort(),e.timeoutMs??Xn);try{const n=await fetch(e.url,{method:e.method,headers:e.headers,body:e.body?JSON.stringify(e.body):void 0,signal:r.signal,credentials:e.credentials});let o;const i=await n.text();try{o=i?JSON.parse(i):null}catch{o={_raw:i}}return{status:n.status,data:o,headers:Object.fromEntries(n.headers.entries())}}finally{clearTimeout(t)}}}const Qn=new Set(["https://cross-wallet-oauth.crosstoken.io","https://stg-cross-wallet-oauth.crosstoken.io","https://dev-cross-wallet-oauth.crosstoken.io"]);class Ee{static generateRandom16Hex(){const e=new Uint8Array(16);return crypto.getRandomValues(e),Array.from(e,r=>r.toString(16).padStart(2,"0")).join("")}static parseJwtPayload(e){const r=e.split(".");if(r.length<2)throw new Error("Invalid JWT format");const t=(r[1]??"").replace(/-/g,"+").replace(/_/g,"/"),n=atob(t);return JSON.parse(n)}static async sha256Hex(e){const r=new TextEncoder().encode(e),t=await crypto.subtle.digest("SHA-256",r);return Array.from(new Uint8Array(t),n=>n.toString(16).padStart(2,"0")).join("")}static async verifyIdTokenNonce(e,r){const t=Ee.parseJwtPayload(e),n=t.nonce,o=typeof t.iss=="string"?t.iss:"(unknown)";if(u.log("[CROSSx] nonce 검증 시작 —",{iss:o,nonceClaimType:typeof n,nonceClaimPresent:typeof n=="string"&&n!=="",appleNonceSupported:t.nonce_supported??"(field absent)"}),o.includes("securetoken.google.com")){u.log("[CROSSx] Firebase ID Token 감지 — nonce 검증 skip.","서버 측에서 Google/Apple nonce 검증이 완료된 것으로 간주합니다.");return}if(typeof n!="string"||n===""){u.warn("[CROSSx] nonce claim 누락 — nonce 검증 skip.",{iss:o,keys:Object.keys(t)});return}if(o.includes("appleid.apple.com")){const i=await Ee.sha256Hex(r);if(u.log("[CROSSx] Apple nonce 검증 —",{expectedHashLength:i.length,receivedHashLength:n.length,match:n===i}),n!==i)throw new Error("ID Token nonce 불일치 — replay attack 가능성이 있습니다")}else if(u.log("[CROSSx] Google nonce 검증 —",{expectedLength:r.length,receivedLength:n.length,match:n===r}),n!==r)throw new Error("ID Token nonce 불일치 — replay attack 가능성이 있습니다");u.log("[CROSSx] nonce 검증 성공 —",{iss:o})}openAuth(e){return new Promise((r,t)=>{const n=Math.max(e.width??500,500),o=Math.max(e.height??700,700),i=window.screenX+(window.outerWidth-n)/2,a=window.screenY+(window.outerHeight-o)/2,c=Ee.generateRandom16Hex(),l=Ee.generateRandom16Hex(),d=e.authUrl.includes("?")?"&":"?",p=`${e.authUrl}${d}state=${c}&nonce=${l}`;u.log("[CROSSx] OAuth 팝업 열기 — state, nonce 생성 완료:",{stateLength:c.length,nonceLength:l.length,authUrlBase:e.authUrl});const _=window.open(p,"CROSSx OAuth",`width=${n},height=${o},left=${i},top=${a}`);if(!_){t(new Error("팝업 창을 열 수 없습니다. 팝업 차단을 해제해 주세요."));return}const f=setTimeout(()=>{u.warn("[CROSSx] OAuth 타임아웃 (5분) — postMessage를 수신하지 못했습니다"),L(),t(new Error("Authentication timeout"))},5*60*1e3),x=10,w=30;let E=0,k=null;const O=()=>{clearInterval(P),u.warn("[CROSSx] COOP 감지 — OAuth 서버 응답을 "+w+"초간 대기합니다"),k=setTimeout(()=>{L(),t(new Error("OAuth 서버로부터 응답을 받지 못했습니다. 브라우저 보안 정책(COOP)으로 인해 팝업 통신이 차단되었을 수 있습니다. 다시 시도해 주세요."))},w*1e3)},P=setInterval(()=>{E++;try{_.closed&&(E<=x?O():(L(),t(new Error("로그인이 취소되었습니다"))))}catch{O()}},1e3),L=()=>{clearTimeout(f),clearInterval(P),k&&clearTimeout(k),window.removeEventListener("message",W)},W=q=>{if(q.origin!==e.expectedOrigin)return;if(!Qn.has(q.origin)){u.error("[CROSSx] postMessage origin이 허용 목록에 없음:",q.origin),L(),t(new Error("Unauthorized OAuth origin"));return}L();const J=typeof q.data.status=="string",G=J?q.data.data??{}:q.data;u.log("[CROSSx] OAuth postMessage 수신 —",{format:J?"wrapped":"flat",status:J?q.data.status:"(flat)"});const X=(G==null?void 0:G.state)??q.data.state;if(!X||X!==c){t(new Error("OAuth state mismatch — possible CSRF attack"));return}if(J&&q.data.status!=="success"){u.error("[CROSSx] OAuth 실패:",G==null?void 0:G.error),t(new Error((G==null?void 0:G.error)||"Authentication failed"));return}if(!J&&(G!=null&&G.error)){u.error("[CROSSx] OAuth 실패:",G.error),t(new Error(G.error||"Authentication failed"));return}const Q=G==null?void 0:G.idToken,U=G==null?void 0:G.accessToken,D=U??Q,B=(G==null?void 0:G.email)||void 0;if(u.log("[CROSSx] OAuth 토큰 수신:",{hasAccessToken:!!U,hasIdToken:!!Q,hasEmail:!!B}),!D){u.error("[CROSSx] 토큰을 찾을 수 없음:",q.data),t(new Error("Token not found in response"));return}const m={token:D,email:B},y=T=>{Ee.verifyIdTokenNonce(T,l).then(()=>r(m)).catch(C=>{u.error("[CROSSx] nonce 검증 실패:",C),t(C instanceof Error?C:new Error("nonce verification failed"))})};U?U.split(".").length===3?y(U):Q?y(Q):(u.warn("[CROSSx] opaque accessToken만 수신 (idToken 없음) — nonce 검증 불가.","서버 측 nonce 검증이 필요합니다."),r(m)):Q?y(Q):t(new Error("Token not found in response"))};window.addEventListener("message",W)})}}const Fe="crossx_wallet_data",Re="crossx_mock_pin_hash";class eo{constructor(e,r){this.storage=e,this.pinStore=r??null}async checkWallet(){if(this.migrateScenario!==void 0)return u.log("[Mock] checkWallet → migration_required"),"migration_required";const r=await this.storage.get(Fe)?"exists":"not_found";return u.log(`[Mock] checkWallet → ${r}`),r}async getOrCreateWallet(e){var r;try{const t=await this.storage.get(Fe);if(t)return t;const n={id:e,address:this.generateMockEvmAddress(),derivationPath:"m/44'/60'/0'/0/0",createdAt:Date.now()};await this.storage.set(Fe,n);const o=(r=this.pinStore)==null?void 0:r.get();return o&&(await this.storage.set(Re,o),u.log("[Mock] 지갑 생성 — PIN 저장됨")),n}catch(t){throw new g(h.WALLET_CREATION_FAILED,"지갑 생성에 실패했습니다",t)}}async getAddresses(e){const r=await this.storage.get(Fe);return r?[{address:r.address,index:0}]:[]}async getAddress(e,r){return{address:this.generateMockEvmAddress(),derivationPath:`m/44'/60'/0'/0/${r}`}}async prepare(e,r){const t="mock-"+crypto.randomUUID();return u.log(`[Mock] prepare action=${e} → uuid=${t}`),{uuid:t,expiresAt:new Date(Date.now()+5*6e4).toISOString()}}async signMessage(e,r,t,n=0,o,i){return u.log(`[Mock] signMessage chainId=${r} index=${n} uuid=${o} from=${i}: "${t}"`),{chainId:r,signature:this.generateMockSignature(),format:"hex"}}async signTypedData(e,r,t,n=0,o,i){return u.log(`[Mock] signTypedData chainId=${r} index=${n} uuid=${o} from=${i}`),{chainId:r,signature:this.generateMockSignature(),format:"hex"}}async signTransaction(e,r,t,n=0,o){u.log(`[Mock] signTransaction chainId=${r} index=${n} uuid=${o}:`,t);const i="0x"+Array(64).fill(0).map(()=>Math.floor(Math.random()*16).toString(16)).join("");return{chainId:r,signature:this.generateMockSignature(),txHash:i,format:"hex"}}async sendTransaction(e,r,t,n){return u.log(`[Mock] sendTransaction chainId=${r} uuid=${n}:`,t),{txHash:"0x"+Array(64).fill(0).map(()=>Math.floor(Math.random()*16).toString(16)).join("")}}async verifyPin(e){var t;if(u.log("[Mock] verifyPin"),this.pinScenario==="wrong")return!1;const r=await this.storage.get(Re);return r&&r!==e?!1:(r||await this.storage.set(Re,e),(t=this.pinStore)==null||t.set(e),u.log("[Mock] verifyPin → 성공"),!0)}async changePin(e,r){var o;if(u.log("[Mock] changePin"),this.pinScenario==="wrong")throw new g(h.PIN_WRONG,"Incorrect PIN.");const t=(o=this.pinStore)==null?void 0:o.get(),n=await this.storage.get(Re);if(n&&t&&n!==t)throw new g(h.PIN_WRONG,"Incorrect PIN.");await this.storage.set(Re,r),u.log("[Mock] changePin 완료")}async migrateWallet(e,r){var o;if(u.log(`[Mock] migrateWallet pin=${e} sub=${r}`),this.migrateScenario==="wrong_pin")throw new g(h.MIGRATION_FAILED,"Incorrect PIN.",{permanent:!1,lockExpiresAt:0,remainingAttempts:4,maxAttempts:5});if(this.migrateScenario!==void 0&&typeof this.migrateScenario=="object"&&"locked"in this.migrateScenario){const a={permanent:!1,lockExpiresAt:Math.floor(Date.now()/1e3)+1800,remainingAttempts:0,maxAttempts:5,...this.migrateScenario.locked};throw new g(h.MIGRATION_PIN_LOCKED,"Too many incorrect PIN attempts. Your account has been temporarily locked.",a)}const t={id:r,address:this.generateMockEvmAddress(),derivationPath:"m/44'/60'/0'/0/0",createdAt:Date.now()};await this.storage.set(Fe,t);const n=(o=this.pinStore)==null?void 0:o.get();return n&&(await this.storage.set(Re,n),u.log("[Mock] 마이그레이션 — PIN 저장됨")),t}async getShareC(){return u.log("[Mock] getShareC"),"mock-share-c-"+crypto.randomUUID()}async verifyRecoveryPin(e,r){return u.log("[Mock] verifyRecoveryPin"),{valid:!0}}generateMockEvmAddress(){return"0x"+Array(40).fill(0).map(()=>Math.floor(Math.random()*16).toString(16)).join("")}generateMockSignature(){return"0x"+Array(130).fill(0).map(()=>Math.floor(Math.random()*16).toString(16)).join("")}}class Ae{constructor(e,r,t,n,o){this._refreshPromise=null,this._walletStatusCache=null,this.adapterConfig=e,this.projectId=e.projectId,this.storage=r,this.transport=t,this.tokenStore=n,this.pinStore=o??null}setOnUnauthorized(e){this._onUnauthorized=e}setTokenRefresher(e){this._onTokenRefresh=e}getPin(){var e;return((e=this.pinStore)==null?void 0:e.get())??void 0}get baseUrl(){return this.adapterConfig.gatewayUrl}async ensureValidToken(){const e=this.tokenStore.get();if(e&&!this.tokenStore.isExpiringSoon(qt))return e;const r=!e||this.tokenStore.isExpiringSoon(qt);if(this._onTokenRefresh){if(await this.deduplicatedRefresh()){const n=this.tokenStore.get();if(n)return n}if(r)throw new g(h.SESSION_EXPIRED,"인증 세션이 만료되었습니다. 다시 로그인해 주세요.")}if(e)return u.warn("[CROSSx] 토큰 갱신 실패 — 기존 토큰으로 요청 시도"),e;throw new g(h.SESSION_EXPIRED,"인증 세션이 만료되었습니다. 다시 로그인해 주세요.")}deduplicatedRefresh(){return this._refreshPromise?this._refreshPromise:(u.log("[CROSSx] 토큰 만료 임박/만료 — 자동 갱신 시도"),this._refreshPromise=this._onTokenRefresh().finally(()=>{this._refreshPromise=null}),this._refreshPromise)}async request(e,r,t,n=!1){var c,l,d,p;const o=await this.ensureValidToken(),i=`${this.baseUrl}${r}`,a={Authorization:`Bearer ${o}`,"Content-Type":"application/json","X-Project-Id":this.projectId};try{const f=(await this.transport.request({url:i,method:e,headers:a,body:t??void 0})).data;if(f&&typeof f.code=="number"){if(f.code<0||f.code>=400){const x=f.message||f.data||"API 요청에 실패했습니다";u.error("[CROSSx] Wallet Gateway API 에러 (HTTP 200):",{code:f.code,message:x,url:i,method:e});const w=Ae.mapGatewayError(f.code,f.data);if(w===h.AUTH_NOT_AUTHENTICATED&&!n&&this._onTokenRefresh){if(u.warn("[CROSSx] Gateway -10002 감지 — 토큰 갱신 후 재시도"),await this.deduplicatedRefresh())return this.request(e,r,t,!0);throw new g(h.SESSION_EXPIRED,"인증 세션이 만료되었습니다. 다시 로그인해 주세요.")}if(w===h.USER_NOT_FOUND)throw u.warn("[CROSSx] Gateway -10033 감지 — 유저 미존재, 강제 로그아웃"),(c=this._onUnauthorized)==null||c.call(this),new g(h.USER_NOT_FOUND,Ae.getGatewayErrorMessage(f.code,x));const E=w===h.AUTH_NOT_AUTHENTICATED?h.SESSION_EXPIRED:w,k=E===h.SESSION_EXPIRED?"인증 세션이 만료되었습니다. 다시 로그인해 주세요.":Ae.getGatewayErrorMessage(f.code,x),O=f.data;let P;throw w===h.PIN_LOCKED?P=Ae.extractLockDetails(O):w===h.ADDRESS_LIMIT_EXCEEDED?P={limit:je,raw:O}:P=O??void 0,new g(E,k,P)}return u.log("[CROSSx] Wallet Gateway API 성공:",{code:f.code,url:i,method:e}),f.data??f}return f}catch(_){if(_ instanceof g)throw _;const f=((l=_.response)==null?void 0:l.status)??_.status;if((f===401||f===403)&&!n&&this._onTokenRefresh){if(u.warn(`[CROSSx] HTTP ${f} 감지 — 토큰 갱신 후 재시도`),await this.deduplicatedRefresh())return this.request(e,r,t,!0);throw new g(h.SESSION_EXPIRED,"인증 세션이 만료되었습니다. 다시 로그인해 주세요.")}if((d=_.response)!=null&&d.data){const x=_.response.data,w=x.message||x.data||"API 요청에 실패했습니다",E=x.code||"UNKNOWN";throw u.error("[CROSSx] Wallet Gateway API 에러 (HTTP 에러):",{code:E,message:w,url:i,method:e,status:(p=_.response)==null?void 0:p.status}),new g(h.UNKNOWN_ERROR,`Wallet Gateway 오류 (${E}): ${w}`)}throw _}}async checkWallet(){u.log("[CROSSx] GET /mnemonic/check");const e=await this.request("GET","/mnemonic/check");return u.log("[CROSSx] /mnemonic/check 결과:",e.result),this._walletStatusCache=e.result,e.result}async getOrCreateWallet(e){let r;if(this._walletStatusCache!==null?(r=this._walletStatusCache,this._walletStatusCache=null,u.log("[CROSSx] getOrCreateWallet — 캐시된 상태 사용:",r)):(u.log("[CROSSx] GET /mnemonic/check — 지갑 상태 확인"),r=await this.checkWallet(),this._walletStatusCache=null),r==="migration_required")throw u.log("[CROSSx] migration_required → MIGRATION_BACKUP_EXISTS throw"),new g(h.MIGRATION_BACKUP_EXISTS,"CROSSx 백업이 존재합니다. 마이그레이션을 진행해 주세요.");if(r==="exists"){u.log("[CROSSx] 기존 지갑 발견 — POST /mnemonic/address(0)로 주소 조회");const o=await this.getAddress(e,0);return u.log("[CROSSx] 주소 조회 완료 — address:",o.address),{id:e,address:o.address,derivationPath:"m/44'/60'/0'/0/0",createdAt:Date.now()}}u.log("[CROSSx] POST /mnemonic/create — 신규 지갑 생성");const t={ignoreBackup:!1,password:this.requirePin()},n=await this.request("POST","/mnemonic/create",t);return u.log("[CROSSx] /mnemonic/create 완료 — address:",n.address),{id:e,address:n.address,derivationPath:"m/44'/60'/0'/0/0",createdAt:Date.now()}}async getAddresses(e){return((await this.request("GET","/mnemonic/addresses")).addresses??[]).map(t=>({address:t.address,index:t.index,name:t.name}))}async getAddress(e,r){const t={password:this.requirePin(),index:r};return{address:(await this.request("POST","/mnemonic/address",t)).address,derivationPath:`m/44'/60'/0'/0/${r}`}}async prepare(e,r){let t;switch(e){case"sign":case"send":if(!r.tx)throw new g(h.TX_INVALID_PARAMS,"prepare: tx가 필요합니다");t={unsignedTx:this.buildUnsignedTx(r.tx)};break;case"sign-message":if(!r.message)throw new g(h.TX_INVALID_PARAMS,"prepare: message가 필요합니다");t={message:r.message},r.from&&(t.from=r.from);break;case"sign-typed-data":if(!r.typedData)throw new g(h.TX_INVALID_PARAMS,"prepare: typedData가 필요합니다");t={typedData:r.typedData},r.from&&(t.from=r.from);break}const n={action:e,params:t},o=await this.request("POST","/mnemonic/prepare",n);return{uuid:o.uuid,expiresAt:o.expiresAt}}async signMessage(e,r,t,n,o,i){const a={message:t,password:this.requirePin()};o&&(a.uuid=o),i&&(a.from=i);const c=await this.request("POST","/mnemonic/sign-message",a);return{chainId:r,signature:c.signature,format:"hex"}}async signTypedData(e,r,t,n,o,i){const a={typedData:t,password:this.requirePin()};o&&(a.uuid=o),i&&(a.from=i);const c=await this.request("POST",`/mnemonic/sign-typed-data/${encodeURIComponent(r)}`,a);return{chainId:r,signature:c.signature,format:"hex"}}buildUnsignedTx(e){return{from:e.from,to:e.to,value:e.value,data:e.data,nonce:e.nonce!==void 0?`0x${e.nonce.toString(16)}`:void 0,gasLimit:e.gasLimit,gasPrice:e.gasPrice,maxFeePerGas:e.maxFeePerGas,maxPriorityFeePerGas:e.maxPriorityFeePerGas,chainId:e.chainId!==void 0?`0x${e.chainId.toString(16)}`:void 0}}async signTransaction(e,r,t,n,o){const i={unsignedTx:this.buildUnsignedTx(t),password:this.requirePin()};o&&(i.uuid=o);const a=await this.request("POST",`/mnemonic/sign/${encodeURIComponent(r)}`,i);return{chainId:r,signature:a.signedTx,txHash:a.txHash,format:"hex"}}async sendTransaction(e,r,t,n){const o={unsignedTx:this.buildUnsignedTx(t),password:this.requirePin()};return n&&(o.uuid=n),{txHash:(await this.request("POST",`/mnemonic/send/${encodeURIComponent(r)}`,o)).txHash}}async migrateWallet(e,r){u.log("[CROSSx][Migration Phase 4] POST /mnemonic/migrate 호출");const t={recoveryPin:e,sub:r,password:this.requirePin()},n=await this.request("POST","/mnemonic/migrate",t);return u.log("[CROSSx][Migration Phase 4] /mnemonic/migrate 완료 — address:",n.address),{id:r,address:n.address,derivationPath:"m/44'/60'/0'/0/0",createdAt:Date.now()}}async withdrawWallet(e){const r={confirmation:e,password:this.requirePin()};await this.request("POST","/mnemonic/withdraw",r)}async verifyPin(e){const r={password:e},t=await this.request("POST","/mnemonic/verify-password",r);if(!t.valid&&t.passwordStatus){const n=t.passwordStatus;if(n.permanent)throw new g(h.PIN_LOCKED,"Incorrect PIN.",{permanent:!0,lockExpiresAt:n.lockExpiresAt,remainingAttempts:0,maxAttempts:n.maxAttempts});if(n.remainingAttempts===0&&n.lockExpiresAt){const o=n.lockExpiresAt>1e10?n.lockExpiresAt:n.lockExpiresAt*1e3;throw new g(h.PIN_LOCKED,"Too many failed PIN attempts.",{permanent:!1,lockExpiresAt:o,remainingAttempts:0,maxAttempts:n.maxAttempts})}if(n.remainingAttempts!=null&&n.remainingAttempts>0)throw new g(h.PIN_LOCKED,"Incorrect PIN.",{permanent:!1,lockExpiresAt:0,remainingAttempts:n.remainingAttempts,maxAttempts:n.maxAttempts})}return t.valid}async changePin(e,r){const t={password:this.requirePin(),newPassword:r};await this.request("POST","/mnemonic/change-password",t)}async getShareC(){const e={password:this.requirePin()};return(await this.request("POST","/mnemonic/share-c",e)).shareC}async verifyRecoveryPin(e,r){const t={recoveryPin:e,sub:r};return this.request("POST","/mnemonic/verify-recovery-pin",t)}requirePin(){const e=this.getPin();if(!e)throw new g(h.PIN_NOT_SET,"PIN이 설정되지 않았습니다. PIN을 입력해 주세요.");return e}static extractLockDetails(e){const r=(e==null?void 0:e.lockStatus)??(e==null?void 0:e.lock_status)??e,t={},n=(r==null?void 0:r.lockExpiresAt)??(r==null?void 0:r.lock_expires_at);n&&(t.lockExpiresAt=n>1e10?n:n*1e3);const o=(r==null?void 0:r.remainingAttempts)??(r==null?void 0:r.remaining_attempts);typeof o=="number"&&(t.remainingAttempts=o);const i=(r==null?void 0:r.maxAttempts)??(r==null?void 0:r.max_attempts);return typeof i=="number"&&(t.maxAttempts=i),typeof(r==null?void 0:r.permanent)=="boolean"&&(t.permanent=r.permanent),t}static hasLockInfo(e){const r=(e==null?void 0:e.lockStatus)??(e==null?void 0:e.lock_status)??e;return!r||typeof r!="object"?!1:typeof r.remainingAttempts=="number"||typeof r.remaining_attempts=="number"||typeof r.maxAttempts=="number"||typeof r.max_attempts=="number"}static mapGatewayError(e,r){switch(e){case-10002:return h.AUTH_NOT_AUTHENTICATED;case-10001:return h.UNKNOWN_ERROR;case-10006:return h.GATEWAY_INTERNAL_ERROR;case-10007:return h.BROADCAST_FAILED;case-10008:return h.GATEWAY_LOCK_CONFLICT;case-10004:return h.WALLET_ALREADY_EXISTS;case-10005:return h.WALLET_NOT_FOUND;case-10011:return h.WALLET_NOT_FOUND;case-10030:return h.WALLET_INCONSISTENT_STATE;case-10010:return h.MIGRATION_FAILED;case-10012:return h.MIGRATION_BACKUP_EXISTS;case-10013:return h.WITHDRAW_FAILED;case-10033:return h.USER_NOT_FOUND;case-10036:return h.ADDRESS_LIMIT_EXCEEDED;case-10027:return h.MIGRATION_PIN_LOCKED;case-10022:return h.PROJECT_NOT_REGISTERED;case-10023:return h.PROJECT_ID_MISSING;case-10024:return h.ORIGIN_NOT_ALLOWED;case-10025:return h.INVALID_APP_TYPE;case-10028:return Ae.hasLockInfo(r)?h.PIN_LOCKED:h.PIN_WRONG;case-10029:return h.PIN_INVALID;case-10031:return h.PIN_LOCKED;case-10032:return h.PIN_REPEATED_PATTERN;case-10026:return h.TYPED_DATA_CHAIN_ID_MISMATCH;case-10014:case-10015:case-10019:return h.PREPARE_FAILED;case-10016:return h.PREPARE_EXPIRED;case-10017:case-10018:return h.PREPARE_MISMATCH;case-10020:return h.CHAIN_NOT_SUPPORTED;case-10021:return h.INVALID_CHAIN;case-10040:return h.HMAC_REQUIRED;case-10041:return h.HMAC_VERIFICATION_FAILED;default:return h.UNKNOWN_ERROR}}static getGatewayErrorMessage(e,r){switch(e){case-10002:return"Session expired. Please sign in again.";case-10004:return"Wallet already exists for this user.";case-10005:return"User wallet not found.";case-10006:return"Internal server error. Please try again later.";case-10007:return`Transaction broadcast failed (${r}). Please try again.`;case-10008:return"Another operation is in progress. Please try again later.";case-10013:return"Withdraw failed. Please try again.";case-10022:return"Project is not whitelisted. Verify your projectId and register the current origin in the management console.";case-10023:return"Project ID is required. Set the projectId field in SDKConfig.";case-10024:return"Origin or App ID is required. Register the current domain or set X-App-Id header.";case-10025:return"Invalid app type. X-App-Type must be android, ios, or windows.";case-10026:return"Domain chainId mismatch: the chainId in typedData.domain does not match the request chainId.";case-10027:return"Too many incorrect PIN attempts. Your account has been temporarily locked.";case-10028:return"Incorrect PIN. Please try again.";case-10029:return"Sequential PIN is not allowed. Please choose a different PIN.";case-10030:return"Wallet is in an inconsistent state. Please contact support.";case-10031:return"Too many failed PIN attempts. Your account is temporarily locked.";case-10032:return"Repeated digit pattern is not allowed. Please choose a different PIN.";case-10033:return"User not found. You have been signed out.";case-10036:return`Account limit reached. Maximum ${je} accounts allowed.`;case-10040:return"HMAC signature header is required.";case-10041:return"HMAC signature verification failed.";default:return`Request failed (${e}): ${r}`}}}class Ue{constructor(e,r){this.cache=new Map,this.loadedAt=0,this.usingFallback=!1,this._fetchPromise=null,this.adapterConfig=e,this.projectId=e.projectId,this.transport=r}get baseUrl(){return this.adapterConfig.gatewayUrl}async getChains(){return this.isCacheValid()?Array.from(this.cache.values()):this._fetchPromise?this._fetchPromise:(this._fetchPromise=this._doFetchChains().finally(()=>{this._fetchPromise=null}),this._fetchPromise)}async _doFetchChains(){try{const e=await this.request("/chains");this.handleEnvelopeError(e);const r=e.data.chains;return this.populateCache(r,!1),r}catch{const e=Ue.fallbackChains();return this.populateCache(e,!0),e}}async getChain(e){const t=(await this.getChains()).find(n=>n.chainId===e);return t||this.fallbackOrThrow(e)}invalidateCache(){this.cache.clear(),this.loadedAt=0,this.usingFallback=!1}isCacheValid(){if(this.loadedAt===0)return!1;const e=this.usingFallback?bs:ys;return e===0?!0:Date.now()-this.loadedAt<e}populateCache(e,r){this.cache.clear();for(const t of e)this.cache.set(t.chainId,t);this.loadedAt=Date.now(),this.usingFallback=r}async request(e){return(await this.transport.request({url:`${this.baseUrl}${e}`,method:"GET",headers:{"X-Project-Id":this.projectId}})).data}handleEnvelopeError(e){if(e.code<0){const r=typeof e.data=="string"?e.data:e.message??"체인 레지스트리 요청에 실패했습니다";throw new g(Ue.mapErrorCode(e.code),r)}}fallbackOrThrow(e){const r=Ue.fallbackChains().find(t=>t.chainId===e);if(r)return r;throw new g(h.CHAIN_NOT_SUPPORTED,`지원하지 않는 체인: ${e}`)}static fallbackChains(){return Object.values(et).map(e=>({chainId:e.caipId,rpcUrl:e.rpcUrl}))}static mapErrorCode(e){switch(e){case-10020:return h.CHAIN_NOT_SUPPORTED;case-10021:return h.INVALID_CHAIN;case-10022:return h.PROJECT_NOT_REGISTERED;case-10023:return h.PROJECT_ID_MISSING;case-10024:return h.APP_IDENTIFIER_MISSING;case-10025:return h.INVALID_APP_TYPE;default:return h.UNKNOWN_ERROR}}}const cr="__crossx-confirm-style",Z="__crossx-confirm-overlay",to={light:{bg:"#FFFFFF",border:"rgba(18,18,18,0.05)",titleColor:"#121212",subtitleColor:"rgba(18,18,18,0.7)",labelColor:"rgba(18,18,18,0.5)",valueColor:"#121212",pillBg:"rgba(18,18,18,0.05)",pillFromColor:"rgba(18,18,18,0.5)",pillAmtColor:"#121212",divider:"rgba(18,18,18,0.1)",closeColor:"#121212",closeHoverBg:"rgba(18,18,18,0.05)",copyColor:"rgba(18,18,18,0.5)",inputBg:"rgba(18,18,18,0.05)",inputBorder:"rgba(18,18,18,0.1)",hintColor:"rgba(18,18,18,0.5)",surfaceSubtle:"rgba(18,18,18,0.1)",primary:"#019D92",primaryGlow:"rgba(1,157,146,0.15)",secondary:"#E70077",errorColor:"#E70077",onPrimary:"#FFFFFF"},dark:{bg:"#121212",border:"rgba(255,255,255,0.05)",titleColor:"#FFFFFF",subtitleColor:"rgba(255,255,255,0.7)",labelColor:"rgba(255,255,255,0.5)",valueColor:"#FFFFFF",pillBg:"rgba(255,255,255,0.05)",pillFromColor:"rgba(255,255,255,0.5)",pillAmtColor:"#FFFFFF",divider:"rgba(255,255,255,0.1)",closeColor:"#FFFFFF",closeHoverBg:"rgba(255,255,255,0.05)",copyColor:"rgba(255,255,255,0.5)",inputBg:"rgba(255,255,255,0.05)",inputBorder:"rgba(255,255,255,0.1)",hintColor:"rgba(255,255,255,0.5)",surfaceSubtle:"rgba(255,255,255,0.1)",primary:"#019D92",primaryGlow:"rgba(1,157,146,0.15)",secondary:"#E70077",errorColor:"#E70077",onPrimary:"#FFFFFF"}};function lr(s,e){const r=to[s],t=e==null?void 0:e[s];return t?{...r,...t.primary!==void 0&&{primary:t.primary},...t.secondary!==void 0&&{secondary:t.secondary},...t.onPrimary!==void 0&&{onPrimary:t.onPrimary},...t.borderDefault!==void 0&&{border:t.borderDefault},...t.borderSubtle!==void 0&&{divider:t.borderSubtle,inputBorder:t.borderSubtle},...t.textIconPrimary!==void 0&&{titleColor:t.textIconPrimary,valueColor:t.textIconPrimary,pillAmtColor:t.textIconPrimary,closeColor:t.textIconPrimary},...t.textIconSecondary!==void 0&&{subtitleColor:t.textIconSecondary},...t.textIconTertiary!==void 0&&{labelColor:t.textIconTertiary,pillFromColor:t.textIconTertiary,copyColor:t.textIconTertiary,hintColor:t.textIconTertiary},...t.surfaceDefault!==void 0&&{pillBg:t.surfaceDefault,closeHoverBg:t.surfaceDefault,inputBg:t.surfaceDefault},...t.surfaceSubtle!==void 0&&{surfaceSubtle:t.surfaceSubtle},...t.bg!==void 0&&{bg:t.bg},...t.error!==void 0&&{errorColor:t.error}}:r}const ro=`
|
|
9
9
|
@import url('https://fonts.googleapis.com/css2?family=Sora:wght@400;600&display=swap');
|
|
10
10
|
|