diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2018-04-25 02:24:55 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-04-27 01:33:37 +0800 |
commit | 0e0a46f373e076843fd4118cdafb325fb754141a (patch) | |
tree | 65c012961c6fa72dd258a2e0e0e671a0a6dc64a6 /packages/contracts/src/utils | |
parent | 732202fe8e4f3eb1847515a0c3f8f2d068d35baf (diff) | |
download | dexon-0x-contracts-0e0a46f373e076843fd4118cdafb325fb754141a.tar.gz dexon-0x-contracts-0e0a46f373e076843fd4118cdafb325fb754141a.tar.zst dexon-0x-contracts-0e0a46f373e076843fd4118cdafb325fb754141a.zip |
Update tests and utils
Diffstat (limited to 'packages/contracts/src/utils')
-rw-r--r-- | packages/contracts/src/utils/crypto.ts | 2 | ||||
-rw-r--r-- | packages/contracts/src/utils/exchange_wrapper.ts | 16 | ||||
-rw-r--r-- | packages/contracts/src/utils/order_factory.ts | 1 | ||||
-rw-r--r-- | packages/contracts/src/utils/order_utils.ts | 3 | ||||
-rw-r--r-- | packages/contracts/src/utils/transaction_factory.ts | 35 | ||||
-rw-r--r-- | packages/contracts/src/utils/types.ts | 9 |
6 files changed, 65 insertions, 1 deletions
diff --git a/packages/contracts/src/utils/crypto.ts b/packages/contracts/src/utils/crypto.ts index 222cb7cca..85a37122a 100644 --- a/packages/contracts/src/utils/crypto.ts +++ b/packages/contracts/src/utils/crypto.ts @@ -29,6 +29,8 @@ export const crypto = { args[i] = new BN(arg.toString(10), 10); } else if (ethUtil.isValidAddress(arg)) { argTypes.push('address'); + } else if (arg instanceof Buffer) { + argTypes.push('bytes'); } else if (_.isString(arg)) { argTypes.push('string'); } else if (_.isBuffer(arg)) { diff --git a/packages/contracts/src/utils/exchange_wrapper.ts b/packages/contracts/src/utils/exchange_wrapper.ts index 7d06c321b..000905854 100644 --- a/packages/contracts/src/utils/exchange_wrapper.ts +++ b/packages/contracts/src/utils/exchange_wrapper.ts @@ -9,7 +9,7 @@ import { constants } from './constants'; import { formatters } from './formatters'; import { LogDecoder } from './log_decoder'; import { orderUtils } from './order_utils'; -import { AssetProxyId, SignedOrder } from './types'; +import { AssetProxyId, SignedOrder, SignedTransaction } from './types'; export class ExchangeWrapper { private _exchange: ExchangeContract; @@ -207,6 +207,20 @@ export class ExchangeWrapper { const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } + public async executeTransactionAsync( + signedTx: SignedTransaction, + from: string, + ): Promise<TransactionReceiptWithDecodedLogs> { + const txHash = await this._exchange.executeTransaction.sendTransactionAsync( + signedTx.salt, + signedTx.signer, + signedTx.data, + signedTx.signature, + { from }, + ); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + return tx; + } public async getOrderHashAsync(signedOrder: SignedOrder): Promise<string> { const order = orderUtils.getOrderStruct(signedOrder); const orderHash = await this._exchange.getOrderHash.callAsync(order); diff --git a/packages/contracts/src/utils/order_factory.ts b/packages/contracts/src/utils/order_factory.ts index 08853054d..044e9b865 100644 --- a/packages/contracts/src/utils/order_factory.ts +++ b/packages/contracts/src/utils/order_factory.ts @@ -19,6 +19,7 @@ export class OrderFactory { ): SignedOrder { const randomExpiration = new BigNumber(Math.floor((Date.now() + Math.random() * 100000000000) / 1000)); const order = ({ + senderAddress: ZeroEx.NULL_ADDRESS, expirationTimeSeconds: randomExpiration, salt: ZeroEx.generatePseudoRandomSalt(), takerAddress: ZeroEx.NULL_ADDRESS, diff --git a/packages/contracts/src/utils/order_utils.ts b/packages/contracts/src/utils/order_utils.ts index 52eb44941..10bbf4f7c 100644 --- a/packages/contracts/src/utils/order_utils.ts +++ b/packages/contracts/src/utils/order_utils.ts @@ -24,6 +24,7 @@ export const orderUtils = { }, getOrderStruct(signedOrder: SignedOrder): OrderStruct { const orderStruct = { + senderAddress: signedOrder.senderAddress, makerAddress: signedOrder.makerAddress, takerAddress: signedOrder.takerAddress, feeRecipientAddress: signedOrder.feeRecipientAddress, @@ -44,6 +45,7 @@ export const orderUtils = { 'address makerAddress', 'address takerAddress', 'address feeRecipientAddress', + 'address senderAddress', 'uint256 makerAssetAmount', 'uint256 takerAssetAmount', 'uint256 makerFee', @@ -58,6 +60,7 @@ export const orderUtils = { order.makerAddress, order.takerAddress, order.feeRecipientAddress, + order.senderAddress, order.makerAssetAmount, order.takerAssetAmount, order.makerFee, diff --git a/packages/contracts/src/utils/transaction_factory.ts b/packages/contracts/src/utils/transaction_factory.ts new file mode 100644 index 000000000..3a4f48330 --- /dev/null +++ b/packages/contracts/src/utils/transaction_factory.ts @@ -0,0 +1,35 @@ +import { ZeroEx } from '0x.js'; +import { BigNumber } from '@0xproject/utils'; +import * as ethUtil from 'ethereumjs-util'; + +import { crypto } from './crypto'; +import { signingUtils } from './signing_utils'; +import { SignatureType, SignedTransaction } from './types'; + +export class TransactionFactory { + private _signer: string; + private _exchangeAddress: string; + private _privateKey: Buffer; + constructor(privateKey: Buffer, exchangeAddress: string) { + this._privateKey = privateKey; + this._exchangeAddress = exchangeAddress; + const signerBuff = ethUtil.privateToAddress(this._privateKey); + this._signer = `0x${signerBuff.toString('hex')}`; + } + public newSignedTransaction( + data: string, + signatureType: SignatureType = SignatureType.Ecrecover, + ): SignedTransaction { + const salt = ZeroEx.generatePseudoRandomSalt(); + const txHash = crypto.solSHA3([this._exchangeAddress, salt, ethUtil.toBuffer(data)]); + const signature = signingUtils.signMessage(txHash, this._privateKey, signatureType); + const signedTx = { + exchangeAddress: this._exchangeAddress, + salt, + signer: this._signer, + data, + signature: `0x${signature.toString('hex')}`, + }; + return signedTx; + } +} diff --git a/packages/contracts/src/utils/types.ts b/packages/contracts/src/utils/types.ts index a23b6b876..934dc52fa 100644 --- a/packages/contracts/src/utils/types.ts +++ b/packages/contracts/src/utils/types.ts @@ -122,6 +122,7 @@ export interface SignedOrder extends UnsignedOrder { } export interface OrderStruct { + senderAddress: string; makerAddress: string; takerAddress: string; feeRecipientAddress: string; @@ -148,3 +149,11 @@ export enum SignatureType { Trezor, Contract, } + +export interface SignedTransaction { + exchangeAddress: string; + salt: BigNumber; + signer: string; + data: string; + signature: string; +} |