UNPKG

@privy-io/js-sdk-core

Version:

Vanilla JS client for the Privy Auth API

2 lines (1 loc) 1.73 kB
import{PrivyClientError as e}from"../../Error.mjs";import{isUnifiedWallet as t}from"../../wallet-api/unified-wallet.mjs";import{getUserValidDelegateWallets as r,getRootWalletForDelegation as s}from"./utils.mjs";import"../../utils/getAllUserEmbeddedEthereumWallets.mjs";import"../../utils/getAllUserEmbeddedSolanaWallets.mjs";import"../../utils/getUserEmbeddedEthereumWallet.mjs";import"../../utils/getUserEmbeddedSolanaWallet.mjs";const o=o=>async({address:d,chainType:a})=>{let{user:l}=await o.user.get();if(!l)throw new e({code:"delegated_actions_before_logged_in",error:"Must be logged in to delegate wallets"});if("solana"!==a&&"ethereum"!==a)throw new e({code:"unsupported_chain_type",error:"Only Solana and Ethereum embedded wallets are supported for delegation and revocation."});let i=r(l).find((e=>e.chain_type===a&&e.address===d));if(!i)throw new e({code:"delegated_actions_wallet_not_found",error:"Address to delegate is not associated with current user."});if(t(i))throw new e({code:"unsupported_wallet_type",error:"useDelegatedActions is only supported for on-device execution and this app uses TEE execution. Use the useSessionSigners hook to provision server side access on behalf of your users. Learn more https://docs.privy.io/recipes/tee-wallet-migration-guide"});if(i.delegated)return{user:l};let n=s(i,l);if(!n)throw new e({code:"delegated_actions_wallet_not_found",error:"Address to delegate is not associated with current user."});await o.embeddedWallet.delegateWallets({rootWallet:{address:n.address,chainType:n.chain_type,imported:n.imported},delegatedWallets:[{address:i.address,chainType:i.chain_type,walletIndex:i.wallet_index}]});let{user:u}=await o.user.get();return{user:u}};export{o as delegateWallet};