-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMetaMaskKeyring.ts
115 lines (104 loc) · 3.17 KB
/
MetaMaskKeyring.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { BaseKeyring, StoredKeyring } from "@keystonehq/base-eth-keyring";
import { MetamaskInteractionProvider } from "./MetaMaskInteractionProvider";
import {
TransactionFactory,
LegacyTransaction as Transaction,
FeeMarketEIP1559Transaction,
} from "@ethereumjs/tx";
import { DataType, EthSignRequest } from "@keystonehq/bc-ur-registry-eth";
import * as uuid from "uuid";
import { RLP } from "@ethereumjs/rlp";
export class MetaMaskKeyring extends BaseKeyring {
static type = BaseKeyring.type;
static instance: MetaMaskKeyring;
constructor(opts?: StoredKeyring) {
super(opts);
if (MetaMaskKeyring.instance) {
MetaMaskKeyring.instance.deserialize(opts);
return MetaMaskKeyring.instance;
}
MetaMaskKeyring.instance = this;
}
getInteraction = (): MetamaskInteractionProvider => {
return new MetamaskInteractionProvider();
};
resetStore = (): void => {
this.getInteraction().reset();
};
getMemStore = () => {
return this.getInteraction().memStore;
};
async signTransaction(address: string, tx: any): Promise<any> {
const dataType =
tx.type === 0 ? DataType.transaction : DataType.typedTransaction;
let messageToSign;
if (tx.type === 0) {
messageToSign = Buffer.from(
RLP.encode((tx as Transaction).getMessageToSign())
);
} else {
messageToSign = Buffer.from(
(tx as FeeMarketEIP1559Transaction).serialize()
);
}
const hdPath = await this._pathFromAddress(address);
const chainId = tx.common.chainId();
const requestId = uuid.v4();
const ethSignRequest = EthSignRequest.constructETHRequest(
messageToSign,
dataType,
hdPath,
this.xfp,
requestId,
chainId,
address
);
const { r, s, v } = await this.requestSignature(
requestId,
ethSignRequest,
"Scan with your Keystone",
'After your Keystone has signed the transaction, click on "Scan Keystone" to receive the signature'
);
const txJson = tx.toJSON();
txJson.v = v;
txJson.s = s;
txJson.r = r;
txJson.type = tx.type;
const transaction = TransactionFactory.fromTxData(txJson, {
common: tx.common,
});
return transaction;
}
removeAccount = (address) => {
if (
!this.accounts.map((a) => a.toLowerCase()).includes(address.toLowerCase())
) {
throw new Error(`Address ${address} not found in this keyring`);
}
this.accounts = this.accounts.filter(
(a) => a.toLowerCase() !== address.toLowerCase()
);
};
forgetDevice = () => {
//common props
this.page = 0;
this.perPage = 5;
this.accounts = [];
this.currentAccount = 0;
this.name = "QR Hardware";
this.initialized = false;
//hd props;
this.xfp = "";
this.xpub = "";
this.hdPath = "";
this.indexes = {};
this.hdk = undefined;
//pubkey props;
this.paths = {};
};
submitCryptoHDKey = this.getInteraction().submitCryptoHDKey;
submitCryptoAccount = this.getInteraction().submitCryptoAccount;
submitSignature = this.getInteraction().submitSignature;
cancelSync = this.getInteraction().cancelSync;
cancelSignRequest = this.getInteraction().cancelRequestSignature;
}