|
| 1 | +package examples |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/alanshaw/ucantone/examples/types" |
| 8 | + "github.com/alanshaw/ucantone/ipld" |
| 9 | + "github.com/alanshaw/ucantone/principal/ed25519" |
| 10 | + "github.com/alanshaw/ucantone/ucan/delegation" |
| 11 | + "github.com/alanshaw/ucantone/ucan/invocation" |
| 12 | + "github.com/alanshaw/ucantone/ucan/promise" |
| 13 | + "github.com/alanshaw/ucantone/validator/capability" |
| 14 | +) |
| 15 | + |
| 16 | +func TestPromises(t *testing.T) { |
| 17 | + // Mailer is an email service that can send emails |
| 18 | + mailer, err := ed25519.Generate() |
| 19 | + if err != nil { |
| 20 | + panic(err) |
| 21 | + } |
| 22 | + |
| 23 | + alice, err := ed25519.Generate() |
| 24 | + if err != nil { |
| 25 | + panic(err) |
| 26 | + } |
| 27 | + |
| 28 | + // A list of email addresses |
| 29 | + mailingList, err := ed25519.Generate() |
| 30 | + if err != nil { |
| 31 | + panic(err) |
| 32 | + } |
| 33 | + |
| 34 | + // A delegation from the mailer to alice allowing her to send emails |
| 35 | + msgSendDlg, err := delegation.Delegate(mailer, alice, mailer, "/msg/send") |
| 36 | + if err != nil { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | + |
| 40 | + // A delegation from the mailing list to alice allowing her to read the emails |
| 41 | + listEmailsDlg, err := delegation.Delegate(mailingList, alice, mailingList, "/emails/list") |
| 42 | + if err != nil { |
| 43 | + panic(err) |
| 44 | + } |
| 45 | + |
| 46 | + // Read the emails on the mailing list. The mailer stores the email listings |
| 47 | + // so the invocation audience is the mailer. |
| 48 | + readListInv, err := invocation.Invoke( |
| 49 | + alice, |
| 50 | + mailingList, |
| 51 | + "/emails/list", |
| 52 | + ipld.Map{"limit": 100}, |
| 53 | + invocation.WithAudience(mailer), |
| 54 | + invocation.WithProofs(listEmailsDlg.Link()), |
| 55 | + ) |
| 56 | + if err != nil { |
| 57 | + panic(err) |
| 58 | + } |
| 59 | + |
| 60 | + // Send a test email to the list. |
| 61 | + // This invocation is blocked on the successful result of the `/emails/list` |
| 62 | + // task above, due to the `await/ok` promise. |
| 63 | + msgSendInv, err := invocation.Invoke( |
| 64 | + alice, |
| 65 | + mailer, |
| 66 | + "/msg/send", |
| 67 | + ipld.Map{ |
| 68 | + "from": "alice@example.com", |
| 69 | + "to": ipld.Map{"await/ok": readListInv.Task().Link()}, |
| 70 | + "message": "test", |
| 71 | + }, |
| 72 | + invocation.WithAudience(mailer), |
| 73 | + invocation.WithProofs(msgSendDlg.Link()), |
| 74 | + ) |
| 75 | + if err != nil { |
| 76 | + panic(err) |
| 77 | + } |
| 78 | + fmt.Println(msgSendInv.Link()) |
| 79 | + |
| 80 | + // Now send these invocations to the mailer for execution. You'll need to also |
| 81 | + // send the two delegations as proof. You may want to use a _container_ for |
| 82 | + // this. See `container_test.go` in this directory. |
| 83 | +} |
| 84 | + |
| 85 | +func TestTypedPromises(t *testing.T) { |
| 86 | + // Define a capability for sending emails |
| 87 | + msgSendCap, err := capability.New[*types.PromisedMsgSendArguments]("/msg/send") |
| 88 | + if err != nil { |
| 89 | + panic(err) |
| 90 | + } |
| 91 | + |
| 92 | + // Define a capability listing emails on a mailing list |
| 93 | + emailListCap, err := capability.New[*types.EmailsListArguments]("/emails/list") |
| 94 | + if err != nil { |
| 95 | + panic(err) |
| 96 | + } |
| 97 | + |
| 98 | + // Mailer is an email service that can send emails |
| 99 | + mailer, err := ed25519.Generate() |
| 100 | + if err != nil { |
| 101 | + panic(err) |
| 102 | + } |
| 103 | + |
| 104 | + alice, err := ed25519.Generate() |
| 105 | + if err != nil { |
| 106 | + panic(err) |
| 107 | + } |
| 108 | + |
| 109 | + // A list of email addresses |
| 110 | + mailingList, err := ed25519.Generate() |
| 111 | + if err != nil { |
| 112 | + panic(err) |
| 113 | + } |
| 114 | + |
| 115 | + // A delegation from the mailer to alice allowing her to send emails |
| 116 | + msgSendDlg, err := msgSendCap.Delegate(mailer, alice, mailer) |
| 117 | + if err != nil { |
| 118 | + panic(err) |
| 119 | + } |
| 120 | + |
| 121 | + // A delegation from the mailing list to alice allowing her to read the emails |
| 122 | + listEmailsDlg, err := emailListCap.Delegate(mailingList, alice, mailingList) |
| 123 | + if err != nil { |
| 124 | + panic(err) |
| 125 | + } |
| 126 | + |
| 127 | + // Read the emails on the mailing list. The mailer stores the email listings |
| 128 | + // so the invocation audience is the mailer. |
| 129 | + readListInv, err := emailListCap.Invoke( |
| 130 | + alice, |
| 131 | + mailingList, |
| 132 | + &types.EmailsListArguments{ |
| 133 | + Limit: uint64(100), |
| 134 | + }, |
| 135 | + invocation.WithAudience(mailer), |
| 136 | + invocation.WithProofs(listEmailsDlg.Link()), |
| 137 | + ) |
| 138 | + if err != nil { |
| 139 | + panic(err) |
| 140 | + } |
| 141 | + |
| 142 | + // Send a test email to the list. |
| 143 | + // This invocation is blocked on the successful result of the `/emails/list` |
| 144 | + // task above, due to the `await/ok` promise. |
| 145 | + msgSendInv, err := msgSendCap.Invoke( |
| 146 | + alice, |
| 147 | + mailer, |
| 148 | + &types.PromisedMsgSendArguments{ |
| 149 | + From: "alice@example.com", |
| 150 | + To: promise.AwaitOK{Task: readListInv.Task().Link()}, |
| 151 | + Message: "test", |
| 152 | + }, |
| 153 | + invocation.WithAudience(mailer), |
| 154 | + invocation.WithProofs(msgSendDlg.Link()), |
| 155 | + ) |
| 156 | + if err != nil { |
| 157 | + panic(err) |
| 158 | + } |
| 159 | + fmt.Println(msgSendInv.Link()) |
| 160 | + |
| 161 | + // Now send these invocations to the mailer for execution. You'll need to also |
| 162 | + // send the two delegations as proof. You may want to use a _container_ for |
| 163 | + // this. See `container_test.go` in this directory. |
| 164 | +} |
0 commit comments