Skip to content

Commit

Permalink
🐛 (context-module) [NO-ISSUE]: Align array element with slice paramet…
Browse files Browse the repository at this point in the history
…ers (#545)
  • Loading branch information
paoun-ledger authored Dec 11, 2024
2 parents c11b93e + e695ae3 commit a4b98e2
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-kangaroos-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/context-module": patch
---

Align array element with slice parameters
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ export interface DataPathElementTuple {
* Path element to navigate in an array of variable size.
* - itemSize: the length of each item in that array (not the number of items which is variable).
* - start: the start of the array slice to iterate on. If unset, start from the beginning of that array.
* - length: the length of the array slice to iterate on. If unset, iterate until the end of that array.
* - end: the end of the array slice to iterate on (exclusive). If unset, iterate until the end of that array.
*/
export interface DataPathElementArray {
type: "ARRAY";
itemSize: number;
start?: number;
length?: number;
end?: number;
}

// Path element to indicate the current item should be de-referenced (its value contains a pointer).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export interface CalldataDescriptorPathElementTupleV1 {
export interface CalldataDescriptorPathElementArrayV1 {
type: "ARRAY";
start?: number;
length?: number;
end?: number;
weight: number;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe("HttpTransactionDataSource", () => {
{
type: "ARRAY",
start: 0,
length: 5,
end: 5,
weight: 1,
},
{
Expand Down Expand Up @@ -340,7 +340,7 @@ describe("HttpTransactionDataSource", () => {
{
type: "ARRAY",
start: 0,
length: 5,
end: 5,
itemSize: 1,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ export class HttpTransactionDataSource implements TransactionDataSource {
typeof data.weight === "number" &&
(typeof data.start === "undefined" ||
typeof data.start === "number") &&
(typeof data.length === "undefined" ||
typeof data.length === "number")) ||
(typeof data.end === "undefined" || typeof data.end === "number")) ||
(data.type === "LEAF" &&
typeof data.leaf_type === "string" &&
["ARRAY_LEAF", "TUPLE_LEAF", "STATIC_LEAF", "DYNAMIC_LEAF"].includes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ describe("TransactionParserService", () => {
type: "ARRAY",
itemSize: 1,
start: 0,
length: 1,
end: 1,
},
{
type: "REF",
Expand Down Expand Up @@ -307,7 +307,7 @@ describe("TransactionParserService", () => {
]);
});

it("Extract all the elements of x[0][-1:]", () => {
it("Extract all the elements of x[0:-1][-1:]", () => {
// GIVEN
const path: DataPathElement[] = [
{
Expand All @@ -321,7 +321,7 @@ describe("TransactionParserService", () => {
type: "ARRAY",
itemSize: 1,
start: 0,
length: 1,
end: -1,
},
{
type: "REF",
Expand Down Expand Up @@ -363,7 +363,7 @@ describe("TransactionParserService", () => {
type: "ARRAY",
itemSize: 1,
start: 0,
length: 1,
end: 1,
},
{
type: "REF",
Expand All @@ -372,7 +372,7 @@ describe("TransactionParserService", () => {
type: "ARRAY",
itemSize: 1,
start: 2,
length: 1,
end: 3,
},
{
type: "LEAF",
Expand Down Expand Up @@ -435,7 +435,7 @@ describe("TransactionParserService", () => {
{
type: "ARRAY",
itemSize: 1,
length: 2,
end: 2,
},
{
type: "REF",
Expand Down Expand Up @@ -471,7 +471,7 @@ describe("TransactionParserService", () => {
type: "ARRAY",
itemSize: 1,
start: 2,
length: 1,
end: 3,
},
{
type: "REF",
Expand Down Expand Up @@ -511,7 +511,7 @@ describe("TransactionParserService", () => {
type: "ARRAY",
itemSize: 1,
start: 2,
length: 1,
end: 3,
},
{
type: "REF",
Expand Down Expand Up @@ -548,7 +548,7 @@ describe("TransactionParserService", () => {
type: "ARRAY",
itemSize: 1,
start: 2,
length: 1,
end: 3,
},
{
type: "REF",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,20 @@ export class TransactionParserService {
? length + element.start
: element.start;
const end =
element.length === undefined ? length : start + element.length;
element.end === undefined
? length
: element.end < 0
? length + element.end
: element.end;
if (
start < 0 ||
start >= length ||
end > length ||
start === end
start >= end
) {
return Left(
new Error(
`Array slice out of bounds, start=${element.start}, length=${element.length}`,
`Array slice out of bounds, start=${element.start}, end=${element.end}`,
),
);
}
Expand Down

0 comments on commit a4b98e2

Please sign in to comment.