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

Fix order of signatures in PartialSign function #225

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 12 additions & 4 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,18 +536,26 @@ func (tx *Transaction) PartialSign(getter privateKeyGetter) (out []Signature, er
}
signerKeys := tx.Message.signerKeys()

signedSignatures := []Signature{}
for _, key := range signerKeys {
// Ensure that the transaction has the correct number of signatures initialized
if len(tx.Signatures) == 0 {
// Initialize the Signatures slice to the correct length if it's empty
tx.Signatures = make([]Signature, len(signerKeys))
} else if len(tx.Signatures) != len(signerKeys) {
// Return an error if the current length of the Signatures slice doesn't match the expected number
return nil, fmt.Errorf("invalid signatures length, expected %d, actual %d", len(signerKeys), len(tx.Signatures))
}

for i, key := range signerKeys {
privateKey := getter(key)
if privateKey != nil {
s, err := privateKey.Sign(messageContent)
if err != nil {
return nil, fmt.Errorf("failed to signed with key %q: %w", key.String(), err)
}
signedSignatures = append(signedSignatures, s)
// Directly assign the signature to the corresponding position in the transaction's signature slice
tx.Signatures[i] = s
}
}
tx.Signatures = append(tx.Signatures, signedSignatures...)
return tx.Signatures, nil
}

Expand Down
39 changes: 31 additions & 8 deletions transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ func TestPartialSignTransaction(t *testing.T) {
signers := []PrivateKey{
NewWallet().PrivateKey,
NewWallet().PrivateKey,
NewWallet().PrivateKey,
}
instructions := []Instruction{
&testTransactionInstructions{
accounts: []*AccountMeta{
{PublicKey: signers[0].PublicKey(), IsSigner: true, IsWritable: false},
{PublicKey: signers[1].PublicKey(), IsSigner: true, IsWritable: true},
{PublicKey: signers[2].PublicKey(), IsSigner: true, IsWritable: false},
},
data: []byte{0xaa, 0xbb},
programID: MustPublicKeyFromBase58("11111111111111111111111111111111"),
Expand All @@ -129,16 +131,37 @@ func TestPartialSignTransaction(t *testing.T) {
trx, err := NewTransaction(instructions, blockhash)
require.NoError(t, err)

assert.Equal(t, trx.Message.Header.NumRequiredSignatures, uint8(2))
assert.Equal(t, trx.Message.Header.NumRequiredSignatures, uint8(3))

// Test various signing orders
signingOrders := [][]int{
{0, 1, 2}, // ABC
{0, 2, 1}, // ACB
{1, 0, 2}, // BAC
{1, 2, 0}, // BCA
{2, 0, 1}, // CAB
{2, 1, 0}, // CBA
}

for _, order := range signingOrders {
// Reset the transaction signatures before each test
trx.Signatures = make([]Signature, len(signers))

signatures, err := trx.PartialSign(func(key PublicKey) *PrivateKey {
if key.Equals(signers[0].PublicKey()) {
return &signers[0]
// Sign the transaction in the specified order
for _, idx := range order {
signer := signers[idx]
signatures, err := trx.PartialSign(func(key PublicKey) *PrivateKey {
if key.Equals(signer.PublicKey()) {
return &signer
}
return nil
})
require.NoError(t, err)
assert.Equal(t, len(signatures), 3)
}
return nil
})
require.NoError(t, err)
assert.Equal(t, len(signatures), 1)
// Verify Signatures
require.NoError(t, trx.VerifySignatures())
}
}

func TestSignTransaction(t *testing.T) {
Expand Down
Loading