|
| 1 | +/* eslint-disable @typescript-eslint/no-unsafe-return */ |
1 | 2 | import { solidityPackedKeccak256 } from 'ethers';
|
| 3 | +import { PaymentOption, CancelOption, UnsignedOfferPayload } from '../shared/types.js'; |
| 4 | +import { |
| 5 | + PAYMENT_OPTION_TYPE_HASH, |
| 6 | + CANCEL_OPTION_TYPE_HASH, |
| 7 | + OFFER_TYPE_HASH, |
| 8 | +} from '@windingtree/contracts'; |
2 | 9 |
|
3 |
| -/** |
4 |
| - * Hashes object using given serializer |
5 |
| - * |
6 |
| - * @template Obj |
7 |
| - * @param {(Obj | Obj[])} obj |
8 |
| - * @param {*} [serialize=JSON.stringify] |
9 |
| - * @returns {string} |
10 |
| - */ |
11 |
| -export const hashObject = <Obj>(obj: Obj | Obj[], serialize = JSON.stringify): string => |
12 |
| - solidityPackedKeccak256(['string'], [serialize(obj)]); |
| 10 | +export const stringify = (data: unknown): string => |
| 11 | + JSON.stringify(data, (_, v) => (typeof v === 'bigint' ? v.toString() : v)); |
| 12 | + |
| 13 | +export const hashObject = (data: unknown): string => |
| 14 | + solidityPackedKeccak256(['string'], [stringify(data)]); |
| 15 | + |
| 16 | +export const hashPaymentOption = (option: PaymentOption): string => |
| 17 | + solidityPackedKeccak256( |
| 18 | + ['bytes32', 'bytes32', 'uint256', 'address'], |
| 19 | + [PAYMENT_OPTION_TYPE_HASH, option.id, option.price, option.asset], |
| 20 | + ); |
| 21 | + |
| 22 | +export const hashCancelOption = (option: CancelOption): string => |
| 23 | + solidityPackedKeccak256( |
| 24 | + ['bytes32', 'uint256', 'uint256'], |
| 25 | + [CANCEL_OPTION_TYPE_HASH, option.time, option.penalty], |
| 26 | + ); |
| 27 | + |
| 28 | +export const hashPaymentOptionArray = (options: PaymentOption[]): string => { |
| 29 | + const hashes = []; |
| 30 | + |
| 31 | + for (let i = 0; i < options.length; i++) { |
| 32 | + hashes[i] = hashPaymentOption(options[i]); |
| 33 | + } |
| 34 | + return solidityPackedKeccak256(['bytes32[]'], [hashes]); |
| 35 | +}; |
| 36 | + |
| 37 | +export const hashCancelOptionArray = (options: CancelOption[]): string => { |
| 38 | + const hashes = []; |
| 39 | + |
| 40 | + for (let i = 0; i < options.length; i++) { |
| 41 | + hashes[i] = hashCancelOption(options[i]); |
| 42 | + } |
| 43 | + |
| 44 | + return solidityPackedKeccak256(['bytes32[]'], [hashes]); |
| 45 | +}; |
| 46 | + |
| 47 | +export const hashOfferPayload = (payload: UnsignedOfferPayload): string => |
| 48 | + solidityPackedKeccak256( |
| 49 | + [ |
| 50 | + 'bytes32', |
| 51 | + 'bytes32', |
| 52 | + 'uint256', |
| 53 | + 'bytes32', |
| 54 | + 'uint256', |
| 55 | + 'bytes32', |
| 56 | + 'bytes32', |
| 57 | + 'bytes32', |
| 58 | + 'bytes32', |
| 59 | + 'bool', |
| 60 | + 'uint256', |
| 61 | + ], |
| 62 | + [ |
| 63 | + OFFER_TYPE_HASH, |
| 64 | + payload.id, |
| 65 | + payload.expire, |
| 66 | + payload.supplierId, |
| 67 | + payload.chainId, |
| 68 | + payload.requestHash, |
| 69 | + payload.optionsHash, |
| 70 | + payload.paymentHash, |
| 71 | + payload.cancelHash, |
| 72 | + payload.transferable, |
| 73 | + payload.checkIn, |
| 74 | + ], |
| 75 | + ); |
| 76 | + |
| 77 | +export const hashCheckInOut = (offerId: string, signer: string): string => |
| 78 | + solidityPackedKeccak256(['bytes32', 'bytes32', 'address'], [OFFER_TYPE_HASH, offerId, signer]); |
0 commit comments