Port over signOrderHashAsync

This commit is contained in:
Fabio Berger 2017-05-29 13:37:08 +02:00
parent 232e031959
commit eb90367fc4
5 changed files with 73 additions and 1 deletions

View File

@ -68,9 +68,11 @@
},
"dependencies": {
"bignumber.js": "^4.0.2",
"compare-versions": "^3.0.1",
"es6-promisify": "^5.0.0",
"ethereumjs-abi": "^0.6.4",
"ethereumjs-util": "^5.1.1",
"find-versions": "^2.0.0",
"jsonschema": "^1.1.1",
"lodash": "^4.17.4",
"truffle-contract": "^2.0.0",

View File

@ -8,9 +8,11 @@ import {Web3Wrapper} from './web3_wrapper';
import {constants} from './utils/constants';
import {utils} from './utils/utils';
import {assert} from './utils/assert';
import findVersions = require('find-versions');
import compareVersions = require('compare-versions');
import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper';
import {ECSignatureSchema} from './schemas/ec_signature_schema';
import {SolidityTypes, ECSignature} from './types';
import {SolidityTypes, ECSignature, ZeroExError} from './types';
const MAX_DIGITS_IN_UNSIGNED_256_INT = 78;
@ -131,4 +133,64 @@ export class ZeroEx {
this.web3Wrapper = new Web3Wrapper(web3);
this.exchange = new ExchangeWrapper(this.web3Wrapper);
}
/**
* Signs an orderHash and returns it's ECSignature
* This method currently supports TestRPC, Geth and Parity above and below V1.6.6
*/
public async signOrderHashAsync(orderHashHex: string): Promise<ECSignature> {
assert.isHexString('orderHashHex', orderHashHex);
let msgHashHex;
const nodeVersion = await this.web3Wrapper.getNodeVersionAsync();
const isParityNode = utils.isParityNode(nodeVersion);
if (isParityNode) {
// Parity node adds the personalMessage prefix itself
msgHashHex = orderHashHex;
} else {
const orderHashBuff = ethUtil.toBuffer(orderHashHex);
const msgHashBuff = ethUtil.hashPersonalMessage(orderHashBuff);
msgHashHex = ethUtil.bufferToHex(msgHashBuff);
}
const makerAddressIfExists = await this.web3Wrapper.getSenderAddressIfExistsAsync();
if (_.isUndefined(makerAddressIfExists)) {
throw new Error(ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES);
}
const signature = await this.web3Wrapper.signTransactionAsync(makerAddressIfExists, msgHashHex);
let signatureData;
const [nodeVersionNumber] = findVersions(nodeVersion);
// Parity v1.6.6 and earlier returns the signatureData as vrs instead of rsv as Geth does
// Later versions return rsv but for the time being we still want to support version < 1.6.6
// Date: May 23rd 2017
const latestParityVersionWithVRS = '1.6.6';
const isVersionBeforeParityFix = compareVersions(nodeVersionNumber, latestParityVersionWithVRS) <= 0;
if (isParityNode && isVersionBeforeParityFix) {
const signatureBuffer = ethUtil.toBuffer(signature);
let v = signatureBuffer[0];
if (v < 27) {
v += 27;
}
signatureData = {
v,
r: signatureBuffer.slice(1, 33),
s: signatureBuffer.slice(33, 65),
};
} else {
signatureData = ethUtil.fromRpcSig(signature);
}
const {v, r, s} = signatureData;
const ecSignature: ECSignature = {
v,
r: ethUtil.bufferToHex(r),
s: ethUtil.bufferToHex(s),
};
const isValidSignature = ZeroEx.isValidSignature(orderHashHex, ecSignature, makerAddressIfExists);
if (!isValidSignature) {
throw new Error(ZeroExError.INVALID_SIGNATURE);
}
return ecSignature;
}
}

3
src/globals.d.ts vendored
View File

@ -3,6 +3,8 @@ declare module 'bn.js';
declare module 'request-promise-native';
declare module 'web3-provider-engine';
declare module 'web3-provider-engine/subproviders/rpc';
declare module 'find-versions';
declare module 'compare-versions';
declare interface Schema {
id: string;
@ -35,6 +37,7 @@ declare module 'ethereumjs-util' {
const pubToAddress: (pubKey: string) => Buffer;
const isValidAddress: (address: string) => boolean;
const bufferToInt: (buffer: Buffer) => number;
const fromRpcSig: (signature: string) => {v: number, r: Buffer, s: Buffer};
}
// truffle-contract declarations

View File

@ -13,6 +13,7 @@ export const ZeroExError = strEnum([
'CONTRACT_DOES_NOT_EXIST',
'UNHANDLED_ERROR',
'USER_HAS_NO_ASSOCIATED_ADDRESSES',
'INVALID_SIGNATURE',
]);
export type ZeroExError = keyof typeof ZeroExError;

View File

@ -1,3 +1,4 @@
import * as _ from 'lodash';
import * as BN from 'bn.js';
export const utils = {
@ -15,4 +16,7 @@ export const utils = {
console.log(message);
/* tslint:enable */
},
isParityNode(nodeVersion: string): boolean {
return _.includes(nodeVersion, 'Parity');
},
};