2
0
mirror of synced 2025-02-24 20:18:07 +00:00

Restructuring for TypeScript.

This commit is contained in:
Richard Moore 2018-08-02 20:30:44 -04:00
parent 30a84d8577
commit 5c8703fb30
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
11 changed files with 223 additions and 323 deletions

View File

@ -2,29 +2,17 @@
import { Contract, Interface } from './contracts'; import { Contract, Interface } from './contracts';
import { import * as providers from './providers';
Provider,
getDefaultProvider,
FallbackProvider,
EtherscanProvider,
InfuraProvider,
JsonRpcProvider,
Web3Provider,
IpcProvider,
} from './providers';
import { JsonRpcSigner } from './providers/json-rpc-provider';
import { HDNode, SigningKey, Wallet } from './wallet'; import { HDNode, SigningKey, Wallet } from './wallet';
import { constants } from './utils/constants';
import * as errors from './utils/errors';
import * as utils from './utils'; 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 // This is empty in node, and used by browserify to inject extra goodies
import { platform } from './utils/shims'; import { platform } from './utils/shims';
@ -32,183 +20,13 @@ import { platform } from './utils/shims';
// This is generated by "npm run dist" // This is generated by "npm run dist"
import { version } from './_version'; import { version } from './_version';
/////////////////////////////// function getDefaultProvider(network?: types.Network | string): providers.BaseProvider {
// Imported Abstracts return new providers.FallbackProvider([
new providers.InfuraProvider(network),
import { Provider as _AbstractProvider } from './providers/abstract-provider'; new providers.EtherscanProvider(network),
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;
} }
///////////////////////////////
const constants = utils.constants;
const providers = {
Provider,
FallbackProvider,
EtherscanProvider,
InfuraProvider,
IpcProvider,
JsonRpcProvider,
Web3Provider,
JsonRpcSigner
};
export { export {
Wallet, Wallet,

View File

@ -15,7 +15,7 @@ import * as errors from '../utils/errors';
/////////////////////////////// ///////////////////////////////
// Imported Abstracts // 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 // 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<Signature>;
export class ProviderSigner extends Signer {
readonly provider: Provider;
readonly signDigest: SignDigestFunc;
private _addressPromise: Promise<string>;
constructor(address: string | Promise<string>, 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<string> {
return this._addressPromise;
}
signMessage(message: Arrayish | string): Promise<string> {
return this.signDigest(arrayify(hashMessage(message))).then((signature) => {
return joinSignature(signature);
});
}
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse> {
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 * EventType
* - "block" * - "block"
@ -536,7 +458,7 @@ type _Event = {
tag: string; tag: string;
} }
export class Provider extends AbstractProvider { export class BaseProvider extends Provider {
private _network: Network; private _network: Network;
private _events: Array<_Event>; private _events: Array<_Event>;
@ -966,7 +888,7 @@ export class Provider extends AbstractProvider {
} }
return undefined; return undefined;
} }
return Provider.checkTransactionResponse(result); return BaseProvider.checkTransactionResponse(result);
}); });
}, { onceBlock: this }); }, { onceBlock: this });
}); });

View File

