From bc57ae4f1f489b04e6db1e18aa35d0d8fef59a96 Mon Sep 17 00:00:00 2001 From: Lucas Portella Date: Tue, 20 Feb 2024 16:43:36 -0300 Subject: [PATCH 1/5] feat: add getBalance method --- src/ada/getBalance.ts | 20 ++++++++++++++++++++ src/ada/types.ts | 6 ++++++ 2 files changed, 26 insertions(+) create mode 100644 src/ada/getBalance.ts diff --git a/src/ada/getBalance.ts b/src/ada/getBalance.ts new file mode 100644 index 0000000..75850e4 --- /dev/null +++ b/src/ada/getBalance.ts @@ -0,0 +1,20 @@ +import { BalanceFetchError } from '@/errors/balance-fetch-error'; +import { EmptyAddressError } from '@/errors/empty-address-error'; +import type { ApiPromise } from './types'; +import type { Balance } from '@/types'; + +export async function getBalance(api: ApiPromise, address: string): Promise { + if (address.length === 0) + throw new EmptyAddressError() + + try { + const accountBalance = await api.getBalance(address) + return { + free: accountBalance, + frozen: 0, // asset staking never gets "locked" in Cardano Network + } + } + catch (error) { + throw new BalanceFetchError(error as Error) + } +} diff --git a/src/ada/types.ts b/src/ada/types.ts index dc7ed8b..9b352a5 100644 --- a/src/ada/types.ts +++ b/src/ada/types.ts @@ -5,3 +5,9 @@ export interface CardanoProviderProps { } export type CardanoUsedAddress = string + +export interface ApiPromise { + getBalance: (address: string) => Promise + getUsedAddresses: () => Promise + getUnusedAddresses: () => Promise +} From c70000e80a227ff17ef4dd58ad7152032bd52469 Mon Sep 17 00:00:00 2001 From: Lucas Portella Date: Tue, 20 Feb 2024 16:44:22 -0300 Subject: [PATCH 2/5] feat: add more available wallets --- src/ada/available-wallets.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ada/available-wallets.ts b/src/ada/available-wallets.ts index 283e492..7d06a74 100644 --- a/src/ada/available-wallets.ts +++ b/src/ada/available-wallets.ts @@ -1,8 +1,16 @@ // TODO: Add klever mobile app source name -export const availableWallets = ['nami'] as const +export const availableWallets = ['begin', 'eternl', 'flint ', 'gerowallet ', 'lace', 'nami', 'nufi', 'raywallet', 'yoroi'] as const export type AvailableWallet = typeof availableWallets[number] export enum CardanoWallet { + BEGIN = 'begin', + ETERNL = 'eternl', + FLINT = 'flint', + GERO_WALLET = 'gerowallet', + LACE = 'lace', NAMI = 'nami', + NUFI = 'nufi', + RAY_WALLET = 'raywallet', + YOROI = 'yoroi', } From 1a8bd822611fb2bcd7d114bec79b78b50cebc9bc Mon Sep 17 00:00:00 2001 From: Lucas Portella Date: Wed, 28 Feb 2024 16:29:45 -0300 Subject: [PATCH 3/5] refactor: cardano replace ada folder to cardano since its the correct fullname of the network --- src/{ada => cardano}/available-wallets.ts | 2 +- src/{ada => cardano}/connect.ts | 4 ++-- src/{ada => cardano}/getBalance.ts | 2 +- src/{ada => cardano}/index.ts | 0 src/{ada => cardano}/types.ts | 4 +--- src/index.ts | 2 +- src/types.ts | 4 ++-- src/web3-provider.ts | 4 ++-- 8 files changed, 10 insertions(+), 12 deletions(-) rename src/{ada => cardano}/available-wallets.ts (71%) rename src/{ada => cardano}/connect.ts (86%) rename src/{ada => cardano}/getBalance.ts (86%) rename src/{ada => cardano}/index.ts (100%) rename src/{ada => cardano}/types.ts (80%) diff --git a/src/ada/available-wallets.ts b/src/cardano/available-wallets.ts similarity index 71% rename from src/ada/available-wallets.ts rename to src/cardano/available-wallets.ts index 7d06a74..f0bad71 100644 --- a/src/ada/available-wallets.ts +++ b/src/cardano/available-wallets.ts @@ -1,5 +1,5 @@ // TODO: Add klever mobile app source name -export const availableWallets = ['begin', 'eternl', 'flint ', 'gerowallet ', 'lace', 'nami', 'nufi', 'raywallet', 'yoroi'] as const +export const availableWallets = ['begin', 'eternl', 'flint', 'gerowallet', 'lace', 'nami', 'nufi', 'raywallet', 'yoroi'] as const export type AvailableWallet = typeof availableWallets[number] diff --git a/src/ada/connect.ts b/src/cardano/connect.ts similarity index 86% rename from src/ada/connect.ts rename to src/cardano/connect.ts index de58df6..83e222a 100644 --- a/src/ada/connect.ts +++ b/src/cardano/connect.ts @@ -20,9 +20,9 @@ export async function connect(wallet?: string): Promise { if (typeof injectedWallet === 'undefined') throw new NoProviderAvailableError() - await web3Window.cardano[injectedWallet].enable() + const api = await web3Window.cardano[injectedWallet].enable() - const usedAddresses: CardanoUsedAddress[] = await web3Window.cardano.getUsedAddresses() + const usedAddresses: CardanoUsedAddress[] = await api.getUsedAddresses() if (usedAddresses.length === 0) throw new NoAvailableAccountsError() diff --git a/src/ada/getBalance.ts b/src/cardano/getBalance.ts similarity index 86% rename from src/ada/getBalance.ts rename to src/cardano/getBalance.ts index 75850e4..c587e5b 100644 --- a/src/ada/getBalance.ts +++ b/src/cardano/getBalance.ts @@ -11,7 +11,7 @@ export async function getBalance(api: ApiPromise, address: string): Promise; export type CardanoUsedAddress = string diff --git a/src/index.ts b/src/index.ts index 8fa2d24..4a2c188 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -export * from './ada/index' +export * from './cardano/index' export * from './networks' export * from './substrate/dot/index' export * from './substrate/ksm/index' diff --git a/src/types.ts b/src/types.ts index 2c4bbb1..27d4012 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -import type { CardanoProviderProps } from './ada/types' +import type { CardanoProviderProps } from './cardano/types' import type { NetworkKey } from './networks' import type { SubstrateProviderProps } from './substrate/types' @@ -19,7 +19,7 @@ export type ProviderBuilderProps = T extends 'dot' | 'ksm' // TODO: Add type and functions of injected providers export type Web3Window = { injectedWeb3: any - cardano: any + cardano?: any } & Window & typeof globalThis export const web3Window = (window as Web3Window) diff --git a/src/web3-provider.ts b/src/web3-provider.ts index 7ea3a8a..4c0c0df 100644 --- a/src/web3-provider.ts +++ b/src/web3-provider.ts @@ -1,5 +1,5 @@ -import { CardanoProvider } from './ada'; -import type { CardanoProviderProps } from './ada/types'; +import { CardanoProvider } from './cardano'; +import type { CardanoProviderProps } from './cardano/types'; import type { ProviderEntity } from './entities/provider-entity'; import { InvalidNetworkError } from './errors/invalid-network-error'; import type { NetworkData, NetworkKey } from './networks'; From 31f63278c1ad96ed141f7c7d45d329e524dc1515 Mon Sep 17 00:00:00 2001 From: Lucas Portella Date: Wed, 28 Feb 2024 16:32:33 -0300 Subject: [PATCH 4/5] cardano connect connect method have some issues, try fixing and debug some through catch block --- src/cardano/connect.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/cardano/connect.ts b/src/cardano/connect.ts index 83e222a..f9c3eea 100644 --- a/src/cardano/connect.ts +++ b/src/cardano/connect.ts @@ -18,13 +18,18 @@ export async function connect(wallet?: string): Promise { } if (typeof injectedWallet === 'undefined') + // maybe impossible to hit this branch since Cardano API demands a wallet in the browser to be injected. throw new NoProviderAvailableError() - const api = await web3Window.cardano[injectedWallet].enable() - - const usedAddresses: CardanoUsedAddress[] = await api.getUsedAddresses() - if (usedAddresses.length === 0) - throw new NoAvailableAccountsError() - - return usedAddresses + try { + const api = await web3Window.cardano[injectedWallet].enable() + const usedAddresses: CardanoUsedAddress[] = await api.getUsedAddresses() + if (usedAddresses.length === 0) + throw new NoAvailableAccountsError() + return usedAddresses + } + catch (error) { + console.log(error) + return [] + } } From 071cc5ca090434da889e2540d0b8aa8fcd8b4054 Mon Sep 17 00:00:00 2001 From: Lucas Portella Date: Mon, 4 Mar 2024 10:13:56 -0300 Subject: [PATCH 5/5] fix: connect change used by unused addresses method for better privacy and avoid empty accounts bug --- src/cardano/connect.ts | 12 ++++++------ src/cardano/types.ts | 6 ++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/cardano/connect.ts b/src/cardano/connect.ts index f9c3eea..78c1863 100644 --- a/src/cardano/connect.ts +++ b/src/cardano/connect.ts @@ -3,9 +3,9 @@ import { NoAvailableAccountsError } from '@/errors/no-accounts-available-error'; import { NoProviderAvailableError } from '@/errors/no-provider-available-error'; import { web3Window } from '@/types'; import { availableWallets } from './available-wallets'; -import type { CardanoUsedAddress } from './types'; +import type { CardanoUnusedAddresses } from './types'; -export async function connect(wallet?: string): Promise { +export async function connect(wallet?: string): Promise { if (!web3Window.cardano) throw new NotInjectedError() @@ -23,13 +23,13 @@ export async function connect(wallet?: string): Promise { try { const api = await web3Window.cardano[injectedWallet].enable() - const usedAddresses: CardanoUsedAddress[] = await api.getUsedAddresses() - if (usedAddresses.length === 0) + const unusedAddresses: CardanoUnusedAddresses[] = await api.getUnusedAddresses() + if (unusedAddresses.length === 0) throw new NoAvailableAccountsError() - return usedAddresses + return unusedAddresses } catch (error) { - console.log(error) + console.error(error) return [] } } diff --git a/src/cardano/types.ts b/src/cardano/types.ts index 82852ed..4748c37 100644 --- a/src/cardano/types.ts +++ b/src/cardano/types.ts @@ -1,9 +1,11 @@ -import type { AvailableWallet } from './available-wallets'; +import type { CardanoWallet } from './available-wallets'; -export type CardanoProviderProps = Record; +export type CardanoProviderProps = Record<'wallet', CardanoWallet>; export type CardanoUsedAddress = string +export type CardanoUnusedAddresses = string + export interface ApiPromise { getBalance: (address: string) => Promise getUsedAddresses: () => Promise