From 929fc72d2b389e5b5ee23a3af132d70c6cc558b2 Mon Sep 17 00:00:00 2001 From: siovanus Date: Mon, 17 Aug 2020 15:25:42 +0800 Subject: [PATCH] fix order bug --- cred_jwt.go | 26 ++++++++++++++++++++++---- cred_test.go | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/cred_jwt.go b/cred_jwt.go index e0e29fa..dad9f13 100644 --- a/cred_jwt.go +++ b/cred_jwt.go @@ -481,13 +481,22 @@ func (this *Credential) JWTPresentation2Json(jwtPresentation string) (*Verifiabl func (cred *JWTCredential) ToString() (string, error) { headerb, err := json.Marshal(cred.Header) if err != nil { - return "", fmt.Errorf("SignData, json.Marshal header error: %s", err) + return "", fmt.Errorf("ToString, json.Marshal header error: %s", err) } headerString := base64.StdEncoding.EncodeToString(headerb) - payloadb, err := json.Marshal(cred.Payload) + payloadb_temp, err := json.Marshal(cred.Payload) if err != nil { - return "", fmt.Errorf("SignData, json.Marshal payload error: %s", err) + return "", fmt.Errorf("ToString, json.Marshal payload error: %s", err) + } + payload := new(Payload) + err = json.Unmarshal(payloadb_temp, payload) + if err != nil { + return "", fmt.Errorf("ToString, json.Unmarshal payload error: %s", err) + } + payloadb, err := json.Marshal(payload) + if err != nil { + return "", fmt.Errorf("ToString, json.Marshal payload error: %s", err) } payloadString := base64.StdEncoding.EncodeToString(payloadb) @@ -501,7 +510,16 @@ func (cred *JWTCredential) SignData() ([]byte, error) { } headerString := base64.StdEncoding.EncodeToString(headerb) - payloadb, err := json.Marshal(cred.Payload) + payloadb_temp, err := json.Marshal(cred.Payload) + if err != nil { + return nil, fmt.Errorf("SignData, json.Marshal payload error: %s", err) + } + payload := new(Payload) + err = json.Unmarshal(payloadb_temp, payload) + if err != nil { + return nil, fmt.Errorf("SignData, json.Unmarshal payload error: %s", err) + } + payloadb, err := json.Marshal(payload) if err != nil { return nil, fmt.Errorf("SignData, json.Marshal payload error: %s", err) } diff --git a/cred_test.go b/cred_test.go index 88bda81..d6b9cf8 100644 --- a/cred_test.go +++ b/cred_test.go @@ -29,9 +29,9 @@ import ( type RelationshipCredential []*Relationship type Relationship struct { + Spouse string `json:"spouse"` Id string `json:"id"` Name string `json:"name"` - Spouse string `json:"spouse"` } func TestCredential(t *testing.T) {