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 authored Aug 14, 2024
1 parent 3e2b0aa commit e768db8
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 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
49 changes: 47 additions & 2 deletions packages/solana/src/providers/WalletConnectProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Connection,
PublicKey,
Transaction,
TransactionMessage,
VersionedTransaction,
type SendOptions
} from '@solana/web3.js'
Expand Down Expand Up @@ -120,9 +121,20 @@ export class WalletConnectProvider extends ProviderEventEmitter implements Provi

const result = await this.request('solana_signTransaction', {
transaction: serializedTransaction,
pubkey: this.getAccount(true).address
pubkey: this.getAccount(true).address,
...this.getRawRPCParams(transaction)
})

// If the result contains signature is the old RPC response
if ('signature' in result) {
transaction.addSignature(
new PublicKey(this.getAccount(true).publicKey),
Buffer.from(base58.decode(result.signature))
)

return transaction
}

const decodedTransaction = base58.decode(result.transaction)

if (isVersionedTransaction(transaction)) {
Expand Down Expand Up @@ -226,6 +238,39 @@ export class WalletConnectProvider extends ProviderEventEmitter implements Provi

return chains
}

/*
* 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)) {
const instructions = TransactionMessage.decompile(transaction.message).instructions
const legacyMessage = new TransactionMessage({
payerKey: new PublicKey(this.getAccount(true).publicKey),
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 ?? ''
}
}
}

export namespace WalletConnectProvider {
Expand All @@ -238,7 +283,7 @@ export namespace WalletConnectProvider {
solana_signMessage: Request<{ message: string; pubkey: string }, { signature: string }>
solana_signTransaction: Request<
{ transaction: string; pubkey: string },
{ transaction: string }
{ signature: string } | { transaction: string }
>
solana_signAndSendTransaction: Request<
{ transaction: string; pubkey: string; sendOptions?: SendOptions },
Expand Down
62 changes: 61 additions & 1 deletion packages/solana/tests/WalletConnectProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from 'vitest'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { mockUniversalProvider } from './mocks/UniversalProvider'
import { WalletConnectProvider } from '../src/providers/WalletConnectProvider'
import { TestConstants } from './util/TestConstants'
Expand Down Expand Up @@ -53,6 +53,26 @@ describe('WalletConnectProvider specific tests', () => {
expect(provider.request).toHaveBeenCalledWith({
method: 'solana_signTransaction',
params: {
feePayer: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP',
instructions: [
{
data: '3Bxs4NN8M2Yn4TLb',
keys: [
{
isSigner: true,
isWritable: true,
pubkey: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP'
},
{
isSigner: false,
isWritable: true,
pubkey: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP'
}
],
programId: '11111111111111111111111111111111'
}
],
recentBlockhash: 'EZySCpmzXRuUtM95P2JGv9SitqYph6Nv6HaYBK7a8PKJ',
transaction:
'AKhoybLLJS1deDJDyjELDNhfkBBX3k4dt4bBfmppjfPVVimhQdFEfDo8AiFcCBCC9VkYWV2r3jkh9n1DAXEhnJPwMmnsrx6huAVrhHAbmRUqfUuWZ9aWMGmdEWaeroCnPR6jkEnjJcn14a59TZhkiTXMygMqu4KaqD1TqzE8vNHSw3YgbW24cfqWfQczGysuy4ugxj4TGSpqRtNmf5D7zRRa76eJTeZEaBcBQGkqxb31vBRXDMdQzGEbq',
pubkey: TestConstants.accounts[0].address
Expand All @@ -68,6 +88,26 @@ describe('WalletConnectProvider specific tests', () => {
expect(provider.request).toHaveBeenCalledWith({
method: 'solana_signTransaction',
params: {
feePayer: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP',
instructions: [
{
data: '3Bxs4NN8M2Yn4TLb',
keys: [
{
isSigner: true,
isWritable: true,
pubkey: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP'
},
{
isSigner: true,
isWritable: true,
pubkey: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP'
}
],
programId: '11111111111111111111111111111111'
}
],
recentBlockhash: 'EZySCpmzXRuUtM95P2JGv9SitqYph6Nv6HaYBK7a8PKJ',
transaction:
'48ckoQL1HhH5aqU1ifKqpQkwq3WPDgMnsHHQkVfddisxYcapwAVXr8hejTi2jeJpMPkZMsF72SwmJFDByyfRtaknz4ytCYNAcdHrxtrHa9hTjMKckVQrFFqS8zG63Wj5mJ6wPfj8dv1wKu2XkU6GSXSGdQmuvfRv3K6LUSMbK5XSP3yBGb1SDZKCuoFX4qDKcKhCG7Awn3ssAWB1yRaXMd6mS6HQHKSF11FTp3jTH2HKUNbKyyuGh4tYtq8b',
pubkey: TestConstants.accounts[0].address
Expand Down Expand Up @@ -103,4 +143,24 @@ describe('WalletConnectProvider specific tests', () => {
}
})
})

it('should return the same transaction if the response comes with signature (legacy)', async () => {
await walletConnectProvider.connect()

const transaction = mockLegacyTransaction()
expect(transaction.signatures.length).toEqual(0)

vi.spyOn(provider, 'request').mockImplementationOnce(
<T>() =>
Promise.resolve({
signature:
'2Lb1KQHWfbV3pWMqXZveFWqneSyhH95YsgCENRWnArSkLydjN1M42oB82zSd6BBdGkM9pE6sQLQf1gyBh8KWM2c4'
}) as T
)

const result = await walletConnectProvider.signTransaction(transaction)

expect(result).toBe(transaction)
expect(result.signatures.length).toEqual(1)
})
})

0 comments on commit e768db8

Please sign in to comment.