Skip to content

Commit

Permalink
extend GistInfo with Proof object (#97)
Browse files Browse the repository at this point in the history
* extend GistInfo from Proof object
  • Loading branch information
volodymyr-basiuk authored Nov 25, 2024
1 parent 3edd1f4 commit 19f1772
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 6 deletions.
53 changes: 47 additions & 6 deletions verifiable/did_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"

"github.com/iden3/go-merkletree-sql/v2"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -126,12 +127,52 @@ type StateInfo struct {

// GistInfo representation state of gist root.
type GistInfo struct {
Root string `json:"root"`
ReplacedByRoot string `json:"replacedByRoot"`
CreatedAtTimestamp string `json:"createdAtTimestamp"`
ReplacedAtTimestamp string `json:"replacedAtTimestamp"`
CreatedAtBlock string `json:"createdAtBlock"`
ReplacedAtBlock string `json:"replacedAtBlock"`
Root string `json:"root"`
ReplacedByRoot string `json:"replacedByRoot"`
CreatedAtTimestamp string `json:"createdAtTimestamp"`
ReplacedAtTimestamp string `json:"replacedAtTimestamp"`
CreatedAtBlock string `json:"createdAtBlock"`
ReplacedAtBlock string `json:"replacedAtBlock"`
Proof *GistInfoProof `json:"proof,omitempty"`
}

// GistInfoProof representation proof of GistInfo object.
type GistInfoProof struct {
merkletree.Proof
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(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.
Expand Down
93 changes: 93 additions & 0 deletions verifiable/did_doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package verifiable

import (
"encoding/json"
"testing"

"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"
]
}`

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.Nil(t, proof.Proof.NodeAux)

marshaled, err := proof.MarshalJSON()
require.NoError(t, err)
require.JSONEq(t, in, string(marshaled))
}

0 comments on commit 19f1772

Please sign in to comment.