-
Notifications
You must be signed in to change notification settings - Fork 0
/
satscard.go
287 lines (188 loc) · 5.73 KB
/
satscard.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package tapcards
import (
"crypto/rand"
"errors"
"fmt"
"log/slog"
"os"
"github.com/fxamacker/cbor/v2"
)
const openDime = "OPENDIME"
var factoryRootPublicKeyString = "03028a0e89e70d0ec0d932053a89ab1da7d9182bdc6d2f03e706ee99517d05d9e1"
// Satscard is a struct that represents a Satscard.
type Satscard struct {
// Public fields
// ActiveSlot is the currently active slot on the card, counting from 0.
ActiveSlot int
// NumberOfSlots is the total number of slots available on the card.
NumberOfSlots int
// Identity is the human readable identity of the card.
Identity string
// ActiveSlotPaymentAddress is the payment address associated with the currently active slot.
ActiveSlotPaymentAddress string
// Proto is the protocol version of the card.
Proto int
// Birth is the block height of the card.
Birth int
// Version is the version of the card.
Version string
// ActiveSlotPrivateKey is the private key of the currently active slot.
ActiveSlotPrivateKey string
// AuthDelay is the authentication delay of the card.
AuthDelay int
// Private fields
// appNonce is the nonce of the application.
appNonce []byte
// currentCardNonce is the current nonce of the card.
currentCardNonce [16]byte
// cardPublicKey is the public key of the card.
cardPublicKey [33]byte
// sessionKey is the session key of the card.
sessionKey [32]byte
// activeSlotPublicKey is the public key of the currently active slot.
activeSlotPublicKey [33]byte
// certificateChain is the certificate chain of the card.
certificateChain [][65]byte
// cvc is the Card Verification Code of the card.
cvc string
// queue is the queue of commands to be sent to the card.
queue
}
func (satscard *Satscard) createNonce() ([]byte, error) {
// Create nonce
nonce := make([]byte, 16)
_, err := rand.Read(nonce)
if err != nil {
return nil, err
}
slog.Debug("Created nonce", "Nonce", fmt.Sprintf("%x", nonce))
satscard.appNonce = nonce
return nonce, nil
}
func (satscard *Satscard) ParseResponse(response []byte) ([]byte, error) {
bytes, err := apduUnwrap(response)
if err != nil {
return nil, err
}
decMode, _ := cbor.DecOptions{ExtraReturnErrors: cbor.ExtraDecErrorUnknownField}.DecMode()
command := satscard.queue.dequeue()
if command == nil {
return nil, fmt.Errorf("queue empty")
}
//TODO: Take a look at generics to see if we can avoid code repetition here
switch command {
case "status":
var v statusData
if err := decMode.Unmarshal(bytes, &v); err != nil {
var e errorData
if err := decMode.Unmarshal(bytes, &e); err != nil {
return nil, err
}
return nil, fmt.Errorf("%d: %v", e.Code, e.Error)
}
err = satscard.parseStatusData(v)
case "read":
var v readData
if err := decMode.Unmarshal(bytes, &v); err != nil {
var e errorData
if err := decMode.Unmarshal(bytes, &e); err != nil {
return nil, err
}
return nil, fmt.Errorf("%d: %v", e.Code, e.Error)
}
err = satscard.parseReadData(v)
case "unseal":
var v unsealData
if err := decMode.Unmarshal(bytes, &v); err != nil {
var e errorData
if err := decMode.Unmarshal(bytes, &e); err != nil {
return nil, err
}
return nil, fmt.Errorf("%d: %v", e.Code, e.Error)
}
err = satscard.parseUnsealData(v)
case "certs":
var v certsData
if err := decMode.Unmarshal(bytes, &v); err != nil {
var e errorData
if err := decMode.Unmarshal(bytes, &e); err != nil {
return nil, err
}
return nil, fmt.Errorf("%d: %v", e.Code, e.Error)
}
err = satscard.parseCertsData(v)
case "check":
var v checkData
if err := decMode.Unmarshal(bytes, &v); err != nil {
var e errorData
if err := decMode.Unmarshal(bytes, &e); err != nil {
return nil, err
}
return nil, fmt.Errorf("%d: %v", e.Code, e.Error)
}
err = satscard.parseCheckData(v)
case "new":
var v newData
if err := decMode.Unmarshal(bytes, &v); err != nil {
var e errorData
if err := decMode.Unmarshal(bytes, &e); err != nil {
return nil, err
}
return nil, fmt.Errorf("%d: %v", e.Code, e.Error)
}
err = satscard.parseNewData(v)
case "wait":
var v waitData
if err := decMode.Unmarshal(bytes, &v); err != nil {
var e errorData
if err := decMode.Unmarshal(bytes, &e); err != nil {
return nil, err
}
return nil, fmt.Errorf("%d: %v", e.Code, e.Error)
}
err = satscard.parseWaitData(v)
default:
return nil, errors.New("incorrect command found in queue")
}
if err != nil {
return nil, err
}
// Check if there are more commands to run
return satscard.nextCommand()
}
func (satscard *Satscard) nextCommand() ([]byte, error) {
command := satscard.queue.peek()
if command == nil {
satscard.cvc = ""
return nil, nil
}
switch command {
case "status":
return satscard.statusRequest()
case "read":
return satscard.readRequest()
case "unseal":
return satscard.unsealRequest()
case "certs":
return satscard.certsRequest()
case "check":
return satscard.checkRequest()
case "new":
return satscard.newRequest()
case "wait":
return satscard.waitRequest()
default:
return nil, errors.New("incorrect command")
}
}
// EnableDebugLogging is a function that enables debug logging in the application.
// It creates a new text handler that writes to the standard error output and sets the log level to debug.
// It then sets this handler as the default handler for the slog package.
func EnableDebugLogging() {
handler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})
slog.SetDefault(slog.New(handler))
}
// UseEmulator is a function that sets the factory root public key string to the specific value associated with the emulator.
func UseEmulator() {
factoryRootPublicKeyString = "022b6750a0c09f632df32afc5bef66568667e04b2e0f57cb8640ac5a040179442b"
}