-
Notifications
You must be signed in to change notification settings - Fork 17
/
cash_command_set.go
70 lines (56 loc) · 1.48 KB
/
cash_command_set.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
package keycard
import (
"github.com/status-im/keycard-go/apdu"
"github.com/status-im/keycard-go/globalplatform"
"github.com/status-im/keycard-go/identifiers"
"github.com/status-im/keycard-go/types"
)
type CashCommandSet struct {
c types.Channel
CashApplicationInfo *types.CashApplicationInfo
}
func NewCashCommandSet(c types.Channel) *CashCommandSet {
return &CashCommandSet{
c: c,
CashApplicationInfo: &types.CashApplicationInfo{},
}
}
func (cs *CashCommandSet) Select() error {
cmd := globalplatform.NewCommandSelect(identifiers.CashInstanceAID)
cmd.SetLe(0)
resp, err := cs.c.Send(cmd)
if err = cs.checkOK(resp, err); err != nil {
return err
}
appInfo, err := types.ParseCashApplicationInfo(resp.Data)
if err != nil {
return err
}
cs.CashApplicationInfo = appInfo
return nil
}
func (cs *CashCommandSet) Sign(data []byte) (*types.Signature, error) {
cmd, err := NewCommandSign(data, 0x00, "")
if err != nil {
return nil, err
}
resp, err := cs.c.Send(cmd)
if err = cs.checkOK(resp, err); err != nil {
return nil, err
}
return types.ParseSignature(data, resp.Data)
}
func (cs *CashCommandSet) checkOK(resp *apdu.Response, err error, allowedResponses ...uint16) error {
if err != nil {
return err
}
if len(allowedResponses) == 0 {
allowedResponses = []uint16{apdu.SwOK}
}
for _, code := range allowedResponses {
if code == resp.Sw {
return nil
}
}
return apdu.NewErrBadResponse(resp.Sw, "unexpected response")
}