Skip to content

Commit ba24770

Browse files
committed
add a didtest package for easier testing of DID based systems
1 parent b520568 commit ba24770

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

didtest/didtest.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Package didtest provides Personas that can be used for testing. Each
2+
// Persona has a name, crypto.PrivKey and associated crypto.PubKey and
3+
// did.DID.
4+
package didtest
5+
6+
import (
7+
"encoding/base64"
8+
"fmt"
9+
10+
"github.com/MetaMask/go-did-it"
11+
didkeyctl "github.com/MetaMask/go-did-it/controller/did-key"
12+
"github.com/MetaMask/go-did-it/crypto"
13+
"github.com/MetaMask/go-did-it/crypto/ed25519"
14+
)
15+
16+
const (
17+
// all are ed25519 as base64
18+
alicePrivKeyB64 = "zth/9cTSUVwlLzfEWwLCcOkaEmjrRGPOI6mOJksWAYZ3Toe7ymxAzDeiseyxbmEpJ81qYM3dZ8XrXqgonnTTEw=="
19+
bobPrivKeyB64 = "+p1REV3MkUnLhUMbFe9RcSsmo33TT/FO85yaV+c6fiYJCBsdiwfMwodlkzSAG3sHQIuZj8qnJ678oJucYy7WEg=="
20+
carolPrivKeyB64 = "aSu3vTwE7z3pXaTaAhVLeizuqnZUJZQHTCSLMLxyZh5LDoZQn80uoQgMEdsbOhR+zIqrjBn5WviGurDkKYVfug=="
21+
danPrivKeyB64 = "s1zM1av6og3o0UMNbEs/RyezS7Nk/jbSYL2Z+xPEw9Cho/KuEAa75Sf4yJHclLwpKXNucbrZ2scE8Iy8K05KWQ=="
22+
erinPrivKeyB64 = "+qHpaAR3iivWMEl+pkXmq+uJeHtqFiY++XOXtZ9Tu/WPABCO+eRFrTCLJykJEzAPGFmkJF8HQ7DMwOH7Ry3Aqw=="
23+
frankPrivKeyB64 = "4k/1N0+Fq73DxmNbGis9PY2KgKxWmtDWhmi1E6sBLuGd7DS0TWjCn1Xa3lXkY49mFszMjhWC+V6DCBf7R68u4Q=="
24+
)
25+
26+
// Persona is a generic participant used for cryptographic testing.
27+
type Persona int
28+
29+
// The provided Personas were selected from the first few generic
30+
// participants listed in this [table].
31+
//
32+
// [table]: https://en.wikipedia.org/wiki/Alice_and_Bob#Cryptographic_systems
33+
const (
34+
PersonaAlice Persona = iota + 1
35+
PersonaBob
36+
PersonaCarol
37+
PersonaDan
38+
PersonaErin
39+
PersonaFrank
40+
)
41+
42+
var privKeys map[Persona]crypto.PrivateKeySigningBytes
43+
44+
func init() {
45+
privKeys = make(map[Persona]crypto.PrivateKeySigningBytes, 6)
46+
for persona, pB64 := range privKeyB64() {
47+
privBytes, err := base64.StdEncoding.DecodeString(pB64)
48+
if err != nil {
49+
return
50+
}
51+
52+
privKey, err := ed25519.PrivateKeyFromBytes(privBytes)
53+
if err != nil {
54+
return
55+
}
56+
57+
privKeys[persona] = privKey
58+
}
59+
}
60+
61+
// DID returns a did.DID based on the Persona's Ed25519 public key.
62+
func (p Persona) DID() did.DID {
63+
return didkeyctl.FromPrivateKey(p.PrivKey())
64+
}
65+
66+
// Name returns the username of the Persona.
67+
func (p Persona) Name() string {
68+
name, ok := map[Persona]string{
69+
PersonaAlice: "Alice",
70+
PersonaBob: "Bob",
71+
PersonaCarol: "Carol",
72+
PersonaDan: "Dan",
73+
PersonaErin: "Erin",
74+
PersonaFrank: "Frank",
75+
}[p]
76+
if !ok {
77+
panic(fmt.Sprintf("Unknown persona: %v", p))
78+
}
79+
80+
return name
81+
}
82+
83+
// PrivKey returns the Ed25519 private key for the Persona.
84+
func (p Persona) PrivKey() crypto.PrivateKeySigningBytes {
85+
res, ok := privKeys[p]
86+
if !ok {
87+
panic(fmt.Sprintf("Unknown persona: %v", p))
88+
}
89+
return res
90+
}
91+
92+
// PubKey returns the Ed25519 public key for the Persona.
93+
func (p Persona) PubKey() crypto.PublicKey {
94+
return p.PrivKey().Public()
95+
}
96+
97+
func privKeyB64() map[Persona]string {
98+
return map[Persona]string{
99+
PersonaAlice: alicePrivKeyB64,
100+
PersonaBob: bobPrivKeyB64,
101+
PersonaCarol: carolPrivKeyB64,
102+
PersonaDan: danPrivKeyB64,
103+
PersonaErin: erinPrivKeyB64,
104+
PersonaFrank: frankPrivKeyB64,
105+
}
106+
}
107+
108+
// Personas returns an (alphabetically) ordered list of the defined
109+
// Persona values.
110+
func Personas() []Persona {
111+
return []Persona{
112+
PersonaAlice,
113+
PersonaBob,
114+
PersonaCarol,
115+
PersonaDan,
116+
PersonaErin,
117+
PersonaFrank,
118+
}
119+
}
120+
121+
// DidToName retrieve the persona's name from its DID.
122+
func DidToName(d did.DID) string {
123+
return map[did.DID]string{
124+
PersonaAlice.DID(): "Alice",
125+
PersonaBob.DID(): "Bob",
126+
PersonaCarol.DID(): "Carol",
127+
PersonaDan.DID(): "Dan",
128+
PersonaErin.DID(): "Erin",
129+
PersonaFrank.DID(): "Frank",
130+
}[d]
131+
}

0 commit comments

Comments
 (0)