Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fix nami wallet problems #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect } from 'react';

import { State, useStore } from './store';
import { WalletName } from './typescript/cip30';

Expand Down Expand Up @@ -56,7 +57,7 @@ const useCardanoWallet = ({
connectWithStore(connectedWalletName, localStorageKey);
}, 10);
}
}, [autoConnect, localStorageKey, (window as any).cardano]);
}, [autoConnect, localStorageKey, connectWithStore]);

const connect = async (walletName: WalletName) => {
await connectWithStore(walletName, localStorageKey);
Expand Down
67 changes: 51 additions & 16 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { bech32 } from 'bech32';
import cbor from 'cbor';
import produce from 'immer';
import create from 'zustand';

import { NetworkId, WalletApi, WalletName } from '../typescript/cip30';
import cbor from 'cbor';
import { fromHex } from '../utils';
import { bech32 } from 'bech32';

export type State = {
isConnected: boolean;
Expand Down Expand Up @@ -43,6 +44,43 @@ const defaults = {
isRefetchingBalance: false,
};

const getRawAddress = async (api: WalletApi) => {
const [rawUnusedAddress] = await api.getUnusedAddresses();

if (rawUnusedAddress) {
return fromHex(rawUnusedAddress);
}

const [rawUsedAddress] = await api.getUsedAddresses();

return fromHex(rawUsedAddress);
};

const getAddressInfo = async (api: WalletApi) => {
const address = await getRawAddress(api);

const words = bech32.toWords(address);

const bechAddr = bech32.encode(
address[0] === NetworkId.MAINNET ? 'addr' : 'addr_test',
words,
130
);

return {
address: bechAddr,
network: address[0] as NetworkId,
};
};

const getLovelaceBalance = async (api: WalletApi) => {
const balance = await api.getBalance();

const [lovelaceBalance] = cbor.decode(fromHex(balance));

return lovelaceBalance || 0;
};

export const useStore = create<State>()((set, get) => ({
...defaults,

Expand Down Expand Up @@ -80,19 +118,16 @@ export const useStore = create<State>()((set, get) => ({
})
);
try {
const api: WalletApi = await (window as any).cardano[walletName].enable();
const [rawAddress] = await api.getUnusedAddresses();
const address = fromHex(rawAddress);
const balance = await api.getBalance();
// Exit early if the Cardano dApp-Wallet Web Bridge (CIP 30) has not been injected
// This can happen in a SSR scenario for example
if (typeof window === 'undefined' || !(window as any).cardano) {
throw Error('window.cardano is undefined');
}

const decodedBalance = cbor.decode(fromHex(balance));
const words = bech32.toWords(address);
const api: WalletApi = await (window as any).cardano[walletName].enable();

const bechAddr = bech32.encode(
address[0] == NetworkId.MAINNET ? 'addr' : 'addr_test',
words,
130
);
const { address, network } = await getAddressInfo(api);
const lovelaceBalance = await getLovelaceBalance(api);

localStorage.setItem(localStorageKey, walletName);

Expand All @@ -101,9 +136,9 @@ export const useStore = create<State>()((set, get) => ({
draft.isConnecting = false;
draft.isConnected = true;
draft.api = api;
draft.lovelaceBalance = decodedBalance[0] ?? 0;
draft.address = bechAddr;
draft.network = address[0] as NetworkId;
draft.lovelaceBalance = lovelaceBalance;
draft.address = address;
draft.network = network;
draft.connectedWallet = walletName;
})
);
Expand Down