diff --git a/src.ts/ethers.ts b/src.ts/ethers.ts index f9c4274d..f3cba407 100644 --- a/src.ts/ethers.ts +++ b/src.ts/ethers.ts @@ -2,29 +2,17 @@ import { Contract, Interface } from './contracts'; -import { - Provider, - getDefaultProvider, - - FallbackProvider, - - EtherscanProvider, - InfuraProvider, - JsonRpcProvider, - Web3Provider, - - IpcProvider, -} from './providers'; - -import { JsonRpcSigner } from './providers/json-rpc-provider'; +import * as providers from './providers'; import { HDNode, SigningKey, Wallet } from './wallet'; +import { constants } from './utils/constants'; +import * as errors from './utils/errors'; import * as utils from './utils'; -import * as wordlists from './wordlists'; +import * as types from './types'; -import * as errors from './utils/errors'; +import * as wordlists from './wordlists'; // This is empty in node, and used by browserify to inject extra goodies import { platform } from './utils/shims'; @@ -32,183 +20,13 @@ import { platform } from './utils/shims'; // This is generated by "npm run dist" import { version } from './_version'; -/////////////////////////////// -// Imported Abstracts - -import { Provider as _AbstractProvider } from './providers/abstract-provider'; -import { Signer as _AbstractSigner } from './wallet/abstract-signer'; - -/////////////////////////////// -// Imported Types - -import { - ContractFunction as _ContractFunction, - Event as _Event, - EventFilter as _EventFilter -} from './contracts/contract'; - -import { - Indexed as _Indexed, - - DeployDescription as _DeplyDescription, - EventDescription as _EventDescription, - FunctionDescription as _FunctionDescription, - LogDescription as _LogDescription, - TransactionDescription as _TransactionDescription -} from './contracts/interface'; - -import { - Block as _Block, - BlockTag as _BlockTag, - EventType as _EventType, - Filter as _Filter, - Log as _Log, - Listener as _Listener, - TransactionReceipt as _TransactionReceipt, - TransactionRequest as _TransactionRequest, - TransactionResponse as _TransactionResponse, -} from './providers/abstract-provider'; - -import { AsyncSendable as _AsyncSendable } from './providers/web3-provider'; - -import { - CoerceFunc as _CoerceFunc, - EventFragment as _EventFragment, - FunctionFragment as _FunctionFragment, - ParamType as _ParamType -} from './utils/abi-coder'; - -import { BigNumberish as _BigNumberish } from './utils/bignumber'; - -import { - Arrayish as _Arrayish, - Hexable as _Hexable, - Signature as _Signature -} from './utils/bytes'; - -import { SupportedAlgorithms as _SupportedAlgorithms } from './utils/hmac'; - -import { - Network as _Network, - Networkish as _Networkish -} from './utils/networks'; - -import { - Transaction as _Transaction, - UnsignedTransaction as _UnsignedTransaction -} from './utils/transaction'; - -import { UnicodeNormalizationForm as _UnicodeNotmalizationForm } from './utils/utf8'; - -import { - ConnectionInfo as _ConnectionInfo, - OnceBlockable as _OnceBlockable, - PollOptions as _PollOptions -} from './utils/web'; - -import { - EncryptOptions as _EncryptOptions, - ProgressCallback as _ProgressCallback -} from './wallet/secret-storage'; - -import { Wordlist as _Wordlist } from './wordlists/wordlist'; - -/////////////////////////////// -// Exported Types - -module types { - export const AbstractSigner = _AbstractSigner; - export const AbstractProvider = _AbstractProvider; - - export type SignerOrProvider = _AbstractSigner | _AbstractProvider; - - // ./contracts/contract - export type ContractFunction = _ContractFunction; - export type EventFilter = _EventFilter; - export interface Event extends _Event { }; - - // ./contracts/interface - export interface Indexed extends _Indexed { }; - export interface DeployDescription extends _DeplyDescription { }; - export interface EventDescription extends _EventDescription { }; - export interface FunctionDescription extends _FunctionDescription { }; - export interface LogDescription extends _LogDescription { }; - export interface TransactionDescription extends _TransactionDescription { }; - - // ./providers/abstract-provider - export type BlockTag = _BlockTag; - export type EventType = _EventType; - export type Filter = _Filter; - export type Listener = _Listener; - export type TransactionRequest = _TransactionRequest; - export interface Block extends _Block { }; - export interface Log extends _Log { }; - export interface TransactionReceipt extends _TransactionReceipt { }; - export interface TransactionResponse extends _TransactionResponse { } - - // ./providers/web3-provider - export type AsyncSendable = _AsyncSendable; - - // ./utils/abi-coder - export type CoerceFunc = _CoerceFunc; - export type EventFragment = _EventFragment; - export type FunctionFragment = _FunctionFragment; - export type ParamType = _ParamType; - - // ./utils/bignumber - export type BigNumberish = _BigNumberish; - - // ./utils/bytes - export type Arrayish = _Arrayish; - export type Hexable = _Hexable; - export type Signature = _Signature; - - // ./utils/hmac - export const SupportedAlgorithms = _SupportedAlgorithms; - - // ./utils/networks - export type Network = _Network; - export type Networkish = _Networkish; - - // ./utils/transaction - export type UnsignedTransaction = _UnsignedTransaction; - export interface Transaction extends _Transaction { }; - - // ./utils/utf8 - export const UnicodeNormalizationForm = _UnicodeNotmalizationForm; - - // ./utils/web - export type ConnectionInfo = _ConnectionInfo; - export interface OnceBlockable extends _OnceBlockable { }; - export type PollOptions = _PollOptions; - - // ./wallet/secret-storage - export type EncryptOptions = _EncryptOptions; - export type ProgressCallback = _ProgressCallback; - - // ./wordlists/wordlist - export const Wordlist = _Wordlist; +function getDefaultProvider(network?: types.Network | string): providers.BaseProvider { + return new providers.FallbackProvider([ + new providers.InfuraProvider(network), + new providers.EtherscanProvider(network), + ]); } -/////////////////////////////// - -const constants = utils.constants; - -const providers = { - Provider, - - FallbackProvider, - - EtherscanProvider, - InfuraProvider, - - IpcProvider, - JsonRpcProvider, - Web3Provider, - - JsonRpcSigner -}; - export { Wallet, diff --git a/src.ts/providers/provider.ts b/src.ts/providers/base-provider.ts similarity index 93% rename from src.ts/providers/provider.ts rename to src.ts/providers/base-provider.ts index e415d5b4..5cb86f4a 100644 --- a/src.ts/providers/provider.ts +++ b/src.ts/providers/base-provider.ts @@ -15,7 +15,7 @@ import * as errors from '../utils/errors'; /////////////////////////////// // Imported Abstracts -import { Provider as AbstractProvider } from './abstract-provider'; +import { Provider } from './abstract-provider'; /////////////////////////////// @@ -441,84 +441,6 @@ function getEventTag(eventName: EventType): string { // Provider Object -/* @TODO: -type Event = { - eventName: string, - listener: any, // @TODO: Function any: any - type: string, -} -*/ - -// @TODO: Perhaps allow a SignDigestAsyncFunc? - -// Enable a simple signing function and provider to provide a full Signer -/* -export type SignDigestFunc = (digest: string) => Promise; -export class ProviderSigner extends Signer { - readonly provider: Provider; - readonly signDigest: SignDigestFunc; - - private _addressPromise: Promise; - - constructor(address: string | Promise, signDigest: SignDigestFunc, provider: Provider) { - super(); - errors.checkNew(this, ProviderSigner); - defineReadOnly(this, '_addressPromise', Promise.resolve(address)); - defineReadOnly(this, 'signDigest', signDigest); - defineReadOnly(this, 'provider', provider); - } - - getAddress(): Promise { - return this._addressPromise; - } - - signMessage(message: Arrayish | string): Promise { - return this.signDigest(arrayify(hashMessage(message))).then((signature) => { - return joinSignature(signature); - }); - } - - sendTransaction(transaction: TransactionRequest): Promise { - transaction = shallowCopy(transaction); - - if (transaction.chainId == null) { - transaction.chainId = this.provider.getNetwork().then((network) => { - return network.chainId; - }); - } - - if (transaction.from == null) { - transaction.from = this.getAddress(); - } - - if (transaction.gasLimit == null) { - transaction.gasLimit = this.provider.estimateGas(transaction); - } - - if (transaction.gasPrice == null) { - transaction.gasPrice = this.provider.getGasPrice(); - } - - return resolveProperties(transaction).then((tx) => { - let unsignedTx = serializeTransaction(tx); - return this.signDigest(keccak256(unsignedTx)).then((signature) => { - let signedTx = serializeTransaxction(tx, (ut) => { - if (unsignedTx !== ut) { throw new Error('this should not happen'); } - return signature; - }); - - return this._addressPromise.then((address) => { - if (parseTransaction(signedTx).from !== address) { - errors.throwError('signing address does not match expected address', errors.UNKNOWN_ERROR, { address: parseTransaction(signedTx).from, expectedAddress: address, signedTransaction: signedTx }); - } - return this.provider.sendTransaction(signedTx); - }); - }); - }); - } -} -*/ - /** * EventType * - "block" @@ -536,7 +458,7 @@ type _Event = { tag: string; } -export class Provider extends AbstractProvider { +export class BaseProvider extends Provider { private _network: Network; private _events: Array<_Event>; @@ -966,7 +888,7 @@ export class Provider extends AbstractProvider { } return undefined; } - return Provider.checkTransactionResponse(result); + return BaseProvider.checkTransactionResponse(result); }); }, { onceBlock: this }); }); diff --git a/src.ts/providers/etherscan-provider.ts b/src.ts/providers/etherscan-provider.ts index 6fb7d6e9..72deed8a 100644 --- a/src.ts/providers/etherscan-provider.ts +++ b/src.ts/providers/etherscan-provider.ts @@ -1,5 +1,5 @@ -import { Provider } from './provider'; +import { BaseProvider } from './base-provider'; import { hexlify, hexStripZeros } from '../utils/bytes'; import { defineReadOnly } from '../utils/properties'; @@ -74,7 +74,7 @@ function checkLogTag(blockTag: string): number | "latest" { } -export class EtherscanProvider extends Provider{ +export class EtherscanProvider extends BaseProvider{ readonly baseUrl: string; readonly apiKey: string; constructor(network?: Networkish, apiKey?: string) { @@ -299,7 +299,7 @@ export class EtherscanProvider extends Provider{ if (tx.creates == null && tx.contractAddress != null) { tx.creates = tx.contractAddress; } - let item = Provider.checkTransactionResponse(tx); + let item = BaseProvider.checkTransactionResponse(tx); if (tx.timeStamp) { item.timestamp = parseInt(tx.timeStamp); } output.push(item); }); diff --git a/src.ts/providers/fallback-provider.ts b/src.ts/providers/fallback-provider.ts index 7aaf1e82..589daafe 100644 --- a/src.ts/providers/fallback-provider.ts +++ b/src.ts/providers/fallback-provider.ts @@ -1,6 +1,6 @@ 'use strict'; -import { Provider } from './provider'; +import { BaseProvider } from './base-provider'; // Imported Types import { Network } from '../utils/networks'; @@ -44,10 +44,10 @@ function checkNetworks(networks: Array): boolean { return result; } -export class FallbackProvider extends Provider { - private _providers: Array; +export class FallbackProvider extends BaseProvider { + private _providers: Array; - constructor(providers: Array) { + constructor(providers: Array) { if (providers.length === 0) { throw new Error('no providers'); } @@ -73,7 +73,7 @@ export class FallbackProvider extends Provider { this._providers = providers.slice(0); } - get providers(): Array { + get providers(): Array { // Return a copy, so we don't get mutated return this._providers.slice(0); } diff --git a/src.ts/providers/index.ts b/src.ts/providers/index.ts index 281b24b5..11980925 100644 --- a/src.ts/providers/index.ts +++ b/src.ts/providers/index.ts @@ -1,6 +1,6 @@ 'use strict'; -import { Provider } from './provider'; +import { BaseProvider } from './base-provider'; import { EtherscanProvider } from './etherscan-provider'; import { FallbackProvider } from './fallback-provider'; @@ -12,27 +12,26 @@ import { Web3Provider } from './web3-provider'; /////////////////////////////// // Imported Abstracts -import { Provider as AbstractProvider } from './abstract-provider'; +//import { Provider as AbstractProvider } from './abstract-provider'; /////////////////////////////// // Imported Types -import { Network } from '../utils/networks'; +//import { Network } from '../utils/networks'; /////////////////////////////// +/* function getDefaultProvider(network?: Network | string): Provider { return new FallbackProvider([ new InfuraProvider(network), new EtherscanProvider(network), ]); } +*/ export { - AbstractProvider, - - Provider, - getDefaultProvider, + BaseProvider, FallbackProvider, diff --git a/src.ts/providers/json-rpc-provider.ts b/src.ts/providers/json-rpc-provider.ts index d31b22a8..197e2c42 100644 --- a/src.ts/providers/json-rpc-provider.ts +++ b/src.ts/providers/json-rpc-provider.ts @@ -2,7 +2,7 @@ // See: https://github.com/ethereum/wiki/wiki/JSON-RPC -import { Provider } from './provider'; +import { BaseProvider } from './base-provider'; import { Signer } from '../wallet/abstract-signer'; @@ -161,7 +161,7 @@ export class JsonRpcSigner extends Signer { } } -export class JsonRpcProvider extends Provider { +export class JsonRpcProvider extends BaseProvider { readonly connection: ConnectionInfo; private _pendingFilter: Promise; diff --git a/src.ts/types.ts b/src.ts/types.ts new file mode 100644 index 00000000..13e612ee --- /dev/null +++ b/src.ts/types.ts @@ -0,0 +1,160 @@ + +/////////////////////////////// +// Imported Abstracts + +import { Provider } from './providers/abstract-provider'; +import { Signer } from './wallet/abstract-signer'; + +/////////////////////////////// +// Imported Types + +import { + ContractFunction, + Event, + EventFilter +} from './contracts/contract'; + +import { + Indexed, + + DeployDescription, + EventDescription, + FunctionDescription, + LogDescription, + TransactionDescription +} from './contracts/interface'; + +import { + Block, + BlockTag, + EventType, + Filter, + Log, + Listener, + TransactionReceipt, + TransactionRequest, + TransactionResponse +} from './providers/abstract-provider'; + +import { AsyncSendable } from './providers/web3-provider'; + +import { + CoerceFunc, + EventFragment, + FunctionFragment, + ParamType, +} from './utils/abi-coder'; + +import { BigNumberish } from './utils/bignumber'; + +import { + Arrayish, + Hexable, + Signature +} from './utils/bytes'; + +import { SupportedAlgorithms } from './utils/hmac'; + +import { + Network, + Networkish +} from './utils/networks'; + +import { + Transaction, + UnsignedTransaction +} from './utils/transaction'; + +import { UnicodeNormalizationForm } from './utils/utf8'; + +import { + ConnectionInfo, + OnceBlockable, + PollOptions +} from './utils/web'; + +import { + EncryptOptions, + ProgressCallback, +} from './wallet/secret-storage'; + +import { Wordlist } from './wordlists/wordlist'; + +/////////////////////////////// +// Exported Types + +export { + // Abstract classes + Provider, + Signer, + + // ./contracts/contract + ContractFunction, + EventFilter, + Event, + + // ./contracts/interface + Indexed, + DeployDescription, + EventDescription, + FunctionDescription, + LogDescription, + TransactionDescription, + + // ./providers/abstract-provider + BlockTag, + EventType, + Filter, + Listener, + TransactionRequest, + Block, + Log, + TransactionReceipt, + TransactionResponse, + + // ./providers/web3-provider + AsyncSendable, + + // ./utils/abi-coder + CoerceFunc, + EventFragment, + FunctionFragment, + ParamType, + + // ./utils/bignumber + BigNumberish, + + // ./utils/bytes + Arrayish, + Hexable, + Signature, + + // ./utils/hmac + SupportedAlgorithms, + + // ./utils/networks + Network, + Networkish, + + // ./utils/transaction + UnsignedTransaction, + Transaction, + + // ./utils/utf8 + UnicodeNormalizationForm, + + // ./utils/web + ConnectionInfo, + OnceBlockable, + PollOptions, + + // ./wallet/secret-storage + EncryptOptions, + ProgressCallback, + + // ./wordlists/wordlist + Wordlist +} + +/////////////////////////////// + diff --git a/src.ts/utils/bytes.ts b/src.ts/utils/bytes.ts index 20cbccd9..089711ae 100644 --- a/src.ts/utils/bytes.ts +++ b/src.ts/utils/bytes.ts @@ -31,9 +31,6 @@ export interface Signature { /////////////////////////////// -export const AddressZero = '0x0000000000000000000000000000000000000000'; -export const HashZero = '0x0000000000000000000000000000000000000000000000000000000000000000'; - export function isHexable(value: any): value is Hexable { return !!(value.toHexString); } diff --git a/src.ts/utils/constants.ts b/src.ts/utils/constants.ts new file mode 100644 index 00000000..d4a2357f --- /dev/null +++ b/src.ts/utils/constants.ts @@ -0,0 +1,31 @@ +import { + ConstantNegativeOne, + ConstantZero, + ConstantOne, + ConstantTwo, + ConstantWeiPerEther, + ConstantMaxUint256 +} from './bignumber'; + +const AddressZero = '0x0000000000000000000000000000000000000000'; +const HashZero = '0x0000000000000000000000000000000000000000000000000000000000000000'; + +// NFKD (decomposed) +//const EtherSymbol = '\uD835\uDF63'; + +// NFKC (composed) +const EtherSymbol = '\u039e'; + +export const constants = { + AddressZero: AddressZero, + HashZero: HashZero, + + EtherSymbol: EtherSymbol, + + NegativeOne: ConstantNegativeOne, + Zero: ConstantZero, + One: ConstantOne, + Two: ConstantTwo, + WeiPerEther: ConstantWeiPerEther, + MaxUint256: ConstantMaxUint256 +}; diff --git a/src.ts/utils/index.ts b/src.ts/utils/index.ts index ae5ce8d4..1b70b944 100644 --- a/src.ts/utils/index.ts +++ b/src.ts/utils/index.ts @@ -3,8 +3,8 @@ import { AbiCoder, defaultAbiCoder, formatSignature, formatParamType, parseSignature, parseParamType } from './abi-coder'; import { getAddress, getContractAddress, getIcapAddress } from './address'; import * as base64 from './base64'; -import { BigNumber, bigNumberify, ConstantNegativeOne, ConstantZero, ConstantOne, ConstantTwo, ConstantWeiPerEther, ConstantMaxUint256 } from './bignumber'; -import { AddressZero, arrayify, concat, HashZero, hexDataSlice, hexDataLength, hexlify, hexStripZeros, hexZeroPad, joinSignature, padZeros, splitSignature, stripZeros } from './bytes'; +import { BigNumber, bigNumberify } from './bignumber'; +import { arrayify, concat, hexDataSlice, hexDataLength, hexlify, hexStripZeros, hexZeroPad, joinSignature, padZeros, splitSignature, stripZeros } from './bytes'; import { hashMessage, id, namehash } from './hash'; import { getJsonWalletAddress } from './json-wallet'; import { keccak256 } from './keccak256'; @@ -20,25 +20,6 @@ import { toUtf8Bytes, toUtf8String } from './utf8'; import { formatEther, parseEther, formatUnits, parseUnits } from './units'; import { fetchJson } from './web'; -import * as errors from './errors'; - -// NFKD (decomposed) -//const etherSymbol = '\uD835\uDF63'; - -// NFKC (composed) -const etherSymbol = '\u039e'; - -const constants = { - AddressZero: AddressZero, - HashZero: HashZero, - NegativeOne: ConstantNegativeOne, - Zero: ConstantZero, - One: ConstantOne, - Two: ConstantTwo, - WeiPerEther: ConstantWeiPerEther, - MaxUint256: ConstantMaxUint256 -}; - export { AbiCoder, defaultAbiCoder, @@ -47,8 +28,6 @@ export { parseSignature, parseParamType, - constants, - RLP, fetchJson, @@ -59,8 +38,6 @@ export { resolveProperties, shallowCopy, - etherSymbol, - arrayify, concat, @@ -113,8 +90,6 @@ export { getJsonWalletAddress, computePublicKey, - verifyMessage, - - errors + verifyMessage } diff --git a/src.ts/wallet/index.ts b/src.ts/wallet/index.ts index aadfd190..af23184e 100644 --- a/src.ts/wallet/index.ts +++ b/src.ts/wallet/index.ts @@ -3,10 +3,8 @@ import { Wallet } from './wallet'; import * as HDNode from './hdnode'; import { SigningKey } from './signing-key'; -import { Signer as AbstractSigner } from './abstract-signer'; export { - AbstractSigner, HDNode, SigningKey, Wallet