Skip to content

Commit

Permalink
delegation: working IPLD round-trip
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed Sep 2, 2024
1 parent ab2f074 commit cfd16fb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 45 deletions.
7 changes: 2 additions & 5 deletions delegation/delegation.ipldsch
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,7 +27,3 @@ type Payload struct {
# The timestamp at which the Invocation becomes invalid
exp nullable Int
}

type Policy struct {

}
9 changes: 1 addition & 8 deletions delegation/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type PayloadModel struct {
Cmd string

// The delegation policy
Pol PolicyModel
Pol datamodel.Node

// A unique, random nonce
Nonce []byte
Expand All @@ -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
}
75 changes: 43 additions & 32 deletions delegation/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

p1, err := DecodeDagJson([]byte(delegationJson))
require.NoError(t, err)

cborBytes, err := p.EncodeDagCbor()
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()
fmt.Println("read Cbor", p2)

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) {
Expand Down

0 comments on commit cfd16fb

Please sign in to comment.