From 112465b5c75fd3e61a62ec37eda67f93eaf8e552 Mon Sep 17 00:00:00 2001 From: Ben Kremer Date: Thu, 17 Mar 2022 14:21:13 +0100 Subject: [PATCH] fix(wallet-v2): fixes signing for `solana_signTransaction` The existing implementation in the wallet was returning signatures that came back `false` when calling `transaction.validateSignatures` on them. This issue doesn't seem to arise when using the `solana-wallet` package to sign the transaction. --- wallets/react-wallet-v2/src/lib/SolanaLib.ts | 41 +++++++------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/wallets/react-wallet-v2/src/lib/SolanaLib.ts b/wallets/react-wallet-v2/src/lib/SolanaLib.ts index 22273c0..d06ff5a 100644 --- a/wallets/react-wallet-v2/src/lib/SolanaLib.ts +++ b/wallets/react-wallet-v2/src/lib/SolanaLib.ts @@ -1,6 +1,7 @@ -import { Keypair, PublicKey, Transaction, TransactionInstructionCtorFields } from '@solana/web3.js' +import { Keypair } from '@solana/web3.js' import bs58 from 'bs58' import nacl from 'tweetnacl' +import SolanaWallet, { SolanaSignTransaction } from 'solana-wallet' /** * Types @@ -14,9 +15,11 @@ interface IInitArguments { */ export default class SolanaLib { keypair: Keypair + solanaWallet: SolanaWallet constructor(keypair: Keypair) { this.keypair = keypair + this.solanaWallet = new SolanaWallet(Buffer.from(keypair.secretKey)) } static init({ secretKey }: IInitArguments) { @@ -41,34 +44,18 @@ export default class SolanaLib { } public async signTransaction( - feePayer: string, - recentBlockhash: string, - instructions: TransactionInstructionCtorFields[] + feePayer: SolanaSignTransaction['feePayer'], + recentBlockhash: SolanaSignTransaction['recentBlockhash'], + instructions: SolanaSignTransaction['instructions'], + partialSignatures?: SolanaSignTransaction['partialSignatures'] ) { - const tx = new Transaction({ - feePayer: new PublicKey(feePayer), - recentBlockhash + const { signature } = await this.solanaWallet.signTransaction(feePayer, { + feePayer, + instructions, + recentBlockhash, + partialSignatures: partialSignatures ?? [] }) - tx.add( - ...instructions.map(i => ({ - programId: new PublicKey(i.programId), - data: i.data ? Buffer.from(i.data) : Buffer.from([]), - keys: i.keys.map(k => ({ - ...k, - pubkey: new PublicKey(k.pubkey) - })) - })) - ) - - await tx.sign(this.keypair) - - if (!tx.signature) { - throw new Error('Missing signature!') - } - - const bs58Signature = bs58.encode(tx.signature) - - return { signature: bs58Signature } + return { signature } } }