From 138a34ea26362bede345dbaf2523b44c37252b2c Mon Sep 17 00:00:00 2001 From: Amaury <1293565+amaurym@users.noreply.github.com> Date: Mon, 16 Jan 2023 15:57:47 +0100 Subject: [PATCH] feat!: Add SIGN_MODE_TEXTUAL (#39) --- CHANGELOG.md | 7 +++++++ user_app.go | 19 ++++++++++++------- user_app_test.go | 4 ++-- 3 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..539e6d7 --- /dev/null +++ b/CHANGELOG.md @@ -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`. diff --git a/user_app.go b/user_app.go index 63b33cc..32f596e 100644 --- a/user_app.go +++ b/user_app.go @@ -20,7 +20,7 @@ import ( "errors" "math" - "github.com/zondax/ledger-go" + ledger_go "github.com/zondax/ledger-go" ) const ( @@ -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") } @@ -220,7 +221,7 @@ 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))) @@ -228,6 +229,10 @@ func (ledger *LedgerCosmos) signv2(bip32Path []uint32, transaction []byte) ([]by 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 { @@ -235,7 +240,7 @@ func (ledger *LedgerCosmos) signv2(bip32Path []uint32, transaction []byte) ([]by 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 { @@ -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]...) } diff --git a/user_app_test.go b/user_app_test.go index 4c3d347..0597c27 100644 --- a/user_app_test.go +++ b/user_app_test.go @@ -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()) } @@ -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()