From 0fe261778b6fb9a78e8dd682a2508add5cf72e9b Mon Sep 17 00:00:00 2001 From: Jan Winkelmann Date: Thu, 15 Sep 2016 19:52:08 +0200 Subject: [PATCH 1/4] add shs/350 --- codec.go | 30 +++++++++++++++++++++++++++++- protocols.csv | 1 + protocols.go | 2 ++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/codec.go b/codec.go index 2068b84..f51fef1 100644 --- a/codec.go +++ b/codec.go @@ -9,6 +9,7 @@ import ( "strconv" "strings" + b58 "github.com/jbenet/go-base58" mh "github.com/jbenet/go-multihash" ) @@ -243,6 +244,16 @@ func addressStringToBytes(p Protocol, s string) ([]byte, error) { size := CodeToVarint(len(m)) b := append(size, m...) return b, nil + + case P_SHS: // shs + // the address is a varint prefixed multihash string representation + a := b58.Decode(s[:]) + if len(a) == 0 { + return nil, fmt.Errorf("failed to parse shs addr: %s", s) + } + size := CodeToVarint(len(a)) + b := append(size, a...) + return b, nil } return []byte{}, fmt.Errorf("failed to parse %s addr: unknown", p.Name) @@ -280,7 +291,24 @@ func addressBytesToString(p Protocol, b []byte) (string, error) { case P_ONION: addr := strings.ToLower(base32.StdEncoding.EncodeToString(b[0:10])) port := binary.BigEndian.Uint16(b[10:12]) - return addr + ":"+ strconv.Itoa(int(port)), nil + return addr + ":" + strconv.Itoa(int(port)), nil + + case P_SHS: // shs + // the address is a varint-prefixed multihash string representation + size, n, err := ReadVarintCode(b) + if err != nil { + return "", err + } + + b = b[n:] + if len(b) != size { + return "", fmt.Errorf("inconsistent lengths") + } + m := b58.Encode(b) + if len(m) == 0 { + return m, fmt.Errorf("could not decode address") + } + return m, nil default: return "", fmt.Errorf("unknown protocol") diff --git a/protocols.csv b/protocols.csv index 53b993c..d001870 100644 --- a/protocols.csv +++ b/protocols.csv @@ -7,6 +7,7 @@ code size name 132 16 sctp 301 0 utp 302 0 udt +350 V shs 421 V ipfs 480 0 http 443 0 https diff --git a/protocols.go b/protocols.go index 3fe0e58..4ee19ba 100644 --- a/protocols.go +++ b/protocols.go @@ -27,6 +27,7 @@ const ( P_SCTP = 132 P_UTP = 301 P_UDT = 302 + P_SHS = 350 P_IPFS = 421 P_HTTP = 480 P_HTTPS = 443 @@ -50,6 +51,7 @@ var Protocols = []Protocol{ Protocol{P_ONION, 96, "onion", CodeToVarint(P_ONION)}, Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)}, Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)}, + Protocol{P_SHS, LengthPrefixedVarSize, "shs", CodeToVarint(P_SHS)}, Protocol{P_HTTP, 0, "http", CodeToVarint(P_HTTP)}, Protocol{P_HTTPS, 0, "https", CodeToVarint(P_HTTPS)}, Protocol{P_IPFS, LengthPrefixedVarSize, "ipfs", CodeToVarint(P_IPFS)}, From 3182932cad5a8b5051c76732a41081ddc9b7fa77 Mon Sep 17 00:00:00 2001 From: Jan Winkelmann Date: Sat, 17 Sep 2016 17:56:12 +0200 Subject: [PATCH 2/4] add shs tests --- multiaddr_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/multiaddr_test.go b/multiaddr_test.go index 3d5a835..deb6373 100644 --- a/multiaddr_test.go +++ b/multiaddr_test.go @@ -41,6 +41,8 @@ func TestConstructFails(t *testing.T) { "/ip4/127.0.0.1/udp", "/ip4/127.0.0.1/tcp/jfodsajfidosajfoidsa", "/ip4/127.0.0.1/tcp", + "/ip4/127.0.0.1/shs", + "/ip4/127.0.0.1/tcp/8008/shs/7ZVaSM9sQ4mCCkn4MVIAhkcmSqF9V4u8p8r5hvxX4FnW", // contains illegal char 'I' "/ip4/127.0.0.1/ipfs", "/ip4/127.0.0.1/ipfs/tcp", } @@ -68,6 +70,7 @@ func TestConstructSucceeds(t *testing.T) { "/sctp/1234", "/udp/65535", "/tcp/65535", + "/shs/7ZVaSM9sQ4mCCkn4MV1AhkcmSqF9V4u8p8r5hvxX4FnW", "/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC", "/udp/1234/sctp/1234", "/udp/1234/udt", @@ -79,6 +82,7 @@ func TestConstructSucceeds(t *testing.T) { "/ip4/127.0.0.1/udp/0", "/ip4/127.0.0.1/tcp/1234", "/ip4/127.0.0.1/tcp/1234/", + "/ip4/127.0.0.1/tcp/8008/shs/7ZVaSM9sQ4mCCkn4MV1AhkcmSqF9V4u8p8r5hvxX4FnW", "/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC", "/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234", } @@ -353,6 +357,10 @@ func TestGetValue(t *testing.T) { assertValueForProto(t, a, P_IP4, "0.0.0.0") assertValueForProto(t, a, P_UDP, "12345") assertValueForProto(t, a, P_UTP, "") + + a = newMultiaddr(t, "/ip4/0.0.0.0/shs/7ZVaSM9sQ4mCCkn4MV1AhkcmSqF9V4u8p8r5hvxX4FnW") // test shs. + assertValueForProto(t, a, P_IP4, "0.0.0.0") + assertValueForProto(t, a, P_SHS, "7ZVaSM9sQ4mCCkn4MV1AhkcmSqF9V4u8p8r5hvxX4FnW") } func TestFuzzBytes(t *testing.T) { @@ -374,7 +382,7 @@ func TestFuzzBytes(t *testing.T) { } func randMaddrString() string { - good_corpus := []string{"tcp", "ip", "udp", "ipfs", "0.0.0.0", "127.0.0.1", "12345", "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP"} + good_corpus := []string{"tcp", "ip", "udp", "ipfs", "shs", "0.0.0.0", "127.0.0.1", "12345", "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP", ""} size := rand.Intn(256) parts := make([]string, 0, size) From 14b1c7b4c38cc80bb21756fd94a1a5be26346740 Mon Sep 17 00:00:00 2001 From: Jan Winkelmann Date: Mon, 19 Sep 2016 13:47:25 +0200 Subject: [PATCH 3/4] fix copypasta fail in test --- multiaddr_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/multiaddr_test.go b/multiaddr_test.go index deb6373..97ac31b 100644 --- a/multiaddr_test.go +++ b/multiaddr_test.go @@ -382,7 +382,7 @@ func TestFuzzBytes(t *testing.T) { } func randMaddrString() string { - good_corpus := []string{"tcp", "ip", "udp", "ipfs", "shs", "0.0.0.0", "127.0.0.1", "12345", "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP", ""} + good_corpus := []string{"tcp", "ip", "udp", "ipfs", "shs", "0.0.0.0", "127.0.0.1", "12345", "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP", "7ZVaSM9sQ4mCCkn4MV1AhkcmSqF9V4u8p8r5hvxX4FnW"} size := rand.Intn(256) parts := make([]string, 0, size) From 33bd2bce5627814565c4cd507aac74fc6905e554 Mon Sep 17 00:00:00 2001 From: Jan Winkelmann Date: Mon, 19 Sep 2016 13:47:42 +0200 Subject: [PATCH 4/4] make shs addresses fixed size 256 bit --- codec.go | 24 ++++++++++-------------- protocols.csv | 2 +- protocols.go | 2 +- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/codec.go b/codec.go index f51fef1..a3191fd 100644 --- a/codec.go +++ b/codec.go @@ -13,6 +13,9 @@ import ( mh "github.com/jbenet/go-multihash" ) +// Size of an ed25519 public key. Copied from github.com/agl/ed25519. +const ed25519PublicKeySize = 32 + func stringToBytes(s string) ([]byte, error) { // consume trailing slashes @@ -247,13 +250,12 @@ func addressStringToBytes(p Protocol, s string) ([]byte, error) { case P_SHS: // shs // the address is a varint prefixed multihash string representation - a := b58.Decode(s[:]) - if len(a) == 0 { - return nil, fmt.Errorf("failed to parse shs addr: %s", s) + a := b58.Decode(s) + if len(a) != ed25519PublicKeySize { + return nil, fmt.Errorf("failed to parse %s addr %s", p.Name, s) } - size := CodeToVarint(len(a)) - b := append(size, a...) - return b, nil + + return a, nil } return []byte{}, fmt.Errorf("failed to parse %s addr: unknown", p.Name) @@ -294,16 +296,10 @@ func addressBytesToString(p Protocol, b []byte) (string, error) { return addr + ":" + strconv.Itoa(int(port)), nil case P_SHS: // shs - // the address is a varint-prefixed multihash string representation - size, n, err := ReadVarintCode(b) - if err != nil { - return "", err + if len(b) != ed25519PublicKeySize { + return "", fmt.Errorf("shs address has wrong length") } - b = b[n:] - if len(b) != size { - return "", fmt.Errorf("inconsistent lengths") - } m := b58.Encode(b) if len(m) == 0 { return m, fmt.Errorf("could not decode address") diff --git a/protocols.csv b/protocols.csv index d001870..5ce9cbf 100644 --- a/protocols.csv +++ b/protocols.csv @@ -7,7 +7,7 @@ code size name 132 16 sctp 301 0 utp 302 0 udt -350 V shs +350 256 shs 421 V ipfs 480 0 http 443 0 https diff --git a/protocols.go b/protocols.go index 4ee19ba..46514dc 100644 --- a/protocols.go +++ b/protocols.go @@ -51,7 +51,7 @@ var Protocols = []Protocol{ Protocol{P_ONION, 96, "onion", CodeToVarint(P_ONION)}, Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP)}, Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT)}, - Protocol{P_SHS, LengthPrefixedVarSize, "shs", CodeToVarint(P_SHS)}, + Protocol{P_SHS, 256, "shs", CodeToVarint(P_SHS)}, Protocol{P_HTTP, 0, "http", CodeToVarint(P_HTTP)}, Protocol{P_HTTPS, 0, "https", CodeToVarint(P_HTTPS)}, Protocol{P_IPFS, LengthPrefixedVarSize, "ipfs", CodeToVarint(P_IPFS)},