-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { NotInjectedError } from '@/errors'; | ||
import { NoAvailableAccountsError } from '@/errors/no-accounts-available-error'; | ||
import { NoProviderAvailableError } from '@/errors/no-provider-available-error'; | ||
import { Account } from '@/types'; | ||
|
||
interface CardanoWeb extends Window { | ||
cardano: { | ||
[key: string]: CardanoExtension; | ||
}; | ||
} | ||
|
||
interface CardanoInitializedExtension extends CardanoWeb { | ||
getBalance: () => Promise<string> | ||
getUsedAddresses: () => Promise<string[]> | ||
getUnusedAddresses: () => Promise<string[]> | ||
} | ||
|
||
interface CardanoExtension { | ||
apiVersion: string; | ||
enable: () => Promise<CardanoInitializedExtension>; | ||
isEnabled: () => Promise<boolean>; | ||
icon: string; | ||
name: string; | ||
} | ||
|
||
|
||
const CardanoWindow: CardanoWeb = (window as any) | ||
|
||
export async function connect(appName: string): Promise<Account[]> { | ||
if (!(window as any).cardano) | ||
throw new NotInjectedError() | ||
|
||
const isInjected = CardanoWindow.cardano[appName].name === appName | ||
if (!isInjected) | ||
throw new NoProviderAvailableError() | ||
|
||
const api: CardanoInitializedExtension = await CardanoWindow.cardano[appName].enable() | ||
|
||
const rawAccounts = await api.getUsedAddresses() | ||
if (rawAccounts.length === 0) | ||
throw new NoAvailableAccountsError() | ||
|
||
const accounts = rawAccounts.map((account) => ({ | ||
address: account | ||
})) | ||
|
||
return accounts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { MissingPropsError } from '@/errors/missing-props-error'; | ||
import { Network } from '@/networks'; | ||
import { SubstrateProvider } from '@/substrate'; | ||
import type { SubstrateProviderProps } from '@/substrate/types'; | ||
|
||
export class CardanoProvider extends SubstrateProvider { | ||
constructor({ appName, rpcProvider }: SubstrateProviderProps) { | ||
if (!appName || appName.length === 0 || !rpcProvider || rpcProvider.length === 0) | ||
throw new MissingPropsError() | ||
|
||
super(Network.CARDANO, appName, rpcProvider) | ||
} | ||
} |