Skip to content

Commit

Permalink
Fix account format
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitayutanov committed Aug 30, 2024
1 parent 4641bcc commit 9fa5f9c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/config/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const NetworkList: Networks = {
},
unit: 'VARA',
units: 12,
ss58: 0, // active account is not getting connected with a custom value
ss58: 137, // active account should be formatted manually on initial connect
brand: {
icon: VaraIconSVG,
token: VaraTokenSVG,
Expand Down
20 changes: 17 additions & 3 deletions src/contexts/ActiveAccounts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import type { ReactNode } from 'react';
import { createContext, useContext, useRef, useState } from 'react';
import type { MaybeAddress } from 'types';
import Keyring from '@polkadot/keyring';
import { setStateWithRef } from '@w3ux/utils';
import { NetworkList } from 'config/networks';
import { useNetwork } from 'contexts/Network';
import type { ActiveAccountsContextInterface, ActiveProxy } from './types';
import { defaultActiveAccountsContext } from './defaults';
Expand Down Expand Up @@ -44,20 +46,32 @@ export const ActiveAccountsProvider = ({
setStateWithRef(newActiveProxy, setActiveProxyState, activeProxyRef);
};

const getVaraAddress = (value: string) => {
const keyring = new Keyring();

keyring.setSS58Format(NetworkList.vara.ss58);

return keyring.addFromAddress(value).address;
};

// Setter for the active account.
const setActiveAccount = (
newActiveAccount: MaybeAddress,
updateLocalStorage = true
) => {
const account = newActiveAccount
? getVaraAddress(newActiveAccount)
: newActiveAccount;

if (updateLocalStorage) {
if (newActiveAccount === null) {
if (account === null) {
localStorage.removeItem(`${network}_active_account`);
} else {
localStorage.setItem(`${network}_active_account`, newActiveAccount);
localStorage.setItem(`${network}_active_account`, account);
}
}

setStateWithRef(newActiveAccount, setActiveAccountState, activeAccountRef);
setStateWithRef(account, setActiveAccountState, activeAccountRef);
};

// Getter for the active account.
Expand Down
2 changes: 1 addition & 1 deletion src/contexts/Api/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const defaultChainState: APIChainState = {
version: {
specVersion: 0,
},
ss58Prefix: 0,
ss58Prefix: 137,
};

export const defaultConsts: APIConstants = {
Expand Down

0 comments on commit 9fa5f9c

Please sign in to comment.