on 91efb05 help sign in
bridge/src/lib/metamask.ts
49 lines 1.6 kB history raw
1import Web3 from 'web3';
2import { toHex } from 'web3-utils';
3import { FakeSignableTx, FakeSignResult } from './txn';
5export function MetamaskWallet(address) {
6 this.address = address;
7}
9// Cheat a bit and pretend to sign transaction
10// Sign it actually when sending
11export const metamaskSignTransaction = async (
12 txn: FakeSignableTx,
13 from: string
14) => {
15 txn.from = from;
16 return FakeSignResult(txn, metamaskSendTransaction);
17};
19const metamaskSendTransaction = async (txn: FakeSignableTx, web3: Web3) => {
20 const metamaskFormattedTxn = {
21 data: txn.data,
22 gasLimit: txn.gasLimit,
23 gasPrice: txn.gasPrice,
24 nonce: txn.nonce,
25 to: toHex(txn.to),
26 from: toHex(txn.from),
27 };
29 let txHash;
30 if (window.ethereum) {
31 //NOTE see also #596, #630, #646
32 // no idea what's going on, we should figure it out,
33 // but we apply this bandaid to hopefully stop the bleeding.
34 // at least it also helps with the NOTE of the other branch.
35 txHash = await window.ethereum.request({
36 method: 'eth_sendTransaction',
37 params: [metamaskFormattedTxn],
38 from: txn.from,
39 });
40 } else {
41 //NOTE since this gives us a receipt instead of just the hash of the
42 // signed tx, it makes us wait for confirmation outside of our own
43 // waitForTransactionConfirm. because of this the progress bar loses
44 // its progressiveness for metamask users.
45 let receipt = await await web3.eth.sendTransaction(metamaskFormattedTxn);
46 txHash = receipt.transactionHash;
47 }
48 return txHash;
49};