-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeerDID.go
90 lines (78 loc) · 2.23 KB
/
peerDID.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
81
82
83
84
85
86
87
88
89
90
package main
import (
"crypto/ed25519"
"encoding/json"
"errors"
"fmt"
"github.com/btcsuite/btcutil/base58"
)
func main() {
publicKey, _, err := ed25519.GenerateKey(nil)
if err != nil {
fmt.Println("Error generating private key:", err)
return
}
did := NewPeerDID(publicKey)
fmt.Println(did.getDid())
doc, err := did.getDidDoc()
if err != nil {
fmt.Println("Error generating DID doc:", err)
return
}
jsonDDOc, err := json.Marshal(doc)
fmt.Println(string(jsonDDOc))
}
const DID = "did"
const DID_METHOD_NAME = "peer"
const TRANSFORMER rune = 'z'
const DEFAULT_CONTEXT string = "https://www.w3.org/ns/did/v1"
type PeerDID struct {
scheme string
method string
transform rune
numAlgo int8
identifier string
}
func NewPeerDID(publicKey []byte) *PeerDID {
identifier := base58.Encode(publicKey)
return &PeerDID{DID, DID_METHOD_NAME, TRANSFORMER, 0, identifier}
}
func (peerDid PeerDID) getDid() string {
return fmt.Sprintf("%s:%s:%d%s%s", peerDid.scheme, peerDid.method, peerDid.numAlgo, string(peerDid.transform), peerDid.identifier)
}
type VerificationMethod struct {
Id string
KeyType string
Controller string
PublicKeyMultibase string
}
type PeerDIDDoc struct {
Context []string
Id string
VerificationMethods []VerificationMethod
Authentications []string
AssertionMethods []string
CapabilityDelegations []string
CapabilityInvocations []string
}
func signatureVerificationMethod(did PeerDID, publicKeyFormat string) (*VerificationMethod, error) {
decodedId := base58.Decode(did.identifier)
if len(decodedId) != 32 {
return nil, errors.New("key must be a Ed25519")
}
return &VerificationMethod{did.getDid() + "#" + did.identifier, publicKeyFormat, did.getDid(), did.identifier}, nil
}
func (peerDid PeerDID) getDidDoc() (*PeerDIDDoc, error) {
verificationMethod, err := signatureVerificationMethod(peerDid, "Ed25519VerificationKey2020")
if err != nil {
return nil, err
}
return &PeerDIDDoc{[]string{DEFAULT_CONTEXT},
peerDid.getDid(),
[]VerificationMethod{*verificationMethod},
[]string{verificationMethod.Id},
[]string{verificationMethod.Id},
[]string{verificationMethod.Id},
[]string{verificationMethod.Id},
}, nil
}