Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/dsdk 280: create context-module package for clear signing #6788

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions libs/ledgerjs/packages/context-module/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import baseConfig from "../../jest.config";

export default {
...baseConfig,
rootDir: __dirname,
};
30 changes: 30 additions & 0 deletions libs/ledgerjs/packages/context-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@ledgerhq/context-module",
"version": "0.0.1",
"description": "Ledger context module",
"files": [
"./lib"
],
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "rimraf lib && tsc",
"test": "jest"
},
"keywords": [],
"license": "Apache-2.0",
"devDependencies": {
"@tsconfig/recommended": "^1.0.6",
"@types/jest": "^29.5.10",
"@types/node": "^20.8.10",
"jest": "^29.7.0",
"rimraf": "^4.4.1",
"ts-jest": "^29.1.1",
"ts-node": "^10.7.0",
"typescript": "^5.4.5"
},
"dependencies": {
"axios": "^1.6.8",
"ethers": "^5.7.2"
}
}
7 changes: 7 additions & 0 deletions libs/ledgerjs/packages/context-module/src/ContextModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ContextResponse } from "./shared/model/ContextResponse";
import { LoaderOptions } from "./shared/model/LoaderOptions";
import { Transaction } from "./shared/model/Transaction";

export interface ContextModule {
getContexts(transaction: Transaction, options: LoaderOptions): Promise<ContextResponse[]>;
aussedatlo marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { DefaultContextModule } from "./DefaultContextModule";
import { LoaderOptions } from "./shared/model/LoaderOptions";
import { Transaction } from "./shared/model/Transaction";

const contextLoaderStubBuilder = () => {
return { load: jest.fn() };
};

describe("DefaultContextModule", () => {
beforeEach(() => {
jest.restoreAllMocks();
});

it("should call all fetch method from metadata fetcher", async () => {
const loader = contextLoaderStubBuilder();
const contextModule = new DefaultContextModule({ loaders: [loader, loader] });

await contextModule.getContexts({} as Transaction, {} as LoaderOptions);

expect(loader.load).toHaveBeenCalledTimes(2);
});

it("should return an array of context response", async () => {
const loader = contextLoaderStubBuilder();
const responses = [
[{ type: "provideERC20Info", payload: "payload1" }],
[
{ type: "provideERC20Info", payload: "payload2" },
{ type: "setPlugin", payload: "payload3" },
],
];
jest
.spyOn(loader, "load")
.mockResolvedValueOnce(responses[0])
.mockResolvedValueOnce(responses[1]);
const contextModule = new DefaultContextModule({ loaders: [loader, loader] });

const res = await contextModule.getContexts({} as Transaction, {} as LoaderOptions);

expect(loader.load).toHaveBeenCalledTimes(2);
expect(res).toEqual(responses.flat());
});
});
26 changes: 26 additions & 0 deletions libs/ledgerjs/packages/context-module/src/DefaultContextModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ContextModule } from "./ContextModule";
import { ContextLoader } from "./shared/domain/ContextLoader";
import { ContextResponse } from "./shared/model/ContextResponse";
import { LoaderOptions } from "./shared/model/LoaderOptions";
import { Transaction } from "./shared/model/Transaction";

type DefaultContextModuleConstructorArgs = {
loaders: ContextLoader[];
aussedatlo marked this conversation as resolved.
Show resolved Hide resolved
};

export class DefaultContextModule implements ContextModule {
private _loaders: ContextLoader[];

constructor({ loaders: loaders }: DefaultContextModuleConstructorArgs) {
this._loaders = loaders;
}

public async getContexts(
transaction: Transaction,
options: LoaderOptions,
): Promise<ContextResponse[]> {
const promises = this._loaders.map(fetcher => fetcher.load(transaction, options));
const responses = await Promise.all(promises);
return responses.flat();
}
}
1 change: 1 addition & 0 deletions libs/ledgerjs/packages/context-module/src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Context Module
1 change: 1 addition & 0 deletions libs/ledgerjs/packages/context-module/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./DefaultContextModule";
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ContextResponse } from "../model/ContextResponse";
import { LoaderOptions } from "../model/LoaderOptions";
import { Transaction } from "../model/Transaction";

export type ContextLoader = {
load: (transaction: Transaction, options: LoaderOptions) => Promise<ContextResponse[]>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type ContextSuccess = {
type:
| "provideERC20TokenInformation"
| "provideNFTInformation"
| "provideDomainName"
| "setPlugin"
| "setExternalPlugin";
payload: string;
};

type ContextError = {
type: "error";
error: Error;
};

export type ContextResponse = ContextSuccess | ContextError;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type LoaderOptions = {
challenge: string;
options?: { forwardDomain?: { domain: string; registry: "ens" } };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Transaction } from "ethers";
7 changes: 7 additions & 0 deletions libs/ledgerjs/packages/context-module/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "lib"
},
"include": ["src/**/*"]
}