Skip to content

Commit 81b5fc8

Browse files
committed
fix(wire): move wire_test to package wire to prevent init dependency
Signed-off-by: Minh Huy Tran <[email protected]>
1 parent 03ce1db commit 81b5fc8

File tree

4 files changed

+19
-50
lines changed

4 files changed

+19
-50
lines changed

Diff for: wire/account.go

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package wire
1616

1717
import (
1818
"crypto/ecdsa"
19+
"log"
1920
"math/rand"
2021

2122
"github.com/ethereum/go-ethereum/crypto"
@@ -62,6 +63,7 @@ func NewRandomAccount(rng *rand.Rand) *Account {
6263
if err != nil {
6364
panic(err)
6465
}
66+
log.Print("Generated new account with address ", crypto.PubkeyToAddress(privateKey.PublicKey).Hex())
6567

6668
addr := crypto.PubkeyToAddress(privateKey.PublicKey)
6769

Diff for: wire/address.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package wire
1616

1717
import (
18+
"log"
1819
"math/rand"
1920

2021
"github.com/ethereum/go-ethereum/crypto"
@@ -36,7 +37,7 @@ func NewAddress() *Address {
3637
}
3738

3839
// Equal returns whether the two addresses are equal.
39-
func (a Address) Equal(b wire.Address) bool {
40+
func (a *Address) Equal(b wire.Address) bool {
4041
bTyped, ok := b.(*Address)
4142
if !ok {
4243
panic("wrong type")
@@ -47,7 +48,7 @@ func (a Address) Equal(b wire.Address) bool {
4748

4849
// Cmp compares the byte representation of two addresses. For `a.Cmp(b)`
4950
// returns -1 if a < b, 0 if a == b, 1 if a > b.
50-
func (a Address) Cmp(b wire.Address) int {
51+
func (a *Address) Cmp(b wire.Address) int {
5152
bTyped, ok := b.(*Address)
5253
if !ok {
5354
panic("wrong type")
@@ -63,7 +64,8 @@ func NewRandomAddress(rng *rand.Rand) *Address {
6364

6465
// Verify verifies a message signature.
6566
// It returns an error if the signature is invalid.
66-
func (a Address) Verify(msg []byte, sig []byte) error {
67+
func (a *Address) Verify(msg []byte, sig []byte) error {
68+
log.Print("Address.Verify called")
6769
hash := PrefixedHash(msg)
6870
sigCopy := make([]byte, SigLen)
6971
copy(sigCopy, sig)

Diff for: wire/init_test.go

-37
This file was deleted.

Diff for: wire/wire_test.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"math/rand"
1919
"testing"
2020

21-
"github.com/perun-network/perun-eth-backend/wire"
21+
ethwire "github.com/perun-network/perun-eth-backend/wire"
2222
"github.com/stretchr/testify/assert"
2323
perunwire "perun.network/go-perun/wire"
2424
"perun.network/go-perun/wire/test"
@@ -29,24 +29,24 @@ var dataToSign = []byte("SomeLongDataThatShouldBeSignedPlease")
2929

3030
func TestAddress(t *testing.T) {
3131
test.TestAddressImplementation(t, func() perunwire.Address {
32-
return wire.NewAddress()
32+
return ethwire.NewAddress()
3333
}, func(rng *rand.Rand) perunwire.Address {
34-
return wire.NewRandomAddress(rng)
34+
return ethwire.NewRandomAddress(rng)
3535
})
3636
}
3737

3838
func TestSignatures_Success(t *testing.T) {
39-
acc := wire.NewRandomAccount(pkgtest.Prng(t))
39+
acc := ethwire.NewRandomAccount(pkgtest.Prng(t))
4040
sig, err := acc.Sign(dataToSign)
4141
assert.NoError(t, err, "Sign with new account should succeed")
4242
assert.NotNil(t, sig)
43-
assert.Equal(t, len(sig), wire.SigLen, "Ethereum signature has wrong length")
43+
assert.Equal(t, len(sig), ethwire.SigLen, "Ethereum signature has wrong length")
4444
err = acc.Address().Verify(dataToSign, sig)
4545
assert.NoError(t, err, "Verification should succeed")
4646
}
4747

4848
func TestSignatures_ModifyData_Failure(t *testing.T) {
49-
acc := wire.NewRandomAccount(pkgtest.Prng(t))
49+
acc := ethwire.NewRandomAccount(pkgtest.Prng(t))
5050
sig, err := acc.Sign(dataToSign)
5151
assert.NoError(t, err, "Sign with new account should succeed")
5252
assert.NotNil(t, sig)
@@ -61,7 +61,7 @@ func TestSignatures_ModifyData_Failure(t *testing.T) {
6161
}
6262

6363
func TestSignatures_ModifySignature_Failure(t *testing.T) {
64-
acc := wire.NewRandomAccount(pkgtest.Prng(t))
64+
acc := ethwire.NewRandomAccount(pkgtest.Prng(t))
6565
sig, err := acc.Sign(dataToSign)
6666
assert.NoError(t, err, "Sign with new account should succeed")
6767
assert.NotNil(t, sig)
@@ -76,7 +76,7 @@ func TestSignatures_ModifySignature_Failure(t *testing.T) {
7676
}
7777

7878
func TestSignatures_ModifyLastByteOfSignature_Failure(t *testing.T) {
79-
acc := wire.NewRandomAccount(pkgtest.Prng(t))
79+
acc := ethwire.NewRandomAccount(pkgtest.Prng(t))
8080
sig, err := acc.Sign(dataToSign)
8181
assert.NoError(t, err, "Sign with new account should succeed")
8282
assert.NotNil(t, sig)
@@ -91,13 +91,15 @@ func TestSignatures_ModifyLastByteOfSignature_Failure(t *testing.T) {
9191
}
9292

9393
func TestSignatures_WrongAccount_Failure(t *testing.T) {
94-
acc := wire.NewRandomAccount(pkgtest.Prng(t))
94+
accPrng := pkgtest.Prng(t)
95+
acc := ethwire.NewRandomAccount(accPrng)
9596
sig, err := acc.Sign(dataToSign)
9697
assert.NoError(t, err, "Sign with new account should succeed")
9798
assert.NotNil(t, sig)
9899

99100
// Verify with a wrong account
100-
wrongAcc := wire.NewRandomAccount(pkgtest.Prng(t))
101+
wrongAcc := ethwire.NewRandomAccount(accPrng)
102+
assert.False(t, acc.Address().Equal(wrongAcc.Address()), "Accounts should be different")
101103
err = wrongAcc.Address().Verify(dataToSign, sig)
102104
assert.Error(t, err, "Verification should fail with wrong account")
103105
}

0 commit comments

Comments
 (0)