Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
206 lines (178 loc) · 7.92 KB
/
Copy pathexample_test.go
File metadata and controls
206 lines (178 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package solidus_test
import (
"context"
"crypto/ed25519"
"encoding/hex"
"fmt"
"net/http"
"net/http/httptest"
"strings"
solidus "github.com/solidusnetwork/solidus-go"
)
// Example functions are Go's native doc gate: `go test` runs each one and
// compares its stdout against the `// Output:` block, and `go doc` renders them
// alongside the API. An example that stops being true is a failing test, not a
// stale paragraph — which is the whole point, given that this branch exists
// because an outsider found four README blocks that did not compile.
// The Trezor all-`abandon` mnemonic. Every value below it is frozen by
// test-vectors/did/derivation-v1.json.
const exampleMnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon " +
"abandon abandon abandon abandon abandon abandon abandon abandon " +
"abandon abandon abandon abandon abandon abandon abandon art"
func ExampleIdentityKey() {
seed := solidus.SeedFromMnemonic(exampleMnemonic, "")
key := solidus.IdentityKey(seed)
fmt.Println(key.DID(solidus.Testnet))
// Output: did:solidus:testnet:3tBoVe6XRtirzr8SdRotGgbkuEQN
}
// A wallet derives a distinct key per verifier, so two verifiers holding the
// same user cannot correlate them.
func ExamplePairwiseKey() {
seed := solidus.SeedFromMnemonic(exampleMnemonic, "")
a := solidus.PairwiseKey(seed, "rp-a.example.com")
b := solidus.PairwiseKey(seed, "rp-b.example.com")
fmt.Println(a.DID(solidus.Testnet))
fmt.Println("unlinkable:", a.Identifier() != b.Identifier())
// Output:
// did:solidus:testnet:3ThmUf3VBefVuaQSBGzC1fcP5iKS
// unlinkable: true
}
// PublicKeyMultibase carries the ed25519-pub multicodec header (0xed01), which
// is what makes the `z6Mk` prefix appear. Omitting it produces a string that
// round-trips with itself and is rejected by every other implementation — which
// is exactly what this project shipped until 2026-08-07.
func ExamplePublicKeyMultibase() {
key, _ := hex.DecodeString("248acbdbaf9e050196de704bea2d68770e519150d103b587dae2d9cad53dd930")
mb := solidus.PublicKeyMultibase(key)
back, err := solidus.DecodePublicKeyMultibase(mb)
fmt.Println(mb)
fmt.Println("round-trips:", err == nil && hex.EncodeToString(back) == hex.EncodeToString(key))
// Output:
// z6MkguuUVNj72BEqpY3qHikwFtSCiUWgDGbwiX8pqu5uh3gK
// round-trips: true
}
// Verification is strict: it rejects small-order keys, which Go's standard
// library does not.
//
// The all-zeros encoding is a point of order 4, so crypto/ed25519 accepts it
// for roughly one message in four — whenever k = SHA-512(R‖A‖M) is divisible
// by 4. An attacker grinds four messages. VerifyStrict rejects it for all of
// them, which is what the count below shows.
func ExampleVerifyStrict() {
priv := ed25519.NewKeyFromSeed(make([]byte, ed25519.SeedSize))
pub := priv.Public().(ed25519.PublicKey)
message := []byte("a credential")
zeroKey, zeroSig := make([]byte, 32), make([]byte, 64)
stdlibAccepted, strictAccepted := 0, 0
for i := 0; i < 100; i++ {
m := []byte(fmt.Sprintf("msg-%d", i))
if ed25519.Verify(zeroKey, m, zeroSig) {
stdlibAccepted++
}
if solidus.VerifyStrict(zeroKey, m, zeroSig) {
strictAccepted++
}
}
fmt.Println("valid signature verifies:", solidus.VerifyStrict(pub, message, ed25519.Sign(priv, message)))
fmt.Println("crypto/ed25519 accepted the all-zeros key more than once:", stdlibAccepted > 1)
fmt.Println("VerifyStrict accepted it, out of 100 messages:", strictAccepted)
// Output:
// valid signature verifies: true
// crypto/ed25519 accepted the all-zeros key more than once: true
// VerifyStrict accepted it, out of 100 messages: 0
}
// A malformed identifier is invalidDid, never notFound: "no such DID" implies
// the identifier could have existed. The network segment is a literal `testnet`
// or `mainnet`, not a chain id.
func ExampleIsValidDID() {
for _, did := range []string{
"did:solidus:testnet:3tBoVe6XRtirzr8SdRotGgbkuEQN",
"did:solidus:1:3tBoVe6XRtirzr8SdRotGgbkuEQN",
"did:solidus:testnet:tooshort",
} {
fmt.Printf("%-5v %s\n", solidus.IsValidDID(did), did)
}
// Output:
// true did:solidus:testnet:3tBoVe6XRtirzr8SdRotGgbkuEQN
// false did:solidus:1:3tBoVe6XRtirzr8SdRotGgbkuEQN
// false did:solidus:testnet:tooshort
}
// The signature is frozen by test-vectors/did/tx-create-v1.json. The preimage
// hashes the payload's JSON serialisation, so serde's output is
// consensus-relevant — see tx.go.
func ExampleSignDIDCreate() {
privateKey := make([]byte, 32)
for i := range privateKey {
privateKey[i] = 0xab
}
tx, err := solidus.SignDIDCreate(privateKey, 0, nil)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(hex.EncodeToString(tx.Signature)[:32])
// Output: 7b6f809c46a4a136515d532bffb33151
}
// Resolution never returns an error for a DID that does not exist — that is a
// successful resolution carrying notFound. Errors are reserved for a node that
// could not be reached or did not speak JSON-RPC, so a caller can tell an
// outage from a missing user.
func ExampleResolver_Resolve() {
// Stands in for a real node so this example is deterministic.
node := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"jsonrpc":"2.0","id":1,"result":null}`))
}))
defer node.Close()
r := solidus.NewResolver(node.URL)
absent, err := r.Resolve(context.Background(), "did:solidus:testnet:3tBoVe6XRtirzr8SdRotGgbkuEQN")
fmt.Println("absent DID error:", err, "/ metadata:", absent.DIDResolutionMetadata.Error)
malformed, err := r.Resolve(context.Background(), "did:solidus:1:3tBoVe6XRtirzr8SdRotGgbkuEQN")
fmt.Println("malformed DID error:", err, "/ metadata:", malformed.DIDResolutionMetadata.Error)
_, err = solidus.NewResolver("http://127.0.0.1:1").
Resolve(context.Background(), "did:solidus:testnet:3tBoVe6XRtirzr8SdRotGgbkuEQN")
fmt.Println("unreachable node returns an error:", err != nil)
// Output:
// absent DID error: <nil> / metadata: notFound
// malformed DID error: <nil> / metadata: invalidDid
// unreachable node returns an error: true
}
// The wire is not the W3C shape. `Resolve` performs the transformation; the
// fields below are the ones a passthrough client would get wrong.
func ExampleResolver_Resolve_wireToW3C() {
node := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"jsonrpc":"2.0","id":1,"result":` + wireResponse + `}`))
}))
defer node.Close()
res, err := solidus.NewResolver(node.URL).
Resolve(context.Background(), "did:solidus:testnet:cSBUyW8isc8PSmqAgLnqSJTJpfu")
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("@context entries:", len(res.DIDDocument.Context))
fmt.Println("publicKeyMultibase:", res.DIDDocument.VerificationMethod[0].PublicKeyMultibase)
// `active: true` on the wire becomes `deactivated: false` — negated.
fmt.Println("deactivated:", res.DIDDocumentMetadata.Deactivated)
// `created_ms` on the wire becomes an ISO 8601 string.
fmt.Println("created:", res.DIDDocument.Created)
// Output:
// @context entries: 2
// publicKeyMultibase: z6MkguuUVNj72BEqpY3qHikwFtSCiUWgDGbwiX8pqu5uh3gK
// deactivated: false
// created: 2026-07-21T21:22:18.963Z
}
// BBS+ is absent and cannot be added: no Go library implements our ciphersuite.
// This example exists so the gap appears in `go doc` rather than only in the
// README, where an evaluator can miss it.
func ExampleVerifyStrictMultibase_bbsIsNotAvailable() {
fmt.Println(strings.TrimSpace(`
Solidus signs credentials with draft-irtf-cfrg-bbs-signatures (80-byte
signature). Every Go BBS+ library implements the 2020 scheme (112-byte) and
rejects ours at parse time. This package verifies DIDs and signs transactions;
it cannot verify a credential.`))
// Output:
// Solidus signs credentials with draft-irtf-cfrg-bbs-signatures (80-byte
// signature). Every Go BBS+ library implements the 2020 scheme (112-byte) and
// rejects ours at parse time. This package verifies DIDs and signs transactions;
// it cannot verify a credential.
}