-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(context-module): add external plugin loader
- Loading branch information
1 parent
b76691c
commit 897203b
Showing
9 changed files
with
656 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
libs/ledgerjs/packages/context-module/src/external-plugin/data/DappResponse.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,40 @@ | ||
export interface DappResponse { | ||
b2c: B2c; | ||
abis: Abis; | ||
b2c_signatures: B2cSignatures; | ||
} | ||
|
||
export interface B2c { | ||
blockchainName: string; | ||
chainId: number; | ||
contracts: Contract[]; | ||
name: string; | ||
} | ||
|
||
interface Contract { | ||
address: string; | ||
contractName: string; | ||
selectors: { [selector: string]: ContractSelector }; | ||
} | ||
|
||
interface ContractSelector { | ||
erc20OfInterest: string[]; | ||
method: string; | ||
plugin: string; | ||
} | ||
|
||
interface Abis { | ||
[address: string]: object; | ||
} | ||
|
||
export interface B2cSignatures { | ||
[address: string]: { | ||
[selector: string]: B2cSignature; | ||
}; | ||
} | ||
|
||
interface B2cSignature { | ||
plugin: string; | ||
serialized_data: string; | ||
signature: string; | ||
} |
11 changes: 11 additions & 0 deletions
11
libs/ledgerjs/packages/context-module/src/external-plugin/data/ExternalPluginDataSource.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,11 @@ | ||
import { DappInfos } from "../model/DappInfos"; | ||
|
||
export type GetDappInfos = { | ||
address: string; | ||
selector: `0x${string}`; | ||
chainId: number; | ||
}; | ||
|
||
export interface ExternalPluginDataSource { | ||
getDappInfos(params: GetDappInfos): Promise<DappInfos | undefined>; | ||
} |
42 changes: 42 additions & 0 deletions
42
...ledgerjs/packages/context-module/src/external-plugin/data/HttpExternalPluginDataSource.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,42 @@ | ||
import axios from "axios"; | ||
import { ExternalPluginDataSource, GetDappInfos } from "./ExternalPluginDataSource"; | ||
import { DappResponse } from "./DappResponse"; | ||
import { DappInfos } from "../model/DappInfos"; | ||
import { SelectorDetails } from "../model/SelectorDetails"; | ||
|
||
export class HttpExternalPluginDataSource implements ExternalPluginDataSource { | ||
constructor() {} | ||
|
||
async getDappInfos({ chainId, address, selector }: GetDappInfos): Promise<DappInfos | undefined> { | ||
const dappInfos = await axios.request<DappResponse[]>({ | ||
method: "GET", | ||
url: "https://crypto-assets-service.api.ledger.com/v1/dapps", | ||
params: { output: "b2c,b2c_signatures,abis", chain_id: chainId, contracts: address }, | ||
}); | ||
|
||
const { erc20OfInterest, method, plugin } = | ||
dappInfos.data[0]?.b2c.contracts?.[0]?.selectors?.[selector] || {}; | ||
const { signature, serialized_data: serializedData } = | ||
dappInfos.data[0]?.b2c_signatures?.[address]?.[selector] || {}; | ||
|
||
if (!erc20OfInterest || !method || !plugin || !signature || !serializedData) { | ||
return; | ||
} | ||
|
||
const abi = dappInfos.data[0]?.abis?.[address]; | ||
|
||
if (!abi) { | ||
return; | ||
} | ||
|
||
const selectorDetails: SelectorDetails = { | ||
method, | ||
plugin, | ||
erc20OfInterest, | ||
signature, | ||
serializedData, | ||
}; | ||
|
||
return { selectorDetails, abi: JSON.stringify(abi) }; | ||
} | ||
} |
Oops, something went wrong.