-
Notifications
You must be signed in to change notification settings - Fork 2
/
proxy.ts
47 lines (40 loc) · 1.23 KB
/
proxy.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
import ethers from "ethers";
import { RuleEngine } from "./engine";
const prompts = require('prompts');
export class Proxy {
txParams: any[] | undefined;
provider: ethers.providers.JsonRpcProvider;
ruleEngine: RuleEngine;
constructor(provider: ethers.providers.JsonRpcProvider, ruleEngine: RuleEngine) {
this.provider = provider;
this.ruleEngine = ruleEngine;
}
async rpcHandler(method: string, params: any): Promise<any> {
switch(method) {
case 'eth_sendRawTransaction':
return this.transactionHandler(params as any[]);
default:
return this.provider.send(method, params);
}
}
private async transactionHandler(params: any[]) {
const tx = ethers.utils.parseTransaction(params[0]);
// Run all rules
await this.ruleEngine.run(tx);
if (!await this.showPrompt()) {
console.log("Rejecting...")
return;
}
console.log("Submitting...")
return this.provider.send('eth_sendRawTransaction', params);
}
private async showPrompt() {
console.log("Do you want to submit the transaction?");
const response = await prompts({
type: 'confirm',
name: 'value',
message: 'Do you want to submit the transaction?',
});
return response.value;
}
}