diff options
author | Fabio Berger <me@fabioberger.com> | 2017-12-07 10:52:48 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-12-07 10:52:48 +0800 |
commit | e8495b0c7bf58cb683f70cd66a983dbf909215c0 (patch) | |
tree | 552f5da749b4e05db5c7e3893c3e9c52402fc6f5 | |
parent | e80579a6055f75edac0fb0e948838495d6599bb5 (diff) | |
download | dexon-sol-tools-e8495b0c7bf58cb683f70cd66a983dbf909215c0.tar.gz dexon-sol-tools-e8495b0c7bf58cb683f70cd66a983dbf909215c0.tar.zst dexon-sol-tools-e8495b0c7bf58cb683f70cd66a983dbf909215c0.zip |
Simplify interface to signPersonalMessageAsync
3 files changed, 12 insertions, 42 deletions
diff --git a/packages/subproviders/src/subproviders/ledger.ts b/packages/subproviders/src/subproviders/ledger.ts index c8fa7fd5a..30d8d574f 100644 --- a/packages/subproviders/src/subproviders/ledger.ts +++ b/packages/subproviders/src/subproviders/ledger.ts @@ -47,15 +47,6 @@ export class LedgerSubprovider extends Subprovider { const isValid = nonPrefixed.match(HEX_REGEX); return isValid; } - private static validatePersonalMessage(msgParams: PartialTxParams) { - if (_.isUndefined(msgParams.from) || !isAddress(msgParams.from)) { - throw new Error(LedgerSubproviderErrors.FromAddressMissingOrInvalid); - } - if (_.isUndefined(msgParams.data)) { - throw new Error(LedgerSubproviderErrors.DataMissingForSignPersonalMessage); - } - assert.isHexString('data', msgParams.data); - } private static validateSender(sender: string) { if (_.isUndefined(sender) || !isAddress(sender)) { throw new Error(LedgerSubproviderErrors.SenderInvalidOrNotSupplied); @@ -131,17 +122,13 @@ export class LedgerSubprovider extends Subprovider { return; case 'personal_sign': - // non-standard "extraParams" to be appended to our "msgParams" obj - // good place for metadata - const extraParams = payload.params[2] || {}; - const msgParams = _.assign({}, extraParams, { - from: payload.params[1], - data: payload.params[0], - }); - + const data = payload.params[0]; try { - LedgerSubprovider.validatePersonalMessage(msgParams); - const ecSignatureHex = await this.signPersonalMessageAsync(msgParams); + if (_.isUndefined(data)) { + throw new Error(LedgerSubproviderErrors.DataMissingForSignPersonalMessage); + } + assert.isHexString('data', data); + const ecSignatureHex = await this.signPersonalMessageAsync(data); end(null, ecSignatureHex); } catch (err) { end(err); @@ -207,12 +194,12 @@ export class LedgerSubprovider extends Subprovider { throw err; } } - public async signPersonalMessageAsync(msgParams: SignPersonalMessageParams): Promise<string> { + public async signPersonalMessageAsync(data: string): Promise<string> { this._ledgerClientIfExists = await this.createLedgerClientAsync(); try { const derivationPath = this.getDerivationPath(); const result = await this._ledgerClientIfExists.signPersonalMessage_async( - derivationPath, ethUtil.stripHexPrefix(msgParams.data)); + derivationPath, ethUtil.stripHexPrefix(data)); const v = result.v - 27; let vHex = v.toString(16); if (vHex.length < 2) { @@ -250,7 +237,7 @@ export class LedgerSubprovider extends Subprovider { this._ledgerClientIfExists = undefined; this._connectionLock.signal(); } - private async sendTransactionAsync(txParams: PartialTxParams): Promise<any> { + private async sendTransactionAsync(txParams: PartialTxParams): Promise<Web3.JSONRPCResponsePayload> { await this._nonceLock.wait(); try { // fill in the extras diff --git a/packages/subproviders/test/integration/ledger_subprovider_test.ts b/packages/subproviders/test/integration/ledger_subprovider_test.ts index ab1ee3264..dfc55be5b 100644 --- a/packages/subproviders/test/integration/ledger_subprovider_test.ts +++ b/packages/subproviders/test/integration/ledger_subprovider_test.ts @@ -41,7 +41,7 @@ describe('LedgerSubprovider', () => { }); it('signs a personal message', async () => { const data = ethUtils.bufferToHex(ethUtils.toBuffer('hello world')); - const ecSignatureHex = await ledgerSubprovider.signPersonalMessageAsync({data}); + const ecSignatureHex = await ledgerSubprovider.signPersonalMessageAsync(data); expect(ecSignatureHex.length).to.be.equal(132); expect(ecSignatureHex.substr(0, 2)).to.be.equal('0x'); }); diff --git a/packages/subproviders/test/unit/ledger_subprovider_test.ts b/packages/subproviders/test/unit/ledger_subprovider_test.ts index 95127b8d8..41c743346 100644 --- a/packages/subproviders/test/unit/ledger_subprovider_test.ts +++ b/packages/subproviders/test/unit/ledger_subprovider_test.ts @@ -71,8 +71,7 @@ describe('LedgerSubprovider', () => { }); it('signs a personal message', async () => { const data = ethUtils.bufferToHex(ethUtils.toBuffer('hello world')); - const msgParams = {data}; - const ecSignatureHex = await ledgerSubprovider.signPersonalMessageAsync(msgParams); + const ecSignatureHex = await ledgerSubprovider.signPersonalMessageAsync(data); // tslint:disable-next-line:max-line-length expect(ecSignatureHex).to.be.equal('0xa6cc284bff14b42bdf5e9286730c152be91719d478605ec46b3bebcd0ae491480652a1a7b742ceb0213d1e744316e285f41f878d8af0b8e632cbca4c279132d001'); }); @@ -80,11 +79,10 @@ describe('LedgerSubprovider', () => { describe('failure cases', () => { it('cannot open multiple simultaneous connections to the Ledger device', async () => { const data = ethUtils.bufferToHex(ethUtils.toBuffer('hello world')); - const msgParams = {data}; try { const result = await Promise.all([ ledgerSubprovider.getAccountsAsync(), - ledgerSubprovider.signPersonalMessageAsync(msgParams), + ledgerSubprovider.signPersonalMessageAsync(data), ]); throw new Error('Multiple simultaneous calls succeeded when they should have failed'); } catch (err) { @@ -157,21 +155,6 @@ describe('LedgerSubprovider', () => { }); }); describe('failure cases', () => { - it('should throw if `from` param missing when calling personal_sign', (done: DoneCallback) => { - const messageHex = ethUtils.bufferToHex(ethUtils.toBuffer('hello world')); - const payload = { - jsonrpc: '2.0', - method: 'personal_sign', - params: [messageHex], // Missing from param - id: 1, - }; - const callback = reportCallbackErrors(done)((err: Error, response: Web3.JSONRPCResponsePayload) => { - expect(err).to.not.be.a('null'); - expect(err.message).to.be.equal(LedgerSubproviderErrors.FromAddressMissingOrInvalid); - done(); - }); - provider.sendAsync(payload, callback); - }); it('should throw if `data` param not hex when calling personal_sign', (done: DoneCallback) => { const nonHexMessage = 'hello world'; const payload = { |