Skip to content

Commit

Permalink
allows consumer to create customised types.Addr compatible types
Browse files Browse the repository at this point in the history
This is needed to make https://github.com/Jorropo/go-pin-argente work.

I have to return a precise [<ip>]:1 format in my net.Addr.String method (that because quic-go REALLY wants to do SNI and I cannot just give it the public key I want to use). (don't look at the :1 too much, it's just to make the SNI IP parser happy, I told it all connections happen on port 1 even tho no such thing exists)

However making my custom address type broke ironwood, this patch allows my custom address type to convert itself into ironwood friendly form.
  • Loading branch information
Jorropo committed Jun 25, 2022
1 parent 319147a commit 581f064
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
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

0 comments on commit 581f064

Please sign in to comment.