ethers.js/src.ts/providers/infura-provider.ts

81 lines
2.6 KiB
TypeScript
Raw Normal View History

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
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
// Imported Types
import { Networkish } from '../utils/networks';
2018-06-13 22:39:39 +03:00
import * as errors from '../errors';
2018-06-13 22:39:39 +03:00
const defaultProjectId = "7d0d81d0919f4f05b9ab6634be01ee73";
2018-06-13 22:39:39 +03:00
export class InfuraProvider extends JsonRpcProvider {
readonly apiAccessToken: string;
readonly projectId: string;
2018-06-13 22:39:39 +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
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;
2019-03-08 22:28:39 +03:00
case 'goerli':
host = 'goerli.infura.io';
break;
2018-06-13 22:39:39 +03:00
case 'kovan':
host = 'kovan.infura.io';
break;
default:
errors.throwError('unsupported network', errors.INVALID_ARGUMENT, {
argument: "network",
value: network
});
2018-06-13 22:39:39 +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);
// Legacy API Access Token
} else {
errors.warn("The legacy INFURA apiAccesToken API is deprecated; please upgrade to a Project ID instead (see INFURA dshboard; https://infura.io)");
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
}
protected _startPending(): void {
errors.warn('WARNING: INFURA does not support pending filters');
2018-06-13 22:39:39 +03:00
}
getSigner(address?: string): JsonRpcSigner {
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([]);
}
}