Skip to content

Commit

Permalink
Fix order of signatures in PartialSign function (#225)
Browse files Browse the repository at this point in the history
Co-authored-by: Vincent <VincentDebug@users.noreply.github.com>
  • Loading branch information
VincentDebug and VincentDebug authored Jun 26, 2024
1 parent 640ff56 commit 34ebd16
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
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

0 comments on commit 34ebd16

Please sign in to comment.