Simplify interface to signPersonalMessageAsync
This commit is contained in:
@@ -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
|
||||
|
@@ -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');
|
||||
});
|
||||
|
@@ -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 = {
|
||||
|
Reference in New Issue
Block a user