Skip to content

Commit a215d5a

Browse files
committed
Add examples
Remove print statements
1 parent 5e0f22e commit a215d5a

File tree

4 files changed

+142
-37
lines changed

4 files changed

+142
-37
lines changed

example_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package trustdidweb_test
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
tdw "github.com/nuts-foundation/trustdidweb-go"
8+
)
9+
10+
func ExampleCreate() {
11+
// Create a new signer
12+
signer, err := tdw.NewSigner(tdw.CRYPTO_SUITE_EDDSA_JCS_2022)
13+
if err != nil {
14+
log.Fatal(err)
15+
}
16+
17+
// Create a new DIDLog
18+
doc, err := tdw.NewMinimalDIDDocument("did:tdw:{SCID}:example.com")
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
23+
didLog, err := tdw.Create(doc, signer)
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
28+
// Print the log
29+
logFile, err := didLog.MarshalText()
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
fmt.Println(string(logFile))
34+
}
35+
36+
func ExampleDIDLog_Update() {
37+
// Create a new signer
38+
signer, err := tdw.NewSigner(tdw.CRYPTO_SUITE_EDDSA_JCS_2022)
39+
if err != nil {
40+
log.Fatal(err)
41+
}
42+
43+
// Create a new DIDDocument
44+
doc, err := tdw.NewMinimalDIDDocument("did:tdw:{SCID}:example.com")
45+
if err != nil {
46+
log.Fatal(err)
47+
}
48+
49+
// Create a new DIDLog
50+
didLog, err := tdw.Create(doc, signer)
51+
if err != nil {
52+
log.Fatal(err)
53+
}
54+
55+
// Get the document from the log
56+
doc, err = didLog.Document()
57+
if err != nil {
58+
log.Fatal(err)
59+
}
60+
61+
// Add a service
62+
doc["service"] = []interface{}{
63+
map[string]interface{}{
64+
"id": fmt.Sprintf("%s#service-1", doc["id"]),
65+
"type": "Service",
66+
"serviceEndpoint": "https://example.com/service/1",
67+
},
68+
}
69+
70+
// Update the log with a new entry containing the updated document
71+
didLog, err = didLog.Update(tdw.LogParams{}, doc, signer)
72+
if err != nil {
73+
log.Fatal(err)
74+
}
75+
76+
// Print the log
77+
logFile, err := didLog.MarshalText()
78+
if err != nil {
79+
log.Fatal(err)
80+
}
81+
fmt.Println(string(logFile))
82+
}
83+
84+
func ExampleDIDLog_Verify() {
85+
// Create a new signer
86+
signer, err := tdw.NewSigner(tdw.CRYPTO_SUITE_EDDSA_JCS_2022)
87+
if err != nil {
88+
log.Fatal(err)
89+
}
90+
91+
// Create a new DIDDocument
92+
doc, err := tdw.NewMinimalDIDDocument("did:tdw:{SCID}:example.com")
93+
if err != nil {
94+
log.Fatal(err)
95+
}
96+
97+
didLog, err := tdw.Create(doc, signer)
98+
if err != nil {
99+
log.Fatal(err)
100+
}
101+
102+
if err := didLog.Verify(); err != nil {
103+
log.Fatal(err)
104+
}
105+
106+
fmt.Println("Log is valid")
107+
108+
// Output:
109+
// Log is valid
110+
}
111+
112+
func ExampleDIDLog_Deactivate() {
113+
// Create a new signer
114+
signer, err := tdw.NewSigner(tdw.CRYPTO_SUITE_EDDSA_JCS_2022)
115+
if err != nil {
116+
log.Fatal(err)
117+
}
118+
119+
// Create a new DIDDocument
120+
doc, err := tdw.NewMinimalDIDDocument("did:tdw:{SCID}:example.com")
121+
if err != nil {
122+
log.Fatal(err)
123+
}
124+
125+
didLog, err := tdw.Create(doc, signer)
126+
if err != nil {
127+
log.Fatal(err)
128+
}
129+
130+
// Deactivate the log
131+
didLog, err = didLog.Deactivate(signer)
132+
if err != nil {
133+
log.Fatal(err)
134+
}
135+
136+
fmt.Printf("Is log deactivated: %t\n", didLog.IsDeactivated())
137+
138+
// Output:
139+
// Is log deactivated: true
140+
}

logentry.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,8 @@ func (l LogEntry) MarshalJSONL() ([]byte, error) {
8989
}
9090

9191
func (entry LogEntry) calculateEntryHash(prevVersionId versionId) (string, error) {
92-
fmt.Print("\ncalculateEntryHash:\n")
93-
94-
fmt.Printf("previous version id: %s\n", prevVersionId.String())
9592
// a hash is calulated over the entry without proof
9693
entry.Proof = nil
97-
// fmt.Print("\n\ncalculateEntryHash:\n\n")
98-
// fmt.Printf("entry: %v\n", entry)
9994

10095
// Canonicalized version of the first log entry
10196
entry.VersionId = prevVersionId
@@ -104,14 +99,10 @@ func (entry LogEntry) calculateEntryHash(prevVersionId versionId) (string, error
10499
return "", err
105100
}
106101

107-
fmt.Printf("logentry: %s\n", b)
108-
109102
entryHash := newEntryHash(b, uint64(multicodec.Sha2_256))
110103
if entryHash == "" {
111104
return "", fmt.Errorf("failed to calculate entry hash")
112105
}
113106

114-
fmt.Printf("calculated hash: %s\n", entryHash)
115-
116107
return string(entryHash), nil
117108
}

proof.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ func (proof Proof) Verify(challenge string, updateKeys []string, doc DIDDocument
104104
return fmt.Errorf("unsupported proof purpose: %s", proof.ProofPurpose)
105105
}
106106
if proof.Challenge != challenge {
107-
fmt.Printf("proof.challenge: %s, expected: %s\n", proof.Challenge, challenge)
108107
return fmt.Errorf("challenge mismatch")
109108
}
110109

