From 2186c1e9dbf534e3dd55f31cfd81e55632017156 Mon Sep 17 00:00:00 2001 From: Delweng Zheng Date: Thu, 14 Jun 2018 21:44:29 +0800 Subject: [PATCH] accounts: inintroduce derivationPathBase to represents 0x80000000 --- accounts/hd.go | 15 +++++++++------ accounts/hd_test.go | 46 ++++++++++++++++++++++----------------------- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/accounts/hd.go b/accounts/hd.go index 277f688e48..c101539c8b 100644 --- a/accounts/hd.go +++ b/accounts/hd.go @@ -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) diff --git a/accounts/hd_test.go b/accounts/hd_test.go index b6b23230dc..efaadbf465 100644 --- a/accounts/hd_test.go +++ b/accounts/hd_test.go @@ -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