aboutsummaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
authorJacob Evans <jacob@dekz.net>2018-10-02 15:32:28 +0800
committerJacob Evans <jacob@dekz.net>2018-10-05 10:00:41 +0800
commit07926ded6ef194969ffe26e3879d6e86a0eb9c50 (patch)
tree9603fa9942cdf1c364fe794e037013351416fc73 /packages
parentadcfaa2e80389f69e196b602955cee858a1eb40f (diff)
downloaddexon-sol-tools-07926ded6ef194969ffe26e3879d6e86a0eb9c50.tar
dexon-sol-tools-07926ded6ef194969ffe26e3879d6e86a0eb9c50.tar.gz
dexon-sol-tools-07926ded6ef194969ffe26e3879d6e86a0eb9c50.tar.bz2
dexon-sol-tools-07926ded6ef194969ffe26e3879d6e86a0eb9c50.tar.lz
dexon-sol-tools-07926ded6ef194969ffe26e3879d6e86a0eb9c50.tar.xz
dexon-sol-tools-07926ded6ef194969ffe26e3879d6e86a0eb9c50.tar.zst
dexon-sol-tools-07926ded6ef194969ffe26e3879d6e86a0eb9c50.zip
Introduce Metamask Subprovider.
MM has a number of inconsistencies with other providers when implementing the JSON RPC interface. This subprovider wraps those nuances so they do not leak into the rest of our code
Diffstat (limited to 'packages')
-rw-r--r--packages/0x.js/src/index.ts9
-rw-r--r--packages/contract-wrappers/test/transaction_encoder_test.ts9
-rw-r--r--packages/contracts/test/exchange/signature_validator.ts12
-rw-r--r--packages/order-utils/src/index.ts1
-rw-r--r--packages/order-utils/src/order_factory.ts9
-rw-r--r--packages/order-utils/src/signature_utils.ts120
-rw-r--r--packages/order-utils/test/signature_utils_test.ts104
-rw-r--r--packages/subproviders/CHANGELOG.json8
-rw-r--r--packages/subproviders/src/index.ts1
-rw-r--r--packages/subproviders/src/subproviders/metamask_subprovider.ts124
-rw-r--r--packages/subproviders/src/subproviders/private_key_wallet.ts2
-rw-r--r--packages/subproviders/src/subproviders/signer.ts13
-rw-r--r--packages/types/src/index.ts10
-rw-r--r--packages/website/ts/blockchain.ts22
14 files changed, 225 insertions, 219 deletions
diff --git a/packages/0x.js/src/index.ts b/packages/0x.js/src/index.ts
index d07bfcfc8..228bcecdb 100644
--- a/packages/0x.js/src/index.ts
+++ b/packages/0x.js/src/index.ts
@@ -53,7 +53,13 @@ export { OrderWatcher, OnOrderStateChangeCallback, OrderWatcherConfig } from '@0
export import Web3ProviderEngine = require('web3-provider-engine');
-export { RPCSubprovider, Callback, JSONRPCRequestPayloadWithMethod, ErrorCallback } from '@0xproject/subproviders';
+export {
+ RPCSubprovider,
+ Callback,
+ JSONRPCRequestPayloadWithMethod,
+ ErrorCallback,
+ MetamaskSubprovider,
+} from '@0xproject/subproviders';
export { AbiDecoder } from '@0xproject/utils';
@@ -68,7 +74,6 @@ export {
OrderStateInvalid,
OrderState,
AssetProxyId,
- SignerType,
ERC20AssetData,
ERC721AssetData,
SignatureType,
diff --git a/packages/contract-wrappers/test/transaction_encoder_test.ts b/packages/contract-wrappers/test/transaction_encoder_test.ts
index a397e43a8..9da8fe2ca 100644
--- a/packages/contract-wrappers/test/transaction_encoder_test.ts
+++ b/packages/contract-wrappers/test/transaction_encoder_test.ts
@@ -1,7 +1,7 @@
import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { FillScenarios } from '@0xproject/fill-scenarios';
import { assetDataUtils, generatePseudoRandomSalt, orderHashUtils, signatureUtils } from '@0xproject/order-utils';
-import { SignedOrder, SignerType } from '@0xproject/types';
+import { SignedOrder } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils';
import 'mocha';
@@ -80,12 +80,7 @@ describe('TransactionEncoder', () => {
): Promise<void> => {
const salt = generatePseudoRandomSalt();
const encodedTransaction = encoder.getTransactionHex(data, salt, signerAddress);
- const signature = await signatureUtils.ecSignOrderHashAsync(
- provider,
- encodedTransaction,
- signerAddress,
- SignerType.Default,
- );
+ const signature = await signatureUtils.ecSignHashAsync(provider, encodedTransaction, signerAddress);
txHash = await contractWrappers.exchange.executeTransactionAsync(
salt,
signerAddress,
diff --git a/packages/contracts/test/exchange/signature_validator.ts b/packages/contracts/test/exchange/signature_validator.ts
index 5cc62e777..192ed3ca9 100644
--- a/packages/contracts/test/exchange/signature_validator.ts
+++ b/packages/contracts/test/exchange/signature_validator.ts
@@ -1,6 +1,6 @@
import { BlockchainLifecycle } from '@0xproject/dev-utils';
import { assetDataUtils, orderHashUtils, signatureUtils } from '@0xproject/order-utils';
-import { RevertReason, SignatureType, SignedOrder, SignerType } from '@0xproject/types';
+import { RevertReason, SignatureType, SignedOrder } from '@0xproject/types';
import * as chai from 'chai';
import { LogWithDecodedArgs } from 'ethereum-types';
import ethUtil = require('ethereumjs-util');
@@ -231,10 +231,7 @@ describe('MixinSignatureValidator', () => {
it('should return true when SignatureType=EthSign and signature is valid', async () => {
// Create EthSign signature
const orderHashHex = orderHashUtils.getOrderHashHex(signedOrder);
- const orderHashWithEthSignPrefixHex = signatureUtils.addSignedMessagePrefix(
- orderHashHex,
- SignerType.Default,
- );
+ const orderHashWithEthSignPrefixHex = signatureUtils.addSignedMessagePrefix(orderHashHex);
const orderHashWithEthSignPrefixBuffer = ethUtil.toBuffer(orderHashWithEthSignPrefixHex);
const ecSignature = ethUtil.ecsign(orderHashWithEthSignPrefixBuffer, signerPrivateKey);
// Create 0x signature from EthSign signature
@@ -257,10 +254,7 @@ describe('MixinSignatureValidator', () => {
it('should return false when SignatureType=EthSign and signature is invalid', async () => {
// Create EthSign signature
const orderHashHex = orderHashUtils.getOrderHashHex(signedOrder);
- const orderHashWithEthSignPrefixHex = signatureUtils.addSignedMessagePrefix(
- orderHashHex,
- SignerType.Default,
- );
+ const orderHashWithEthSignPrefixHex = signatureUtils.addSignedMessagePrefix(orderHashHex);
const orderHashWithEthSignPrefixBuffer = ethUtil.toBuffer(orderHashWithEthSignPrefixHex);
const ecSignature = ethUtil.ecsign(orderHashWithEthSignPrefixBuffer, signerPrivateKey);
// Create 0x signature from EthSign signature
diff --git a/packages/order-utils/src/index.ts b/packages/order-utils/src/index.ts
index 1553647c6..e7a23682c 100644
--- a/packages/order-utils/src/index.ts
+++ b/packages/order-utils/src/index.ts
@@ -29,7 +29,6 @@ export {
ERC20AssetData,
ERC721AssetData,
AssetProxyId,
- SignerType,
SignatureType,
OrderStateValid,
OrderStateInvalid,
diff --git a/packages/order-utils/src/order_factory.ts b/packages/order-utils/src/order_factory.ts
index b1292903a..0f0cd6046 100644
--- a/packages/order-utils/src/order_factory.ts
+++ b/packages/order-utils/src/order_factory.ts
@@ -1,4 +1,4 @@
-import { Order, SignedOrder, SignerType } from '@0xproject/types';
+import { Order, SignedOrder } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils';
import { Provider } from 'ethereum-types';
import * as _ from 'lodash';
@@ -71,12 +71,7 @@ export const orderFactory = {
createOrderOpts,
);
const orderHash = orderHashUtils.getOrderHashHex(order);
- const signature = await signatureUtils.ecSignOrderHashAsync(
- provider,
- orderHash,
- makerAddress,
- SignerType.Default,
- );
+ const signature = await signatureUtils.ecSignHashAsync(provider, orderHash, makerAddress);
const signedOrder: SignedOrder = _.assign(order, { signature });
return signedOrder;
},
diff --git a/packages/order-utils/src/signature_utils.ts b/packages/order-utils/src/signature_utils.ts
index 05c673ae2..8e0fd702b 100644
--- a/packages/order-utils/src/signature_utils.ts
+++ b/packages/order-utils/src/signature_utils.ts
@@ -1,5 +1,5 @@
import { schemas } from '@0xproject/json-schemas';
-import { ECSignature, Order, SignatureType, SignerType, ValidatorSignature } from '@0xproject/types';
+import { ECSignature, Order, SignatureType, ValidatorSignature } from '@0xproject/types';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import { Provider } from 'ethereum-types';
import * as ethUtil from 'ethereumjs-util';
@@ -11,7 +11,7 @@ import { EIP712_DOMAIN_NAME, EIP712_DOMAIN_SCHEMA, EIP712_DOMAIN_VERSION } from
import { ExchangeContract } from './generated_contract_wrappers/exchange';
import { IValidatorContract } from './generated_contract_wrappers/i_validator';
import { IWalletContract } from './generated_contract_wrappers/i_wallet';
-import { EIP712_ORDER_SCHEMA } from './order_hash';
+import { EIP712_ORDER_SCHEMA, orderHashUtils } from './order_hash';
import { OrderError } from './types';
import { utils } from './utils';
@@ -51,7 +51,7 @@ export const signatureUtils = {
case SignatureType.EthSign: {
const ecSignature = signatureUtils.parseECSignature(signature);
- const prefixedMessageHex = signatureUtils.addSignedMessagePrefix(data, SignerType.Default);
+ const prefixedMessageHex = signatureUtils.addSignedMessagePrefix(data);
return signatureUtils.isValidECSignature(prefixedMessageHex, ecSignature, signerAddress);
}
@@ -194,19 +194,41 @@ export const signatureUtils = {
}
},
/**
- * Signs an order using `eth_signTypedData` and returns it's elliptic curve signature and signature type.
- * This method currently supports Ganache.
+ * Signs an order and returns its elliptic curve signature and signature type. First `eth_signTypedData` is requested
+ * then a fallback to `eth_sign` if not available on this provider.
* @param order The Order to sign.
* @param signerAddress The hex encoded Ethereum address you wish to sign it with. This address
* must be available via the Provider supplied to 0x.js.
* @return A hex encoded string containing the Elliptic curve signature generated by signing the orderHash and the Signature Type.
*/
async ecSignOrderAsync(provider: Provider, order: Order, signerAddress: string): Promise<string> {
+ try {
+ const signatureHex = signatureUtils.ecSignTypedDataOrderAsync(provider, order, signerAddress);
+ return signatureHex;
+ } catch (err) {
+ // Fallback to using EthSign when ethSignTypedData is not supported
+ const orderHash = orderHashUtils.getOrderHashHex(order);
+ const signatureHex = await signatureUtils.ecSignHashAsync(provider, orderHash, signerAddress);
+ return signatureHex;
+ }
+ },
+ /**
+ * Signs an order using `eth_signTypedData` and returns its elliptic curve signature and signature type.
+ * @param order The Order to sign.
+ * @param signerAddress The hex encoded Ethereum address you wish to sign it with. This address
+ * must be available via the Provider supplied to 0x.js.
+ * @return A hex encoded string containing the Elliptic curve signature generated by signing the order with `eth_signTypedData`
+ * and the Signature Type.
+ */
+ async ecSignTypedDataOrderAsync(provider: Provider, order: Order, signerAddress: string): Promise<string> {
assert.isWeb3Provider('provider', provider);
assert.isETHAddressHex('signerAddress', signerAddress);
const web3Wrapper = new Web3Wrapper(provider);
await assert.isSenderAddressAsync('signerAddress', signerAddress, web3Wrapper);
const normalizedSignerAddress = signerAddress.toLowerCase();
+ const normalizedOrder = _.mapValues(order, value => {
+ return _.isObject(value) ? value.toString() : value;
+ });
const typedData = {
types: {
EIP712Domain: EIP712_DOMAIN_SCHEMA.parameters,
@@ -217,15 +239,7 @@ export const signatureUtils = {
version: EIP712_DOMAIN_VERSION,
verifyingContract: order.exchangeAddress,
},
- message: {
- ...order,
- salt: order.salt.toString(),
- makerFee: order.makerFee.toString(),
- takerFee: order.takerFee.toString(),
- makerAssetAmount: order.makerAssetAmount.toString(),
- takerAssetAmount: order.takerAssetAmount.toString(),
- expirationTimeSeconds: order.expirationTimeSeconds.toString(),
- },
+ message: normalizedOrder,
primaryType: 'Order',
};
const signature = await web3Wrapper.signTypedDataAsync(normalizedSignerAddress, typedData);
@@ -240,36 +254,23 @@ export const signatureUtils = {
return signatureHex;
},
/**
- * Signs an orderHash and returns it's elliptic curve signature and signature type.
+ * Signs a hash and returns its elliptic curve signature and signature type.
* This method currently supports TestRPC, Geth and Parity above and below V1.6.6
- * @param orderHash Hex encoded orderHash to sign.
+ * @param msgHash Hex encoded message to sign.
* @param signerAddress The hex encoded Ethereum address you wish to sign it with. This address
* must be available via the Provider supplied to 0x.js.
- * @param signerType Different signers add/require different prefixes to be prepended to the message being signed.
- * Since we cannot know ahead of time which signer you are using, you must supply a SignerType.
- * @return A hex encoded string containing the Elliptic curve signature generated by signing the orderHash and the Signature Type.
+ * @return A hex encoded string containing the Elliptic curve signature generated by signing the msgHash and the Signature Type.
*/
- async ecSignOrderHashAsync(
- provider: Provider,
- orderHash: string,
- signerAddress: string,
- signerType: SignerType,
- ): Promise<string> {
+ async ecSignHashAsync(provider: Provider, msgHash: string, signerAddress: string): Promise<string> {
assert.isWeb3Provider('provider', provider);
- assert.isHexString('orderHash', orderHash);
+ assert.isHexString('msgHash', msgHash);
assert.isETHAddressHex('signerAddress', signerAddress);
const web3Wrapper = new Web3Wrapper(provider);
await assert.isSenderAddressAsync('signerAddress', signerAddress, web3Wrapper);
const normalizedSignerAddress = signerAddress.toLowerCase();
- let msgHashHex = orderHash;
- const prefixedMsgHashHex = signatureUtils.addSignedMessagePrefix(orderHash, signerType);
- // Metamask incorrectly implements eth_sign and does not prefix the message as per the spec
- // Source: https://github.com/MetaMask/metamask-extension/commit/a9d36860bec424dcee8db043d3e7da6a5ff5672e
- if (signerType === SignerType.Metamask) {
- msgHashHex = prefixedMsgHashHex;
- }
- const signature = await web3Wrapper.signMessageAsync(normalizedSignerAddress, msgHashHex);
+ const signature = await web3Wrapper.signMessageAsync(normalizedSignerAddress, msgHash);
+ const prefixedMsgHashHex = signatureUtils.addSignedMessagePrefix(msgHash);
// HACK: There is no consensus on whether the signatureHex string should be formatted as
// v + r + s OR r + s + v, and different clients (even different versions of the same client)
@@ -286,10 +287,7 @@ export const signatureUtils = {
normalizedSignerAddress,
);
if (isValidRSVSignature) {
- const convertedSignatureHex = signatureUtils.convertECSignatureToSignatureHex(
- ecSignatureRSV,
- signerType,
- );
+ const convertedSignatureHex = signatureUtils.convertECSignatureToSignatureHex(ecSignatureRSV);
return convertedSignatureHex;
}
}
@@ -301,10 +299,7 @@ export const signatureUtils = {
normalizedSignerAddress,
);
if (isValidVRSSignature) {
- const convertedSignatureHex = signatureUtils.convertECSignatureToSignatureHex(
- ecSignatureVRS,
- signerType,
- );
+ const convertedSignatureHex = signatureUtils.convertECSignatureToSignatureHex(ecSignatureVRS);
return convertedSignatureHex;
}
}
@@ -312,30 +307,18 @@ export const signatureUtils = {
throw new Error(OrderError.InvalidSignature);
},
/**
- * Combines ECSignature with V,R,S and the relevant signature type for use in 0x protocol
+ * Combines ECSignature with V,R,S and the EthSign signature type for use in 0x protocol
* @param ecSignature The ECSignature of the signed data
- * @param signerType The SignerType of the signed data
* @return Hex encoded string of signature (v,r,s) with Signature Type
*/
- convertECSignatureToSignatureHex(ecSignature: ECSignature, signerType: SignerType): string {
+ convertECSignatureToSignatureHex(ecSignature: ECSignature): string {
const signatureBuffer = Buffer.concat([
ethUtil.toBuffer(ecSignature.v),
ethUtil.toBuffer(ecSignature.r),
ethUtil.toBuffer(ecSignature.s),
]);
const signatureHex = `0x${signatureBuffer.toString('hex')}`;
- let signatureType;
- switch (signerType) {
- case SignerType.Metamask:
- case SignerType.Ledger:
- case SignerType.Default: {
- signatureType = SignatureType.EthSign;
- break;
- }
- default:
- throw new Error(`Unrecognized SignerType: ${signerType}`);
- }
- const signatureWithType = signatureUtils.convertToSignatureWithType(signatureHex, signatureType);
+ const signatureWithType = signatureUtils.convertToSignatureWithType(signatureHex, SignatureType.EthSign);
return signatureWithType;
},
/**
@@ -352,28 +335,17 @@ export const signatureUtils = {
/**
* Adds the relevant prefix to the message being signed.
* @param message Message to sign
- * @param signerType The type of message prefix to add for a given SignerType. Different signers expect
- * specific message prefixes.
* @return Prefixed message
*/
- addSignedMessagePrefix(message: string, signerType: SignerType = SignerType.Default): string {
+ addSignedMessagePrefix(message: string): string {
assert.isString('message', message);
- assert.doesBelongToStringEnum('signerType', signerType, SignerType);
- switch (signerType) {
- case SignerType.Metamask:
- case SignerType.Ledger:
- case SignerType.Default: {
- const msgBuff = ethUtil.toBuffer(message);
- const prefixedMsgBuff = ethUtil.hashPersonalMessage(msgBuff);
- const prefixedMsgHex = ethUtil.bufferToHex(prefixedMsgBuff);
- return prefixedMsgHex;
- }
- default:
- throw new Error(`Unrecognized SignerType: ${signerType}`);
- }
+ const msgBuff = ethUtil.toBuffer(message);
+ const prefixedMsgBuff = ethUtil.hashPersonalMessage(msgBuff);
+ const prefixedMsgHex = ethUtil.bufferToHex(prefixedMsgBuff);
+ return prefixedMsgHex;
},
/**
- * Parse a 0x protocol hex-encoded signature string into it's ECSignature components
+ * Parse a 0x protocol hex-encoded signature string into its ECSignature components
* @param signature A hex encoded ecSignature 0x Protocol signature
* @return An ECSignature object with r,s,v parameters
*/
diff --git a/packages/order-utils/test/signature_utils_test.ts b/packages/order-utils/test/signature_utils_test.ts
index 40ce16165..03354cd65 100644
--- a/packages/order-utils/test/signature_utils_test.ts
+++ b/packages/order-utils/test/signature_utils_test.ts
@@ -1,4 +1,4 @@
-import { Order, SignatureType, SignerType } from '@0xproject/types';
+import { Order, SignatureType } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils';
import * as chai from 'chai';
import { JSONRPCErrorCallback, JSONRPCRequestPayload } from 'ethereum-types';
@@ -153,10 +153,17 @@ describe('Signature utils', () => {
]);
const signatureHex = `0x${signatureBuffer.toString('hex')}`;
const eip712Signature = await signatureUtils.ecSignOrderAsync(provider, order, makerAddress);
+ const isValidSignature = await signatureUtils.isValidSignatureAsync(
+ provider,
+ orderHashHex,
+ eip712Signature,
+ makerAddress,
+ );
expect(signatureHex).to.eq(eip712Signature);
+ expect(isValidSignature).to.eq(true);
});
});
- describe('#ecSignOrderHashAsync', () => {
+ describe('#ecSignHashAsync', () => {
let makerAddress: string;
before(async () => {
const availableAddreses = await web3Wrapper.getAvailableAddressesAsync();
@@ -166,12 +173,7 @@ describe('Signature utils', () => {
const orderHash = '0x6927e990021d23b1eb7b8789f6a6feaf98fe104bb0cf8259421b79f9a34222b0';
const expectedSignature =
'0x1b61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc3340349190569279751135161d22529dc25add4f6069af05be04cacbda2ace225403';
- const ecSignature = await signatureUtils.ecSignOrderHashAsync(
- provider,
- orderHash,
- makerAddress,
- SignerType.Default,
- );
+ const ecSignature = await signatureUtils.ecSignHashAsync(provider, orderHash, makerAddress);
expect(ecSignature).to.equal(expectedSignature);
});
it('should return the correct Signature for signatureHex concatenated as R + S + V', async () => {
@@ -197,12 +199,7 @@ describe('Signature utils', () => {
}
},
};
- const ecSignature = await signatureUtils.ecSignOrderHashAsync(
- fakeProvider,
- orderHash,
- makerAddress,
- SignerType.Default,
- );
+ const ecSignature = await signatureUtils.ecSignHashAsync(fakeProvider, orderHash, makerAddress);
expect(ecSignature).to.equal(expectedSignature);
});
it('should return the correct Signature for signatureHex concatenated as V + R + S', async () => {
@@ -225,56 +222,12 @@ describe('Signature utils', () => {
},
};
- const ecSignature = await signatureUtils.ecSignOrderHashAsync(
- fakeProvider,
- orderHash,
- makerAddress,
- SignerType.Default,
- );
- expect(ecSignature).to.equal(expectedSignature);
- });
- // Note this is due to a bug in Metamask where it does not prefix before signing, this is a known issue and is to be fixed in the future
- // Source: https://github.com/MetaMask/metamask-extension/commit/a9d36860bec424dcee8db043d3e7da6a5ff5672e
- it('should receive a payload modified with a prefix when Metamask is SignerType', async () => {
- const orderHash = '0x34decbedc118904df65f379a175bb39ca18209d6ce41d5ed549d54e6e0a95004';
- const orderHashPrefixed = '0xae70f31d26096291aa681b26cb7574563956221d0b4213631e1ef9df675d4cba';
- const expectedSignature =
- '0x1b117902c86dfb95fe0d1badd983ee166ad259b27acb220174cbb4460d872871137feabdfe76e05924b484789f79af4ee7fa29ec006cedce1bbf369320d034e10b03';
- // Generated from a MM eth_sign request from 0x5409ed021d9299bf6814279a6a1411a7e866a631 signing 0xae70f31d26096291aa681b26cb7574563956221d0b4213631e1ef9df675d4cba
- const metamaskSignature =
- '0x117902c86dfb95fe0d1badd983ee166ad259b27acb220174cbb4460d872871137feabdfe76e05924b484789f79af4ee7fa29ec006cedce1bbf369320d034e10b1b';
- const fakeProvider = {
- async sendAsync(payload: JSONRPCRequestPayload, callback: JSONRPCErrorCallback): Promise<void> {
- if (payload.method === 'eth_sign') {
- const [, message] = payload.params;
- expect(message).to.equal(orderHashPrefixed);
- callback(null, {
- id: 42,
- jsonrpc: '2.0',
- result: metamaskSignature,
- });
- } else {
- callback(null, { id: 42, jsonrpc: '2.0', result: [makerAddress] });
- }
- },
- };
-
- const ecSignature = await signatureUtils.ecSignOrderHashAsync(
- fakeProvider,
- orderHash,
- makerAddress,
- SignerType.Metamask,
- );
+ const ecSignature = await signatureUtils.ecSignHashAsync(fakeProvider, orderHash, makerAddress);
expect(ecSignature).to.equal(expectedSignature);
});
it('should return a valid signature', async () => {
const orderHash = '0x34decbedc118904df65f379a175bb39ca18209d6ce41d5ed549d54e6e0a95004';
- const ecSignature = await signatureUtils.ecSignOrderHashAsync(
- provider,
- orderHash,
- makerAddress,
- SignerType.Default,
- );
+ const ecSignature = await signatureUtils.ecSignHashAsync(provider, orderHash, makerAddress);
const isValidSignature = await signatureUtils.isValidSignatureAsync(
provider,
@@ -291,38 +244,11 @@ describe('Signature utils', () => {
r: '0xaca7da997ad177f040240cdccf6905b71ab16b74434388c3a72f34fd25d64393',
s: '0x46b2bac274ff29b48b3ea6e2d04c1336eaceafda3c53ab483fc3ff12fac3ebf2',
};
- it('should concatenate v,r,s and append the EthSign signature type when SignerType is Default', async () => {
+ it('should concatenate v,r,s and append the EthSign signature type', async () => {
const expectedSignatureWithSignatureType =
'0x1baca7da997ad177f040240cdccf6905b71ab16b74434388c3a72f34fd25d6439346b2bac274ff29b48b3ea6e2d04c1336eaceafda3c53ab483fc3ff12fac3ebf203';
- const signatureWithSignatureType = signatureUtils.convertECSignatureToSignatureHex(
- ecSignature,
- SignerType.Default,
- );
+ const signatureWithSignatureType = signatureUtils.convertECSignatureToSignatureHex(ecSignature);
expect(signatureWithSignatureType).to.equal(expectedSignatureWithSignatureType);
});
- it('should concatenate v,r,s and append the EthSign signature type when SignerType is Ledger', async () => {
- const expectedSignatureWithSignatureType =
- '0x1baca7da997ad177f040240cdccf6905b71ab16b74434388c3a72f34fd25d6439346b2bac274ff29b48b3ea6e2d04c1336eaceafda3c53ab483fc3ff12fac3ebf203';
- const signatureWithSignatureType = signatureUtils.convertECSignatureToSignatureHex(
- ecSignature,
- SignerType.Ledger,
- );
- expect(signatureWithSignatureType).to.equal(expectedSignatureWithSignatureType);
- });
- it('should concatenate v,r,s and append the EthSign signature type when SignerType is Metamask', async () => {
- const expectedSignatureWithSignatureType =
- '0x1baca7da997ad177f040240cdccf6905b71ab16b74434388c3a72f34fd25d6439346b2bac274ff29b48b3ea6e2d04c1336eaceafda3c53ab483fc3ff12fac3ebf203';
- const signatureWithSignatureType = signatureUtils.convertECSignatureToSignatureHex(
- ecSignature,
- SignerType.Metamask,
- );
- expect(signatureWithSignatureType).to.equal(expectedSignatureWithSignatureType);
- });
- it('should throw if the SignerType is invalid', async () => {
- const expectedMessage = 'Unrecognized SignerType: INVALID_SIGNER';
- expect(() =>
- signatureUtils.convertECSignatureToSignatureHex(ecSignature, 'INVALID_SIGNER' as SignerType),
- ).to.throw(expectedMessage);
- });
});
});
diff --git a/packages/subproviders/CHANGELOG.json b/packages/subproviders/CHANGELOG.json
index 97f886f64..30887c6fe 100644
--- a/packages/subproviders/CHANGELOG.json
+++ b/packages/subproviders/CHANGELOG.json
@@ -1,5 +1,13 @@
[
{
+ "version": "2.1.0",
+ "changes": [
+ {
+ "note": "Add Metamask Subprovider to handle inconsistent JSON RPC behaviour"
+ }
+ ]
+ },
+ {
"version": "2.0.7",
"changes": [
{
diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts
index b5f9b3f90..8b5446007 100644
--- a/packages/subproviders/src/index.ts
+++ b/packages/subproviders/src/index.ts
@@ -27,6 +27,7 @@ export { Subprovider } from './subproviders/subprovider';
export { NonceTrackerSubprovider } from './subproviders/nonce_tracker';
export { PrivateKeyWalletSubprovider } from './subproviders/private_key_wallet';
export { MnemonicWalletSubprovider } from './subproviders/mnemonic_wallet';
+export { MetamaskSubprovider } from './subproviders/metamask_subprovider';
export { EthLightwalletSubprovider } from './subproviders/eth_lightwallet_subprovider';
export {
diff --git a/packages/subproviders/src/subproviders/metamask_subprovider.ts b/packages/subproviders/src/subproviders/metamask_subprovider.ts
new file mode 100644
index 000000000..724edd574
--- /dev/null
+++ b/packages/subproviders/src/subproviders/metamask_subprovider.ts
@@ -0,0 +1,124 @@
+import { marshaller, Web3Wrapper } from '@0xproject/web3-wrapper';
+import { JSONRPCRequestPayload, Provider } from 'ethereum-types';
+import * as ethUtil from 'ethereumjs-util';
+
+import { Callback, ErrorCallback } from '../types';
+
+import { Subprovider } from './subprovider';
+
+/**
+ * This class implements the [web3-provider-engine](https://github.com/MetaMask/provider-engine)
+ * subprovider interface and the provider sendAsync interface.
+ * It handles inconsistencies with Metamask implementations of various JSON RPC methods.
+ * It forwards JSON RPC requests involving the domain of a signer (getAccounts,
+ * sendTransaction, signMessage etc...) to the provider instance supplied at instantiation. All other requests
+ * are passed onwards for subsequent subproviders to handle.
+ */
+export class MetamaskSubprovider extends Subprovider {
+ private readonly _web3Wrapper: Web3Wrapper;
+ private readonly _provider: Provider;
+ /**
+ * Instantiates a new SignerSubprovider
+ * @param provider Web3 provider that should handle all user account related requests
+ */
+ constructor(provider: Provider) {
+ super();
+ this._web3Wrapper = new Web3Wrapper(provider);
+ this._provider = provider;
+ }
+ /**
+ * This method conforms to the web3-provider-engine interface.
+ * It is called internally by the ProviderEngine when it is this subproviders
+ * turn to handle a JSON RPC request.
+ * @param payload JSON RPC payload
+ * @param next Callback to call if this subprovider decides not to handle the request
+ * @param end Callback to call if subprovider handled the request and wants to pass back the request.
+ */
+ // tslint:disable-next-line:prefer-function-over-method async-suffix
+ public async handleRequest(payload: JSONRPCRequestPayload, next: Callback, end: ErrorCallback): Promise<void> {
+ let message;
+ let address;
+ switch (payload.method) {
+ case 'web3_clientVersion':
+ try {
+ const nodeVersion = await this._web3Wrapper.getNodeVersionAsync();
+ end(null, nodeVersion);
+ } catch (err) {
+ end(err);
+ }
+ return;
+ case 'eth_accounts':
+ try {
+ const accounts = await this._web3Wrapper.getAvailableAddressesAsync();
+ end(null, accounts);
+ } catch (err) {
+ end(err);
+ }
+ return;
+ case 'eth_sendTransaction':
+ const [txParams] = payload.params;
+ try {
+ const txData = marshaller.unmarshalTxData(txParams);
+ const txHash = await this._web3Wrapper.sendTransactionAsync(txData);
+ end(null, txHash);
+ } catch (err) {
+ end(err);
+ }
+ return;
+ case 'eth_sign':
+ [address, message] = payload.params;
+ try {
+ // Metamask incorrectly implements eth_sign and does not prefix the message as per the spec
+ // Source: https://github.com/MetaMask/metamask-extension/commit/a9d36860bec424dcee8db043d3e7da6a5ff5672e
+ const msgBuff = ethUtil.toBuffer(message);
+ const prefixedMsgBuff = ethUtil.hashPersonalMessage(msgBuff);
+ const prefixedMsgHex = ethUtil.bufferToHex(prefixedMsgBuff);
+ const signature = await this._web3Wrapper.signMessageAsync(address, prefixedMsgHex);
+ signature ? end(null, signature) : end(new Error('Error performing eth_sign'), null);
+ } catch (err) {
+ end(err);
+ }
+ return;
+ case 'eth_signTypedData':
+ case 'eth_signTypedData_v3':
+ [address, message] = payload.params;
+ try {
+ // Metamask has namespaced signTypedData to v3 for an indeterminate period of time.
+ // and expects message to be serialised as JSON
+ const messageJSON = JSON.stringify(message);
+ const signature = await this._web3Wrapper.sendRawPayloadAsync<string>({
+ method: 'eth_signTypedData_v3',
+ params: [address, messageJSON],
+ });
+ signature ? end(null, signature) : end(new Error('Error performing eth_signTypedData'), null);
+ } catch (err) {
+ end(err);
+ }
+ return;
+ default:
+ next();
+ return;
+ }
+ }
+ /**
+ * This method conforms to the provider sendAsync interface.
+ * Allowing the MetamaskSubprovider to be used as a generic provider (outside of Web3ProviderEngine) with the
+ * addition of wrapping the inconsistent Metamask behaviour
+ * @param payload JSON RPC payload
+ * @return The contents nested under the result key of the response body
+ */
+ public sendAsync(payload: JSONRPCRequestPayload, callback: ErrorCallback): void {
+ void this.handleRequest(
+ payload,
+ // handleRequest has decided to not handle this, so fall through to the provider
+ () => {
+ const sendAsync = this._provider.sendAsync.bind(this._provider);
+ sendAsync(payload, callback);
+ },
+ // handleRequest has called end and will handle this
+ (err, data) => {
+ err ? callback(err) : callback(null, { ...payload, result: data });
+ },
+ );
+ }
+}
diff --git a/packages/subproviders/src/subproviders/private_key_wallet.ts b/packages/subproviders/src/subproviders/private_key_wallet.ts
index 9d6fc487e..dbd51e8d7 100644
--- a/packages/subproviders/src/subproviders/private_key_wallet.ts
+++ b/packages/subproviders/src/subproviders/private_key_wallet.ts
@@ -23,7 +23,7 @@ export class PrivateKeyWalletSubprovider extends BaseWalletSubprovider {
constructor(privateKey: string) {
assert.isString('privateKey', privateKey);
super();
- this._privateKeyBuffer = new Buffer(privateKey, 'hex');
+ this._privateKeyBuffer = Buffer.from(privateKey, 'hex');
this._address = `0x${ethUtil.privateToAddress(this._privateKeyBuffer).toString('hex')}`;
}
/**
diff --git a/packages/subproviders/src/subproviders/signer.ts b/packages/subproviders/src/subproviders/signer.ts
index d5fd86897..6b519865f 100644
--- a/packages/subproviders/src/subproviders/signer.ts
+++ b/packages/subproviders/src/subproviders/signer.ts
@@ -31,6 +31,8 @@ export class SignerSubprovider extends Subprovider {
*/
// tslint:disable-next-line:prefer-function-over-method async-suffix
public async handleRequest(payload: JSONRPCRequestPayload, next: Callback, end: ErrorCallback): Promise<void> {
+ let message;
+ let address;
switch (payload.method) {
case 'web3_clientVersion':
try {
@@ -59,7 +61,7 @@ export class SignerSubprovider extends Subprovider {
}
return;
case 'eth_sign':
- const [address, message] = payload.params;
+ [address, message] = payload.params;
try {
const signature = await this._web3Wrapper.signMessageAsync(address, message);
end(null, signature);
@@ -67,6 +69,15 @@ export class SignerSubprovider extends Subprovider {
end(err);
}
return;
+ case 'eth_signTypedData':
+ [address, message] = payload.params;
+ try {
+ const signature = await this._web3Wrapper.signTypedDataAsync(address, message);
+ end(null, signature);
+ } catch (err) {
+ end(err);
+ }
+ return;
default:
next();
return;
diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts
index 3ae0536d5..2f148f0e6 100644
--- a/packages/types/src/index.ts
+++ b/packages/types/src/index.ts
@@ -143,16 +143,6 @@ export enum SignatureType {
NSignatureTypes,
}
-/**
- * The type of the Signer implementation. Some signer implementations use different message prefixes or implement different
- * eth_sign behaviour (e.g Metamask). Default assumes a spec compliant `eth_sign`.
- */
-export enum SignerType {
- Default = 'DEFAULT',
- Ledger = 'LEDGER',
- Metamask = 'METAMASK',
-}
-
export enum AssetProxyId {
ERC20 = '0xf47261b0',
ERC721 = '0x02571792',
diff --git a/packages/website/ts/blockchain.ts b/packages/website/ts/blockchain.ts
index c420bbf3a..652f2eb1d 100644
--- a/packages/website/ts/blockchain.ts
+++ b/packages/website/ts/blockchain.ts
@@ -9,14 +9,14 @@ import {
ExchangeFillEventArgs,
IndexedFilterValues,
} from '@0xproject/contract-wrappers';
-import { assetDataUtils, orderHashUtils, signatureUtils, SignerType } from '@0xproject/order-utils';
+import { assetDataUtils, orderHashUtils, signatureUtils } from '@0xproject/order-utils';
import { EtherscanLinkSuffixes, utils as sharedUtils } from '@0xproject/react-shared';
import {
ledgerEthereumBrowserClientFactoryAsync,
LedgerSubprovider,
+ MetamaskSubprovider,
RedundantSubprovider,
RPCSubprovider,
- SignerSubprovider,
Web3ProviderEngine,
} from '@0xproject/subproviders';
import { SignedOrder, Token as ZeroExToken } from '@0xproject/types';
@@ -161,7 +161,7 @@ export class Blockchain {
// We catch all requests involving a users account and send it to the injectedWeb3
// instance. All other requests go to the public hosted node.
const provider = new Web3ProviderEngine();
- provider.addProvider(new SignerSubprovider(injectedWeb3.currentProvider));
+ provider.addProvider(new MetamaskSubprovider(injectedWeb3.currentProvider));
provider.addProvider(new FilterSubprovider());
const rpcSubproviders = _.map(publicNodeUrlsIfExistsForNetworkId, publicNodeUrl => {
return new RPCSubprovider(publicNodeUrl);
@@ -432,21 +432,7 @@ export class Blockchain {
}
this._showFlashMessageIfLedger();
const provider = this._contractWrappers.getProvider();
- const isLedgerSigner = !_.isUndefined(this._ledgerSubprovider);
- const injectedProvider = Blockchain._getInjectedWeb3().currentProvider;
- const isMetaMaskSigner = utils.getProviderType(injectedProvider) === Providers.Metamask;
- let signerType = SignerType.Default;
- if (isLedgerSigner) {
- signerType = SignerType.Ledger;
- } else if (isMetaMaskSigner) {
- signerType = SignerType.Metamask;
- }
- const ecSignatureString = await signatureUtils.ecSignOrderHashAsync(
- provider,
- orderHash,
- makerAddress,
- signerType,
- );
+ const ecSignatureString = await signatureUtils.ecSignHashAsync(provider, orderHash, makerAddress);
this._dispatcher.updateSignature(ecSignatureString);
return ecSignatureString;
}