Skip to content

Commit fe0199f

Browse files
authored
Remove v1 code (#84)
- Removed all v1 legacy code and validator app support - Refactored codebase to follow Go best practices: - Added exported constants and sentinel errors - Converted tests to table-driven style - Improved documentation with GoDoc comments - Extracted helper functions for better readability Close #52
1 parent 9e3c918 commit fe0199f

File tree

8 files changed

+472
-757
lines changed

8 files changed

+472
-757
lines changed

common.go

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,89 +18,99 @@ package ledger_cosmos_go
1818

1919
import (
2020
"encoding/binary"
21+
"errors"
2122
"fmt"
2223
)
2324

24-
// VersionInfo contains app version information
25+
const (
26+
// bip32PathLength is the required number of elements in a BIP32/BIP44 path
27+
bip32PathLength = 5
28+
29+
// bip32BytesLength is the byte length of the serialized BIP32 path (5 elements * 4 bytes)
30+
bip32BytesLength = 20
31+
32+
// hardenedOffset is the offset added to hardened path elements
33+
hardenedOffset = 0x80000000
34+
)
35+
36+
// ErrInvalidPathLength is returned when the BIP32 path doesn't have exactly 5 elements
37+
var ErrInvalidPathLength = errors.New("BIP32 path must contain exactly 5 elements")
38+
39+
// VersionInfo contains app version information.
2540
type VersionInfo struct {
2641
AppMode uint8
2742
Major uint8
2843
Minor uint8
2944
Patch uint8
3045
}
3146

32-
func (c VersionInfo) String() string {
33-
return fmt.Sprintf("%d.%d.%d", c.Major, c.Minor, c.Patch)
47+
// String returns the version as a string in the format "Major.Minor.Patch".
48+
func (v VersionInfo) String() string {
49+
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
3450
}
3551

36-
// VersionRequiredError the command is not supported by this app
52+
// VersionRequiredError indicates that the app version does not meet the minimum requirements.
3753
type VersionRequiredError struct {
3854
Found VersionInfo
3955
Required VersionInfo
4056
}
4157

42-
func (e VersionRequiredError) Error() string {
43-
return fmt.Sprintf("App Version required %s - Version found: %s", e.Required, e.Found)
58+
// Error implements the error interface for VersionRequiredError.
59+
func (e *VersionRequiredError) Error() string {
60+
return fmt.Sprintf("app version required %s - version found: %s", e.Required, e.Found)
4461
}
4562

46-
func NewVersionRequiredError(req VersionInfo, ver VersionInfo) error {
63+
// NewVersionRequiredError creates a new VersionRequiredError.
64+
func NewVersionRequiredError(required, found VersionInfo) error {
4765
return &VersionRequiredError{
48-
Found: ver,
49-
Required: req,
66+
Found: found,
67+
Required: required,
5068
}
5169
}
5270

53-
// CheckVersion compares the current version with the required version
54-
func CheckVersion(ver VersionInfo, req VersionInfo) error {
55-
if ver.Major != req.Major {
56-
if ver.Major > req.Major {
57-
return nil
58-
}
59-
return NewVersionRequiredError(req, ver)
71+
// CheckVersion compares the current version with the required version.
72+
// Returns nil if the current version meets or exceeds the required version.
73+
func CheckVersion(current, required VersionInfo) error {
74+
if current.Major > required.Major {
75+
return nil
6076
}
61-
62-
if ver.Minor != req.Minor {
63-
if ver.Minor > req.Minor {
64-
return nil
65-
}
66-
return NewVersionRequiredError(req, ver)
77+
if current.Major < required.Major {
78+
return NewVersionRequiredError(required, current)
6779
}
6880

69-
if ver.Patch >= req.Patch {
81+
// Major versions are equal, check minor
82+
if current.Minor > required.Minor {
7083
return nil
7184
}
72-
return NewVersionRequiredError(req, ver)
73-
}
74-
75-
func GetBip32bytesv1(bip32Path []uint32, hardenCount int) ([]byte, error) {
76-
message := make([]byte, 41)
77-
if len(bip32Path) > 10 {
78-
return nil, fmt.Errorf("maximum bip32 depth = 10")
85+
if current.Minor < required.Minor {
86+
return NewVersionRequiredError(required, current)
7987
}
80-
message[0] = byte(len(bip32Path))
81-
for index, element := range bip32Path {
82-
pos := 1 + index*4
83-
value := element
84-
if index < hardenCount {
85-
value = 0x80000000 | element
86-
}
87-
binary.LittleEndian.PutUint32(message[pos:], value)
88+
89+
// Minor versions are equal, check patch
90+
if current.Patch >= required.Patch {
91+
return nil
8892
}
89-
return message, nil
93+
94+
return NewVersionRequiredError(required, current)
9095
}
9196

92-
func GetBip32bytesv2(bip44Path []uint32, hardenCount int) ([]byte, error) {
93-
message := make([]byte, 20)
94-
if len(bip44Path) != 5 {
95-
return nil, fmt.Errorf("path should contain 5 elements")
97+
// GetBip32bytes serializes a BIP32/BIP44 path into bytes for the Ledger device.
98+
// The path must contain exactly 5 elements. Elements at indices less than hardenCount
99+
// will be hardened (have 0x80000000 added to them).
100+
func GetBip32bytes(path []uint32, hardenCount int) ([]byte, error) {
101+
if len(path) != bip32PathLength {
102+
return nil, ErrInvalidPathLength
96103
}
97-
for index, element := range bip44Path {
98-
pos := index * 4
104+
105+
result := make([]byte, bip32BytesLength)
106+
107+
for i, element := range path {
99108
value := element
100-
if index < hardenCount {
101-
value = 0x80000000 | element
109+
if i < hardenCount {
110+
value |= hardenedOffset
102111
}
103-
binary.LittleEndian.PutUint32(message[pos:], value)
112+
binary.LittleEndian.PutUint32(result[i*4:], value)
104113
}
105-
return message, nil
114+
115+
return result, nil
106116
}

0 commit comments

Comments
 (0)