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 (
|
2025-07-31 14:43:42 +02:00
|
|
|
"encoding/base64"
|
2024-11-20 14:55:48 +01:00
|
|
|
"fmt"
|
2024-11-19 14:35:46 -05:00
|
|
|
|
2025-07-31 14:43:42 +02:00
|
|
|
"github.com/MetaMask/go-did-it"
|
|
|
|
|
didkeyctl "github.com/MetaMask/go-did-it/controller/did-key"
|
|
|
|
|
"github.com/MetaMask/go-did-it/crypto"
|
|
|
|
|
"github.com/MetaMask/go-did-it/crypto/ed25519"
|
2024-11-19 14:35:46 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2025-07-31 14:43:42 +02:00
|
|
|
// all are ed25519 as base64
|
|
|
|
|
alicePrivKeyB64 = "zth/9cTSUVwlLzfEWwLCcOkaEmjrRGPOI6mOJksWAYZ3Toe7ymxAzDeiseyxbmEpJ81qYM3dZ8XrXqgonnTTEw=="
|
|
|
|
|
bobPrivKeyB64 = "+p1REV3MkUnLhUMbFe9RcSsmo33TT/FO85yaV+c6fiYJCBsdiwfMwodlkzSAG3sHQIuZj8qnJ678oJucYy7WEg=="
|
|
|
|
|
carolPrivKeyB64 = "aSu3vTwE7z3pXaTaAhVLeizuqnZUJZQHTCSLMLxyZh5LDoZQn80uoQgMEdsbOhR+zIqrjBn5WviGurDkKYVfug=="
|
|
|
|
|
danPrivKeyB64 = "s1zM1av6og3o0UMNbEs/RyezS7Nk/jbSYL2Z+xPEw9Cho/KuEAa75Sf4yJHclLwpKXNucbrZ2scE8Iy8K05KWQ=="
|
|
|
|
|
erinPrivKeyB64 = "+qHpaAR3iivWMEl+pkXmq+uJeHtqFiY++XOXtZ9Tu/WPABCO+eRFrTCLJykJEzAPGFmkJF8HQ7DMwOH7Ry3Aqw=="
|
|
|
|
|
frankPrivKeyB64 = "4k/1N0+Fq73DxmNbGis9PY2KgKxWmtDWhmi1E6sBLuGd7DS0TWjCn1Xa3lXkY49mFszMjhWC+V6DCBf7R68u4Q=="
|
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 (
|
2025-01-13 13:30:03 +01:00
|
|
|
PersonaAlice Persona = iota + 1
|
2024-11-19 14:35:46 -05:00
|
|
|
PersonaBob
|
|
|
|
|
PersonaCarol
|
|
|
|
|
PersonaDan
|
|
|
|
|
PersonaErin
|
|
|
|
|
PersonaFrank
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-31 14:43:42 +02:00
|
|
|
var privKeys map[Persona]crypto.PrivateKeySigningBytes
|
2024-11-20 14:55:48 +01:00
|
|
|
|
|
|
|
|
func init() {
|
2025-07-31 14:43:42 +02:00
|
|
|
privKeys = make(map[Persona]crypto.PrivateKeySigningBytes, 6)
|
|
|
|
|
for persona, pB64 := range privKeyB64() {
|
|
|
|
|
privBytes, err := base64.StdEncoding.DecodeString(pB64)
|
2024-11-20 14:55:48 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-11-19 14:35:46 -05:00
|
|
|
|
2025-07-31 14:43:42 +02:00
|
|
|
privKey, err := ed25519.PrivateKeyFromBytes(privBytes)
|
2024-11-20 14:55:48 +01:00
|
|
|
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 {
|
2025-07-31 14:43:42 +02:00
|
|
|
return didkeyctl.FromPrivateKey(p.PrivKey())
|
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.
|
2025-07-31 14:43:42 +02:00
|
|
|
func (p Persona) PrivKey() crypto.PrivateKeySigningBytes {
|
2024-11-21 15:49:29 +01:00
|
|
|
res, ok := privKeys[p]
|
|
|
|
|
if !ok {
|
|
|
|
|
panic(fmt.Sprintf("Unknown persona: %v", p))
|
|
|
|
|
}
|
|
|
|
|
return res
|
2024-11-19 14:35:46 -05:00
|
|
|
}
|
|
|
|
|
|
2024-12-12 16:05:13 +01:00
|
|
|
func (p Persona) PrivKeyConfig() string {
|
|
|
|
|
res, ok := privKeyB64()[p]
|
|
|
|
|
if !ok {
|
|
|
|
|
panic(fmt.Sprintf("Unknown persona: %v", p))
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 14:35:46 -05:00
|
|
|
// PubKey returns the Ed25519 public key for the Persona.
|
2025-07-31 14:43:42 +02:00
|
|
|
func (p Persona) PubKey() crypto.PublicKey {
|
|
|
|
|
return p.PrivKey().Public()
|
2024-11-19 14:35:46 -05:00
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-04 19:55:29 +01:00
|
|
|
|
|
|
|
|
// DidToName retrieve the persona's name from its DID.
|
|
|
|
|
func DidToName(d did.DID) string {
|
|
|
|
|
return map[did.DID]string{
|
|
|
|
|
PersonaAlice.DID(): "Alice",
|
|
|
|
|
PersonaBob.DID(): "Bob",
|
|
|
|
|
PersonaCarol.DID(): "Carol",
|
|
|
|
|
PersonaDan.DID(): "Dan",
|
|
|
|
|
PersonaErin.DID(): "Erin",
|
|
|
|
|
PersonaFrank.DID(): "Frank",
|
|
|
|
|
}[d]
|
|
|
|
|
}
|