Skip to content

Commit

Permalink
Merge pull request #16 from Zondax/dev
Browse files Browse the repository at this point in the history
some updates
  • Loading branch information
ftheirs authored Mar 8, 2024
2 parents 4dea4ef + 29ef6fc commit 3f23fe4
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 99 deletions.
18 changes: 0 additions & 18 deletions .circleci/config.yml

This file was deleted.

16 changes: 16 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: daily
time: "11:00"
open-pull-requests-limit: 10
- package-ecosystem: gomod
directory: "/"
schedule:
interval: daily
time: "11:00"
open-pull-requests-limit: 10
labels:
- T:dependencies
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Test
on:
workflow_dispatch:
push:
pull_request:
branches:
- main

jobs:
Test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: "1.21"
- name: Testing build
run: go build
- name: Run common tests
run: go test -run '.' common_test.go common.go types.go
84 changes: 39 additions & 45 deletions app.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* (c) 2019 ZondaX GmbH
* (c) 2019 - 2023 ZondaX AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,38 +22,6 @@ import (
ledger_go "github.com/zondax/ledger-go"
)

const (
CLA = 0x06

INSGetVersion = 0
INSGetAddrSECP256K1 = 1
INSSignSECP256K1 = 2
INSSignDataCap = 5
INSSignClientDeal = 6
INSSignRawBytes = 7
)

const (
PayloadChunkInit = 0
PayloadChunkAdd = 1
PayloadChunkLast = 2
)

const HardenCount int = 2

// LedgerFilecoin represents a connection to the Ledger app
type LedgerFilecoin struct {
api ledger_go.LedgerDevice
version VersionInfo
}

type SignatureAnswer struct {
r []byte
s []byte
v uint8
derSignature []byte
}

