From 2d7c474795850506211146e6154e91ae14accc87 Mon Sep 17 00:00:00 2001 From: gagliardetto Date: Sat, 27 Apr 2024 16:35:55 +0200 Subject: [PATCH] Add GetProgramIDs --- keys.go | 4 ++-- transaction.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/keys.go b/keys.go index b9b9c1dd..904e9757 100644 --- a/keys.go +++ b/keys.go @@ -567,7 +567,7 @@ const ( // Ported from https://github.com/solana-labs/solana/blob/216983c50e0a618facc39aa07472ba6d23f1b33a/sdk/program/src/pubkey.rs#L159 func CreateWithSeed(base PublicKey, seed string, owner PublicKey) (PublicKey, error) { if len(seed) > MaxSeedLength { - return PublicKey{}, errors.New("Max seed length exceeded") + return PublicKey{}, ErrMaxSeedLengthExceeded } // let owner = owner.as_ref(); @@ -588,7 +588,7 @@ func CreateWithSeed(base PublicKey, seed string, owner PublicKey) (PublicKey, er const PDA_MARKER = "ProgramDerivedAddress" -var ErrMaxSeedLengthExceeded = errors.New("Max seed length exceeded") +var ErrMaxSeedLengthExceeded = errors.New("max seed length exceeded") // Create a program address. // Ported from https://github.com/solana-labs/solana/blob/216983c50e0a618facc39aa07472ba6d23f1b33a/sdk/program/src/pubkey.rs#L204 diff --git a/transaction.go b/transaction.go index 273cfca3..2fdbf9ba 100644 --- a/transaction.go +++ b/transaction.go @@ -699,6 +699,34 @@ func (tx *Transaction) VerifySignatures() error { return nil } +// GetProgramIDs returns the program IDs of the instructions in the transaction. +// NOTE: this does NOT include the program IDs of the internal instructions. +func (tx *Transaction) GetProgramIDs() PublicKeySlice { + programIDs := make(PublicKeySlice, 0) + for ixi, inst := range tx.Message.Instructions { + progKey, err := tx.ResolveProgramIDIndex(inst.ProgramIDIndex) + if err == nil { + programIDs = append(programIDs, progKey) + } else { + panic(fmt.Errorf("cannot resolve program ID for instruction %d: %w", ixi, err)) + } + } + return programIDs +} + +func (tx *Transaction) GetProgramIDsWithError() (PublicKeySlice, error) { + programIDs := make(PublicKeySlice, 0) + for ixi, inst := range tx.Message.Instructions { + progKey, err := tx.ResolveProgramIDIndex(inst.ProgramIDIndex) + if err == nil { + programIDs = append(programIDs, progKey) + } else { + return nil, fmt.Errorf("cannot resolve program ID for instruction %d: %w", ixi, err) + } + } + return programIDs, nil +} + func (tx *Transaction) NumWriteableAccounts() int { return countWriteableAccounts(tx) }