2
0
mirror of synced 2025-02-25 04:25:16 +00:00

Added support for getting JsonRpcSigner by index and fixed gas limit in sendTransaction.

This commit is contained in:
Richard Moore 2018-08-03 15:22:28 -04:00
parent 694c0b3f7d
commit f63c844c42
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295

View File

@ -51,11 +51,13 @@ const _constructorGuard = {};
export class JsonRpcSigner extends Signer { export class JsonRpcSigner extends Signer {
readonly provider: JsonRpcProvider; readonly provider: JsonRpcProvider;
private _index: number;
private _address: string; private _address: string;
constructor(constructorGuard: any, provider: JsonRpcProvider, address?: string) { constructor(constructorGuard: any, provider: JsonRpcProvider, addressOrIndex?: string | number) {
super(); super();
errors.checkNew(this, JsonRpcSigner); errors.checkNew(this, JsonRpcSigner);
if (constructorGuard !== _constructorGuard) { if (constructorGuard !== _constructorGuard) {
throw new Error('do not call the JsonRpcSigner constructor directly; use provider.getSigner'); throw new Error('do not call the JsonRpcSigner constructor directly; use provider.getSigner');
} }
@ -63,8 +65,16 @@ export class JsonRpcSigner extends Signer {
defineReadOnly(this, 'provider', provider); defineReadOnly(this, 'provider', provider);
// Statically attach to a given address // Statically attach to a given address
if (address) { if (addressOrIndex) {
defineReadOnly(this, '_address', address); if (typeof(addressOrIndex) === 'string') {
defineReadOnly(this, '_address', getAddress(addressOrIndex));
} else if (typeof(addressOrIndex) === 'number') {
defineReadOnly(this, '_index', addressOrIndex);
} else {
errors.throwError('invalid address or index', errors.INVALID_ARGUMENT, { argument: 'addressOrIndex', value: addressOrIndex });
}
} else {
defineReadOnly(this, '_index', 0);
} }
} }
@ -81,10 +91,10 @@ export class JsonRpcSigner extends Signer {
} }
return this.provider.send('eth_accounts', []).then((accounts) => { return this.provider.send('eth_accounts', []).then((accounts) => {
if (accounts.length === 0) { if (accounts.length <= this._index) {
errors.throwError('no accounts', errors.UNSUPPORTED_OPERATION, { operation: 'getAddress' }); errors.throwError('unknown account #' + this._index, errors.UNSUPPORTED_OPERATION, { operation: 'getAddress' });
} }
return getAddress(accounts[0]); return getAddress(accounts[this._index]);
}); });
} }
@ -107,7 +117,7 @@ export class JsonRpcSigner extends Signer {
} }
if (transaction.gasLimit == null) { if (transaction.gasLimit == null) {
tx.gasLimit = this.provider.estimateGas(transaction); tx.gasLimit = this.provider.estimateGas(tx);
} }
return resolveProperties(tx).then((tx) => { return resolveProperties(tx).then((tx) => {
@ -210,8 +220,8 @@ export class JsonRpcProvider extends BaseProvider {
} }
getSigner(address?: string): JsonRpcSigner { getSigner(addressOrIndex?: string | number): JsonRpcSigner {
return new JsonRpcSigner(_constructorGuard, this, address); return new JsonRpcSigner(_constructorGuard, this, addressOrIndex);
} }
listAccounts(): Promise<Array<string>> { listAccounts(): Promise<Array<string>> {