-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathidkey.tmpl
200 lines (165 loc) · 6.41 KB
/
idkey.tmpl
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
// MIT License
//
// Copyright 2018 Canonical Ledgers, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// Code generated DO NOT EDIT
package factom
// Defines IDKeys ID1Key - ID4Key and corresponding SKKeys SK1Key - SK4Key.
var (
{{range . -}}
id{{.ID}}PrefixBytes = [...]byte{ {{.IDPrefix}} }
{{end}}
{{range . -}}
sk{{.ID}}PrefixBytes = [...]byte{ {{.SKPrefix}} }
{{end}}
)
const (
{{range . -}}
id{{.ID}}PrefixStr = "id{{.ID}}"
{{end}}
{{range . -}}
sk{{.ID}}PrefixStr = "sk{{.ID}}"
{{end}}
)
{{range .}}
// ID{{.ID}}Key is the id{{.ID}} public key for an identity.
type ID{{.ID}}Key [sha256.Size]byte
// SK{{.ID}}Key is the sk{{.ID}} secret key for an identity.
type SK{{.ID}}Key [sha256.Size]byte
// Payload returns key as a byte array.
func (key ID{{.ID}}Key) Payload() [sha256.Size]byte {
return key
}
// Payload returns key as a byte array.
func (key SK{{.ID}}Key) Payload() [sha256.Size]byte {
return key
}
// payload returns adr as *payload. This is syntactic sugar useful in other
// methods that leverage *payload.
func (key *ID{{.ID}}Key) payload() *payload {
return (*payload)(key)
}
func (key *SK{{.ID}}Key) payload() *payload {
return (*payload)(key)
}
// PrefixBytes returns the two byte prefix for the address type as a byte
// array. Note that the prefix for a given address type is always the same and
// does not depend on the address value. Returns []byte{ {{- .IDPrefix -}} }.
func (ID{{.ID}}Key) PrefixBytes() []byte {
prefix := id{{.ID}}PrefixBytes
return prefix[:]
}
// PrefixBytes returns the two byte prefix for the address type as a byte
// array. Note that the prefix for a given address type is always the same and
// does not depend on the address value. Returns []byte{ {{- .SKPrefix -}} }.
func (SK{{.ID}}Key) PrefixBytes() []byte {
prefix := sk{{.ID}}PrefixBytes
return prefix[:]
}
// PrefixString returns the two prefix bytes for the address type as an encoded
// string. Note that the prefix for a given address type is always the same and
// does not depend on the address value. Returns "id{{.ID}}".
func (ID{{.ID}}Key) PrefixString() string {
return id{{.ID}}PrefixStr
}
// PrefixString returns the two prefix bytes for the address type as an encoded
// string. Note that the prefix for a given address type is always the same and
// does not depend on the address value. Returns "sk{{.ID}}".
func (SK{{.ID}}Key) PrefixString() string {
return sk{{.ID}}PrefixStr
}
// String encodes key into its human readable form: a base58check string with
// key.PrefixBytes().
func (key ID{{.ID}}Key) String() string {
return key.payload().StringWithPrefix(key.PrefixBytes())
}
// String encodes key into its human readable form: a base58check string with
// key.PrefixBytes().
func (key SK{{.ID}}Key) String() string {
return key.payload().StringWithPrefix(key.PrefixBytes())
}
// Type returns PrefixString() satisfies the pflag.Value interface.
func (ID{{.ID}}Key) Type() string {
return id{{.ID}}PrefixStr
}
// Type returns PrefixString() satisfies the pflag.Value interface.
func (SK{{.ID}}Key) Type() string {
return sk{{.ID}}PrefixStr
}
// MarshalText encodes key as a string using key.String().
func (key ID{{.ID}}Key) MarshalText() ([]byte, error) {
return key.payload().MarshalTextWithPrefix(key.PrefixBytes())
}
// MarshalText encodes key as a string using key.String().
func (key SK{{.ID}}Key) MarshalText() ([]byte, error) {
return key.payload().MarshalTextWithPrefix(key.PrefixBytes())
}
// NewID{{.ID}}Key attempts to parse keyStr into a new ID{{.ID}}Key.
func NewID{{.ID}}Key(keyStr string) (key ID{{.ID}}Key, err error) {
err = key.Set(keyStr)
return
}
// NewSK{{.ID}}Key attempts to parse keyStr into a new SK{{.ID}}Key.
func NewSK{{.ID}}Key(keyStr string) (key SK{{.ID}}Key, err error) {
err = key.Set(keyStr)
return
}
// GenerateSK{{.ID}}Key generates a secure random private Entry Credit address using
// crypto/rand.Random as the source of randomness.
func GenerateSK{{.ID}}Key() (SK{{.ID}}Key, error) {
return generatePrivKey()
}
// Set attempts to parse keyStr into key.
func (key *ID{{.ID}}Key) Set(keyStr string) error {
return key.payload().SetWithPrefix(keyStr, key.PrefixString())
}
// Set attempts to parse keyStr into key.
func (key *SK{{.ID}}Key) Set(keyStr string) error {
return key.payload().SetWithPrefix(keyStr, key.PrefixString())
}
// UnmarshalText decodes a string with a human readable id{{.ID}} key into key.
func (key *ID{{.ID}}Key) UnmarshalText(text []byte) error {
return key.payload().UnmarshalTextWithPrefix(text, key.PrefixString())
}
// UnmarshalText decodes a string with a human readable sk{{.ID}} key into key.
func (key *SK{{.ID}}Key) UnmarshalText(text []byte) error {
return key.payload().UnmarshalTextWithPrefix(text, key.PrefixString())
}
// ID{{.ID}}Key computes the ID{{.ID}}Key corresponding to key.
func (key SK{{.ID}}Key) ID{{.ID}}Key() ID{{.ID}}Key {
return ID{{.ID}}Key(key.RCD().Hash())
}
// RCD computes the RCD for key.
func (key SK{{.ID}}Key) RCD() RCD {
return append([]byte{byte(RCDType01)}, key.PublicKey()[:]...)
}
// Sign the msg.
func (key SK{{.ID}}Key) Sign(msg []byte) []byte {
return ed25519.Sign(key.PrivateKey(), msg)
}
// PublicKey computes the ed25519.PublicKey for key.
func (key SK{{.ID}}Key) PublicKey() ed25519.PublicKey {
return key.PrivateKey().Public().(ed25519.PublicKey)
}
// PrivateKey returns the ed25519.PrivateKey for key.
func (key SK{{.ID}}Key) PrivateKey() ed25519.PrivateKey {
return ed25519.NewKeyFromSeed(key[:])
}
{{end}}