diff options
author | Fabio Berger <me@fabioberger.com> | 2018-07-12 00:53:50 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-07-12 00:53:50 +0800 |
commit | b6ac132c789939414e2aba911246e6a931facea4 (patch) | |
tree | 66c6f0ea16868aa06a6ffc188bcc575d2891b5c3 /packages/0x.js/src/0x.ts | |
parent | abf87a643d85827cb1186e913fa6ae6bc04c8df1 (diff) | |
download | dexon-sol-tools-b6ac132c789939414e2aba911246e6a931facea4.tar dexon-sol-tools-b6ac132c789939414e2aba911246e6a931facea4.tar.gz dexon-sol-tools-b6ac132c789939414e2aba911246e6a931facea4.tar.bz2 dexon-sol-tools-b6ac132c789939414e2aba911246e6a931facea4.tar.lz dexon-sol-tools-b6ac132c789939414e2aba911246e6a931facea4.tar.xz dexon-sol-tools-b6ac132c789939414e2aba911246e6a931facea4.tar.zst dexon-sol-tools-b6ac132c789939414e2aba911246e6a931facea4.zip |
Refactor 0x.js to V2
Diffstat (limited to 'packages/0x.js/src/0x.ts')
-rw-r--r-- | packages/0x.js/src/0x.ts | 82 |
1 files changed, 49 insertions, 33 deletions
diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index c6315ac7a..597bc07a5 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -4,19 +4,22 @@ import { ContractWrappersConfig, ERC20ProxyWrapper, ERC20TokenWrapper, + ERC721ProxyWrapper, + ERC721TokenWrapper, EtherTokenWrapper, ExchangeWrapper, } from '@0xproject/contract-wrappers'; import { + ecSignOrderHashAsync, generatePseudoRandomSalt, - getOrderHashHex, - isValidOrderHash, - isValidSignature, - signOrderHashAsync, + isValidSignatureAsync, + MessagePrefixOpts, + orderHashUtils, } from '@0xproject/order-utils'; -import { ECSignature, Order, Provider, SignedOrder, TransactionReceiptWithDecodedLogs } from '@0xproject/types'; +import { ECSignature, 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'; @@ -36,21 +39,28 @@ export class ZeroEx { */ public exchange: ExchangeWrapper; /** + * An instance of the ERC20TokenWrapper class containing methods for interacting with any ERC20 token smart contract. */ + 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 proxy: TokenTransferProxyWrapper; + public erc20Proxy: ERC20ProxyWrapper; + /** + * An instance of the ERC721ProxyWrapper class containing methods for interacting with the + * ERC721 proxy smart contract. + */ + public erc721Proxy: ERC721ProxyWrapper; private _contractWrappers: ContractWrappers; /** * Generates a pseudo-random 256-bit salt. @@ -62,23 +72,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. @@ -88,7 +87,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). @@ -129,12 +128,31 @@ 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.etherToken = this._contractWrappers.etherToken; } /** + * Verifies that the provided signature is valid according to the 0x Protocol smart contracts + * @param provider The Web3Provider to use. Some signature types involve checking contract state. + * @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. @@ -166,23 +184,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. |