From beb10d5c418f67eca765067af51c9c4f89a7763e Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 14 Jun 2023 15:02:44 +0200 Subject: [PATCH] renaming to parseV0Namespace, dropping refactoring commits --- cmd/celestia/rpc.go | 59 ++++++++++++++++++++++------------------ cmd/celestia/rpc_test.go | 4 +-- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/cmd/celestia/rpc.go b/cmd/celestia/rpc.go index 8f260d43bb..18851dc797 100644 --- a/cmd/celestia/rpc.go +++ b/cmd/celestia/rpc.go @@ -113,7 +113,7 @@ func parseParams(method string, params []string) []interface{} { } parsedParams[0] = root // 2. NamespaceID - nID, err := parseNamespaceID(params[1]) + nID, err := parseV0NamespaceID(params[1]) if err != nil { panic(fmt.Sprintf("Error parsing namespace ID: %v", err)) } @@ -121,7 +121,7 @@ func parseParams(method string, params []string) []interface{} { case "Submit": // 1. NamespaceID var err error - nID, err := parseNamespaceID(params[0]) + nID, err := parseV0NamespaceID(params[0]) if err != nil { panic(fmt.Sprintf("Error parsing namespace ID: %v", err)) } @@ -163,7 +163,7 @@ func parseParams(method string, params []string) []interface{} { } parsedParams[1] = num // 3. NamespaceID - nID, err := parseNamespaceID(params[2]) + nID, err := parseV0NamespaceID(params[2]) if err != nil { panic(fmt.Sprintf("Error parsing namespace ID: %v", err)) } @@ -202,7 +202,7 @@ func parseParams(method string, params []string) []interface{} { } parsedParams[0] = num // 2. NamespaceID - nID, err := parseNamespaceID(params[1]) + nID, err := parseV0NamespaceID(params[1]) if err != nil { panic(fmt.Sprintf("Error parsing namespace ID: %v", err)) } @@ -222,7 +222,7 @@ func parseParams(method string, params []string) []interface{} { } parsedParams[0] = num // 2. NamespaceID - nID, err := parseNamespaceID(params[1]) + nID, err := parseV0NamespaceID(params[1]) if err != nil { panic(fmt.Sprintf("Error parsing namespace ID: %v", err)) } @@ -369,7 +369,7 @@ func sendJSONRPCRequest(namespace, method string, params []interface{}) { rawResponseJSON, err := parseJSON(string(responseBody)) if err != nil { - panic(err) + log.Fatalf("Error parsing JSON-RPC response: %v", err) } if printRequest { output, err := json.MarshalIndent(outputWithRequest{ @@ -419,34 +419,41 @@ func parseSignatureForHelpstring(methodSig reflect.StructField) string { return simplifiedSignature } -func parseNamespaceID(param string) (namespace.ID, error) { - var nID []byte - var err error +// parseV0NamespaceID parses a namespace ID from a base64 or hex string. The param +// is expected to be the user-specified portion of a v0 namespace ID (i.e. the +// last 10 bytes). +func parseV0NamespaceID(param string) (namespace.ID, error) { + userBytes, err := decodeToBytes(param) + if err != nil { + return nil, err + } + + if len(userBytes) > appns.NamespaceVersionZeroIDSize { + return nil, fmt.Errorf( + "namespace ID %v is too large to be a v0 namespace, want <= %v bytes", + userBytes, appns.NamespaceVersionZeroIDSize, + ) + } + // if the namespace ID is <= 10 bytes, left pad it with 0s + return share.NewNamespaceV0(userBytes) +} + +// decodeToBytes decodes a Base64 or hex input string into a byte slice. +func decodeToBytes(param string) ([]byte, error) { if strings.HasPrefix(param, "0x") { decoded, err := hex.DecodeString(param[2:]) if err != nil { return nil, fmt.Errorf("error decoding namespace ID: %w", err) } - nID = decoded - } else { - // otherwise, it's just a base64 string - nID, err = base64.StdEncoding.DecodeString(param) - if err != nil { - return nil, fmt.Errorf("error decoding namespace ID: %w", err) - } + return decoded, nil } - // if the namespace ID is <= 10 bytes, add v0 share + namespace prefix and zero pad - if len(nID) <= appns.NamespaceVersionZeroIDSize { - nID, err = share.NewNamespaceV0(nID) - if err != nil { - return nil, err - } - } else if len(nID) < appns.NamespaceSize { - return nil, fmt.Errorf("passed namespace is too large") + // otherwise, it's just a base64 string + decoded, err := base64.StdEncoding.DecodeString(param) + if err != nil { + return nil, fmt.Errorf("error decoding namespace ID: %w", err) } - return nID, nil + return decoded, nil } - func parseJSON(param string) (json.RawMessage, error) { var raw json.RawMessage err := json.Unmarshal([]byte(param), &raw) diff --git a/cmd/celestia/rpc_test.go b/cmd/celestia/rpc_test.go index f16042c80d..aab8fba131 100644 --- a/cmd/celestia/rpc_test.go +++ b/cmd/celestia/rpc_test.go @@ -42,7 +42,7 @@ func Test_parseNamespaceID(t *testing.T) { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // v0 ID prefix 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // namespace ID }, - wantErr: false, + wantErr: true, }, { name: "11 byte hex encoded namespace ID returns error", @@ -69,7 +69,7 @@ func Test_parseNamespaceID(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - got, err := parseNamespaceID(tc.param) + got, err := parseV0NamespaceID(tc.param) if tc.wantErr { assert.Error(t, err) return