From 0499541e114e6dd36565428f4f914e0dbdece2b8 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 24 Apr 2018 16:36:35 +0200 Subject: Move order utils to @0xproject/order-utils --- packages/0x.js/src/0x.ts | 144 ++++++++++++++++------------------------------- 1 file changed, 50 insertions(+), 94 deletions(-) (limited to 'packages/0x.js/src/0x.ts') diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index 94d97c23e..780d1b52a 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -1,4 +1,12 @@ import { schemas, SchemaValidator } from '@0xproject/json-schemas'; +import { + assert, + generatePseudoRandomSalt, + getOrderHashHex, + isValidOrderHash, + isValidSignature, + signOrderHashAsync, +} from '@0xproject/order-utils'; import { ECSignature, Order, Provider, SignedOrder, TransactionReceiptWithDecodedLogs } from '@0xproject/types'; import { AbiDecoder, BigNumber, intervalUtils } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; @@ -16,10 +24,8 @@ import { zeroExConfigSchema } from './schemas/zero_ex_config_schema'; import { zeroExPrivateNetworkConfigSchema } from './schemas/zero_ex_private_network_config_schema'; import { zeroExPublicNetworkConfigSchema } from './schemas/zero_ex_public_network_config_schema'; import { OrderStateWatcherConfig, ZeroExConfig, ZeroExError } from './types'; -import { assert } from './utils/assert'; import { constants } from './utils/constants'; import { decorators } from './utils/decorators'; -import { signatureUtils } from './utils/signature_utils'; import { utils } from './utils/utils'; /** @@ -33,6 +39,36 @@ export class ZeroEx { * this constant for your convenience. */ public static NULL_ADDRESS = constants.NULL_ADDRESS; + /** + * Generates a pseudo-random 256-bit salt. + * The salt can be included in a 0x order, ensuring that the order generates a unique orderHash + * and will not collide with other outstanding orders that are identical in all other parameters. + * @return A pseudo-random 256-bit number that can be used as a salt. + */ + public static generatePseudoRandomSalt = generatePseudoRandomSalt; + /** + * Verifies that the elliptic curve signature `signature` was generated + * by signing `data` with the private key corresponding to the `signerAddress` address. + * @param data The hex encoded data signed by the supplied signature. + * @param signature An object containing the elliptic curve signature parameters. + * @param signerAddress The hex encoded address that signed the data, producing the supplied signature. + * @return Whether the signature is valid for the supplied signerAddress and data. + */ + public static isValidSignature = isValidSignature; + /** + * Computes the orderHash for a supplied order. + * @param order An object that conforms to the Order or SignedOrder interface definitions. + * @return The resulting orderHash from hashing the supplied order. + */ + public static getOrderHashHex = getOrderHashHex; + /** + * Checks if the supplied hex encoded order hash is valid. + * Note: Valid means it has the expected format, not that an order with the orderHash exists. + * Use this method when processing orderHashes submitted as user input. + * @param orderHash Hex encoded orderHash. + * @return Whether the supplied orderHash has the expected format. + */ + public static isValidOrderHash = isValidOrderHash; /** * An instance of the ExchangeWrapper class containing methods for interacting with the 0x Exchange smart contract. @@ -58,52 +94,6 @@ export class ZeroEx { */ public proxy: TokenTransferProxyWrapper; private _web3Wrapper: Web3Wrapper; - /** - * Verifies that the elliptic curve signature `signature` was generated - * by signing `data` with the private key corresponding to the `signerAddress` address. - * @param data The hex encoded data signed by the supplied signature. - * @param signature An object containing the elliptic curve signature parameters. - * @param signerAddress The hex encoded address that signed the data, producing the supplied signature. - * @return Whether the signature is valid for the supplied signerAddress and data. - */ - public static isValidSignature(data: string, signature: ECSignature, signerAddress: string): boolean { - assert.isHexString('data', data); - assert.doesConformToSchema('signature', signature, schemas.ecSignatureSchema); - assert.isETHAddressHex('signerAddress', signerAddress); - const normalizedSignerAddress = signerAddress.toLowerCase(); - - const isValidSignature = signatureUtils.isValidSignature(data, signature, normalizedSignerAddress); - return isValidSignature; - } - /** - * Generates a pseudo-random 256-bit salt. - * The salt can be included in a 0x order, ensuring that the order generates a unique orderHash - * and will not collide with other outstanding orders that are identical in all other parameters. - * @return A pseudo-random 256-bit number that can be used as a salt. - */ - public static generatePseudoRandomSalt(): BigNumber { - // BigNumber.random returns a pseudo-random number between 0 & 1 with a passed in number of decimal places. - // Source: https://mikemcl.github.io/bignumber.js/#random - const randomNumber = BigNumber.random(constants.MAX_DIGITS_IN_UNSIGNED_256_INT); - const factor = new BigNumber(10).pow(constants.MAX_DIGITS_IN_UNSIGNED_256_INT - 1); - const salt = randomNumber.times(factor).round(); - return salt; - } - /** - * Checks if the supplied hex encoded order hash is valid. - * Note: Valid means it has the expected format, not that an order with the orderHash exists. - * Use this method when processing orderHashes submitted as user input. - * @param orderHash Hex encoded orderHash. - * @return Whether the supplied orderHash has the expected format. - */ - public static isValidOrderHash(orderHash: string): boolean { - // Since this method can be called to check if any arbitrary string conforms to an orderHash's - // format, we only assert that we were indeed passed a string. - assert.isString('orderHash', orderHash); - const schemaValidator = new SchemaValidator(); - const isValidOrderHash = schemaValidator.validate(orderHash, schemas.orderHashSchema).valid; - return isValidOrderHash; - } /** * A unit amount is defined as the amount of a token above the specified decimal places (integer part). * E.g: If a currency has 18 decimal places, 1e18 or one quintillion of the currency is equivalent @@ -132,17 +122,6 @@ export class ZeroEx { const baseUnitAmount = Web3Wrapper.toBaseUnitAmount(amount, decimals); return baseUnitAmount; } - /** - * Computes the orderHash for a supplied order. - * @param order An object that conforms to the Order or SignedOrder interface definitions. - * @return The resulting orderHash from hashing the supplied order. - */ - @decorators.syncZeroExErrorHandler - public static getOrderHashHex(order: Order | SignedOrder): string { - assert.doesConformToSchema('order', order, schemas.orderSchema); - const orderHashHex = utils.getOrderHashHex(order); - return orderHashHex; - } /** * Instantiates a new ZeroEx instance that provides the public interface to the 0x.js library. * @param provider The Provider instance you would like the 0x.js library to use for interacting with @@ -204,6 +183,12 @@ export class ZeroEx { (this.etherToken as any)._invalidateContractInstance(); (this.etherToken as any)._setNetworkId(networkId); } + /** + * Get the provider instance currently used by 0x.js + */ + public getProvider(): Provider { + return this._web3Wrapper.getProvider(); + } /** * Get user Ethereum addresses available through the supplied web3 provider available for sending transactions. * @return An array of available user Ethereum addresses. @@ -229,41 +214,12 @@ export class ZeroEx { signerAddress: string, shouldAddPersonalMessagePrefix: boolean, ): Promise { - assert.isHexString('orderHash', orderHash); - await assert.isSenderAddressAsync('signerAddress', signerAddress, this._web3Wrapper); - const normalizedSignerAddress = signerAddress.toLowerCase(); - - let msgHashHex = orderHash; - if (shouldAddPersonalMessagePrefix) { - const orderHashBuff = ethUtil.toBuffer(orderHash); - const msgHashBuff = ethUtil.hashPersonalMessage(orderHashBuff); - msgHashHex = ethUtil.bufferToHex(msgHashBuff); - } - - const signature = await this._web3Wrapper.signMessageAsync(normalizedSignerAddress, msgHashHex); - - // 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) - // return the signature params in different orders. In order to support all client implementations, - // we parse the signature in both ways, and evaluate if either one is a valid signature. - const validVParamValues = [27, 28]; - const ecSignatureVRS = signatureUtils.parseSignatureHexAsVRS(signature); - if (_.includes(validVParamValues, ecSignatureVRS.v)) { - const isValidVRSSignature = ZeroEx.isValidSignature(orderHash, ecSignatureVRS, normalizedSignerAddress); - if (isValidVRSSignature) { - return ecSignatureVRS; - } - } - - const ecSignatureRSV = signatureUtils.parseSignatureHexAsRSV(signature); - if (_.includes(validVParamValues, ecSignatureRSV.v)) { - const isValidRSVSignature = ZeroEx.isValidSignature(orderHash, ecSignatureRSV, normalizedSignerAddress); - if (isValidRSVSignature) { - return ecSignatureRSV; - } - } - - throw new Error(ZeroExError.InvalidSignature); + return signOrderHashAsync( + this._web3Wrapper.getProvider(), + orderHash, + signerAddress, + shouldAddPersonalMessagePrefix, + ); } /** * Waits for a transaction to be mined and returns the transaction receipt. -- cgit v1.2.3 From adaff92c9243e7e13737f92cf4b6ade77ebd6e43 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 2 May 2018 15:25:30 +0300 Subject: Create wrapper functions so that docs render properly --- packages/0x.js/src/0x.ts | 66 +++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 29 deletions(-) (limited to 'packages/0x.js/src/0x.ts') diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index 780d1b52a..bf776cd95 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -39,13 +39,39 @@ export class ZeroEx { * this constant for your convenience. */ public static NULL_ADDRESS = constants.NULL_ADDRESS; + /** + * An instance of the ExchangeWrapper class containing methods for interacting with the 0x Exchange smart contract. + */ + public exchange: ExchangeWrapper; + /** + * An instance of the TokenRegistryWrapper class containing methods for interacting with the 0x + * TokenRegistry smart contract. + */ + public tokenRegistry: TokenRegistryWrapper; + /** + * An instance of the TokenWrapper class containing methods for interacting with any ERC20 token smart contract. + */ + public token: TokenWrapper; + /** + * An instance of the EtherTokenWrapper class containing methods for interacting with the + * wrapped ETH ERC20 token smart contract. + */ + public etherToken: EtherTokenWrapper; + /** + * An instance of the TokenTransferProxyWrapper class containing methods for interacting with the + * tokenTransferProxy smart contract. + */ + public proxy: TokenTransferProxyWrapper; + private _web3Wrapper: Web3Wrapper; /** * Generates a pseudo-random 256-bit salt. * The salt can be included in a 0x order, ensuring that the order generates a unique orderHash * and will not collide with other outstanding orders that are identical in all other parameters. * @return A pseudo-random 256-bit number that can be used as a salt. */ - public static generatePseudoRandomSalt = generatePseudoRandomSalt; + public static generatePseudoRandomSalt(): BigNumber { + return generatePseudoRandomSalt(); + } /** * Verifies that the elliptic curve signature `signature` was generated * by signing `data` with the private key corresponding to the `signerAddress` address. @@ -54,13 +80,17 @@ export class ZeroEx { * @param signerAddress The hex encoded address that signed the data, producing the supplied signature. * @return Whether the signature is valid for the supplied signerAddress and data. */ - public static isValidSignature = isValidSignature; + public static isValidSignature(data: string, signature: ECSignature, signerAddress: string): boolean { + return isValidSignature(data, signature, signerAddress); + } /** * Computes the orderHash for a supplied order. * @param order An object that conforms to the Order or SignedOrder interface definitions. * @return The resulting orderHash from hashing the supplied order. */ - public static getOrderHashHex = getOrderHashHex; + public static getOrderHashHex(order: Order | SignedOrder): string { + return getOrderHashHex(order); + } /** * Checks if the supplied hex encoded order hash is valid. * Note: Valid means it has the expected format, not that an order with the orderHash exists. @@ -68,32 +98,9 @@ export class ZeroEx { * @param orderHash Hex encoded orderHash. * @return Whether the supplied orderHash has the expected format. */ - public static isValidOrderHash = isValidOrderHash; - - /** - * An instance of the ExchangeWrapper class containing methods for interacting with the 0x Exchange smart contract. - */ - public exchange: ExchangeWrapper; - /** - * An instance of the TokenRegistryWrapper class containing methods for interacting with the 0x - * TokenRegistry smart contract. - */ - public tokenRegistry: TokenRegistryWrapper; - /** - * An instance of the TokenWrapper class containing methods for interacting with any ERC20 token smart contract. - */ - public token: TokenWrapper; - /** - * An instance of the EtherTokenWrapper class containing methods for interacting with the - * wrapped ETH ERC20 token smart contract. - */ - public etherToken: EtherTokenWrapper; - /** - * An instance of the TokenTransferProxyWrapper class containing methods for interacting with the - * tokenTransferProxy smart contract. - */ - public proxy: TokenTransferProxyWrapper; - private _web3Wrapper: Web3Wrapper; + public static isValidOrderHash(orderHash: string): boolean { + return isValidOrderHash(orderHash); + } /** * A unit amount is defined as the amount of a token above the specified decimal places (integer part). * E.g: If a currency has 18 decimal places, 1e18 or one quintillion of the currency is equivalent @@ -185,6 +192,7 @@ export class ZeroEx { } /** * Get the provider instance currently used by 0x.js + * @return Web3 provider instance */ public getProvider(): Provider { return this._web3Wrapper.getProvider(); -- cgit v1.2.3 From a6046af0240d9257a9e9dd80dc4dc5f533df8799 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 2 May 2018 18:38:38 +0300 Subject: Stop exporting assertions from order-utils --- packages/0x.js/src/0x.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/0x.js/src/0x.ts') diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index bf776cd95..78f9f6beb 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -1,6 +1,5 @@ import { schemas, SchemaValidator } from '@0xproject/json-schemas'; import { - assert, generatePseudoRandomSalt, getOrderHashHex, isValidOrderHash, @@ -24,6 +23,7 @@ import { zeroExConfigSchema } from './schemas/zero_ex_config_schema'; import { zeroExPrivateNetworkConfigSchema } from './schemas/zero_ex_private_network_config_schema'; import { zeroExPublicNetworkConfigSchema } from './schemas/zero_ex_public_network_config_schema'; import { OrderStateWatcherConfig, ZeroExConfig, ZeroExError } from './types'; +import { assert } from './utils/assert'; import { constants } from './utils/constants'; import { decorators } from './utils/decorators'; import { utils } from './utils/utils'; -- cgit v1.2.3