From 5ead6d935a31efabd90fe7977053ac7966ebecc3 Mon Sep 17 00:00:00 2001 From: Anton Astafiev Date: Tue, 20 Apr 2021 17:14:19 +0200 Subject: [PATCH 1/3] Move change inside crypto.go --- cmd/account.go | 10 +++------- cmd/legacy-address.go | 10 +++------- cmd/principal.go | 8 ++------ cmd/private-key.go | 10 +++------- cmd/public-key.go | 10 +++------- crypto/crypto.go | 5 +++++ 6 files changed, 19 insertions(+), 34 deletions(-) diff --git a/cmd/account.go b/cmd/account.go index 273a940..5134bbb 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -43,18 +43,14 @@ func (cmd *AccountCmd) Run() error { if err != nil { return err } - childXPrivKey, err := masterXPrivKey.Derive(0) - if err != nil { - return err - } - _, grandchildECPubKey, err := crypto.DeriveChildECKeyPair( - childXPrivKey, + _, childECPubKey, err := crypto.DeriveChildECKeyPair( + masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { return err } - accountId, err := account.FromECPubKey(grandchildECPubKey) + accountId, err := account.FromECPubKey(childECPubKey) if err != nil { return err } diff --git a/cmd/legacy-address.go b/cmd/legacy-address.go index a8494fb..e584e21 100644 --- a/cmd/legacy-address.go +++ b/cmd/legacy-address.go @@ -44,18 +44,14 @@ func (cmd *LegacyAddressCmd) Run() error { if err != nil { return err } - childXPrivKey, err := masterXPrivKey.Derive(0) - if err != nil { - return err - } - _, grandchildECPubKey, err := crypto.DeriveChildECKeyPair( - childXPrivKey, + _, childECPubKey, err := crypto.DeriveChildECKeyPair( + masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { return err } - address := eth.PubkeyToAddress(*grandchildECPubKey.ToECDSA()) + address := eth.PubkeyToAddress(*childECPubKey.ToECDSA()) output := strings.ToLower(strings.TrimPrefix(address.String(), "0x")) fmt.Println(output) return nil diff --git a/cmd/principal.go b/cmd/principal.go index aec866d..74b829f 100644 --- a/cmd/principal.go +++ b/cmd/principal.go @@ -43,12 +43,8 @@ func (cmd *PrincipalCmd) Run() error { if err != nil { return err } - childXPrivKey, err := masterXPrivKey.Derive(0) - if err != nil { - return err - } - _, grandchildECPubKey, err := crypto.DeriveChildECKeyPair( - childXPrivKey, + _, childECPubKey, err := crypto.DeriveChildECKeyPair( + masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { diff --git a/cmd/private-key.go b/cmd/private-key.go index 3c05d19..a786901 100644 --- a/cmd/private-key.go +++ b/cmd/private-key.go @@ -53,18 +53,14 @@ func (cmd *PrivateKeyCmd) Run() error { if err != nil { return err } - childXPrivKey, err := masterXPrivKey.Derive(0) - if err != nil { - return err - } - grandchildECPrivKey, _, err := crypto.DeriveChildECKeyPair( - childXPrivKey, + childECPrivKey, _, err := crypto.DeriveChildECKeyPair( + masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { return err } - output, err := codec.ECPrivKeyToPEM(grandchildECPrivKey) + output, err := codec.ECPrivKeyToPEM(childECPrivKey) if err != nil { return err } diff --git a/cmd/public-key.go b/cmd/public-key.go index c587f60..8a23f22 100644 --- a/cmd/public-key.go +++ b/cmd/public-key.go @@ -43,18 +43,14 @@ func (cmd *PublicKeyCmd) Run() error { if err != nil { return err } - childXPrivKey, err := masterXPrivKey.Derive(0) - if err != nil { - return err - } - _, grandchildECPubKey, err := crypto.DeriveChildECKeyPair( - childXPrivKey, + _, childECPubKey, err := crypto.DeriveChildECKeyPair( + masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { return err } - output := hex.EncodeToString(grandchildECPubKey.SerializeUncompressed()) + output := hex.EncodeToString(childECPubKey.SerializeUncompressed()) fmt.Println(output) return nil } diff --git a/crypto/crypto.go b/crypto/crypto.go index f5904b1..30338e0 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -34,6 +34,11 @@ func DeriveChildECKeyPair( masterXPrivKey *hdkeychain.ExtendedKey, i uint32, ) (*btcec.PrivateKey, *btcec.PublicKey, error) { + // First apply the change. + childXPrivKey, err := masterXPrivKey.Derive(0) + if err != nil { + return nil, nil, err + } childXPrivKey, err := masterXPrivKey.Derive(i) if err != nil { return nil, nil, err From 9d945aaf084354cc0caf0e183c1074a49fe05399 Mon Sep 17 00:00:00 2001 From: Anton Astafiev Date: Tue, 20 Apr 2021 17:22:17 +0200 Subject: [PATCH 2/3] Move change inside crypto.go --- cmd/account.go | 10 +++------- cmd/legacy-address.go | 10 +++------- cmd/principal.go | 10 +++------- cmd/private-key.go | 10 +++------- cmd/public-key.go | 10 +++------- crypto/crypto.go | 7 ++++++- 6 files changed, 21 insertions(+), 36 deletions(-) diff --git a/cmd/account.go b/cmd/account.go index 273a940..5134bbb 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -43,18 +43,14 @@ func (cmd *AccountCmd) Run() error { if err != nil { return err } - childXPrivKey, err := masterXPrivKey.Derive(0) - if err != nil { - return err - } - _, grandchildECPubKey, err := crypto.DeriveChildECKeyPair( - childXPrivKey, + _, childECPubKey, err := crypto.DeriveChildECKeyPair( + masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { return err } - accountId, err := account.FromECPubKey(grandchildECPubKey) + accountId, err := account.FromECPubKey(childECPubKey) if err != nil { return err } diff --git a/cmd/legacy-address.go b/cmd/legacy-address.go index a8494fb..e584e21 100644 --- a/cmd/legacy-address.go +++ b/cmd/legacy-address.go @@ -44,18 +44,14 @@ func (cmd *LegacyAddressCmd) Run() error { if err != nil { return err } - childXPrivKey, err := masterXPrivKey.Derive(0) - if err != nil { - return err - } - _, grandchildECPubKey, err := crypto.DeriveChildECKeyPair( - childXPrivKey, + _, childECPubKey, err := crypto.DeriveChildECKeyPair( + masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { return err } - address := eth.PubkeyToAddress(*grandchildECPubKey.ToECDSA()) + address := eth.PubkeyToAddress(*childECPubKey.ToECDSA()) output := strings.ToLower(strings.TrimPrefix(address.String(), "0x")) fmt.Println(output) return nil diff --git a/cmd/principal.go b/cmd/principal.go index aec866d..79aca41 100644 --- a/cmd/principal.go +++ b/cmd/principal.go @@ -43,18 +43,14 @@ func (cmd *PrincipalCmd) Run() error { if err != nil { return err } - childXPrivKey, err := masterXPrivKey.Derive(0) - if err != nil { - return err - } - _, grandchildECPubKey, err := crypto.DeriveChildECKeyPair( - childXPrivKey, + _, childECPubKey, err := crypto.DeriveChildECKeyPair( + masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { return err } - principalId, err := principal.FromECPubKey(grandchildECPubKey) + principalId, err := principal.FromECPubKey(childECPubKey) if err != nil { return err } diff --git a/cmd/private-key.go b/cmd/private-key.go index 3c05d19..a786901 100644 --- a/cmd/private-key.go +++ b/cmd/private-key.go @@ -53,18 +53,14 @@ func (cmd *PrivateKeyCmd) Run() error { if err != nil { return err } - childXPrivKey, err := masterXPrivKey.Derive(0) - if err != nil { - return err - } - grandchildECPrivKey, _, err := crypto.DeriveChildECKeyPair( - childXPrivKey, + childECPrivKey, _, err := crypto.DeriveChildECKeyPair( + masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { return err } - output, err := codec.ECPrivKeyToPEM(grandchildECPrivKey) + output, err := codec.ECPrivKeyToPEM(childECPrivKey) if err != nil { return err } diff --git a/cmd/public-key.go b/cmd/public-key.go index c587f60..8a23f22 100644 --- a/cmd/public-key.go +++ b/cmd/public-key.go @@ -43,18 +43,14 @@ func (cmd *PublicKeyCmd) Run() error { if err != nil { return err } - childXPrivKey, err := masterXPrivKey.Derive(0) - if err != nil { - return err - } - _, grandchildECPubKey, err := crypto.DeriveChildECKeyPair( - childXPrivKey, + _, childECPubKey, err := crypto.DeriveChildECKeyPair( + masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { return err } - output := hex.EncodeToString(grandchildECPubKey.SerializeUncompressed()) + output := hex.EncodeToString(childECPubKey.SerializeUncompressed()) fmt.Println(output) return nil } diff --git a/crypto/crypto.go b/crypto/crypto.go index f5904b1..ed76ef8 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -34,7 +34,12 @@ func DeriveChildECKeyPair( masterXPrivKey *hdkeychain.ExtendedKey, i uint32, ) (*btcec.PrivateKey, *btcec.PublicKey, error) { - childXPrivKey, err := masterXPrivKey.Derive(i) + // First apply the change. + childXPrivKey, err := masterXPrivKey.Derive(0) + if err != nil { + return nil, nil, err + } + childXPrivKey, err = masterXPrivKey.Derive(i) if err != nil { return nil, nil, err } From 447230808034d553c6146b16cac1808bc7f77588 Mon Sep 17 00:00:00 2001 From: Enzo Haussecker Date: Tue, 20 Apr 2021 17:24:19 +0200 Subject: [PATCH 3/3] Clarify behavior of derivation function --- cmd/account.go | 4 ++-- cmd/legacy-address.go | 4 ++-- cmd/principal.go | 2 +- cmd/private-key.go | 4 ++-- cmd/public-key.go | 4 ++-- crypto/crypto.go | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/account.go b/cmd/account.go index 5134bbb..48dc826 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -43,14 +43,14 @@ func (cmd *AccountCmd) Run() error { if err != nil { return err } - _, childECPubKey, err := crypto.DeriveChildECKeyPair( + _, grandchildECPubKey, err := crypto.DeriveGrandchildECKeyPair( masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { return err } - accountId, err := account.FromECPubKey(childECPubKey) + accountId, err := account.FromECPubKey(grandchildECPubKey) if err != nil { return err } diff --git a/cmd/legacy-address.go b/cmd/legacy-address.go index e584e21..a4bc2aa 100644 --- a/cmd/legacy-address.go +++ b/cmd/legacy-address.go @@ -44,14 +44,14 @@ func (cmd *LegacyAddressCmd) Run() error { if err != nil { return err } - _, childECPubKey, err := crypto.DeriveChildECKeyPair( + _, grandchildECPubKey, err := crypto.DeriveGrandchildECKeyPair( masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { return err } - address := eth.PubkeyToAddress(*childECPubKey.ToECDSA()) + address := eth.PubkeyToAddress(*grandchildECPubKey.ToECDSA()) output := strings.ToLower(strings.TrimPrefix(address.String(), "0x")) fmt.Println(output) return nil diff --git a/cmd/principal.go b/cmd/principal.go index 74b829f..3ed5cd9 100644 --- a/cmd/principal.go +++ b/cmd/principal.go @@ -43,7 +43,7 @@ func (cmd *PrincipalCmd) Run() error { if err != nil { return err } - _, childECPubKey, err := crypto.DeriveChildECKeyPair( + _, grandchildECPubKey, err := crypto.DeriveGrandchildECKeyPair( masterXPrivKey, uint32(*cmd.Args.Index), ) diff --git a/cmd/private-key.go b/cmd/private-key.go index a786901..c3d597e 100644 --- a/cmd/private-key.go +++ b/cmd/private-key.go @@ -53,14 +53,14 @@ func (cmd *PrivateKeyCmd) Run() error { if err != nil { return err } - childECPrivKey, _, err := crypto.DeriveChildECKeyPair( + grandchildECPrivKey, _, err := crypto.DeriveGrandchildECKeyPair( masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { return err } - output, err := codec.ECPrivKeyToPEM(childECPrivKey) + output, err := codec.ECPrivKeyToPEM(grandchildECPrivKey) if err != nil { return err } diff --git a/cmd/public-key.go b/cmd/public-key.go index 8a23f22..07121f6 100644 --- a/cmd/public-key.go +++ b/cmd/public-key.go @@ -43,14 +43,14 @@ func (cmd *PublicKeyCmd) Run() error { if err != nil { return err } - _, childECPubKey, err := crypto.DeriveChildECKeyPair( + _, grandchildECPubKey, err := crypto.DeriveGrandchildECKeyPair( masterXPrivKey, uint32(*cmd.Args.Index), ) if err != nil { return err } - output := hex.EncodeToString(childECPubKey.SerializeUncompressed()) + output := hex.EncodeToString(grandchildECPubKey.SerializeUncompressed()) fmt.Println(output) return nil } diff --git a/crypto/crypto.go b/crypto/crypto.go index 30338e0..b279b27 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -30,7 +30,7 @@ func DeriveMasterXPrivKey(seed []byte) (*hdkeychain.ExtendedKey, error) { return masterXPrivKey, nil } -func DeriveChildECKeyPair( +func DeriveGrandchildECKeyPair( masterXPrivKey *hdkeychain.ExtendedKey, i uint32, ) (*btcec.PrivateKey, *btcec.PublicKey, error) { @@ -39,7 +39,7 @@ func DeriveChildECKeyPair( if err != nil { return nil, nil, err } - childXPrivKey, err := masterXPrivKey.Derive(i) + childXPrivKey, err = masterXPrivKey.Derive(i) if err != nil { return nil, nil, err }