UNPKG

@wuwei-labs/srsly

Version:
103 lines 3.41 kB
/** * @purpose Universal signer handling for @solana/kit and @solana/web3.js compatibility * * Accepts signers from both libraries and extracts address strings for use with Codama. */ import { type TransactionSigner, type Address } from '@solana/kit'; /** * Universal signer type that accepts various signer formats * * Supports: * - String addresses (for browser usage) * - web3.js Keypair/Wallet (objects with `publicKey.toBase58()`) * - web3.js PublicKey (objects with direct `toBase58()`) * - @solana/kit TransactionSigner (objects with `address`) */ export type UniversalSigner = string | { publicKey: { toBase58(): string; }; } | { toBase58(): string; } | { address: string; } | TransactionSigner; /** * Extract address string from any supported signer format * * @param signer - Signer in any supported format * @returns Base58-encoded address string * @throws Error if signer format is invalid * * @example * ```typescript * // String address * const addr1 = getSignerAddress("9WzDXw..."); * * // web3.js Keypair * import { Keypair } from '@solana/web3.js'; * const keypair = Keypair.generate(); * const addr2 = getSignerAddress(keypair); * * // @solana/kit signer * import { createNoopSigner, address } from '@solana/kit'; * const kitSigner = createNoopSigner(address("9WzDXw...")); * const addr3 = getSignerAddress(kitSigner); * ``` */ export declare function getSignerAddress(signer: UniversalSigner): Address; /** * Validate that a string is a valid Solana address * * Basic validation - checks length only. For full validation, * use @solana/kit's address() function or web3.js PublicKey constructor. * * @param address - Address string to validate * @throws Error if address format is invalid */ export declare function validateAddress(address: string): void; /** * Type guard to check if a value is a web3.js signer */ export declare function isWeb3jsSigner(signer: UniversalSigner): signer is { publicKey: { toBase58(): string; }; }; /** * Type guard to check if a value is a @solana/kit signer */ export declare function isKitSigner(signer: UniversalSigner): signer is { address: string; } | TransactionSigner; /** * Type guard to check if a value is a string address */ export declare function isAddressString(signer: UniversalSigner): signer is string; /** * Convert UniversalSigner to TransactionSigner * * Intelligently handles different signer types: * - If already a TransactionSigner with signing capability → return as-is * - Otherwise → create noop signer from address (for browser/wallet cases) * * This enables the SDK to work in both: * - Browser/wallet contexts (where you only have addresses, not private keys) * - CLI/backend contexts (where you have full keypair signers) * * @param signer - Signer in any supported format * @returns TransactionSigner (either real or noop) * * @example * ```typescript * // CLI/backend - has private key * const wallet = await createKeyPairSignerFromBytes(secretKey); * const txSigner = toTransactionSigner(wallet); // Returns wallet as-is * * // Browser - only has address * const walletAddress = "9WzDXw..."; * const txSigner = toTransactionSigner(walletAddress); // Creates noop signer * ``` */ export declare function toTransactionSigner(signer: UniversalSigner): TransactionSigner; //# sourceMappingURL=signer.d.ts.map