UNPKG

@privy-io/js-sdk-core

Version:

Vanilla JS client for the Privy Auth API

2 lines (1 loc) 1.4 kB
"use strict";var e=require("../../Error.js"),t=require("./utils.js");require("../../utils/getAllUserEmbeddedEthereumWallets.js"),require("../../utils/getAllUserEmbeddedSolanaWallets.js"),require("../../utils/getUserEmbeddedEthereumWallet.js"),require("../../utils/getUserEmbeddedSolanaWallet.js");exports.delegateWallet=r=>async({address:l,chainType:a})=>{let{user:d}=await r.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 s=t.getUserValidDelegateWallets(d).find((e=>e.chain_type===a&&e.address===l));if(!s)throw new e.PrivyClientError({code:"delegated_actions_wallet_not_found",error:"Address to delegate is not associated with current user."});if(s.delegated)return{user:d};let i=t.getRootWalletForDelegation(s,d);if(!i)throw new e.PrivyClientError({code:"delegated_actions_wallet_not_found",error:"Address to delegate is not associated with current user."});await r.embeddedWallet.delegateWallets({rootWallet:{address:i.address,chainType:i.chain_type,imported:i.imported},delegatedWallets:[{address:s.address,chainType:s.chain_type,walletIndex:s.wallet_index}]});let{user:o}=await r.user.get();return{user:o}};