tpuproxy/pkg/tpu/tpu.go

54 lines
993 B
Go
Raw Normal View History

2021-12-16 07:01:47 -08:00
package tpu
import (
"crypto/ed25519"
"errors"
"github.com/gagliardetto/binary"
"github.com/gagliardetto/solana-go"
)
func ParseTx(p []byte) (tx *solana.Transaction, err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New("ParseTx panic")
}
}()
tx, err = solana.TransactionFromDecoder(bin.NewBinDecoder(p))
if err != nil {
return nil, err
}
return
}
func VerifyTxSig(tx *solana.Transaction) (ok bool) {
msg, err := tx.Message.MarshalBinary()
if err != nil {
panic(err)
}
signers := ExtractSigners(tx)
if len(signers) != len(tx.Signatures) {
return false
}
for i, sig := range tx.Signatures {
if !ed25519.Verify(signers[i][:], msg, sig[:]) {
return false
}
}
return true
}
func ExtractSigners(tx *solana.Transaction) []solana.PublicKey {
signers := make([]solana.PublicKey, 0, len(tx.Signatures))
for _, acc := range tx.Message.AccountKeys {
if tx.IsSigner(acc) {
signers = append(signers, acc)
}
}
return signers
}