Skip to content

Commit bc26c51

Browse files
authored
Support to crypto curves (#35)
* feat: add support to many curves on go funcs * feat: add dependabot for gomods * fix: dependabot config * fix: remove dependabot changes * fix: app_test file
1 parent 0f19f52 commit bc26c51

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-44
lines changed

app.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,13 @@ func (ledger *LedgerFilecoin) GetVersion() (*VersionInfo, error) {
145145

146146
// Deprecated: Use Sign method instead.
147147
func (ledger *LedgerFilecoin) SignSECP256K1(bip44Path []uint32, transaction []byte) (*SignatureAnswer, error) {
148-
return ledger.Sign(bip44Path, transaction)
148+
return ledger.Sign(bip44Path, transaction, SECP256K1)
149149
}
150150

151-
152-
// SignSECP256K1 signs a transaction using Filecoin user app
151+
// Sign signs a transaction using Filecoin user app
153152
// this command requires user confirmation in the device
154-
func (ledger *LedgerFilecoin) Sign(bip44Path []uint32, transaction []byte) (*SignatureAnswer, error) {
155-
signatureBytes, err := ledger.sign(bip44Path, transaction)
153+
func (ledger *LedgerFilecoin) Sign(bip44Path []uint32, transaction []byte, curve CryptoCurve) (*SignatureAnswer, error) {
154+
signatureBytes, err := ledger.sign(bip44Path, transaction, curve)
156155
if err != nil {
157156
return nil, err
158157
}
@@ -171,43 +170,39 @@ func (ledger *LedgerFilecoin) Sign(bip44Path []uint32, transaction []byte) (*Sig
171170
return &signatureAnswer, nil
172171
}
173172

174-
175-
176173
// Deprecated: Use GetPublicKey instead.
177174
func (ledger *LedgerFilecoin) GetPublicKeySECP256K1(bip44Path []uint32) ([]byte, error) {
178-
pubkey, err := ledger.GetPublicKey(bip44Path)
175+
pubkey, err := ledger.GetPublicKey(bip44Path, SECP256K1)
179176
return pubkey, err
180177
}
181178

182-
183-
// GetPublicKeySECP256K1 retrieves the public key for the corresponding bip44 derivation path
179+
// GetPublicKey retrieves the public key for the corresponding bip44 derivation path
184180
// this command DOES NOT require user confirmation in the device
185-
func (ledger *LedgerFilecoin) GetPublicKey(bip44Path []uint32) ([]byte, error) {
186-
pubkey, _, _, err := ledger.retrieveAddressPubKey(bip44Path, false)
181+
func (ledger *LedgerFilecoin) GetPublicKey(bip44Path []uint32, curve CryptoCurve) ([]byte, error) {
182+
pubkey, _, _, err := ledger.retrieveAddressPubKey(bip44Path, curve, false)
187183
return pubkey, err
188184
}
189185

190186
// Deprecated: Use GetAddressPubKey instead.
191187
func (ledger *LedgerFilecoin) GetAddressPubKeySECP256K1(bip44Path []uint32) (pubkey []byte, addrByte []byte, addrString string, err error) {
192-
return ledger.GetAddressPubKey(bip44Path)
188+
return ledger.GetAddressPubKey(bip44Path, SECP256K1)
193189
}
194190

195-
// GetAddressPubKeySECP256K1 returns the pubkey and addresses
191+
// GetAddressPubKey returns the pubkey and addresses
196192
// this command does not require user confirmation
197-
func (ledger *LedgerFilecoin) GetAddressPubKey(bip44Path []uint32) (pubkey []byte, addrByte []byte, addrString string, err error) {
198-
return ledger.retrieveAddressPubKey(bip44Path, false)
193+
func (ledger *LedgerFilecoin) GetAddressPubKey(bip44Path []uint32, curve CryptoCurve) (pubkey []byte, addrByte []byte, addrString string, err error) {
194+
return ledger.retrieveAddressPubKey(bip44Path, curve, false)
199195
}
200196

201-
202197
// Deprecated: Use ShowAddressPubKey instead.
203198
func (ledger *LedgerFilecoin) ShowAddressPubKeySECP256K1(bip44Path []uint32) (pubkey []byte, addrByte []byte, addrString string, err error) {
204-
return ledger.ShowAddressPubKey(bip44Path)
199+
return ledger.ShowAddressPubKey(bip44Path, SECP256K1)
205200
}
206201

207-
// ShowAddressPubKeySECP256K1 returns the pubkey (compressed) and addresses
202+
// ShowAddressPubKey returns the pubkey (compressed) and addresses
208203
// this command requires user confirmation in the device
209-
func (ledger *LedgerFilecoin) ShowAddressPubKey(bip44Path []uint32) (pubkey []byte, addrByte []byte, addrString string, err error) {
210-
return ledger.retrieveAddressPubKey(bip44Path, true)
204+
func (ledger *LedgerFilecoin) ShowAddressPubKey(bip44Path []uint32, curve CryptoCurve) (pubkey []byte, addrByte []byte, addrString string, err error) {
205+
return ledger.retrieveAddressPubKey(bip44Path, curve, true)
211206
}
212207

213208
func (ledger *LedgerFilecoin) GetBip44bytes(bip44Path []uint32, hardenCount int) ([]byte, error) {
@@ -219,7 +214,10 @@ func (ledger *LedgerFilecoin) GetBip44bytes(bip44Path []uint32, hardenCount int)
219214
return pathBytes, nil
220215
}
221216

222-
func (ledger *LedgerFilecoin) sign(bip44Path []uint32, transaction []byte) ([]byte, error) {
217+
func (ledger *LedgerFilecoin) sign(bip44Path []uint32, transaction []byte, curve CryptoCurve) ([]byte, error) {
218+
if err := isCryptoCurveSupported(curve); err != nil {
219+
return nil, err
220+
}
223221

224222
pathBytes, err := ledger.GetBip44bytes(bip44Path, HardenCount)
225223
if err != nil {
@@ -277,7 +275,11 @@ func (ledger *LedgerFilecoin) sign(bip44Path []uint32, transaction []byte) ([]by
277275
}
278276

279277
// retrieveAddressPubKey returns the pubkey and address
280-
func (ledger *LedgerFilecoin) retrieveAddressPubKey(bip44Path []uint32, requireConfirmation bool) (pubkey []byte, addrByte []byte, addrString string, err error) {
278+
func (ledger *LedgerFilecoin) retrieveAddressPubKey(bip44Path []uint32, curve CryptoCurve, requireConfirmation bool) (pubkey []byte, addrByte []byte, addrString string, err error) {
279+
if err := isCryptoCurveSupported(curve); err != nil {
280+
return nil, nil, "", err
281+
}
282+
281283
pathBytes, err := ledger.GetBip44bytes(bip44Path, HardenCount)
282284
if err != nil {
283285
return nil, nil, "", err

app_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func Test_UserGetPublicKey(t *testing.T) {
6666

6767
path := []uint32{44, 461, 5, 0, 21}
6868

69-
pubKey, err := app.GetPublicKey(path)
69+
pubKey, err := app.GetPublicKey(path, SECP256K1)
7070
if err != nil {
7171
t.Fatalf("Detected error, err: %s\n", err.Error())
7272
}
@@ -90,7 +90,7 @@ func Test_GetAddressPubKeySECP256K1_Zero(t *testing.T) {
9090

9191
path := []uint32{44, 461, 0, 0, 0}
9292

93-
pubKey, addrByte, addrString, err := app.GetAddressPubKey(path)
93+
pubKey, addrByte, addrString, err := app.GetAddressPubKey(path, SECP256K1)
9494
if err != nil {
9595
t.Fatalf("Detected error, err: %s\n", err.Error())
9696
}
@@ -116,7 +116,7 @@ func Test_GetAddressPubKeySECP256K1(t *testing.T) {
116116

117117
path := []uint32{44, 461, 5, 0, 21}
118118

119-
pubKey, addrByte, addrString, err := app.GetAddressPubKey(path)
119+
pubKey, addrByte, addrString, err := app.GetAddressPubKey(path, SECP256K1)
120120
if err != nil {
121121
t.Fatalf("Detected error, err: %s\n", err.Error())
122122
}
@@ -142,7 +142,7 @@ func Test_ShowAddressPubKeySECP256K1(t *testing.T) {
142142

143143
path := []uint32{44, 461, 5, 0, 21}
144144

145-
pubKey, addrByte, addrString, err := app.ShowAddressPubKey(path)
145+
pubKey, addrByte, addrString, err := app.ShowAddressPubKey(path, SECP256K1)
146146
if err != nil {
147147
t.Fatalf("Detected error, err: %s\n", err.Error())
148148
}
@@ -185,7 +185,7 @@ func Test_UserPK_HDPaths(t *testing.T) {
185185
for i := uint32(0); i < 10; i++ {
186186
path[4] = i
187187

188-
pubKey, err := app.GetPublicKey(path)
188+
pubKey, err := app.GetPublicKey(path, SECP256K1)
189189
if err != nil {
190190
t.Fatalf("Detected error, err: %s\n", err.Error())
191191
}
@@ -214,13 +214,13 @@ func Test_Sign(t *testing.T) {
214214

215215
message, _ := hex.DecodeString("8a0058310396a1a3e4ea7a14d49985e661b22401d44fed402d1d0925b243c923589c0fbc7e32cd04e29ed78d15d37d3aaa3fe6da3358310386b454258c589475f7d16f5aac018a79f6c1169d20fc33921dd8b5ce1cac6c348f90a3603624f6aeb91b64518c2e80950144000186a01961a8430009c44200000040")
216216

217-
signature, err := app.Sign(path, message)
217+
signature, err := app.Sign(path, message, SECP256K1)
218218
if err != nil {
219219
t.Fatalf("[Sign] Error: %s\n", err.Error())
220220
}
221221

222222
// Verify Signature
223-
pubKey, err := app.GetPublicKey(path)
223+
pubKey, err := app.GetPublicKey(path, SECP256K1)
224224
if err != nil {
225225
t.Fatalf("Detected error, err: %s\n", err.Error())
226226
}
@@ -261,13 +261,13 @@ func Test_Sign2(t *testing.T) {
261261

262262
message, _ := hex.DecodeString("8a0055019f4c34943e4b92f4542bed08af54be955629fc6f5501ef8fd1e48a1e0f1a49310ec675bc677a3954147400430003e81903e84200014200010040")
263263

264-
signature, err := app.Sign(path, message)
264+
signature, err := app.Sign(path, message, SECP256K1)
265265
if err != nil {
266266
t.Fatalf("[Sign] Error: %s\n", err.Error())
267267
}
268268

269269
// Verify Signature
270-
pubKey, err := app.GetPublicKey(path)
270+
pubKey, err := app.GetPublicKey(path, SECP256K1)
271271
if err != nil {
272272
t.Fatalf("Detected error, err: %s\n", err.Error())
273273
}
@@ -310,13 +310,13 @@ func Test_Sign3(t *testing.T) {
310310

311311
message, _ := hex.DecodeString("8a0055019f4c34943e4b92f4542bed08af54be955629fc6f5501ef8fd1e48a1e0f1a49310ec675bc677a3954147400430003e81903e84200014200010040")
312312

313-
signature, err := app.Sign(path, message)
313+
signature, err := app.Sign(path, message, SECP256K1)
314314
if err != nil {
315315
t.Fatalf("[Sign] Error: %s\n", err.Error())
316316
}
317317

318318
// Verify Signature
319-
pubKey, err := app.GetPublicKey(path)
319+
pubKey, err := app.GetPublicKey(path, SECP256K1)
320320
if err != nil {
321321
t.Fatalf("Detected error, err: %s\n", err.Error())
322322
}
@@ -377,7 +377,7 @@ func Test_Sign_Fails(t *testing.T) {
377377
garbage := []byte{65}
378378
message = append(garbage, message...)
379379

380-
_, err = app.Sign(path, message)
380+
_, err = app.Sign(path, message, SECP256K1)
381381
assert.Error(t, err)
382382
errMessage := err.Error()
383383
assert.Equal(t, errMessage, "Unexpected data type")
@@ -386,7 +386,7 @@ func Test_Sign_Fails(t *testing.T) {
386386
garbage = []byte{65}
387387
message = append(message, garbage...)
388388

389-
_, err = app.Sign(path, message)
389+
_, err = app.Sign(path, message, SECP256K1)
390390
assert.Error(t, err)
391391
errMessage = err.Error()
392392
assert.Equal(t, errMessage, "Unexpected CBOR EOF")

types.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import (
2222
)
2323

2424
const (
25-
CLA = 0x06
25+
CLA = 0x06
2626

27-
INSGetVersion = 0
28-
INSGetAddr = 1
29-
INSSign = 2
30-
INSSignDataCap = 5
31-
INSSignClientDeal = 6
32-
INSSignRawBytes = 7
27+
INSGetVersion = 0
28+
INSGetAddr = 1
29+
INSSign = 2
30+
INSSignDataCap = 5
31+
INSSignClientDeal = 6
32+
INSSignRawBytes = 7
3333
)
3434

3535
const (
@@ -40,6 +40,13 @@ const (
4040

4141
const HardenCount int = 2
4242

43+
type CryptoCurve uint64
44+
45+
const (
46+
SECP256K1 CryptoCurve = iota
47+
BLS
48+
)
49+
4350
// LedgerFilecoin represents a connection to the Ledger app
4451
type LedgerFilecoin struct {
4552
api ledger_go.LedgerDevice
@@ -61,7 +68,6 @@ type VersionInfo struct {
6168
Patch uint8
6269
}
6370

64-
6571
// VersionRequiredError the command is not supported by this app
6672
type VersionRequiredError struct {
6773
Found VersionInfo

utils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ledger_filecoin_go
2+
3+
import "fmt"
4+
5+
func isCryptoCurveSupported(curve CryptoCurve) error {
6+
switch curve {
7+
case SECP256K1:
8+
return nil
9+
default:
10+
return fmt.Errorf("curve not supported yet")
11+
12+
}
13+
}

0 commit comments

Comments
 (0)