Skip to content

Commit ceba168

Browse files
committed
add prepare chunks function
1 parent fb2e967 commit ceba168

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@zondax/ledger-js",
33
"author": "Zondax AG",
44
"license": "Apache-2.0",
5-
"version": "0.2.0",
5+
"version": "0.2.1",
66
"description": "TS / Node API for apps running on Ledger Nano S/S+/X",
77
"main": "./dist/index.js",
88
"types": "./dist/index.d.ts",

src/GenericApp.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ export default class GenericApp {
1616
readonly INS: INSGeneric;
1717
readonly P1_VALUES: P1_VALUESGeneric;
1818
readonly acceptedPathLengths?: number[];
19+
readonly CHUNK_SIZE: number;
1920

2021
constructor(transport: Transport, params: ConstructorParams) {
2122
this.transport = transport;
2223
this.CLA = params.cla;
2324
this.INS = params.ins;
2425
this.P1_VALUES = params.p1Values;
26+
this.CHUNK_SIZE = params.chunkSize;
2527
this.acceptedPathLengths = params.acceptedPathLengths;
2628
}
2729

@@ -56,6 +58,27 @@ export default class GenericApp {
5658
return buf;
5759
}
5860

61+
prepareChunks(path: string, message: Buffer): Buffer[] {
62+
const chunks = [];
63+
const serializedPathBuffer = this.serializePath(path);
64+
65+
// First chunk (only path)
66+
chunks.push(serializedPathBuffer);
67+
68+
const messageBuffer = Buffer.from(message);
69+
70+
const buffer = Buffer.concat([messageBuffer]);
71+
for (let i = 0; i < buffer.length; i += this.CHUNK_SIZE) {
72+
let end = i + this.CHUNK_SIZE;
73+
if (i > buffer.length) {
74+
end = buffer.length;
75+
}
76+
chunks.push(buffer.subarray(i, end));
77+
}
78+
79+
return chunks;
80+
}
81+
5982
async getVersion(): Promise<ResponseVersion> {
6083
const versionResponse: ResponseVersion = await this.transport
6184
.send(this.CLA, this.INS.GET_VERSION, 0, 0)

src/consts.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*******************************************************************************
1717
*/
1818

19-
export const CHUNK_SIZE = 250;
20-
2119
export const PAYLOAD_TYPE = {
2220
INIT: 0x00,
2321
ADD: 0x01,

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ export interface ConstructorParams {
4545
cla: number;
4646
ins: INSGeneric;
4747
p1Values: P1_VALUESGeneric;
48+
chunkSize: number;
4849
acceptedPathLengths?: number[];
4950
}

0 commit comments

Comments
 (0)