import { SuiClient } from '@mysten/sui.js/client'; import { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519'; import { TransactionBlock } from '@mysten/sui.js/transactions'; import { SUI_CLIENT } from '@/lib/globals'; export class LocalWallet { private readonly kp: Ed25519Keypair; private readonly client: SuiClient; constructor(client: SuiClient, privateKey?: Uint8Array) { this.client = client; this.kp = privateKey ? Ed25519Keypair.fromSecretKey(privateKey) : new Ed25519Keypair(); } static getDefault() { if (!process.env.SUI_WALLET_PRIV_KEY) { throw new Error('Private key not found. Please set private key from environment variable SUI_WALLET_PRIV_KEY'); } if (!process.env.SUI_WALLET_PRIV_KEY2) { throw new Error('Private key not found. Please set private key from environment variable SUI_WALLET_PRIV_KEY'); } const privateKeys = [process.env.SUI_WALLET_PRIV_KEY, process.env.SUI_WALLET_PRIV_KEY2]; return privateKeys.map((privateKey) => { const pk = this.base64ToUint8Array(privateKey); return new LocalWallet(SUI_CLIENT, pk.slice(1)); }); } async address() { return this.kp.toSuiAddress(); } async signAndSubmitTransaction(txb: TransactionBlock) { txb.setSenderIfNotSet(await this.address()); return this.client.signAndExecuteTransactionBlock({ transactionBlock: txb, signer: this.kp, options: { showEffects: true, showObjectChanges: true, showEvents: true, }, }); } async inspect(txb: TransactionBlock) { return this.client.devInspectTransactionBlock({ sender: await this.address(), transactionBlock: txb, }); } private static base64ToUint8Array(base64: string): Uint8Array { const binaryString = atob(base64); const len = binaryString.length; const array = new Uint8Array(len); for (let i = 0; i < len; i++) { array[i] = binaryString.charCodeAt(i); } return array; } }