-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
98 lines (63 loc) · 1.9 KB
/
read.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
package tapcards
import (
"crypto/sha256"
"errors"
"fmt"
"log/slog"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
)
func (satscard *Satscard) ReadRequest() ([]byte, error) {
slog.Debug("Request read")
if satscard.currentCardNonce == [16]byte{} {
satscard.queue.enqueue("status")
}
satscard.queue.enqueue("read")
return satscard.nextCommand()
}
func (satscard *Satscard) readRequest() ([]byte, error) {
command := command{Cmd: "read"}
nonce, err := satscard.createNonce()
if err != nil {
return nil, err
}
readCommand := readCommand{
command: command,
Nonce: nonce,
}
return apduWrap(readCommand)
}
// READ
// read a SATSCARD’s current payment address
func (satscard *Satscard) parseReadData(readData readData) error {
slog.Debug("Parse read")
slog.Debug("READ", "Signature", fmt.Sprintf("%x", readData.Signature))
slog.Debug("READ", "PublicKey", fmt.Sprintf("%x", readData.PublicKey))
// Verify public key with signature
message := append([]byte(openDime), satscard.currentCardNonce[:]...)
message = append(message, satscard.appNonce[:]...)
message = append(message, []byte{byte(satscard.ActiveSlot)}...)
messageDigest := sha256.Sum256([]byte(message))
r := new(btcec.ModNScalar)
r.SetByteSlice(readData.Signature[0:32])
s := new(btcec.ModNScalar)
s.SetByteSlice(readData.Signature[32:])
signature := ecdsa.NewSignature(r, s)
publicKey, err := btcec.ParsePubKey(readData.PublicKey[:])
if err != nil {
return err
}
verified := signature.Verify(messageDigest[:], publicKey)
if !verified {
return errors.New("invalid signature read")
}
// Save the current slot public key
satscard.activeSlotPublicKey = readData.PublicKey
satscard.currentCardNonce = readData.CardNonce
paymentAddress, err := paymentAddress(readData.PublicKey)
if err != nil {
return err
}
satscard.ActiveSlotPaymentAddress = paymentAddress
return nil
}