1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| export const calculateOrderHash = (orderComponents: OrderComponents) => { const offerItemTypeString = "OfferItem(uint8 itemType,address token,uint256 identifierOrCriteria,uint256 startAmount,uint256 endAmount)"; const considerationItemTypeString = "ConsiderationItem(uint8 itemType,address token,uint256 identifierOrCriteria,uint256 startAmount,uint256 endAmount,address recipient)"; const orderComponentsPartialTypeString = "OrderComponents(address offerer,address zone,OfferItem[] offer,ConsiderationItem[] consideration,uint8 orderType,uint256 startTime,uint256 endTime,bytes32 zoneHash,uint256 salt,bytes32 conduitKey,uint256 counter)"; const orderTypeString = `${orderComponentsPartialTypeString}${considerationItemTypeString}${offerItemTypeString}`;
const offerItemTypeHash = keccak256(toUtf8Bytes(offerItemTypeString)); const considerationItemTypeHash = keccak256( toUtf8Bytes(considerationItemTypeString) ); const orderTypeHash = keccak256(toUtf8Bytes(orderTypeString));
const offerHash = keccak256( "0x" + orderComponents.offer .map((offerItem) => { return keccak256( "0x" + [ offerItemTypeHash.slice(2), offerItem.itemType.toString().padStart(64, "0"), offerItem.token.slice(2).padStart(64, "0"), toBN(offerItem.identifierOrCriteria) .toHexString() .slice(2) .padStart(64, "0"), toBN(offerItem.startAmount) .toHexString() .slice(2) .padStart(64, "0"), toBN(offerItem.endAmount) .toHexString() .slice(2) .padStart(64, "0"), ].join("") ).slice(2); }) .join("") );
const considerationHash = keccak256( "0x" + orderComponents.consideration .map((considerationItem) => { return keccak256( "0x" + [ considerationItemTypeHash.slice(2), considerationItem.itemType.toString().padStart(64, "0"), considerationItem.token.slice(2).padStart(64, "0"), toBN(considerationItem.identifierOrCriteria) .toHexString() .slice(2) .padStart(64, "0"), toBN(considerationItem.startAmount) .toHexString() .slice(2) .padStart(64, "0"), toBN(considerationItem.endAmount) .toHexString() .slice(2) .padStart(64, "0"), considerationItem.recipient.slice(2).padStart(64, "0"), ].join("") ).slice(2); }) .join("") ); const derivedOrderHash = keccak256( "0x" + [ orderTypeHash.slice(2), orderComponents.offerer.slice(2).padStart(64, "0"), orderComponents.zone.slice(2).padStart(64, "0"), offerHash.slice(2), considerationHash.slice(2), orderComponents.orderType.toString().padStart(64, "0"), toBN(orderComponents.startTime) .toHexString() .slice(2) .padStart(64, "0"), toBN(orderComponents.endTime).toHexString().slice(2).padStart(64, "0"), orderComponents.zoneHash.slice(2), orderComponents.salt.slice(2).padStart(64, "0"), orderComponents.conduitKey.slice(2).padStart(64, "0"), toBN(orderComponents.counter).toHexString().slice(2).padStart(64, "0"), ].join("") );
return derivedOrderHash; };
|