Use new transaction schema throughout monorepo

This commit is contained in:
Amir Bandeali 2019-02-01 16:28:29 -08:00
parent c7c4cb9bc6
commit a394967268
7 changed files with 21 additions and 37 deletions

View File

@ -8,14 +8,13 @@ import {
OrderFactory, OrderFactory,
orderUtils, orderUtils,
provider, provider,
SignedTransaction,
TransactionFactory, TransactionFactory,
txDefaults, txDefaults,
web3Wrapper, web3Wrapper,
} from '@0x/contracts-test-utils'; } from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils'; import { BlockchainLifecycle } from '@0x/dev-utils';
import { assetDataUtils, generatePseudoRandomSalt } from '@0x/order-utils'; import { assetDataUtils, generatePseudoRandomSalt } from '@0x/order-utils';
import { OrderWithoutExchangeAddress, RevertReason, SignedOrder } from '@0x/types'; import { OrderWithoutExchangeAddress, RevertReason, SignedOrder, SignedZeroExTransaction } from '@0x/types';
import { BigNumber } from '@0x/utils'; import { BigNumber } from '@0x/utils';
import * as chai from 'chai'; import * as chai from 'chai';
import * as _ from 'lodash'; import * as _ from 'lodash';
@ -41,7 +40,7 @@ describe('Exchange transactions', () => {
let erc20Balances: ERC20BalancesByOwner; let erc20Balances: ERC20BalancesByOwner;
let signedOrder: SignedOrder; let signedOrder: SignedOrder;
let signedTx: SignedTransaction; let signedTx: SignedZeroExTransaction;
let orderWithoutExchangeAddress: OrderWithoutExchangeAddress; let orderWithoutExchangeAddress: OrderWithoutExchangeAddress;
let orderFactory: OrderFactory; let orderFactory: OrderFactory;
let makerTransactionFactory: TransactionFactory; let makerTransactionFactory: TransactionFactory;

View File

@ -1,14 +1,7 @@
import { artifacts as erc20Artifacts } from '@0x/contracts-erc20'; import { artifacts as erc20Artifacts } from '@0x/contracts-erc20';
import { artifacts as erc721Artifacts } from '@0x/contracts-erc721'; import { artifacts as erc721Artifacts } from '@0x/contracts-erc721';
import { import { FillResults, formatters, LogDecoder, OrderInfo, orderUtils } from '@0x/contracts-test-utils';
FillResults, import { SignedOrder, SignedZeroExTransaction } from '@0x/types';
formatters,
LogDecoder,
OrderInfo,
orderUtils,
SignedTransaction,
} from '@0x/contracts-test-utils';
import { SignedOrder } from '@0x/types';
import { AbiEncoder, BigNumber } from '@0x/utils'; import { AbiEncoder, BigNumber } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper'; import { Web3Wrapper } from '@0x/web3-wrapper';
import { MethodAbi, Provider, TransactionReceiptWithDecodedLogs } from 'ethereum-types'; import { MethodAbi, Provider, TransactionReceiptWithDecodedLogs } from 'ethereum-types';
@ -206,7 +199,7 @@ export class ExchangeWrapper {
return tx; return tx;
} }
public async executeTransactionAsync( public async executeTransactionAsync(
signedTx: SignedTransaction, signedTx: SignedZeroExTransaction,
from: string, from: string,
): Promise<TransactionReceiptWithDecodedLogs> { ): Promise<TransactionReceiptWithDecodedLogs> {
const txHash = await this._exchange.executeTransaction.sendTransactionAsync( const txHash = await this._exchange.executeTransaction.sendTransactionAsync(

View File

@ -31,7 +31,6 @@ export {
MarketBuyOrders, MarketBuyOrders,
MarketSellOrders, MarketSellOrders,
ERC721TokenIdsByOwner, ERC721TokenIdsByOwner,
SignedTransaction,
OrderStatus, OrderStatus,
AllowanceAmountScenario, AllowanceAmountScenario,
AssetDataScenario, AssetDataScenario,

View File

@ -1,10 +1,8 @@
import { eip712Utils, generatePseudoRandomSalt } from '@0x/order-utils'; import { generatePseudoRandomSalt, transactionHashUtils } from '@0x/order-utils';
import { SignatureType } from '@0x/types'; import { SignatureType, SignedZeroExTransaction } from '@0x/types';
import { signTypedDataUtils } from '@0x/utils';
import * as ethUtil from 'ethereumjs-util'; import * as ethUtil from 'ethereumjs-util';
import { signingUtils } from './signing_utils'; import { signingUtils } from './signing_utils';
import { SignedTransaction } from './types';
export class TransactionFactory { export class TransactionFactory {
private readonly _signerBuff: Buffer; private readonly _signerBuff: Buffer;
@ -15,23 +13,25 @@ export class TransactionFactory {
this._exchangeAddress = exchangeAddress; this._exchangeAddress = exchangeAddress;
this._signerBuff = ethUtil.privateToAddress(this._privateKey); this._signerBuff = ethUtil.privateToAddress(this._privateKey);
} }
public newSignedTransaction(data: string, signatureType: SignatureType = SignatureType.EthSign): SignedTransaction { public newSignedTransaction(
data: string,
signatureType: SignatureType = SignatureType.EthSign,
): SignedZeroExTransaction {
const salt = generatePseudoRandomSalt(); const salt = generatePseudoRandomSalt();
const signerAddress = `0x${this._signerBuff.toString('hex')}`; const signerAddress = `0x${this._signerBuff.toString('hex')}`;
const executeTransactionData = { const transaction = {
salt, salt,
signerAddress, signerAddress,
data, data,
verifyingContractAddress: this._exchangeAddress,
}; };
const typedData = eip712Utils.createZeroExTransactionTypedData(executeTransactionData, this._exchangeAddress); const transactionHashBuffer = transactionHashUtils.getTransactionHashBuffer(transaction);
const eip712MessageBuffer = signTypedDataUtils.generateTypedDataHash(typedData); const signature = signingUtils.signMessage(transactionHashBuffer, this._privateKey, signatureType);
const signature = signingUtils.signMessage(eip712MessageBuffer, this._privateKey, signatureType); const signedTransaction = {
const signedTx = { ...transaction,
exchangeAddress: this._exchangeAddress,
signature: `0x${signature.toString('hex')}`, signature: `0x${signature.toString('hex')}`,
...executeTransactionData,
}; };
return signedTx; return signedTransaction;
} }
} }

View File

@ -107,14 +107,6 @@ export enum ContractName {
BalanceThresholdFilter = 'BalanceThresholdFilter', BalanceThresholdFilter = 'BalanceThresholdFilter',
} }
export interface SignedTransaction {
exchangeAddress: string;
salt: BigNumber;
signerAddress: string;
data: string;
signature: string;
}
export interface TransferAmountsByMatchOrders { export interface TransferAmountsByMatchOrders {
// Left Maker // Left Maker
amountBoughtByLeftMaker: BigNumber; amountBoughtByLeftMaker: BigNumber;

View File

@ -28,11 +28,12 @@ export class TransactionEncoder {
public getTransactionHex(data: string, salt: BigNumber, signerAddress: string): string { public getTransactionHex(data: string, salt: BigNumber, signerAddress: string): string {
const exchangeAddress = this._getExchangeContract().address; const exchangeAddress = this._getExchangeContract().address;
const executeTransactionData = { const executeTransactionData = {
verifyingContractAddress: exchangeAddress,
salt, salt,
signerAddress, signerAddress,
data, data,
}; };
const typedData = eip712Utils.createZeroExTransactionTypedData(executeTransactionData, exchangeAddress); const typedData = eip712Utils.createZeroExTransactionTypedData(executeTransactionData);
const eip712MessageBuffer = signTypedDataUtils.generateTypedDataHash(typedData); const eip712MessageBuffer = signTypedDataUtils.generateTypedDataHash(typedData);
const messageHex = `0x${eip712MessageBuffer.toString('hex')}`; const messageHex = `0x${eip712MessageBuffer.toString('hex')}`;
return messageHex; return messageHex;

View File

@ -12,7 +12,7 @@ import { chaiSetup } from './utils/chai_setup';
chaiSetup.configure(); chaiSetup.configure();
const expect = chai.expect; const expect = chai.expect;
describe.only('0x transaction hashing', () => { describe('0x transaction hashing', () => {
describe('#getTransactionHashHex', () => { describe('#getTransactionHashHex', () => {
const expectedTransactionHash = '0x82c9bb2dcac4f868ec7a15c20ff6175cfc384c20ae6a872aa0342a840f108c2b'; const expectedTransactionHash = '0x82c9bb2dcac4f868ec7a15c20ff6175cfc384c20ae6a872aa0342a840f108c2b';
const fakeVerifyingContractAddress = '0x5e72914535f202659083db3a02c984188fa26e9f'; const fakeVerifyingContractAddress = '0x5e72914535f202659083db3a02c984188fa26e9f';