From 5b73ba71c0e6d239fcc49d1a3141aa650c91bca0 Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Mon, 12 Aug 2024 10:28:04 -0300 Subject: [PATCH] fix: bring back old parameters for RPC call on solana_signTransaction --- .../Solana/SolanaSignTransactionTest.tsx | 6 +- .../src/connectors/walletConnectConnector.ts | 59 ++++++++++++++++++- .../src/utils/scaffold/SolanaTypesUtil.ts | 10 +++- 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/apps/laboratory/src/components/Solana/SolanaSignTransactionTest.tsx b/apps/laboratory/src/components/Solana/SolanaSignTransactionTest.tsx index eb1eacd852..41789ce2a4 100644 --- a/apps/laboratory/src/components/Solana/SolanaSignTransactionTest.tsx +++ b/apps/laboratory/src/components/Solana/SolanaSignTransactionTest.tsx @@ -51,7 +51,7 @@ export function SolanaSignTransactionTest() { const signature = signedTransaction.signatures[0]?.signature if (!signature) { - throw Error('Failed to sign transaction') + throw Error('Empty signature') } toast({ @@ -103,7 +103,7 @@ export function SolanaSignTransactionTest() { const signature = signedTransaction.signatures[0] if (!signature) { - throw Error('Failed to sign transaction') + throw Error('Empty signature') } toast({ @@ -114,7 +114,7 @@ export function SolanaSignTransactionTest() { } catch (err) { toast({ title: 'Error', - description: 'Failed to sign transaction', + description: (err as Error).message, type: 'error' }) } finally { diff --git a/packages/solana/src/connectors/walletConnectConnector.ts b/packages/solana/src/connectors/walletConnectConnector.ts index 5a1778ae55..7b4bf4e93e 100644 --- a/packages/solana/src/connectors/walletConnectConnector.ts +++ b/packages/solana/src/connectors/walletConnectConnector.ts @@ -1,5 +1,12 @@ import base58 from 'bs58' -import { Connection, Transaction, VersionedTransaction, type SendOptions } from '@solana/web3.js' +import { + Connection, + PublicKey, + Transaction, + TransactionMessage, + VersionedTransaction, + type SendOptions +} from '@solana/web3.js' import { OptionsController } from '@web3modal/core' import { SolStoreUtil } from '../utils/scaffold/index.js' @@ -89,9 +96,20 @@ export class WalletConnectConnector extends BaseConnector implements Connector { const result = await this.request('solana_signTransaction', { transaction: serializedTransaction, - pubkey: this.getPubkey() + pubkey: this.getPubkey(), + ...this.getRawRPCParams(transaction) }) + // If the result contains signature is the old RPC response + if ('signature' in result) { + transaction.addSignature( + new PublicKey(SolStoreUtil.state.address ?? ''), + Buffer.from(base58.decode(result.signature)) + ) + + return transaction + } + const decodedTransaction = base58.decode(result.transaction) if (isVersionedTransaction(transaction)) { @@ -205,4 +223,41 @@ export class WalletConnectConnector extends BaseConnector implements Connector { return address } + + /* + * This is a deprecated method that is used to support older versions of the + * WalletConnect RPC API. It should be removed in the future + */ + private getRawRPCParams(_transaction: AnyTransaction) { + let transaction = _transaction + + if (isVersionedTransaction(transaction)) { + if (!SolStoreUtil.state.address) { + throw new Error('No signer connected') + } + + const instructions = TransactionMessage.decompile(transaction.message).instructions + const legacyMessage = new TransactionMessage({ + payerKey: new PublicKey(SolStoreUtil.state.address), + recentBlockhash: transaction.message.recentBlockhash, + instructions: [...instructions] + }).compileToLegacyMessage() + + transaction = Transaction.populate(legacyMessage) + } + + return { + feePayer: transaction.feePayer?.toBase58() ?? '', + instructions: transaction.instructions.map(instruction => ({ + data: base58.encode(instruction.data), + keys: instruction.keys.map(key => ({ + isWritable: key.isWritable, + isSigner: key.isSigner, + pubkey: key.pubkey.toBase58() + })), + programId: instruction.programId.toBase58() + })), + recentBlockhash: transaction.recentBlockhash ?? '' + } + } } diff --git a/packages/solana/src/utils/scaffold/SolanaTypesUtil.ts b/packages/solana/src/utils/scaffold/SolanaTypesUtil.ts index c647848cba..e03625a24a 100644 --- a/packages/solana/src/utils/scaffold/SolanaTypesUtil.ts +++ b/packages/solana/src/utils/scaffold/SolanaTypesUtil.ts @@ -201,9 +201,13 @@ export interface RequestMethods { transaction: string pubkey: string } - returns: { - transaction: string - } + returns: + | { + signature: string + } + | { + transaction: string + } } solana_signAndSendTransaction: { params: {