Skip to content

Commit

Permalink
fix: ledger discover
Browse files Browse the repository at this point in the history
  • Loading branch information
amritkumarj committed Jan 16, 2024
1 parent b2046c5 commit a8bc261
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/btcsuite/btcd/btcutil v1.1.3
github.com/cockroachdb/apd/v3 v3.2.1
github.com/cosmos/ledger-cosmos-go v0.13.3
github.com/davecgh/go-spew v1.1.1
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0
github.com/dgraph-io/badger/v3 v3.2103.4
Expand Down Expand Up @@ -40,19 +41,26 @@ require (
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/zondax/hid v0.9.2 // indirect
github.com/zondax/ledger-go v0.14.3 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c // indirect
google.golang.org/grpc v1.58.3 // indirect
)

require (
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c // indirect
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v1.12.1 // indirect
Expand Down
18 changes: 15 additions & 3 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tm2/pkg/crypto/ledger/ledger_real.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ledger

import (
ledger "github.com/cosmos/ledger-cosmos-go"
)

func init() {
discoverLedger = func() (LedgerSECP256K1, error) {
device, err := ledger.FindLedgerCosmosUserApp()
if err != nil {
return nil, err
}

return device, nil
}
}
29 changes: 24 additions & 5 deletions tm2/pkg/crypto/ledger/ledger_secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package ledger

import (
"fmt"
"math/big"
"os"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
secp "github.com/decred/dcrd/dcrec/secp256k1/v4"

"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/crypto"
Expand All @@ -32,7 +34,7 @@ type (
// Returns a compressed pubkey and bech32 address (requires user confirmation)
GetAddressPubKeySECP256K1([]uint32, string) ([]byte, string, error)
// Signs a message (requires user confirmation)
SignSECP256K1([]uint32, []byte) ([]byte, error)
SignSECP256K1([]uint32, []byte, byte) ([]byte, error)
}

// PrivKeyLedgerSecp256k1 implements PrivKey, calling the ledger nano we
Expand Down Expand Up @@ -167,12 +169,30 @@ func warnIfErrors(f func() error) {
}

func convertDERtoBER(signatureDER []byte) ([]byte, error) {
sigDER, err := ecdsa.ParseDERSignature(signatureDER[:])
sigDER, err := ecdsa.ParseDERSignature(signatureDER)
if err != nil {
return nil, err
}

return sigDER.Serialize(), nil
sigStr := sigDER.Serialize()
// The format of a DER encoded signature is as follows:
// 0x30 <total length> 0x02 <length of R> <R> 0x02 <length of S> <S>
r, s := new(big.Int), new(big.Int)
r.SetBytes(sigStr[4 : 4+sigStr[3]])
s.SetBytes(sigStr[4+sigStr[3]+2:])

sModNScalar := new(secp.ModNScalar)
sModNScalar.SetByteSlice(s.Bytes())
if sModNScalar.IsOverHalfOrder() {
s = new(big.Int).Sub(secp.S256().N, s)
}

sigBytes := make([]byte, 64)
// 0 pad the byte arrays from the left if they aren't big enough.
copy(sigBytes[32-len(r.Bytes()):32], r.Bytes())
copy(sigBytes[64-len(s.Bytes()):64], s.Bytes())

return sigBytes, nil
}

func getLedgerDevice() (LedgerSECP256K1, error) {
Expand Down Expand Up @@ -212,8 +232,7 @@ func sign(device LedgerSECP256K1, pkl PrivKeyLedgerSecp256k1, msg []byte) ([]byt
if err != nil {
return nil, err
}

sig, err := device.SignSECP256K1(pkl.Path.DerivationPath(), msg)
sig, err := device.SignSECP256K1(pkl.Path.DerivationPath(), msg, 0)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit a8bc261

Please sign in to comment.