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

allows consumer to create customised types.Addr compatible types #9

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
2 changes: 1 addition & 1 deletion encrypted/packetconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (pc *PacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
return 0, errors.New("closed")
default:
}
destKey, ok := addr.(types.Addr)
destKey, ok := types.ExtractAddrKey(addr)
if !ok || len(destKey) != edPubSize {
return 0, errors.New("bad destination address")
}
Expand Down
4 changes: 2 additions & 2 deletions network/packetconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func (pc *PacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
return 0, errors.New("closed")
default:
}
if _, ok := addr.(types.Addr); !ok {
dest, ok := types.ExtractAddrKey(addr)
if !ok {
return 0, errors.New("incorrect address type, expected types.Addr")
}
dest := addr.(types.Addr)
if len(dest) != publicKeySize {
return 0, errors.New("incorrect address length")
}
Expand Down
20 changes: 20 additions & 0 deletions types/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,28 @@ package types
import (
"crypto/ed25519"
"encoding/hex"
"net"
)

// ConvertibleAddr is for apps that want to implement a custom address behaviour
// but want to tell ironwood which public key to contact.
type ConvertibleAddr interface {
IronwoodAddr() Addr
}

func ExtractAddrKey(a net.Addr) (addr Addr, ok bool) {
var destKey Addr
switch v := a.(type) {
case Addr:
destKey = v
case ConvertibleAddr:
destKey = v.IronwoodAddr()
default:
return nil, false
}
return destKey, true
}

// Addr implements the `net.Addr` interface for `ed25519.PublicKey` values.
type Addr ed25519.PublicKey

Expand Down