Skip to content

Commit

Permalink
Merge pull request #6 from dfinity/fix-change
Browse files Browse the repository at this point in the history
Clarify behavior of derivation function
  • Loading branch information
enzoh authored Apr 20, 2021
2 parents 4250cfe + 7e95b52 commit 304deb9
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/legacy-address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ func (cmd *PrincipalCmd) 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
}
principalId, err := principal.FromECPubKey(childECPubKey)
principalId, err := principal.FromECPubKey(grandchildECPubKey)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/private-key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/public-key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 7 additions & 7 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ func DeriveMasterXPrivKey(seed []byte) (*hdkeychain.ExtendedKey, error) {
return masterXPrivKey, nil
}

func DeriveChildECKeyPair(
func DeriveGrandchildECKeyPair(
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 = childXPrivKey.Derive(i)
return nil, nil, err
}
grandchildXPrivKey, err := childXPrivKey.Derive(i)
if err != nil {
return nil, nil, err
}
childECPrivKey, err := childXPrivKey.ECPrivKey()
grandchildECPrivKey, err := grandchildXPrivKey.ECPrivKey()
if err != nil {
return nil, nil, err
}
childECPubKey := childECPrivKey.PubKey()
return childECPrivKey, childECPubKey, nil
grandchildECPubKey := grandchildECPrivKey.PubKey()
return grandchildECPrivKey, grandchildECPubKey, nil
}

0 comments on commit 304deb9

Please sign in to comment.