-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathweb3-provider.ts
169 lines (135 loc) · 5.18 KB
/
web3-provider.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
167
168
169
"use strict";
import { Networkish } from "@ethersproject/networks";
import { deepCopy, defineReadOnly } from "@ethersproject/properties";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
import { JsonRpcProvider } from "./json-rpc-provider";
// Exported Types
export type ExternalProvider = {
isMetaMask?: boolean;
isStatus?: boolean;
host?: string;
path?: string;
sendAsync?: (request: { method: string, params?: Array<any> }, callback: (error: any, response: any) => void) => void
send?: (request: { method: string, params?: Array<any> }, callback: (error: any, response: any) => void) => void
request?: (request: { method: string, params?: Array<any> }) => Promise<any>
}
let _nextId = 1;
export type JsonRpcFetchFunc = (method: string, params?: Array<any>) => Promise<any>;
type Web3LegacySend = (request: any, callback: (error: Error, response: any) => void) => void;
function buildWeb3LegacyFetcher(provider: ExternalProvider, sendFunc: Web3LegacySend) : JsonRpcFetchFunc {
const fetcher = "Web3LegacyFetcher";
return function(method: string, params: Array<any>): Promise<any> {
const request = {
method: method,
params: params,
id: (_nextId++),
jsonrpc: "2.0"
};
return new Promise((resolve, reject) => {
this.emit("debug", {
action: "request",
fetcher,
request: deepCopy(request),
provider: this
});
sendFunc(request, (error, response) => {
if (error) {
this.emit("debug", {
action: "response",
fetcher,
error,
request,
provider: this
});
return reject(error);
}
this.emit("debug", {
action: "response",
fetcher,
request,
response,
provider: this
});
if (response.error) {
const error = new Error(response.error.message);
(<any>error).code = response.error.code;
(<any>error).data = response.error.data;
return reject(error);
}
resolve(response.result);
});
});
}
}
function buildEip1193Fetcher(provider: ExternalProvider): JsonRpcFetchFunc {
return function(method: string, params: Array<any>): Promise<any> {
if (params == null) { params = [ ]; }
const request = { method, params };
this.emit("debug", {
action: "request",
fetcher: "Eip1193Fetcher",
request: deepCopy(request),
provider: this
});
return provider.request(request).then((response) => {
this.emit("debug", {
action: "response",
fetcher: "Eip1193Fetcher",
request,
response,
provider: this
});
return response;
}, (error) => {
this.emit("debug", {
action: "response",
fetcher: "Eip1193Fetcher",
request,
error,
provider: this
});
throw error;
});
}
}
export class Web3Provider extends JsonRpcProvider {
readonly provider: ExternalProvider;
readonly jsonRpcFetchFunc: JsonRpcFetchFunc;
constructor(provider: ExternalProvider | JsonRpcFetchFunc, network?: Networkish) {
if (provider == null) {
logger.throwArgumentError("missing provider", "provider", provider);
}
let path: string = null;
let jsonRpcFetchFunc: JsonRpcFetchFunc = null;
let subprovider: ExternalProvider = null;
if (typeof(provider) === "function") {
path = "unknown:";
jsonRpcFetchFunc = provider;
} else {
path = provider.host || provider.path || "";
if (!path && provider.isMetaMask) {
path = "metamask";
}
subprovider = provider;
if (provider.request) {
if (path === "") { path = "eip-1193:"; }
jsonRpcFetchFunc = buildEip1193Fetcher(provider);
} else if (provider.sendAsync) {
jsonRpcFetchFunc = buildWeb3LegacyFetcher(provider, provider.sendAsync.bind(provider));
} else if (provider.send) {
jsonRpcFetchFunc = buildWeb3LegacyFetcher(provider, provider.send.bind(provider));
} else {
logger.throwArgumentError("unsupported provider", "provider", provider);
}
if (!path) { path = "unknown:"; }
}
super(path, network);
defineReadOnly(this, "jsonRpcFetchFunc", jsonRpcFetchFunc);
defineReadOnly(this, "provider", subprovider);
}
send(method: string, params: Array<any>): Promise<any> {
return this.jsonRpcFetchFunc(method, params);
}
}