Skip to content

Commit adda745

Browse files
committed
docs: minimal examples on the readme
1 parent fb556da commit adda745

3 files changed

Lines changed: 165 additions & 5 deletions

File tree

README.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,112 @@
1414

1515
### Examples
1616

17-
TODO
17+
#### Principals
18+
19+
See examples in [principals_test.go](./examples/principals_test.go)
20+
21+
```go
22+
principal, err := did.Parse("did:key:z6MkfBSb2hC6g3UGnqNmWfmGvPdfMorBpT2osm9bk9b4Cyqu")
23+
fmt.Println("DID:", principal.DID())
24+
25+
// generate a new ed25519 signer (it is also a principal - has a DID() method)
26+
signerPrincipal, err := ed25519.Generate()
27+
fmt.Println("DID:", signer.DID())
28+
29+
// this principal can sign
30+
sig := signerPrincipal.Sign([]byte{1, 2, 3})
31+
fmt.Printf("Signature: 0x%x\n", sig)
32+
33+
// and has a private key (use format utility to multibase base64pad encode)
34+
fmt.Println("Private Key:", signer.Format(signerPrincipal))
35+
36+
// which can be stored and decoded later...
37+
signerPrincipal2, err := ed25519.Decode(signerPrincipal.Bytes())
38+
```
39+
40+
#### Delegation
41+
42+
See examples in [delegations_test.go](./examples/delegations_test.go)
43+
44+
```go
45+
dlg, err = delegation.Delegate(
46+
alice, // issuer
47+
bob, // audience (receiver)
48+
mailer, // subject
49+
"/message/send", // command
50+
// policy (alice delegates bob capability to use the email service, but only
51+
// allows bob to send to example.com email addresses)
52+
delegation.WithPolicyBuilder(
53+
policy.All(".to", policy.Like(".", "*.example.com")),
54+
),
55+
)
56+
```
57+
58+
#### Invocation
59+
60+
See examples in [invocations_test.go](./examples/invocations_test.go)
61+
62+
```go
63+
inv, err := invocation.Invoke(
64+
alice,
65+
mailer,
66+
"/message/send",
67+
ipld.Map{
68+
"from": "alice@example.com",
69+
"to": "bob@example.com",
70+
"message": "Hello Bob!",
71+
},
72+
invocation.WithProofs(dlg.Link()),
73+
)
74+
```
75+
76+
#### Capability definition
77+
78+
See examples in [capability_definition_test.go](./examples/capability_definition_test.go)
79+
80+
```go
81+
type MessageSendArguments struct {
82+
To []string
83+
Message string
84+
}
85+
86+
messageSend, err := capability.New[*MessageSendArguments](
87+
"/message/send",
88+
capability.WithPolicyBuilder(
89+
policy.NotEqual(".to", []string{}),
90+
),
91+
)
92+
93+
// delegate the capability
94+
dlg, err := messageSend.Delegate(mailer, alice, mailer)
95+
96+
// invoke the capability
97+
invocation, err := messageSend.Invoke(
98+
alice,
99+
mailer,
100+
&MessageSendArguments{
101+
To: []string{"bob@example.com"},
102+
Message: "Hello Bob, How do you do?",
103+
},
104+
invocation.WithProofs(dlg.Link()),
105+
)
106+
```
107+
108+
#### Container
109+
110+
See examples in [container_test.go](./examples/container_test.go)
111+
112+
```go
113+
ct, err := container.New(
114+
container.WithDelegations(dlg0, dlg1),
115+
container.WithInvocations(inv0),
116+
// container.WithReceipts(...),
117+
)
118+
119+
// Various encoding options are available, the following (Base64Gzip) is good
120+
// for when you want to add the container to a HTTP header.
121+
buf, err := container.Encode(container.Base64Gzip, ct)
122+
```
18123

19124
## Contributing
20125

examples/delegations_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ func TestDelegations(t *testing.T) {
2727

2828
// delegate alice capability to use the email service
2929
_, err = delegation.Delegate(
30-
mailer,
31-
alice,
32-
mailer,
33-
"/message/send",
30+
mailer, // issuer
31+
alice, // audience (receiver)
32+
mailer, // subject
33+
"/message/send", // command
3434
)
3535
if err != nil {
3636
panic(err)

examples/invocations_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package examples
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/alanshaw/ucantone/ipld"
8+
"github.com/alanshaw/ucantone/principal/ed25519"
9+
"github.com/alanshaw/ucantone/ucan/delegation"
10+
"github.com/alanshaw/ucantone/ucan/invocation"
11+
)
12+
13+
func TestInvocations(t *testing.T) {
14+
// mailer is an email service that can send emails
15+
mailer, err := ed25519.Generate()
16+
if err != nil {
17+
panic(err)
18+
}
19+
20+
alice, err := ed25519.Generate()
21+
if err != nil {
22+
panic(err)
23+
}
24+
25+
// delegate alice capability to use the email service
26+
dlg, err := delegation.Delegate(
27+
mailer, // issuer
28+
alice, // audience (receiver)
29+
mailer, // subject
30+
"/message/send", // command
31+
)
32+
if err != nil {
33+
panic(err)
34+
}
35+
36+
inv, err := invocation.Invoke(
37+
alice,
38+
mailer,
39+
"/message/send",
40+
ipld.Map{
41+
"from": "alice@example.com",
42+
"to": "bob@example.com",
43+
"message": "Hello Bob!",
44+
},
45+
invocation.WithProofs(dlg.Link()),
46+
)
47+
if err != nil {
48+
panic(err)
49+
}
50+
fmt.Println(inv.Link())
51+
52+
// Now send the invocations to the mailer for execution. You'll need to also
53+
// send the delegation as proof. You may want to use a _container_ for this.
54+
// See `container_test.go` in this directory.
55+
}

0 commit comments

Comments
 (0)