-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (signer) [DSDK-365]: Add signer context-module (#122)
- Loading branch information
Showing
68 changed files
with
3,682 additions
and
61 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
File renamed without changes.
File renamed without changes.
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 |
File renamed without changes.
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 "./src/index"; |
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,20 @@ | ||
/* eslint no-restricted-syntax: 0 */ | ||
import type { JestConfigWithTsJest } from "ts-jest"; | ||
|
||
const config: JestConfigWithTsJest = { | ||
preset: "@ledgerhq/jest-config-dsdk", | ||
setupFiles: ["<rootDir>/jest.setup.ts"], | ||
testPathIgnorePatterns: ["<rootDir>/lib/esm", "<rootDir>/lib/cjs"], | ||
collectCoverageFrom: [ | ||
"src/**/*.ts", | ||
"!src/**/*.stub.ts", | ||
"!src/index.ts", | ||
"!src/api/index.ts", | ||
], | ||
moduleNameMapper: { | ||
"^@/(.*)$": "<rootDir>/src/$1", | ||
"^@root/(.*)$": "<rootDir>/$1", | ||
}, | ||
}; | ||
|
||
export default config; |
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 @@ | ||
import "reflect-metadata"; |
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
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,36 @@ | ||
#!/usr/bin/env zx | ||
import "zx/globals"; | ||
import { basename } from "node:path"; | ||
const modules = argv._; | ||
|
||
if (!modules.length) { | ||
console.error(`Usage: ${basename(__filename)} <module1> [<module2> ...]`); | ||
process.exit(1); | ||
} | ||
|
||
within(async () => { | ||
cd("src/internal"); | ||
for (const mod of modules) { | ||
const rootFolderName = `${mod}`; | ||
const moduleUppercased = mod.charAt(0).toUpperCase() + mod.slice(1); | ||
await $`mkdir ${rootFolderName}`; | ||
within(async () => { | ||
cd(rootFolderName); | ||
await $`mkdir data di model service usecase`; | ||
const files = [ | ||
`data/${moduleUppercased}DataSource.ts`, | ||
`di/${mod}Module.test.ts`, | ||
`di/${mod}Module.ts`, | ||
`di/${mod}Types.ts`, | ||
`model/.gitkeep`, | ||
`service/${moduleUppercased}Service.ts`, | ||
`service/Default${moduleUppercased}Service.test.ts`, | ||
`service/Default${moduleUppercased}Service.ts`, | ||
`usecase/.gitkeep`, | ||
]; | ||
for (const file of files) { | ||
await $`touch ${file}`; | ||
} | ||
}); | ||
} | ||
}); |
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
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 { ClearSignContext } from "@/shared/model/ClearSignContext"; | ||
|
||
import { TransactionContext } from "./shared/model/TransactionContext"; | ||
|
||
export interface ContextModule { | ||
getContexts(transaction: TransactionContext): Promise<ClearSignContext[]>; | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/signer/context-module/src/ContextModuleBuilder.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,24 @@ | ||
import { ContextModuleBuilder } from "./ContextModuleBuilder"; | ||
import { DefaultContextModule } from "./DefaultContextModule"; | ||
|
||
describe("ContextModuleBuilder", () => { | ||
it("should return a default context module", () => { | ||
const contextModuleBuilder = new ContextModuleBuilder(); | ||
|
||
const res = contextModuleBuilder.build(); | ||
|
||
expect(res).toBeInstanceOf(DefaultContextModule); | ||
}); | ||
|
||
it("should return a custom context module", () => { | ||
const contextModuleBuilder = new ContextModuleBuilder(); | ||
const customLoader = { load: jest.fn() }; | ||
|
||
const res = contextModuleBuilder | ||
.withoutDefaultLoaders() | ||
.addLoader(customLoader) | ||
.build(); | ||
|
||
expect(res).toBeInstanceOf(DefaultContextModule); | ||
}); | ||
}); |
64 changes: 64 additions & 0 deletions
64
packages/signer/context-module/src/ContextModuleBuilder.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,64 @@ | ||
import { externalPluginTypes } from "@/external-plugin/di/externalPluginTypes"; | ||
import { forwardDomainTypes } from "@/forward-domain/di/forwardDomainTypes"; | ||
import { nftTypes } from "@/nft/di/nftTypes"; | ||
import { tokenTypes } from "@/token/di/tokenTypes"; | ||
|
||
import { ExternalPluginContextLoader } from "./external-plugin/domain/ExternalPluginContextLoader"; | ||
import { ForwardDomainContextLoader } from "./forward-domain/domain/ForwardDomainContextLoader"; | ||
import { NftContextLoader } from "./nft/domain/NftContextLoader"; | ||
import { ContextLoader } from "./shared/domain/ContextLoader"; | ||
import { TokenContextLoader } from "./token/domain/TokenContextLoader"; | ||
import { ContextModule } from "./ContextModule"; | ||
import { DefaultContextModule } from "./DefaultContextModule"; | ||
import { makeContainer } from "./di"; | ||
|
||
export class ContextModuleBuilder { | ||
private customLoaders: ContextLoader[] = []; | ||
private defaultLoaders: ContextLoader[] = []; | ||
|
||
constructor() { | ||
const container = makeContainer(); | ||
|
||
this.defaultLoaders = [ | ||
container.get<ExternalPluginContextLoader>( | ||
externalPluginTypes.ExternalPluginContextLoader, | ||
), | ||
container.get<ForwardDomainContextLoader>( | ||
forwardDomainTypes.ForwardDomainContextLoader, | ||
), | ||
container.get<NftContextLoader>(nftTypes.NftContextLoader), | ||
container.get<TokenContextLoader>(tokenTypes.TokenContextLoader), | ||
]; | ||
} | ||
|
||
/** | ||
* Remove default loaders from the list of loaders | ||
* | ||
* @returns this | ||
*/ | ||
withoutDefaultLoaders() { | ||
this.defaultLoaders = []; | ||
return this; | ||
} | ||
|
||
/** | ||
* Add a custom loader to the list of loaders | ||
* | ||
* @param loader loader to add to the list of loaders | ||
* @returns this | ||
*/ | ||
addLoader(loader: ContextLoader) { | ||
this.customLoaders.push(loader); | ||
return this; | ||
} | ||
|
||
/** | ||
* Build the context module | ||
* | ||
* @returns the context module | ||
*/ | ||
build(): ContextModule { | ||
const loaders = [...this.defaultLoaders, ...this.customLoaders]; | ||
return new DefaultContextModule({ loaders }); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
packages/signer/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,62 @@ | ||
import { TransactionContext } from "./shared/model/TransactionContext"; | ||
import { DefaultContextModule } from "./DefaultContextModule"; | ||
|
||
const contextLoaderStubBuilder = () => { | ||
return { load: jest.fn() }; | ||
}; | ||
|
||
describe("DefaultContextModule", () => { | ||
beforeEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it("should initialize the context module with all the default loaders", async () => { | ||
const contextModule = new DefaultContextModule({ loaders: [] }); | ||
|
||
const res = await contextModule.getContexts({} as TransactionContext); | ||
|
||
expect(res).toEqual([]); | ||
}); | ||
|
||
it("should return an empty array when no loaders", async () => { | ||
const contextModule = new DefaultContextModule({ loaders: [] }); | ||
|
||
const res = await contextModule.getContexts({} as TransactionContext); | ||
|
||
expect(res).toEqual([]); | ||
}); | ||
|
||
it("should call all fetch method from metadata fetcher", async () => { | ||
const loader = contextLoaderStubBuilder(); | ||
const contextModule = new DefaultContextModule({ | ||
loaders: [loader, loader], | ||
}); | ||
|
||
await contextModule.getContexts({} as TransactionContext); | ||
|
||
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: "plugin", 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 TransactionContext); | ||
|
||
expect(loader.load).toHaveBeenCalledTimes(2); | ||
expect(res).toEqual(responses.flat()); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
packages/signer/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,24 @@ | ||
import { ContextLoader } from "./shared/domain/ContextLoader"; | ||
import { ClearSignContext } from "./shared/model/ClearSignContext"; | ||
import { TransactionContext } from "./shared/model/TransactionContext"; | ||
import { ContextModule } from "./ContextModule"; | ||
|
||
type DefaultContextModuleConstructorArgs = { | ||
loaders: ContextLoader[]; | ||
}; | ||
|
||
export class DefaultContextModule implements ContextModule { | ||
private _loaders: ContextLoader[]; | ||
|
||
constructor(args: DefaultContextModuleConstructorArgs) { | ||
this._loaders = args.loaders; | ||
} | ||
|
||
public async getContexts( | ||
transaction: TransactionContext, | ||
): Promise<ClearSignContext[]> { | ||
const promises = this._loaders.map((fetcher) => fetcher.load(transaction)); | ||
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,19 @@ | ||
import { Container } from "inversify"; | ||
|
||
import { externalPluginModuleFactory } from "@/external-plugin/di/externalPluginModuleFactory"; | ||
import { forwardDomainModuleFactory } from "@/forward-domain/di/forwardDomainModuleFactory"; | ||
import { nftModuleFactory } from "@/nft/di/nftModuleFactory"; | ||
import { tokenModuleFactory } from "@/token/di/tokenModuleFactory"; | ||
|
||
export const makeContainer = () => { | ||
const container = new Container(); | ||
|
||
container.load( | ||
externalPluginModuleFactory(), | ||
forwardDomainModuleFactory(), | ||
nftModuleFactory(), | ||
tokenModuleFactory(), | ||
); | ||
|
||
return container; | ||
}; |
Oops, something went wrong.