Skip to content

Commit

Permalink
fix: wallet providers cache and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
iGroza committed Mar 18, 2024
1 parent 898aa5d commit c7c6836
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 74 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
"@evmos/transactions": "0.2.13",
"@expo/react-native-action-sheet": "4.0.1",
"@haqq/encryption-react-native": "0.0.4",
"@haqq/provider-base": "0.0.22",
"@haqq/provider-hot-react-native": "0.0.9",
"@haqq/provider-keystone-react-native": "0.0.6",
"@haqq/provider-ledger-react-native": "0.0.27",
"@haqq/provider-mnemonic-react-native": "0.0.13",
"@haqq/provider-sss-react-native": "0.0.13",
"@haqq/provider-base": "0.0.23",
"@haqq/provider-hot-react-native": "0.0.10",
"@haqq/provider-keystone-react-native": "0.0.7",
"@haqq/provider-ledger-react-native": "0.0.28",
"@haqq/provider-mnemonic-react-native": "0.0.14",
"@haqq/provider-sss-react-native": "0.0.14",
"@haqq/provider-web3-utils": "0.0.14",
"@haqq/shared-react-native": "0.0.9",
"@invertase/react-native-apple-authentication": "2.2.2",
Expand Down
78 changes: 40 additions & 38 deletions src/helpers/provider-instance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ProviderInterface} from '@haqq/provider-base';
import {ProviderBaseError, ProviderInterface} from '@haqq/provider-base';
import {ProviderHotReactNative} from '@haqq/provider-hot-react-native';
import {ProviderKeystoneReactNative} from '@haqq/provider-keystone-react-native';
import {ProviderLedgerReactNative} from '@haqq/provider-ledger-react-native';
Expand Down Expand Up @@ -57,30 +57,27 @@ export async function getProviderInstanceForWallet(
forceUpdate: boolean = false,
): Promise<ProviderInterface> {
const id = getId(wallet);
if (forceUpdate) {

if (cache.has(id) && !forceUpdate) {
return cache.get(id);
}

let provider: ProviderInterface;
switch (wallet.type) {
case WalletType.mnemonic:
cache.set(
id,
new ProviderMnemonicReactNative({
account: wallet.accountId!,
getPassword: app.getPassword.bind(app),
}),
);
provider = new ProviderMnemonicReactNative({
account: wallet.accountId!,
getPassword: app.getPassword.bind(app),
});
break;
case WalletType.hot:
cache.set(
id,
new ProviderHotReactNative({
getPassword: app.getPassword.bind(app),
account: wallet.accountId!,
}),
);
provider = new ProviderHotReactNative({
getPassword: app.getPassword.bind(app),
account: wallet.accountId!,
});
break;
case WalletType.ledgerBt: {
const provider = new ProviderLedgerReactNative({
provider = new ProviderLedgerReactNative({
getPassword: app.getPassword.bind(app),
deviceId: wallet.accountId!,
appName: LEDGER_APP,
Expand All @@ -89,34 +86,39 @@ export async function getProviderInstanceForWallet(
awaitForLedger(provider);
cache.set(id, provider);
}
return provider;
break;
}
case WalletType.sss:
const storage = await getProviderStorage(wallet.accountId as string);
cache.set(
id,
new ProviderSSSReactNative({
storage,
getPassword: app.getPassword.bind(app),
account: wallet.accountId!,
}),
);
provider = new ProviderSSSReactNative({
storage,
getPassword: app.getPassword.bind(app),
account: wallet.accountId!,
});
break;
case WalletType.keystone:
cache.set(
id,
new ProviderKeystoneReactNative({
qrCBORHex: wallet.accountId!,
awaitForSign: async params => {
const signatureHex = await awaitForQRSign(params);
return {signatureHex};
},
}),
);
provider = new ProviderKeystoneReactNative({
qrCBORHex: wallet.accountId!,
awaitForSign: async params => {
const signatureHex = await awaitForQRSign(params);
return {signatureHex};
},
});
break;
default:
throw new Error('transport_not_implemented');
}

return cache.get(id);
provider.on('catch-error', ({error, source}: ProviderBaseError) => {
if (error) {
Logger.captureException(error, 'provider-catch-error', {
wallet,
skipAwaitForLedgerCall,
forceUpdate,
source: source,
error: error,
});
}
});
cache.set(id, provider);
return provider;
}
60 changes: 30 additions & 30 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2356,20 +2356,20 @@ __metadata:
languageName: node
linkType: hard

"@haqq/provider-base@npm:0.0.22":
version: 0.0.22
resolution: "@haqq/provider-base@npm:0.0.22"
"@haqq/provider-base@npm:0.0.23":
version: 0.0.23
resolution: "@haqq/provider-base@npm:0.0.23"
dependencies:
bech32-converting: ^1.0.9
ethers: ^5.7.2
events: ^3.3.0
checksum: 1c4e8a6e38942d68544159e565d0b5056d53be3cd680b0549bfa20ed0f9103e700505225077d1ce802e9cf38d4fe654405c6031a5e232046d6b3f5783125d907
checksum: 2b34d31e39567ba7e2996b8bece47037aae6ae760225dc0423c561d832934bcd65787f5d0688ead955a97254d80ba8353f3d4627775ca02d94460fe5fe06abd9
languageName: node
linkType: hard

"@haqq/provider-hot-react-native@npm:0.0.9":
version: 0.0.9
resolution: "@haqq/provider-hot-react-native@npm:0.0.9"
"@haqq/provider-hot-react-native@npm:0.0.10":
version: 0.0.10
resolution: "@haqq/provider-hot-react-native@npm:0.0.10"
dependencies:
"@ethersproject/bytes": ^5.7.0
"@ethersproject/transactions": ^5.7.0
Expand All @@ -2379,13 +2379,13 @@ __metadata:
"@haqq/provider-web3-utils": "*"
"@haqq/shared-react-native": "*"
react-native-encrypted-storage: "*"
checksum: a919c6683fbc6c5a5307b77c183ea407dba6d58cd2fce3c772f942790a173c23b258cb418da92555f3c256a189ce255dd81289d885773de7ad1bb89fdb38abbe
checksum: c8eb370eea2a4ae06b77352308ddd3e9bbca77061e9704b0521f0500f6b956134d5f0e932d39f28ed90365fb49f7eda6163f5a1e46e19c65b75fcb76ba3dea4e
languageName: node
linkType: hard

"@haqq/provider-keystone-react-native@npm:0.0.6":
version: 0.0.6
resolution: "@haqq/provider-keystone-react-native@npm:0.0.6"
"@haqq/provider-keystone-react-native@npm:0.0.7":
version: 0.0.7
resolution: "@haqq/provider-keystone-react-native@npm:0.0.7"
dependencies:
"@ethersproject/abstract-provider": ^5.7.0
"@keystonehq/bc-ur-registry-eth": ^0.7.7
Expand All @@ -2396,13 +2396,13 @@ __metadata:
"@haqq/provider-base": "*"
react: "*"
react-native: "*"
checksum: fe30a5990c2738cc8e56049846592fc076d002647723ac1bb68614cff9978612229df4f8c29a0523336ceea507e8f7c056d17e88780df5c68b154a43a5f0efa5
checksum: cbec5f892b86d88770430e5c1e509a96e29090db315529bfd4887902fabd6b5ef0337cc11c86495f1d4bef087d50516a23523da54183d436a34ba476bac2abd8
languageName: node
linkType: hard

"@haqq/provider-ledger-react-native@npm:0.0.27":
version: 0.0.27
resolution: "@haqq/provider-ledger-react-native@npm:0.0.27"
"@haqq/provider-ledger-react-native@npm:0.0.28":
version: 0.0.28
resolution: "@haqq/provider-ledger-react-native@npm:0.0.28"
dependencies:
"@ethersproject/abstract-provider": ^5.7.0
"@ledgerhq/hw-app-eth": 6.35.4
Expand All @@ -2416,13 +2416,13 @@ __metadata:
react: "*"
react-native: "*"
react-native-ble-plx: "*"
checksum: a26e034d95e128e1c7d6fb20aae5af61b93970fce7039e6b53dd47fd4465ed0401f693e5c4221e5970927baea9097950f00192833c68f28fb189240a52222490
checksum: 318d2eea26e856dfff2ac1cddf4cd97234ac5df793a825323e0f425cf42ed617686a751fb4cc4c850e9436994162d9a17c51d963f537b0cbbae3b1a44a1caca5
languageName: node
linkType: hard

"@haqq/provider-mnemonic-react-native@npm:0.0.13":
version: 0.0.13
resolution: "@haqq/provider-mnemonic-react-native@npm:0.0.13"
"@haqq/provider-mnemonic-react-native@npm:0.0.14":
version: 0.0.14
resolution: "@haqq/provider-mnemonic-react-native@npm:0.0.14"
dependencies:
"@ethersproject/bytes": ^5.7.0
"@ethersproject/transactions": ^5.7.0
Expand All @@ -2433,13 +2433,13 @@ __metadata:
"@haqq/provider-web3-utils": "*"
"@haqq/shared-react-native": "*"
react-native-encrypted-storage: "*"
checksum: 68803cccb4da71c9f9240be365694375496634765dc4e77d2108952ccd84558ddaadd04eba65fe02fb81df2de8129333fa7e3c542b07b19304f21e6be147875e
checksum: 35506b1ea1d079af309f71f6e6015a19ee1ca6c3950a07587a308d17711d2523e051bd4b52984287d5f06ff02da23e8ab89a4e22d266a29a7ebf31146a23750d
languageName: node
linkType: hard

"@haqq/provider-sss-react-native@npm:0.0.13":
version: 0.0.13
resolution: "@haqq/provider-sss-react-native@npm:0.0.13"
"@haqq/provider-sss-react-native@npm:0.0.14":
version: 0.0.14
resolution: "@haqq/provider-sss-react-native@npm:0.0.14"
dependencies:
"@ethersproject/bytes": 5.7.0
"@ethersproject/transactions": 5.7.0
Expand All @@ -2452,7 +2452,7 @@ __metadata:
react: "*"
react-native: "*"
react-native-encrypted-storage: "*"
checksum: 18b4a5ebab51df9ff9eae2e624f7f6fdea4179739f6c1067f7cd70560be562dca38347c42978074fd058cc8bc59b3b0f48d3b3955f5033f2b53f3c57e74a0029
checksum: 59430174e5f4fcb5a0b01c58496287dd8b5e5f08725e160f4f60380ce495749ef504f3a6deed04f0776d6afffe55e9f259cdd56eb7bcc8f6a5d46636a6b7ce89
languageName: node
linkType: hard

Expand Down Expand Up @@ -10187,12 +10187,12 @@ __metadata:
"@evmos/transactions": 0.2.13
"@expo/react-native-action-sheet": 4.0.1
"@haqq/encryption-react-native": 0.0.4
"@haqq/provider-base": 0.0.22
"@haqq/provider-hot-react-native": 0.0.9
"@haqq/provider-keystone-react-native": 0.0.6
"@haqq/provider-ledger-react-native": 0.0.27
"@haqq/provider-mnemonic-react-native": 0.0.13
"@haqq/provider-sss-react-native": 0.0.13
"@haqq/provider-base": 0.0.23
"@haqq/provider-hot-react-native": 0.0.10
"@haqq/provider-keystone-react-native": 0.0.7
"@haqq/provider-ledger-react-native": 0.0.28
"@haqq/provider-mnemonic-react-native": 0.0.14
"@haqq/provider-sss-react-native": 0.0.14
"@haqq/provider-web3-utils": 0.0.14
"@haqq/shared-react-native": 0.0.9
"@invertase/react-native-apple-authentication": 2.2.2
Expand Down

0 comments on commit c7c6836

Please sign in to comment.