From 2159c377eb4ce8e43e09c9a172c8622173302111 Mon Sep 17 00:00:00 2001 From: Cole Brown Date: Fri, 31 May 2019 11:22:49 -0400 Subject: [PATCH 1/2] Implement memory multiaddrs --- multiaddr_test.go | 2 ++ protocols.go | 10 ++++++++++ transcoders.go | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/multiaddr_test.go b/multiaddr_test.go index 37e825c..2d3b73c 100644 --- a/multiaddr_test.go +++ b/multiaddr_test.go @@ -85,6 +85,7 @@ func TestConstructFails(t *testing.T) { "/ip4/1.2.3.4/tcp/80/unix", "/ip4/1.2.3.4/tcp/-1", "/ip4/127.0.0.1/tcp/9090/http/p2p-webcrt-direct", + fmt.Sprintf("/memory/%d1", uint64(1<<63)), "/", "", "/p2p/QmxoHT6iViN5xAjoz1VZ553cL31U9F94ht3QvWR1FrEbZY", // sha256 multihash with digest len > 32 @@ -197,6 +198,7 @@ var good = []string{ "/http-path/foo", "/ip4/127.0.0.1/tcp/0/p2p/12D3KooWCryG7Mon9orvQxcS1rYZjotPgpwoJNHHKcLLfE4Hf5mV/http-path/foo", "/ip4/127.0.0.1/tcp/443/tls/sni/example.com/http/http-path/foo", + "/memory/4", } func TestConstructSucceeds(t *testing.T) { diff --git a/protocols.go b/protocols.go index d3117b2..985a9cd 100644 --- a/protocols.go +++ b/protocols.go @@ -41,6 +41,7 @@ const ( P_PLAINTEXTV2 = 7367777 P_WEBRTC_DIRECT = 280 P_WEBRTC = 281 + P_MEMORY = 0x0309 // 777 decimal ) var ( @@ -281,6 +282,14 @@ var ( Code: P_WEBRTC, VCode: CodeToVarint(P_WEBRTC), } + + protoMemory = Protocol{ + Name: "memory", + Code: P_MEMORY, + VCode: CodeToVarint(P_MEMORY), + Size: 64, + Transcoder: TranscoderMemory, + } ) func init() { @@ -322,6 +331,7 @@ func init() { protoPlaintextV2, protoWebRTCDirect, protoWebRTC, + protoMemory, } { if err := AddProtocol(p); err != nil { panic(err) diff --git a/transcoders.go b/transcoders.go index 9a36fc4..a6bde0d 100644 --- a/transcoders.go +++ b/transcoders.go @@ -489,3 +489,23 @@ func validateHTTPPath(b []byte) error { } return nil // We can represent any byte slice when we escape it. } + +var TranscoderMemory = NewTranscoderFromFunctions(memoryStB, memoryBtS, nil) + +func memoryStB(s string) ([]byte, error) { + z, err := strconv.ParseUint(s, 10, 64) + if err != nil { + return nil, err + } + buf := make([]byte, 8) + binary.BigEndian.PutUint64(buf, z) + return buf, nil +} + +func memoryBtS(b []byte) (string, error) { + if len(b) != 8 { + return "", fmt.Errorf("expected uint64, only found %d bits", len(b)*8) + } + z := binary.BigEndian.Uint64(b) + return strconv.FormatUint(z, 10), nil +} From 94c19d5a932b9d3ac0daa90b352f72db1346c617 Mon Sep 17 00:00:00 2001 From: Srdjan S Date: Mon, 28 Oct 2024 22:36:21 +0100 Subject: [PATCH 2/2] Add memory validation function --- protocols.go | 2 +- transcoders.go | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/protocols.go b/protocols.go index 985a9cd..0e74b5a 100644 --- a/protocols.go +++ b/protocols.go @@ -41,7 +41,7 @@ const ( P_PLAINTEXTV2 = 7367777 P_WEBRTC_DIRECT = 280 P_WEBRTC = 281 - P_MEMORY = 0x0309 // 777 decimal + P_MEMORY = 777 ) var ( diff --git a/transcoders.go b/transcoders.go index a6bde0d..03b39ae 100644 --- a/transcoders.go +++ b/transcoders.go @@ -490,7 +490,7 @@ func validateHTTPPath(b []byte) error { return nil // We can represent any byte slice when we escape it. } -var TranscoderMemory = NewTranscoderFromFunctions(memoryStB, memoryBtS, nil) +var TranscoderMemory = NewTranscoderFromFunctions(memoryStB, memoryBtS, memoryValidate) func memoryStB(s string) ([]byte, error) { z, err := strconv.ParseUint(s, 10, 64) @@ -509,3 +509,12 @@ func memoryBtS(b []byte) (string, error) { z := binary.BigEndian.Uint64(b) return strconv.FormatUint(z, 10), nil } + +func memoryValidate(b []byte) error { + // Ensure the byte array is exactly 8 bytes long for a uint64 in big-endian format + if len(b) != 8 { + return errors.New("invalid length: must be exactly 8 bytes") + } + + return nil +}