From d174393ebc42e37e7b4031a75b3af9149eca8b9f Mon Sep 17 00:00:00 2001 From: Anton Astafiev Date: Tue, 20 Apr 2021 17:26:14 +0200 Subject: [PATCH] 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..562a248 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 = childXPrivKey.Derive(i) if err != nil { return nil, nil, err }