add derivationpath encoder

This commit is contained in:
Andrea Franz 2019-03-15 17:03:01 +01:00
parent 6b81810595
commit 8809bfb0f5
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
3 changed files with 75 additions and 17 deletions

36
derivationpath/encoder.go Normal file
View File

@ -0,0 +1,36 @@
package derivationpath
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
)
func Encode(rawPath []uint32) string {
segments := []string{string(tokenMaster)}
for _, i := range rawPath {
suffix := ""
if i >= hardenedStart {
i = i - hardenedStart
suffix = string(tokenHardened)
}
segments = append(segments, fmt.Sprintf("%d%s", i, suffix))
}
return strings.Join(segments, string(tokenSeparator))
}
func EncodeFromBytes(data []byte) (string, error) {
buf := bytes.NewBuffer(data)
rawPath := make([]uint32, buf.Len()/4)
err := binary.Read(buf, binary.BigEndian, &rawPath)
if err != nil {
return "", err
}
return Encode(rawPath), nil
}

View File

@ -0,0 +1,35 @@
package derivationpath
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEncode(t *testing.T) {
scenarios := []struct {
path []uint32
expectedPath string
}{
{
path: []uint32{},
expectedPath: "m",
},
{
path: []uint32{0, 1, 2},
expectedPath: "m/0/1/2",
},
{
path: []uint32{hardenedStart + 10, 1, 2},
expectedPath: "m/10'/1/2",
},
}
for i, s := range scenarios {
t.Run(fmt.Sprintf("scenario %d", i), func(t *testing.T) {
path := Encode(s.path)
assert.Equal(t, s.expectedPath, path)
})
}
}

View File

@ -2,12 +2,10 @@ package types
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"strings"
"github.com/status-im/keycard-go/apdu"
"github.com/status-im/keycard-go/derivationpath"
)
const hardenedStart = 0x80000000 // 2^31
@ -48,24 +46,13 @@ func ParseApplicationStatus(data []byte) (*ApplicationStatus, error) {
func parseKeyPathStatus(data []byte) (*ApplicationStatus, error) {
appStatus := &ApplicationStatus{}
buf := bytes.NewBuffer(data)
rawPath := make([]uint32, buf.Len()/4)
err := binary.Read(buf, binary.BigEndian, &rawPath)
path, err := derivationpath.EncodeFromBytes(data)
if err != nil {
return nil, err
}
segments := []string{"m"}
for _, i := range rawPath {
suffix := ""
if i >= hardenedStart {
i = i - hardenedStart
suffix = "'"
}
segments = append(segments, fmt.Sprintf("%d%s", i, suffix))
}
appStatus.Path = strings.Join(segments, "/")
appStatus.Path = path
return appStatus, nil
}