UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) 2.23 kB
import{useMemo as e}from"react";import{create as t,rawSign as r}from"@privy-io/js-sdk-core";import{u as a,a as i}from"./internal-context-e-Eni5bG.mjs";import{u as n,a as s,h as o,d,n as l}from"./get-is-unified-wallet-DvKS5IUO.mjs";import{u as c}from"./context-Bycd1SKV.mjs";import{u as p}from"./use-sign-with-user-signer-eEm9Olt_.mjs";import"ofetch";import"viem";import"react/jsx-runtime";import"tinycolor2";const u=()=>{let{refreshSessionAndUser:r,privy:i}=a();return e((()=>({async createWallet(e){let a=await t(i,{request:{chain_type:e.chainType}}),n=await r();if(!n)throw Error("User must be authenticated before creating a Privy wallet");return{user:n,wallet:a}}})),[r,i])};function m(){let{user:t,getAccessToken:r}=n(),{walletProxy:l,openModal:p,client:u}=a(),{setModalData:m}=s(),h=c();return e((()=>({exportWallet:e=>new Promise((async(a,n)=>{if(!t)return void n(new i("User must be authenticated before exporting their Privy wallet"));let s=o(t,e.address);s?"spark"===s.chainType&&d(s)?await r()?l?(m({connectWallet:{recoveryMethod:s.recoveryMethod,connectingWalletAddress:s.address,isUnifiedWallet:!0,onCompleteNavigateTo:"EmbeddedWalletKeyExportScreen",onFailure:n,shouldForceMFA:!0},keyExport:{appId:h.id,appClientId:h.appClientId,origin:u.apiUrl,address:s.address,hdWalletIndex:s.walletIndex,chainType:s.chainType,walletId:s.id,isUnifiedWallet:!0,imported:s.imported,onSuccess:a,onFailure:n}}),p("EmbeddedWalletConnectingScreen")):n(new i("Wallet not ready for export")):n(new i("Must have valid access token.")):n(Error("Export via extended-chains is only supported for unified stack wallets")):n(new i("User must have an embedded wallet."))}))})),[t,r,l,p,u,m,h])}const h=()=>{let{user:t}=n(),{privy:i}=a(),{signWithUserSigner:s}=p();return e((()=>({async signRawHash({address:e,chainType:a,hash:n}){if(!t)throw Error("User must be authenticated before signing");let o=l(t,a).filter((e=>d(e))).find((t=>t.address===e));if(!o)throw Error("Wallet not found");let c=await r(i,s,{wallet_id:o.id,params:{hash:n}});if(c.data&&"signature"in c.data&&"hex"===c.data.encoding)return{signature:c.data.signature};throw Error("Failed to sign")}})),[i,s,t])};export{u as useCreateWallet,m as useExportWallet,h as useSignRawHash};