-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy patheip1193-bridge.ts
166 lines (145 loc) · 6.15 KB
/
eip1193-bridge.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"use strict";
import EventEmitter from "events";
import { ethers } from "ethers";
import { version } from "./_version";
const logger = new ethers.utils.Logger(version);
/*
function getBlockTag(tag) {
if (tag == null) { return "latest"; }
if (tag === "earliest" || tag === "latest" || tag === "pending") {
return tag;
}
return ethers.utils.hexValue(tag)
}
*/
export class _Eip1193Bridge extends EventEmitter {
readonly signer: ethers.Signer;
readonly provider: ethers.providers.Provider;
constructor(signer: ethers.Signer, provider?: ethers.providers.Provider) {
super();
ethers.utils.defineReadOnly(this, "signer", signer);
ethers.utils.defineReadOnly(this, "provider", provider || null);
}
async send(method: string, params?: Array<any>): Promise<any> {
function throwUnsupported(message: string): never {
return logger.throwError("eth_sign requires a signer", ethers.utils.Logger.errors.UNSUPPORTED_OPERATION, {
method: method,
params: params
});
}
let coerce = (value: any) => value;
switch (method) {
case "eth_gasPrice": {
const result = await this.provider.getGasPrice();
return result.toHexString();
}
case "eth_accounts": {
const result = [ ];
if (this.signer) {
const address = await this.signer.getAddress();
result.push(address);
}
return result;
}
case "eth_blockNumber": {
return await this.provider.getBlockNumber();
}
case "eth_chainId": {
const result = await this.provider.getNetwork();
return result.chainId;
}
case "eth_getBalance": {
const result = await this.provider.getBalance(params[0], params[1]);
return result.toHexString();
}
case "eth_getStorageAt": {
return this.provider.getStorageAt(params[0], params[1], params[2]);
}
case "eth_getTransactionCount": {
const result = await this.provider.getTransactionCount(params[0], params[1]);
return ethers.utils.hexValue(result);
}
case "eth_getBlockTransactionCountByHash":
case "eth_getBlockTransactionCountByNumber": {
const result = await this.provider.getBlock(params[0]);
return ethers.utils.hexValue(result.transactions.length);
}
case "eth_getCode": {
const result = await this.provider.getBlock(params[0]);
return result;
}
case "eth_sendRawTransaction": {
return await this.provider.sendTransaction(params[0]);
}
case "eth_call": {
const req = ethers.providers.JsonRpcProvider.hexlifyTransaction(params[0]);
return await this.provider.call(req, params[1]);
}
case "estimateGas": {
if (params[1] && params[1] !== "latest") {
throwUnsupported("estimateGas does not support blockTag");
}
const req = ethers.providers.JsonRpcProvider.hexlifyTransaction(params[0]);
const result = await this.provider.estimateGas(req);
return result.toHexString();
}
// @TOOD: Transform? No uncles?
case "eth_getBlockByHash":
case "eth_getBlockByNumber": {
if (params[1]) {
return await this.provider.getBlockWithTransactions(params[0]);
} else {
return await this.provider.getBlock(params[0]);
}
}
case "eth_getTransactionByHash": {
return await this.provider.getTransaction(params[0]);
}
case "eth_getTransactionReceipt": {
return await this.provider.getTransactionReceipt(params[0]);
}
case "eth_sign": {
if (!this.signer) {
return throwUnsupported("eth_sign requires an account");
}
const address = await this.signer.getAddress();
if (address !== ethers.utils.getAddress(params[0])) {
logger.throwArgumentError("account mismatch or account not found", "params[0]", params[0]);
}
return this.signer.signMessage(ethers.utils.arrayify(params[1]));
}
case "eth_sendTransaction": {
if (!this.signer) {
return throwUnsupported("eth_sign requires an account");
}
const req = ethers.providers.JsonRpcProvider.hexlifyTransaction(params[0]);
const tx = await this.signer.sendTransaction(req);
return tx.hash;
}
case "eth_getUncleCountByBlockHash":
case "eth_getUncleCountByBlockNumber":
{
coerce = ethers.utils.hexValue;
break;
}
case "eth_getTransactionByBlockHashAndIndex":
case "eth_getTransactionByBlockNumberAndIndex":
case "eth_getUncleByBlockHashAndIndex":
case "eth_getUncleByBlockNumberAndIndex":
case "eth_newFilter":
case "eth_newBlockFilter":
case "eth_newPendingTransactionFilter":
case "eth_uninstallFilter":
case "eth_getFilterChanges":
case "eth_getFilterLogs":
case "eth_getLogs":
break;
}
// If our provider supports send, maybe it can do a better job?
if ((<any>(this.provider)).send) {
const result = await (<any>(this.provider)).send(method, params);
return coerce(result);
}
return throwUnsupported(`unsupported method: ${ method }`);
}
}