client: follow go-ucan changes, improvements, example
This commit is contained in:
committed by
Michael Muré
parent
1187674a24
commit
2eeaaccc6d
76
toolkit/client/client_test.go
Normal file
76
toolkit/client/client_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"iter"
|
||||
"time"
|
||||
|
||||
"github.com/ucan-wg/go-ucan/did"
|
||||
"github.com/ucan-wg/go-ucan/did/didtest"
|
||||
"github.com/ucan-wg/go-ucan/pkg/command"
|
||||
"github.com/ucan-wg/go-ucan/pkg/policy"
|
||||
"github.com/ucan-wg/go-ucan/token/delegation"
|
||||
"github.com/ucan-wg/go-ucan/token/invocation"
|
||||
)
|
||||
|
||||
func ExampleNewClient() {
|
||||
servicePersona := didtest.PersonaAlice
|
||||
clientPersona := didtest.PersonaBob
|
||||
|
||||
// requester is an adaptor for a real world issuer, we use a mock in that example
|
||||
requester := &requesterMock{persona: servicePersona}
|
||||
|
||||
client, err := NewClient(clientPersona.PrivKey(), requester)
|
||||
handleError(err)
|
||||
|
||||
cont, err := client.PrepareInvoke(
|
||||
context.Background(),
|
||||
command.New("crud", "add"),
|
||||
servicePersona.DID(),
|
||||
// extra invocation parameters:
|
||||
invocation.WithExpirationIn(10*time.Minute),
|
||||
invocation.WithArgument("foo", "bar"),
|
||||
invocation.WithMeta("baz", 1234),
|
||||
)
|
||||
handleError(err)
|
||||
|
||||
// this container holds the invocation and all the delegation proofs
|
||||
b64, err := cont.ToCborBase64()
|
||||
handleError(err)
|
||||
|
||||
fmt.Println(string(b64))
|
||||
}
|
||||
|
||||
func handleError(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type requesterMock struct {
|
||||
persona didtest.Persona
|
||||
}
|
||||
|
||||
func (r requesterMock) RequestDelegation(_ context.Context, audience did.DID, cmd command.Command, _ did.DID) (iter.Seq2[*delegation.Bundle, error], error) {
|
||||
// the mock issue whatever the client asks:
|
||||
dlg, err := delegation.Root(r.persona.DID(), audience, cmd, policy.Policy{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dlgBytes, dlgCid, err := dlg.ToSealed(r.persona.PrivKey())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bundle := &delegation.Bundle{
|
||||
Cid: dlgCid,
|
||||
Decoded: dlg,
|
||||
Sealed: dlgBytes,
|
||||
}
|
||||
|
||||
return func(yield func(*delegation.Bundle, error) bool) {
|
||||
yield(bundle, nil)
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user