UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) 2.24 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-Z-fyxadS.mjs";import{u as s,a as n,h as o,b as d,m as l}from"./get-is-unified-wallet-Boa8DYrQ.mjs";import{u as c}from"./context-WTldtXS8.mjs";import{u as p}from"./use-sign-with-user-signer-Do5Oi_rb.mjs";import"ofetch";import"@privy-io/are-addresses-equal";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}}),s=await r();if(!s)throw Error("User must be authenticated before creating a Privy wallet");return{user:s,wallet:a}}})),[r,i])};function m(){let{user:t,getAccessToken:r}=s(),{walletProxy:l,openModal:p,client:u}=a(),{setModalData:m}=n(),h=c();return e((()=>({exportWallet:e=>new Promise((async(a,s)=>{if(!t)return void s(new i("User must be authenticated before exporting their Privy wallet"));let n=o(t,e.address);n?d(n)?await r()?l?(m({connectWallet:{recoveryMethod:n.recoveryMethod,connectingWalletAddress:n.address,isUnifiedWallet:!0,onCompleteNavigateTo:"EmbeddedWalletKeyExportScreen",onFailure:s,shouldForceMFA:!0},keyExport:{appId:h.id,appClientId:h.appClientId,origin:u.apiUrl,address:n.address,hdWalletIndex:n.walletIndex,chainType:n.chainType,walletId:n.id,isUnifiedWallet:!0,imported:n.imported,onSuccess:a,onFailure:s}}),p("EmbeddedWalletConnectingScreen")):s(new i("Wallet not ready for export")):s(new i("Must have valid access token.")):s(Error("Export via extended-chains is only supported for unified stack wallets")):s(new i("User must have an embedded wallet."))}))})),[t,r,l,p,u,m,h])}const h=()=>{let{user:t}=s(),{privy:i}=a(),{signWithUserSigner:n}=p();return e((()=>({async signRawHash({address:e,chainType:a,hash:s}){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,n,{wallet_id:o.id,params:{hash:s}});if(c.data&&"signature"in c.data&&"hex"===c.data.encoding)return{signature:c.data.signature};throw Error("Failed to sign")}})),[i,n,t])};export{u as useCreateWallet,m as useExportWallet,h as useSignRawHash};