2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import { JsonRpcProvider, JsonRpcSigner } from './json-rpc-provider';
|
2018-06-18 12:42:41 +03:00
|
|
|
|
2019-01-24 00:04:54 +03:00
|
|
|
import { isHexString } from "../utils/bytes";
|
2018-07-16 07:48:41 +03:00
|
|
|
import { getNetwork } from '../utils/networks';
|
2018-06-18 12:42:41 +03:00
|
|
|
import { defineReadOnly } from '../utils/properties';
|
2018-07-16 07:48:41 +03:00
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
// Imported Types
|
|
|
|
import { Networkish } from '../utils/networks';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-09-24 22:55:17 +03:00
|
|
|
import * as errors from '../errors';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2019-01-24 00:04:54 +03:00
|
|
|
const defaultProjectId = "7d0d81d0919f4f05b9ab6634be01ee73";
|
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
export class InfuraProvider extends JsonRpcProvider {
|
|
|
|
readonly apiAccessToken: string;
|
2019-01-24 00:04:54 +03:00
|
|
|
readonly projectId: string;
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2019-01-24 00:04:54 +03:00
|
|
|
constructor(network?: Networkish, projectId?: string) {
|
|
|
|
let standard = getNetwork((network == null) ? 'homestead': network);
|
|
|
|
if (projectId == null) { projectId = defaultProjectId; }
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2019-01-24 00:04:54 +03:00
|
|
|
let host = null;
|
|
|
|
switch(standard.name) {
|
2018-06-13 22:39:39 +03:00
|
|
|
case 'homestead':
|
|
|
|
host = 'mainnet.infura.io';
|
|
|
|
break;
|
|
|
|
case 'ropsten':
|
|
|
|
host = 'ropsten.infura.io';
|
|
|
|
break;
|
|
|
|
case 'rinkeby':
|
|
|
|
host = 'rinkeby.infura.io';
|
|
|
|
break;
|
|
|
|
case 'kovan':
|
|
|
|
host = 'kovan.infura.io';
|
|
|
|
break;
|
|
|
|
default:
|
2019-01-24 00:04:54 +03:00
|
|
|
errors.throwError('unsupported network', errors.INVALID_ARGUMENT, {
|
|
|
|
argument: "network",
|
|
|
|
value: network
|
|
|
|
});
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2019-01-24 00:04:54 +03:00
|
|
|
// New-style Project ID
|
|
|
|
if (isHexString("0x" + projectId, 16)) {
|
|
|
|
super('https://' + host + '/v3/' + projectId, standard);
|
|
|
|
defineReadOnly(this, 'apiAccessToken', null);
|
|
|
|
defineReadOnly(this, 'projectId', projectId);
|
2018-06-14 04:10:41 +03:00
|
|
|
|
2019-01-24 00:04:54 +03:00
|
|
|
// Legacy API Access Token
|
|
|
|
} else {
|
|
|
|
super('https://' + host + '/' + projectId, standard);
|
|
|
|
defineReadOnly(this, 'apiAccessToken', projectId);
|
|
|
|
defineReadOnly(this, 'projectId', null);
|
|
|
|
}
|
|
|
|
|
|
|
|
errors.checkNew(this, InfuraProvider);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
2018-07-16 10:27:49 +03:00
|
|
|
protected _startPending(): void {
|
2018-12-27 23:48:38 +03:00
|
|
|
errors.warn('WARNING: INFURA does not support pending filters');
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getSigner(address?: string): JsonRpcSigner {
|
2019-01-24 00:04:54 +03:00
|
|
|
return errors.throwError(
|
2018-06-13 22:39:39 +03:00
|
|
|
'INFURA does not support signing',
|
|
|
|
errors.UNSUPPORTED_OPERATION,
|
|
|
|
{ operation: 'getSigner' }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
listAccounts(): Promise<Array<string>> {
|
2018-06-13 22:39:39 +03:00
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
}
|