Use of public address program accounts (PDA) in another program
In the ecosystem of Solana, the program public address (PDA) account allows for safe and effective management of token transfers. However, when it comes to using such an account in another program or application, there are specific requirements that must be fulfilled.
background
PDA is a public address that any program can use to receive funds. However, not all programs allow withdrawal from their PDA accounts. In order to solve this problem, we must consider how the withdrawal procedure works and what is necessary for the account to be allowed in another program.
Procedure of withdrawal
When you create a PDA account in the Solana program, the following steps take place:
- The program creates a PDA account using its own wallet.
- When the user withdraws funds from his PDA account, the withdrawal transaction shall be sent to the PDA program account.
- The PDA program account signs the withdrawal transaction with its private key, which allows it to check the sender and the amount withdrawn.
Problem
Now, let’s consider how the account created in the program can use the withdrawal procedure on a white list that only allows users from certain addresses to the white list to withdraw the funds. This requires additional setting and configuration:
- PDA account must be configured to accept the withdrawal from the original program.
- The original program must implement the function that informs the white list when trying to withdraw, using the PDA account public address.
- When the user tries to withdraw funds using his PDA account, the following steps are happening:
a. The user triggers the transaction of withdrawal and surrendering it to the network.
b. If the original program correctly configured the PDA account, it will sign the withdrawal transaction with its private key.
Solution
To solve this problem, we can implement the custom solution that allows the account created in the program to use the withdrawal procedure on the white list when connected to the same Solan network. Here is one possible approach:
- Get to know the new wallet on the original program that provides a PDA similar address.
- Configure the new wallet to accept withdrawal from your public address using the private key to the original program.
3
Example Code
Here’s a simple example of how this can be done using the Solan SDK:
`Typescript
Import {Account, program} from “@Solana/Web3.js”;
Import {spltoKenprogram} from “./spl-token-program”;
Const Originalprogram = new program (spltokenprogram, “original_program”);
Const Newwallet = new account (“New_wallet”, “New_wallet_address”);
ASYNC withdrawal function (account: account) {
// Get a private key to a similar PDA account address
Const Privatekey = Weet Account.getprivate ();
// Create a new wallet with a new PDA -like address
Const NewwalletProgram = new program (spltookenProgram, “New_wallet-Program”);
Const Newwalletaddress = wait for NewwalletProgram.createaccount (Privatekey);
// Sign the withdrawal transaction using a new address of a similar PDA
Const withdrawaltransation = {
account,
amount: spltoKenprogram.Amount,
};
Const signatransation = wait for the originalprogram.signtransaction (GUPALTRANRANSACTION, Privatekey);
// Send a signed transaction to the network
await the originalprogram.sendsignniedtransation (signed);
Iche
// Use:
Const Newwallet = Wait OriginalProgram.newallet ();
NewwalletProgram = waiting for Newwallet.createaccount (Newwalletaddress);
Report (original program.