generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cmd_did_create.go
80 lines (63 loc) · 1.64 KB
/
cmd_did_create.go
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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/tbd54566975/web5-go/dids/did"
"github.com/tbd54566975/web5-go/dids/diddht"
"github.com/tbd54566975/web5-go/dids/didjwk"
"github.com/tbd54566975/web5-go/dids/didweb"
)
type didCreateCMD struct {
JWK didCreateJWKCMD `cmd:"" help:"Create a did:jwk."`
Web didCreateWebCMD `cmd:"" help:"Create a did:web."`
DHT didCreateDHTCMD `cmd:"" help:"Create a did:dht."`
}
type didCreateJWKCMD struct {
NoIndent bool `help:"Print the portable DID without indentation." default:"false"`
}
func (c *didCreateJWKCMD) Run() error {
did, err := didjwk.Create()
if err != nil {
return err
}
return printDID(did, c.NoIndent)
}
type didCreateWebCMD struct {
Domain string `arg:"" help:"The domain name for the DID." required:""`
NoIndent bool `help:"Print the portable DID without indentation." default:"false"`
}
func (c *didCreateWebCMD) Run() error {
did, err := didweb.Create(c.Domain)
if err != nil {
return err
}
return printDID(did, c.NoIndent)
}
type didCreateDHTCMD struct {
NoIndent bool `help:"Print the portable DID without indentation." default:"false"`
}
func (c *didCreateDHTCMD) Run() error {
did, err := diddht.CreateWithContext(context.Background())
if err != nil {
return err
}
return printDID(did, c.NoIndent)
}
func printDID(d did.BearerDID, noIndent bool) error {
portableDID, err := d.ToPortableDID()
if err != nil {
return err
}
var jsonDID []byte
if noIndent {
jsonDID, err = json.Marshal(portableDID)
} else {
jsonDID, err = json.MarshalIndent(portableDID, "", " ")
}
if err != nil {
return err
}
fmt.Println(string(jsonDID))
return nil
}