Added VoidSigner for read-only access as a specific address.

This commit is contained in:
Richard Moore 2018-09-04 10:28:26 -04:00
parent 6018bf63f9
commit e0cefb0398
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
3 changed files with 43 additions and 4 deletions

View File

@ -20,6 +20,7 @@ import { Signer } from '../wallet/abstract-signer';
///////////////////////////////
// Imported Types
import { Arrayish } from '../utils/bytes';
import { EventDescription } from './interface';
import { ParamType } from '../utils/abi-coder';
import { Block, Listener, Log, TransactionReceipt, TransactionRequest, TransactionResponse } from '../providers/abstract-provider';
@ -52,6 +53,38 @@ export interface Event extends Log {
///////////////////////////////
export class VoidSigner extends Signer {
readonly address: string;
constructor(address: string, provider: Provider) {
super();
defineReadOnly(this, 'address', address);
defineReadOnly(this, 'provider', provider);
}
getAddress(): Promise<string> {
return Promise.resolve(this.address);
}
_fail(message: string, operation: string): Promise<any> {
return Promise.resolve().then(() => {
errors.throwError(message, errors.UNSUPPORTED_OPERATION, { operation: operation });
});
}
signMessage(message: Arrayish | string): Promise<string> {
return this._fail('VoidSigner cannot sign messages', 'signMessage');
}
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse> {
return this._fail('VoidSigner cannot sign transactions', 'sendTransaction');
}
connect(provider: Provider): VoidSigner {
return new VoidSigner(this.address, provider);
}
}
var allowedTransactionKeys: { [ key: string ]: boolean } = {
data: true, from: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true
}
@ -412,7 +445,11 @@ export class Contract {
}
// Reconnect to a different signer or provider
connect(signerOrProvider: Signer | Provider): Contract {
connect(signerOrProvider: Signer | Provider | string): Contract {
if (typeof(signerOrProvider) === 'string') {
signerOrProvider = new VoidSigner(signerOrProvider, this.provider);
}
let contract = new Contract(this.address, this.interface, signerOrProvider);
if (this.deployTransaction) {
defineReadOnly(contract, 'deployTransaction', this.deployTransaction);
@ -586,7 +623,7 @@ export class Contract {
return this;
}
addEventLisener(eventName: EventFilter | string, listener: Listener): Contract {
addListener(eventName: EventFilter | string, listener: Listener): Contract {
return this.on(eventName, listener);
}

View File

@ -1,10 +1,11 @@
'use strict';
import { Contract } from './contract';
import { Contract, VoidSigner } from './contract';
import { Interface } from './interface';
export {
Contract,
Interface,
VoidSigner
}

View File

@ -1,6 +1,6 @@
'use strict';
import { Contract, Interface } from './contracts';
import { Contract, Interface, VoidSigner } from './contracts';
import * as providers from './providers';
@ -32,6 +32,7 @@ function getDefaultProvider(network?: types.Network | string): providers.BasePro
export {
Wallet,
VoidSigner,
HDNode,
SigningKey,