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

Rebuild comm package #60

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: Test
on:
push:
branches:
- master
pull_request:

jobs:
Expand All @@ -13,12 +11,8 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.18'
go-version: '1.21'
- name: test & coverage report creation
run: |
go test common.go -mod=readonly -timeout 5m -short -race -coverprofile=coverage.txt -covermode=atomic
go test common.go -mod=readonly -timeout 5m
- uses: codecov/[email protected]
with:
file: ./coverage.txt
fail_ci_if_error: true
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2018 - 2022 ZondaX AG
Copyright 2018 - 2023 ZondaX AG

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
# ledger-cosmos-go

![zondax_light](docs/zondax_light.png#gh-light-mode-only)
![zondax_dark](docs/zondax_dark.png#gh-dark-mode-only)

[![Test](https://github.com/cosmos/ledger-cosmos-go/actions/workflows/test.yml/badge.svg)](https://github.com/cosmos/ledger-cosmos-go/actions/workflows/test.yml)
[![Build status](https://ci.appveyor.com/api/projects/status/ovpfx35t289n3403?svg=true)](https://ci.appveyor.com/project/cosmos/ledger-cosmos-go)

This project is work in progress. Some aspects are subject to change.
This package provides a basic client library to communicate with a Cosmos App running in a Ledger Nano S/S+/X device

| Operation | Response | Command |
| -------------------- | --------------------------- | -------------------------------- |
| GetVersion | app version | --------------- |
| GetAddressAndPubKey | pubkey + address | HRP + HDPath + ShowInDevice |
| Sign | signature in DER format | HDPath + HRP + SignMode* + Message |

*Available sign modes for Cosmos app: SignModeAmino (0) | SignModeTextual (1)


# Who we are?

# Get source
Apart from cloning, be sure you install dep dependency management tool
https://github.com/golang/dep
We are Zondax, a company pioneering blockchain services. If you want to know more about us, please visit us at [zondax.ch](https://zondax.ch)
118 changes: 55 additions & 63 deletions common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* (c) 2018 - 2022 ZondaX AG
* (c) 2018 - 2023 ZondaX AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,89 +18,81 @@

import (
"encoding/binary"
"errors"
"fmt"
"strconv"
"strings"
)

// VersionInfo contains app version information
type VersionInfo struct {
AppMode uint8
Major uint8
Minor uint8
Patch uint8
type VersionResponse struct {
AppMode uint8 // 0: release | 0xFF: debug
Major uint8
Minor uint8
Patch uint8
AppLocked uint8
TargetId uint32
}

func (c VersionInfo) String() string {
return fmt.Sprintf("%d.%d.%d", c.Major, c.Minor, c.Patch)
}

// VersionRequiredError the command is not supported by this app
type VersionRequiredError struct {
Found VersionInfo
Required VersionInfo
type AddressResponse struct {
pubkey []byte
address string
}

func (e VersionRequiredError) Error() string {
return fmt.Sprintf("App Version required %s - Version found: %s", e.Required, e.Found)
type SignatureResponse struct {
signatureDER []byte
ftheirs marked this conversation as resolved.
Show resolved Hide resolved
}

func NewVersionRequiredError(req VersionInfo, ver VersionInfo) error {
return &VersionRequiredError{
Found: ver,
Required: req,
}
func (c VersionResponse) String() string {
return fmt.Sprintf("%d.%d.%d", c.Major, c.Minor, c.Patch)
}

// CheckVersion compares the current version with the required version
func CheckVersion(ver VersionInfo, req VersionInfo) error {
if ver.Major != req.Major {
if ver.Major > req.Major {
return nil
}
return NewVersionRequiredError(req, ver)
// Validate HRP: Max length = 83
// All characters must be in range [33, 126], displayable chars in Ledger devices
func serializeHRP(hrp string) (hrpBytes []byte, err error) {
if len(hrp) > HRP_MAX_LENGTH {

Check failure on line 52 in common.go

View workflow job for this annotation

GitHub Actions / Test

undefined: HRP_MAX_LENGTH

Check failure on line 52 in common.go

View workflow job for this annotation

GitHub Actions / Test

undefined: HRP_MAX_LENGTH
return nil, errors.New("HRP len should be <= 83")
ftheirs marked this conversation as resolved.
Show resolved Hide resolved
}

if ver.Minor != req.Minor {
if ver.Minor > req.Minor {
return nil
hrpBytes = []byte(hrp)
for _, b := range hrpBytes {
if b < MIN_DISPLAYABLE_CHAR || b > MAX_DISPLAYABLE_CHAR {

Check failure on line 58 in common.go

View workflow job for this annotation

GitHub Actions / Test

undefined: MIN_DISPLAYABLE_CHAR

Check failure on line 58 in common.go

View workflow job for this annotation

GitHub Actions / Test

undefined: MAX_DISPLAYABLE_CHAR

Check failure on line 58 in common.go

View workflow job for this annotation

GitHub Actions / Test

undefined: MIN_DISPLAYABLE_CHAR

Check failure on line 58 in common.go

View workflow job for this annotation

GitHub Actions / Test

undefined: MAX_DISPLAYABLE_CHAR
return nil, errors.New("all characters in the HRP must be in the [33, 126] range")
}
return NewVersionRequiredError(req, ver)
}

if ver.Patch >= req.Patch {
return nil
}
return NewVersionRequiredError(req, ver)
return hrpBytes, nil
}

func GetBip32bytesv1(bip32Path []uint32, hardenCount int) ([]byte, error) {
message := make([]byte, 41)
if len(bip32Path) > 10 {
return nil, fmt.Errorf("maximum bip32 depth = 10")
func serializePath(path string) (pathBytes []byte, err error) {
if !strings.HasPrefix(path, "m/") {
return nil, errors.New(`path should start with "m/" (e.g "m/44'/118'/0'/0/3")`)
}
message[0] = byte(len(bip32Path))
for index, element := range bip32Path {
pos := 1 + index*4
value := element
if index < hardenCount {
value = 0x80000000 | element
}
binary.LittleEndian.PutUint32(message[pos:], value)
}
return message, nil
}

func GetBip32bytesv2(bip44Path []uint32, hardenCount int) ([]byte, error) {
message := make([]byte, 20)
if len(bip44Path) != 5 {
return nil, fmt.Errorf("path should contain 5 elements")
pathArray := strings.Split(path, "/")
pathArray = pathArray[1:] // remove "m"

if len(pathArray) != DEFAULT_PATH_LENGTH {

Check failure on line 74 in common.go

View workflow job for this annotation

GitHub Actions / Test

undefined: DEFAULT_PATH_LENGTH

Check failure on line 74 in common.go

View workflow job for this annotation

GitHub Actions / Test

undefined: DEFAULT_PATH_LENGTH
return nil, errors.New("invalid path: it must contain 5 elements")
}
for index, element := range bip44Path {
pos := index * 4
value := element
if index < hardenCount {
value = 0x80000000 | element

// Reserve 20 bytes for serialized path
buffer := make([]byte, 4*len(pathArray))

for i, child := range pathArray {
value := 0
if strings.HasSuffix(child, "'") {
value += HARDENED

Check failure on line 84 in common.go

View workflow job for this annotation

GitHub Actions / Test

undefined: HARDENED

Check failure on line 84 in common.go

View workflow job for this annotation

GitHub Actions / Test

undefined: HARDENED
child = strings.TrimSuffix(child, "'")
}
numChild, err := strconv.Atoi(child)
if err != nil {
return nil, fmt.Errorf("invalid path : %s is not a number (e.g \"m/44'/118'/0'/0/3\")", child)
}
if numChild >= HARDENED {

Check failure on line 91 in common.go

View workflow job for this annotation

GitHub Actions / Test

undefined: HARDENED

Check failure on line 91 in common.go

View workflow job for this annotation

GitHub Actions / Test

undefined: HARDENED
return nil, errors.New("incorrect child value (bigger or equal to 0x80000000)")
}
binary.LittleEndian.PutUint32(message[pos:], value)
value += numChild
binary.LittleEndian.PutUint32(buffer[i*4:], uint32(value))
}
return message, nil
return buffer, nil
}
Loading
Loading