Skip to content

Commit

Permalink
fix: encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinSchere committed Jul 24, 2024
1 parent f0df13a commit 27705d6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect } from 'react';

import { useStore, DetectedWallet as _, State } from './store';
import { useStore, WalletInfo as _, State } from './store';
import { WalletName } from './typescript/cip30';

export type UseCardanoWalletOptions = {
Expand Down
21 changes: 8 additions & 13 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import produce from 'immer';
import create from 'zustand';

import { NetworkId, WalletApi, WalletName } from '../typescript/cip30';
import { walletPrettyNames } from '../wallets';
import { fromHex, parseBalance } from '../utils';
import { fromHex, parseBalance, toWalletInfo } from '../utils';

export interface DetectedWallet {
export interface WalletInfo {
name: WalletName;
icon: string;
displayName: string;
Expand All @@ -17,7 +16,7 @@ export type State = {
isConnecting: boolean;
isRefetchingBalance: boolean;

detectedWallets: DetectedWallet[];
detectedWallets: WalletInfo[];

address: null | string;

Expand All @@ -26,12 +25,12 @@ export type State = {
/**
* The wallet that was selected to connect.
*/
selectedWallet: null | string;
selectedWallet: null | WalletInfo;

/**
* The wallet that is currently connected.
*/
connectedWallet: null | string;
connectedWallet: null | WalletInfo;

lovelaceBalance: null | number;
api: null | WalletApi;
Expand Down Expand Up @@ -96,11 +95,7 @@ export const useStore = create<State>()((set, get) => ({
produce((draft: State) => {
draft.detectedWallets = Object.keys(ns)
.filter(ns => Object.values(WalletName).includes(ns as WalletName))
.map(n => ({
name: n as WalletName,
displayName: walletPrettyNames[n as WalletName],
icon: ns[n].icon,
}));
.map(n => toWalletInfo(n as WalletName));
})
);
},
Expand All @@ -109,7 +104,7 @@ export const useStore = create<State>()((set, get) => ({
set(
produce((draft: State) => {
draft.isConnecting = true;
draft.selectedWallet = walletName;
draft.selectedWallet = toWalletInfo(walletName);
})
);

Expand Down Expand Up @@ -164,7 +159,7 @@ export const useStore = create<State>()((set, get) => ({
draft.rewardAddress = rewardAddress;
draft.address = bechAddr;
draft.network = address[0] as NetworkId;
draft.connectedWallet = walletName;
draft.connectedWallet = toWalletInfo(walletName);
})
);
} catch (e) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/balance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ describe('Batcher tests', () => {

const result2 = parseBalance('1b00000001dc87463b');
expect(result2).toBe('7994820155');

const result3 = parseBalance(
'821a0510379ea1581cceb8a6f66d8abf778e111ffb982626e6e795d8ef15e7261ea98738d2a14d4d61727342697264733732383001'
);

expect(result3).toBe('84948894');
});
});
18 changes: 16 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { decode } from 'cbor-x';
import { Encoder } from 'cbor-x';
import { WalletName } from './typescript/cip30';
import { WalletInfo } from './store';
import { walletPrettyNames } from './wallets';

export const fromHex = (hexString: string) => Buffer.from(hexString, 'hex');

export const parseBalance = (balance: string) => {
const decodedBalance = decode(fromHex(balance));
const encoder = new Encoder({ mapsAsObjects: false });
const decodedBalance = encoder.decode(Buffer.from(balance, 'hex'));

if (typeof decodedBalance === 'bigint') {
return decodedBalance.toString();
Expand All @@ -13,3 +17,13 @@ export const parseBalance = (balance: string) => {
}
return (decodedBalance[0] ?? 0).toString();
};

export const toWalletInfo = (walletName: WalletName): WalletInfo => {
const ns = (window as any).cardano;

return {
name: walletName,
displayName: walletPrettyNames[walletName],
icon: ns[walletName].icon,
};
};

0 comments on commit 27705d6

Please sign in to comment.