* v2-prototype: (45 commits) Check length before accessing indices, add awaitTransactionSuccess where needed, and rename function Add back before/after snapshots for each test Rename Signer to Wallet, rename GAS_ESTIMATE to GAS_LIMIT Make preSigned and allowedValidators mappings public Change names of signature types Fix formatting and tests Make AssetProxyId last byte of assetData Add signer to txHash, allow approveValidator to be used with executeTransaction Update Whitelist Fix Exchange interface Increase block gas limit Use last byte of signature as signature type Remove TxOrigin signature type, modify whitelist to use Validator signature type Update Whitelist contract with comments, also require maker to be whitelisted Fix build Add example whitelist contract and minimum tests Add sample whitelist contract Add TxOrigin signature type and rearrange order of types Add approveValidator function Add Validator signature type ... # Conflicts: # packages/contracts/src/contracts/current/protocol/Exchange/MixinSignatureValidator.sol # packages/contracts/src/utils/types.ts # packages/contracts/test/exchange/transactions.ts # packages/order-utils/src/asset_proxy_utils.ts
94 lines
3.9 KiB
TypeScript
94 lines
3.9 KiB
TypeScript
import { BlockchainLifecycle } from '@0xproject/dev-utils';
|
|
import { orderHashUtils } from '@0xproject/order-utils';
|
|
import { assetProxyUtils } from '@0xproject/order-utils';
|
|
import { SignedOrder } from '@0xproject/types';
|
|
import { BigNumber } from '@0xproject/utils';
|
|
import * as chai from 'chai';
|
|
import ethUtil = require('ethereumjs-util');
|
|
|
|
import { TestSignatureValidatorContract } from '../../src/contract_wrappers/generated/test_signature_validator';
|
|
import { addressUtils } from '../../src/utils/address_utils';
|
|
import { artifacts } from '../../src/utils/artifacts';
|
|
import { chaiSetup } from '../../src/utils/chai_setup';
|
|
import { constants } from '../../src/utils/constants';
|
|
import { OrderFactory } from '../../src/utils/order_factory';
|
|
import { provider, txDefaults, web3Wrapper } from '../../src/utils/web3_wrapper';
|
|
|
|
chaiSetup.configure();
|
|
const expect = chai.expect;
|
|
|
|
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
|
|
|
|
describe('MixinSignatureValidator', () => {
|
|
let signedOrder: SignedOrder;
|
|
let orderFactory: OrderFactory;
|
|
let signatureValidator: TestSignatureValidatorContract;
|
|
|
|
before(async () => {
|
|
await blockchainLifecycle.startAsync();
|
|
});
|
|
after(async () => {
|
|
await blockchainLifecycle.revertAsync();
|
|
});
|
|
before(async () => {
|
|
const accounts = await web3Wrapper.getAvailableAddressesAsync();
|
|
const makerAddress = accounts[0];
|
|
signatureValidator = await TestSignatureValidatorContract.deployFrom0xArtifactAsync(
|
|
artifacts.TestSignatureValidator,
|
|
provider,
|
|
txDefaults,
|
|
);
|
|
|
|
const defaultOrderParams = {
|
|
...constants.STATIC_ORDER_PARAMS,
|
|
exchangeAddress: signatureValidator.address,
|
|
makerAddress,
|
|
feeRecipientAddress: addressUtils.generatePseudoRandomAddress(),
|
|
makerAssetData: assetProxyUtils.encodeERC20ProxyData(addressUtils.generatePseudoRandomAddress()),
|
|
takerAssetData: assetProxyUtils.encodeERC20ProxyData(addressUtils.generatePseudoRandomAddress()),
|
|
};
|
|
const privateKey = constants.TESTRPC_PRIVATE_KEYS[accounts.indexOf(makerAddress)];
|
|
orderFactory = new OrderFactory(privateKey, defaultOrderParams);
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
await blockchainLifecycle.startAsync();
|
|
});
|
|
afterEach(async () => {
|
|
await blockchainLifecycle.revertAsync();
|
|
});
|
|
|
|
describe('isValidSignature', () => {
|
|
beforeEach(async () => {
|
|
signedOrder = orderFactory.newSignedOrder();
|
|
});
|
|
|
|
it('should return true with a valid signature', async () => {
|
|
const orderHashHex = orderHashUtils.getOrderHashHex(signedOrder);
|
|
const isValidSignature = await signatureValidator.publicIsValidSignature.callAsync(
|
|
orderHashHex,
|
|
signedOrder.makerAddress,
|
|
signedOrder.signature,
|
|
);
|
|
expect(isValidSignature).to.be.true();
|
|
});
|
|
|
|
it('should return false with an invalid signature', async () => {
|
|
const v = ethUtil.toBuffer(signedOrder.signature.slice(0, 4));
|
|
const invalidR = ethUtil.sha3('invalidR');
|
|
const invalidS = ethUtil.sha3('invalidS');
|
|
const signatureType = ethUtil.toBuffer(`0x${signedOrder.signature.slice(-2)}`);
|
|
const invalidSigBuff = Buffer.concat([v, invalidR, invalidS, signatureType]);
|
|
const invalidSigHex = `0x${invalidSigBuff.toString('hex')}`;
|
|
signedOrder.signature = invalidSigHex;
|
|
const orderHashHex = orderHashUtils.getOrderHashHex(signedOrder);
|
|
const isValidSignature = await signatureValidator.publicIsValidSignature.callAsync(
|
|
orderHashHex,
|
|
signedOrder.makerAddress,
|
|
signedOrder.signature,
|
|
);
|
|
expect(isValidSignature).to.be.false();
|
|
});
|
|
});
|
|
});
|