@privy-io/js-sdk-core
Version:
Vanilla JS client for the Privy Auth API
2 lines (1 loc) • 1.79 kB
JavaScript
"use strict";var e=require("../../Error.js"),r=require("../../wallet-api/unified-wallet.js"),t=require("./utils.js");require("../../utils/getAllUserEmbeddedEthereumWallets.js"),require("../../utils/getAllUserEmbeddedSolanaWallets.js"),require("../../utils/getUserEmbeddedEthereumWallet.js"),require("../../utils/getUserEmbeddedSolanaWallet.js");exports.delegateWallet=s=>async({address:l,chainType:a})=>{let{user:d}=await s.user.get();if(!d)throw new e.PrivyClientError({code:"delegated_actions_before_logged_in",error:"Must be logged in to delegate wallets"});if("solana"!==a&&"ethereum"!==a)throw new e.PrivyClientError({code:"unsupported_chain_type",error:"Only Solana and Ethereum embedded wallets are supported for delegation and revocation."});let i=t.getUserValidDelegateWallets(d).find((e=>e.chain_type===a&&e.address===l));if(!i)throw new e.PrivyClientError({code:"delegated_actions_wallet_not_found",error:"Address to delegate is not associated with current user."});if(r.isUnifiedWallet(i))throw new e.PrivyClientError({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:d};let o=t.getRootWalletForDelegation(i,d);if(!o)throw new e.PrivyClientError({code:"delegated_actions_wallet_not_found",error:"Address to delegate is not associated with current user."});await s.embeddedWallet.delegateWallets({rootWallet:{address:o.address,chainType:o.chain_type,imported:o.imported},delegatedWallets:[{address:i.address,chainType:i.chain_type,walletIndex:i.wallet_index}]});let{user:n}=await s.user.get();return{user:n}};