Merge pull request #95 from ucan-wg/minor-impro

Minor impro
This commit is contained in:
Michael Muré
2024-12-12 16:36:17 +01:00
committed by GitHub
7 changed files with 37 additions and 12 deletions

View File

@@ -5,10 +5,8 @@ package didtest
import ( import (
"fmt" "fmt"
"testing"
"github.com/libp2p/go-libp2p/core/crypto" "github.com/libp2p/go-libp2p/core/crypto"
"github.com/stretchr/testify/require"
"github.com/ucan-wg/go-ucan/did" "github.com/ucan-wg/go-ucan/did"
) )
@@ -92,6 +90,14 @@ func (p Persona) PrivKey() crypto.PrivKey {
return res 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. // PubKey returns the Ed25519 public key for the Persona.
func (p Persona) PubKey() crypto.PubKey { func (p Persona) PubKey() crypto.PubKey {
return p.PrivKey().GetPublic() return p.PrivKey().GetPublic()
@@ -99,10 +105,11 @@ func (p Persona) PubKey() crypto.PubKey {
// PubKeyConfig returns the marshaled and encoded Ed25519 public key // PubKeyConfig returns the marshaled and encoded Ed25519 public key
// for the Persona. // for the Persona.
func (p Persona) PubKeyConfig(t *testing.T) string { func (p Persona) PubKeyConfig() string {
pubKeyMar, err := crypto.MarshalPublicKey(p.PrivKey().GetPublic()) pubKeyMar, err := crypto.MarshalPublicKey(p.PrivKey().GetPublic())
require.NoError(t, err) if err != nil {
panic(err)
}
return crypto.ConfigEncodeKey(pubKeyMar) return crypto.ConfigEncodeKey(pubKeyMar)
} }

View File

@@ -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 // Iter iterates over the args key/values
func (a *Args) Iter() iter.Seq2[string, ipld.Node] { func (a *Args) Iter() iter.Seq2[string, ipld.Node] {
return func(yield func(string, ipld.Node) bool) { return func(yield func(string, ipld.Node) bool) {

View File

@@ -14,6 +14,10 @@ func (r ReadOnly) GetNode(key string) (ipld.Node, error) {
return r.args.GetNode(key) return r.args.GetNode(key)
} }
func (r ReadOnly) Len() int {
return r.args.Len()
}
func (r ReadOnly) Iter() iter.Seq2[string, ipld.Node] { func (r ReadOnly) Iter() iter.Seq2[string, ipld.Node] {
return r.args.Iter() return r.args.Iter()
} }

View File

@@ -10,8 +10,8 @@ import (
"github.com/ipld/go-ipld-prime" "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/printer" "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/policy/literal"
"github.com/ucan-wg/go-ucan/pkg/secretbox"
) )
var ErrNotFound = errors.New("key not found in meta") 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 return "", err
} }
decrypted, err := crypto.DecryptStringWithKey(v, encryptionKey) decrypted, err := secretbox.DecryptStringWithKey(v, encryptionKey)
if err != nil { if err != nil {
return "", err return "", err
} }
@@ -111,7 +111,7 @@ func (m *Meta) GetEncryptedBytes(key string, encryptionKey []byte) ([]byte, erro
return nil, err return nil, err
} }
decrypted, err := crypto.DecryptStringWithKey(v, encryptionKey) decrypted, err := secretbox.DecryptStringWithKey(v, encryptionKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -157,12 +157,12 @@ func (m *Meta) AddEncrypted(key string, val any, encryptionKey []byte) error {
switch val := val.(type) { switch val := val.(type) {
case string: case string:
encrypted, err = crypto.EncryptWithKey([]byte(val), encryptionKey) encrypted, err = secretbox.EncryptWithKey([]byte(val), encryptionKey)
if err != nil { if err != nil {
return err return err
} }
case []byte: case []byte:
encrypted, err = crypto.EncryptWithKey(val, encryptionKey) encrypted, err = secretbox.EncryptWithKey(val, encryptionKey)
if err != nil { if err != nil {
return err 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 // Iter iterates over the meta key/values
func (m *Meta) Iter() iter.Seq2[string, ipld.Node] { func (m *Meta) Iter() iter.Seq2[string, ipld.Node] {
return func(yield func(string, ipld.Node) bool) { return func(yield func(string, ipld.Node) bool) {

View File

@@ -43,6 +43,10 @@ func (r ReadOnly) GetNode(key string) (ipld.Node, error) {
return r.meta.GetNode(key) return r.meta.GetNode(key)
} }
func (r ReadOnly) Len() int {
return r.meta.Len()
}
func (r ReadOnly) Iter() iter.Seq2[string, ipld.Node] { func (r ReadOnly) Iter() iter.Seq2[string, ipld.Node] {
return r.meta.Iter() return r.meta.Iter()
} }

View File

@@ -1,4 +1,4 @@
package crypto package secretbox
import ( import (
"crypto/rand" "crypto/rand"

View File

@@ -1,4 +1,4 @@
package crypto package secretbox
import ( import (
"bytes" "bytes"