mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-02-21 23:38:31 +00:00
* Poll for changes to MetaMask's network and reset the wallet if they don't match local * Add tests for * Add a test suite for handleMetaMaskPolling * Add a userful comment and remove an unnecessary jest.mock * Remove superfluous comment * Change logout to a window reload * Adjust verbiage of test case
89 lines
2.4 KiB
TypeScript
89 lines
2.4 KiB
TypeScript
import { bufferToHex } from 'ethereumjs-util';
|
||
|
||
import { getTransactionFields, makeTransaction } from 'libs/transaction';
|
||
import Web3Node from 'libs/nodes/web3';
|
||
import { INode } from 'libs/nodes/INode';
|
||
import { IFullWallet } from '../IWallet';
|
||
|
||
export default class Web3Wallet implements IFullWallet {
|
||
public network: string;
|
||
|
||
private address: string;
|
||
|
||
constructor(address: string, network: string) {
|
||
this.address = address;
|
||
this.network = network;
|
||
}
|
||
|
||
public getAddressString(): string {
|
||
return this.address;
|
||
}
|
||
|
||
public signRawTransaction(): Promise<Buffer> {
|
||
return Promise.reject(new Error('Web3 wallets cannot sign raw transactions.'));
|
||
}
|
||
|
||
public async signMessage(msg: string, nodeLib: Web3Node | INode): Promise<string> {
|
||
const msgHex = bufferToHex(Buffer.from(msg));
|
||
|
||
if (!nodeLib) {
|
||
throw new Error('');
|
||
}
|
||
/*
|
||
if (!isWeb3Node(nodeLib)) {
|
||
throw new Error('Web3 wallets can only be used with a Web3 node.');
|
||
}*/
|
||
|
||
return (nodeLib as Web3Node).signMessage(msgHex, this.address);
|
||
}
|
||
|
||
public async sendTransaction(
|
||
serializedTransaction: string,
|
||
nodeLib: Web3Node,
|
||
networkConfig: any
|
||
): 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
|
||
};
|
||
|
||
if (!nodeLib) {
|
||
throw new Error('');
|
||
}
|
||
|
||
/*
|
||
if (!isWeb3Node(nodeLib)) {
|
||
throw new Error('Web3 wallets can only be used with a Web3 node.');
|
||
}*/
|
||
await this.networkCheck(nodeLib, networkConfig);
|
||
|
||
return nodeLib.sendTransaction(web3Tx);
|
||
}
|
||
|
||
private async networkCheck(lib: Web3Node, networkConfig: any) {
|
||
const netId = await lib.getNetVersion();
|
||
// const networkConfig = getNetworkByChainId(configuredStore.getState(), netId);
|
||
|
||
if (!networkConfig) {
|
||
throw new Error(`MyCrypto doesn’t support the network with chain ID '${netId}'`);
|
||
} else if (this.network !== networkConfig.id) {
|
||
throw new Error(
|
||
`Expected MetaMask / Mist network to be ${this.network}, but got ${
|
||
networkConfig.id
|
||
}. Please change the network or refresh the page.`
|
||
);
|
||
}
|
||
}
|
||
}
|