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

feat!: Add SIGN_MODE_TEXTUAL #39

Merged
merged 2 commits into from
Jan 16, 2023
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# CHANGELOG

## Unreleased

### API-Breaking Changes

* [#39](https://github.com/cosmos/ledger-cosmos-go/pull/39) Add support for SIGN_MODE_TEXTUAL by adding a new argument `p2 byte` to `SignSECP256K1`.
19 changes: 12 additions & 7 deletions user_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"errors"
"math"

"github.com/zondax/ledger-go"
ledger_go "github.com/zondax/ledger-go"
)

const (
Expand Down Expand Up @@ -114,14 +114,15 @@ func (ledger *LedgerCosmos) GetVersion() (*VersionInfo, error) {
return &ledger.version, nil
}

// SignSECP256K1 signs a transaction using Cosmos user app
// SignSECP256K1 signs a transaction using Cosmos user app. It can either use
// SIGN_MODE_LEGACY_AMINO_JSON (P2=0) or SIGN_MODE_TEXTUAL (P2=1).
// this command requires user confirmation in the device
func (ledger *LedgerCosmos) SignSECP256K1(bip32Path []uint32, transaction []byte) ([]byte, error) {
func (ledger *LedgerCosmos) SignSECP256K1(bip32Path []uint32, transaction []byte, p2 byte) ([]byte, error) {
switch ledger.version.Major {
case 1:
return ledger.signv1(bip32Path, transaction)
case 2:
return ledger.signv2(bip32Path, transaction)
return ledger.signv2(bip32Path, transaction, p2)
default:
return nil, errors.New("App version is not supported")
}
Expand Down Expand Up @@ -220,22 +221,26 @@ func (ledger *LedgerCosmos) signv1(bip32Path []uint32, transaction []byte) ([]by
return finalResponse, nil
}

func (ledger *LedgerCosmos) signv2(bip32Path []uint32, transaction []byte) ([]byte, error) {
func (ledger *LedgerCosmos) signv2(bip32Path []uint32, transaction []byte, p2 byte) ([]byte, error) {
var packetIndex byte = 1
var packetCount = 1 + byte(math.Ceil(float64(len(transaction))/float64(userMessageChunkSize)))

var finalResponse []byte

var message []byte

if p2 > 1 {
return nil, errors.New("only values of SIGN_MODE_LEGACY_AMINO (P2=0) and SIGN_MODE_TEXTUAL (P2=1) are allowed")
}

for packetIndex <= packetCount {
chunk := userMessageChunkSize
if packetIndex == 1 {
pathBytes, err := ledger.GetBip32bytes(bip32Path, 3)
if err != nil {
return nil, err
}
header := []byte{userCLA, userINSSignSECP256K1, 0, 0, byte(len(pathBytes))}
header := []byte{userCLA, userINSSignSECP256K1, 0, p2, byte(len(pathBytes))}
message = append(header, pathBytes...)
} else {
if len(transaction) < userMessageChunkSize {
Expand All @@ -247,7 +252,7 @@ func (ledger *LedgerCosmos) signv2(bip32Path []uint32, transaction []byte) ([]by
payloadDesc = byte(2)
}

header := []byte{userCLA, userINSSignSECP256K1, payloadDesc, 0, byte(chunk)}
header := []byte{userCLA, userINSSignSECP256K1, payloadDesc, p2, byte(chunk)}
message = append(header, transaction[:chunk]...)
}

Expand Down
4 changes: 2 additions & 2 deletions user_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func Test_UserSign(t *testing.T) {
path := []uint32{44, 118, 0, 0, 5}

message := getDummyTx()
signature, err := userApp.SignSECP256K1(path, message)
signature, err := userApp.SignSECP256K1(path, message, 0)
if err != nil {
t.Fatalf("[Sign] Error: %s\n", err.Error())
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func Test_UserSign_Fails(t *testing.T) {
garbage := []byte{65}
message = append(garbage, message...)

_, err = userApp.SignSECP256K1(path, message)
_, err = userApp.SignSECP256K1(path, message, 0)
assert.Error(t, err)
errMessage := err.Error()

Expand Down