diff options
Diffstat (limited to 'packages/order-utils/src/transaction_hash.ts')
-rw-r--r-- | packages/order-utils/src/transaction_hash.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/packages/order-utils/src/transaction_hash.ts b/packages/order-utils/src/transaction_hash.ts new file mode 100644 index 000000000..2b2345af7 --- /dev/null +++ b/packages/order-utils/src/transaction_hash.ts @@ -0,0 +1,46 @@ +import { schemas, SchemaValidator } from '@0x/json-schemas'; +import { SignedZeroExTransaction, ZeroExTransaction } from '@0x/types'; +import { signTypedDataUtils } from '@0x/utils'; +import * as _ from 'lodash'; + +import { assert } from './assert'; +import { eip712Utils } from './eip712_utils'; + +export const transactionHashUtils = { + /** + * Checks if the supplied hex encoded 0x transaction hash is valid. + * Note: Valid means it has the expected format, not that a transaction with the transactionHash exists. + * Use this method when processing transactionHashes submitted as user input. + * @param transactionHash Hex encoded transactionHash. + * @return Whether the supplied transactionHash has the expected format. + */ + isValidTransactionHash(transactionHash: string): boolean { + // Since this method can be called to check if any arbitrary string conforms to an transactionHash's + // format, we only assert that we were indeed passed a string. + assert.isString('transactionHash', transactionHash); + const schemaValidator = new SchemaValidator(); + const isValid = schemaValidator.validate(transactionHash, schemas.orderHashSchema).valid; + return isValid; + }, + /** + * Computes the transactionHash for a supplied 0x transaction. + * @param transaction An object that conforms to the ZeroExTransaction or SignedZeroExTransaction interface definitions. + * @return Hex encoded string transactionHash from hashing the supplied order. + */ + getTransactionHashHex(transaction: ZeroExTransaction | SignedZeroExTransaction): string { + assert.doesConformToSchema('transaction', transaction, schemas.zeroExTransactionSchema, [schemas.hexSchema]); + const transactionHashBuff = transactionHashUtils.getTransactionHashBuffer(transaction); + const transactionHashHex = `0x${transactionHashBuff.toString('hex')}`; + return transactionHashHex; + }, + /** + * Computes the transactionHash for a supplied 0x transaction. + * @param transaction An object that conforms to the ZeroExTransaction or SignedZeroExTransaction interface definitions. + * @return A Buffer containing the resulting transactionHash from hashing the supplied 0x transaction. + */ + getTransactionHashBuffer(transaction: ZeroExTransaction | SignedZeroExTransaction): Buffer { + const typedData = eip712Utils.createZeroExTransactionTypedData(transaction); + const transactionHashBuff = signTypedDataUtils.generateTypedDataHash(typedData); + return transactionHashBuff; + }, +}; |