@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
72 lines • 2.38 kB
JavaScript
/**
* @purpose Conversion helpers for @solana/web3.js compatibility
*
* Converts between @solana/kit and @solana/web3.js instruction formats.
*/
/**
* Convert @solana/kit instruction to @solana/web3.js format
*
* @param kitInstruction - Instruction in @solana/kit format (from Codama)
* @param PublicKey - Optional PublicKey constructor for web3.js compatibility
* @returns Instruction in @solana/web3.js format
*
* @example
* ```typescript
* // Without PublicKey - returns string addresses
* const web3Ix = toWeb3Instruction(kitIx);
* // web3Ix.keys[0].pubkey is a string
*
* // With PublicKey - returns PublicKey instances
* import { PublicKey } from '@solana/web3.js';
* const web3Ix = toWeb3Instruction(kitIx, PublicKey);
* // web3Ix.keys[0].pubkey is a PublicKey instance
* ```
*/
export function toWeb3Instruction(kitInstruction, PublicKey) {
const { accounts = [], programAddress, data } = kitInstruction;
if (PublicKey) {
// Return instruction with PublicKey instances
return {
keys: accounts.map(account => ({
pubkey: new PublicKey(account.address),
isWritable: (account.role & 1) === 1, // Check writable bit
isSigner: (account.role & 2) === 2, // Check signer bit
})),
programId: new PublicKey(programAddress),
data: data || new Uint8Array(),
};
}
// Return instruction with string addresses
return {
keys: accounts.map(account => ({
pubkey: account.address,
isWritable: (account.role & 1) === 1,
isSigner: (account.role & 2) === 2,
})),
programId: programAddress,
data: data || new Uint8Array(),
};
}
/**
* Convert PublicKey or Address to string
*
* Helper function for normalizing addresses from different libraries.
*
* @param value - Address as string or PublicKey instance
* @returns Base58-encoded address string
*
* @example
* ```typescript
* // String address (passthrough)
* const addr1 = toAddress("9WzDXw...");
*
* // web3.js PublicKey
* import { PublicKey } from '@solana/web3.js';
* const pubkey = new PublicKey("9WzDXw...");
* const addr2 = toAddress(pubkey);
* ```
*/
export function toAddress(value) {
return typeof value === 'string' ? value : value.toBase58();
}
//# sourceMappingURL=web3js.js.map