From 8d42febe180a6357d8a0233a36c787f1e0064a05 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 14 Sep 2022 15:19:04 -0400 Subject: [PATCH] fix: make 0x prefix optional (#306) --- mobile/api.go | 5 +++-- mobile/encoding.go | 10 +++++----- waku/v2/protocol/rln/waku_rln_relay.go | 6 +++--- waku/v2/rpc/private.go | 7 ++++--- waku/v2/rpc/utils.go | 5 +++-- waku/v2/utils/hex.go | 11 +++++++++++ 6 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 waku/v2/utils/hex.go diff --git a/mobile/api.go b/mobile/api.go index 0e67c37d..d570099b 100644 --- a/mobile/api.go +++ b/mobile/api.go @@ -22,6 +22,7 @@ import ( "github.com/status-im/go-waku/waku/v2/node" "github.com/status-im/go-waku/waku/v2/protocol" "github.com/status-im/go-waku/waku/v2/protocol/pb" + "github.com/status-im/go-waku/waku/v2/utils" ) var wakuNode *node.WakuNode @@ -351,7 +352,7 @@ func DecodeSymmetric(messageJSON string, symmetricKey string) string { Kind: node.Symmetric, } - keyInfo.SymKey, err = hexutil.Decode(symmetricKey) + keyInfo.SymKey, err = utils.DecodeHexString(symmetricKey) if err != nil { return makeJSONResponse(err) } @@ -393,7 +394,7 @@ func DecodeAsymmetric(messageJSON string, privateKey string) string { Kind: node.Asymmetric, } - keyBytes, err := hexutil.Decode(privateKey) + keyBytes, err := utils.DecodeHexString(privateKey) if err != nil { return makeJSONResponse(err) } diff --git a/mobile/encoding.go b/mobile/encoding.go index e9e8b03d..1cc75aac 100644 --- a/mobile/encoding.go +++ b/mobile/encoding.go @@ -3,10 +3,10 @@ package gowaku import ( "encoding/json" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" "github.com/status-im/go-waku/waku/v2/node" "github.com/status-im/go-waku/waku/v2/protocol/pb" + "github.com/status-im/go-waku/waku/v2/utils" ) func wakuMessage(messageJSON string) (pb.WakuMessage, error) { @@ -29,7 +29,7 @@ func wakuMessageSymmetricEncoding(messageJSON string, publicKey string, optional }, } - keyBytes, err := hexutil.Decode(publicKey) + keyBytes, err := utils.DecodeHexString(publicKey) if err != nil { return msg, err } @@ -40,7 +40,7 @@ func wakuMessageSymmetricEncoding(messageJSON string, publicKey string, optional } if optionalSigningKey != "" { - signingKeyBytes, err := hexutil.Decode(optionalSigningKey) + signingKeyBytes, err := utils.DecodeHexString(optionalSigningKey) if err != nil { return msg, err } @@ -70,7 +70,7 @@ func wakuMessageAsymmetricEncoding(messageJSON string, publicKey string, optiona }, } - keyBytes, err := hexutil.Decode(publicKey) + keyBytes, err := utils.DecodeHexString(publicKey) if err != nil { return msg, err } @@ -81,7 +81,7 @@ func wakuMessageAsymmetricEncoding(messageJSON string, publicKey string, optiona } if optionalSigningKey != "" { - signingKeyBytes, err := hexutil.Decode(optionalSigningKey) + signingKeyBytes, err := utils.DecodeHexString(optionalSigningKey) if err != nil { return msg, err } diff --git a/waku/v2/protocol/rln/waku_rln_relay.go b/waku/v2/protocol/rln/waku_rln_relay.go index 18dfed85..0945c3ee 100644 --- a/waku/v2/protocol/rln/waku_rln_relay.go +++ b/waku/v2/protocol/rln/waku_rln_relay.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "crypto/ecdsa" - "encoding/hex" "errors" "math" "time" @@ -18,6 +17,7 @@ import ( r "github.com/status-im/go-rln/rln" "github.com/status-im/go-waku/waku/v2/protocol/pb" "github.com/status-im/go-waku/waku/v2/protocol/relay" + "github.com/status-im/go-waku/waku/v2/utils" "go.uber.org/zap" ) @@ -365,11 +365,11 @@ func toMembershipKeyPairs(groupKeys [][]string) ([]r.MembershipKeyPair, error) { groupKeyPairs := []r.MembershipKeyPair{} for _, pair := range groupKeys { - idKey, err := hex.DecodeString(pair[0]) + idKey, err := utils.DecodeHexString(pair[0]) if err != nil { return nil, err } - idCommitment, err := hex.DecodeString(pair[1]) + idCommitment, err := utils.DecodeHexString(pair[1]) if err != nil { return nil, err } diff --git a/waku/v2/rpc/private.go b/waku/v2/rpc/private.go index b4e244e4..33b689d2 100644 --- a/waku/v2/rpc/private.go +++ b/waku/v2/rpc/private.go @@ -14,6 +14,7 @@ import ( "github.com/status-im/go-waku/waku/v2/protocol" "github.com/status-im/go-waku/waku/v2/protocol/pb" "github.com/status-im/go-waku/waku/v2/protocol/relay" + "github.com/status-im/go-waku/waku/v2/utils" "go.uber.org/zap" ) @@ -107,7 +108,7 @@ func (p *PrivateService) GetV1AsymmetricKeypair(req *http.Request, args *Empty, } func (p *PrivateService) PostV1SymmetricMessage(req *http.Request, args *SymmetricMessageArgs, reply *SuccessReply) error { - symKeyBytes, err := hexutil.Decode(args.SymKey) + symKeyBytes, err := utils.DecodeHexString(args.SymKey) if err != nil { return fmt.Errorf("invalid symmetric key: %w", err) } @@ -142,7 +143,7 @@ func (p *PrivateService) PostV1AsymmetricMessage(req *http.Request, args *Asymme keyInfo := new(node.KeyInfo) keyInfo.Kind = node.Asymmetric - pubKeyBytes, err := hexutil.Decode(args.PublicKey) + pubKeyBytes, err := utils.DecodeHexString(args.PublicKey) if err != nil { return fmt.Errorf("public key cannot be decoded: %v", err) } @@ -184,7 +185,7 @@ func (p *PrivateService) GetV1SymmetricMessages(req *http.Request, args *Symmetr p.messages[args.Topic] = make([]*pb.WakuMessage, 0) } - symKeyBytes, err := hexutil.Decode(args.SymKey) + symKeyBytes, err := utils.DecodeHexString(args.SymKey) if err != nil { return fmt.Errorf("invalid symmetric key: %w", err) } diff --git a/waku/v2/rpc/utils.go b/waku/v2/rpc/utils.go index 3c1613e0..d4665bb9 100644 --- a/waku/v2/rpc/utils.go +++ b/waku/v2/rpc/utils.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/status-im/go-waku/waku/v2/protocol/pb" + "github.com/status-im/go-waku/waku/v2/utils" ) // HexBytes is marshalled to a hex string @@ -107,7 +108,7 @@ func (h *HexBytes) UnmarshalText(b []byte) error { hexString = string(b) } - decoded, err := hex.DecodeString(hexString) + decoded, err := utils.DecodeHexString(hexString) if err != nil { return err } @@ -181,7 +182,7 @@ func (h *ByteArray) UnmarshalText(b []byte) error { hexString = string(b) } - decoded, err := hex.DecodeString(hexString) + decoded, err := utils.DecodeHexString(hexString) if err != nil { return err } diff --git a/waku/v2/utils/hex.go b/waku/v2/utils/hex.go new file mode 100644 index 00000000..cdc767a2 --- /dev/null +++ b/waku/v2/utils/hex.go @@ -0,0 +1,11 @@ +package utils + +import ( + "encoding/hex" + "strings" +) + +func DecodeHexString(input string) ([]byte, error) { + input = strings.TrimPrefix(input, "0x") + return hex.DecodeString(input) +}