mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
668 B
37 lines
668 B
6 years ago
|
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
|
||
|
}
|