Skip to content

Commit

Permalink
channel: Implement channel.Backend interface
Browse files Browse the repository at this point in the history
  • Loading branch information
janbormet committed Aug 8, 2023
1 parent 88640af commit 83093e6
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions channel/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package channel

import (
"crypto/sha256"
"perun.network/go-perun/channel"
"perun.network/go-perun/wallet"
"perun.network/perun-stellar-backend/channel/types"
"perun.network/perun-stellar-backend/wire"
)

type backend struct{}

var Backend = backend{}

func init() {
channel.SetBackend(Backend)
}

func (b backend) CalcID(params *channel.Params) channel.ID {
wp := wire.MustMakeParams(*params)
bytes, err := wp.MarshalBinary()
if err != nil {
panic(err)
}
return sha256.Sum256(bytes)
}

func (b backend) Sign(account wallet.Account, state *channel.State) (wallet.Sig, error) {
bytes, err := EncodeState(state)
if err != nil {
return nil, err
}
return account.SignData(bytes)
}

func (b backend) Verify(addr wallet.Address, state *channel.State, sig wallet.Sig) (bool, error) {
bytes, err := EncodeState(state)
if err != nil {
return false, err
}
return wallet.VerifySignature(bytes, sig, addr)
}

func (b backend) NewAsset() channel.Asset {
return &types.StellarAsset{}
}

func EncodeState(state *channel.State) ([]byte, error) {
ws, err := wire.MakeState(*state)
if err != nil {
return nil, err
}
return ws.MarshalBinary()
}

0 comments on commit 83093e6

Please sign in to comment.