@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 1.08 kB
JavaScript
import{useMemo as r}from"react";import{create as e,rawSign as t}from"@privy-io/js-sdk-core";import{u as a}from"./internal-context-LlMdpfkb.mjs";import{u as i,l as s,g as n}from"./privy-context-V2csHgUI.mjs";import{u as o}from"./use-sign-with-user-signer-DwqsbciW.mjs";import"viem";const u=()=>{let{refreshSessionAndUser:t,privy:i}=a();return r((()=>({async createWallet(r){let a=await e(i,{request:{chain_type:r.chainType}}),s=await t();if(!s)throw Error("User must be authenticated before creating a Privy wallet");return{user:s,wallet:a}}})),[t,i])},l=()=>{let{user:e}=i(),{privy:u}=a(),{signWithUserSigner:l}=o();return r((()=>({async signRawHash({address:r,chainType:a,hash:i}){if(!e)throw Error("User must be authenticated before signing");let o=s(e,a).filter((r=>n(r))).find((e=>e.address===r));if(!o)throw Error("Wallet not found");let d=await t(u,l,{wallet_id:o.id,params:{hash:i}});if(d.data&&"signature"in d.data&&"hex"===d.data.encoding)return{signature:d.data.signature};throw Error("Failed to sign")}})),[u,l,e])};export{u as useCreateWallet,l as useSignRawHash};