-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
json-rpc-provider.ts
742 lines (614 loc) · 27.5 KB
/
json-rpc-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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
"use strict";
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC
import { Provider, TransactionRequest, TransactionResponse } from "@ethersproject/abstract-provider";
import { Signer, TypedDataDomain, TypedDataField, TypedDataSigner } from "@ethersproject/abstract-signer";
import { BigNumber } from "@ethersproject/bignumber";
import { Bytes, hexlify, hexValue, hexZeroPad, isHexString } from "@ethersproject/bytes";
import { _TypedDataEncoder } from "@ethersproject/hash";
import { Network, Networkish } from "@ethersproject/networks";
import { checkProperties, deepCopy, Deferrable, defineReadOnly, getStatic, resolveProperties, shallowCopy } from "@ethersproject/properties";
import { toUtf8Bytes } from "@ethersproject/strings";
import { AccessList, accessListify } from "@ethersproject/transactions";
import { ConnectionInfo, fetchJson, poll } from "@ethersproject/web";
import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);
import { BaseProvider, Event } from "./base-provider";
const errorGas = [ "call", "estimateGas" ];
function spelunk(value: any, requireData: boolean): null | { message: string, data: null | string } {
if (value == null) { return null; }
// These *are* the droids we're looking for.
if (typeof(value.message) === "string" && value.message.match("reverted")) {
const data = isHexString(value.data) ? value.data: null;
if (!requireData || data) {
return { message: value.message, data };
}
}
// Spelunk further...
if (typeof(value) === "object") {
for (const key in value) {
const result = spelunk(value[key], requireData);
if (result) { return result; }
}
return null;
}
// Might be a JSON string we can further descend...
if (typeof(value) === "string") {
try {
return spelunk(JSON.parse(value), requireData);
} catch (error) { }
}
return null;
}
function checkError(method: string, error: any, params: any): any {
const transaction = params.transaction || params.signedTransaction;
// Undo the "convenience" some nodes are attempting to prevent backwards
// incompatibility; maybe for v6 consider forwarding reverts as errors
if (method === "call") {
const result = spelunk(error, true);
if (result) { return result.data; }
// Nothing descriptive..
logger.throwError("missing revert data in call exception; Transaction reverted without a reason string", Logger.errors.CALL_EXCEPTION, {
data: "0x", transaction, error
});
}
if (method === "estimateGas") {
// Try to find something, with a preference on SERVER_ERROR body
let result = spelunk(error.body, false);
if (result == null) { result = spelunk(error, false); }
// Found "reverted", this is a CALL_EXCEPTION
if (result) {
logger.throwError("cannot estimate gas; transaction may fail or may require manual gas limit", Logger.errors.UNPREDICTABLE_GAS_LIMIT, {
reason: result.message, method, transaction, error
});
}
}
// @TODO: Should we spelunk for message too?
let message = error.message;
if (error.code === Logger.errors.SERVER_ERROR && error.error && typeof(error.error.message) === "string") {
message = error.error.message;
} else if (typeof(error.body) === "string") {
message = error.body;
} else if (typeof(error.responseText) === "string") {
message = error.responseText;
}
message = (message || "").toLowerCase();
// "insufficient funds for gas * price + value + cost(data)"
if (message.match(/insufficient funds|base fee exceeds gas limit|InsufficientFunds/i)) {
logger.throwError("insufficient funds for intrinsic transaction cost", Logger.errors.INSUFFICIENT_FUNDS, {
error, method, transaction
});
}
// "nonce too low"
if (message.match(/nonce (is )?too low/i)) {
logger.throwError("nonce has already been used", Logger.errors.NONCE_EXPIRED, {
error, method, transaction
});
}
// "replacement transaction underpriced"
if (message.match(/replacement transaction underpriced|transaction gas price.*too low/i)) {
logger.throwError("replacement fee too low", Logger.errors.REPLACEMENT_UNDERPRICED, {
error, method, transaction
});
}
// "replacement transaction underpriced"
if (message.match(/only replay-protected/i)) {
logger.throwError("legacy pre-eip-155 transactions not supported", Logger.errors.UNSUPPORTED_OPERATION, {
error, method, transaction
});
}
if (errorGas.indexOf(method) >= 0 && message.match(/gas required exceeds allowance|always failing transaction|execution reverted|revert/)) {
logger.throwError("cannot estimate gas; transaction may fail or may require manual gas limit", Logger.errors.UNPREDICTABLE_GAS_LIMIT, {
error, method, transaction
});
}
throw error;
}
function timer(timeout: number): Promise<any> {
return new Promise(function(resolve) {
setTimeout(resolve, timeout);
});
}
function getResult(payload: { error?: { code?: number, data?: any, message?: string }, result?: any }): any {
if (payload.error) {
// @TODO: not any
const error: any = new Error(payload.error.message);
error.code = payload.error.code;
error.data = payload.error.data;
throw error;
}
return payload.result;
}
function getLowerCase(value: string): string {
if (value) { return value.toLowerCase(); }
return value;
}
const _constructorGuard = {};
export class JsonRpcSigner extends Signer implements TypedDataSigner {
readonly provider: JsonRpcProvider;
_index: number;
_address: string;
constructor(constructorGuard: any, provider: JsonRpcProvider, addressOrIndex?: string | number) {
super();
if (constructorGuard !== _constructorGuard) {
throw new Error("do not call the JsonRpcSigner constructor directly; use provider.getSigner");
}
defineReadOnly(this, "provider", provider);
if (addressOrIndex == null) { addressOrIndex = 0; }
if (typeof(addressOrIndex) === "string") {
defineReadOnly(this, "_address", this.provider.formatter.address(addressOrIndex));
defineReadOnly(this, "_index", null);
} else if (typeof(addressOrIndex) === "number") {
defineReadOnly(this, "_index", addressOrIndex);
defineReadOnly(this, "_address", null);
} else {
logger.throwArgumentError("invalid address or index", "addressOrIndex", addressOrIndex);
}
}
connect(provider: Provider): JsonRpcSigner {
return logger.throwError("cannot alter JSON-RPC Signer connection", Logger.errors.UNSUPPORTED_OPERATION, {
operation: "connect"
});
}
connectUnchecked(): JsonRpcSigner {
return new UncheckedJsonRpcSigner(_constructorGuard, this.provider, this._address || this._index);
}
getAddress(): Promise<string> {
if (this._address) {
return Promise.resolve(this._address);
}
return this.provider.send("eth_accounts", []).then((accounts) => {
if (accounts.length <= this._index) {
logger.throwError("unknown account #" + this._index, Logger.errors.UNSUPPORTED_OPERATION, {
operation: "getAddress"
});
}
return this.provider.formatter.address(accounts[this._index])
});
}
sendUncheckedTransaction(transaction: Deferrable<TransactionRequest>): Promise<string> {
transaction = shallowCopy(transaction);
const fromAddress = this.getAddress().then((address) => {
if (address) { address = address.toLowerCase(); }
return address;
});
// The JSON-RPC for eth_sendTransaction uses 90000 gas; if the user
// wishes to use this, it is easy to specify explicitly, otherwise
// we look it up for them.
if (transaction.gasLimit == null) {
const estimate = shallowCopy(transaction);
estimate.from = fromAddress;
transaction.gasLimit = this.provider.estimateGas(estimate);
}
if (transaction.to != null) {
transaction.to = Promise.resolve(transaction.to).then(async (to) => {
if (to == null) { return null; }
const address = await this.provider.resolveName(to);
if (address == null) {
logger.throwArgumentError("provided ENS name resolves to null", "tx.to", to);
}
return address;
});
}
return resolveProperties({
tx: resolveProperties(transaction),
sender: fromAddress
}).then(({ tx, sender }) => {
if (tx.from != null) {
if (tx.from.toLowerCase() !== sender) {
logger.throwArgumentError("from address mismatch", "transaction", transaction);
}
} else {
tx.from = sender;
}
const hexTx = (<any>this.provider.constructor).hexlifyTransaction(tx, { from: true });
return this.provider.send("eth_sendTransaction", [ hexTx ]).then((hash) => {
return hash;
}, (error) => {
if (typeof(error.message) === "string" && error.message.match(/user denied/i)) {
logger.throwError("user rejected transaction", Logger.errors.ACTION_REJECTED, {
action: "sendTransaction",
transaction: tx
});
}
return checkError("sendTransaction", error, hexTx);
});
});
}
signTransaction(transaction: Deferrable<TransactionRequest>): Promise<string> {
return logger.throwError("signing transactions is unsupported", Logger.errors.UNSUPPORTED_OPERATION, {
operation: "signTransaction"
});
}
async sendTransaction(transaction: Deferrable<TransactionRequest>): Promise<TransactionResponse> {
// This cannot be mined any earlier than any recent block
const blockNumber = await this.provider._getInternalBlockNumber(100 + 2 * this.provider.pollingInterval);
// Send the transaction
const hash = await this.sendUncheckedTransaction(transaction);
try {
// Unfortunately, JSON-RPC only provides and opaque transaction hash
// for a response, and we need the actual transaction, so we poll
// for it; it should show up very quickly
return await poll(async () => {
const tx = await this.provider.getTransaction(hash);
if (tx === null) { return undefined; }
return this.provider._wrapTransaction(tx, hash, blockNumber);
}, { oncePoll: this.provider });
} catch (error) {
(<any>error).transactionHash = hash;
throw error;
}
}
async signMessage(message: Bytes | string): Promise<string> {
const data = ((typeof(message) === "string") ? toUtf8Bytes(message): message);
const address = await this.getAddress();
try {
return await this.provider.send("personal_sign", [ hexlify(data), address.toLowerCase() ]);
} catch (error) {
if (typeof(error.message) === "string" && error.message.match(/user denied/i)) {
logger.throwError("user rejected signing", Logger.errors.ACTION_REJECTED, {
action: "signMessage",
from: address,
messageData: message
});
}
throw error;
}
}
async _legacySignMessage(message: Bytes | string): Promise<string> {
const data = ((typeof(message) === "string") ? toUtf8Bytes(message): message);
const address = await this.getAddress();
try {
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign
return await this.provider.send("eth_sign", [ address.toLowerCase(), hexlify(data) ]);
} catch (error) {
if (typeof(error.message) === "string" && error.message.match(/user denied/i)) {
logger.throwError("user rejected signing", Logger.errors.ACTION_REJECTED, {
action: "_legacySignMessage",
from: address,
messageData: message
});
}
throw error;
}
}
async _signTypedData(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, value: Record<string, any>): Promise<string> {
// Populate any ENS names (in-place)
const populated = await _TypedDataEncoder.resolveNames(domain, types, value, (name: string) => {
return this.provider.resolveName(name);
});
const address = await this.getAddress();
try {
return await this.provider.send("eth_signTypedData_v4", [
address.toLowerCase(),
JSON.stringify(_TypedDataEncoder.getPayload(populated.domain, types, populated.value))
]);
} catch (error) {
if (typeof(error.message) === "string" && error.message.match(/user denied/i)) {
logger.throwError("user rejected signing", Logger.errors.ACTION_REJECTED, {
action: "_signTypedData",
from: address,
messageData: { domain: populated.domain, types, value: populated.value }
});
}
throw error;
}
}
async unlock(password: string): Promise<boolean> {
const provider = this.provider;
const address = await this.getAddress();
return provider.send("personal_unlockAccount", [ address.toLowerCase(), password, null ]);
}
}
class UncheckedJsonRpcSigner extends JsonRpcSigner {
sendTransaction(transaction: Deferrable<TransactionRequest>): Promise<TransactionResponse> {
return this.sendUncheckedTransaction(transaction).then((hash) => {
return <TransactionResponse>{
hash: hash,
nonce: null,
gasLimit: null,
gasPrice: null,
data: null,
value: null,
chainId: null,
confirmations: 0,
from: null,
wait: (confirmations?: number) => { return this.provider.waitForTransaction(hash, confirmations); }
};
});
}
}
const allowedTransactionKeys: { [ key: string ]: boolean } = {
chainId: true, data: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true,
type: true, accessList: true,
maxFeePerGas: true, maxPriorityFeePerGas: true
}
export class JsonRpcProvider extends BaseProvider {
readonly connection: ConnectionInfo;
_pendingFilter: Promise<number>;
_nextId: number;
// During any given event loop, the results for a given call will
// all be the same, so we can dedup the calls to save requests and
// bandwidth. @TODO: Try out generalizing this against send?
_eventLoopCache: Record<string, Promise<any>>;
get _cache(): Record<string, Promise<any>> {
if (this._eventLoopCache == null) {
this._eventLoopCache = { };
}
return this._eventLoopCache;
}
constructor(url?: ConnectionInfo | string, network?: Networkish) {
let networkOrReady: Networkish | Promise<Network> = network;
// The network is unknown, query the JSON-RPC for it
if (networkOrReady == null) {
networkOrReady = new Promise((resolve, reject) => {
setTimeout(() => {
this.detectNetwork().then((network) => {
resolve(network);
}, (error) => {
reject(error);
});
}, 0);
});
}
super(networkOrReady);
// Default URL
if (!url) { url = getStatic<() => string>(this.constructor, "defaultUrl")(); }
if (typeof(url) === "string") {
defineReadOnly(this, "connection",Object.freeze({
url: url
}));
} else {
defineReadOnly(this, "connection", Object.freeze(shallowCopy(url)));
}
this._nextId = 42;
}
static defaultUrl(): string {
return "http:/\/localhost:8545";
}
detectNetwork(): Promise<Network> {
if (!this._cache["detectNetwork"]) {
this._cache["detectNetwork"] = this._uncachedDetectNetwork();
// Clear this cache at the beginning of the next event loop
setTimeout(() => {
this._cache["detectNetwork"] = null;
}, 0);
}
return this._cache["detectNetwork"];
}
async _uncachedDetectNetwork(): Promise<Network> {
await timer(0);
let chainId = null;
try {
chainId = await this.send("eth_chainId", [ ]);
} catch (error) {
try {
chainId = await this.send("net_version", [ ]);
} catch (error) { }
}
if (chainId != null) {
const getNetwork = getStatic<(network: Networkish) => Network>(this.constructor, "getNetwork");
try {
return getNetwork(BigNumber.from(chainId).toNumber());
} catch (error) {
return logger.throwError("could not detect network", Logger.errors.NETWORK_ERROR, {
chainId: chainId,
event: "invalidNetwork",
serverError: error
});
}
}
return logger.throwError("could not detect network", Logger.errors.NETWORK_ERROR, {
event: "noNetwork"
});
}
getSigner(addressOrIndex?: string | number): JsonRpcSigner {
return new JsonRpcSigner(_constructorGuard, this, addressOrIndex);
}
getUncheckedSigner(addressOrIndex?: string | number): UncheckedJsonRpcSigner {
return this.getSigner(addressOrIndex).connectUnchecked();
}
listAccounts(): Promise<Array<string>> {
return this.send("eth_accounts", []).then((accounts: Array<string>) => {
return accounts.map((a) => this.formatter.address(a));
});
}
send(method: string, params: Array<any>): Promise<any> {
const request = {
method: method,
params: params,
id: (this._nextId++),
jsonrpc: "2.0"
};
this.emit("debug", {
action: "request",
request: deepCopy(request),
provider: this
});
// We can expand this in the future to any call, but for now these
// are the biggest wins and do not require any serializing parameters.
const cache = ([ "eth_chainId", "eth_blockNumber" ].indexOf(method) >= 0);
if (cache && this._cache[method]) {
return this._cache[method];
}
const result = fetchJson(this.connection, JSON.stringify(request), getResult).then((result) => {
this.emit("debug", {
action: "response",
request: request,
response: result,
provider: this
});
return result;
}, (error) => {
this.emit("debug", {
action: "response",
error: error,
request: request,
provider: this
});
throw error;
});
// Cache the fetch, but clear it on the next event loop
if (cache) {
this._cache[method] = result;
setTimeout(() => {
this._cache[method] = null;
}, 0);
}
return result;
}
prepareRequest(method: string, params: any): [ string, Array<any> ] {
switch (method) {
case "getBlockNumber":
return [ "eth_blockNumber", [] ];
case "getGasPrice":
return [ "eth_gasPrice", [] ];
case "getBalance":
return [ "eth_getBalance", [ getLowerCase(params.address), params.blockTag ] ];
case "getTransactionCount":
return [ "eth_getTransactionCount", [ getLowerCase(params.address), params.blockTag ] ];
case "getCode":
return [ "eth_getCode", [ getLowerCase(params.address), params.blockTag ] ];
case "getStorageAt":
return [ "eth_getStorageAt", [ getLowerCase(params.address), hexZeroPad(params.position, 32), params.blockTag ] ];
case "sendTransaction":
return [ "eth_sendRawTransaction", [ params.signedTransaction ] ]
case "getBlock":
if (params.blockTag) {
return [ "eth_getBlockByNumber", [ params.blockTag, !!params.includeTransactions ] ];
} else if (params.blockHash) {
return [ "eth_getBlockByHash", [ params.blockHash, !!params.includeTransactions ] ];
}
return null;
case "getTransaction":
return [ "eth_getTransactionByHash", [ params.transactionHash ] ];
case "getTransactionReceipt":
return [ "eth_getTransactionReceipt", [ params.transactionHash ] ];
case "call": {
const hexlifyTransaction = getStatic<(t: TransactionRequest, a?: { [key: string]: boolean }) => { [key: string]: string }>(this.constructor, "hexlifyTransaction");
return [ "eth_call", [ hexlifyTransaction(params.transaction, { from: true }), params.blockTag ] ];
}
case "estimateGas": {
const hexlifyTransaction = getStatic<(t: TransactionRequest, a?: { [key: string]: boolean }) => { [key: string]: string }>(this.constructor, "hexlifyTransaction");
return [ "eth_estimateGas", [ hexlifyTransaction(params.transaction, { from: true }) ] ];
}
case "getLogs":
if (params.filter && params.filter.address != null) {
params.filter.address = getLowerCase(params.filter.address);
}
return [ "eth_getLogs", [ params.filter ] ];
default:
break;
}
return null;
}
async perform(method: string, params: any): Promise<any> {
// Legacy networks do not like the type field being passed along (which
// is fair), so we delete type if it is 0 and a non-EIP-1559 network
if (method === "call" || method === "estimateGas") {
const tx = params.transaction;
if (tx && tx.type != null && BigNumber.from(tx.type).isZero()) {
// If there are no EIP-1559 properties, it might be non-EIP-1559
if (tx.maxFeePerGas == null && tx.maxPriorityFeePerGas == null) {
const feeData = await this.getFeeData();
if (feeData.maxFeePerGas == null && feeData.maxPriorityFeePerGas == null) {
// Network doesn't know about EIP-1559 (and hence type)
params = shallowCopy(params);
params.transaction = shallowCopy(tx);
delete params.transaction.type;
}
}
}
}
const args = this.prepareRequest(method, params);
if (args == null) {
logger.throwError(method + " not implemented", Logger.errors.NOT_IMPLEMENTED, { operation: method });
}
try {
return await this.send(args[0], args[1])
} catch (error) {
return checkError(method, error, params);
}
}
_startEvent(event: Event): void {
if (event.tag === "pending") { this._startPending(); }
super._startEvent(event);
}
_startPending(): void {
if (this._pendingFilter != null) { return; }
const self = this;
const pendingFilter: Promise<number> = this.send("eth_newPendingTransactionFilter", []);
this._pendingFilter = pendingFilter;
pendingFilter.then(function(filterId) {
function poll() {
self.send("eth_getFilterChanges", [ filterId ]).then(function(hashes: Array<string>) {
if (self._pendingFilter != pendingFilter) { return null; }
let seq = Promise.resolve();
hashes.forEach(function(hash) {
// @TODO: This should be garbage collected at some point... How? When?
self._emitted["t:" + hash.toLowerCase()] = "pending";
seq = seq.then(function() {
return self.getTransaction(hash).then(function(tx) {
self.emit("pending", tx);
return null;
});
});
});
return seq.then(function() {
return timer(1000);
});
}).then(function() {
if (self._pendingFilter != pendingFilter) {
self.send("eth_uninstallFilter", [ filterId ]);
return;
}
setTimeout(function() { poll(); }, 0);
return null;
}).catch((error: Error) => { });
}
poll();
return filterId;
}).catch((error: Error) => { });
}
_stopEvent(event: Event): void {
if (event.tag === "pending" && this.listenerCount("pending") === 0) {
this._pendingFilter = null;
}
super._stopEvent(event);
}
// Convert an ethers.js transaction into a JSON-RPC transaction
// - gasLimit => gas
// - All values hexlified
// - All numeric values zero-striped
// - All addresses are lowercased
// NOTE: This allows a TransactionRequest, but all values should be resolved
// before this is called
// @TODO: This will likely be removed in future versions and prepareRequest
// will be the preferred method for this.
static hexlifyTransaction(transaction: TransactionRequest, allowExtra?: { [key: string]: boolean }): { [key: string]: string | AccessList } {
// Check only allowed properties are given
const allowed = shallowCopy(allowedTransactionKeys);
if (allowExtra) {
for (const key in allowExtra) {
if (allowExtra[key]) { allowed[key] = true; }
}
}
checkProperties(transaction, allowed);
const result: { [key: string]: string | AccessList } = {};
// JSON-RPC now requires numeric values to be "quantity" values
["chainId", "gasLimit", "gasPrice", "type", "maxFeePerGas", "maxPriorityFeePerGas", "nonce", "value"].forEach(function(key) {
if ((<any>transaction)[key] == null) { return; }
const value = hexValue(BigNumber.from((<any>transaction)[key]));
if (key === "gasLimit") { key = "gas"; }
result[key] = value;
});
["from", "to", "data"].forEach(function(key) {
if ((<any>transaction)[key] == null) { return; }
result[key] = hexlify((<any>transaction)[key]);
});
if ((<any>transaction).accessList) {
result["accessList"] = accessListify((<any>transaction).accessList);
}
return result;
}
}