2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC
|
|
|
|
|
2018-08-03 03:30:44 +03:00
|
|
|
import { BaseProvider } from './base-provider';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-09-24 22:55:17 +03:00
|
|
|
import { Signer } from '../abstract-signer';
|
2018-07-31 01:59:52 +03:00
|
|
|
|
2018-10-15 02:00:15 +03:00
|
|
|
import * as errors from '../errors';
|
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
import { getAddress } from '../utils/address';
|
2018-07-31 01:59:52 +03:00
|
|
|
import { BigNumber } from '../utils/bignumber';
|
2018-07-16 10:27:49 +03:00
|
|
|
import { hexlify, hexStripZeros } from '../utils/bytes';
|
2018-07-16 07:48:41 +03:00
|
|
|
import { getNetwork } from '../utils/networks';
|
2018-12-05 01:13:55 +03:00
|
|
|
import { checkProperties, defineReadOnly, resolveProperties, shallowCopy } from '../utils/properties';
|
2018-06-13 22:39:39 +03:00
|
|
|
import { toUtf8Bytes } from '../utils/utf8';
|
2018-07-16 10:27:49 +03:00
|
|
|
import { fetchJson, poll } from '../utils/web';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
// Imported Types
|
|
|
|
import { Arrayish } from '../utils/bytes';
|
|
|
|
import { Network, Networkish } from '../utils/networks';
|
|
|
|
import { ConnectionInfo } from '../utils/web';
|
2018-07-16 07:48:41 +03:00
|
|
|
|
2018-10-15 02:00:15 +03:00
|
|
|
import { BlockTag, TransactionRequest, TransactionResponse } from '../providers/abstract-provider';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
function timer(timeout: number): Promise<any> {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
setTimeout(function() {
|
|
|
|
resolve();
|
|
|
|
}, timeout);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
function getResult(payload: { error?: { code?: number, data?: any, message?: string }, result?: any }): any {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (payload.error) {
|
|
|
|
// @TODO: not any
|
2018-12-15 02:36:24 +03:00
|
|
|
let error: any = new Error(payload.error.message);
|
2018-06-13 22:39:39 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
const _constructorGuard = {};
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
export class JsonRpcSigner extends Signer {
|
2018-06-13 22:39:39 +03:00
|
|
|
readonly provider: JsonRpcProvider;
|
2018-08-03 22:22:28 +03:00
|
|
|
private _index: number;
|
2018-06-19 01:49:00 +03:00
|
|
|
private _address: string;
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-08-03 22:22:28 +03:00
|
|
|
constructor(constructorGuard: any, provider: JsonRpcProvider, addressOrIndex?: string | number) {
|
2018-06-18 12:42:41 +03:00
|
|
|
super();
|
2018-06-14 04:10:41 +03:00
|
|
|
errors.checkNew(this, JsonRpcSigner);
|
2018-08-03 22:22:28 +03:00
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
if (constructorGuard !== _constructorGuard) {
|
|
|
|
throw new Error('do not call the JsonRpcSigner constructor directly; use provider.getSigner');
|
|
|
|
}
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
defineReadOnly(this, 'provider', provider);
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
// Statically attach to a given address
|
2018-08-03 22:22:28 +03:00
|
|
|
if (addressOrIndex) {
|
|
|
|
if (typeof(addressOrIndex) === 'string') {
|
|
|
|
defineReadOnly(this, '_address', getAddress(addressOrIndex));
|
|
|
|
} else if (typeof(addressOrIndex) === 'number') {
|
|
|
|
defineReadOnly(this, '_index', addressOrIndex);
|
|
|
|
} else {
|
|
|
|
errors.throwError('invalid address or index', errors.INVALID_ARGUMENT, { argument: 'addressOrIndex', value: addressOrIndex });
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
defineReadOnly(this, '_index', 0);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getAddress(): Promise<string> {
|
|
|
|
if (this._address) {
|
|
|
|
return Promise.resolve(this._address);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.provider.send('eth_accounts', []).then((accounts) => {
|
2018-08-03 22:22:28 +03:00
|
|
|
if (accounts.length <= this._index) {
|
|
|
|
errors.throwError('unknown account #' + this._index, errors.UNSUPPORTED_OPERATION, { operation: 'getAddress' });
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
2018-08-27 14:27:59 +03:00
|
|
|
this._address = getAddress(accounts[this._index]);
|
|
|
|
return this._address;
|
2018-06-13 22:39:39 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getBalance(blockTag?: BlockTag): Promise<BigNumber> {
|
2018-06-18 12:42:41 +03:00
|
|
|
return this.provider.getBalance(this.getAddress(), blockTag);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
getTransactionCount(blockTag?: BlockTag): Promise<number> {
|
2018-06-18 12:42:41 +03:00
|
|
|
return this.provider.getTransactionCount(this.getAddress(), blockTag);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-12-15 02:36:24 +03:00
|
|
|
sendUncheckedTransaction(transaction: TransactionRequest): Promise<string> {
|
2018-12-05 11:32:24 +03:00
|
|
|
transaction = shallowCopy(transaction);
|
2018-06-18 12:42:41 +03:00
|
|
|
|
2018-12-05 01:13:55 +03:00
|
|
|
let fromAddress = this.getAddress().then((address) => {
|
|
|
|
if (address) { address = address.toLowerCase(); }
|
|
|
|
return address;
|
2018-10-15 02:00:15 +03:00
|
|
|
});
|
2018-08-01 22:47:02 +03:00
|
|
|
|
2018-12-05 11:32:24 +03:00
|
|
|
// 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) {
|
|
|
|
let estimate = shallowCopy(transaction);
|
|
|
|
estimate.from = fromAddress;
|
|
|
|
transaction.gasLimit = this.provider.estimateGas(estimate);
|
|
|
|
}
|
|
|
|
|
2018-12-05 01:13:55 +03:00
|
|
|
return Promise.all([
|
|
|
|
resolveProperties(transaction),
|
|
|
|
fromAddress
|
|
|
|
]).then((results) => {
|
|
|
|
let tx = results[0];
|
2018-10-15 02:00:15 +03:00
|
|
|
let hexTx = JsonRpcProvider.hexlifyTransaction(tx);
|
2018-12-05 01:13:55 +03:00
|
|
|
hexTx.from = results[1];
|
2018-10-15 02:00:15 +03:00
|
|
|
return this.provider.send('eth_sendTransaction', [ hexTx ]).then((hash) => {
|
2018-12-15 02:36:24 +03:00
|
|
|
return hash;
|
2018-08-02 00:02:27 +03:00
|
|
|
}, (error) => {
|
2018-10-04 22:24:29 +03:00
|
|
|
if (error.responseText) {
|
|
|
|
// See: JsonRpcProvider.sendTransaction (@TODO: Expose a ._throwError??)
|
|
|
|
if (error.responseText.indexOf('insufficient funds') >= 0) {
|
|
|
|
errors.throwError('insufficient funds', errors.INSUFFICIENT_FUNDS, {
|
|
|
|
transaction: tx
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (error.responseText.indexOf('nonce too low') >= 0) {
|
|
|
|
errors.throwError('nonce has already been used', errors.NONCE_EXPIRED, {
|
|
|
|
transaction: tx
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (error.responseText.indexOf('replacement transaction underpriced') >= 0) {
|
|
|
|
errors.throwError('replacement fee too low', errors.REPLACEMENT_UNDERPRICED, {
|
|
|
|
transaction: tx
|
|
|
|
});
|
|
|
|
}
|
2018-08-02 00:02:27 +03:00
|
|
|
}
|
|
|
|
throw error;
|
2018-06-27 01:37:21 +03:00
|
|
|
});
|
2018-06-13 22:39:39 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-15 02:36:24 +03:00
|
|
|
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse> {
|
|
|
|
return this.sendUncheckedTransaction(transaction).then((hash) => {
|
|
|
|
return poll(() => {
|
|
|
|
return this.provider.getTransaction(hash).then((tx: TransactionResponse) => {
|
|
|
|
if (tx === null) { return undefined; }
|
|
|
|
return this.provider._wrapTransaction(tx, hash);
|
|
|
|
});
|
|
|
|
}, { onceBlock: this.provider }).catch((error: Error) => {
|
|
|
|
(<any>error).transactionHash = hash;
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
signMessage(message: Arrayish | string): Promise<string> {
|
2018-12-15 02:36:24 +03:00
|
|
|
let data = ((typeof(message) === 'string') ? toUtf8Bytes(message): message);
|
2018-06-13 22:39:39 +03:00
|
|
|
return this.getAddress().then((address) => {
|
|
|
|
|
|
|
|
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign
|
|
|
|
return this.provider.send('eth_sign', [ address.toLowerCase(), hexlify(data) ]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-22 09:13:34 +03:00
|
|
|
unlock(password: string): Promise<boolean> {
|
2018-12-15 02:36:24 +03:00
|
|
|
let provider = this.provider;
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
return this.getAddress().then(function(address) {
|
|
|
|
return provider.send('personal_unlockAccount', [ address.toLowerCase(), password, null ]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 23:03:18 +03:00
|
|
|
const allowedTransactionKeys: { [ key: string ]: boolean } = {
|
|
|
|
chainId: true, data: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true
|
|
|
|
}
|
|
|
|
|
2018-08-03 03:30:44 +03:00
|
|
|
export class JsonRpcProvider extends BaseProvider {
|
2018-06-13 22:39:39 +03:00
|
|
|
readonly connection: ConnectionInfo;
|
|
|
|
|
|
|
|
private _pendingFilter: Promise<number>;
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
constructor(url?: ConnectionInfo | string, network?: Networkish) {
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
// One parameter, but it is a network name, so swap it with the URL
|
|
|
|
if (typeof(url) === 'string') {
|
|
|
|
if (network === null && getNetwork(url)) {
|
|
|
|
network = url;
|
|
|
|
url = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
if (network) {
|
|
|
|
// The network has been specified explicitly, we can use it
|
|
|
|
super(network);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// The network is unknown, query the JSON-RPC for it
|
|
|
|
let ready: Promise<Network> = new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.send('net_version', [ ]).then((result) => {
|
2018-07-03 23:44:05 +03:00
|
|
|
return resolve(getNetwork(parseInt(result)));
|
|
|
|
}).catch((error) => {
|
|
|
|
reject(error);
|
2018-06-18 12:42:41 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
super(ready);
|
|
|
|
}
|
|
|
|
|
2018-06-14 04:10:41 +03:00
|
|
|
errors.checkNew(this, JsonRpcProvider);
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
// Default URL
|
|
|
|
if (!url) { url = 'http://localhost:8545'; }
|
|
|
|
|
|
|
|
if (typeof(url) === 'string') {
|
|
|
|
this.connection = {
|
|
|
|
url: url
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.connection = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-03 22:22:28 +03:00
|
|
|
getSigner(addressOrIndex?: string | number): JsonRpcSigner {
|
|
|
|
return new JsonRpcSigner(_constructorGuard, this, addressOrIndex);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
listAccounts(): Promise<Array<string>> {
|
2018-06-23 03:30:50 +03:00
|
|
|
return this.send('eth_accounts', []).then((accounts: Array<string>) => {
|
2018-06-13 22:39:39 +03:00
|
|
|
return accounts.map((a) => getAddress(a));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
send(method: string, params: any): Promise<any> {
|
2018-11-08 23:59:30 +03:00
|
|
|
let request = {
|
2018-06-13 22:39:39 +03:00
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: 42,
|
|
|
|
jsonrpc: "2.0"
|
|
|
|
};
|
2018-06-15 11:18:17 +03:00
|
|
|
|
2018-11-08 23:59:30 +03:00
|
|
|
return fetchJson(this.connection, JSON.stringify(request), getResult).then((result) => {
|
|
|
|
this.emit('debug', {
|
|
|
|
action: 'send',
|
|
|
|
request: request,
|
|
|
|
response: result,
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
});
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
perform(method: string, params: any): Promise<any> {
|
2018-06-13 22:39:39 +03:00
|
|
|
switch (method) {
|
|
|
|
case 'getBlockNumber':
|
|
|
|
return this.send('eth_blockNumber', []);
|
|
|
|
|
|
|
|
case 'getGasPrice':
|
|
|
|
return this.send('eth_gasPrice', []);
|
|
|
|
|
|
|
|
case 'getBalance':
|
|
|
|
return this.send('eth_getBalance', [ getLowerCase(params.address), params.blockTag ]);
|
|
|
|
|
|
|
|
case 'getTransactionCount':
|
|
|
|
return this.send('eth_getTransactionCount', [ getLowerCase(params.address), params.blockTag ]);
|
|
|
|
|
|
|
|
case 'getCode':
|
|
|
|
return this.send('eth_getCode', [ getLowerCase(params.address), params.blockTag ]);
|
|
|
|
|
|
|
|
case 'getStorageAt':
|
|
|
|
return this.send('eth_getStorageAt', [ getLowerCase(params.address), params.position, params.blockTag ]);
|
|
|
|
|
|
|
|
case 'sendTransaction':
|
2018-08-02 00:02:27 +03:00
|
|
|
return this.send('eth_sendRawTransaction', [ params.signedTransaction ]).catch((error) => {
|
2018-10-04 22:24:29 +03:00
|
|
|
if (error.responseText) {
|
|
|
|
// "insufficient funds for gas * price + value"
|
|
|
|
if (error.responseText.indexOf('insufficient funds') > 0) {
|
|
|
|
errors.throwError('insufficient funds', errors.INSUFFICIENT_FUNDS, { });
|
|
|
|
}
|
|
|
|
// "nonce too low"
|
|
|
|
if (error.responseText.indexOf('nonce too low') > 0) {
|
|
|
|
errors.throwError('nonce has already been used', errors.NONCE_EXPIRED, { });
|
|
|
|
}
|
|
|
|
// "replacement transaction underpriced"
|
|
|
|
if (error.responseText.indexOf('replacement transaction underpriced') > 0) {
|
|
|
|
errors.throwError('replacement fee too low', errors.REPLACEMENT_UNDERPRICED, { });
|
|
|
|
}
|
2018-08-02 00:02:27 +03:00
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
});
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
case 'getBlock':
|
|
|
|
if (params.blockTag) {
|
2018-09-04 17:08:50 +03:00
|
|
|
return this.send('eth_getBlockByNumber', [ params.blockTag, !!params.includeTransactions ]);
|
2018-06-13 22:39:39 +03:00
|
|
|
} else if (params.blockHash) {
|
2018-09-04 17:08:50 +03:00
|
|
|
return this.send('eth_getBlockByHash', [ params.blockHash, !!params.includeTransactions ]);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
return Promise.reject(new Error('invalid block tag or block hash'));
|
|
|
|
|
|
|
|
case 'getTransaction':
|
|
|
|
return this.send('eth_getTransactionByHash', [ params.transactionHash ]);
|
|
|
|
|
|
|
|
case 'getTransactionReceipt':
|
|
|
|
return this.send('eth_getTransactionReceipt', [ params.transactionHash ]);
|
|
|
|
|
|
|
|
case 'call':
|
2018-10-11 23:03:18 +03:00
|
|
|
return this.send('eth_call', [ JsonRpcProvider.hexlifyTransaction(params.transaction, { from: true }), params.blockTag ]);
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
case 'estimateGas':
|
2018-10-11 23:03:18 +03:00
|
|
|
return this.send('eth_estimateGas', [ JsonRpcProvider.hexlifyTransaction(params.transaction, { from: true }) ]);
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
case 'getLogs':
|
|
|
|
if (params.filter && params.filter.address != null) {
|
|
|
|
params.filter.address = getLowerCase(params.filter.address);
|
|
|
|
}
|
|
|
|
return this.send('eth_getLogs', [ params.filter ]);
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
errors.throwError(method + ' not implemented', errors.NOT_IMPLEMENTED, { operation: method });
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-16 10:27:49 +03:00
|
|
|
protected _startPending(): void {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (this._pendingFilter != null) { return; }
|
2018-12-15 02:36:24 +03:00
|
|
|
let self = this;
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-12-15 02:36:24 +03:00
|
|
|
let pendingFilter: Promise<number> = this.send('eth_newPendingTransactionFilter', []);
|
2018-06-13 22:39:39 +03:00
|
|
|
this._pendingFilter = pendingFilter;
|
|
|
|
|
|
|
|
pendingFilter.then(function(filterId) {
|
|
|
|
function poll() {
|
2018-06-23 03:30:50 +03:00
|
|
|
self.send('eth_getFilterChanges', [ filterId ]).then(function(hashes: Array<string>) {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (self._pendingFilter != pendingFilter) { return null; }
|
|
|
|
|
2018-12-15 02:36:24 +03:00
|
|
|
let seq = Promise.resolve();
|
2018-06-13 22:39:39 +03:00
|
|
|
hashes.forEach(function(hash) {
|
2018-11-13 01:17:43 +03:00
|
|
|
// @TODO: This should be garbage collected at some point... How? When?
|
2018-06-13 22:39:39 +03:00
|
|
|
self._emitted['t:' + hash.toLowerCase()] = 'pending';
|
|
|
|
seq = seq.then(function() {
|
|
|
|
return self.getTransaction(hash).then(function(tx) {
|
|
|
|
self.emit('pending', tx);
|
2018-07-03 23:44:05 +03:00
|
|
|
return null;
|
2018-06-13 22:39:39 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return seq.then(function() {
|
|
|
|
return timer(1000);
|
|
|
|
});
|
|
|
|
}).then(function() {
|
|
|
|
if (self._pendingFilter != pendingFilter) {
|
|
|
|
self.send('eth_uninstallFilter', [ filterId ]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(function() { poll(); }, 0);
|
2018-07-03 23:44:05 +03:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}).catch((error: Error) => { });
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
poll();
|
|
|
|
|
|
|
|
return filterId;
|
2018-07-03 23:44:05 +03:00
|
|
|
}).catch((error: Error) => { });
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-07-16 10:27:49 +03:00
|
|
|
protected _stopPending(): void {
|
2018-06-13 22:39:39 +03:00
|
|
|
this._pendingFilter = null;
|
|
|
|
}
|
2018-07-15 00:19:08 +03:00
|
|
|
|
|
|
|
// Convert an ethers.js transaction into a JSON-RPC transaction
|
|
|
|
// - gasLimit => gas
|
|
|
|
// - All values hexlified
|
|
|
|
// - All numeric values zero-striped
|
2018-10-11 23:03:18 +03:00
|
|
|
// NOTE: This allows a TransactionRequest, but all values should be resolved
|
|
|
|
// before this is called
|
|
|
|
static hexlifyTransaction(transaction: TransactionRequest, allowExtra?: { [key: string]: boolean }): { [key: string]: string } {
|
2018-10-15 02:00:15 +03:00
|
|
|
|
|
|
|
// Check only allowed properties are given
|
|
|
|
let allowed = shallowCopy(allowedTransactionKeys);
|
|
|
|
if (allowExtra) {
|
|
|
|
for (let key in allowExtra) {
|
|
|
|
if (allowExtra[key]) { allowed[key] = true; }
|
2018-10-11 23:03:18 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-15 02:00:15 +03:00
|
|
|
checkProperties(transaction, allowed);
|
2018-10-11 23:03:18 +03:00
|
|
|
|
|
|
|
let result: { [key: string]: string } = {};
|
2018-07-15 00:19:08 +03:00
|
|
|
|
2018-10-15 02:00:15 +03:00
|
|
|
// Some nodes (INFURA ropsten; INFURA mainnet is fine) don't like leading zeros.
|
2018-07-15 00:19:08 +03:00
|
|
|
['gasLimit', 'gasPrice', 'nonce', 'value'].forEach(function(key) {
|
|
|
|
if ((<any>transaction)[key] == null) { return; }
|
|
|
|
let value = hexStripZeros(hexlify((<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]);
|
2018-10-11 23:03:18 +03:00
|
|
|
});
|
2018-07-15 00:19:08 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|