@0x/contracts-exchange: Update Wallet signature type behavior to match v2.1.

`@0x/contracts-exchange`: Add EOA tests to `signature_validator`.
This commit is contained in:
Lawrence Forman
2019-08-07 15:48:30 -04:00
parent 6752fc9fe5
commit 3dd8dac146
5 changed files with 171 additions and 13 deletions

View File

@@ -18,7 +18,7 @@ import {
transactionHashUtils,
} from '@0x/order-utils';
import { SignatureType, SignedOrder, SignedZeroExTransaction } from '@0x/types';
import { BigNumber, StringRevertError } from '@0x/utils';
import { BigNumber, LibBytesRevertErrors, StringRevertError } from '@0x/utils';
import { LogWithDecodedArgs } from 'ethereum-types';
import ethUtil = require('ethereumjs-util');
@@ -236,6 +236,18 @@ blockchainTests.resets('MixinSignatureValidator', env => {
return expect(tx).to.revertWith(expectedError);
});
it('should revert when signer is an EOA and SignatureType=Wallet', async () => {
const hashHex = getCurrentHashHex();
const signatureHex = hexConcat(SignatureType.Wallet);
const expectedError = new LibBytesRevertErrors.InvalidByteOperationError(
LibBytesRevertErrors.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsFourRequired,
new BigNumber(0),
new BigNumber(4),
);
const tx = validateAsync(hashHex, signerAddress, signatureHex);
return expect(tx).to.revertWith(expectedError);
});
it('should revert when validator reverts and SignatureType=Wallet', async () => {
const hashHex = getCurrentHashHex(validatorWallet.address);
// Doesn't have to contain a real signature since our wallet contract
@@ -298,6 +310,18 @@ blockchainTests.resets('MixinSignatureValidator', env => {
return signatureValidator.isValidHashSignature.callAsync(_hashHex, _signerAddress, signatureHex);
};
it('should revert when signerAddress == 0', async () => {
const signatureHex = hexConcat(SignatureType.EIP712);
const expectedError = new ExchangeRevertErrors.SignatureError(
ExchangeRevertErrors.SignatureErrorCode.InvalidSigner,
hashHex,
constants.NULL_ADDRESS,
signatureHex,
);
const tx = validateAsync(hashHex, constants.NULL_ADDRESS, signatureHex);
return expect(tx).to.revertWith(expectedError);
});
it('should revert when SignatureType=Validator', async () => {
const signatureHex = hexConcat(SignatureType.Validator);
const expectedError = new ExchangeRevertErrors.SignatureError(
@@ -411,6 +435,23 @@ blockchainTests.resets('MixinSignatureValidator', env => {
return signatureValidator.isValidOrderSignature.callAsync(order, order.makerAddress, signatureHex);
};
it('should revert when signerAddress == 0', async () => {
const signatureHex = hexConcat(SignatureType.EIP712);
const orderHashHex = orderHashUtils.getOrderHashHex(signedOrder);
const expectedError = new ExchangeRevertErrors.SignatureError(
ExchangeRevertErrors.SignatureErrorCode.InvalidSigner,
orderHashHex,
constants.NULL_ADDRESS,
signatureHex,
);
const tx = signatureValidator.isValidOrderSignature.callAsync(
signedOrder,
constants.NULL_ADDRESS,
signatureHex,
);
return expect(tx).to.revertWith(expectedError);
});
it('should return true when SignatureType=Validator, signature is valid and validator is approved', async () => {
// Doesn't have to contain a real signature since our wallet contract
// just does a hash comparison.
@@ -544,9 +585,7 @@ blockchainTests.resets('MixinSignatureValidator', env => {
it('should revert when validator reverts and SignatureType=EIP1271Wallet', async () => {
signedOrder.makerAddress = validatorWallet.address;
// Doesn't have to contain a real signature since our wallet contract
// just does a hash comparison.
const signatureHex = hexConcat(generateRandomSignature(), SignatureType.EIP1271Wallet);
const signatureHex = hexConcat(SignatureType.EIP1271Wallet);
const orderHashHex = orderHashUtils.getOrderHashHex(signedOrder);
const expectedError = new ExchangeRevertErrors.SignatureWalletError(
orderHashHex,
@@ -558,6 +597,34 @@ blockchainTests.resets('MixinSignatureValidator', env => {
return expect(tx).to.revertWith(expectedError);
});
it('should revert when signer is an EOA and SignatureType=EIP1271Wallet', async () => {
const signatureHex = hexConcat(SignatureType.EIP1271Wallet);
const expectedError = new LibBytesRevertErrors.InvalidByteOperationError(
LibBytesRevertErrors.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsFourRequired,
new BigNumber(0),
new BigNumber(4),
);
const tx = signatureValidator.isValidOrderSignature.callAsync(signedOrder, signerAddress, signatureHex);
return expect(tx).to.revertWith(expectedError);
});
it('should revert when signer is an EOA and SignatureType=Validator', async () => {
const signatureHex = hexConcat(notSignerAddress, SignatureType.Validator);
const expectedError = new LibBytesRevertErrors.InvalidByteOperationError(
LibBytesRevertErrors.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsFourRequired,
new BigNumber(0),
new BigNumber(4),
);
// Register an EOA as a validator.
await signatureValidator.setSignatureValidatorApproval.awaitTransactionSuccessAsync(
notSignerAddress,
true,
{ from: signerAddress },
);
const tx = signatureValidator.isValidOrderSignature.callAsync(signedOrder, signerAddress, signatureHex);
return expect(tx).to.revertWith(expectedError);
});
// Run hash-only signature type tests as well.
const validateOrderHashAsync = async (
hashHex: string,
@@ -618,6 +685,23 @@ blockchainTests.resets('MixinSignatureValidator', env => {
);
};
it('should revert when signerAddress == 0', async () => {
const signatureHex = hexConcat(SignatureType.EIP712);
const transactionHashHex = transactionHashUtils.getTransactionHashHex(signedTransaction);
const expectedError = new ExchangeRevertErrors.SignatureError(
ExchangeRevertErrors.SignatureErrorCode.InvalidSigner,
transactionHashHex,
constants.NULL_ADDRESS,
signatureHex,
);
const tx = signatureValidator.isValidTransactionSignature.callAsync(
signedTransaction,
constants.NULL_ADDRESS,
signatureHex,
);
return expect(tx).to.revertWith(expectedError);
});
it('should return true when SignatureType=Validator, signature is valid and validator is approved', async () => {
// Doesn't have to contain a real signature since our wallet contract
// just does a hash comparison.
@@ -765,6 +849,42 @@ blockchainTests.resets('MixinSignatureValidator', env => {
return expect(tx).to.revertWith(expectedError);
});
it('should revert when signer is an EOA and SignatureType=EIP1271Wallet', async () => {
const signatureHex = hexConcat(SignatureType.EIP1271Wallet);
const expectedError = new LibBytesRevertErrors.InvalidByteOperationError(
LibBytesRevertErrors.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsFourRequired,
new BigNumber(0),
new BigNumber(4),
);
const tx = signatureValidator.isValidTransactionSignature.callAsync(
signedTransaction,
signerAddress,
signatureHex,
);
return expect(tx).to.revertWith(expectedError);
});
it('should revert when signer is an EOA and SignatureType=Validator', async () => {
const signatureHex = hexConcat(notSignerAddress, SignatureType.Validator);
const expectedError = new LibBytesRevertErrors.InvalidByteOperationError(
LibBytesRevertErrors.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsFourRequired,
new BigNumber(0),
new BigNumber(4),
);
// Register an EOA as a validator.
await signatureValidator.setSignatureValidatorApproval.awaitTransactionSuccessAsync(
notSignerAddress,
true,
{ from: signerAddress },
);
const tx = signatureValidator.isValidTransactionSignature.callAsync(
signedTransaction,
signerAddress,
signatureHex,
);
return expect(tx).to.revertWith(expectedError);
});
// Run hash-only signature type tests as well.
const validateOrderHashAsync = async (
hashHex: string,