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

Adapt to go-perun v0.11.0 #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
96 changes: 96 additions & 0 deletions channel/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2024 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package channel

import (
"math/rand"

dotwallet "github.com/perun-network/perun-polkadot-backend/wallet/sr25519"
dottestwallet "github.com/perun-network/perun-polkadot-backend/wallet/sr25519/test"
"perun.network/go-perun/channel"
)

var _ channel.AppID = new(AppID)

// AppID is an app identifier and
// OffIdentity is the type of the app identifier (serialized address).
type AppID struct {
OffIdentity
}

// AppIDKey is the key representation of an app identifier.
type AppIDKey string

// Equal compares two AppID objects for equality.
func (a AppID) Equal(b channel.AppID) bool {
bTyped, ok := b.(*AppID)
if !ok {
return false
}
return a.OffIdentity == bTyped.OffIdentity

}

// Key returns the key representation of this app identifier.
func (a AppID) Key() channel.AppIDKey {
b, err := a.MarshalBinary()
if err != nil {
panic(err)
}
return channel.AppIDKey(b)
}

// MarshalBinary marshals the contents of AppID into a byte string.
func (a AppID) MarshalBinary() ([]byte, error) {
data := a.OffIdentity
return data[:], nil
// return data, nil
}

// UnmarshalBinary converts a bytestring, representing AppID into the AppID struct.
func (a *AppID) UnmarshalBinary(data []byte) error {
addr := &dotwallet.Address{}
err := addr.UnmarshalBinary(data)
if err != nil {
return err
}

var appIdent [OffIdentityLen]byte
copy(appIdent[:], data)

appaddr := &AppID{appIdent}
*a = *appaddr
return nil
}

func NewRandomAppID(rng *rand.Rand) *AppID {
addr := dottestwallet.NewRandomAddress(rng)

// Assuming addr is of type wallet.Address, and we need sr25519.Address
// Convert or cast the address to sr25519.Address if possible
srAddr, ok := addr.(*dotwallet.Address)
if !ok {
panic("address is not of type *sr25519.Address")
}
appIdent, err := srAddr.MarshalBinary()
if err != nil {
panic(err)
}

var offIdentity OffIdentity
copy(offIdentity[:], appIdent)

return &AppID{offIdentity}
}
19 changes: 16 additions & 3 deletions channel/backend.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 PolyCrypt GmbH
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -16,9 +16,9 @@ package channel

import (
"fmt"
"log"

eth "github.com/ethereum/go-ethereum/crypto"
dotwallet "github.com/perun-network/perun-polkadot-backend/wallet/sr25519"
"log"
pchannel "perun.network/go-perun/channel"
pwallet "perun.network/go-perun/wallet"
)
Expand Down Expand Up @@ -80,3 +80,16 @@ func CalcID(params *pchannel.Params) (id pchannel.ID) {
}
return eth.Keccak256Hash(bytes)
}

