fix: make 0x prefix optional (#306)

This commit is contained in:
Richard Ramos 2022-09-14 15:19:04 -04:00 committed by GitHub
parent 83250be0fb
commit 8d42febe18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 15 deletions

View File

@ -22,6 +22,7 @@ import (
"github.com/status-im/go-waku/waku/v2/node" "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"
"github.com/status-im/go-waku/waku/v2/protocol/pb" "github.com/status-im/go-waku/waku/v2/protocol/pb"
"github.com/status-im/go-waku/waku/v2/utils"
) )
var wakuNode *node.WakuNode var wakuNode *node.WakuNode
@ -351,7 +352,7 @@ func DecodeSymmetric(messageJSON string, symmetricKey string) string {
Kind: node.Symmetric, Kind: node.Symmetric,
} }
keyInfo.SymKey, err = hexutil.Decode(symmetricKey) keyInfo.SymKey, err = utils.DecodeHexString(symmetricKey)
if err != nil { if err != nil {
return makeJSONResponse(err) return makeJSONResponse(err)
} }
@ -393,7 +394,7 @@ func DecodeAsymmetric(messageJSON string, privateKey string) string {
Kind: node.Asymmetric, Kind: node.Asymmetric,
} }
keyBytes, err := hexutil.Decode(privateKey) keyBytes, err := utils.DecodeHexString(privateKey)
if err != nil { if err != nil {
return makeJSONResponse(err) return makeJSONResponse(err)
} }

View File

@ -3,10 +3,10 @@ package gowaku
import ( import (
"encoding/json" "encoding/json"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/status-im/go-waku/waku/v2/node" "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/protocol/pb"
"github.com/status-im/go-waku/waku/v2/utils"
) )
func wakuMessage(messageJSON string) (pb.WakuMessage, error) { 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 { if err != nil {
return msg, err return msg, err
} }
@ -40,7 +40,7 @@ func wakuMessageSymmetricEncoding(messageJSON string, publicKey string, optional
} }
if optionalSigningKey != "" { if optionalSigningKey != "" {
signingKeyBytes, err := hexutil.Decode(optionalSigningKey) signingKeyBytes, err := utils.DecodeHexString(optionalSigningKey)
if err != nil { if err != nil {
return msg, err 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 { if err != nil {
return msg, err return msg, err
} }
@ -81,7 +81,7 @@ func wakuMessageAsymmetricEncoding(messageJSON string, publicKey string, optiona
} }
if optionalSigningKey != "" { if optionalSigningKey != "" {
signingKeyBytes, err := hexutil.Decode(optionalSigningKey) signingKeyBytes, err := utils.DecodeHexString(optionalSigningKey)
if err != nil { if err != nil {
return msg, err return msg, err
} }

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"encoding/hex"
"errors" "errors"
"math" "math"
"time" "time"
@ -18,6 +17,7 @@ import (
r "github.com/status-im/go-rln/rln" 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/pb"
"github.com/status-im/go-waku/waku/v2/protocol/relay" "github.com/status-im/go-waku/waku/v2/protocol/relay"
"github.com/status-im/go-waku/waku/v2/utils"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -365,11 +365,11 @@ func toMembershipKeyPairs(groupKeys [][]string) ([]r.MembershipKeyPair, error) {
groupKeyPairs := []r.MembershipKeyPair{} groupKeyPairs := []r.MembershipKeyPair{}
for _, pair := range groupKeys { for _, pair := range groupKeys {
idKey, err := hex.DecodeString(pair[0]) idKey, err := utils.DecodeHexString(pair[0])
if err != nil { if err != nil {
return nil, err return nil, err
} }
idCommitment, err := hex.DecodeString(pair[1]) idCommitment, err := utils.DecodeHexString(pair[1])
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -14,6 +14,7 @@ import (
"github.com/status-im/go-waku/waku/v2/protocol" "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/pb"
"github.com/status-im/go-waku/waku/v2/protocol/relay" "github.com/status-im/go-waku/waku/v2/protocol/relay"
"github.com/status-im/go-waku/waku/v2/utils"
"go.uber.org/zap" "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 { 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 { if err != nil {
return fmt.Errorf("invalid symmetric key: %w", err) 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 := new(node.KeyInfo)
keyInfo.Kind = node.Asymmetric keyInfo.Kind = node.Asymmetric
pubKeyBytes, err := hexutil.Decode(args.PublicKey) pubKeyBytes, err := utils.DecodeHexString(args.PublicKey)
if err != nil { if err != nil {
return fmt.Errorf("public key cannot be decoded: %v", err) 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) p.messages[args.Topic] = make([]*pb.WakuMessage, 0)
} }
symKeyBytes, err := hexutil.Decode(args.SymKey) symKeyBytes, err := utils.DecodeHexString(args.SymKey)
if err != nil { if err != nil {
return fmt.Errorf("invalid symmetric key: %w", err) return fmt.Errorf("invalid symmetric key: %w", err)
} }

View File

@ -6,6 +6,7 @@ import (
"strings" "strings"
"github.com/status-im/go-waku/waku/v2/protocol/pb" "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 // HexBytes is marshalled to a hex string
@ -107,7 +108,7 @@ func (h *HexBytes) UnmarshalText(b []byte) error {
hexString = string(b) hexString = string(b)
} }
decoded, err := hex.DecodeString(hexString) decoded, err := utils.DecodeHexString(hexString)
if err != nil { if err != nil {
return err return err
} }
@ -181,7 +182,7 @@ func (h *ByteArray) UnmarshalText(b []byte) error {
hexString = string(b) hexString = string(b)
} }
decoded, err := hex.DecodeString(hexString) decoded, err := utils.DecodeHexString(hexString)
if err != nil { if err != nil {
return err return err
} }

11
waku/v2/utils/hex.go Normal file
View File

@ -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)
}