diff --git a/did/didtest/crypto.go b/did/didtest/crypto.go index 513787a..6999b4e 100644 --- a/did/didtest/crypto.go +++ b/did/didtest/crypto.go @@ -5,10 +5,8 @@ package didtest import ( "fmt" - "testing" "github.com/libp2p/go-libp2p/core/crypto" - "github.com/stretchr/testify/require" "github.com/ucan-wg/go-ucan/did" ) @@ -92,6 +90,14 @@ func (p Persona) PrivKey() crypto.PrivKey { return res } +func (p Persona) PrivKeyConfig() string { + res, ok := privKeyB64()[p] + if !ok { + panic(fmt.Sprintf("Unknown persona: %v", p)) + } + return res +} + // PubKey returns the Ed25519 public key for the Persona. func (p Persona) PubKey() crypto.PubKey { return p.PrivKey().GetPublic() @@ -99,10 +105,11 @@ func (p Persona) PubKey() crypto.PubKey { // PubKeyConfig returns the marshaled and encoded Ed25519 public key // for the Persona. -func (p Persona) PubKeyConfig(t *testing.T) string { +func (p Persona) PubKeyConfig() string { pubKeyMar, err := crypto.MarshalPublicKey(p.PrivKey().GetPublic()) - require.NoError(t, err) - + if err != nil { + panic(err) + } return crypto.ConfigEncodeKey(pubKeyMar) } diff --git a/pkg/args/args.go b/pkg/args/args.go index a840c6e..305b9af 100644 --- a/pkg/args/args.go +++ b/pkg/args/args.go @@ -92,6 +92,11 @@ func (a *Args) Include(other Iterator) { } } +// Len return the number of arguments. +func (a *Args) Len() int { + return len(a.Keys) +} + // Iter iterates over the args key/values func (a *Args) Iter() iter.Seq2[string, ipld.Node] { return func(yield func(string, ipld.Node) bool) { diff --git a/pkg/args/readonly.go b/pkg/args/readonly.go index 62c6587..0e4e44f 100644 --- a/pkg/args/readonly.go +++ b/pkg/args/readonly.go @@ -14,6 +14,10 @@ func (r ReadOnly) GetNode(key string) (ipld.Node, error) { return r.args.GetNode(key) } +func (r ReadOnly) Len() int { + return r.args.Len() +} + func (r ReadOnly) Iter() iter.Seq2[string, ipld.Node] { return r.args.Iter() } diff --git a/pkg/meta/meta.go b/pkg/meta/meta.go index 9b0e79f..9273bbc 100644 --- a/pkg/meta/meta.go +++ b/pkg/meta/meta.go @@ -10,8 +10,8 @@ import ( "github.com/ipld/go-ipld-prime" "github.com/ipld/go-ipld-prime/printer" - "github.com/ucan-wg/go-ucan/pkg/meta/internal/crypto" "github.com/ucan-wg/go-ucan/pkg/policy/literal" + "github.com/ucan-wg/go-ucan/pkg/secretbox" ) var ErrNotFound = errors.New("key not found in meta") @@ -63,7 +63,7 @@ func (m *Meta) GetEncryptedString(key string, encryptionKey []byte) (string, err return "", err } - decrypted, err := crypto.DecryptStringWithKey(v, encryptionKey) + decrypted, err := secretbox.DecryptStringWithKey(v, encryptionKey) if err != nil { return "", err } @@ -111,7 +111,7 @@ func (m *Meta) GetEncryptedBytes(key string, encryptionKey []byte) ([]byte, erro return nil, err } - decrypted, err := crypto.DecryptStringWithKey(v, encryptionKey) + decrypted, err := secretbox.DecryptStringWithKey(v, encryptionKey) if err != nil { return nil, err } @@ -157,12 +157,12 @@ func (m *Meta) AddEncrypted(key string, val any, encryptionKey []byte) error { switch val := val.(type) { case string: - encrypted, err = crypto.EncryptWithKey([]byte(val), encryptionKey) + encrypted, err = secretbox.EncryptWithKey([]byte(val), encryptionKey) if err != nil { return err } case []byte: - encrypted, err = crypto.EncryptWithKey(val, encryptionKey) + encrypted, err = secretbox.EncryptWithKey(val, encryptionKey) if err != nil { return err } @@ -192,6 +192,11 @@ func (m *Meta) Include(other Iterator) { } } +// Len returns the number of key/values. +func (m *Meta) Len() int { + return len(m.Values) +} + // Iter iterates over the meta key/values func (m *Meta) Iter() iter.Seq2[string, ipld.Node] { return func(yield func(string, ipld.Node) bool) { diff --git a/pkg/meta/readonly.go b/pkg/meta/readonly.go index 26d667d..4e6ddfb 100644 --- a/pkg/meta/readonly.go +++ b/pkg/meta/readonly.go @@ -43,6 +43,10 @@ func (r ReadOnly) GetNode(key string) (ipld.Node, error) { return r.meta.GetNode(key) } +func (r ReadOnly) Len() int { + return r.meta.Len() +} + func (r ReadOnly) Iter() iter.Seq2[string, ipld.Node] { return r.meta.Iter() } diff --git a/pkg/meta/internal/crypto/secretbox.go b/pkg/secretbox/secretbox.go similarity index 99% rename from pkg/meta/internal/crypto/secretbox.go rename to pkg/secretbox/secretbox.go index 690be7e..227a333 100644 --- a/pkg/meta/internal/crypto/secretbox.go +++ b/pkg/secretbox/secretbox.go @@ -1,4 +1,4 @@ -package crypto +package secretbox import ( "crypto/rand" diff --git a/pkg/meta/internal/crypto/secretbox_test.go b/pkg/secretbox/secretbox_test.go similarity index 99% rename from pkg/meta/internal/crypto/secretbox_test.go rename to pkg/secretbox/secretbox_test.go index d87f860..df8ab89 100644 --- a/pkg/meta/internal/crypto/secretbox_test.go +++ b/pkg/secretbox/secretbox_test.go @@ -1,4 +1,4 @@ -package crypto +package secretbox import ( "bytes"