Skip to content

Commit

Permalink
feat(hook): add getAvailableWallets function
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianbormann committed Sep 19, 2022
1 parent d3eea63 commit 7b7e216
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/global/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ export interface MenuItemStyle {
borderRadius?: number;
gap?: number;
}

export type Wallet = {
name: string;
isEnabled: boolean;
icon: string;
apiVersion: string;
};
25 changes: 24 additions & 1 deletion src/hooks/useCardano.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect, useCallback } from 'react';
import { SignErrorCode } from '../global/types';
import { SignErrorCode, Wallet } from '../global/types';
import { bech32 } from 'bech32';
import { Buffer } from 'buffer';
import useLocalStorage from './useLocalStorage';
Expand Down Expand Up @@ -137,6 +137,28 @@ function useCardano() {
}
}, [isConnected]);

const getAvailableWallets: (
supportedWallets?: Array<String>
) => Promise<Array<Wallet>> = async (supportedWallets) => {
const cardano = (window as any).cardano;
const walletExtensions = Object.keys(cardano);

const availableWallets: Array<Wallet> = [];

for (const walletExtension of walletExtensions) {
if (typeof cardano[walletExtension].isEnabled === 'function') {
availableWallets.push({
name: cardano[walletExtension].name,
isEnabled: await cardano[walletExtension].isEnabled(),
apiVersion: cardano[walletExtension].apiVersion,
icon: cardano[walletExtension].icon,
});
}
}

return availableWallets;
};

return {
isEnabled,
isConnected,
Expand All @@ -145,6 +167,7 @@ function useCardano() {
signMessage,
connect,
disconnect,
getAvailableWallets,
};
}

Expand Down

0 comments on commit 7b7e216

Please sign in to comment.