Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Commit

Permalink
LL-8906 implement better resilience of hw-app-eth on resolution servi…
Browse files Browse the repository at this point in the history
…ce (#755)

* Implement better resilience of hw-app-eth on resolution service

LL-8906

* doc
  • Loading branch information
gre authored Jan 6, 2022
1 parent d74633a commit 520944e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
15 changes: 15 additions & 0 deletions packages/hw-app-eth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Ledger Hardware Wallet ETH JavaScript bindings.
* [byContractAddressAndChainId](#bycontractaddressandchainid)
* [Parameters](#parameters-16)
* [list](#list)
* [ResolutionConfig](#resolutionconfig)
* [Properties](#properties)

### Eth

Expand Down Expand Up @@ -359,3 +361,16 @@ Returns **(TokenInfo | null | [undefined](https://developer.mozilla.org/docs/Web
list all the ERC20 tokens informations

Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\<TokenInfo>**

### ResolutionConfig

Allows to configure precisely what the service need to resolve.
for instance you can set nft:true if you need clear signing on NFTs. If you set it and it is not a NFT transaction, it should still work but will do a useless service resolution.

Type: {nft: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?, externalPlugins: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?, erc20: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?}

#### Properties

* `nft` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?**
* `externalPlugins` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?**
* `erc20` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?**
5 changes: 4 additions & 1 deletion packages/hw-app-eth/src/Eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ export default class Eth {
" + const resolution = await ledgerService.resolveTransaction(rawTxHex);"
);
resolution = await ledgerService
.resolveTransaction(rawTxHex, this.loadConfig)
.resolveTransaction(rawTxHex, this.loadConfig, {
externalPlugins: true,
erc20: true,
})
.catch((e) => {
console.warn(
"an error occurred in resolveTransaction => fallback to blind signing: " +
Expand Down
34 changes: 20 additions & 14 deletions packages/hw-app-eth/src/services/ledger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getNFTInfo, loadNftPlugin } from "./nfts";
import { decodeTxInfo } from "../../utils";

const ledgerService: LedgerEthTransactionService = {
resolveTransaction: async (rawTxHex, loadConfig) => {
resolveTransaction: async (rawTxHex, loadConfig, resolutionConfig) => {
const resolution: LedgerEthTransactionResolution = {
erc20Tokens: [],
nfts: [],
Expand All @@ -36,7 +36,9 @@ const ledgerService: LedgerEthTransactionService = {
const rawTx = Buffer.from(rawTxHex, "hex");
const { decodedTx, chainIdTruncated } = decodeTxInfo(rawTx);
const provideForContract = async (address) => {
const nftInfo = await getNFTInfo(address, chainIdTruncated, loadConfig);
const nftInfo = resolutionConfig.nft
? await getNFTInfo(address, chainIdTruncated, loadConfig)
: null;
if (nftInfo) {
log(
"ethereum",
Expand Down Expand Up @@ -68,22 +70,26 @@ const ledgerService: LedgerEthTransactionService = {

if (decodedTx.data.length >= 10) {
const selector = decodedTx.data.substring(0, 10);
const nftPluginPayload = await loadNftPlugin(
decodedTx.to,
selector,
chainIdTruncated,
loadConfig
);
const nftPluginPayload = resolutionConfig.nft
? await loadNftPlugin(
decodedTx.to,
selector,
chainIdTruncated,
loadConfig
)
: null;

if (nftPluginPayload) {
setPlugin(nftPluginPayload);
} else {
const infos = await loadInfosForContractMethod(
decodedTx.to,
selector,
chainIdTruncated,
loadConfig
);
const infos = resolutionConfig.externalPlugins
? await loadInfosForContractMethod(
decodedTx.to,
selector,
chainIdTruncated,
loadConfig
)
: null;

if (infos) {
const { plugin, payload, signature, erc20OfInterest, abi } = infos;
Expand Down
16 changes: 15 additions & 1 deletion packages/hw-app-eth/src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,23 @@ export type LoadConfig = {
extraPlugins?: any | null;
};

/**
* Allows to configure precisely what the service need to resolve.
* for instance you can set nft:true if you need clear signing on NFTs. If you set it and it is not a NFT transaction, it should still work but will do a useless service resolution.
*/
export type ResolutionConfig = {
// NFT resolution service
nft?: boolean;
// external plugins resolution service (e.G. LIDO)
externalPlugins?: boolean;
// ERC20 resolution service (to clear sign erc20 transfers & other actions)
erc20?: boolean;
};

export type LedgerEthTransactionService = {
resolveTransaction: (
rawTxHex: string,
loadConfig: LoadConfig
loadConfig: LoadConfig,
resolutionConfig: ResolutionConfig
) => Promise<LedgerEthTransactionResolution>;
};

0 comments on commit 520944e

Please sign in to comment.