Skip to content

Commit

Permalink
[LW-10289] replace Koralabs handleProvider with SDK handleHttpProvider (
Browse files Browse the repository at this point in the history
#1132)

* feat: replace Koralabs handleProvider with SDK handleHttpProvider

* fix: for address book validation

* fix: add adapter on handleProvider
  • Loading branch information
VanessaPC authored May 7, 2024
1 parent 0bb7467 commit 5f2c036
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 45 deletions.
6 changes: 3 additions & 3 deletions apps/browser-extension-wallet/.env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ CEXPLORER_URL_PREPROD=https://preprod.cexplorer.io
CEXPLORER_URL_SANCHONET=https://sancho.cexplorer.io

# ADA Handle URLs
ADA_HANDLE_URL_MAINNET=https://api.handle.me
ADA_HANDLE_URL_PREVIEW=https://preview.api.handle.me
ADA_HANDLE_URL_PREPROD=https://preprod.api.handle.me
ADA_HANDLE_URL_MAINNET=https://dev-mainnet.lw.iog.io/
ADA_HANDLE_URL_PREPROD=https://dev-preprod.lw.iog.io/
ADA_HANDLE_URL_PREVIEW=https://dev-preview.lw.iog.io/
ADA_HANDLE_URL_SANCHONET=

# Manifest.json
Expand Down
8 changes: 4 additions & 4 deletions apps/browser-extension-wallet/.env.developerpreview
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SAVED_PRICE_DURATION_IN_MINUTES=720
# Feature Flags
USE_PASSWORD_VERIFICATION=false
USE_DAPP_CONNECTOR=true
USE_TREZOR_HW=true
USE_TREZOR_HW=false
USE_TOKEN_PRICING=true
USE_NFT_FOLDERS=true
USE_MULTI_CURRENCY=true
Expand Down Expand Up @@ -74,9 +74,9 @@ CEXPLORER_URL_PREPROD=https://preprod.cexplorer.io
CEXPLORER_URL_SANCHONET=https://sancho.cexplorer.io

# ADA Handle URLs
ADA_HANDLE_URL_MAINNET=https://api.handle.me
ADA_HANDLE_URL_PREVIEW=https://preview.api.handle.me
ADA_HANDLE_URL_PREPROD=https://preprod.api.handle.me
ADA_HANDLE_URL_MAINNET=https://dev-mainnet.lw.iog.io
ADA_HANDLE_URL_PREVIEW=https://dev-preview.lw.iog.io
ADA_HANDLE_URL_PREPROD=https://dev-preprod.lw.iog.io
ADA_HANDLE_URL_SANCHONET=

# Manifest.json
Expand Down
6 changes: 3 additions & 3 deletions apps/browser-extension-wallet/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ CEXPLORER_URL_PREPROD=https://preprod.cexplorer.io
CEXPLORER_URL_SANCHONET=https://sancho.cexplorer.io

# ADA Handle URLs
ADA_HANDLE_URL_MAINNET=https://api.handle.me
ADA_HANDLE_URL_PREVIEW=https://preview.api.handle.me
ADA_HANDLE_URL_PREPROD=https://preprod.api.handle.me
ADA_HANDLE_URL_MAINNET=https://dev-mainnet.lw.iog.io
ADA_HANDLE_URL_PREVIEW=https://dev-preview.lw.iog.io
ADA_HANDLE_URL_PREPROD=https://dev-preprod.lw.iog.io
ADA_HANDLE_URL_SANCHONET=

# Manifest.json
Expand Down
1 change: 0 additions & 1 deletion apps/browser-extension-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@cardano-sdk/wallet": "0.37.3",
"@cardano-sdk/web-extension": "0.27.3",
"@emurgo/cip14-js": "~3.0.1",
"@koralabs/handles-public-api-interfaces": "^1.6.6",
"@lace/cardano": "0.1.0",
"@lace/common": "0.1.0",
"@lace/core": "0.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Wallet } from '@lace/cardano';
export const ADA_HANDLE_POLICY_ID = Wallet.ADA_HANDLE_POLICY_ID;
export const isAdaHandleEnabled = process.env.USE_ADA_HANDLE === 'true';
export const HANDLE_SERVER_URLS: Record<Exclude<Cardano.NetworkMagics, Cardano.NetworkMagics.Sanchonet>, string> = {
[Cardano.NetworkMagics.Mainnet]: 'https://api.handle.me',
[Cardano.NetworkMagics.Preprod]: 'https://preprod.api.handle.me',
[Cardano.NetworkMagics.Preview]: 'https://preview.api.handle.me'
[Cardano.NetworkMagics.Mainnet]: process.env.ADA_HANDLE_URL_MAINNET,
[Cardano.NetworkMagics.Preprod]: process.env.ADA_HANDLE_URL_PREPROD,
[Cardano.NetworkMagics.Preview]: process.env.ADA_HANDLE_URL_PREVIEW
};
14 changes: 8 additions & 6 deletions apps/browser-extension-wallet/src/hooks/useHandleResolver.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { useMemo } from 'react';
import { useWalletStore } from '@src/stores';
import { KoraLabsHandleProvider } from '@cardano-sdk/cardano-services-client';
import { ADA_HANDLE_POLICY_ID, HANDLE_SERVER_URLS } from '@src/features/ada-handle/config';
import { handleHttpProvider } from '@cardano-sdk/cardano-services-client';
import { HandleProvider } from '@cardano-sdk/core';
import { HANDLE_SERVER_URLS } from '@src/features/ada-handle/config';
import { logger } from '@lib/wallet-api-ui';

export const useHandleResolver = (): KoraLabsHandleProvider => {
export const useHandleResolver = (): HandleProvider => {
const {
currentChain: { networkMagic }
} = useWalletStore();

return useMemo(() => {
const serverUrl = HANDLE_SERVER_URLS[networkMagic as keyof typeof HANDLE_SERVER_URLS];
return new KoraLabsHandleProvider({
serverUrl,
policyId: ADA_HANDLE_POLICY_ID
return handleHttpProvider({
baseUrl: serverUrl,
logger
});
}, [networkMagic]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { runtime, storage as webStorage } from 'webextension-polyfill';
import { of, combineLatest, map, EMPTY } from 'rxjs';
import { getProviders } from './config';
import { DEFAULT_LOOK_AHEAD_SEARCH, HDSequentialDiscovery, createPersonalWallet, storage } from '@cardano-sdk/wallet';
import { KoraLabsHandleProvider } from '@cardano-sdk/cardano-services-client';
import axiosFetchAdapter from '@vespaiach/axios-fetch-adapter';
import { handleHttpProvider } from '@cardano-sdk/cardano-services-client';
import {
AnyWallet,
StoresFactory,
Expand All @@ -21,9 +20,10 @@ import {
walletRepositoryProperties
} from '@cardano-sdk/web-extension';
import { Wallet } from '@lace/cardano';
import { ADA_HANDLE_POLICY_ID, HANDLE_SERVER_URLS } from '@src/features/ada-handle/config';
import { HANDLE_SERVER_URLS } from '@src/features/ada-handle/config';
import { Cardano, NotImplementedError } from '@cardano-sdk/core';
import { cacheActivatedWalletAddressSubscription } from './cache-wallets-address';
import axiosFetchAdapter from '@vespaiach/axios-fetch-adapter';

const logger = console;

Expand Down Expand Up @@ -76,17 +76,17 @@ const walletFactory: WalletFactory<Wallet.WalletMetadata, Wallet.AccountMetadata
logger,
...providers,
stores,
handleProvider: new KoraLabsHandleProvider({
serverUrl:
handleProvider: handleHttpProvider({
adapter: axiosFetchAdapter,
baseUrl:
HANDLE_SERVER_URLS[
// TODO: remove exclude to support sanchonet
Cardano.ChainIds[chainName].networkMagic as Exclude<
Cardano.NetworkMagics,
Cardano.NetworkMagics.Sanchonet
>
],
adapter: axiosFetchAdapter,
policyId: ADA_HANDLE_POLICY_ID
logger
}),
addressDiscovery: new HDSequentialDiscovery(providers.chainHistoryProvider, DEFAULT_LOOK_AHEAD_SEARCH),
witnesser,
Expand Down
17 changes: 7 additions & 10 deletions apps/browser-extension-wallet/src/utils/validators/address-book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const verifyHandle = async (
): Promise<ValidationResult & { handles?: HandleResolution[] }> => {
try {
const resolvedHandles = await handleResolver.resolveHandles({ handles: [value.slice(1).toLowerCase()] });
if (resolvedHandles.length === 0 || resolvedHandles === null) {
if (!resolvedHandles[0]) {
return { valid: false };
}
return { valid: true, handles: resolvedHandles };
Expand Down Expand Up @@ -95,12 +95,10 @@ export const validateWalletHandle = async ({ value, handleResolver }: validateWa
}

const response = await verifyHandle(value, handleResolver);
const handles = response.handles;

if (!handles) {
if (!response.valid) {
throw new Error(i18n.t('general.errors.incorrectHandle'));
}

return '';
};

Expand All @@ -118,21 +116,20 @@ export const ensureHandleOwnerHasntChanged = async ({
}

const { handle, cardanoAddress } = handleResolution;
const response = await handleResolver.resolveHandles({ handles: [handle] });
const newHandleResolution = response[0];
const resolvedHandle = await handleResolver.resolveHandles({ handles: [handle] });

if (!newHandleResolution) {
if (!resolvedHandle[0]) {
throw new CustomError(i18n.t('general.errors.incorrectHandle'), false);
}

if (!Cardano.util.addressesShareAnyKey(cardanoAddress, newHandleResolution.cardanoAddress)) {
if (!Cardano.util.addressesShareAnyKey(cardanoAddress, resolvedHandle[0].cardanoAddress)) {
throw new CustomConflictError({
message: `${i18n.t('general.errors.handleConflict', {
receivedAddress: cardanoAddress,
actualAddress: newHandleResolution.cardanoAddress
actualAddress: resolvedHandle[0].cardanoAddress
})}`,
expectedAddress: cardanoAddress,
actualAddress: newHandleResolution.cardanoAddress
actualAddress: resolvedHandle[0].cardanoAddress
});
}
};
Expand Down
8 changes: 0 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10578,13 +10578,6 @@ __metadata:
languageName: node
linkType: hard

"@koralabs/handles-public-api-interfaces@npm:^1.6.6":
version: 1.6.6
resolution: "@koralabs/handles-public-api-interfaces@npm:1.6.6"
checksum: 10687928187ac935089781fc616a3c10596120aed2f5be2c28852cf2d8c1b14acbb72b90243f3228ff0bc9034c99230162d48d08f5aeb657c7d4decd6c6364e2
languageName: node
linkType: hard

"@lace/browser-extension-wallet@workspace:apps/browser-extension-wallet":
version: 0.0.0-use.local
resolution: "@lace/browser-extension-wallet@workspace:apps/browser-extension-wallet"
Expand All @@ -10600,7 +10593,6 @@ __metadata:
"@cardano-sdk/web-extension": 0.27.3
"@emurgo/cardano-message-signing-asmjs": 1.0.1
"@emurgo/cip14-js": ~3.0.1
"@koralabs/handles-public-api-interfaces": ^1.6.6
"@lace/cardano": 0.1.0
"@lace/common": 0.1.0
"@lace/core": 0.1.0
Expand Down

0 comments on commit 5f2c036

Please sign in to comment.