diff --git a/verifiable/did_doc.go b/verifiable/did_doc.go index 8fb63b2..7f9e567 100644 --- a/verifiable/did_doc.go +++ b/verifiable/did_doc.go @@ -142,6 +142,39 @@ type GistInfoProof struct { Type ProofType `json:"type"` } +// MarshalJSON for GistInfoProof +func (g GistInfoProof) MarshalJSON() ([]byte, error) { + proofData, err := json.Marshal(g.Proof) + if err != nil { + return nil, err + } + proof := map[string]interface{}{} + if err := json.Unmarshal(proofData, &proof); err != nil { + return nil, err + } + proof["type"] = g.Type + return json.Marshal(proof) +} + +// UnmarshalJSON for GistInfoProof +func (g *GistInfoProof) UnmarshalJSON(data []byte) error { + var proof merkletree.Proof + if err := json.Unmarshal([]byte(data), &proof); err != nil { + return err + } + + typeStruct := struct { + Type ProofType `json:"type"` + }{} + if err := json.Unmarshal(data, &typeStruct); err != nil { + return err + } + + g.Proof = proof + g.Type = typeStruct.Type + return nil +} + // IdentityState representation all info about identity. type IdentityState struct { Published *bool `json:"published,omitempty"` diff --git a/verifiable/did_doc_test.go b/verifiable/did_doc_test.go new file mode 100644 index 0000000..03a6869 --- /dev/null +++ b/verifiable/did_doc_test.go @@ -0,0 +1,98 @@ +package verifiable + +import ( + "encoding/json" + "testing" + + "github.com/iden3/go-merkletree-sql/v2" + "github.com/stretchr/testify/require" +) + +func TestGistInfoProof_JSON_Unmarshal_Marshal(t *testing.T) { + in := `{ + "type": "Iden3SparseMerkleTreeProof", + "existence": true, + "siblings": [ + "1362535354014507859867367590099676368653533743679052873579632656491435384778", + "11921658728427020988213827821301476324611070652461851254718454837799781090130", + "14437346982570868636439880944965253984519016799788166801110955632411304936181", + "7008861419840281183040259263097349725975544589604657255528412015559570756430", + "12919820512704336619019284308940813320869421725637735792759784734583345278320", + "10847811404722023193836917968795578158377516355689063480344319030883153551997", + "7501704662566146993443082955484915477984763397289571730014912300112522436190", + "15319676397008451935308301168627943776087314271828889852225733045012068685123", + "13580625240484189131905658989056965789342053909035527622054608432235108291371", + "15701076866894648427718398501239266270187920232235356979681337424723013748037", + "18391822292664048359198417757393480551710071249895941413402198372170950884043", + "0", + "1956510840262628579400226733676154238486255274390348671620337333964042370619", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0", + "0" + ], + "node_aux": { + "key": "0", + "value": "0" + } + }` + + var proof GistInfoProof + err := json.Unmarshal([]byte(in), &proof) + require.NoError(t, err) + require.Equal(t, Iden3SparseMerkleTreeProofType, proof.Type) + require.Equal(t, true, proof.Existence) + require.Len(t, proof.Proof.AllSiblings(), 64) + require.Equal(t, &merkletree.HashZero, proof.NodeAux.Key) + + marshaled, err := proof.MarshalJSON() + require.NoError(t, err) + require.JSONEq(t, in, string(marshaled)) +}