Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: add NewTransaction optional isSkipSign #181

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"encoding/binary"
"errors"
"fmt"

"github.com/blocto/solana-go-sdk/common"

"github.com/blocto/solana-go-sdk/pkg/bincode"
)

Expand All @@ -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{
Expand Down