diff --git a/encrypted/packetconn.go b/encrypted/packetconn.go index f9027fa..a8ff0f8 100644 --- a/encrypted/packetconn.go +++ b/encrypted/packetconn.go @@ -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") } diff --git a/network/packetconn.go b/network/packetconn.go index 0666d14..b9f1101 100644 --- a/network/packetconn.go +++ b/network/packetconn.go @@ -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") } diff --git a/types/addr.go b/types/addr.go index 6ebfd68..f27c92e 100644 --- a/types/addr.go +++ b/types/addr.go @@ -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