@ -1,5 +1,5 @@
import { Provider } from './provider'; import { BaseProvider } from './base-provider';
import { hexlify, hexStripZeros } from '../utils/bytes'; import { hexlify, hexStripZeros } from '../utils/bytes';
import { defineReadOnly } from '../utils/properties'; 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 baseUrl: string;
readonly apiKey: string; readonly apiKey: string;
constructor(network?: Networkish, apiKey?: string) { constructor(network?: Networkish, apiKey?: string) {
@ -299,7 +299,7 @@ export class EtherscanProvider extends Provider{
if (tx.creates == null && tx.contractAddress != null) { if (tx.creates == null && tx.contractAddress != null) {
tx.creates = tx.contractAddress; tx.creates = tx.contractAddress;
} }
let item = Provider.checkTransactionResponse(tx); let item = BaseProvider.checkTransactionResponse(tx);
if (tx.timeStamp) { item.timestamp = parseInt(tx.timeStamp); } if (tx.timeStamp) { item.timestamp = parseInt(tx.timeStamp); }
output.push(item); output.push(item);
}); });

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
import { Provider } from './provider'; import { BaseProvider } from './base-provider';
// Imported Types // Imported Types
import { Network } from '../utils/networks'; import { Network } from '../utils/networks';
@ -44,10 +44,10 @@ function checkNetworks(networks: Array<Network>): boolean {
return result; return result;
} }
export class FallbackProvider extends Provider { export class FallbackProvider extends BaseProvider {
private _providers: Array<Provider>; private _providers: Array<BaseProvider>;
constructor(providers: Array<Provider>) { constructor(providers: Array<BaseProvider>) {
if (providers.length === 0) { throw new Error('no providers'); } if (providers.length === 0) { throw new Error('no providers'); }
@ -73,7 +73,7 @@ export class FallbackProvider extends Provider {
this._providers = providers.slice(0); this._providers = providers.slice(0);
} }
get providers(): Array<Provider> { get providers(): Array<BaseProvider> {
// Return a copy, so we don't get mutated // Return a copy, so we don't get mutated
return this._providers.slice(0); return this._providers.slice(0);
} }

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
import { Provider } from './provider'; import { BaseProvider } from './base-provider';
import { EtherscanProvider } from './etherscan-provider'; import { EtherscanProvider } from './etherscan-provider';
import { FallbackProvider } from './fallback-provider'; import { FallbackProvider } from './fallback-provider';
@ -12,27 +12,26 @@ import { Web3Provider } from './web3-provider';
/////////////////////////////// ///////////////////////////////
// Imported Abstracts // Imported Abstracts
import { Provider as AbstractProvider } from './abstract-provider'; //import { Provider as AbstractProvider } from './abstract-provider';
/////////////////////////////// ///////////////////////////////
// Imported Types // Imported Types
import { Network } from '../utils/networks'; //import { Network } from '../utils/networks';
/////////////////////////////// ///////////////////////////////
/*
function getDefaultProvider(network?: Network | string): Provider { function getDefaultProvider(network?: Network | string): Provider {
return new FallbackProvider([ return new FallbackProvider([
new InfuraProvider(network), new InfuraProvider(network),
new EtherscanProvider(network), new EtherscanProvider(network),
]); ]);
} }
*/
export { export {
AbstractProvider, BaseProvider,
Provider,
getDefaultProvider,
FallbackProvider, FallbackProvider,

View File

@ -2,7 +2,7 @@
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC // See: https://github.com/ethereum/wiki/wiki/JSON-RPC
import { Provider } from './provider'; import { BaseProvider } from './base-provider';
import { Signer } from '../wallet/abstract-signer'; 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; readonly connection: ConnectionInfo;
private _pendingFilter: Promise<number>; private _pendingFilter: Promise<number>;

160
src.ts/types.ts Normal file
View File

@ -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
}
///////////////////////////////

View File

@ -31,9 +31,6 @@ export interface Signature {
/////////////////////////////// ///////////////////////////////
export const AddressZero = '0x0000000000000000000000000000000000000000';
export const HashZero = '0x0000000000000000000000000000000000000000000000000000000000000000';
export function isHexable(value: any): value is Hexable { export function isHexable(value: any): value is Hexable {
return !!(value.toHexString); return !!(value.toHexString);
} }

31
src.ts/utils/constants.ts Normal file
View File

@ -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
};

View File

@ -3,8 +3,8 @@
import { AbiCoder, defaultAbiCoder, formatSignature, formatParamType, parseSignature, parseParamType } from './abi-coder'; import { AbiCoder, defaultAbiCoder, formatSignature, formatParamType, parseSignature, parseParamType } from './abi-coder';
import { getAddress, getContractAddress, getIcapAddress } from './address'; import { getAddress, getContractAddress, getIcapAddress } from './address';
import * as base64 from './base64'; import * as base64 from './base64';
import { BigNumber, bigNumberify, ConstantNegativeOne, ConstantZero, ConstantOne, ConstantTwo, ConstantWeiPerEther, ConstantMaxUint256 } from './bignumber'; import { BigNumber, bigNumberify } from './bignumber';
import { AddressZero, arrayify, concat, HashZero, hexDataSlice, hexDataLength, hexlify, hexStripZeros, hexZeroPad, joinSignature, padZeros, splitSignature, stripZeros } from './bytes'; import { arrayify, concat, hexDataSlice, hexDataLength, hexlify, hexStripZeros, hexZeroPad, joinSignature, padZeros, splitSignature, stripZeros } from './bytes';
import { hashMessage, id, namehash } from './hash'; import { hashMessage, id, namehash } from './hash';
import { getJsonWalletAddress } from './json-wallet'; import { getJsonWalletAddress } from './json-wallet';
import { keccak256 } from './keccak256'; import { keccak256 } from './keccak256';
@ -20,25 +20,6 @@ import { toUtf8Bytes, toUtf8String } from './utf8';
import { formatEther, parseEther, formatUnits, parseUnits } from './units'; import { formatEther, parseEther, formatUnits, parseUnits } from './units';
import { fetchJson } from './web'; 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 { export {
AbiCoder, AbiCoder,
defaultAbiCoder, defaultAbiCoder,
@ -47,8 +28,6 @@ export {
parseSignature, parseSignature,
parseParamType, parseParamType,
constants,
RLP, RLP,
fetchJson, fetchJson,
@ -59,8 +38,6 @@ export {
resolveProperties, resolveProperties,
shallowCopy, shallowCopy,
etherSymbol,
arrayify, arrayify,
concat, concat,
@ -113,8 +90,6 @@ export {
getJsonWalletAddress, getJsonWalletAddress,
computePublicKey, computePublicKey,
verifyMessage, verifyMessage
errors
} }

View File

@ -3,10 +3,8 @@
import { Wallet } from './wallet'; import { Wallet } from './wallet';
import * as HDNode from './hdnode'; import * as HDNode from './hdnode';
import { SigningKey } from './signing-key'; import { SigningKey } from './signing-key';
import { Signer as AbstractSigner } from './abstract-signer';
export { export {
AbstractSigner,
HDNode, HDNode,
SigningKey, SigningKey,
Wallet Wallet