2024-11-19 14:35:46 -05:00
|
|
|
// Package didtest provides Personas that can be used for testing. Each
|
|
|
|
|
// Persona has a name, crypto.PrivKey and associated crypto.PubKey and
|
|
|
|
|
// did.DID.
|
|
|
|
|
package didtest
|
|
|
|
|
|
|
|
|
|
import (
|
2024-11-20 14:55:48 +01:00
|
|
|
"fmt"
|
2024-11-19 14:35:46 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-11-20 14:55:48 +01:00
|
|
|
|
2024-11-19 14:35:46 -05:00
|
|
|
"github.com/ucan-wg/go-ucan/did"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2024-11-20 14:55:48 +01:00
|
|
|
alicePrivKeyB64 = "CAESQHdNJLBBiuc1AdwPHBkubB2KS1p0cv2JEF7m8tfwtrcm5ajaYPm+XmVCmtcHOF2lGDlmaiDA7emfwD3IrcyES0M="
|
|
|
|
|
bobPrivKeyB64 = "CAESQHBz+AIop1g+9iBDj+ufUc/zm9/ry7c6kDFO8Wl/D0+H63V9hC6s9l4npf3pYEFCjBtlR0AMNWMoFQKSlYNKo20="
|
|
|
|
|
carolPrivKeyB64 = "CAESQPrCgkcHnYFXDT9AlAydhPECBEivEuuVx9dJxLjVvDTmJIVNivfzg6H4mAiPfYS+5ryVVUZTHZBzvMuvvvG/Ks0="
|
|
|
|
|
danPrivKeyB64 = "CAESQCgNhzofKhC+7hW6x+fNd7iMPtQHeEmKRhhlduf/I7/TeOEFYAEflbJ0sAhMeDJ/HQXaAvsWgHEbJ3ZLhP8q2B0="
|
|
|
|
|
erinPrivKeyB64 = "CAESQKhCJo5UBpQcthko8DKMFsbdZ+qqQ5oc01CtLCqrE90dF2GfRlrMmot3WPHiHGCmEYi5ZMEHuiSI095e/6O4Bpw="
|
|
|
|
|
frankPrivKeyB64 = "CAESQDlXPKsy3jHh7OWTWQqyZF95Ueac5DKo7xD0NOBE5F2BNr1ZVxRmJ2dBELbOt8KP9sOACcO9qlCB7uMA1UQc7sk="
|
2024-11-19 14:35:46 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Persona is a generic participant used for cryptographic testing.
|
|
|
|
|
type Persona int
|
|
|
|
|
|
2024-11-20 14:55:48 +01:00
|
|
|
// The provided Personas were selected from the first few generic
|
2024-11-19 14:35:46 -05:00
|
|
|
// participants listed in this [table].
|
|
|
|
|
//
|
|
|
|
|
// [table]: https://en.wikipedia.org/wiki/Alice_and_Bob#Cryptographic_systems
|
|
|
|
|
const (
|
|
|
|
|
PersonaAlice Persona = iota
|
|
|
|
|
PersonaBob
|
|
|
|
|
PersonaCarol
|
|
|
|
|
PersonaDan
|
|
|
|
|
PersonaErin
|
|
|
|
|
PersonaFrank
|
|
|
|
|
)
|
|
|
|
|
|
2024-11-20 14:55:48 +01:00
|
|
|
var privKeys map[Persona]crypto.PrivKey
|
|
|
|
|
|
|
|
|
|
func init() {
|
2024-11-19 14:35:46 -05:00
|
|
|
privKeys = make(map[Persona]crypto.PrivKey, 6)
|
2024-11-20 14:55:48 +01:00
|
|
|
for persona, privKeyCfg := range privKeyB64() {
|
|
|
|
|
privKeyMar, err := crypto.ConfigDecodeKey(privKeyCfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-11-19 14:35:46 -05:00
|
|
|
|
2024-11-20 14:55:48 +01:00
|
|
|
privKey, err := crypto.UnmarshalPrivateKey(privKeyMar)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-11-19 14:35:46 -05:00
|
|
|
|
2024-11-20 14:55:48 +01:00
|
|
|
privKeys[persona] = privKey
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-19 14:35:46 -05:00
|
|
|
|
2024-11-20 14:55:48 +01:00
|
|
|
// DID returns a did.DID based on the Persona's Ed25519 public key.
|
|
|
|
|
func (p Persona) DID() did.DID {
|
|
|
|
|
d, err := did.FromPrivKey(p.PrivKey())
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return d
|
2024-11-19 14:35:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name returns the username of the Persona.
|
2024-11-20 14:55:48 +01:00
|
|
|
func (p Persona) Name() string {
|
2024-11-19 14:35:46 -05:00
|
|
|
name, ok := map[Persona]string{
|
|
|
|
|
PersonaAlice: "Alice",
|
|
|
|
|
PersonaBob: "Bob",
|
|
|
|
|
PersonaCarol: "Carol",
|
|
|
|
|
PersonaDan: "Dan",
|
|
|
|
|
PersonaErin: "Erin",
|
|
|
|
|
PersonaFrank: "Frank",
|
|
|
|
|
}[p]
|
|
|
|
|
if !ok {
|
2024-11-20 14:55:48 +01:00
|
|
|
panic(fmt.Sprintf("Unknown persona: %v", p))
|
2024-11-19 14:35:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PrivKey returns the Ed25519 private key for the Persona.
|
2024-11-20 14:55:48 +01:00
|
|
|
func (p Persona) PrivKey() crypto.PrivKey {
|
2024-11-19 14:35:46 -05:00
|
|
|
return privKeys[p]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PubKey returns the Ed25519 public key for the Persona.
|
2024-11-20 14:55:48 +01:00
|
|
|
func (p Persona) PubKey() crypto.PubKey {
|
|
|
|
|
return p.PrivKey().GetPublic()
|
2024-11-19 14:35:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PubKeyConfig returns the marshaled and encoded Ed25519 public key
|
|
|
|
|
// for the Persona.
|
|
|
|
|
func (p Persona) PubKeyConfig(t *testing.T) string {
|
2024-11-20 14:55:48 +01:00
|
|
|
pubKeyMar, err := crypto.MarshalPublicKey(p.PrivKey().GetPublic())
|
2024-11-19 14:35:46 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
return crypto.ConfigEncodeKey(pubKeyMar)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-20 14:55:48 +01:00
|
|
|
func privKeyB64() map[Persona]string {
|
2024-11-19 14:35:46 -05:00
|
|
|
return map[Persona]string{
|
2024-11-20 14:55:48 +01:00
|
|
|
PersonaAlice: alicePrivKeyB64,
|
|
|
|
|
PersonaBob: bobPrivKeyB64,
|
|
|
|
|
PersonaCarol: carolPrivKeyB64,
|
|
|
|
|
PersonaDan: danPrivKeyB64,
|
|
|
|
|
PersonaErin: erinPrivKeyB64,
|
|
|
|
|
PersonaFrank: frankPrivKeyB64,
|
2024-11-19 14:35:46 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Personas returns an (alphabetically) ordered list of the defined
|
|
|
|
|
// Persona values.
|
2024-11-20 14:55:48 +01:00
|
|
|
func Personas() []Persona {
|
2024-11-19 14:35:46 -05:00
|
|
|
return []Persona{
|
|
|
|
|
PersonaAlice,
|
|
|
|
|
PersonaBob,
|
|
|
|
|
PersonaCarol,
|
|
|
|
|
PersonaDan,
|
|
|
|
|
PersonaErin,
|
|
|
|
|
PersonaFrank,
|
|
|
|
|
}
|
|
|
|
|
}
|