mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-12 03:54:13 +00:00
01fc5f1a89
* Start splitting networks into their own reducers * Split out nodes and networks into their own reducers * Cleanup file structure * Make selectors for new state * Change custom network typing * re-type repo * Fix up components to use selectors, work on fixing sagas * Provide consistency in naming, fix more sagas * Get non web3 node switching working * Split config rehydration off into a different file for store * Inline auth for custom nodes * Include typing for app state * moar selectors * Get web3 working + cleanup sagas * Cleanup tsc errors * Use forof loop instead of foreach for clearing pruning custom networks * Add reducer tests for new redux state * Export needed variables * Add console error * Remove old comment * Work on saga tests * Get passing existing saga tests * Fix more tests * Remove irrlevant tests * add console error * Get rest of tests passing * Fix merge errors * Remove random text * Fix store saving * Fix selector lib only grabbing from static nodes * Fix custom node removal crashing app * Infer selected network via node * Prune custom networks properly on node removal * Infer network name from chainid from selecting state * Cleanup tsc errors * Remove MEW nodes for main and testnet
82 lines
2.4 KiB
TypeScript
82 lines
2.4 KiB
TypeScript
import { getTransactionFields, makeTransaction } from 'libs/transaction';
|
|
import { IFullWallet } from '../IWallet';
|
|
import { bufferToHex } from 'ethereumjs-util';
|
|
import { configuredStore } from 'store';
|
|
import { getNodeLib, getNetworkNameByChainId } from 'selectors/config';
|
|
import Web3Node, { isWeb3Node } from 'libs/nodes/web3';
|
|
import { INode } from 'libs/nodes/INode';
|
|
|
|
export default class Web3Wallet implements IFullWallet {
|
|
private address: string;
|
|
private network: string;
|
|
|
|
constructor(address: string, network: string) {
|
|
this.address = address;
|
|
this.network = network;
|
|
}
|
|
|
|
public getAddressString(): Promise<string> {
|
|
return Promise.resolve(this.address);
|
|
}
|
|
|
|
public signRawTransaction(): Promise<Buffer> {
|
|
return Promise.reject(new Error('Web3 wallets cannot sign raw transactions.'));
|
|
}
|
|
|
|
public async signMessage(msg: string): Promise<string> {
|
|
const msgHex = bufferToHex(Buffer.from(msg));
|
|
const state = configuredStore.getState();
|
|
const nodeLib: Web3Node | INode = getNodeLib(state);
|
|
|
|
if (!nodeLib) {
|
|
throw new Error('');
|
|
}
|
|
if (!isWeb3Node(nodeLib)) {
|
|
throw new Error('Web3 wallets can only be used with a Web3 node.');
|
|
}
|
|
|
|
return nodeLib.signMessage(msgHex, this.address);
|
|
}
|
|
|
|
public async sendTransaction(serializedTransaction: string): Promise<string> {
|
|
const transactionInstance = makeTransaction(serializedTransaction);
|
|
const { to, value, gasLimit: gas, gasPrice, data, nonce, chainId } = getTransactionFields(
|
|
transactionInstance
|
|
);
|
|
const from = this.address;
|
|
|
|
const web3Tx = {
|
|
from,
|
|
to,
|
|
value,
|
|
gas,
|
|
gasPrice,
|
|
data,
|
|
nonce,
|
|
chainId
|
|
};
|
|
|
|
const state = configuredStore.getState();
|
|
const nodeLib: Web3Node | INode | undefined = getNodeLib(state);
|
|
|
|
if (!isWeb3Node(nodeLib)) {
|
|
throw new Error('Web3 wallets can only be used with a Web3 node.');
|
|
}
|
|
await this.networkCheck(nodeLib);
|
|
|
|
return nodeLib.sendTransaction(web3Tx);
|
|
}
|
|
|
|
private async networkCheck(lib: Web3Node) {
|
|
const netId = await lib.getNetVersion();
|
|
const netName = getNetworkNameByChainId(configuredStore.getState(), netId);
|
|
if (this.network !== netName) {
|
|
throw new Error(
|
|
`Expected MetaMask / Mist network to be ${
|
|
this.network
|
|
}, but got ${netName}. Please change the network or refresh the page.`
|
|
);
|
|
}
|
|
}
|
|
}
|