Skip to content

Commit

Permalink
chore: Adapt to app-channel in perun-examples
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrik Amler <[email protected]>
  • Loading branch information
tinnendo committed Sep 17, 2024
1 parent 3c47eab commit 17e6db9
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions channel/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,15 @@ func Verify(addr wallet.Address, s *channel.State, sig wallet.Sig) (bool, error)
func ToEthParams(p *channel.Params) adjudicator.ChannelParams {
var app common.Address
if p.App != nil && !channel.IsNoApp(p.App) {
appDef, ok := p.App.Def().(*AppID)
appDef, ok := p.App.Def().(channel.AppID)
ethAddress, err := ExtractEthereumAddress(appDef)
if err != nil {
log.Panicf("error extracting Ethereum address: %v", err)
}
if !ok {
panic("appDef is not of type *AppID")
panic("appDef is not of type channel.AppID")
}
app = ethwallet.AsEthAddr(appDef.Address)
app = ethAddress
}

return adjudicator.ChannelParams{
Expand All @@ -175,6 +179,23 @@ func ToEthParams(p *channel.Params) adjudicator.ChannelParams {
}
}

func ExtractEthereumAddress(appID channel.AppID) (common.Address, error) {

Check warning on line 182 in channel/backend.go

View workflow job for this annotation

GitHub Actions / Lint

exported: exported function ExtractEthereumAddress should have comment or be unexported (revive)
// Marshal the AppID into bytes
addressBytes, err := appID.MarshalBinary()
if err != nil {
return common.Address{}, errors.WithMessage(err, "failed to marshal AppID")
}

// Ensure the byte length is correct for an Ethereum address (20 bytes)
if len(addressBytes) != 20 {

Check failure on line 190 in channel/backend.go

View workflow job for this annotation

GitHub Actions / Lint

mnd: Magic number: 20, in <condition> detected (gomnd)
return common.Address{}, errors.WithMessagef(err, "invalid length for Ethereum address: %d", len(addressBytes))
}

// Convert the byte slice to an Ethereum address
ethAddress := common.BytesToAddress(addressBytes)
return ethAddress, nil
}

// ToEthState converts a channel.State to a ChannelState struct.
func ToEthState(s *channel.State) adjudicator.ChannelState {
locked := make([]adjudicator.ChannelSubAlloc, len(s.Locked))
Expand Down

0 comments on commit 17e6db9

Please sign in to comment.