1. /* Sourced from 0x.js */
    2. import { BigNumber } from '@0xproject/utils';
    3. import BN = require('bn.js');
    4. import * as ethABI from 'ethereumjs-abi';
    5. import * as ethUtil from 'ethereumjs-util';
    6. import * as _ from 'lodash';
    7. import { Order, SignedOrder, SolidityTypes } from '../types';
    8. export const utils = {
    9. /**
    10. * 将BigNumber实例转换为BN
    11. * 我们转换成BN的唯一原因是保持与ethABI兼容。' solidysha3 ',
    12. * 期望将solid类型' uint '的值作为类型' BN '传递。
    13. * We do not use BN anywhere else in the codebase.
    14. */
    15. bigNumberToBN(value: BigNumber) {
    16. return new BN(value.toString(), 10);
    17. },
    18. consoleLog(message: string): void {
    19. // tslint:disable-next-line: no-console
    20. console.log(message);
    21. },
    22. isParityNode(nodeVersion: string): boolean {
    23. return _.includes(nodeVersion, 'Parity');
    24. },
    25. isTestRpc(nodeVersion: string): boolean {
    26. return _.includes(nodeVersion, 'TestRPC');
    27. },
    28. spawnSwitchErr(name: string, value: any): Error {
    29. return new Error(`Unexpected switch value: ${value} encountered for ${name}`);
    30. },
    31. getAssetHashHex(assetHash: string, schema: string): string {
    32. const concat = schema + ':' + assetHash;
    33. const hashBuf = ethABI.soliditySHA3(['string'], [concat]);
    34. return ethUtil.bufferToHex(hashBuf);
    35. },
    36. getOrderHashHex(order: Order | SignedOrder): string {
    37. const orderPartsA = [
    38. { value: order.exchange, type: SolidityTypes.Address },
    39. { value: order.maker, type: SolidityTypes.Address },
    40. { value: order.taker, type: SolidityTypes.Address },
    41. { value: utils.bigNumberToBN(order.makerRelayerFee), type: SolidityTypes.Uint256 },
    42. { value: utils.bigNumberToBN(order.takerRelayerFee), type: SolidityTypes.Uint256 },
    43. { value: utils.bigNumberToBN(order.makerProtocolFee), type: SolidityTypes.Uint256 },
    44. { value: utils.bigNumberToBN(order.takerProtocolFee), type: SolidityTypes.Uint256 },
    45. { value: order.feeRecipient, type: SolidityTypes.Address },
    46. { value: order.feeMethod, type: SolidityTypes.Uint8 },
    47. { value: order.side, type: SolidityTypes.Uint8 },
    48. { value: order.saleKind, type: SolidityTypes.Uint8 },
    49. { value: order.target, type: SolidityTypes.Address },
    50. { value: order.howToCall, type: SolidityTypes.Uint8 },
    51. ];
    52. const orderPartsB = [
    53. { value: new Buffer(order.calldata.slice(2), 'hex'), type: SolidityTypes.Bytes },
    54. { value: new Buffer(order.replacementPattern.slice(2), 'hex'), type: SolidityTypes.Bytes },
    55. { value: order.staticTarget, type: SolidityTypes.Address },
    56. { value: new Buffer(order.staticExtradata.slice(2), 'hex'), type: SolidityTypes.Bytes },
    57. { value: order.paymentToken, type: SolidityTypes.Address },
    58. { value: utils.bigNumberToBN(order.basePrice), type: SolidityTypes.Uint256 },
    59. { value: utils.bigNumberToBN(order.extra), type: SolidityTypes.Uint256 },
    60. { value: utils.bigNumberToBN(order.listingTime), type: SolidityTypes.Uint256 },
    61. { value: utils.bigNumberToBN(order.expirationTime), type: SolidityTypes.Uint256 },
    62. { value: utils.bigNumberToBN(order.salt), type: SolidityTypes.Uint256 },
    63. ];
    64. const typesA = _.map(orderPartsA, o => o.type);
    65. const valuesA = _.map(orderPartsA, o => o.value);
    66. const hashBufA = ethABI.soliditySHA3(typesA, valuesA);
    67. const typesB = _.map(orderPartsB, o => o.type);
    68. const valuesB = _.map(orderPartsB, o => o.value);
    69. const hashBufB = ethABI.soliditySHA3(typesB, valuesB);
    70. const orderPartsC = [
    71. { value: '\x19Ethereum Signed Message:\n32', type: SolidityTypes.String },
    72. { value: hashBufA, type: SolidityTypes.Bytes },
    73. { value: hashBufB, type: SolidityTypes.Bytes },
    74. ];
    75. const typesC = _.map(orderPartsC, o => o.type);
    76. const valuesC = _.map(orderPartsC, o => o.value);
    77. const hashBufC = ethABI.soliditySHA3(typesC, valuesC);
    78. const hashHexC = ethUtil.bufferToHex(hashBufC);
    79. return hashHexC;
    80. },
    81. getCurrentUnixTimestampSec(): BigNumber {
    82. return new BigNumber(Date.now() / 1000).round();
    83. },
    84. getCurrentUnixTimestampMs(): BigNumber {
    85. return new BigNumber(Date.now());
    86. },
    87. };