mirror of
https://github.com/status-im/keycard-go.git
synced 2025-01-20 16:59:09 +00:00
add GenerateMnemonic
This commit is contained in:
parent
8877e09ca4
commit
2eea99ee0c
@ -1,8 +1,10 @@
|
||||
package keycard
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
@ -14,6 +16,7 @@ import (
|
||||
)
|
||||
|
||||
var ErrNoAvailablePairingSlots = errors.New("no available pairing slots")
|
||||
var ErrBadChecksumSize = errors.New("bad checksum size")
|
||||
|
||||
type WrongPINError struct {
|
||||
RemainingAttempts int
|
||||
@ -231,6 +234,32 @@ func (cs *CommandSet) GenerateKey() ([]byte, error) {
|
||||
return resp.Data, nil
|
||||
}
|
||||
|
||||
func (cs *CommandSet) GenerateMnemonic(checksumSize int) ([]int, error) {
|
||||
if checksumSize < 4 || checksumSize > 8 {
|
||||
return nil, ErrBadChecksumSize
|
||||
}
|
||||
|
||||
cmd := NewCommandGenerateMnemonic(byte(checksumSize))
|
||||
resp, err := cs.sc.Send(cmd)
|
||||
if err = cs.checkOK(resp, err); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(resp.Data)
|
||||
indexes := make([]int, 0)
|
||||
for {
|
||||
var index int16
|
||||
err := binary.Read(buf, binary.BigEndian, &index)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
indexes = append(indexes, int(index))
|
||||
}
|
||||
|
||||
return indexes, nil
|
||||
}
|
||||
|
||||
func (cs *CommandSet) RemoveKey() error {
|
||||
cmd := NewCommandRemoveKey()
|
||||
resp, err := cs.sc.Send(cmd)
|
||||
|
11
commands.go
11
commands.go
@ -26,6 +26,7 @@ const (
|
||||
InsSign = 0xC0
|
||||
InsSetPinlessPath = 0xC1
|
||||
InsLoadKey = 0xD0
|
||||
InsGenerateMnemonic = 0xD2
|
||||
|
||||
P1PairingFirstStep = 0x00
|
||||
P1PairingFinalStep = 0x01
|
||||
@ -131,6 +132,16 @@ func NewCommandGenerateKey() *apdu.Command {
|
||||
)
|
||||
}
|
||||
|
||||
func NewCommandGenerateMnemonic(checksumSize byte) *apdu.Command {
|
||||
return apdu.NewCommand(
|
||||
globalplatform.ClaGp,
|
||||
InsGenerateMnemonic,
|
||||
checksumSize,
|
||||
0,
|
||||
[]byte{},
|
||||
)
|
||||
}
|
||||
|
||||
func NewCommandRemoveKey() *apdu.Command {
|
||||
return apdu.NewCommand(
|
||||
globalplatform.ClaGp,
|
||||
|
Loading…
x
Reference in New Issue
Block a user