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 app-channel in perun-examples #53

Merged
Merged
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
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
Loading