From b78a9d37e691ed90c0ab6034db02473e9fc718e8 Mon Sep 17 00:00:00 2001 From: zi <549384715@qq.com> Date: Mon, 18 Nov 2024 16:18:51 +0800 Subject: [PATCH] perf: add NewTransaction optional isSkipSign --- types/transaction.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/types/transaction.go b/types/transaction.go index 1ee78e83..61b3959f 100644 --- a/types/transaction.go +++ b/types/transaction.go @@ -5,8 +5,8 @@ import ( "encoding/binary" "errors" "fmt" - "github.com/blocto/solana-go-sdk/common" + "github.com/blocto/solana-go-sdk/pkg/bincode" ) @@ -27,27 +27,29 @@ type NewTransactionParam struct { } // NewTransaction create a new tx by message and signer. it will reserve signatures slot. -func NewTransaction(param NewTransactionParam) (Transaction, error) { +func NewTransaction(param NewTransactionParam, isSkipSign ...bool) (Transaction, error) { signatures := make([]Signature, 0, param.Message.Header.NumRequireSignatures) for i := uint8(0); i < param.Message.Header.NumRequireSignatures; i++ { signatures = append(signatures, make([]byte, 64)) } - m := map[common.PublicKey]uint8{} - for i := uint8(0); i < param.Message.Header.NumRequireSignatures; i++ { - m[param.Message.Accounts[i]] = i - } + if len(isSkipSign) == 0 || !isSkipSign[0] { + m := map[common.PublicKey]uint8{} + for i := uint8(0); i < param.Message.Header.NumRequireSignatures; i++ { + m[param.Message.Accounts[i]] = i + } - data, err := param.Message.Serialize() - if err != nil { - return Transaction{}, fmt.Errorf("failed to serialize message, err: %v", err) - } - for _, signer := range param.Signers { - idx, ok := m[signer.PublicKey] - if !ok { - return Transaction{}, fmt.Errorf("%w, %v is not a signer", ErrTransactionAddNotNecessarySignatures, signer.PublicKey) + data, err := param.Message.Serialize() + if err != nil { + return Transaction{}, fmt.Errorf("failed to serialize message, err: %v", err) + } + for _, signer := range param.Signers { + idx, ok := m[signer.PublicKey] + if !ok { + return Transaction{}, fmt.Errorf("%w, %v is not a signer", ErrTransactionAddNotNecessarySignatures, signer.PublicKey) + } + signatures[idx] = signer.Sign(data) } - signatures[idx] = signer.Sign(data) } return Transaction{