-
Notifications
You must be signed in to change notification settings - Fork 0
/
unseal.go
81 lines (53 loc) · 1.81 KB
/
unseal.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
package tapcards
import (
"fmt"
"log/slog"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
)
func (satscard *Satscard) UnsealRequest(cvc string) ([]byte, error) {
slog.Debug("Request unseal")
if satscard.currentCardNonce == [16]byte{} {
satscard.queue.enqueue("status")
}
satscard.queue.enqueue("unseal")
satscard.cvc = cvc
return satscard.nextCommand()
}
func (satscard *Satscard) unsealRequest() ([]byte, error) {
command := command{Cmd: "unseal"}
auth, err := satscard.authenticate(satscard.cvc, command)
if err != nil {
return nil, err
}
unsealCommand := unsealCommand{
command: command,
auth: *auth,
Slot: satscard.ActiveSlot,
}
return apduWrap(unsealCommand)
}
func (satscard *Satscard) parseUnsealData(unsealData unsealData) error {
slog.Debug("Parse unseal")
slog.Debug("UNSEAL", "Slot", unsealData.Slot)
slog.Debug("UNSEAL", "PrivateKey", fmt.Sprintf("%x", unsealData.PrivateKey))
slog.Debug("UNSEAL", "PublicKey", fmt.Sprintf("%x", unsealData.PublicKey))
slog.Debug("UNSEAL", "MasterPublicKey", fmt.Sprintf("%x", unsealData.MasterPublicKey))
slog.Debug("UNSEAL", "ChainCode", fmt.Sprintf("%x", unsealData.ChainCode))
slog.Debug("UNSEAL", "CardNonce", fmt.Sprintf("%x", unsealData.CardNonce))
satscard.currentCardNonce = unsealData.CardNonce
// Calculate and return private key as wif
unencryptedPrivateKeyBytes, err := xor(unsealData.PrivateKey[:], satscard.sessionKey[:])
if err != nil {
return err
}
privateKey, _ := btcec.PrivKeyFromBytes(unencryptedPrivateKeyBytes)
// TODO support other than mainnet for development and testing purposes
wif, err := btcutil.NewWIF(privateKey, &chaincfg.MainNetParams, true)
if err != nil {
return err
}
satscard.ActiveSlotPrivateKey = wif.String()
return nil
}