forked from cerc-io/laconic-wallet
121 lines
2.8 KiB
TypeScript
121 lines
2.8 KiB
TypeScript
// Taken from https://medium.com/walletconnect/how-to-build-a-wallet-in-react-native-with-the-web3wallet-sdk-b6f57bf02f9a
|
|
|
|
import { utils } from 'ethers';
|
|
|
|
import { Account } from '../../types';
|
|
import { EIP155_CHAINS, TEIP155Chain } from './EIP155Lib';
|
|
|
|
/**
|
|
* Truncates string (in the middle) via given lenght value
|
|
*/
|
|
export function truncate(value: string, length: number) {
|
|
if (value?.length <= length) {
|
|
return value;
|
|
}
|
|
|
|
const separator = '...';
|
|
const stringLength = length - separator.length;
|
|
const frontLength = Math.ceil(stringLength / 2);
|
|
const backLength = Math.floor(stringLength / 2);
|
|
|
|
return (
|
|
value.substring(0, frontLength) +
|
|
separator +
|
|
value.substring(value.length - backLength)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Converts hex to utf8 string if it is valid bytes
|
|
*/
|
|
export function convertHexToUtf8(value: string) {
|
|
if (utils.isHexString(value)) {
|
|
return utils.toUtf8String(value);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* Gets message from various signing request methods by filtering out
|
|
* a value that is not an address (thus is a message).
|
|
* If it is a hex string, it gets converted to utf8 string
|
|
*/
|
|
export function getSignParamsMessage(params: string[]) {
|
|
const message = params.filter(p => !utils.isAddress(p))[0];
|
|
|
|
return convertHexToUtf8(message);
|
|
}
|
|
|
|
/**
|
|
* Gets data from various signTypedData request methods by filtering out
|
|
* a value that is not an address (thus is data).
|
|
* If data is a string convert it to object
|
|
*/
|
|
export function getSignTypedDataParamsData(params: string[]) {
|
|
const data = params.filter(p => !utils.isAddress(p))[0];
|
|
|
|
if (typeof data === 'string') {
|
|
return JSON.parse(data);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
/**
|
|
* Get our address from params checking if params string contains one
|
|
* of our wallet addresses
|
|
*/
|
|
export async function getAccountNumberFromParams(
|
|
addresses: string[],
|
|
ethAccounts: Account[],
|
|
params: any,
|
|
) {
|
|
const paramsString = JSON.stringify(params);
|
|
let address = '';
|
|
|
|
addresses.forEach(addr => {
|
|
if (paramsString.includes(addr)) {
|
|
address = addr;
|
|
}
|
|
});
|
|
|
|
const currentAccount = ethAccounts!.find(
|
|
account => account.address === address,
|
|
);
|
|
|
|
if (!currentAccount) {
|
|
throw new Error('Account with given adress not found');
|
|
}
|
|
|
|
return currentAccount.counterId;
|
|
}
|
|
|
|
/**
|
|
* Check if chain is part of EIP155 standard
|
|
*/
|
|
export function isEIP155Chain(chain: string) {
|
|
return chain.includes('eip155');
|
|
}
|
|
|
|
/**
|
|
* Check if chain is part of COSMOS standard
|
|
*/
|
|
export function isCosmosChain(chain: string) {
|
|
return chain.includes('cosmos');
|
|
}
|
|
|
|
/**
|
|
* Check if chain is part of SOLANA standard
|
|
*/
|
|
export function isSolanaChain(chain: string) {
|
|
return chain.includes('solana');
|
|
}
|
|
|
|
/**
|
|
* Formats chainId to its name
|
|
*/
|
|
export function formatChainName(chainId: string) {
|
|
return EIP155_CHAINS[chainId as TEIP155Chain]?.name ?? chainId;
|
|
}
|