Simplify interface to signPersonalMessageAsync
This commit is contained in:
		@@ -47,15 +47,6 @@ export class LedgerSubprovider extends Subprovider {
 | 
				
			|||||||
        const isValid = nonPrefixed.match(HEX_REGEX);
 | 
					        const isValid = nonPrefixed.match(HEX_REGEX);
 | 
				
			||||||
        return isValid;
 | 
					        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) {
 | 
					    private static validateSender(sender: string) {
 | 
				
			||||||
        if (_.isUndefined(sender) || !isAddress(sender)) {
 | 
					        if (_.isUndefined(sender) || !isAddress(sender)) {
 | 
				
			||||||
            throw new Error(LedgerSubproviderErrors.SenderInvalidOrNotSupplied);
 | 
					            throw new Error(LedgerSubproviderErrors.SenderInvalidOrNotSupplied);
 | 
				
			||||||
@@ -131,17 +122,13 @@ export class LedgerSubprovider extends Subprovider {
 | 
				
			|||||||
                return;
 | 
					                return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            case 'personal_sign':
 | 
					            case 'personal_sign':
 | 
				
			||||||
                // non-standard "extraParams" to be appended to our "msgParams" obj
 | 
					                const data = payload.params[0];
 | 
				
			||||||
                // good place for metadata
 | 
					 | 
				
			||||||
                const extraParams = payload.params[2] || {};
 | 
					 | 
				
			||||||
                const msgParams = _.assign({}, extraParams, {
 | 
					 | 
				
			||||||
                    from: payload.params[1],
 | 
					 | 
				
			||||||
                    data: payload.params[0],
 | 
					 | 
				
			||||||
                });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                try {
 | 
					                try {
 | 
				
			||||||
                    LedgerSubprovider.validatePersonalMessage(msgParams);
 | 
					                    if (_.isUndefined(data)) {
 | 
				
			||||||
                    const ecSignatureHex = await this.signPersonalMessageAsync(msgParams);
 | 
					                        throw new Error(LedgerSubproviderErrors.DataMissingForSignPersonalMessage);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    assert.isHexString('data', data);
 | 
				
			||||||
 | 
					                    const ecSignatureHex = await this.signPersonalMessageAsync(data);
 | 
				
			||||||
                    end(null, ecSignatureHex);
 | 
					                    end(null, ecSignatureHex);
 | 
				
			||||||
                } catch (err) {
 | 
					                } catch (err) {
 | 
				
			||||||
                    end(err);
 | 
					                    end(err);
 | 
				
			||||||
@@ -207,12 +194,12 @@ export class LedgerSubprovider extends Subprovider {
 | 
				
			|||||||
            throw err;
 | 
					            throw err;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    public async signPersonalMessageAsync(msgParams: SignPersonalMessageParams): Promise<string> {
 | 
					    public async signPersonalMessageAsync(data: string): Promise<string> {
 | 
				
			||||||
        this._ledgerClientIfExists = await this.createLedgerClientAsync();
 | 
					        this._ledgerClientIfExists = await this.createLedgerClientAsync();
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            const derivationPath = this.getDerivationPath();
 | 
					            const derivationPath = this.getDerivationPath();
 | 
				
			||||||
            const result = await this._ledgerClientIfExists.signPersonalMessage_async(
 | 
					            const result = await this._ledgerClientIfExists.signPersonalMessage_async(
 | 
				
			||||||
                derivationPath, ethUtil.stripHexPrefix(msgParams.data));
 | 
					                derivationPath, ethUtil.stripHexPrefix(data));
 | 
				
			||||||
            const v = result.v - 27;
 | 
					            const v = result.v - 27;
 | 
				
			||||||
            let vHex = v.toString(16);
 | 
					            let vHex = v.toString(16);
 | 
				
			||||||
            if (vHex.length < 2) {
 | 
					            if (vHex.length < 2) {
 | 
				
			||||||
@@ -250,7 +237,7 @@ export class LedgerSubprovider extends Subprovider {
 | 
				
			|||||||
        this._ledgerClientIfExists = undefined;
 | 
					        this._ledgerClientIfExists = undefined;
 | 
				
			||||||
        this._connectionLock.signal();
 | 
					        this._connectionLock.signal();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    private async sendTransactionAsync(txParams: PartialTxParams): Promise<any> {
 | 
					    private async sendTransactionAsync(txParams: PartialTxParams): Promise<Web3.JSONRPCResponsePayload> {
 | 
				
			||||||
        await this._nonceLock.wait();
 | 
					        await this._nonceLock.wait();
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            // fill in the extras
 | 
					            // fill in the extras
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -41,7 +41,7 @@ describe('LedgerSubprovider', () => {
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
        it('signs a personal message', async () => {
 | 
					        it('signs a personal message', async () => {
 | 
				
			||||||
            const data = ethUtils.bufferToHex(ethUtils.toBuffer('hello world'));
 | 
					            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.length).to.be.equal(132);
 | 
				
			||||||
            expect(ecSignatureHex.substr(0, 2)).to.be.equal('0x');
 | 
					            expect(ecSignatureHex.substr(0, 2)).to.be.equal('0x');
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -71,8 +71,7 @@ describe('LedgerSubprovider', () => {
 | 
				
			|||||||
            });
 | 
					            });
 | 
				
			||||||
            it('signs a personal message', async () => {
 | 
					            it('signs a personal message', async () => {
 | 
				
			||||||
                const data = ethUtils.bufferToHex(ethUtils.toBuffer('hello world'));
 | 
					                const data = ethUtils.bufferToHex(ethUtils.toBuffer('hello world'));
 | 
				
			||||||
                const msgParams = {data};
 | 
					                const ecSignatureHex = await ledgerSubprovider.signPersonalMessageAsync(data);
 | 
				
			||||||
                const ecSignatureHex = await ledgerSubprovider.signPersonalMessageAsync(msgParams);
 | 
					 | 
				
			||||||
                // tslint:disable-next-line:max-line-length
 | 
					                // tslint:disable-next-line:max-line-length
 | 
				
			||||||
                expect(ecSignatureHex).to.be.equal('0xa6cc284bff14b42bdf5e9286730c152be91719d478605ec46b3bebcd0ae491480652a1a7b742ceb0213d1e744316e285f41f878d8af0b8e632cbca4c279132d001');
 | 
					                expect(ecSignatureHex).to.be.equal('0xa6cc284bff14b42bdf5e9286730c152be91719d478605ec46b3bebcd0ae491480652a1a7b742ceb0213d1e744316e285f41f878d8af0b8e632cbca4c279132d001');
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
@@ -80,11 +79,10 @@ describe('LedgerSubprovider', () => {
 | 
				
			|||||||
        describe('failure cases', () => {
 | 
					        describe('failure cases', () => {
 | 
				
			||||||
            it('cannot open multiple simultaneous connections to the Ledger device', async () => {
 | 
					            it('cannot open multiple simultaneous connections to the Ledger device', async () => {
 | 
				
			||||||
                const data = ethUtils.bufferToHex(ethUtils.toBuffer('hello world'));
 | 
					                const data = ethUtils.bufferToHex(ethUtils.toBuffer('hello world'));
 | 
				
			||||||
                const msgParams = {data};
 | 
					 | 
				
			||||||
                try {
 | 
					                try {
 | 
				
			||||||
                    const result = await Promise.all([
 | 
					                    const result = await Promise.all([
 | 
				
			||||||
                        ledgerSubprovider.getAccountsAsync(),
 | 
					                        ledgerSubprovider.getAccountsAsync(),
 | 
				
			||||||
                        ledgerSubprovider.signPersonalMessageAsync(msgParams),
 | 
					                        ledgerSubprovider.signPersonalMessageAsync(data),
 | 
				
			||||||
                    ]);
 | 
					                    ]);
 | 
				
			||||||
                    throw new Error('Multiple simultaneous calls succeeded when they should have failed');
 | 
					                    throw new Error('Multiple simultaneous calls succeeded when they should have failed');
 | 
				
			||||||
                } catch (err) {
 | 
					                } catch (err) {
 | 
				
			||||||
@@ -157,21 +155,6 @@ describe('LedgerSubprovider', () => {
 | 
				
			|||||||
            });
 | 
					            });
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
        describe('failure cases', () => {
 | 
					        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) => {
 | 
					            it('should throw if `data` param not hex when calling personal_sign', (done: DoneCallback) => {
 | 
				
			||||||
                const nonHexMessage = 'hello world';
 | 
					                const nonHexMessage = 'hello world';
 | 
				
			||||||
                const payload = {
 | 
					                const payload = {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user