Skip to content

Commit 138a34e

Browse files
authored
feat!: Add SIGN_MODE_TEXTUAL (#39)
1 parent 6376f7b commit 138a34e

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# CHANGELOG
2+
3+
## Unreleased
4+
5+
### API-Breaking Changes
6+
7+
* [#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`.

user_app.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"errors"
2121
"math"
2222

23-
"github.com/zondax/ledger-go"
23+
ledger_go "github.com/zondax/ledger-go"
2424
)
2525

2626
const (
@@ -114,14 +114,15 @@ func (ledger *LedgerCosmos) GetVersion() (*VersionInfo, error) {
114114
return &ledger.version, nil
115115
}
116116

117-
// SignSECP256K1 signs a transaction using Cosmos user app
117+
// SignSECP256K1 signs a transaction using Cosmos user app. It can either use
118+
// SIGN_MODE_LEGACY_AMINO_JSON (P2=0) or SIGN_MODE_TEXTUAL (P2=1).
118119
// this command requires user confirmation in the device
119-
func (ledger *LedgerCosmos) SignSECP256K1(bip32Path []uint32, transaction []byte) ([]byte, error) {
120+
func (ledger *LedgerCosmos) SignSECP256K1(bip32Path []uint32, transaction []byte, p2 byte) ([]byte, error) {
120121
switch ledger.version.Major {
121122
case 1:
122123
return ledger.signv1(bip32Path, transaction)
123124
case 2:
124-
return ledger.signv2(bip32Path, transaction)
125+
return ledger.signv2(bip32Path, transaction, p2)
125126
default:
126127
return nil, errors.New("App version is not supported")
127128
}
@@ -220,22 +221,26 @@ func (ledger *LedgerCosmos) signv1(bip32Path []uint32, transaction []byte) ([]by
220221
return finalResponse, nil
221222
}
222223

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

227228
var finalResponse []byte
228229

229230
var message []byte
230231

232+
if p2 > 1 {
233+
return nil, errors.New("only values of SIGN_MODE_LEGACY_AMINO (P2=0) and SIGN_MODE_TEXTUAL (P2=1) are allowed")
234+
}
235+
231236
for packetIndex <= packetCount {
232237
chunk := userMessageChunkSize
233238
if packetIndex == 1 {
234239
pathBytes, err := ledger.GetBip32bytes(bip32Path, 3)
235240
if err != nil {
236241
return nil, err
237242
}
238-
header := []byte{userCLA, userINSSignSECP256K1, 0, 0, byte(len(pathBytes))}
243+
header := []byte{userCLA, userINSSignSECP256K1, 0, p2, byte(len(pathBytes))}
239244
message = append(header, pathBytes...)
240245
} else {
241246
if len(transaction) < userMessageChunkSize {
@@ -247,7 +252,7 @@ func (ledger *LedgerCosmos) signv2(bip32Path []uint32, transaction []byte) ([]by
247252
payloadDesc = byte(2)
248253
}
249254

250-
header := []byte{userCLA, userINSSignSECP256K1, payloadDesc, 0, byte(chunk)}
255+
header := []byte{userCLA, userINSSignSECP256K1, payloadDesc, p2, byte(chunk)}
251256
message = append(header, transaction[:chunk]...)
252257
}
253258

user_app_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func Test_UserSign(t *testing.T) {
207207
path := []uint32{44, 118, 0, 0, 5}
208208

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

259-
_, err = userApp.SignSECP256K1(path, message)
259+
_, err = userApp.SignSECP256K1(path, message, 0)
260260
assert.Error(t, err)
261261
errMessage := err.Error()
262262

0 commit comments

Comments
 (0)