adjust the toolkit to the new location

This commit is contained in:
Michael Muré
2025-08-05 12:11:20 +02:00
committed by Michael Muré
parent 06f478b9c3
commit 0647e4ff8a
47 changed files with 233 additions and 828 deletions

View File

@@ -5,9 +5,10 @@ import (
"fmt"
"iter"
"github.com/MetaMask/go-did-it"
"github.com/MetaMask/go-did-it/crypto"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/pkg/container"
"github.com/ucan-wg/go-ucan/pkg/policy"
@@ -17,17 +18,13 @@ import (
type Client struct {
did did.DID
privKey crypto.PrivKey
privKey crypto.PrivateKeySigningBytes
pool *Pool
requester DelegationRequester
}
func NewClient(privKey crypto.PrivKey, requester DelegationRequester) (*Client, error) {
d, err := did.FromPrivKey(privKey)
if err != nil {
return nil, err
}
func NewClient(privKey crypto.PrivateKeySigningBytes, d did.DID, requester DelegationRequester) (*Client, error) {
return &Client{
did: d,
privKey: privKey,

View File

@@ -6,8 +6,9 @@ import (
"iter"
"time"
"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/did/didtest"
"github.com/MetaMask/go-did-it"
"github.com/MetaMask/go-did-it/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"
@@ -21,7 +22,7 @@ func ExampleNewClient() {
// 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)
client, err := NewClient(clientPersona.PrivKey(), clientPersona.DID(), requester)
handleError(err)
cont, err := client.PrepareInvoke(

View File

@@ -5,9 +5,10 @@ import (
"fmt"
"iter"
"github.com/MetaMask/go-did-it"
"github.com/MetaMask/go-did-it/crypto"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/token/delegation"
)
@@ -29,8 +30,8 @@ type WithIssuer struct {
logic DlgIssuingLogic
}
func NewWithIssuer(privKey crypto.PrivKey, requester DelegationRequester, logic DlgIssuingLogic) (*WithIssuer, error) {
client, err := NewClient(privKey, requester)
func NewWithIssuer(privKey crypto.PrivateKeySigningBytes, d did.DID, requester DelegationRequester, logic DlgIssuingLogic) (*WithIssuer, error) {
client, err := NewClient(privKey, d, requester)
if err != nil {
return nil, err
}

View File

@@ -6,8 +6,9 @@ import (
"sync"
"time"
"github.com/MetaMask/go-did-it"
"github.com/ipfs/go-cid"
"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/token/delegation"
)

View File

@@ -4,8 +4,9 @@ import (
"iter"
"math"
"github.com/MetaMask/go-did-it"
"github.com/ipfs/go-cid"
"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/token/delegation"
)
@@ -23,12 +24,12 @@ func FindProof(dlgs func() iter.Seq[*delegation.Bundle], issuer did.DID, cmd com
// TODO: maybe that should be part of delegation.Token directly?
dlgMatch := func(dlg *delegation.Token, issuer did.DID, cmd command.Command, subject did.DID) bool {
// The Subject of each delegation must equal the invocation's Subject (or Audience if defined). - 4f
if dlg.Subject() != subject {
if !dlg.Subject().Equal(subject) {
return false
}
// The first proof must be issued to the Invoker (audience DID). - 4c
// The Issuer of each delegation must be the Audience in the next one. - 4d
if dlg.Audience() != issuer {
if !dlg.Audience().Equal(issuer) {
return false
}
// The command of each delegation must "allow" the one before it. - 4g
@@ -72,7 +73,7 @@ func FindProof(dlgs func() iter.Seq[*delegation.Bundle], issuer did.DID, cmd com
at := cur.bundle
// if it's a root delegation, we found a valid proof
if at.Decoded.Issuer() == at.Decoded.Subject() {
if at.Decoded.Issuer().Equal(at.Decoded.Subject()) {
if len(bestProof) == 0 || len(cur.path) < len(bestProof) || len(cur.path) == len(bestProof) && cur.size < bestSize {
bestProof = append([]cid.Cid{}, cur.path...) // make a copy
bestSize = cur.size

View File

@@ -4,8 +4,9 @@ import (
"iter"
"testing"
"github.com/MetaMask/go-did-it/didtest"
"github.com/stretchr/testify/require"
"github.com/ucan-wg/go-ucan/did/didtest"
"github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/token/delegation"
"github.com/ucan-wg/go-ucan/token/delegation/delegationtest"

View File

@@ -5,8 +5,9 @@ import (
"iter"
"time"
"github.com/MetaMask/go-did-it"
"github.com/avast/retry-go/v4"
"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/token/delegation"
)

View File

@@ -1,99 +0,0 @@
package client
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"iter"
"net/http"
"net/url"
"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/token/delegation"
)
var _ DelegationRequester = &InfuraRequester{}
type InfuraRequester struct {
baseURL string
}
// NewInfuraRequester create a requester client for the Infura UCAN token issuer.
// dev: http://ucan-issuer-api.commercial-dev.eks-dev.infura.org
// prod: http://ucan-issuer-api.commercial-prod.eks.infura.org
func NewInfuraRequester(baseURL string) *InfuraRequester {
return &InfuraRequester{baseURL: baseURL}
}
func (i InfuraRequester) RequestDelegation(ctx context.Context, audience did.DID, cmd command.Command, subject did.DID) (iter.Seq2[*delegation.Bundle, error], error) {
p, err := url.JoinPath(i.baseURL, "v1/token/generate-with-did")
if err != nil {
return nil, err
}
payload := struct {
Cmd string `json:"cmd"`
Aud string `json:"aud"`
}{
Cmd: cmd.String(),
Aud: audience.String(),
}
body, err := json.Marshal(payload)
if err != nil {
return nil, err
}
r, err := http.NewRequest(http.MethodPost, p, bytes.NewReader(body))
if err != nil {
return nil, err
}
res, err := http.DefaultClient.Do(r.WithContext(ctx))
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
msg, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("request failed with status %d, then failed to read response body: %w", res.StatusCode, err)
}
return nil, fmt.Errorf("request failed with status %d: %s", res.StatusCode, msg)
}
resp := struct {
Cid string `json:"cid"`
Content string `json:"content"`
}{}
if err := json.NewDecoder(res.Body).Decode(&resp); err != nil {
return nil, err
}
raw, err := base64.StdEncoding.DecodeString(resp.Content)
if err != nil {
return nil, err
}
tkn, c, err := delegation.FromSealed(raw)
if err != nil {
return nil, err
}
// For sanity, we verify that the delegation we got matches the expected subject,
// meaning that we are talking to the expected issuer.
if tkn.Subject() != subject {
return nil, fmt.Errorf("received token has unexpected subject: expected %s, got %s", subject, tkn.Subject())
}
return func(yield func(*delegation.Bundle, error) bool) {
yield(&delegation.Bundle{
Cid: c,
Decoded: tkn,
Sealed: raw,
}, nil)
}, nil
}