diff options
Diffstat (limited to 'packages/0x.js/src/0x.ts')
-rw-r--r-- | packages/0x.js/src/0x.ts | 115 |
1 files changed, 59 insertions, 56 deletions
diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index 001f336e7..7c06a53af 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -2,23 +2,28 @@ import { assert } from '@0xproject/assert'; import { ContractWrappers, ContractWrappersConfig, + ERC20ProxyWrapper, + ERC20TokenWrapper, + ERC721ProxyWrapper, + ERC721TokenWrapper, EtherTokenWrapper, ExchangeWrapper, - TokenRegistryWrapper, - TokenTransferProxyWrapper, - TokenWrapper, } from '@0xproject/contract-wrappers'; import { + assetDataUtils, + ecSignOrderHashAsync, generatePseudoRandomSalt, - getOrderHashHex, - isValidOrderHash, - isValidSignature, - signOrderHashAsync, + isValidSignatureAsync, + MessagePrefixOpts, + orderHashUtils, } from '@0xproject/order-utils'; -import { OrderWatcher, OrderWatcherConfig } from '@0xproject/order-watcher'; -import { ECSignature, Order, Provider, SignedOrder, TransactionReceiptWithDecodedLogs } from '@0xproject/types'; +// HACK: Since we export assetDataUtils from ZeroEx and it has AssetProxyId, ERC20AssetData and ERC721AssetData +// in it's public interface, we need to import these types here. +// tslint:disable-next-line:no-unused-variable +import { AssetProxyId, ECSignature, ERC20AssetData, ERC721AssetData, Order, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import { Provider, TransactionReceiptWithDecodedLogs } from 'ethereum-types'; import { constants } from './utils/constants'; @@ -34,29 +39,37 @@ export class ZeroEx { */ public static NULL_ADDRESS = constants.NULL_ADDRESS; /** + * A set of methods to easily decode/encode assetData fields found in 0x orders. + */ + public static assetData = assetDataUtils; + /** * 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. + * An instance of the ERC20TokenWrapper class containing methods for interacting with any ERC20 token smart contract. */ - public tokenRegistry: TokenRegistryWrapper; + public erc20Token: ERC20TokenWrapper; /** - * An instance of the TokenWrapper class containing methods for interacting with any ERC20 token smart contract. + * An instance of the ERC721TokenWrapper class containing methods for interacting with any ERC721 token smart contract. */ - public token: TokenWrapper; + public erc721Token: ERC721TokenWrapper; /** * 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. + * An instance of the ERC20ProxyWrapper class containing methods for interacting with the + * ERC20 proxy smart contract. + */ + public erc20Proxy: ERC20ProxyWrapper; + /** + * An instance of the ERC721ProxyWrapper class containing methods for interacting with the + * ERC721 proxy smart contract. */ - public proxy: TokenTransferProxyWrapper; - private _contractWrappers: ContractWrappers; + public erc721Proxy: ERC721ProxyWrapper; + private readonly _contractWrappers: ContractWrappers; /** * Generates a pseudo-random 256-bit salt. * The salt can be included in a 0x order, ensuring that the order generates a unique orderHash @@ -67,23 +80,12 @@ export class ZeroEx { return 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(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(order: Order | SignedOrder): string { - return getOrderHashHex(order); + return orderHashUtils.getOrderHashHex(order); } /** * Checks if the supplied hex encoded order hash is valid. @@ -93,7 +95,7 @@ export class ZeroEx { * @return Whether the supplied orderHash has the expected format. */ public static isValidOrderHash(orderHash: string): boolean { - return isValidOrderHash(orderHash); + return orderHashUtils.isValidOrderHash(orderHash); } /** * A unit amount is defined as the amount of a token above the specified decimal places (integer part). @@ -134,13 +136,30 @@ export class ZeroEx { assert.isWeb3Provider('provider', provider); this._contractWrappers = new ContractWrappers(provider, config); - this.proxy = this._contractWrappers.proxy; - this.token = this._contractWrappers.token; + this.erc20Proxy = this._contractWrappers.erc20Proxy; + this.erc721Proxy = this._contractWrappers.erc721Proxy; + this.erc20Token = this._contractWrappers.erc20Token; + this.erc721Token = this._contractWrappers.erc721Token; this.exchange = this._contractWrappers.exchange; - this.tokenRegistry = this._contractWrappers.tokenRegistry; this.etherToken = this._contractWrappers.etherToken; } /** + * Verifies that the provided signature is valid according to the 0x Protocol smart contracts + * @param data The hex encoded data signed by the supplied signature. + * @param signature The hex encoded signature. + * @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 async isValidSignatureAsync(data: string, signature: string, signerAddress: string): Promise<boolean> { + const isValid = await isValidSignatureAsync( + this._contractWrappers.getProvider(), + data, + signature, + signerAddress, + ); + return isValid; + } + /** * Sets a new web3 provider for 0x.js. Updating the provider will stop all * subscriptions so you will need to re-subscribe to all events relevant to your app after this call. * @param provider The Web3Provider you would like the 0x.js library to use from now on. @@ -172,23 +191,21 @@ export class ZeroEx { * @param orderHash Hex encoded orderHash to sign. * @param signerAddress The hex encoded Ethereum address you wish to sign it with. This address * must be available via the Provider supplied to 0x.js. - * @param shouldAddPersonalMessagePrefix Some signers add the personal message prefix `\x19Ethereum Signed Message` - * themselves (e.g Parity Signer, Ledger, TestRPC) and others expect it to already be done by the client - * (e.g Metamask). Depending on which signer this request is going to, decide on whether to add the prefix - * before sending the request. + * @param MessagePrefixOpts Options regarding the desired prefix and whether to add it before calling `eth_sign` * @return An object containing the Elliptic curve signature parameters generated by signing the orderHash. */ - public async signOrderHashAsync( + public async ecSignOrderHashAsync( orderHash: string, signerAddress: string, - shouldAddPersonalMessagePrefix: boolean, + messagePrefixOpts: MessagePrefixOpts, ): Promise<ECSignature> { - return signOrderHashAsync( + const signature = await ecSignOrderHashAsync( this._contractWrappers.getProvider(), orderHash, signerAddress, - shouldAddPersonalMessagePrefix, + messagePrefixOpts, ); + return signature; } /** * Waits for a transaction to be mined and returns the transaction receipt. @@ -211,18 +228,4 @@ export class ZeroEx { ); return transactionReceiptWithDecodedLogs; } - /** - * Instantiates and returns a new OrderWatcher instance. - * Defaults to watching the pending state. - * @param config The configuration object. Look up the type for the description. - * @return An instance of the 0x.js OrderWatcher class. - */ - public async createOrderWatcherAsync(config?: OrderWatcherConfig): Promise<OrderWatcher> { - // Hack: Get Web3Wrapper from ContractWrappers - const web3Wrapper: Web3Wrapper = (this._contractWrappers as any)._web3Wrapper; - const networkId = await web3Wrapper.getNetworkIdAsync(); - const provider = this._contractWrappers.getProvider(); - const orderWatcher = new OrderWatcher(provider, networkId, config); - return orderWatcher; - } } |