Added new checks and fixed up deploy for Contract.
This commit is contained in:
parent
f7bfa50f15
commit
c411d9744d
@ -2,10 +2,9 @@
|
|||||||
|
|
||||||
import { Interface } from './interface';
|
import { Interface } from './interface';
|
||||||
|
|
||||||
// @TODO: Move to utils?
|
import { Provider, TransactionResponse } from '../providers/provider';
|
||||||
import { TransactionResponse } from '../providers/provider';
|
|
||||||
import { Network } from '../providers/networks';
|
|
||||||
|
|
||||||
|
import { getContractAddress } from '../utils/address';
|
||||||
import { ParamType } from '../utils/abi-coder';
|
import { ParamType } from '../utils/abi-coder';
|
||||||
import { BigNumber, ConstantZero } from '../utils/bignumber';
|
import { BigNumber, ConstantZero } from '../utils/bignumber';
|
||||||
import { defineReadOnly, resolveProperties } from '../utils/properties';
|
import { defineReadOnly, resolveProperties } from '../utils/properties';
|
||||||
@ -211,6 +210,7 @@ function runMethod(contract: Contract, functionName: string, estimateOnly: boole
|
|||||||
throw new Error('unsupport type - ' + method.type);
|
throw new Error('unsupport type - ' + method.type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
interface Provider {
|
interface Provider {
|
||||||
getNetwork(): Promise<Network>;
|
getNetwork(): Promise<Network>;
|
||||||
getGasPrice(): Promise<BigNumber>;
|
getGasPrice(): Promise<BigNumber>;
|
||||||
@ -219,11 +219,12 @@ interface Provider {
|
|||||||
estimateGas(tx: any): Promise<BigNumber>;
|
estimateGas(tx: any): Promise<BigNumber>;
|
||||||
sendTransaction(signedTransaction: string | Promise<string>): Promise<TransactionResponse>;
|
sendTransaction(signedTransaction: string | Promise<string>): Promise<TransactionResponse>;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
interface Signer {
|
interface Signer {
|
||||||
defaultGasLimit?: BigNumber;
|
defaultGasLimit?: BigNumber;
|
||||||
defaultGasPrice?: BigNumber;
|
defaultGasPrice?: BigNumber;
|
||||||
address?: string;
|
address?: string;
|
||||||
|
provider?: Provider;
|
||||||
|
|
||||||
getAddress(): Promise<string>;
|
getAddress(): Promise<string>;
|
||||||
getTransactionCount(): Promise<number>;
|
getTransactionCount(): Promise<number>;
|
||||||
@ -232,6 +233,10 @@ interface Signer {
|
|||||||
sign(tx: any): string | Promise<string>;
|
sign(tx: any): string | Promise<string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isSigner(value: any): value is Signer {
|
||||||
|
return (value && value.provider != null);
|
||||||
|
}
|
||||||
|
|
||||||
export type ContractEstimate = (...params: Array<any>) => Promise<BigNumber>;
|
export type ContractEstimate = (...params: Array<any>) => Promise<BigNumber>;
|
||||||
export type ContractFunction = (...params: Array<any>) => Promise<any>;
|
export type ContractFunction = (...params: Array<any>) => Promise<any>;
|
||||||
export type ContractEvent = (...params: Array<any>) => void;
|
export type ContractEvent = (...params: Array<any>) => void;
|
||||||
@ -253,11 +258,14 @@ export class Contract {
|
|||||||
|
|
||||||
readonly addressPromise: Promise<string>;
|
readonly addressPromise: Promise<string>;
|
||||||
|
|
||||||
|
// This is only set if the contract was created with a call to deploy
|
||||||
|
readonly deployTransaction: TransactionResponse;
|
||||||
|
|
||||||
// https://github.com/Microsoft/TypeScript/issues/5453
|
// https://github.com/Microsoft/TypeScript/issues/5453
|
||||||
// Once this issue is resolved (there are open PR) we can do this nicer. :)
|
// Once this issue is resolved (there are open PR) we can do this nicer. :)
|
||||||
|
|
||||||
constructor(addressOrName: string, contractInterface: Contractish, signerOrProvider: any) {
|
constructor(addressOrName: string, contractInterface: Contractish, signerOrProvider: Signer | Provider) {
|
||||||
//if (!(this instanceof Contract)) { throw new Error('missing new'); }
|
errors.checkNew(this, Contract);
|
||||||
|
|
||||||
// @TODO: Maybe still check the addressOrName looks like a valid address or name?
|
// @TODO: Maybe still check the addressOrName looks like a valid address or name?
|
||||||
//address = getAddress(address);
|
//address = getAddress(address);
|
||||||
@ -269,28 +277,29 @@ export class Contract {
|
|||||||
|
|
||||||
if (!signerOrProvider) { throw new Error('missing signer or provider'); }
|
if (!signerOrProvider) { throw new Error('missing signer or provider'); }
|
||||||
|
|
||||||
var signer = signerOrProvider;
|
if (isSigner(signerOrProvider)) {
|
||||||
var provider = null;
|
defineReadOnly(this, 'provider', signerOrProvider.provider);
|
||||||
|
defineReadOnly(this, 'signer', signerOrProvider);
|
||||||
if (signerOrProvider.provider) {
|
|
||||||
provider = signerOrProvider.provider;
|
|
||||||
} else {
|
} else {
|
||||||
provider = signerOrProvider;
|
defineReadOnly(this, 'provider', signerOrProvider);
|
||||||
signer = null;
|
defineReadOnly(this, 'signer', null);
|
||||||
}
|
}
|
||||||
|
|
||||||
defineReadOnly(this, 'signer', signer);
|
|
||||||
defineReadOnly(this, 'provider', provider);
|
|
||||||
|
|
||||||
if (!addressOrName) { return; }
|
|
||||||
|
|
||||||
defineReadOnly(this, 'address', addressOrName);
|
|
||||||
defineReadOnly(this, 'addressPromise', provider.resolveName(addressOrName));
|
|
||||||
|
|
||||||
defineReadOnly(this, 'estimate', { });
|
defineReadOnly(this, 'estimate', { });
|
||||||
defineReadOnly(this, 'events', { });
|
defineReadOnly(this, 'events', { });
|
||||||
defineReadOnly(this, 'functions', { });
|
defineReadOnly(this, 'functions', { });
|
||||||
|
|
||||||
|
// Not connected to an on-chain instance, so do not connect functions and events
|
||||||
|
if (!addressOrName) {
|
||||||
|
defineReadOnly(this, 'address', null);
|
||||||
|
defineReadOnly(this, 'addressPromise', Promise.resolve(null));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
defineReadOnly(this, 'address', addressOrName || null);
|
||||||
|
defineReadOnly(this, 'addressPromise', this.provider.resolveName(addressOrName || null));
|
||||||
|
|
||||||
Object.keys(this.interface.functions).forEach((name) => {
|
Object.keys(this.interface.functions).forEach((name) => {
|
||||||
var run = runMethod(this, name, false);
|
var run = runMethod(this, name, false);
|
||||||
|
|
||||||
@ -311,9 +320,9 @@ export class Contract {
|
|||||||
|
|
||||||
let eventCallback = null;
|
let eventCallback = null;
|
||||||
|
|
||||||
let addressPromise = this.addressPromise;
|
let contract = this;
|
||||||
function handleEvent(log) {
|
function handleEvent(log) {
|
||||||
addressPromise.then((address) => {
|
contract.addressPromise.then((address) => {
|
||||||
// Not meant for us (the topics just has the same name)
|
// Not meant for us (the topics just has the same name)
|
||||||
if (address != log.address) { return; }
|
if (address != log.address) { return; }
|
||||||
|
|
||||||
@ -325,12 +334,12 @@ export class Contract {
|
|||||||
log.event = eventName;
|
log.event = eventName;
|
||||||
log.parse = eventInfo.parse;
|
log.parse = eventInfo.parse;
|
||||||
log.removeListener = function() {
|
log.removeListener = function() {
|
||||||
provider.removeListener(eventInfo.topics, handleEvent);
|
contract.provider.removeListener(eventInfo.topics, handleEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
log.getBlock = function() { return provider.getBlock(log.blockHash);; }
|
log.getBlock = function() { return contract.provider.getBlock(log.blockHash);; }
|
||||||
log.getTransaction = function() { return provider.getTransaction(log.transactionHash); }
|
log.getTransaction = function() { return contract.provider.getTransaction(log.transactionHash); }
|
||||||
log.getTransactionReceipt = function() { return provider.getTransactionReceipt(log.transactionHash); }
|
log.getTransactionReceipt = function() { return contract.provider.getTransactionReceipt(log.transactionHash); }
|
||||||
log.eventSignature = eventInfo.signature;
|
log.eventSignature = eventInfo.signature;
|
||||||
|
|
||||||
eventCallback.apply(log, Array.prototype.slice.call(result));
|
eventCallback.apply(log, Array.prototype.slice.call(result));
|
||||||
@ -349,10 +358,10 @@ export class Contract {
|
|||||||
if (!value) { value = null; }
|
if (!value) { value = null; }
|
||||||
|
|
||||||
if (!value && eventCallback) {
|
if (!value && eventCallback) {
|
||||||
provider.removeListener(eventInfo.topics, handleEvent);
|
contract.provider.removeListener(eventInfo.topics, handleEvent);
|
||||||
|
|
||||||
} else if (value && !eventCallback) {
|
} else if (value && !eventCallback) {
|
||||||
provider.on(eventInfo.topics, handleEvent);
|
contract.provider.on(eventInfo.topics, handleEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
eventCallback = value;
|
eventCallback = value;
|
||||||
@ -369,11 +378,15 @@ export class Contract {
|
|||||||
}, this);
|
}, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(signerOrProvider) {
|
// Reconnect to a different signer or provider
|
||||||
|
connect(signerOrProvider: Signer | Provider): Contract {
|
||||||
return new Contract(this.address, this.interface, signerOrProvider);
|
return new Contract(this.address, this.interface, signerOrProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
deploy(bytecode: string, ...args): Promise<TransactionResponse> {
|
// Deploy the contract with the bytecode, resolving to the deployed address.
|
||||||
|
// Use contract.deployTransaction.wait() to wait until the contract has
|
||||||
|
// been mined.
|
||||||
|
deploy(bytecode: string, ...args: Array<any>): Promise<Contract> {
|
||||||
if (this.signer == null) {
|
if (this.signer == null) {
|
||||||
throw new Error('missing signer'); // @TODO: errors.throwError
|
throw new Error('missing signer'); // @TODO: errors.throwError
|
||||||
}
|
}
|
||||||
@ -381,6 +394,10 @@ export class Contract {
|
|||||||
// @TODO: overrides of args.length = this.interface.deployFunction.inputs.length + 1
|
// @TODO: overrides of args.length = this.interface.deployFunction.inputs.length + 1
|
||||||
return this.signer.sendTransaction({
|
return this.signer.sendTransaction({
|
||||||
data: this.interface.deployFunction.encode(bytecode, args)
|
data: this.interface.deployFunction.encode(bytecode, args)
|
||||||
|
}).then((tx) => {
|
||||||
|
let contract = new Contract(getContractAddress(tx), this.interface, this.provider);
|
||||||
|
defineReadOnly(contract, 'deployTransaction', tx);
|
||||||
|
return contract;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -331,7 +331,7 @@ export class Interface {
|
|||||||
readonly deployFunction: DeployDescription;
|
readonly deployFunction: DeployDescription;
|
||||||
|
|
||||||
constructor(abi: Array<string | ParamType> | string) {
|
constructor(abi: Array<string | ParamType> | string) {
|
||||||
if (!(this instanceof Interface)) { throw new Error('missing new'); }
|
errors.checkNew(this, Interface);
|
||||||
|
|
||||||
if (typeof(abi) === 'string') {
|
if (typeof(abi) === 'string') {
|
||||||
try {
|
try {
|
||||||
|
@ -5,6 +5,7 @@ import { Network } from './networks';
|
|||||||
import { hexlify, hexStripZeros } from '../utils/convert';
|
import { hexlify, hexStripZeros } from '../utils/convert';
|
||||||
import { fetchJson } from '../utils/web';
|
import { fetchJson } from '../utils/web';
|
||||||
|
|
||||||
|
import * as errors from '../utils/errors';
|
||||||
|
|
||||||
// The transaction has already been sanitized by the calls in Provider
|
// The transaction has already been sanitized by the calls in Provider
|
||||||
function getTransactionString(transaction: TransactionRequest): string {
|
function getTransactionString(transaction: TransactionRequest): string {
|
||||||
@ -70,6 +71,7 @@ export class EtherscanProvider extends Provider{
|
|||||||
constructor(network?: Network | string, apiKey?: string) {
|
constructor(network?: Network | string, apiKey?: string) {
|
||||||
|
|
||||||
super(network || 'homestead');
|
super(network || 'homestead');
|
||||||
|
errors.checkNew(this, EtherscanProvider);
|
||||||
|
|
||||||
let name = 'invalid';
|
let name = 'invalid';
|
||||||
if (this.network) { name = this.network.name; }
|
if (this.network) { name = this.network.name; }
|
||||||
|
@ -46,15 +46,16 @@ export class FallbackProvider extends Provider {
|
|||||||
private _providers: Array<Provider>;
|
private _providers: Array<Provider>;
|
||||||
|
|
||||||
constructor(providers: Array<Provider>) {
|
constructor(providers: Array<Provider>) {
|
||||||
//if (!(this instanceof FallbackProvider)) { throw new Error('missing new'); }
|
|
||||||
|
|
||||||
if (providers.length === 0) { throw new Error('no providers'); }
|
if (providers.length === 0) { throw new Error('no providers'); }
|
||||||
|
|
||||||
let ready = checkNetworks(providers.map((p) => p.network));
|
let ready = checkNetworks(providers.map((p) => p.network));
|
||||||
if (ready) {
|
if (ready) {
|
||||||
super(providers[0].network);
|
super(providers[0].network);
|
||||||
|
errors.checkNew(this, FallbackProvider);
|
||||||
} else {
|
} else {
|
||||||
super(null);
|
super(null);
|
||||||
|
errors.checkNew(this, FallbackProvider);
|
||||||
|
|
||||||
// We re-assign the ready function to make sure all networks actually match
|
// We re-assign the ready function to make sure all networks actually match
|
||||||
this.ready = Promise.all(providers.map((p) => p.getNetwork())).then((networks) => {
|
this.ready = Promise.all(providers.map((p) => p.getNetwork())).then((networks) => {
|
||||||
@ -65,6 +66,7 @@ export class FallbackProvider extends Provider {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
this._providers = providers.slice(0);
|
this._providers = providers.slice(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,6 @@ export class InfuraProvider extends JsonRpcProvider {
|
|||||||
readonly apiAccessToken: string;
|
readonly apiAccessToken: string;
|
||||||
|
|
||||||
constructor(network?: Network | string, apiAccessToken?: string) {
|
constructor(network?: Network | string, apiAccessToken?: string) {
|
||||||
//errors.checkNew(this, InfuraProvider);
|
|
||||||
|
|
||||||
network = getNetwork(network || 'homestead');
|
network = getNetwork(network || 'homestead');
|
||||||
|
|
||||||
@ -32,6 +31,8 @@ export class InfuraProvider extends JsonRpcProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
super('https://' + host + '/' + (apiAccessToken || ''), network);
|
super('https://' + host + '/' + (apiAccessToken || ''), network);
|
||||||
|
errors.checkNew(this, InfuraProvider);
|
||||||
|
|
||||||
this.apiAccessToken = (apiAccessToken || null);
|
this.apiAccessToken = (apiAccessToken || null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,13 +9,13 @@ import * as errors from '../utils/errors';
|
|||||||
export class IpcProvider extends JsonRpcProvider {
|
export class IpcProvider extends JsonRpcProvider {
|
||||||
readonly path: string;
|
readonly path: string;
|
||||||
constructor(path: string, network?: Network | string) {
|
constructor(path: string, network?: Network | string) {
|
||||||
//errors.checkNew(this, IpcProvider);
|
|
||||||
|
|
||||||
if (path == null) {
|
if (path == null) {
|
||||||
errors.throwError('missing path', errors.MISSING_ARGUMENT, { arg: 'path' });
|
errors.throwError('missing path', errors.MISSING_ARGUMENT, { arg: 'path' });
|
||||||
}
|
}
|
||||||
|
|
||||||
super('ipc://' + path, network);
|
super('ipc://' + path, network);
|
||||||
|
errors.checkNew(this, IpcProvider);
|
||||||
|
|
||||||
this.path = path;
|
this.path = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ export class JsonRpcSigner {
|
|||||||
// private _syncAddress: boolean;
|
// private _syncAddress: boolean;
|
||||||
|
|
||||||
constructor(provider: JsonRpcProvider, address?: string) {
|
constructor(provider: JsonRpcProvider, address?: string) {
|
||||||
//errors.checkNew(this, JsonRpcSigner);
|
errors.checkNew(this, JsonRpcSigner);
|
||||||
|
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
|
|
||||||
@ -161,7 +161,6 @@ export class JsonRpcProvider extends Provider {
|
|||||||
private _pendingFilter: Promise<number>;
|
private _pendingFilter: Promise<number>;
|
||||||
|
|
||||||
constructor(url?: ConnectionInfo | string, network?: Network | string) {
|
constructor(url?: ConnectionInfo | string, network?: Network | string) {
|
||||||
//errors.checkNew(this, JsonRpcProvider);
|
|
||||||
|
|
||||||
// One parameter, but it is a network name, so swap it with the URL
|
// One parameter, but it is a network name, so swap it with the URL
|
||||||
if (typeof(url) === 'string') {
|
if (typeof(url) === 'string') {
|
||||||
@ -172,6 +171,7 @@ export class JsonRpcProvider extends Provider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
super(network);
|
super(network);
|
||||||
|
errors.checkNew(this, JsonRpcProvider);
|
||||||
|
|
||||||
// Default URL
|
// Default URL
|
||||||
if (!url) { url = 'http://localhost:8545'; }
|
if (!url) { url = 'http://localhost:8545'; }
|
||||||
|
@ -2,11 +2,8 @@
|
|||||||
|
|
||||||
//import inherits = require('inherits');
|
//import inherits = require('inherits');
|
||||||
|
|
||||||
//import networks = require('./networks.json');
|
import { getAddress, getContractAddress } from '../utils/address';
|
||||||
|
|
||||||
import { getAddress } from '../utils/address';
|
|
||||||
import { BigNumber, bigNumberify, BigNumberish } from '../utils/bignumber';
|
import { BigNumber, bigNumberify, BigNumberish } from '../utils/bignumber';
|
||||||
import { getContractAddress } from '../utils/contract-address';
|
|
||||||
import { Arrayish, hexlify, hexStripZeros, isHexString, stripZeros } from '../utils/convert';
|
import { Arrayish, hexlify, hexStripZeros, isHexString, stripZeros } from '../utils/convert';
|
||||||
import { toUtf8String } from '../utils/utf8';
|
import { toUtf8String } from '../utils/utf8';
|
||||||
import { decode as rlpDecode, encode as rlpEncode } from '../utils/rlp';
|
import { decode as rlpDecode, encode as rlpEncode } from '../utils/rlp';
|
||||||
@ -604,7 +601,7 @@ export class Provider {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
constructor(network: string | Network) {
|
constructor(network: string | Network) {
|
||||||
//if (!(this instanceof Provider)) { throw new Error('missing new'); }
|
errors.checkNew(this, Provider);
|
||||||
|
|
||||||
network = getNetwork(network);
|
network = getNetwork(network);
|
||||||
|
|
||||||
@ -803,7 +800,7 @@ export class Provider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
getBalance(addressOrName: string | Promise<string>, blockTag: BlockTag | Promise<BlockTag>): Promise<BigNumber> {
|
getBalance(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<BigNumber> {
|
||||||
return this.ready.then(() => {
|
return this.ready.then(() => {
|
||||||
return resolveProperties({ addressOrName: addressOrName, blockTag: blockTag }).then(({ addressOrName, blockTag }) => {
|
return resolveProperties({ addressOrName: addressOrName, blockTag: blockTag }).then(({ addressOrName, blockTag }) => {
|
||||||
return this.resolveName(addressOrName).then((address) => {
|
return this.resolveName(addressOrName).then((address) => {
|
||||||
@ -816,7 +813,7 @@ export class Provider {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getTransactionCount(addressOrName: string | Promise<string>, blockTag: BlockTag | Promise<BlockTag>): Promise<number> {
|
getTransactionCount(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<number> {
|
||||||
return this.ready.then(() => {
|
return this.ready.then(() => {
|
||||||
return resolveProperties({ addressOrName: addressOrName, blockTag: blockTag }).then(({ addressOrName, blockTag }) => {
|
return resolveProperties({ addressOrName: addressOrName, blockTag: blockTag }).then(({ addressOrName, blockTag }) => {
|
||||||
return this.resolveName(addressOrName).then((address) => {
|
return this.resolveName(addressOrName).then((address) => {
|
||||||
@ -831,7 +828,7 @@ export class Provider {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getCode(addressOrName: string | Promise<string>, blockTag: BlockTag | Promise<BlockTag>): Promise<string> {
|
getCode(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
|
||||||
return this.ready.then(() => {
|
return this.ready.then(() => {
|
||||||
return resolveProperties({ addressOrName: addressOrName, blockTag: blockTag }).then(({ addressOrName, blockTag }) => {
|
return resolveProperties({ addressOrName: addressOrName, blockTag: blockTag }).then(({ addressOrName, blockTag }) => {
|
||||||
return this.resolveName(addressOrName).then((address) => {
|
return this.resolveName(addressOrName).then((address) => {
|
||||||
@ -844,7 +841,7 @@ export class Provider {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag: BlockTag | Promise<BlockTag>): Promise<string> {
|
getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string> {
|
||||||
return this.ready.then(() => {
|
return this.ready.then(() => {
|
||||||
return resolveProperties({ addressOrName: addressOrName, position: position, blockTag: blockTag }).then(({ addressOrName, position, blockTag }) => {
|
return resolveProperties({ addressOrName: addressOrName, position: position, blockTag: blockTag }).then(({ addressOrName, position, blockTag }) => {
|
||||||
return this.resolveName(addressOrName).then((address) => {
|
return this.resolveName(addressOrName).then((address) => {
|
||||||
|
@ -24,7 +24,6 @@ export class Web3Provider extends JsonRpcProvider {
|
|||||||
readonly _web3Provider: AsyncProvider;
|
readonly _web3Provider: AsyncProvider;
|
||||||
|
|
||||||
constructor(web3Provider: AsyncProvider, network?: Network | string) {
|
constructor(web3Provider: AsyncProvider, network?: Network | string) {
|
||||||
//errors.checkNew(this, Web3Provider);
|
|
||||||
|
|
||||||
if (!web3Provider || !web3Provider.sendAsync) {
|
if (!web3Provider || !web3Provider.sendAsync) {
|
||||||
errors.throwError(
|
errors.throwError(
|
||||||
@ -38,6 +37,8 @@ export class Web3Provider extends JsonRpcProvider {
|
|||||||
var url = web3Provider.host || web3Provider.path || 'unknown';
|
var url = web3Provider.host || web3Provider.path || 'unknown';
|
||||||
|
|
||||||
super(url, network);
|
super(url, network);
|
||||||
|
errors.checkNew(this, Web3Provider);
|
||||||
|
|
||||||
this._web3Provider = web3Provider;
|
this._web3Provider = web3Provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
// We use this for base 36 maths
|
||||||
import BN = require('bn.js');
|
import BN = require('bn.js');
|
||||||
|
|
||||||
import { arrayify } from './convert';
|
import { BigNumber } from './bignumber';
|
||||||
|
import { arrayify, Arrayish, stripZeros, hexlify } from './convert';
|
||||||
import { keccak256 } from './keccak256';
|
import { keccak256 } from './keccak256';
|
||||||
|
import { encode } from './rlp';
|
||||||
|
|
||||||
import errors = require('./errors');
|
import errors = require('./errors');
|
||||||
|
|
||||||
|
|
||||||
function getChecksumAddress(address: string): string {
|
function getChecksumAddress(address: string): string {
|
||||||
if (typeof(address) !== 'string' || !address.match(/^0x[0-9A-Fa-f]{40}$/)) {
|
if (typeof(address) !== 'string' || !address.match(/^0x[0-9A-Fa-f]{40}$/)) {
|
||||||
errors.throwError('invalid address', errors.INVALID_ARGUMENT, { arg: 'address', value: address });
|
errors.throwError('invalid address', errors.INVALID_ARGUMENT, { arg: 'address', value: address });
|
||||||
@ -118,3 +122,14 @@ export function getAddress(address: string, icapFormat?: boolean): string {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
|
||||||
|
export function getContractAddress(transaction: { from: string, nonce: Arrayish | BigNumber | number }) {
|
||||||
|
if (!transaction.from) { throw new Error('missing from address'); }
|
||||||
|
var nonce = transaction.nonce;
|
||||||
|
|
||||||
|
return getAddress('0x' + keccak256(encode([
|
||||||
|
getAddress(transaction.from),
|
||||||
|
stripZeros(hexlify(nonce))
|
||||||
|
])).substring(26));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
import { getAddress } from './address';
|
|
||||||
import { BigNumber } from './bignumber';
|
|
||||||
import { Arrayish, stripZeros, hexlify } from './convert';
|
|
||||||
import { keccak256 } from './keccak256';
|
|
||||||
import { encode } from './rlp';
|
|
||||||
|
|
||||||
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
|
|
||||||
export function getContractAddress(transaction: { from: string, nonce: Arrayish | BigNumber | number }) {
|
|
||||||
if (!transaction.from) { throw new Error('missing from address'); }
|
|
||||||
var nonce = transaction.nonce;
|
|
||||||
|
|
||||||
return getAddress('0x' + keccak256(encode([
|
|
||||||
getAddress(transaction.from),
|
|
||||||
stripZeros(hexlify(nonce))
|
|
||||||
])).substring(26));
|
|
||||||
}
|
|
@ -3,11 +3,10 @@
|
|||||||
// This is SUPER useful, but adds 140kb (even zipped, adds 40kb)
|
// This is SUPER useful, but adds 140kb (even zipped, adds 40kb)
|
||||||
//var unorm = require('unorm');
|
//var unorm = require('unorm');
|
||||||
|
|
||||||
import { getAddress } from './address';
|
import { getAddress, getContractAddress } from './address';
|
||||||
import { AbiCoder, defaultAbiCoder, parseSignature } from './abi-coder';
|
import { AbiCoder, defaultAbiCoder, parseSignature } from './abi-coder';
|
||||||
import * as base64 from './base64';
|
import * as base64 from './base64';
|
||||||
import * as bigNumber from './bignumber';
|
import * as bigNumber from './bignumber';
|
||||||
import { getContractAddress } from './contract-address';
|
|
||||||
import * as convert from './convert';
|
import * as convert from './convert';
|
||||||
import { id } from './id';
|
import { id } from './id';
|
||||||
import { keccak256 } from './keccak256';
|
import { keccak256 } from './keccak256';
|
||||||
|
@ -15,6 +15,8 @@ import { pbkdf2 } from '../utils/pbkdf2';
|
|||||||
import { createSha512Hmac } from '../utils/hmac';
|
import { createSha512Hmac } from '../utils/hmac';
|
||||||
import { sha256 } from '../utils/sha2';
|
import { sha256 } from '../utils/sha2';
|
||||||
|
|
||||||
|
import * as errors from '../utils/errors';
|
||||||
|
|
||||||
// "Bitcoin seed"
|
// "Bitcoin seed"
|
||||||
var MasterSecret = toUtf8Bytes('Bitcoin seed');
|
var MasterSecret = toUtf8Bytes('Bitcoin seed');
|
||||||
|
|
||||||
@ -46,7 +48,7 @@ export class HDNode {
|
|||||||
|
|
||||||
// @TODO: Private constructor?
|
// @TODO: Private constructor?
|
||||||
constructor(keyPair: secp256k1.KeyPair, chainCode: Uint8Array, index: number, depth: number, mnemonic: string, path: string) {
|
constructor(keyPair: secp256k1.KeyPair, chainCode: Uint8Array, index: number, depth: number, mnemonic: string, path: string) {
|
||||||
//if (!(this instanceof HDNode)) { throw new Error('missing new'); }
|
errors.checkNew(this, HDNode);
|
||||||
|
|
||||||
this.keyPair = keyPair;
|
this.keyPair = keyPair;
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ export class SigningKey {
|
|||||||
private readonly keyPair: secp256k1.KeyPair;
|
private readonly keyPair: secp256k1.KeyPair;
|
||||||
|
|
||||||
constructor(privateKey: any) {
|
constructor(privateKey: any) {
|
||||||
//errors.checkNew(this, SigningKey);
|
errors.checkNew(this, SigningKey);
|
||||||
|
|
||||||
if (privateKey.privateKey) {
|
if (privateKey.privateKey) {
|
||||||
this.mnemonic = privateKey.mnemonic;
|
this.mnemonic = privateKey.mnemonic;
|
||||||
|
19
src/contracts/contract.d.ts
vendored
19
src/contracts/contract.d.ts
vendored
@ -1,20 +1,12 @@
|
|||||||
import { Interface } from './interface';
|
import { Interface } from './interface';
|
||||||
import { TransactionResponse } from '../providers/provider';
|
import { Provider, TransactionResponse } from '../providers/provider';
|
||||||
import { Network } from '../providers/networks';
|
|
||||||
import { ParamType } from '../utils/abi-coder';
|
import { ParamType } from '../utils/abi-coder';
|
||||||
import { BigNumber } from '../utils/bignumber';
|
import { BigNumber } from '../utils/bignumber';
|
||||||
interface Provider {
|
|
||||||
getNetwork(): Promise<Network>;
|
|
||||||
getGasPrice(): Promise<BigNumber>;
|
|
||||||
getTransactionCount(address: string | Promise<string>): Promise<number>;
|
|
||||||
call(data: string): Promise<string>;
|
|
||||||
estimateGas(tx: any): Promise<BigNumber>;
|
|
||||||
sendTransaction(signedTransaction: string | Promise<string>): Promise<TransactionResponse>;
|
|
||||||
}
|
|
||||||
interface Signer {
|
interface Signer {
|
||||||
defaultGasLimit?: BigNumber;
|
defaultGasLimit?: BigNumber;
|
||||||
defaultGasPrice?: BigNumber;
|
defaultGasPrice?: BigNumber;
|
||||||
address?: string;
|
address?: string;
|
||||||
|
provider?: Provider;
|
||||||
getAddress(): Promise<string>;
|
getAddress(): Promise<string>;
|
||||||
getTransactionCount(): Promise<number>;
|
getTransactionCount(): Promise<number>;
|
||||||
estimateGas(tx: any): Promise<BigNumber>;
|
estimateGas(tx: any): Promise<BigNumber>;
|
||||||
@ -37,8 +29,9 @@ export declare class Contract {
|
|||||||
readonly functions: Bucket<ContractFunction>;
|
readonly functions: Bucket<ContractFunction>;
|
||||||
readonly events: Bucket<ContractEvent>;
|
readonly events: Bucket<ContractEvent>;
|
||||||
readonly addressPromise: Promise<string>;
|
readonly addressPromise: Promise<string>;
|
||||||
constructor(addressOrName: string, contractInterface: Contractish, signerOrProvider: any);
|
readonly deployTransaction: TransactionResponse;
|
||||||
connect(signerOrProvider: any): Contract;
|
constructor(addressOrName: string, contractInterface: Contractish, signerOrProvider: Signer | Provider);
|
||||||
deploy(bytecode: string, ...args: any[]): Promise<TransactionResponse>;
|
connect(signerOrProvider: Signer | Provider): Contract;
|
||||||
|
deploy(bytecode: string, ...args: Array<any>): Promise<Contract>;
|
||||||
}
|
}
|
||||||
export {};
|
export {};
|
||||||
|
@ -8,6 +8,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|||||||
};
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
var interface_1 = require("./interface");
|
var interface_1 = require("./interface");
|
||||||
|
var address_1 = require("../utils/address");
|
||||||
var bignumber_1 = require("../utils/bignumber");
|
var bignumber_1 = require("../utils/bignumber");
|
||||||
var properties_1 = require("../utils/properties");
|
var properties_1 = require("../utils/properties");
|
||||||
var errors = __importStar(require("../utils/errors"));
|
var errors = __importStar(require("../utils/errors"));
|
||||||
@ -193,12 +194,15 @@ function runMethod(contract, functionName, estimateOnly) {
|
|||||||
throw new Error('unsupport type - ' + method.type);
|
throw new Error('unsupport type - ' + method.type);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
function isSigner(value) {
|
||||||
|
return (value && value.provider != null);
|
||||||
|
}
|
||||||
var Contract = /** @class */ (function () {
|
var Contract = /** @class */ (function () {
|
||||||
// https://github.com/Microsoft/TypeScript/issues/5453
|
// https://github.com/Microsoft/TypeScript/issues/5453
|
||||||
// Once this issue is resolved (there are open PR) we can do this nicer. :)
|
// Once this issue is resolved (there are open PR) we can do this nicer. :)
|
||||||
function Contract(addressOrName, contractInterface, signerOrProvider) {
|
function Contract(addressOrName, contractInterface, signerOrProvider) {
|
||||||
//if (!(this instanceof Contract)) { throw new Error('missing new'); }
|
|
||||||
var _this = this;
|
var _this = this;
|
||||||
|
errors.checkNew(this, Contract);
|
||||||
// @TODO: Maybe still check the addressOrName looks like a valid address or name?
|
// @TODO: Maybe still check the addressOrName looks like a valid address or name?
|
||||||
//address = getAddress(address);
|
//address = getAddress(address);
|
||||||
if (contractInterface instanceof interface_1.Interface) {
|
if (contractInterface instanceof interface_1.Interface) {
|
||||||
@ -210,25 +214,25 @@ var Contract = /** @class */ (function () {
|
|||||||
if (!signerOrProvider) {
|
if (!signerOrProvider) {
|
||||||
throw new Error('missing signer or provider');
|
throw new Error('missing signer or provider');
|
||||||
}
|
}
|
||||||
var signer = signerOrProvider;
|
if (isSigner(signerOrProvider)) {
|
||||||
var provider = null;
|
properties_1.defineReadOnly(this, 'provider', signerOrProvider.provider);
|
||||||
if (signerOrProvider.provider) {
|
properties_1.defineReadOnly(this, 'signer', signerOrProvider);
|
||||||
provider = signerOrProvider.provider;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
provider = signerOrProvider;
|
properties_1.defineReadOnly(this, 'provider', signerOrProvider);
|
||||||
signer = null;
|
properties_1.defineReadOnly(this, 'signer', null);
|
||||||
}
|
}
|
||||||
properties_1.defineReadOnly(this, 'signer', signer);
|
|
||||||
properties_1.defineReadOnly(this, 'provider', provider);
|
|
||||||
if (!addressOrName) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
properties_1.defineReadOnly(this, 'address', addressOrName);
|
|
||||||
properties_1.defineReadOnly(this, 'addressPromise', provider.resolveName(addressOrName));
|
|
||||||
properties_1.defineReadOnly(this, 'estimate', {});
|
properties_1.defineReadOnly(this, 'estimate', {});
|
||||||
properties_1.defineReadOnly(this, 'events', {});
|
properties_1.defineReadOnly(this, 'events', {});
|
||||||
properties_1.defineReadOnly(this, 'functions', {});
|
properties_1.defineReadOnly(this, 'functions', {});
|
||||||
|
// Not connected to an on-chain instance, so do not connect functions and events
|
||||||
|
if (!addressOrName) {
|
||||||
|
properties_1.defineReadOnly(this, 'address', null);
|
||||||
|
properties_1.defineReadOnly(this, 'addressPromise', Promise.resolve(null));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
properties_1.defineReadOnly(this, 'address', addressOrName || null);
|
||||||
|
properties_1.defineReadOnly(this, 'addressPromise', this.provider.resolveName(addressOrName || null));
|
||||||
Object.keys(this.interface.functions).forEach(function (name) {
|
Object.keys(this.interface.functions).forEach(function (name) {
|
||||||
var run = runMethod(_this, name, false);
|
var run = runMethod(_this, name, false);
|
||||||
if (_this[name] == null) {
|
if (_this[name] == null) {
|
||||||
@ -245,9 +249,9 @@ var Contract = /** @class */ (function () {
|
|||||||
Object.keys(this.interface.events).forEach(function (eventName) {
|
Object.keys(this.interface.events).forEach(function (eventName) {
|
||||||
var eventInfo = _this.interface.events[eventName];
|
var eventInfo = _this.interface.events[eventName];
|
||||||
var eventCallback = null;
|
var eventCallback = null;
|
||||||
var addressPromise = _this.addressPromise;
|
var contract = _this;
|
||||||
function handleEvent(log) {
|
function handleEvent(log) {
|
||||||
addressPromise.then(function (address) {
|
contract.addressPromise.then(function (address) {
|
||||||
// Not meant for us (the topics just has the same name)
|
// Not meant for us (the topics just has the same name)
|
||||||
if (address != log.address) {
|
if (address != log.address) {
|
||||||
return;
|
return;
|
||||||
@ -259,11 +263,11 @@ var Contract = /** @class */ (function () {
|
|||||||
log.event = eventName;
|
log.event = eventName;
|
||||||
log.parse = eventInfo.parse;
|
log.parse = eventInfo.parse;
|
||||||
log.removeListener = function () {
|
log.removeListener = function () {
|
||||||
provider.removeListener(eventInfo.topics, handleEvent);
|
contract.provider.removeListener(eventInfo.topics, handleEvent);
|
||||||
};
|
};
|
||||||
log.getBlock = function () { return provider.getBlock(log.blockHash); ; };
|
log.getBlock = function () { return contract.provider.getBlock(log.blockHash); ; };
|
||||||
log.getTransaction = function () { return provider.getTransaction(log.transactionHash); };
|
log.getTransaction = function () { return contract.provider.getTransaction(log.transactionHash); };
|
||||||
log.getTransactionReceipt = function () { return provider.getTransactionReceipt(log.transactionHash); };
|
log.getTransactionReceipt = function () { return contract.provider.getTransactionReceipt(log.transactionHash); };
|
||||||
log.eventSignature = eventInfo.signature;
|
log.eventSignature = eventInfo.signature;
|
||||||
eventCallback.apply(log, Array.prototype.slice.call(result));
|
eventCallback.apply(log, Array.prototype.slice.call(result));
|
||||||
}
|
}
|
||||||
@ -282,10 +286,10 @@ var Contract = /** @class */ (function () {
|
|||||||
value = null;
|
value = null;
|
||||||
}
|
}
|
||||||
if (!value && eventCallback) {
|
if (!value && eventCallback) {
|
||||||
provider.removeListener(eventInfo.topics, handleEvent);
|
contract.provider.removeListener(eventInfo.topics, handleEvent);
|
||||||
}
|
}
|
||||||
else if (value && !eventCallback) {
|
else if (value && !eventCallback) {
|
||||||
provider.on(eventInfo.topics, handleEvent);
|
contract.provider.on(eventInfo.topics, handleEvent);
|
||||||
}
|
}
|
||||||
eventCallback = value;
|
eventCallback = value;
|
||||||
}
|
}
|
||||||
@ -297,10 +301,15 @@ var Contract = /** @class */ (function () {
|
|||||||
Object.defineProperty(_this.events, eventName, property);
|
Object.defineProperty(_this.events, eventName, property);
|
||||||
}, this);
|
}, this);
|
||||||
}
|
}
|
||||||
|
// Reconnect to a different signer or provider
|
||||||
Contract.prototype.connect = function (signerOrProvider) {
|
Contract.prototype.connect = function (signerOrProvider) {
|
||||||
return new Contract(this.address, this.interface, signerOrProvider);
|
return new Contract(this.address, this.interface, signerOrProvider);
|
||||||
};
|
};
|
||||||
|
// Deploy the contract with the bytecode, resolving to the deployed address.
|
||||||
|
// Use contract.deployTransaction.wait() to wait until the contract has
|
||||||
|
// been mined.
|
||||||
Contract.prototype.deploy = function (bytecode) {
|
Contract.prototype.deploy = function (bytecode) {
|
||||||
|
var _this = this;
|
||||||
var args = [];
|
var args = [];
|
||||||
for (var _i = 1; _i < arguments.length; _i++) {
|
for (var _i = 1; _i < arguments.length; _i++) {
|
||||||
args[_i - 1] = arguments[_i];
|
args[_i - 1] = arguments[_i];
|
||||||
@ -311,6 +320,10 @@ var Contract = /** @class */ (function () {
|
|||||||
// @TODO: overrides of args.length = this.interface.deployFunction.inputs.length + 1
|
// @TODO: overrides of args.length = this.interface.deployFunction.inputs.length + 1
|
||||||
return this.signer.sendTransaction({
|
return this.signer.sendTransaction({
|
||||||
data: this.interface.deployFunction.encode(bytecode, args)
|
data: this.interface.deployFunction.encode(bytecode, args)
|
||||||
|
}).then(function (tx) {
|
||||||
|
var contract = new Contract(address_1.getContractAddress(tx), _this.interface, _this.provider);
|
||||||
|
properties_1.defineReadOnly(contract, 'deployTransaction', tx);
|
||||||
|
return contract;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
return Contract;
|
return Contract;
|
||||||
|
@ -303,9 +303,7 @@ function addMethod(method) {
|
|||||||
var Interface = /** @class */ (function () {
|
var Interface = /** @class */ (function () {
|
||||||
function Interface(abi) {
|
function Interface(abi) {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
if (!(this instanceof Interface)) {
|
errors.checkNew(this, Interface);
|
||||||
throw new Error('missing new');
|
|
||||||
}
|
|
||||||
if (typeof (abi) === 'string') {
|
if (typeof (abi) === 'string') {
|
||||||
try {
|
try {
|
||||||
abi = JSON.parse(abi);
|
abi = JSON.parse(abi);
|
||||||
|
@ -9,10 +9,18 @@ var __extends = (this && this.__extends) || (function () {
|
|||||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
var __importStar = (this && this.__importStar) || function (mod) {
|
||||||
|
if (mod && mod.__esModule) return mod;
|
||||||
|
var result = {};
|
||||||
|
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
||||||
|
result["default"] = mod;
|
||||||
|
return result;
|
||||||
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
var provider_1 = require("./provider");
|
var provider_1 = require("./provider");
|
||||||
var convert_1 = require("../utils/convert");
|
var convert_1 = require("../utils/convert");
|
||||||
var web_1 = require("../utils/web");
|
var web_1 = require("../utils/web");
|
||||||
|
var errors = __importStar(require("../utils/errors"));
|
||||||
// The transaction has already been sanitized by the calls in Provider
|
// The transaction has already been sanitized by the calls in Provider
|
||||||
function getTransactionString(transaction) {
|
function getTransactionString(transaction) {
|
||||||
var result = [];
|
var result = [];
|
||||||
@ -75,6 +83,7 @@ var EtherscanProvider = /** @class */ (function (_super) {
|
|||||||
__extends(EtherscanProvider, _super);
|
__extends(EtherscanProvider, _super);
|
||||||
function EtherscanProvider(network, apiKey) {
|
function EtherscanProvider(network, apiKey) {
|
||||||
var _this = _super.call(this, network || 'homestead') || this;
|
var _this = _super.call(this, network || 'homestead') || this;
|
||||||
|
errors.checkNew(_this, EtherscanProvider);
|
||||||
var name = 'invalid';
|
var name = 'invalid';
|
||||||
if (_this.network) {
|
if (_this.network) {
|
||||||
name = _this.network.name;
|
name = _this.network.name;
|
||||||
|
@ -50,7 +50,6 @@ function checkNetworks(networks) {
|
|||||||
var FallbackProvider = /** @class */ (function (_super) {
|
var FallbackProvider = /** @class */ (function (_super) {
|
||||||
__extends(FallbackProvider, _super);
|
__extends(FallbackProvider, _super);
|
||||||
function FallbackProvider(providers) {
|
function FallbackProvider(providers) {
|
||||||
//if (!(this instanceof FallbackProvider)) { throw new Error('missing new'); }
|
|
||||||
var _this = this;
|
var _this = this;
|
||||||
if (providers.length === 0) {
|
if (providers.length === 0) {
|
||||||
throw new Error('no providers');
|
throw new Error('no providers');
|
||||||
@ -58,9 +57,11 @@ var FallbackProvider = /** @class */ (function (_super) {
|
|||||||
var ready = checkNetworks(providers.map(function (p) { return p.network; }));
|
var ready = checkNetworks(providers.map(function (p) { return p.network; }));
|
||||||
if (ready) {
|
if (ready) {
|
||||||
_this = _super.call(this, providers[0].network) || this;
|
_this = _super.call(this, providers[0].network) || this;
|
||||||
|
errors.checkNew(_this, FallbackProvider);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_this = _super.call(this, null) || this;
|
_this = _super.call(this, null) || this;
|
||||||
|
errors.checkNew(_this, FallbackProvider);
|
||||||
// We re-assign the ready function to make sure all networks actually match
|
// We re-assign the ready function to make sure all networks actually match
|
||||||
_this.ready = Promise.all(providers.map(function (p) { return p.getNetwork(); })).then(function (networks) {
|
_this.ready = Promise.all(providers.map(function (p) { return p.getNetwork(); })).then(function (networks) {
|
||||||
if (!checkNetworks(networks)) {
|
if (!checkNetworks(networks)) {
|
||||||
|
@ -23,7 +23,6 @@ var errors = __importStar(require("../utils/errors"));
|
|||||||
var InfuraProvider = /** @class */ (function (_super) {
|
var InfuraProvider = /** @class */ (function (_super) {
|
||||||
__extends(InfuraProvider, _super);
|
__extends(InfuraProvider, _super);
|
||||||
function InfuraProvider(network, apiAccessToken) {
|
function InfuraProvider(network, apiAccessToken) {
|
||||||
//errors.checkNew(this, InfuraProvider);
|
|
||||||
var _this = this;
|
var _this = this;
|
||||||
network = networks_1.getNetwork(network || 'homestead');
|
network = networks_1.getNetwork(network || 'homestead');
|
||||||
var host = null;
|
var host = null;
|
||||||
@ -44,6 +43,7 @@ var InfuraProvider = /** @class */ (function (_super) {
|
|||||||
throw new Error('unsupported network');
|
throw new Error('unsupported network');
|
||||||
}
|
}
|
||||||
_this = _super.call(this, 'https://' + host + '/' + (apiAccessToken || ''), network) || this;
|
_this = _super.call(this, 'https://' + host + '/' + (apiAccessToken || ''), network) || this;
|
||||||
|
errors.checkNew(_this, InfuraProvider);
|
||||||
_this.apiAccessToken = (apiAccessToken || null);
|
_this.apiAccessToken = (apiAccessToken || null);
|
||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
|
@ -26,12 +26,12 @@ var errors = __importStar(require("../utils/errors"));
|
|||||||
var IpcProvider = /** @class */ (function (_super) {
|
var IpcProvider = /** @class */ (function (_super) {
|
||||||
__extends(IpcProvider, _super);
|
__extends(IpcProvider, _super);
|
||||||
function IpcProvider(path, network) {
|
function IpcProvider(path, network) {
|
||||||
//errors.checkNew(this, IpcProvider);
|
|
||||||
var _this = this;
|
var _this = this;
|
||||||
if (path == null) {
|
if (path == null) {
|
||||||
errors.throwError('missing path', errors.MISSING_ARGUMENT, { arg: 'path' });
|
errors.throwError('missing path', errors.MISSING_ARGUMENT, { arg: 'path' });
|
||||||
}
|
}
|
||||||
_this = _super.call(this, 'ipc://' + path, network) || this;
|
_this = _super.call(this, 'ipc://' + path, network) || this;
|
||||||
|
errors.checkNew(_this, IpcProvider);
|
||||||
_this.path = path;
|
_this.path = path;
|
||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ function getLowerCase(value) {
|
|||||||
var JsonRpcSigner = /** @class */ (function () {
|
var JsonRpcSigner = /** @class */ (function () {
|
||||||
// private _syncAddress: boolean;
|
// private _syncAddress: boolean;
|
||||||
function JsonRpcSigner(provider, address) {
|
function JsonRpcSigner(provider, address) {
|
||||||
//errors.checkNew(this, JsonRpcSigner);
|
errors.checkNew(this, JsonRpcSigner);
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
// Statically attach to a given address
|
// Statically attach to a given address
|
||||||
if (address) {
|
if (address) {
|
||||||
@ -165,7 +165,6 @@ exports.JsonRpcSigner = JsonRpcSigner;
|
|||||||
var JsonRpcProvider = /** @class */ (function (_super) {
|
var JsonRpcProvider = /** @class */ (function (_super) {
|
||||||
__extends(JsonRpcProvider, _super);
|
__extends(JsonRpcProvider, _super);
|
||||||
function JsonRpcProvider(url, network) {
|
function JsonRpcProvider(url, network) {
|
||||||
//errors.checkNew(this, JsonRpcProvider);
|
|
||||||
var _this = this;
|
var _this = this;
|
||||||
// One parameter, but it is a network name, so swap it with the URL
|
// One parameter, but it is a network name, so swap it with the URL
|
||||||
if (typeof (url) === 'string') {
|
if (typeof (url) === 'string') {
|
||||||
@ -175,6 +174,7 @@ var JsonRpcProvider = /** @class */ (function (_super) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_this = _super.call(this, network) || this;
|
_this = _super.call(this, network) || this;
|
||||||
|
errors.checkNew(_this, JsonRpcProvider);
|
||||||
// Default URL
|
// Default URL
|
||||||
if (!url) {
|
if (!url) {
|
||||||
url = 'http://localhost:8545';
|
url = 'http://localhost:8545';
|
||||||
|
8
src/providers/provider.d.ts
vendored
8
src/providers/provider.d.ts
vendored
@ -98,10 +98,10 @@ export declare class Provider {
|
|||||||
waitForTransaction(transactionHash: string, timeout?: number): Promise<TransactionResponse>;
|
waitForTransaction(transactionHash: string, timeout?: number): Promise<TransactionResponse>;
|
||||||
getBlockNumber(): Promise<number>;
|
getBlockNumber(): Promise<number>;
|
||||||
getGasPrice(): Promise<BigNumber>;
|
getGasPrice(): Promise<BigNumber>;
|
||||||
getBalance(addressOrName: string | Promise<string>, blockTag: BlockTag | Promise<BlockTag>): Promise<BigNumber>;
|
getBalance(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<BigNumber>;
|
||||||
getTransactionCount(addressOrName: string | Promise<string>, blockTag: BlockTag | Promise<BlockTag>): Promise<number>;
|
getTransactionCount(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<number>;
|
||||||
getCode(addressOrName: string | Promise<string>, blockTag: BlockTag | Promise<BlockTag>): Promise<string>;
|
getCode(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string>;
|
||||||
getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag: BlockTag | Promise<BlockTag>): Promise<string>;
|
getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag?: BlockTag | Promise<BlockTag>): Promise<string>;
|
||||||
sendTransaction(signedTransaction: string | Promise<string>): Promise<string>;
|
sendTransaction(signedTransaction: string | Promise<string>): Promise<string>;
|
||||||
call(transaction: TransactionRequest): Promise<string>;
|
call(transaction: TransactionRequest): Promise<string>;
|
||||||
estimateGas(transaction: TransactionRequest): Promise<BigNumber>;
|
estimateGas(transaction: TransactionRequest): Promise<BigNumber>;
|
||||||
|
@ -8,10 +8,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|||||||
};
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
//import inherits = require('inherits');
|
//import inherits = require('inherits');
|
||||||
//import networks = require('./networks.json');
|
|
||||||
var address_1 = require("../utils/address");
|
var address_1 = require("../utils/address");
|
||||||
var bignumber_1 = require("../utils/bignumber");
|
var bignumber_1 = require("../utils/bignumber");
|
||||||
var contract_address_1 = require("../utils/contract-address");
|
|
||||||
var convert_1 = require("../utils/convert");
|
var convert_1 = require("../utils/convert");
|
||||||
var utf8_1 = require("../utils/utf8");
|
var utf8_1 = require("../utils/utf8");
|
||||||
var rlp_1 = require("../utils/rlp");
|
var rlp_1 = require("../utils/rlp");
|
||||||
@ -194,7 +192,7 @@ function checkTransactionResponse(transaction) {
|
|||||||
}
|
}
|
||||||
// If to and creates are empty, populate the creates from the transaction
|
// If to and creates are empty, populate the creates from the transaction
|
||||||
if (transaction.to == null && transaction.creates == null) {
|
if (transaction.to == null && transaction.creates == null) {
|
||||||
transaction.creates = contract_address_1.getContractAddress(transaction);
|
transaction.creates = address_1.getContractAddress(transaction);
|
||||||
}
|
}
|
||||||
if (!transaction.raw) {
|
if (!transaction.raw) {
|
||||||
// Very loose providers (e.g. TestRPC) don't provide a signature or raw
|
// Very loose providers (e.g. TestRPC) don't provide a signature or raw
|
||||||
@ -438,7 +436,7 @@ var Provider = /** @class */ (function () {
|
|||||||
* - Otherwise, the sub-class must assign a Promise to ready
|
* - Otherwise, the sub-class must assign a Promise to ready
|
||||||
*/
|
*/
|
||||||
function Provider(network) {
|
function Provider(network) {
|
||||||
//if (!(this instanceof Provider)) { throw new Error('missing new'); }
|
errors.checkNew(this, Provider);
|
||||||
network = networks_1.getNetwork(network);
|
network = networks_1.getNetwork(network);
|
||||||
if (network) {
|
if (network) {
|
||||||
this._network = network;
|
this._network = network;
|
||||||
|
@ -22,7 +22,6 @@ var errors = __importStar(require("../utils/errors"));
|
|||||||
var Web3Provider = /** @class */ (function (_super) {
|
var Web3Provider = /** @class */ (function (_super) {
|
||||||
__extends(Web3Provider, _super);
|
__extends(Web3Provider, _super);
|
||||||
function Web3Provider(web3Provider, network) {
|
function Web3Provider(web3Provider, network) {
|
||||||
//errors.checkNew(this, Web3Provider);
|
|
||||||
var _this = this;
|
var _this = this;
|
||||||
if (!web3Provider || !web3Provider.sendAsync) {
|
if (!web3Provider || !web3Provider.sendAsync) {
|
||||||
errors.throwError('invalid web3Provider', errors.INVALID_ARGUMENT, { arg: 'web3Provider', value: web3Provider });
|
errors.throwError('invalid web3Provider', errors.INVALID_ARGUMENT, { arg: 'web3Provider', value: web3Provider });
|
||||||
@ -30,6 +29,7 @@ var Web3Provider = /** @class */ (function (_super) {
|
|||||||
// HTTP has a host; IPC has a path.
|
// HTTP has a host; IPC has a path.
|
||||||
var url = web3Provider.host || web3Provider.path || 'unknown';
|
var url = web3Provider.host || web3Provider.path || 'unknown';
|
||||||
_this = _super.call(this, url, network) || this;
|
_this = _super.call(this, url, network) || this;
|
||||||
|
errors.checkNew(_this, Web3Provider);
|
||||||
_this._web3Provider = web3Provider;
|
_this._web3Provider = web3Provider;
|
||||||
return _this;
|
return _this;
|
||||||
}
|
}
|
||||||
|
6
src/utils/address.d.ts
vendored
6
src/utils/address.d.ts
vendored
@ -1 +1,7 @@
|
|||||||
|
import { BigNumber } from './bignumber';
|
||||||
|
import { Arrayish } from './convert';
|
||||||
export declare function getAddress(address: string, icapFormat?: boolean): string;
|
export declare function getAddress(address: string, icapFormat?: boolean): string;
|
||||||
|
export declare function getContractAddress(transaction: {
|
||||||
|
from: string;
|
||||||
|
nonce: Arrayish | BigNumber | number;
|
||||||
|
}): string;
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
// We use this for base 36 maths
|
||||||
var BN = require("bn.js");
|
var BN = require("bn.js");
|
||||||
var convert_1 = require("./convert");
|
var convert_1 = require("./convert");
|
||||||
var keccak256_1 = require("./keccak256");
|
var keccak256_1 = require("./keccak256");
|
||||||
|
var rlp_1 = require("./rlp");
|
||||||
var errors = require("./errors");
|
var errors = require("./errors");
|
||||||
function getChecksumAddress(address) {
|
function getChecksumAddress(address) {
|
||||||
if (typeof (address) !== 'string' || !address.match(/^0x[0-9A-Fa-f]{40}$/)) {
|
if (typeof (address) !== 'string' || !address.match(/^0x[0-9A-Fa-f]{40}$/)) {
|
||||||
@ -104,3 +106,15 @@ function getAddress(address, icapFormat) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
exports.getAddress = getAddress;
|
exports.getAddress = getAddress;
|
||||||
|
// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
|
||||||
|
function getContractAddress(transaction) {
|
||||||
|
if (!transaction.from) {
|
||||||
|
throw new Error('missing from address');
|
||||||
|
}
|
||||||
|
var nonce = transaction.nonce;
|
||||||
|
return getAddress('0x' + keccak256_1.keccak256(rlp_1.encode([
|
||||||
|
getAddress(transaction.from),
|
||||||
|
convert_1.stripZeros(convert_1.hexlify(nonce))
|
||||||
|
])).substring(26));
|
||||||
|
}
|
||||||
|
exports.getContractAddress = getContractAddress;
|
||||||
|
3
src/utils/index.d.ts
vendored
3
src/utils/index.d.ts
vendored
@ -1,8 +1,7 @@
|
|||||||
import { getAddress } from './address';
|
import { getAddress, getContractAddress } from './address';
|
||||||
import { AbiCoder, parseSignature } from './abi-coder';
|
import { AbiCoder, parseSignature } from './abi-coder';
|
||||||
import * as base64 from './base64';
|
import * as base64 from './base64';
|
||||||
import * as bigNumber from './bignumber';
|
import * as bigNumber from './bignumber';
|
||||||
import { getContractAddress } from './contract-address';
|
|
||||||
import * as convert from './convert';
|
import * as convert from './convert';
|
||||||
import { id } from './id';
|
import { id } from './id';
|
||||||
import { keccak256 } from './keccak256';
|
import { keccak256 } from './keccak256';
|
||||||
|
@ -13,7 +13,6 @@ var address_1 = require("./address");
|
|||||||
var abi_coder_1 = require("./abi-coder");
|
var abi_coder_1 = require("./abi-coder");
|
||||||
var base64 = __importStar(require("./base64"));
|
var base64 = __importStar(require("./base64"));
|
||||||
var bigNumber = __importStar(require("./bignumber"));
|
var bigNumber = __importStar(require("./bignumber"));
|
||||||
var contract_address_1 = require("./contract-address");
|
|
||||||
var convert = __importStar(require("./convert"));
|
var convert = __importStar(require("./convert"));
|
||||||
var id_1 = require("./id");
|
var id_1 = require("./id");
|
||||||
var keccak256_1 = require("./keccak256");
|
var keccak256_1 = require("./keccak256");
|
||||||
@ -50,7 +49,7 @@ exports.default = {
|
|||||||
namehash: namehash_1.namehash,
|
namehash: namehash_1.namehash,
|
||||||
id: id_1.id,
|
id: id_1.id,
|
||||||
getAddress: address_1.getAddress,
|
getAddress: address_1.getAddress,
|
||||||
getContractAddress: contract_address_1.getContractAddress,
|
getContractAddress: address_1.getContractAddress,
|
||||||
formatEther: units.formatEther,
|
formatEther: units.formatEther,
|
||||||
parseEther: units.parseEther,
|
parseEther: units.parseEther,
|
||||||
formatUnits: units.formatUnits,
|
formatUnits: units.formatUnits,
|
||||||
|
@ -21,6 +21,7 @@ var utf8_1 = require("../utils/utf8");
|
|||||||
var pbkdf2_1 = require("../utils/pbkdf2");
|
var pbkdf2_1 = require("../utils/pbkdf2");
|
||||||
var hmac_1 = require("../utils/hmac");
|
var hmac_1 = require("../utils/hmac");
|
||||||
var sha2_1 = require("../utils/sha2");
|
var sha2_1 = require("../utils/sha2");
|
||||||
|
var errors = __importStar(require("../utils/errors"));
|
||||||
// "Bitcoin seed"
|
// "Bitcoin seed"
|
||||||
var MasterSecret = utf8_1.toUtf8Bytes('Bitcoin seed');
|
var MasterSecret = utf8_1.toUtf8Bytes('Bitcoin seed');
|
||||||
var HardenedBit = 0x80000000;
|
var HardenedBit = 0x80000000;
|
||||||
@ -35,7 +36,7 @@ function getLowerMask(bits) {
|
|||||||
var HDNode = /** @class */ (function () {
|
var HDNode = /** @class */ (function () {
|
||||||
// @TODO: Private constructor?
|
// @TODO: Private constructor?
|
||||||
function HDNode(keyPair, chainCode, index, depth, mnemonic, path) {
|
function HDNode(keyPair, chainCode, index, depth, mnemonic, path) {
|
||||||
//if (!(this instanceof HDNode)) { throw new Error('missing new'); }
|
errors.checkNew(this, HDNode);
|
||||||
this.keyPair = keyPair;
|
this.keyPair = keyPair;
|
||||||
this.privateKey = convert_1.hexlify(keyPair.priv.toArray('be', 32));
|
this.privateKey = convert_1.hexlify(keyPair.priv.toArray('be', 32));
|
||||||
this.publicKey = '0x' + keyPair.getPublic(true, 'hex');
|
this.publicKey = '0x' + keyPair.getPublic(true, 'hex');
|
||||||
|
@ -19,7 +19,7 @@ var keccak256_1 = require("../utils/keccak256");
|
|||||||
var errors = require("../utils/errors");
|
var errors = require("../utils/errors");
|
||||||
var SigningKey = /** @class */ (function () {
|
var SigningKey = /** @class */ (function () {
|
||||||
function SigningKey(privateKey) {
|
function SigningKey(privateKey) {
|
||||||
//errors.checkNew(this, SigningKey);
|
errors.checkNew(this, SigningKey);
|
||||||
if (privateKey.privateKey) {
|
if (privateKey.privateKey) {
|
||||||
this.mnemonic = privateKey.mnemonic;
|
this.mnemonic = privateKey.mnemonic;
|
||||||
this.path = privateKey.path;
|
this.path = privateKey.path;
|
||||||
|
Loading…
Reference in New Issue
Block a user