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 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 }
}
}