Skip to content

Commit

Permalink
fix: bring back old parameters for RPC call on solana_signTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
zoruka committed Aug 12, 2024
1 parent af4e05e commit 5b73ba7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand All @@ -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 {
Expand Down
59 changes: 57 additions & 2 deletions packages/solana/src/connectors/walletConnectConnector.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 ?? ''
}
}
}
10 changes: 7 additions & 3 deletions packages/solana/src/utils/scaffold/SolanaTypesUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,13 @@ export interface RequestMethods {
transaction: string
pubkey: string
}
returns: {
transaction: string
}
returns:
| {
signature: string
}
| {
transaction: string
}
}
solana_signAndSendTransaction: {
params: {
Expand Down

0 comments on commit 5b73ba7

Please sign in to comment.