Skip to content

Commit

Permalink
fix(wire): move wire_test to package wire to prevent init dependency
Browse files Browse the repository at this point in the history
Signed-off-by: Minh Huy Tran <[email protected]>
  • Loading branch information
NhoxxKienn committed Jul 2, 2024
1 parent 03ce1db commit e360848
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 50 deletions.
2 changes: 2 additions & 0 deletions wire/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package wire

import (
"crypto/ecdsa"
"log"
"math/rand"

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

addr := crypto.PubkeyToAddress(privateKey.PublicKey)

Expand Down
8 changes: 5 additions & 3 deletions wire/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package wire

import (
"log"
"math/rand"

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

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

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

// Verify verifies a message signature.
// It returns an error if the signature is invalid.
func (a Address) Verify(msg []byte, sig []byte) error {
func (a *Address) Verify(msg []byte, sig []byte) error {
log.Print("Address.Verify called")
hash := PrefixedHash(msg)
sigCopy := make([]byte, SigLen)
copy(sigCopy, sig)
Expand Down
37 changes: 0 additions & 37 deletions wire/init_test.go

This file was deleted.

22 changes: 12 additions & 10 deletions wire/wire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"math/rand"
"testing"

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

func TestAddress(t *testing.T) {
test.TestAddressImplementation(t, func() perunwire.Address {
return wire.NewAddress()
return ethwire.NewAddress()
}, func(rng *rand.Rand) perunwire.Address {
return wire.NewRandomAddress(rng)
return ethwire.NewRandomAddress(rng)
})
}

func TestSignatures_Success(t *testing.T) {
acc := wire.NewRandomAccount(pkgtest.Prng(t))
acc := ethwire.NewRandomAccount(pkgtest.Prng(t))
sig, err := acc.Sign(dataToSign)
assert.NoError(t, err, "Sign with new account should succeed")
assert.NotNil(t, sig)
assert.Equal(t, len(sig), wire.SigLen, "Ethereum signature has wrong length")
assert.Equal(t, len(sig), ethwire.SigLen, "Ethereum signature has wrong length")
err = acc.Address().Verify(dataToSign, sig)
assert.NoError(t, err, "Verification should succeed")
}

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

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

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

func TestSignatures_WrongAccount_Failure(t *testing.T) {
acc := wire.NewRandomAccount(pkgtest.Prng(t))
accPrng := pkgtest.Prng(t)
acc := ethwire.NewRandomAccount(accPrng)
sig, err := acc.Sign(dataToSign)
assert.NoError(t, err, "Sign with new account should succeed")
assert.NotNil(t, sig)

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

0 comments on commit e360848

Please sign in to comment.