accounts: inintroduce derivationPathBase to represents 0x80000000

This commit is contained in:
Delweng Zheng 2018-06-14 21:44:29 +08:00
parent aa34173f13
commit 2186c1e9db
2 changed files with 32 additions and 29 deletions

View file

@ -24,20 +24,23 @@ import (
"strings"
)
// derivationPathBase represents the base index of DerivationPath
const derivationPathBase = 0x80000000
// DefaultRootDerivationPath is the root path to which custom derivation endpoints
// are appended. As such, the first account will be at m/44'/60'/0'/0, the second
// at m/44'/60'/0'/1, etc.
var DefaultRootDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}
var DefaultRootDerivationPath = DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0}
// DefaultBaseDerivationPath is the base path from which custom derivation endpoints
// are incremented. As such, the first account will be at m/44'/60'/0'/0, the second
// at m/44'/60'/0'/1, etc.
var DefaultBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0}
var DefaultBaseDerivationPath = DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0, 0}
// DefaultLedgerBaseDerivationPath is the base path from which custom derivation endpoints
// are incremented. As such, the first account will be at m/44'/60'/0'/0, the second
// at m/44'/60'/0'/1, etc.
var DefaultLedgerBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}
var DefaultLedgerBaseDerivationPath = DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0}
// DerivationPath represents the computer friendly version of a hierarchical
// deterministic wallet account derivaion path.
@ -93,7 +96,7 @@ func ParseDerivationPath(path string) (DerivationPath, error) {
// Handle hardened paths
if strings.HasSuffix(component, "'") {
value = 0x80000000
value = derivationPathBase
component = strings.TrimSpace(strings.TrimSuffix(component, "'"))
}
// Handle the non hardened component
@ -122,8 +125,8 @@ func (path DerivationPath) String() string {
result := "m"
for _, component := range path {
var hardened bool
if component >= 0x80000000 {
component -= 0x80000000
if component >= derivationPathBase {
component -= derivationPathBase
hardened = true
}
result = fmt.Sprintf("%s/%d", result, component)

View file

@ -29,37 +29,37 @@ func TestHDPathParsing(t *testing.T) {
output DerivationPath
}{
// Plain absolute derivation paths
{"m/44'/60'/0'/0", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}},
{"m/44'/60'/0'/128", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 128}},
{"m/44'/60'/0'/0'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 0}},
{"m/44'/60'/0'/128'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 128}},
{"m/2147483692/2147483708/2147483648/0", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}},
{"m/2147483692/2147483708/2147483648/2147483648", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 0}},
{"m/44'/60'/0'/0", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0}},
{"m/44'/60'/0'/128", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 128}},
{"m/44'/60'/0'/0'", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, derivationPathBase + 0}},
{"m/44'/60'/0'/128'", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, derivationPathBase + 128}},
{"m/2147483692/2147483708/2147483648/0", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0}},
{"m/2147483692/2147483708/2147483648/2147483648", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, derivationPathBase + 0}},
// Plain relative derivation paths
{"0", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0}},
{"128", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 128}},
{"0'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 0}},
{"128'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 128}},
{"2147483648", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 0}},
{"0", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0, 0}},
{"128", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0, 128}},
{"0'", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0, derivationPathBase + 0}},
{"128'", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0, derivationPathBase + 128}},
{"2147483648", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0, derivationPathBase + 0}},
// Hexadecimal absolute derivation paths
{"m/0x2C'/0x3c'/0x00'/0x00", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}},
{"m/0x2C'/0x3c'/0x00'/0x80", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 128}},
{"m/0x2C'/0x3c'/0x00'/0x00'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 0}},
{"m/0x2C'/0x3c'/0x00'/0x80'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 128}},
{"m/0x8000002C/0x8000003c/0x80000000/0x00", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}},
{"m/0x8000002C/0x8000003c/0x80000000/0x80000000", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 0}},
{"m/0x2C'/0x3c'/0x00'/0x00", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0}},
{"m/0x2C'/0x3c'/0x00'/0x80", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 128}},
{"m/0x2C'/0x3c'/0x00'/0x00'", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, derivationPathBase + 0}},
{"m/0x2C'/0x3c'/0x00'/0x80'", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, derivationPathBase + 128}},
{"m/0x8000002C/0x8000003c/derivationPathBase/0x00", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0}},
{"m/0x8000002C/0x8000003c/derivationPathBase/derivationPathBase", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, derivationPathBase + 0}},
// Hexadecimal relative derivation paths
{"0x00", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0}},
{"0x80", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 128}},
{"0x00'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 0}},
{"0x80'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 128}},
{"0x80000000", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 0}},
{"0x00", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0, 0}},
{"0x80", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0, 128}},
{"0x00'", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0, derivationPathBase + 0}},
{"0x80'", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0, derivationPathBase + 128}},
{"derivationPathBase", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0, derivationPathBase + 0}},
// Weird inputs just to ensure they work
{" m / 44 '\n/\n 60 \n\n\t' /\n0 ' /\t\t 0", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}},
{" m / 44 '\n/\n 60 \n\n\t' /\n0 ' /\t\t 0", DerivationPath{derivationPathBase + 44, derivationPathBase + 60, derivationPathBase + 0, 0}},
// Invaid derivation paths
{"", nil}, // Empty relative derivation path