Skip to content

Commit

Permalink
refactor: cardano-js-sdk wallet 0.12.0 min compat
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-barros committed May 31, 2023
1 parent ad8a1a6 commit 547b405
Show file tree
Hide file tree
Showing 25 changed files with 205 additions and 154 deletions.
17 changes: 9 additions & 8 deletions apps/browser-extension-wallet/package.json
Expand Up @@ -30,12 +30,13 @@
"homepage": "https://github.com/input-output-hk/lace/blob/master/apps/browser-extension-wallet/README.md",
"dependencies": {
"@ant-design/icons": "^4.7.0",
"@cardano-sdk/core": "~0.11.0",
"@cardano-sdk/core": "~0.12.0",
"@cardano-sdk/dapp-connector": "~0.9.0",
"@cardano-sdk/input-selection": "~0.9.1",
"@cardano-sdk/util": "~0.9.0",
"@cardano-sdk/wallet": "~0.11.0",
"@cardano-sdk/web-extension": "~0.9.0",
"@cardano-sdk/util": "~0.10.0",
"@cardano-sdk/tx-construction": "~0.5.0",
"@cardano-sdk/wallet": "~0.12.0",
"@cardano-sdk/web-extension": "~0.10.0",
"@emurgo/cip14-js": "~3.0.1",
"@koralabs/handles-public-api-interfaces": "^1.6.6",
"@lace/cardano": "0.1.0",
Expand All @@ -58,6 +59,7 @@
"dexie-react-hooks": "1.0.7",
"graphql-tag": "2.12.5",
"i18next": "20.4.0",
"intersection-observer-polyfill": "0.1.0",
"jdenticon": "3.1.0",
"lodash": "4.17.21",
"matomo-tracker": "^2.2.4",
Expand All @@ -73,8 +75,7 @@
"readable-stream": "^3.6.0",
"rxjs": "7.4.0",
"webextension-polyfill": "0.8.0",
"zustand": "3.5.14",
"intersection-observer-polyfill": "0.1.0"
"zustand": "3.5.14"
},
"devDependencies": {
"@dcspark/cardano-multiplatform-lib-asmjs": "^3.1.0",
Expand All @@ -91,8 +92,8 @@
"fake-indexeddb": "3.1.3",
"fork-ts-checker-webpack-plugin": "^7.2.1",
"jest-webextension-mock": "^3.7.19",
"webassembly-loader-sw": "^1.1.0",
"tsconfig-paths-webpack-plugin": "3.5.2"
"tsconfig-paths-webpack-plugin": "3.5.2",
"webassembly-loader-sw": "^1.1.0"
},
"directories": {
"lib": "src",
Expand Down
Expand Up @@ -77,7 +77,7 @@ export const DelegationContainer = (): React.ReactElement => {
const analytics = useAnalyticsContext();

const delegationDetails = useDelegationDetails();
const isStakeRegistered = rewardAccounts && rewardAccounts[0].keyStatus === Wallet.StakeKeyStatus.Registered;
const isStakeRegistered = rewardAccounts && rewardAccounts[0].keyStatus === Wallet.Cardano.StakeKeyStatus.Registered;
const coinBalance = balance?.total?.coinBalance && Number(balance?.total?.coinBalance);
const hasNoFunds = (coinBalance < Number(minAda) && !isStakeRegistered) || (coinBalance === 0 && isStakeRegistered);
const isDelegating = !!(rewardAccounts && delegationDetails);
Expand Down
@@ -1,9 +1,10 @@
import axiosFetchAdapter from '@vespaiach/axios-fetch-adapter';
import { Wallet } from '@lace/cardano';
import { RemoteApiPropertyType } from '@cardano-sdk/web-extension';
import { RemoteApiProperties, RemoteApiPropertyType } from '@cardano-sdk/web-extension';
import { getBaseUrlForChain } from '@src/utils/chain';
import { BackgroundService } from '../types';

export const backgroundServiceProperties = {
export const backgroundServiceProperties: RemoteApiProperties<BackgroundService> = {
requestMessage$: RemoteApiPropertyType.HotObservable,
migrationState$: RemoteApiPropertyType.HotObservable,
coinPrices: {
Expand Down
@@ -1,5 +1,5 @@
import { Origin } from '@cardano-sdk/dapp-connector';
import { consumeRemoteApi, exposeApi, RemoteApiPropertyType } from '@cardano-sdk/web-extension';
import { consumeRemoteApi, exposeApi, RemoteApiProperties, RemoteApiPropertyType } from '@cardano-sdk/web-extension';
import { AuthorizedDappService, AuthorizedDappStorage } from '@src/types/dappConnector';
import { DAPP_CHANNELS } from '@src/utils/constants';
import { Subject, of } from 'rxjs';
Expand Down Expand Up @@ -30,7 +30,7 @@ const authorizedDappsApi: AuthorizedDappService = {
}
};

export const dappServiceProperties = {
export const dappServiceProperties: RemoteApiProperties<AuthorizedDappService> = {
authorizedDappsList: RemoteApiPropertyType.HotObservable,
removeAuthorizedDapp: RemoteApiPropertyType.MethodReturningPromise
};
Expand Down
Expand Up @@ -15,7 +15,7 @@ import { Subject, of, BehaviorSubject } from 'rxjs';
import { walletRoutePaths } from '@routes/wallet-paths';
import { backgroundServiceProperties } from '../config';
import { exposeApi } from '@cardano-sdk/web-extension';
import { Cardano, Asset } from '@cardano-sdk/core';
import { Cardano } from '@cardano-sdk/core';
import { config } from '@src/config';
import {
setBackgroundStorage,
Expand Down Expand Up @@ -99,7 +99,7 @@ const fetchTokenPrices = () => {
if (!assetName) continue;
const policyId = Cardano.PolicyId(policy);
// get the asset id to use as key for tokenPrices Map
const assetId = Asset.util.assetIdFromPolicyAndName(policyId, assetName);
const assetId = Cardano.AssetId.fromParts(policyId, assetName);
// it is possible for the price to come as NA so we need check this
const price = priceInfo.last_price === 'NA' ? 0 : (priceInfo.last_price as number);

Expand Down
2 changes: 1 addition & 1 deletion apps/browser-extension-wallet/src/types/wallet.ts
Expand Up @@ -5,7 +5,7 @@ export type Tokens = Wallet.Cardano.TokenMap;
export type TokensDetails = Wallet.Asset.AssetInfo;
export type Balance = Wallet.Balance;
export type CoinOverview = Omit<CoinItemProps, 'handleClick'>;
export type RewardAccount = Wallet.RewardAccount;
export type RewardAccount = Wallet.Cardano.RewardAccount;
export type Transaction = Wallet.Cardano.HydratedTx | Wallet.Cardano.Tx;
export type CoinId = {
id: string;
Expand Down
Expand Up @@ -195,6 +195,7 @@ export const mockAssetMetadata: Wallet.Asset.AssetInfo = {
],
name: Wallet.Cardano.AssetName('54534c41'),
policyId: Wallet.Cardano.PolicyId('659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba82'),
quantity: BigInt('1000'),
supply: BigInt('1000')
};

Expand Down Expand Up @@ -323,6 +324,7 @@ export const mockAsset: Wallet.Asset.AssetInfo = {
fingerprint: Wallet.Cardano.AssetFingerprint('asset1cvmyrfrc7lpht2hcjwr9lulzyyjv27uxh3kcz0'),
name: Wallet.Cardano.AssetName('54657374636f696e'),
policyId: Wallet.Cardano.PolicyId('6b8d07d69639e9413dd637a1a815a7323c69c86abbafb66dbfdb1aa7'),
quantity: BigInt('100042'),
supply: BigInt('100042'),
tokenMetadata: {
assetId: Wallet.Cardano.AssetId('6b8d07d69639e9413dd637a1a815a7323c69c86abbafb66dbfdb1aa7'),
Expand All @@ -347,6 +349,7 @@ export const mockNft: Wallet.Asset.AssetInfo = {
fingerprint: Wallet.Cardano.AssetFingerprint('asset1pkpwyknlvul7az0xx8czhl60pyel45rpje4z8w'),
name: Wallet.Cardano.AssetName('54534c41'),
policyId: Wallet.Cardano.PolicyId('659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba82'),
quantity: BigInt(1),
supply: BigInt(1),
tokenMetadata: {
assetId: Wallet.Cardano.AssetId('659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba8254534c41'),
Expand Down
Expand Up @@ -70,7 +70,7 @@ export const StakePoolConfirmation = ({ popupView }: StakePoolConfirmationProps)
const protocolParameters = useObservable(inMemoryWallet?.protocolParameters$);
const rewardAccounts = useObservable(inMemoryWallet.delegation.rewardAccounts$);
const deposit =
rewardAccounts && rewardAccounts[0]?.keyStatus !== Wallet.StakeKeyStatus.Registered
rewardAccounts && rewardAccounts[0]?.keyStatus !== Wallet.Cardano.StakeKeyStatus.Registered
? protocolParameters?.stakeKeyDeposit
: undefined;
const fee = delegationBuiltTx ? delegationBuiltTx?.body?.fee : 0;
Expand Down
Expand Up @@ -59,7 +59,7 @@ export const Staking = (): React.ReactElement => {
}, [fetchNetworkInfo, blockchainProvider]);

const coinBalance = balance?.total?.coinBalance && Number(balance?.total?.coinBalance);
const isStakeRegistered = rewardAccounts && rewardAccounts[0].keyStatus === Wallet.StakeKeyStatus.Registered;
const isStakeRegistered = rewardAccounts && rewardAccounts[0].keyStatus === Wallet.Cardano.StakeKeyStatus.Registered;
const isDelegating = rewardAccounts && delegationDetails;
const hasNoFunds = (coinBalance < Number(minAda) && !isStakeRegistered) || (coinBalance === 0 && isStakeRegistered);
const canDelegate = !isDelegating && isNumber(coinBalance) && !hasNoFunds;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -54,7 +54,8 @@
"packages/staking/package.json": "sort-package-json"
},
"contributors": [
"Rhys Bartels-Waller <rhys.bartelswaller@iohk.io> (https://iohk.io)"
"Rhys Bartels-Waller <rhys.bartelswaller@iohk.io> (https://iohk.io)",
"Lucas Barros Araújo <lucas.araujo@iohk.io> (https://iohk.io)"
],
"license": "Apache-2.0",
"bugs": {
Expand Down
15 changes: 8 additions & 7 deletions packages/cardano/package.json
Expand Up @@ -29,13 +29,14 @@
},
"homepage": "https://github.com/input-output-hk/lace/blob/master/packages/cardano/README.md",
"dependencies": {
"@cardano-sdk/cardano-services-client": "~0.9.0",
"@cardano-sdk/core": "~0.11.0",
"@cardano-sdk/cardano-services-client": "~0.9.1",
"@cardano-sdk/core": "~0.12.0",
"@cardano-sdk/crypto": "~0.1.4",
"@cardano-sdk/key-management": "~0.5.1",
"@cardano-sdk/util": "~0.9.0",
"@cardano-sdk/wallet": "~0.11.0",
"@cardano-sdk/web-extension": "~0.9.0",
"@cardano-sdk/hardware-ledger": "~0.2.0",
"@cardano-sdk/key-management": "~0.6.0",
"@cardano-sdk/util": "~0.10.0",
"@cardano-sdk/wallet": "~0.12.0",
"@cardano-sdk/web-extension": "~0.10.0",
"@lace/common": "0.1.0",
"@stablelib/chacha20poly1305": "1.0.1",
"bignumber.js": "9.0.1",
Expand All @@ -55,7 +56,7 @@
"webextension-polyfill": "0.8.0"
},
"devDependencies": {
"@cardano-sdk/util-dev": "~0.9.0",
"@cardano-sdk/util-dev": "~0.10.0",
"@emurgo/cardano-message-signing-browser": "1.0.1",
"@dcspark/cardano-multiplatform-lib-browser": "^3.1.1",
"rollup-plugin-polyfill-node": "^0.8.0",
Expand Down
11 changes: 7 additions & 4 deletions packages/cardano/src/wallet/index.ts
Expand Up @@ -24,12 +24,8 @@ export { ProtocolParameters } from '../../../../node_modules/@cardano-sdk/core/d

export {
BalanceTracker as Balance,
InitializeTxProps,
InitializeTxPropsValidationResult,
RewardAccount,
RewardsHistory,
SingleAddressWallet,
StakeKeyStatus,
storage,
SingleAddressWalletProps,
ObservableWallet,
Expand All @@ -39,6 +35,13 @@ export {
TxInFlight
} from '../../../../node_modules/@cardano-sdk/wallet/dist/cjs';

export {
InitializeTxProps,
InitializeTxResult,
InitializeTxPropsValidationResult,
MinimumCoinQuantityPerOutput
} from '@cardano-sdk/tx-construction';

export * as KeyManagement from '../../../../node_modules/@cardano-sdk/key-management/dist/cjs';

export { HexBlob } from '@cardano-sdk/util';
Expand Down
@@ -1,5 +1,5 @@
import { Cardano } from '@cardano-sdk/core';
import { ObservableWallet, StakeKeyStatus } from '@cardano-sdk/wallet';
import { ObservableWallet } from '@cardano-sdk/wallet';
import { rewardAcountMock } from '@src/wallet/test/mocks/mock';
import { mockObservableWallet } from '@src/wallet/test/mocks';
import { firstValueFrom, of } from 'rxjs';
Expand All @@ -24,7 +24,7 @@ describe('Testing buildDelegation', () => {
const wallet = {
...mockObservableWallet,
delegation: {
rewardAccounts$: of([{ ...rewardAcountMock, keyStatus: StakeKeyStatus.Unregistered }])
rewardAccounts$: of([{ ...rewardAcountMock, keyStatus: Cardano.StakeKeyStatus.Unregistered }])
}
} as unknown as ObservableWallet;
const { certificates } = await buildDelegation(wallet, poolId);
Expand All @@ -39,7 +39,7 @@ describe('Testing buildDelegation', () => {
delegation: { rewardAccounts$: of([rewardAcountMock]) }
} as unknown as ObservableWallet;
const walletRewardAccount = (await firstValueFrom(wallet.delegation.rewardAccounts$))[0];
walletRewardAccount.keyStatus = StakeKeyStatus.Registered;
walletRewardAccount.keyStatus = Cardano.StakeKeyStatus.Registered;
const { certificates } = await buildDelegation(wallet, poolId);

expect(certificates).toContainEqual(delegationCertificate);
Expand Down
@@ -1,6 +1,7 @@
/* eslint-disable no-magic-numbers */
import { Cardano } from '@cardano-sdk/core';
import { InitializeTxProps, SingleAddressWallet } from '@cardano-sdk/wallet';
import { SingleAddressWallet } from '@cardano-sdk/wallet';
import { InitializeTxProps } from '@cardano-sdk/tx-construction';
import * as mocks from '../../test/mocks';
import { buildTransaction } from '../build-transaction';

Expand Down
9 changes: 5 additions & 4 deletions packages/cardano/src/wallet/lib/build-delegation.ts
@@ -1,16 +1,17 @@
import { Cardano } from '@cardano-sdk/core';
import { InitializeTxProps, ObservableWallet, RewardAccount, StakeKeyStatus } from '@cardano-sdk/wallet';
import { ObservableWallet } from '@cardano-sdk/wallet';
import { InitializeTxProps } from '@cardano-sdk/tx-construction';
import { firstValueFrom } from 'rxjs';
const { CertificateType } = Cardano;
const { CertificateType, StakeKeyStatus, RewardAccount } = Cardano;

const buildDelegationCertificates = (
walletRewardAccount: RewardAccount,
walletRewardAccount: Cardano.RewardAccountInfo,
poolToDelegateId: Cardano.PoolId
): Cardano.Certificate[] => {
const { address: rewardAccount, keyStatus } = walletRewardAccount;
const isStakeKeyRegistered = keyStatus === StakeKeyStatus.Registered;

const stakeKeyHash = Cardano.RewardAccount.toHash(rewardAccount);
const stakeKeyHash = RewardAccount.toHash(rewardAccount);

const delegationCertificate: Cardano.StakeDelegationCertificate = {
__typename: CertificateType.StakeDelegation,
Expand Down
3 changes: 2 additions & 1 deletion packages/cardano/src/wallet/lib/build-transaction-props.ts
@@ -1,5 +1,6 @@
import { Cardano } from '@cardano-sdk/core';
import { Assets, InitializeTxProps } from '@cardano-sdk/wallet';
import { Assets } from '@cardano-sdk/wallet';
import { InitializeTxProps } from '@cardano-sdk/tx-construction';
import isEmpty from 'lodash/isEmpty';
import { assetBalanceToBigInt } from '../util/asset-balance';
import { getAuxiliaryData } from './get-auxiliary-data';
Expand Down
9 changes: 2 additions & 7 deletions packages/cardano/src/wallet/lib/build-transaction.ts
@@ -1,10 +1,5 @@
import {
createWalletUtil,
InitializeTxProps,
InitializeTxResult,
MinimumCoinQuantityPerOutput,
ObservableWallet
} from '@cardano-sdk/wallet';
import { createWalletUtil, ObservableWallet } from '@cardano-sdk/wallet';
import { InitializeTxProps, InitializeTxResult, MinimumCoinQuantityPerOutput } from '@cardano-sdk/tx-construction';

export interface InitializedCardanoTransaction {
transaction: InitializeTxResult;
Expand Down
12 changes: 6 additions & 6 deletions packages/cardano/src/wallet/lib/cardano-wallet.ts
Expand Up @@ -16,8 +16,9 @@ import {
ObservableWallet,
SingleAddressWalletDependencies,
storage,
SetupWalletProps
} from '@cardano-sdk/wallet';
SetupWalletProps,
restoreKeyAgent
} from '../../../../../node_modules/@cardano-sdk/wallet/dist/cjs';
import * as KeyManagement from '../../../../../node_modules/@cardano-sdk/key-management/dist/cjs';
import { WalletManagerActivateProps, WalletManagerUi } from '@cardano-sdk/web-extension';
import { ChainName, WalletManagerProviderTypes } from '../types';
Expand Down Expand Up @@ -193,8 +194,7 @@ export const restoreWallet = async (
createWallet: SetupWalletProps<ObservableWallet, KeyManagement.KeyAgent>['createWallet']
): Promise<{ keyAgent: KeyManagement.KeyAgent; wallet: ObservableWallet }> => {
const { keyAgent, wallet } = await setupWallet({
createKeyAgent: async (dependencies) =>
await KeyManagement.restoreKeyAgent(keyAgentData, dependencies, getPassword),
createKeyAgent: async (dependencies) => await restoreKeyAgent(keyAgentData, dependencies, getPassword),
createWallet,
logger: console,
bip32Ed25519: new Crypto.CmlBip32Ed25519(CML)
Expand Down Expand Up @@ -258,7 +258,7 @@ export const validateWalletPassword = async (
password: string
): Promise<boolean> => {
const getPassword = async () => Buffer.from(password);
const keyAgent = await KeyManagement.restoreKeyAgent(
const keyAgent = await restoreKeyAgent(
keyAgentData,
// Not needed for this
{
Expand All @@ -285,7 +285,7 @@ export const validateWalletMnemonic = async (
password: string
): Promise<boolean> => {
const getPassword = async () => Buffer.from(password);
const originalKeyAgent = await KeyManagement.restoreKeyAgent(
const originalKeyAgent = await restoreKeyAgent(
keyAgentData,
// Not needed for this
{
Expand Down
2 changes: 1 addition & 1 deletion packages/cardano/src/wallet/lib/get-total-minimum-coins.ts
@@ -1,4 +1,4 @@
import { InitializeTxPropsValidationResult } from '@cardano-sdk/wallet';
import { InitializeTxPropsValidationResult } from '@cardano-sdk/tx-construction';
import BigNumber from 'bignumber.js';

export interface TxMinimumCoinQuantity {
Expand Down

0 comments on commit 547b405

Please sign in to comment.