From 3a7f2608b3613e40c055e8f9a6afb9337c28c580 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 14 Oct 2022 11:41:27 -0400 Subject: [PATCH] fix(library): signature and pubkey empty string --- mobile/api.go | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/mobile/api.go b/mobile/api.go index 12d07c72..d7746ecf 100644 --- a/mobile/api.go +++ b/mobile/api.go @@ -335,6 +335,19 @@ func unmarshalPubkey(pub []byte) (ecdsa.PublicKey, error) { return ecdsa.PublicKey{Curve: secp256k1.S256(), X: x, Y: y}, nil } +func extractPubKeyAndSignature(payload *node.DecodedPayload) (pubkey string, signature string) { + pkBytes := crypto.FromECDSAPub(payload.PubKey) + if len(pkBytes) != 0 { + pubkey = hexutil.Encode(pkBytes) + } + + if len(payload.Signature) != 0 { + signature = hexutil.Encode(payload.Signature) + } + + return +} + func DecodeSymmetric(messageJSON string, symmetricKey string) string { var msg pb.WakuMessage err := json.Unmarshal([]byte(messageJSON), &msg) @@ -362,14 +375,16 @@ func DecodeSymmetric(messageJSON string, symmetricKey string) string { return MakeJSONResponse(err) } + pubkey, signature := extractPubKeyAndSignature(payload) + response := struct { - PubKey string `json:"pubkey"` - Signature string `json:"signature"` + PubKey string `json:"pubkey,omitempty"` + Signature string `json:"signature,omitempty"` Data []byte `json:"data"` Padding []byte `json:"padding"` }{ - PubKey: hexutil.Encode(crypto.FromECDSAPub(payload.PubKey)), - Signature: hexutil.Encode(payload.Signature), + PubKey: pubkey, + Signature: signature, Data: payload.Data, Padding: payload.Padding, } @@ -409,14 +424,16 @@ func DecodeAsymmetric(messageJSON string, privateKey string) string { return MakeJSONResponse(err) } + pubkey, signature := extractPubKeyAndSignature(payload) + response := struct { - PubKey string `json:"pubkey"` - Signature string `json:"signature"` + PubKey string `json:"pubkey,omitempty"` + Signature string `json:"signature,omitempty"` Data []byte `json:"data"` Padding []byte `json:"padding"` }{ - PubKey: hexutil.Encode(crypto.FromECDSAPub(payload.PubKey)), - Signature: hexutil.Encode(payload.Signature), + PubKey: pubkey, + Signature: signature, Data: payload.Data, Padding: payload.Padding, }