From 99529e70c81d2b0806928698f45f03110f874040 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch Date: Tue, 21 Jan 2020 17:32:12 -0800 Subject: [PATCH] Fix bug in weight >= threshold check --- txnbuild/transaction.go | 6 +++--- txnbuild/transaction_test.go | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/txnbuild/transaction.go b/txnbuild/transaction.go index d9aff3f669..01c7a71384 100644 --- a/txnbuild/transaction.go +++ b/txnbuild/transaction.go @@ -516,12 +516,12 @@ func VerifyChallengeTxThreshold(challengeTx, serverAccountID, network string, th return nil, err } - weight := Threshold(0) + weight := int32(0) for _, s := range signersFound { - weight += Threshold(signerSummary[s]) + weight += signerSummary[s] } - if weight < threshold { + if weight < int32(threshold) { return nil, errors.Errorf("signers with weight %d do not meet threshold %d", weight, threshold) } diff --git a/txnbuild/transaction_test.go b/txnbuild/transaction_test.go index a790a837ec..457be94488 100644 --- a/txnbuild/transaction_test.go +++ b/txnbuild/transaction_test.go @@ -1780,6 +1780,44 @@ func TestVerifyChallengeTxThreshold_invalidNoSigners(t *testing.T) { assert.EqualError(t, err, "no signers provided") } +func TestVerifyChallengeTxThreshold_weightsAddToMoreThan8Bits(t *testing.T) { + serverKP := newKeypair0() + clientKP1 := newKeypair1() + clientKP2 := newKeypair2() + txSource := NewSimpleAccount(serverKP.Address(), -1) + opSource := NewSimpleAccount(clientKP1.Address(), 0) + op := ManageData{ + SourceAccount: &opSource, + Name: "testserver auth", + Value: []byte(base64.StdEncoding.EncodeToString(make([]byte, 48))), + } + tx := Transaction{ + SourceAccount: &txSource, + Operations: []Operation{&op}, + Timebounds: NewTimeout(1000), + Network: network.TestNetworkPassphrase, + } + threshold := Threshold(1) + signerSummary := SignerSummary{ + clientKP1.Address(): 255, + clientKP2.Address(): 1, + } + wantSigners := []string{ + clientKP1.Address(), + clientKP2.Address(), + } + + err := tx.Build() + require.NoError(t, err) + err = tx.Sign(serverKP, clientKP1, clientKP2) + assert.NoError(t, err) + tx64, err := tx.Base64() + require.NoError(t, err) + signersFound, err := VerifyChallengeTxThreshold(tx64, serverKP.Address(), network.TestNetworkPassphrase, threshold, signerSummary) + assert.Equal(t, wantSigners, signersFound) + assert.NoError(t, err) +} + func TestVerifyChallengeTxSigners_validServerAndClientMasterKey(t *testing.T) { serverKP := newKeypair0() clientKP := newKeypair1()