UNPKG

@magiceden/magiceden-sdk

Version:

A TypeScript SDK for interacting with Magic Eden's API across multiple chains.

149 lines (148 loc) 4.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SolanaKeypairWalletProvider = void 0; const web3_js_1 = require("@solana/web3.js"); const bs58_1 = __importDefault(require("bs58")); const tweetnacl_1 = __importDefault(require("tweetnacl")); const solanaWalletProvider_1 = require("./solanaWalletProvider"); /** * A Solana wallet implementation using a local keypair */ class SolanaKeypairWalletProvider extends solanaWalletProvider_1.SolanaWalletProvider { /** * Creates a new Solana wallet provider */ constructor(config) { super(); // Initialize the RPC connection this.connection = new web3_js_1.Connection(config.rpcEndpoint, 'confirmed'); // Create the keypair from the provided secret this.keypair = this.createKeyFromSecret(config.secretKey); } /** * Gets the RPC connection */ getConnection() { return this.connection; } /** * Gets the wallet's public address */ getAddress() { return this.keypair.publicKey.toBase58(); } /** * Gets the wallet's SOL balance */ async getBalance() { return this.connection.getBalance(this.keypair.publicKey).then((balance) => BigInt(balance)); } /** * Sign a message * * @param message - The message to sign * @returns The base58 encoded signature */ async signMessage(message) { const messageBytes = typeof message === 'string' ? new TextEncoder().encode(message) : message; const signature = tweetnacl_1.default.sign.detached(messageBytes, this.keypair.secretKey); return bs58_1.default.encode(signature); } /** * Sign a transaction * * @param transaction - The transaction to sign * @returns The signed transaction */ async signTransaction(transaction) { transaction.sign([this.keypair]); return transaction; } /** * Sign and send a transaction * * @param transaction - The transaction to sign and send * @returns The signature */ async signAndSendTransaction(transaction) { const signedTransaction = await this.signTransaction(transaction); return await this.connection.sendTransaction(signedTransaction); } /** * Get the status of a transaction * * @param signature - The signature * @param options - The options for the status * @returns The status */ async getSignatureStatus(signature, options) { return this.connection.getSignatureStatus(signature, options); } /** * Wait for transaction confirmation * * @param signature - The signature * @returns The confirmation response */ async waitForTransactionConfirmation(signature) { try { // Get the latest blockhash for transaction confirmation const { blockhash, lastValidBlockHeight } = await this.connection.getLatestBlockhash({ commitment: 'confirmed', }); // Wait for confirmation const confirmResult = await this.connection.confirmTransaction({ blockhash, lastValidBlockHeight, signature, }, 'confirmed'); // Format the receipt return { txId: signature, status: confirmResult.value.err ? 'failed' : 'confirmed', error: confirmResult.value.err ? String(confirmResult.value.err) : undefined, metadata: { blockhash, lastValidBlockHeight, }, }; } catch (error) { // Handle timeout or other errors return { txId: signature, status: 'failed', error: error instanceof Error ? error.message : 'Unknown error during confirmation', metadata: { errorType: error instanceof Error ? error.name : 'Unknown', }, }; } } /** * Helper to create a keypair from different secret formats */ createKeyFromSecret(secret) { if (typeof secret === 'string') { try { const decoded = bs58_1.default.decode(secret); return web3_js_1.Keypair.fromSecretKey(decoded); } catch (error) { throw new Error('Invalid base58 encoded secret key'); } } else { try { return web3_js_1.Keypair.fromSecretKey(secret); } catch (error) { throw new Error('Invalid secret key bytes'); } } } } exports.SolanaKeypairWalletProvider = SolanaKeypairWalletProvider;