From cfd16fb0d2838c21535c7bac7d07e26dc3885fb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Mon, 2 Sep 2024 02:26:48 +0200 Subject: [PATCH] delegation: working IPLD round-trip --- delegation/delegation.ipldsch | 7 +--- delegation/schema.go | 9 +---- delegation/schema_test.go | 75 ++++++++++++++++++++--------------- 3 files changed, 46 insertions(+), 45 deletions(-) diff --git a/delegation/delegation.ipldsch b/delegation/delegation.ipldsch index 0e8e489..c65988a 100644 --- a/delegation/delegation.ipldsch +++ b/delegation/delegation.ipldsch @@ -13,7 +13,8 @@ type Payload struct { cmd String # The delegation policy - pol Policy + # It doesn't seem possible to represent it with a schema. + pol Any # A unique, random nonce nonce Bytes @@ -26,7 +27,3 @@ type Payload struct { # The timestamp at which the Invocation becomes invalid exp nullable Int } - -type Policy struct { - -} \ No newline at end of file diff --git a/delegation/schema.go b/delegation/schema.go index f2c7c38..4615c6b 100644 --- a/delegation/schema.go +++ b/delegation/schema.go @@ -46,7 +46,7 @@ type PayloadModel struct { Cmd string // The delegation policy - Pol PolicyModel + Pol datamodel.Node // A unique, random nonce Nonce []byte @@ -67,10 +67,3 @@ type MetaModel struct { Keys []string Values map[string]datamodel.Node } - -type PolicyModel struct { -} - -func PointerTo[T any](v T) *T { - return &v -} diff --git a/delegation/schema_test.go b/delegation/schema_test.go index 6abad79..bb7daed 100644 --- a/delegation/schema_test.go +++ b/delegation/schema_test.go @@ -5,51 +5,62 @@ import ( "testing" "github.com/ipld/go-ipld-prime" - "github.com/ipld/go-ipld-prime/datamodel" - "github.com/ipld/go-ipld-prime/node/bindnode" "github.com/stretchr/testify/require" ) func TestSchemaRoundTrip(t *testing.T) { - p := &PayloadModel{ - Iss: "did:key:abc123", - Aud: "did:key:def456", - Sub: PointerTo(""), - Cmd: "/foo/bar", - Pol: PolicyModel{}, // TODO: have something here - Nonce: []byte("super-random"), - Meta: MetaModel{ - Keys: []string{"foo", "bar"}, - Values: map[string]datamodel.Node{ - "foo": bindnode.Wrap(PointerTo("fooo"), nil), - "bar": bindnode.Wrap(PointerTo("baaar"), nil), - }, - }, - Nbf: PointerTo(int64(123456)), - Exp: PointerTo(int64(123456)), - } + const delegationJson = ` +{ + "aud":"did:key:def456", + "cmd":"/foo/bar", + "exp":123456, + "iss":"did:key:abc123", + "meta":{ + "bar":"baaar", + "foo":"fooo" + }, + "nbf":123456, + "nonce":{ + "/":{ + "bytes":"c3VwZXItcmFuZG9t" + } + }, + "pol":[ + ["==", ".status", "draft"], + ["all", ".reviewer", [ + ["like", ".email", "*@example.com"]] + ], + ["any", ".tags", [ + ["or", [ + ["==", ".", "news"], + ["==", ".", "press"]] + ]] + ] + ], + "sub":"" +} +` + // format: dagJson --> PayloadModel --> dagCbor --> PayloadModel --> dagJson + // function: DecodeDagJson() EncodeDagCbor() DecodeDagCbor() EncodeDagJson() - cborBytes, err := p.EncodeDagCbor() + p1, err := DecodeDagJson([]byte(delegationJson)) + require.NoError(t, err) + + cborBytes, err := p1.EncodeDagCbor() require.NoError(t, err) fmt.Println("cborBytes length", len(cborBytes)) fmt.Println("cbor", string(cborBytes)) - jsonBytes, err := p.EncodeDagJson() + p2, err := DecodeDagCbor(cborBytes) require.NoError(t, err) - fmt.Println("jsonBytes length", len(jsonBytes)) - fmt.Println("json: ", string(jsonBytes)) + fmt.Println("read Cbor", p2) - fmt.Println() - - readCbor, err := DecodeDagCbor(cborBytes) + readJson, err := p2.EncodeDagJson() require.NoError(t, err) - fmt.Println("readCbor", readCbor) - require.Equal(t, p, readCbor) + fmt.Println("readJson length", len(readJson)) + fmt.Println("json: ", string(readJson)) - readJson, err := DecodeDagJson(jsonBytes) - require.NoError(t, err) - fmt.Println("readJson", readJson) - require.Equal(t, p, readJson) + require.JSONEq(t, delegationJson, string(readJson)) } func BenchmarkSchemaLoad(b *testing.B) {