Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add command to derive child key from master public key #44

Merged

Conversation

KonradStaniec
Copy link
Collaborator

  • add comand to derive private key from master private public key

Copy link
Member

@Lazar955 Lazar955 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work! lgtm

Comment on lines +54 to +88
func ParsePath(path string) ([]uint32, error) {
splitted := strings.Split(path, "/")

if len(splitted) != ExpectedDerivationDepth {
return nil, fmt.Errorf("invalid derivation path length")
}

h1, err := parseHardened(splitted[0])
if err != nil {
return nil, fmt.Errorf("invalid derivation path element at index 0: %w", err)
}

h2, err := parseHardened(splitted[1])
if err != nil {
return nil, fmt.Errorf("invalid derivation path element at index 1: %w", err)
}

h3, err := parseHardened(splitted[2])
if err != nil {
return nil, fmt.Errorf("invalid derivation path element at index 2: %w", err)
}

n4, err := parseNormal(splitted[3])
if err != nil {
return nil, fmt.Errorf("invalid derivation path element at index 3: %w", err)
}

n5, err := parseNormal(splitted[4])
if err != nil {
return nil, fmt.Errorf("invalid derivation path element at index 4: %w", err)
}

return []uint32{h1, h2, h3, n4, n5}, nil

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func ParsePath(path string) ([]uint32, error) {
splitted := strings.Split(path, "/")
if len(splitted) != ExpectedDerivationDepth {
return nil, fmt.Errorf("invalid derivation path length")
}
h1, err := parseHardened(splitted[0])
if err != nil {
return nil, fmt.Errorf("invalid derivation path element at index 0: %w", err)
}
h2, err := parseHardened(splitted[1])
if err != nil {
return nil, fmt.Errorf("invalid derivation path element at index 1: %w", err)
}
h3, err := parseHardened(splitted[2])
if err != nil {
return nil, fmt.Errorf("invalid derivation path element at index 2: %w", err)
}
n4, err := parseNormal(splitted[3])
if err != nil {
return nil, fmt.Errorf("invalid derivation path element at index 3: %w", err)
}
n5, err := parseNormal(splitted[4])
if err != nil {
return nil, fmt.Errorf("invalid derivation path element at index 4: %w", err)
}
return []uint32{h1, h2, h3, n4, n5}, nil
}
func ParsePath(path string) ([]uint32, error) {
const expectedDepth = 5
parsers := []func(string) (uint32, error){
parseHardened,
parseHardened,
parseHardened,
parseNormal,
parseNormal,
}
splitted := strings.Split(path, "/")
if len(splitted) != expectedDepth {
return nil, fmt.Errorf("invalid derivation path length")
}
result := make([]uint32, expectedDepth)
for i, part := range splitted {
parsed, err := parsers[i](part)
if err != nil {
return nil, fmt.Errorf("invalid derivation path element at index %d: %w", i, err)
}
result[i] = parsed
}
return result, nil
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion, you don't have to accept it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh tbh I find this flow a bit convoluted

Current is a bit verbose, but it is super simple. (though it may be one of those things that everybody sees a bit different)

I will leave it as it is.

return nil, fmt.Errorf("invalid derivation path: %w", err)
}

var keyResult *hdkeychain.ExtendedKey = parsedMasterKey
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var keyResult *hdkeychain.ExtendedKey = parsedMasterKey
keyResult := parsedMasterKey

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bit less verbose

@KonradStaniec KonradStaniec force-pushed the konradstaniec/add-command-to-derive-child-private-key branch from 9d75b9b to a8f2425 Compare November 26, 2024 13:24
@KonradStaniec KonradStaniec merged commit 6b2d632 into main Nov 26, 2024
13 checks passed
@KonradStaniec KonradStaniec deleted the konradstaniec/add-command-to-derive-child-private-key branch November 26, 2024 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants