Overview
The SolanaAccount represents a server-managed Solana account that provides signing capabilities and blockchain interaction methods for the Solana network.
Type Definition
type SolanaAccount = {
address : string ;
name ?: string ;
policies ?: string [];
publicKey : string ;
signMessage : ( options : { message : string }) => Promise < SignatureResult >;
signTransaction : ( options : { transaction : string }) => Promise < SignTransactionResult >;
};
Properties
The Solana address of the account (base58 encoded).
Optional name for the account.
Array of Policy IDs that apply to the account.
The public key of the account (base58 encoded).
Methods
signMessage
Signs a message and returns the signature.
The message to sign (UTF-8 string).
The signature as a base58 encoded string.
const result = await account . signMessage ({
message: "Hello, Solana!" ,
});
console . log ( "Signature:" , result . signature );
signTransaction
Signs a Solana transaction and returns the signed transaction.
The base64 encoded transaction to sign.
The signed transaction as a base64 encoded string.
The signature as a base58 encoded string.
import { Transaction , SystemProgram , PublicKey } from "@solana/web3.js" ;
// Create a Solana transaction
const transaction = new Transaction (). add (
SystemProgram . transfer ({
fromPubkey: new PublicKey ( account . address ),
toPubkey: new PublicKey ( "9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin" ),
lamports: 1000000 , // 0.001 SOL
})
);
// Serialize with requireAllSignatures: false
const serializedTx = transaction . serialize ({
requireAllSignatures: false ,
});
// Sign the transaction
const result = await account . signTransaction ({
transaction: Buffer . from ( serializedTx ). toString ( "base64" ),
});
console . log ( "Signed transaction:" , result . signedTransaction );
SolanaClient Methods
The following methods are available through cdp.solana:
createAccount
Creates a new Solana account.
Optional name for the account.
Optional policy ID to apply.
Idempotency key for request.
The created Solana account.
const account = await cdp . solana . createAccount ({
name: "My Solana Account" ,
});
getAccount
Retrieves an existing Solana account.
The address of the account to retrieve.
The name of the account to retrieve.
// By address
const account = await cdp . solana . getAccount ({
address: "9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin" ,
});
// By name
const account = await cdp . solana . getAccount ({
name: "My Solana Account" ,
});
getOrCreateAccount
Retrieves an account by name, or creates it if it doesn’t exist.
The account (existing or newly created).
const account = await cdp . solana . getOrCreateAccount ({
name: "My Solana Account" ,
});
listAccounts
Lists all Solana accounts.
Number of accounts per page.
Array of Solana accounts.
let page = await cdp . solana . listAccounts ();
while ( page . nextPageToken ) {
page = await cdp . solana . listAccounts ({
pageToken: page . nextPageToken ,
});
}
updateAccount
Updates a Solana account.
The address of the account to update.
New name for the account.
const account = await cdp . solana . updateAccount ({
address: "9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin" ,
update: {
name: "Updated Name" ,
},
});
importAccount
Imports a Solana account using a private key.
privateKey
string | Uint8Array
required
The private key (base58 string or raw bytes).
Optional name for the account.
const account = await cdp . solana . importAccount ({
privateKey: "3Kzj..." , // base58 encoded private key
name: "Imported Account" ,
});
exportAccount
Exports a Solana account’s private key.
The address of the account to export.
The name of the account to export.
The 64-byte private key as a base58 encoded string.
const privateKey = await cdp . solana . exportAccount ({
address: "9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin" ,
});
// Store securely!
console . log ( "Private key:" , privateKey );
listTokenBalances
Lists token balances for a Solana account.
The network (defaults to “solana”).
Number of balances per page.
const { balances } = await cdp . solana . listTokenBalances ({
address: account . address ,
network: "solana-devnet" ,
});
balances . forEach ( balance => {
console . log ( ` ${ balance . token . symbol } : ${ balance . amount . amount } ` );
});
requestFaucet
Requests testnet funds from a Solana faucet.
The address to receive funds.
The transaction signature.
const result = await cdp . solana . requestFaucet ({
address: account . address ,
token: "sol" ,
});
console . log ( "Faucet signature:" , result . signature );
sendTransaction
Sends a Solana transaction to the network.
The base64 encoded transaction.
The transaction signature.
const result = await cdp . solana . sendTransaction ({
network: "solana-devnet" ,
transaction: signedTransaction ,
});
console . log ( "Transaction signature:" , result . signature );
signMessage
Signs a message with a Solana account.
const result = await cdp . solana . signMessage ({
address: account . address ,
message: "Hello, Solana!" ,
});
signTransaction
Signs a Solana transaction.
The base64 encoded transaction.
const result = await cdp . solana . signTransaction ({
address: account . address ,
transaction: serializedTransaction ,
});
Example: Complete Transfer Flow
import { CdpClient } from "@coinbase/cdp-sdk" ;
import { Transaction , SystemProgram , PublicKey , Connection } from "@solana/web3.js" ;
const cdp = new CdpClient ();
// Create account
const account = await cdp . solana . createAccount ();
// Request testnet funds
await cdp . solana . requestFaucet ({
address: account . address ,
token: "sol" ,
});
// Create and sign transaction
const connection = new Connection ( "https://api.devnet.solana.com" );
const transaction = new Transaction (). add (
SystemProgram . transfer ({
fromPubkey: new PublicKey ( account . address ),
toPubkey: new PublicKey ( "9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin" ),
lamports: 1000000 ,
})
);
transaction . recentBlockhash = ( await connection . getLatestBlockhash ()). blockhash ;
transaction . feePayer = new PublicKey ( account . address );
const serializedTx = transaction . serialize ({
requireAllSignatures: false ,
});
const signedResult = await cdp . solana . signTransaction ({
address: account . address ,
transaction: Buffer . from ( serializedTx ). toString ( "base64" ),
});
// Send transaction
const result = await cdp . solana . sendTransaction ({
network: "solana-devnet" ,
transaction: signedResult . signedTransaction ,
});
console . log ( "Transaction signature:" , result . signature );