Skip to content

Commit

Permalink
Merge pull request #53 from perun-network/chore-update-to-goperun-0.11.0
Browse files Browse the repository at this point in the history
Adapt to app-channel in perun-examples
  • Loading branch information
tinnendo authored Sep 18, 2024
2 parents 3c47eab + ada4367 commit 9461f0d
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions channel/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
phaseDispute = iota
phaseForceExec
phaseConcluded
ethereumAddressLength = 20
)

var (
Expand Down Expand Up @@ -158,11 +159,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 +180,24 @@ func ToEthParams(p *channel.Params) adjudicator.ChannelParams {
}
}

// ExtractEthereumAddress extracts an Ethereum address from the given channel.AppID.
func ExtractEthereumAddress(appID channel.AppID) (common.Address, error) {
// 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) != ethereumAddressLength {
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 9461f0d

Please sign in to comment.