-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e989ad
commit 870818e
Showing
13 changed files
with
298 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import baseConfig from "../../jest.config"; | ||
|
||
export default { | ||
...baseConfig, | ||
rootDir: __dirname, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]>; | ||
} |
43 changes: 43 additions & 0 deletions
43
libs/ledgerjs/packages/context-module/src/DefaultContextModule.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
libs/ledgerjs/packages/context-module/src/DefaultContextModule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
}; | ||
|
||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Context Module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./DefaultContextModule"; |
7 changes: 7 additions & 0 deletions
7
libs/ledgerjs/packages/context-module/src/shared/domain/ContextLoader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]>; | ||
}; |
16 changes: 16 additions & 0 deletions
16
libs/ledgerjs/packages/context-module/src/shared/model/ContextResponse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
4 changes: 4 additions & 0 deletions
4
libs/ledgerjs/packages/context-module/src/shared/model/LoaderOptions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } }; | ||
}; |
1 change: 1 addition & 0 deletions
1
libs/ledgerjs/packages/context-module/src/shared/model/Transaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Transaction } from "ethers"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "lib" | ||
}, | ||
"include": ["src/**/*"] | ||
} |
Oops, something went wrong.