mirror of
https://github.com/status-im/keycard-go.git
synced 2025-01-18 16:01:03 +00:00
46 lines
1015 B
Go
46 lines
1015 B
Go
package smartcard
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/status-im/status-go/smartcard/apdu"
|
|
"github.com/status-im/status-go/smartcard/globalplatform"
|
|
"github.com/status-im/status-go/smartcard/hexutils"
|
|
)
|
|
|
|
type SecureChannel struct {
|
|
session *globalplatform.Session
|
|
c Channel
|
|
w *globalplatform.APDUWrapper
|
|
}
|
|
|
|
func NewSecureChannel(session *globalplatform.Session, c Channel) *SecureChannel {
|
|
return &SecureChannel{
|
|
session: session,
|
|
c: c,
|
|
w: globalplatform.NewAPDUWrapper(session.KeyProvider().Mac()),
|
|
}
|
|
}
|
|
|
|
func (c *SecureChannel) Send(cmd *apdu.Command) (*apdu.Response, error) {
|
|
rawCmd, err := cmd.Serialize()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fmt.Printf("WRAPPING %s\n", hexutils.BytesToHexWithSpaces(rawCmd))
|
|
wrappedCmd, err := c.w.Wrap(cmd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rawWrappedCmd, err := wrappedCmd.Serialize()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fmt.Printf("WRAPPED %s\n", hexutils.BytesToHexWithSpaces(rawWrappedCmd))
|
|
|
|
return c.c.Send(wrappedCmd)
|
|
}
|