// NewAppID creates a new app identifier
func (b *backend) NewAppID() pchannel.AppID {
addr := &dotwallet.Address{}
appIdent, err := addr.MarshalBinary()
if err != nil {
panic(err)
}

var offIdentity OffIdentity
copy(offIdentity[:], appIdent)
return &AppID{offIdentity}
}
4 changes: 1 addition & 3 deletions channel/channel.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 PolyCrypt GmbH
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -63,8 +63,6 @@ type (
Balance = types.U128
// Sig is an off-chain signature.
Sig = [SigLen]byte
// AppID is the identifier of a channel application.
AppID = OffIdentity

// Params holds the fixed parameters of a channel and uniquely identifies it.
Params struct {
Expand Down
17 changes: 11 additions & 6 deletions channel/encoding.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 PolyCrypt GmbH
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -195,19 +195,24 @@ func NewParams(p *pchannel.Params) (*Params, error) {
return nil, err
}

var appID OffIdentity
var appDef AppID
if !pchannel.IsNoApp(p.App) {
appID, err = MakeOffIdent(p.App.Def())
if err != nil {
return nil, err
var ok bool
appDefPtr, ok := p.App.Def().(*AppID)
if !ok {
panic("appDef is not of type *AppID")
}
appDef = *appDefPtr

} else {
appDef = AppID{OffIdentity: [32]byte{}}
}

return &Params{
Nonce: nonce,
Participants: parts,
ChallengeDuration: MakeChallengeDuration(p.ChallengeDuration),
App: appID,
App: appDef,
}, err
}

Expand Down
12 changes: 9 additions & 3 deletions channel/pallet/adjudicator_sub.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 PolyCrypt GmbH
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -133,12 +133,18 @@ func (s *AdjudicatorSub) makePerunEvent(event channel.PerunEvent) (pchannel.Adju
return nil, err
}

appPK, err := pkg_sr25519.NewPK(event.App[:])
appPK, err := pkg_sr25519.NewPK(event.App.OffIdentity[:])
if err != nil {
return nil, err
}
appAddr := sr25519.NewAddressFromPK(appPK)
app, err := pchannel.Resolve(appAddr)
appIdent, err := appAddr.MarshalBinary()
if err != nil {
return nil, err
}
var offIdentity channel.OffIdentity
copy(offIdentity[:], appIdent)
app, err := pchannel.Resolve(&channel.AppID{OffIdentity: offIdentity})
if err != nil {
return nil, err
}
Expand Down
11 changes: 9 additions & 2 deletions client/appchannel_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 - See NOTICE file for copyright holders.
// Copyright 2024 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,7 +55,14 @@ func TestAppChannel(t *testing.T) {

appAddress := dotwallet.NewAddressFromPK(pk)
require.NoError(t, err)
app := channel.NewMockApp(appAddress)

appIdent, err := appAddress.MarshalBinary()
require.NoError(t, err)

var offIdentity pchannel.OffIdentity
copy(offIdentity[:], appIdent)

app := channel.NewMockApp(&pchannel.AppID{OffIdentity: offIdentity})
channel.RegisterApp(app)

execConfig := &clienttest.ProgressionExecConfig{
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.2
github.com/vedhavyas/go-subkey v1.0.2
perun.network/go-perun v0.10.6
perun.network/go-perun v0.11.0
polycry.pt/poly-go v0.0.0-20220301085937-fb9d71b45a37
)

Expand All @@ -24,6 +24,7 @@ require (
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa h1:Q75Upo5UN4JbPFURXZ8nLKYUvF85dyFRop/vQ0Rv+64=
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.1-0.20190629185528-ae1634f6a989/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
Expand Down Expand Up @@ -308,7 +310,7 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
perun.network/go-perun v0.10.6 h1:uj1e33yfCSfE75DK/uwjNp+TwvGG85Qhi6HuYQ9EPrQ=
perun.network/go-perun v0.10.6/go.mod h1:BGBZC3npkX457u87pjDd0NEIXr1a4dsH4H/YpLdGGe8=
perun.network/go-perun v0.11.0 h1:25aL0MsyXQ2rHziOnMwJMe70K6NTCbopZMwX67qxt/k=
perun.network/go-perun v0.11.0/go.mod h1:pY/1pJ2OMlCQgEbnfGh9wVfRMJtqN0iAKsiJBLH0/Gc=
polycry.pt/poly-go v0.0.0-20220301085937-fb9d71b45a37 h1:iA5GzEa/hHfVlQpimEjPV09NATwHXxSjWNB0VVodtew=
polycry.pt/poly-go v0.0.0-20220301085937-fb9d71b45a37/go.mod h1:XUBrNtqgEhN3EEOP/5gh7IBd3xVHKidCjXDZfl9+kMU=
9 changes: 8 additions & 1 deletion wallet/sr25519/test/wallet.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 PolyCrypt GmbH
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,6 +49,13 @@ func (w *Wallet) NewRandomAccount(rng *rand.Rand) pwallet.Account {
func NewAddressZero() *sr25519.Address {
return sr25519.NewAddressFromPK(ZeroPK())
}
func NewRandomAddress(rng *rand.Rand) pwallet.Address {
pk, err := pkgsr25519.NewPKFromRng(rng)
if err != nil {
panic(err)
}
return sr25519.NewAddressFromPK(pk)
}

// ZeroPK returns a PK that can be used to create a zero address.
func ZeroPK() *schnorrkel.PublicKey {
Expand Down
Loading