-
Notifications
You must be signed in to change notification settings - Fork 3
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
add command to derive child key from master public key #44
Conversation
KonradStaniec
commented
Nov 26, 2024
- add comand to derive private key from master private public key
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice work! lgtm
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 | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 | |
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var keyResult *hdkeychain.ExtendedKey = parsedMasterKey | |
keyResult := parsedMasterKey |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a bit less verbose
9d75b9b
to
a8f2425
Compare