func (sa *SignatureAnswer) SignatureBytes() []byte {
out := make([]byte, 65)
copy(out[:32], sa.r)
Expand Down Expand Up @@ -175,9 +143,15 @@ func (ledger *LedgerFilecoin) GetVersion() (*VersionInfo, error) {
return &ledger.version, nil
}

// Deprecated: Use Sign method instead.
func (ledger *LedgerFilecoin) SignSECP256K1(bip44Path []uint32, transaction []byte) (*SignatureAnswer, error) {
return ledger.Sign(bip44Path, transaction)
}


// SignSECP256K1 signs a transaction using Filecoin user app
// this command requires user confirmation in the device
func (ledger *LedgerFilecoin) SignSECP256K1(bip44Path []uint32, transaction []byte) (*SignatureAnswer, error) {
func (ledger *LedgerFilecoin) Sign(bip44Path []uint32, transaction []byte) (*SignatureAnswer, error) {
signatureBytes, err := ledger.sign(bip44Path, transaction)
if err != nil {
return nil, err
Expand All @@ -197,23 +171,43 @@ func (ledger *LedgerFilecoin) SignSECP256K1(bip44Path []uint32, transaction []by
return &signatureAnswer, nil
}



// Deprecated: Use GetPublicKey instead.
func (ledger *LedgerFilecoin) GetPublicKeySECP256K1(bip44Path []uint32) ([]byte, error) {
pubkey, err := ledger.GetPublicKey(bip44Path)
return pubkey, err
}


// GetPublicKeySECP256K1 retrieves the public key for the corresponding bip44 derivation path
// this command DOES NOT require user confirmation in the device
func (ledger *LedgerFilecoin) GetPublicKeySECP256K1(bip44Path []uint32) ([]byte, error) {
pubkey, _, _, err := ledger.retrieveAddressPubKeySECP256K1(bip44Path, false)
func (ledger *LedgerFilecoin) GetPublicKey(bip44Path []uint32) ([]byte, error) {
pubkey, _, _, err := ledger.retrieveAddressPubKey(bip44Path, false)
return pubkey, err
}

// Deprecated: Use GetAddressPubKey instead.
func (ledger *LedgerFilecoin) GetAddressPubKeySECP256K1(bip44Path []uint32) (pubkey []byte, addrByte []byte, addrString string, err error) {
return ledger.GetAddressPubKey(bip44Path)
}

// GetAddressPubKeySECP256K1 returns the pubkey and addresses
// this command does not require user confirmation
func (ledger *LedgerFilecoin) GetAddressPubKeySECP256K1(bip44Path []uint32) (pubkey []byte, addrByte []byte, addrString string, err error) {
return ledger.retrieveAddressPubKeySECP256K1(bip44Path, false)
func (ledger *LedgerFilecoin) GetAddressPubKey(bip44Path []uint32) (pubkey []byte, addrByte []byte, addrString string, err error) {
return ledger.retrieveAddressPubKey(bip44Path, false)
}


// Deprecated: Use ShowAddressPubKey instead.
func (ledger *LedgerFilecoin) ShowAddressPubKeySECP256K1(bip44Path []uint32) (pubkey []byte, addrByte []byte, addrString string, err error) {
return ledger.ShowAddressPubKey(bip44Path)
}

// ShowAddressPubKeySECP256K1 returns the pubkey (compressed) and addresses
// this command requires user confirmation in the device
func (ledger *LedgerFilecoin) ShowAddressPubKeySECP256K1(bip44Path []uint32) (pubkey []byte, addrByte []byte, addrString string, err error) {
return ledger.retrieveAddressPubKeySECP256K1(bip44Path, true)
func (ledger *LedgerFilecoin) ShowAddressPubKey(bip44Path []uint32) (pubkey []byte, addrByte []byte, addrString string, err error) {
return ledger.retrieveAddressPubKey(bip44Path, true)
}

func (ledger *LedgerFilecoin) GetBip44bytes(bip44Path []uint32, hardenCount int) ([]byte, error) {
Expand Down Expand Up @@ -247,7 +241,7 @@ func (ledger *LedgerFilecoin) sign(bip44Path []uint32, transaction []byte) ([]by
payloadLen := byte(len(chunks[chunkIndex]))

if chunkIndex == 0 {
header := []byte{CLA, INSSignSECP256K1, PayloadChunkInit, 0, payloadLen}
header := []byte{CLA, INSSign, PayloadChunkInit, 0, payloadLen}
message = append(header, chunks[chunkIndex]...)
} else {

Expand All @@ -256,7 +250,7 @@ func (ledger *LedgerFilecoin) sign(bip44Path []uint32, transaction []byte) ([]by
payloadDesc = byte(PayloadChunkLast)
}

header := []byte{CLA, INSSignSECP256K1, payloadDesc, 0, payloadLen}
header := []byte{CLA, INSSign, payloadDesc, 0, payloadLen}
message = append(header, chunks[chunkIndex]...)
}

Expand All @@ -282,8 +276,8 @@ func (ledger *LedgerFilecoin) sign(bip44Path []uint32, transaction []byte) ([]by
return finalResponse, nil
}

// retrieveAddressPubKeySECP256K1 returns the pubkey and address
func (ledger *LedgerFilecoin) retrieveAddressPubKeySECP256K1(bip44Path []uint32, requireConfirmation bool) (pubkey []byte, addrByte []byte, addrString string, err error) {
// retrieveAddressPubKey returns the pubkey and address
func (ledger *LedgerFilecoin) retrieveAddressPubKey(bip44Path []uint32, requireConfirmation bool) (pubkey []byte, addrByte []byte, addrString string, err error) {
pathBytes, err := ledger.GetBip44bytes(bip44Path, HardenCount)
if err != nil {
return nil, nil, "", err
Expand All @@ -295,7 +289,7 @@ func (ledger *LedgerFilecoin) retrieveAddressPubKeySECP256K1(bip44Path []uint32,
}

// Prepare message
header := []byte{CLA, INSGetAddrSECP256K1, p1, 0, 0}
header := []byte{CLA, INSGetAddr, p1, 0, 0}
message := append(header, pathBytes...)
message[4] = byte(len(message) - len(header)) // update length

Expand Down
44 changes: 22 additions & 22 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ package ledger_filecoin_go
import (
"encoding/hex"
"fmt"
"testing"

"github.com/btcsuite/btcd"
"github.com/btcsuite/btcd/btcec/v2"
ecdsa "github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/ipsn/go-secp256k1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/blake2b"
"testing"
)

// Ledger Test Mnemonic: equip will roof matter pink blind book anxiety banner elbow sun young
Expand Down Expand Up @@ -66,7 +66,7 @@ func Test_UserGetPublicKey(t *testing.T) {

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

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

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

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

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

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

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

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

pubKey, err := app.GetPublicKeySECP256K1(path)
pubKey, err := app.GetPublicKey(path)
if err != nil {
t.Fatalf("Detected error, err: %s\n", err.Error())
}
Expand Down Expand Up @@ -214,24 +214,24 @@ func Test_Sign(t *testing.T) {

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

signature, err := app.SignSECP256K1(path, message)
signature, err := app.Sign(path, message)
if err != nil {
t.Fatalf("[Sign] Error: %s\n", err.Error())
}

// Verify Signature
pubKey, err := app.GetPublicKeySECP256K1(path)
pubKey, err := app.GetPublicKey(path)
if err != nil {
t.Fatalf("Detected error, err: %s\n", err.Error())
}

pub2, err := btcec.ParsePubKey(pubKey, btcec.S256())
pub2, err := btcec.ParsePubKey(pubKey)
if err != nil {
t.Fatalf("[ParsePK] Error: " + err.Error())
return
}

sig2, err := btcec.ParseDERSignature(signature.derSignature, btcec.S256())
sig2, err := ecdsa.ParseDERSignature(signature.derSignature)
if err != nil {
t.Fatalf("[ParseSig] Error: " + err.Error())
return
Expand Down Expand Up @@ -261,24 +261,24 @@ func Test_Sign2(t *testing.T) {

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

signature, err := app.SignSECP256K1(path, message)
signature, err := app.Sign(path, message)
if err != nil {
t.Fatalf("[Sign] Error: %s\n", err.Error())
}

// Verify Signature
pubKey, err := app.GetPublicKeySECP256K1(path)
pubKey, err := app.GetPublicKey(path)
if err != nil {
t.Fatalf("Detected error, err: %s\n", err.Error())
}

pub2, err := btcec.ParsePubKey(pubKey, btcec.S256())
pub2, err := btcec.ParsePubKey(pubKey)
if err != nil {
t.Fatalf("[ParsePK] Error: " + err.Error())
return
}

sig2, err := btcec.ParseDERSignature(signature.derSignature, btcec.S256())
sig2, err := ecdsa.ParseDERSignature(signature.derSignature)
if err != nil {
t.Fatalf("[ParseSig] Error: " + err.Error())
return
Expand Down Expand Up @@ -310,24 +310,24 @@ func Test_Sign3(t *testing.T) {

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

signature, err := app.SignSECP256K1(path, message)
signature, err := app.Sign(path, message)
if err != nil {
t.Fatalf("[Sign] Error: %s\n", err.Error())
}

// Verify Signature
pubKey, err := app.GetPublicKeySECP256K1(path)
pubKey, err := app.GetPublicKey(path)
if err != nil {
t.Fatalf("Detected error, err: %s\n", err.Error())
}

pub2, err := btcec.ParsePubKey(pubKey, btcec.S256())
pub2, err := btcec.ParsePubKey(pubKey)
if err != nil {
t.Fatalf("[ParsePK] Error: " + err.Error())
return
}

sig2, err := btcec.ParseDERSignature(signature.derSignature, btcec.S256())
sig2, err := ecdsa.ParseDERSignature(signature.derSignature)
if err != nil {
t.Fatalf("[ParseSig] Error: " + err.Error())
return
Expand Down Expand Up @@ -377,7 +377,7 @@ func Test_Sign_Fails(t *testing.T) {
garbage := []byte{65}
message = append(garbage, message...)

_, err = app.SignSECP256K1(path, message)
_, err = app.Sign(path, message)
assert.Error(t, err)
errMessage := err.Error()
assert.Equal(t, errMessage, "Unexpected data type")
Expand All @@ -386,7 +386,7 @@ func Test_Sign_Fails(t *testing.T) {
garbage = []byte{65}
message = append(message, garbage...)

_, err = app.SignSECP256K1(path, message)
_, err = app.Sign(path, message)
assert.Error(t, err)
errMessage = err.Error()
assert.Equal(t, errMessage, "Unexpected CBOR EOF")
Expand Down
Loading

0 comments on commit 3f23fe4

Please sign in to comment.