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