@@ -149,8 +148,6 @@ func (proof Proof) Verify(challenge string, updateKeys []string, doc DIDDocument
149148
if err != nil {
150149
return fmt.Errorf("failed to decode proof value: %w", err)
151150
}
152-
fmt.Printf("signature: %x\n", signature)
153-
fmt.Printf("signature length: %d\n", len(signature))
154151

155152
switch pubKey := pubKey.(type) {
156153
case *ecdsa.PublicKey:
@@ -213,13 +210,9 @@ func hashLogVersion(document map[string]interface{}, proof Proof, hashfn hash.Ha
213210
hashfn.Write(optionData)
214211
optionsHash := hashfn.Sum(nil)
215212

216-
fmt.Printf("dataHash: %x\n", dataHash)
217-
fmt.Printf("optionsHash: %x\n", optionsHash)
218-
219213
logger().Debug("hash", "data", base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(dataHash[:]))
220214
logger().Debug("hash", "options", base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(optionsHash[:]))
221215

222216
input := append(dataHash[:], optionsHash[:]...)
223-
fmt.Printf("input: %x\n", input)
224217
return input, nil
225218
}

trustdidweb.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
"crypto/rand"
1010
"crypto/sha256"
1111
"crypto/sha512"
12-
"encoding/base64"
1312
"encoding/binary"
1413
"errors"
1514
"fmt"
1615
"hash"
16+
"io/ioutil"
1717
"log/slog"
1818
"strconv"
1919
"strings"
@@ -247,7 +247,7 @@ func (log DIDLog) Document() (DIDDocument, error) {
247247
}
248248

249249
func logger() *slog.Logger {
250-
return slog.Default().WithGroup("trustdidweb")
250+
return slog.New(slog.NewTextHandler(ioutil.Discard, &slog.HandlerOptions{Level: slog.LevelDebug}))
251251
}
252252

253253
func renderPathTemplate(didTemplate, scid string) string {
@@ -346,8 +346,6 @@ func Create(doc DIDDocument, signer crypto.Signer, nextKeyhashes ...NextKeyHash)
346346
}
347347

348348
func (log DIDLog) Update(params LogParams, modifiedDoc map[string]interface{}, signer crypto.Signer) (DIDLog, error) {
349-
fmt.Print("\n\nUpdate:\n\n")
350-
351349
currentDoc, err := log.Document()
352350
if err != nil {
353351
return DIDLog{}, err
@@ -412,8 +410,6 @@ func NewSigner(cryptoSuite string) (crypto.Signer, error) {
412410

413411
// buildProof creates a proof for the latest entry in the log
414412
func (log DIDLog) buildProof(signer crypto.Signer) (Proof, error) {
415-
fmt.Print("\n\nbuildProof:\n\n")
416-
417413
entry := log[len(log)-1]
418414

419415
verificationMethod, err := verificationMethodFromSigner(signer)
@@ -464,18 +460,12 @@ func (log DIDLog) buildProof(signer crypto.Signer) (Proof, error) {
464460
return Proof{}, fmt.Errorf("failed to hash entry: %w", err)
465461
}
466462

467-
fmt.Printf("input length: %d\n", len(input))
468-
469-
fmt.Printf("input: %s\n", base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(input))
470-
471463
// sign the entry
472464
signature, err := signer.Sign(rand.Reader, input, signerOpts)
473465
if err != nil {
474466
return Proof{}, fmt.Errorf("failed to sign entry: %w", err)
475467
}
476468

477-
fmt.Printf("signature: %s\n", base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(signature))
478-
479469
encodedProof, err := multibase.Encode(multibase.Base58BTC, signature)
480470
if err != nil {
481471
return Proof{}, fmt.Errorf("failed to encode signature: %w", err)
@@ -487,9 +477,6 @@ func (log DIDLog) buildProof(signer crypto.Signer) (Proof, error) {
487477

488478
// calculateEntryHash calculates the hash of the
489479
func (log DIDLog) calculateVersionId(version int) (versionId, error) {
490-
fmt.Print("\ncalculateVersionId:\n")
491-
fmt.Printf("version: %d\n", version)
492-
493480
var entry LogEntry
494481
var prevVersionId versionId
495482
switch version {
@@ -513,7 +500,6 @@ func (log DIDLog) calculateVersionId(version int) (versionId, error) {
513500
}
514501

515502
func (log DIDLog) Verify() error {
516-
fmt.Print("\n\nVerify:\n\n")
517503
// empty log should not be considered valid
518504
if len(log) == 0 {
519505
return fmt.Errorf("empty log")
@@ -522,14 +508,11 @@ func (log DIDLog) Verify() error {
522508
var scid string
523509

524510
for i, entry := range log {
525-
fmt.Printf("\nentry i: %d\n", i)
526-
527511
if i+1 != entry.VersionId.Version {
528512
return fmt.Errorf("invalid log sequence number, expected: %d, got: %d", i+1, entry.VersionId.Version)
529513
}
530514

531515
if i == 0 {
532-
fmt.Printf("verify scid first entry\n")
533516
if entry.Params.Scid == "" {
534517
return fmt.Errorf("missing scid in the first log entry params")
535518
}
@@ -633,8 +616,6 @@ func (log DIDLog) Verify() error {
633616
}
634617

635618
func (log DIDLog) Deactivate(signer crypto.Signer) (DIDLog, error) {
636-
fmt.Print("\n\nDeactivate:\n\n")
637-
638619
if len(log) == 0 {
639620
return nil, fmt.Errorf("empty log")
640621
}

0 commit comments

Comments
 (0)