|
14 | 14 |
|
15 | 15 | ### Examples |
16 | 16 |
|
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 | +``` |
18 | 123 |
|
19 | 124 | ## Contributing |
20 | 125 |
|
|
0 commit comments