delegation: working IPLD round-trip

This commit is contained in:
Michael Muré
2024-09-02 02:26:48 +02:00
parent ab2f074b22
commit cfd16fb0d2
3 changed files with 46 additions and 45 deletions

View File

@@ -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 {
}

View File

@@ -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
}

View File

@@ -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) {