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.
This commit is contained in:
Ben Kremer 2022-03-17 14:21:13 +01:00
parent 9f7e78969c
commit 112465b5c7

View File

@ -1,6 +1,7 @@
import { Keypair, PublicKey, Transaction, TransactionInstructionCtorFields } from '@solana/web3.js' import { Keypair } from '@solana/web3.js'
import bs58 from 'bs58' import bs58 from 'bs58'
import nacl from 'tweetnacl' import nacl from 'tweetnacl'
import SolanaWallet, { SolanaSignTransaction } from 'solana-wallet'
/** /**
* Types * Types
@ -14,9 +15,11 @@ interface IInitArguments {
*/ */
export default class SolanaLib { export default class SolanaLib {
keypair: Keypair keypair: Keypair
solanaWallet: SolanaWallet
constructor(keypair: Keypair) { constructor(keypair: Keypair) {
this.keypair = keypair this.keypair = keypair
this.solanaWallet = new SolanaWallet(Buffer.from(keypair.secretKey))
} }
static init({ secretKey }: IInitArguments) { static init({ secretKey }: IInitArguments) {
@ -41,34 +44,18 @@ export default class SolanaLib {
} }
public async signTransaction( public async signTransaction(
feePayer: string, feePayer: SolanaSignTransaction['feePayer'],
recentBlockhash: string, recentBlockhash: SolanaSignTransaction['recentBlockhash'],
instructions: TransactionInstructionCtorFields[] instructions: SolanaSignTransaction['instructions'],
partialSignatures?: SolanaSignTransaction['partialSignatures']
) { ) {
const tx = new Transaction({ const { signature } = await this.solanaWallet.signTransaction(feePayer, {
feePayer: new PublicKey(feePayer), feePayer,
recentBlockhash instructions,
recentBlockhash,
partialSignatures: partialSignatures ?? []
}) })
tx.add( return { signature }
...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 }
} }
} }