2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
import { getNetwork, Network, Networkish } from './networks';
|
2018-06-14 09:25:56 +03:00
|
|
|
import { BlockTag, Provider, TransactionRequest, TransactionResponse } from './provider';
|
2018-06-18 12:42:41 +03:00
|
|
|
import { Signer } from '../wallet/wallet';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
import { getAddress } from '../utils/address';
|
|
|
|
import { BigNumber } from '../utils/bignumber';
|
2018-06-17 23:47:28 +03:00
|
|
|
import { Arrayish, hexlify, hexStripZeros } from '../utils/bytes';
|
2018-06-27 01:37:21 +03:00
|
|
|
import { defineReadOnly, resolveProperties, shallowCopy } from '../utils/properties';
|
2018-06-13 22:39:39 +03:00
|
|
|
import { toUtf8Bytes } from '../utils/utf8';
|
|
|
|
import { ConnectionInfo, fetchJson } from '../utils/web';
|
|
|
|
|
|
|
|
import * as errors from '../utils/errors';
|
|
|
|
|
|
|
|
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
|
|
|
|
var error: any = new Error(payload.error.message);
|
|
|
|
error.code = payload.error.code;
|
|
|
|
error.data = payload.error.data;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert an ethers.js transaction into a JSON-RPC transaction
|
|
|
|
// - gasLimit => gas
|
|
|
|
// - All values hexlified
|
|
|
|
// - All numeric values zero-striped
|
2018-06-22 09:13:34 +03:00
|
|
|
// @TODO: Not any, a dictionary of string to strings
|
|
|
|
export function hexlifyTransaction(transaction: TransactionRequest): any {
|
2018-06-13 22:39:39 +03:00
|
|
|
var result: any = {};
|
|
|
|
|
|
|
|
// Some nodes (INFURA ropsten; INFURA mainnet is fine) don't like extra zeros.
|
|
|
|
['gasLimit', 'gasPrice', 'nonce', 'value'].forEach(function(key) {
|
2018-06-23 03:30:50 +03:00
|
|
|
if ((<any>transaction)[key] == null) { return; }
|
|
|
|
let value = hexStripZeros(hexlify((<any>transaction)[key]));
|
2018-06-13 22:39:39 +03:00
|
|
|
if (key === 'gasLimit') { key = 'gas'; }
|
|
|
|
result[key] = value;
|
|
|
|
});
|
|
|
|
|
|
|
|
['from', 'to', 'data'].forEach(function(key) {
|
2018-06-23 03:30:50 +03:00
|
|
|
if ((<any>transaction)[key] == null) { return; }
|
|
|
|
result[key] = hexlify((<any>transaction)[key]);
|
2018-06-13 22:39:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLowerCase(value: string): string {
|
|
|
|
if (value) { return value.toLowerCase(); }
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
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-06-19 01:49:00 +03:00
|
|
|
private _address: string;
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
constructor(provider: JsonRpcProvider, address?: string) {
|
2018-06-18 12:42:41 +03:00
|
|
|
super();
|
2018-06-14 04:10:41 +03:00
|
|
|
errors.checkNew(this, JsonRpcSigner);
|
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
|
|
|
|
if (address) {
|
2018-06-18 12:42:41 +03:00
|
|
|
defineReadOnly(this, '_address', address);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get address(): string {
|
|
|
|
if (!this._address) {
|
|
|
|
errors.throwError('no sync sync address available; use getAddress', errors.UNSUPPORTED_OPERATION, { operation: 'address' });
|
|
|
|
}
|
|
|
|
return this._address
|
|
|
|
}
|
|
|
|
|
|
|
|
getAddress(): Promise<string> {
|
|
|
|
if (this._address) {
|
|
|
|
return Promise.resolve(this._address);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.provider.send('eth_accounts', []).then((accounts) => {
|
|
|
|
if (accounts.length === 0) {
|
|
|
|
errors.throwError('no accounts', errors.UNSUPPORTED_OPERATION, { operation: 'getAddress' });
|
|
|
|
}
|
|
|
|
return getAddress(accounts[0]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-18 12:42:41 +03:00
|
|
|
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse> {
|
2018-06-27 01:37:21 +03:00
|
|
|
let tx = shallowCopy(transaction);
|
2018-06-18 12:42:41 +03:00
|
|
|
|
|
|
|
if (tx.from == null) {
|
|
|
|
tx.from = this.getAddress().then((address) => {
|
|
|
|
if (!address) { return null; }
|
|
|
|
return address.toLowerCase();
|
2018-06-13 22:39:39 +03:00
|
|
|
});
|
2018-06-18 12:42:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return resolveProperties(tx).then((tx) => {
|
2018-06-27 01:37:21 +03:00
|
|
|
tx = hexlifyTransaction(tx);
|
|
|
|
return this.provider.send('eth_sendTransaction', [ tx ]).then((hash) => {
|
|
|
|
// @TODO: Make a pollProperty method in utils
|
|
|
|
return new Promise((resolve: (result: TransactionResponse) => void, reject) => {
|
|
|
|
let count = 0;
|
|
|
|
let check = () => {
|
|
|
|
this.provider.getTransaction(hash).then((tx: TransactionResponse) => {
|
|
|
|
if (tx == null) {
|
|
|
|
if (count++ > 500) {
|
|
|
|
// @TODO: Better error
|
|
|
|
reject(new Error('could not find transaction'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(check, 200);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-27 01:59:22 +03:00
|
|
|
resolve(this.provider._wrapTransaction(tx, hash));
|
2018-06-27 01:37:21 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
setTimeout(check, 50);
|
|
|
|
});
|
|
|
|
});
|
2018-06-13 22:39:39 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
signMessage(message: Arrayish | string): Promise<string> {
|
|
|
|
var data = ((typeof(message) === 'string') ? toUtf8Bytes(message): message);
|
|
|
|
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-06-13 22:39:39 +03:00
|
|
|
var provider = this.provider;
|
|
|
|
|
|
|
|
return this.getAddress().then(function(address) {
|
|
|
|
return provider.send('personal_unlockAccount', [ address.toLowerCase(), password, null ]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class JsonRpcProvider extends Provider {
|
|
|
|
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) => {
|
|
|
|
let chainId = parseInt(result);
|
|
|
|
|
|
|
|
resolve(getNetwork(chainId));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
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-06-27 00:49:54 +03:00
|
|
|
getSigner(address?: string): JsonRpcSigner {
|
2018-06-13 22:39:39 +03:00
|
|
|
return new JsonRpcSigner(this, address);
|
|
|
|
}
|
|
|
|
|
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-06-13 22:39:39 +03:00
|
|
|
var request = {
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: 42,
|
|
|
|
jsonrpc: "2.0"
|
|
|
|
};
|
2018-06-15 11:18:17 +03:00
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
return fetchJson(this.connection, JSON.stringify(request), getResult);
|
|
|
|
}
|
|
|
|
|
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':
|
|
|
|
return this.send('eth_sendRawTransaction', [ params.signedTransaction ]);
|
|
|
|
|
|
|
|
case 'getBlock':
|
|
|
|
if (params.blockTag) {
|
|
|
|
return this.send('eth_getBlockByNumber', [ params.blockTag, false ]);
|
|
|
|
} else if (params.blockHash) {
|
|
|
|
return this.send('eth_getBlockByHash', [ params.blockHash, false ]);
|
|
|
|
}
|
|
|
|
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':
|
|
|
|
return this.send('eth_call', [ hexlifyTransaction(params.transaction), 'latest' ]);
|
|
|
|
|
|
|
|
case 'estimateGas':
|
|
|
|
return this.send('eth_estimateGas', [ hexlifyTransaction(params.transaction) ]);
|
|
|
|
|
|
|
|
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-06-15 11:18:17 +03:00
|
|
|
_startPending(): void {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (this._pendingFilter != null) { return; }
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var pendingFilter: Promise<number> = this.send('eth_newPendingTransactionFilter', []);
|
|
|
|
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; }
|
|
|
|
|
|
|
|
var seq = Promise.resolve();
|
|
|
|
hashes.forEach(function(hash) {
|
|
|
|
self._emitted['t:' + hash.toLowerCase()] = 'pending';
|
|
|
|
seq = seq.then(function() {
|
|
|
|
return self.getTransaction(hash).then(function(tx) {
|
|
|
|
self.emit('pending', tx);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return seq.then(function() {
|
|
|
|
return timer(1000);
|
|
|
|
});
|
|
|
|
}).then(function() {
|
|
|
|
if (self._pendingFilter != pendingFilter) {
|
|
|
|
self.send('eth_uninstallFilter', [ filterId ]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(function() { poll(); }, 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
poll();
|
|
|
|
|
|
|
|
return filterId;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
_stopPending(): void {
|
2018-06-13 22:39:39 +03:00
|
|
|
this._pendingFilter = null;
|
|
|
|
}
|
|
|
|
}
|