Skip to content

Commit 4c5600d

Browse files
committed
use chunk processing functions from ledger_go
1 parent 00e7521 commit 4c5600d

File tree

3 files changed

+10
-84
lines changed

3 files changed

+10
-84
lines changed

app.go

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,12 @@ func (ledger *LedgerFilecoin) sign(bip44Path []uint32, transaction []byte, curve
209209
return nil, err
210210
}
211211

212-
chunks, err := prepareChunks(pathBytes, transaction)
212+
chunks, err := ledger_go.PrepareChunks(pathBytes, transaction)
213213
if err != nil {
214214
return nil, err
215215
}
216216

217-
return ledger.processChunks(chunks, INSSign)
217+
return ledger_go.ProcessChunks(ledger.api, chunks, CLA, INSSign, apduP2Default, handleExchangeError)
218218
}
219219

220220
// SignPersonalMessageFVM signs a personal message for FVM (Filecoin Virtual Machine)
@@ -241,12 +241,12 @@ func (ledger *LedgerFilecoin) signPersonalMessage(bip44Path []uint32, message []
241241
binary.BigEndian.PutUint32(fullMessage[0:messageLengthPrefixSize], messageLen)
242242
copy(fullMessage[messageLengthPrefixSize:], message)
243243

244-
chunks, err := prepareChunks(pathBytes, fullMessage)
244+
chunks, err := ledger_go.PrepareChunks(pathBytes, fullMessage)
245245
if err != nil {
246246
return nil, err
247247
}
248248

249-
return ledger.processChunks(chunks, INSSignPersonalMsg)
249+
return ledger_go.ProcessChunks(ledger.api, chunks, CLA, INSSignPersonalMsg, apduP2Default, handleExchangeError)
250250
}
251251

252252
// retrieveAddressPubKey returns the pubkey and address
@@ -274,12 +274,12 @@ func (ledger *LedgerFilecoin) signRawBytes(bip44Path []uint32, message []byte) (
274274
copy(fullMessage, varintLen)
275275
copy(fullMessage[len(varintLen):], message)
276276

277-
chunks, err := prepareChunks(pathBytes, fullMessage)
277+
chunks, err := ledger_go.PrepareChunks(pathBytes, fullMessage)
278278
if err != nil {
279279
return nil, err
280280
}
281281

282-
return ledger.processChunks(chunks, INSSignRawBytes)
282+
return ledger_go.ProcessChunks(ledger.api, chunks, CLA, INSSignRawBytes, apduP2Default, handleExchangeError)
283283
}
284284

285285
func parseSignatureResponse(signatureBytes []byte) (*SignatureAnswer, error) {
@@ -295,34 +295,7 @@ func parseSignatureResponse(signatureBytes []byte) (*SignatureAnswer, error) {
295295
}, nil
296296
}
297297

298-
func (ledger *LedgerFilecoin) processChunks(chunks [][]byte, instruction byte) ([]byte, error) {
299-
var finalResponse []byte
300-
301-
for chunkIndex, chunk := range chunks {
302-
payloadLen := byte(len(chunk))
303-
payloadDesc := PayloadChunkAdd
304-
305-
if chunkIndex == 0 {
306-
payloadDesc = PayloadChunkInit
307-
} else if chunkIndex == len(chunks)-1 {
308-
payloadDesc = PayloadChunkLast
309-
}
310-
311-
header := []byte{CLA, instruction, byte(payloadDesc), apduP2Default, payloadLen}
312-
message := append(header, chunk...)
313-
314-
response, err := ledger.api.Exchange(message)
315-
if err != nil {
316-
return nil, handleExchangeError(err, response)
317-
}
318-
319-
finalResponse = response
320-
}
321-
322-
return finalResponse, nil
323-
}
324-
325-
func handleExchangeError(err error, response []byte) error {
298+
func handleExchangeError(err error, response []byte, instruction byte) error {
326299
errorMsg := err.Error()
327300
switch {
328301
case errorMsg == "[APDU_CODE_BAD_KEY_HANDLE] The parameters in the data field are incorrect":

common.go

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ package ledger_filecoin_go
1919
import (
2020
"encoding/binary"
2121
"fmt"
22-
"math"
2322
)
2423

2524
const (
26-
userMessageChunkSize = 250
27-
publicKeyLength = 65
25+
publicKeyLength = 65
2826

2927
// Signature-related constants
3028
signatureLength = 65
@@ -115,29 +113,3 @@ func GetBip44bytes(bip44Path []uint32, hardenCount int) ([]byte, error) {
115113
}
116114
return message, nil
117115
}
118-
119-
func prepareChunks(bip44PathBytes []byte, transaction []byte) ([][]byte, error) {
120-
var packetIndex = 0
121-
// first chunk + number of chunk needed for transaction
122-
var packetCount = 1 + int(math.Ceil(float64(len(transaction))/float64(userMessageChunkSize)))
123-
124-
chunks := make([][]byte, packetCount)
125-
126-
// First chunk is path
127-
chunks[0] = bip44PathBytes
128-
packetIndex++
129-
130-
for packetIndex < packetCount {
131-
var start = (packetIndex - 1) * userMessageChunkSize
132-
var end = packetIndex * userMessageChunkSize
133-
134-
if end >= len(transaction) {
135-
chunks[packetIndex] = transaction[start:]
136-
} else {
137-
chunks[packetIndex] = transaction[start:end]
138-
}
139-
packetIndex++
140-
}
141-
142-
return chunks, nil
143-
}

common_test.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
package ledger_filecoin_go
1818

1919
import (
20-
"encoding/hex"
2120
"fmt"
22-
"github.com/stretchr/testify/assert"
2321
"testing"
22+
23+
"github.com/stretchr/testify/assert"
2424
)
2525

2626
func Test_PrintVersion(t *testing.T) {
@@ -100,22 +100,3 @@ func Test_PathGeneration3(t *testing.T) {
100100
fmt.Sprintf("%x", pathBytes),
101101
"Unexpected PathBytes\n")
102102
}
103-
104-
func Test_ChunkGeneration(t *testing.T) {
105-
bip44Path := []uint32{44, 123, 0, 0, 0}
106-
107-
pathBytes, err := GetBip44bytes(bip44Path, 0)
108-
if err != nil {
109-
t.Fatalf("Detected error, err: %s\n", err.Error())
110-
}
111-
112-
message, _ := hex.DecodeString("885501fd1d0f4dfcd7e99afcb99a8326b7dc459d32c6285501b882619d46558f3d9e316d11b48dcf211327025a0144000186a0430009c4430061a80040")
113-
114-
chunks, err := prepareChunks(pathBytes, message)
115-
116-
assert.Equal(
117-
t,
118-
chunks[0],
119-
pathBytes,
120-
"First chunk should be pathBytes\n")
121-
}

0 commit comments

Comments
 (0)