Skip to content

Commit

Permalink
Improve input type for salt (#250)
Browse files Browse the repository at this point in the history
* improve input types

* move test folder outside src

* fix lint

* improvements

* fix lint, fix test, pad salt to 64 bytes
  • Loading branch information
ryanio authored May 26, 2023
1 parent 988b9e2 commit 763efaf
Show file tree
Hide file tree
Showing 27 changed files with 98 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .c8rc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"reporter": ["lcov", "text"],
"src": ["src"],
"exclude": ["hardhat.config.ts", "src/__tests__/*", "src/abi/*"]
"exclude": ["hardhat.config.ts", "src/abi/*"]
}
2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const config: HardhatUserConfig = {
target: "ethers-v5",
},
paths: {
tests: "src/__tests__",
tests: "test",
artifacts: "src/artifacts",
sources: "src/contracts",
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"scripts": {
"build": "hardhat compile && tsc -p tsconfig.build.json",
"check-types": "tsc --noEmit -p src/__tests__/tsconfig.json",
"check-types": "tsc --noEmit ",
"check-types:incremental": "npm run check-types --incremental",
"coverage": "c8 yarn test",
"eslint:check": "eslint . --max-warnings 0 --ext .js,.jsx,.ts,.tsx",
Expand Down
7 changes: 0 additions & 7 deletions src/__tests__/tsconfig.json

This file was deleted.

17 changes: 10 additions & 7 deletions src/seaport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import {
deductFees,
feeToConsiderationItem,
generateRandomSalt,
generateRandomSaltWithDomain,
mapInputItemToOfferItem,
totalItemsAmount,
} from "./utils/order";
Expand Down Expand Up @@ -356,8 +355,9 @@ export class Seaport {
];

const saltFollowingConditional =
salt ||
(domain ? generateRandomSaltWithDomain(domain) : generateRandomSalt());
salt !== undefined
? `0x${BigNumber.from(salt).toHexString().slice(2).padStart(64, "0")}`
: generateRandomSalt(domain);

const orderComponents: OrderComponents = {
offerer,
Expand Down Expand Up @@ -739,7 +739,10 @@ export class Seaport {
.slice(2)
.padStart(64, "0"),
orderComponents.zoneHash.slice(2),
orderComponents.salt.slice(2).padStart(64, "0"),
BigNumber.from(orderComponents.salt)
.toHexString()
.slice(2)
.padStart(64, "0"),
orderComponents.conduitKey.slice(2).padStart(64, "0"),
ethers.BigNumber.from(orderComponents.counter)
.toHexString()
Expand Down Expand Up @@ -781,7 +784,7 @@ export class Seaport {
accountAddress,
conduitKey = this.defaultConduitKey,
recipientAddress = ethers.constants.AddressZero,
domain = "",
domain,
exactApproval = false,
}: {
order: OrderWithCounter;
Expand Down Expand Up @@ -939,7 +942,7 @@ export class Seaport {
accountAddress,
conduitKey = this.defaultConduitKey,
recipientAddress = ethers.constants.AddressZero,
domain = "",
domain,
exactApproval = false,
}: {
fulfillOrderDetails: {
Expand Down Expand Up @@ -1066,7 +1069,7 @@ export class Seaport {
fulfillments,
overrides,
accountAddress,
domain = "",
domain,
}: {
orders: (OrderWithCounter | Order)[];
fulfillments: MatchOrdersFulfillment[];
Expand Down
7 changes: 3 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,16 @@ export type Fee = {
export type CreateOrderInput = {
conduitKey?: string;
zone?: string;
startTime?: string;
endTime?: string;
startTime?: BigNumberish;
endTime?: BigNumberish;
offer: readonly CreateInputItem[];
consideration: readonly ConsiderationInputItem[];
counter?: BigNumberish;
fees?: readonly Fee[];
allowPartialFills?: boolean;
restrictedByZone?: boolean;
useProxy?: boolean;
domain?: string;
salt?: string;
salt?: BigNumberish;
};

export type InputCriteria = {
Expand Down
23 changes: 11 additions & 12 deletions src/utils/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,17 @@ export const mapOrderAmountsFromUnitsToFill = (
};
};

export const generateRandomSalt = () => {
return `0x${Buffer.from(randomBytes(8)).toString("hex").padStart(24, "0")}`;
};

export const generateRandomSaltWithDomain = (domain: string) => {
return `0x${Buffer.from(
concat([
keccak256(toUtf8Bytes(domain)).slice(0, 10),
Uint8Array.from(Array(20).fill(0)),
randomBytes(8),
])
).toString("hex")}`;
export const generateRandomSalt = (domain?: string) => {
if (domain) {
return `0x${Buffer.from(
concat([
keccak256(toUtf8Bytes(domain)).slice(0, 10),
Uint8Array.from(Array(20).fill(0)),
randomBytes(8),
])
).toString("hex")}`;
}
return `0x${Buffer.from(randomBytes(8)).toString("hex").padStart(64, "0")}`;
};

export const shouldUseMatchForFulfill = () => true;
2 changes: 1 addition & 1 deletion src/utils/usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const getTransactionMethods = <
contract: T,
method: U,
args: Parameters<T["functions"][U]>,
domain: string = ""
domain?: string
): TransactionMethods<ContractMethodReturnType<T, U>> => {
const lastArg = args[args.length - 1];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { expect } from "chai";
import { BigNumber } from "ethers";
import { parseEther } from "ethers/lib/utils";
import { ethers } from "hardhat";
import { ItemType, MAX_INT, OrderType } from "../constants";
import { CreateOrderInput, CurrencyItem } from "../types";
import * as fulfill from "../utils/fulfill";
import { generateRandomSalt } from "../utils/order";
import { getTagFromDomain } from "../utils/usecase";
import { ItemType, MAX_INT, OrderType } from "../src/constants";
import { CreateOrderInput, CurrencyItem } from "../src/types";
import * as fulfill from "../src/utils/fulfill";
import { generateRandomSalt } from "../src/utils/order";
import { getTagFromDomain } from "../src/utils/usecase";
import {
getBalancesForFulfillOrder,
verifyBalancesAfterFulfill,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { expect } from "chai";
import { BigNumber } from "ethers";
import { parseEther } from "ethers/lib/utils";
import { ethers } from "hardhat";
import { ItemType, MAX_INT } from "../constants";
import { CreateOrderInput, CurrencyItem } from "../types";
import * as fulfill from "../utils/fulfill";
import { ItemType, MAX_INT } from "../src/constants";
import { CreateOrderInput, CurrencyItem } from "../src/types";
import * as fulfill from "../src/utils/fulfill";
import {
getBalancesForFulfillOrder,
verifyBalancesAfterFulfill,
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/bundle.spec.ts → test/bundle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { expect } from "chai";
import { BigNumber } from "ethers";
import { parseEther } from "ethers/lib/utils";
import { ethers } from "hardhat";
import { ItemType, MAX_INT } from "../constants";
import { TestERC1155, TestERC721 } from "../typechain-types";
import { CreateOrderInput, CurrencyItem } from "../types";
import * as fulfill from "../utils/fulfill";
import { ItemType, MAX_INT } from "../src/constants";
import { TestERC1155, TestERC721 } from "../src/typechain-types";
import { CreateOrderInput, CurrencyItem } from "../src/types";
import * as fulfill from "../src/utils/fulfill";
import {
getBalancesForFulfillOrder,
verifyBalancesAfterFulfill,
} from "./utils/balance";
import { getTagFromDomain } from "../utils/usecase";
import { getTagFromDomain } from "../src/utils/usecase";
import { describeWithFixture } from "./utils/setup";

const sinon = require("sinon");
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/cancel.spec.ts → test/cancel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { expect } from "chai";
import { parseEther } from "ethers/lib/utils";
import { ethers } from "hardhat";
import { ItemType } from "../constants";
import { CreateOrderInput } from "../types";
import { ItemType } from "../src/constants";
import { CreateOrderInput } from "../src/types";
import { describeWithFixture } from "./utils/setup";

describeWithFixture("As a user I want to cancel an order", (fixture) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { expect } from "chai";
import { ethers } from "hardhat";
import { ItemType, MAX_INT, NO_CONDUIT, OrderType } from "../constants";
import { ItemType, MAX_INT, NO_CONDUIT, OrderType } from "../src/constants";
import {
ApprovalAction,
BasicErc721Item,
CreateBulkOrdersAction,
} from "../types";
import { generateRandomSalt } from "../utils/order";
} from "../src/types";
import { generateRandomSalt } from "../src/utils/order";
import { describeWithFixture } from "./utils/setup";

describeWithFixture(
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/create-order.spec.ts → test/create-order.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { expect } from "chai";
import { parseEther } from "ethers/lib/utils";
import { ethers } from "hardhat";
import { ItemType, MAX_INT, NO_CONDUIT, OrderType } from "../constants";
import { ApprovalAction, CreateOrderAction } from "../types";
import { generateRandomSalt } from "../utils/order";
import { ItemType, MAX_INT, NO_CONDUIT, OrderType } from "../src/constants";
import { ApprovalAction, CreateOrderAction } from "../src/types";
import { generateRandomSalt } from "../src/utils/order";
import { describeWithFixture } from "./utils/setup";

describeWithFixture("As a user I want to create an order", (fixture) => {
Expand Down Expand Up @@ -826,7 +826,7 @@ describeWithFixture("As a user I want to create an order", (fixture) => {
await testErc721.mint(offerer.address, nftId);
const startTime = "0";
const endTime = MAX_INT.toString();
const salt = "0xabc";
const salt = "0xabcd";

const { executeAllActions } = await seaport.createOrder({
startTime,
Expand Down Expand Up @@ -858,6 +858,6 @@ describeWithFixture("As a user I want to create an order", (fixture) => {
const localOrderHash = seaport.getOrderHash(order.parameters);

expect(contractOrderHash).eq(localOrderHash);
expect(order.parameters.salt).eq("0xabc");
expect(order.parameters.salt).eq(`0x${"0".repeat(60)}abcd`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { expect } from "chai";
import { BigNumber } from "ethers";
import { parseEther } from "ethers/lib/utils";
import { ethers } from "hardhat";
import { ItemType, MAX_INT } from "../constants";
import { CreateOrderInput, CurrencyItem, OrderWithCounter } from "../types";
import * as fulfill from "../utils/fulfill";
import { MerkleTree } from "../utils/merkletree";
import { ItemType, MAX_INT } from "../src/constants";
import { CreateOrderInput, CurrencyItem, OrderWithCounter } from "../src/types";
import * as fulfill from "../src/utils/fulfill";
import { MerkleTree } from "../src/utils/merkletree";
import {
getBalancesForFulfillOrder,
verifyBalancesAfterFulfill,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { expect } from "chai";
import { BigNumber } from "ethers";
import { parseEther } from "ethers/lib/utils";
import { ethers } from "hardhat";
import { ItemType, MAX_INT } from "../constants";
import { TestERC1155, TestERC721 } from "../typechain-types";
import { CreateOrderInput, CurrencyItem } from "../types";
import * as fulfill from "../utils/fulfill";
import { getTagFromDomain } from "../utils/usecase";
import { ItemType, MAX_INT } from "../src/constants";
import { TestERC1155, TestERC721 } from "../src/typechain-types";
import { CreateOrderInput, CurrencyItem } from "../src/types";
import * as fulfill from "../src/utils/fulfill";
import { getTagFromDomain } from "../src/utils/usecase";
import { describeWithFixture } from "./utils/setup";

const sinon = require("sinon");
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/gifting.spec.ts → test/gifting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { expect } from "chai";
import { BigNumber } from "ethers";
import { parseEther } from "ethers/lib/utils";
import { ethers } from "hardhat";
import { ItemType, MAX_INT } from "../constants";
import { CreateOrderInput, CurrencyItem } from "../types";
import { ItemType, MAX_INT } from "../src/constants";
import { CreateOrderInput, CurrencyItem } from "../src/types";
import { describeWithFixture } from "./utils/setup";

describeWithFixture(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { BigNumber } from "ethers";
import { parseEther } from "ethers/lib/utils";
import { ethers } from "hardhat";

import { ItemType, MAX_INT } from "../constants";
import { CreateOrderInput, CurrencyItem } from "../types";
import { ItemType, MAX_INT } from "../src/constants";
import { CreateOrderInput, CurrencyItem } from "../src/types";
import {
getBalancesForFulfillOrder,
verifyBalancesAfterFulfill,
Expand All @@ -15,7 +15,7 @@ import {
getPrivateListingFulfillments,
} from "./utils/examples/privateListings";
import { describeWithFixture } from "./utils/setup";
import { getTransactionMethods } from "../utils/usecase";
import { getTransactionMethods } from "../src/utils/usecase";
import { expect } from "chai";

describeWithFixture("As a user I want to match an order", (fixture) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { expect } from "chai";
import { BigNumber } from "ethers";
import { parseEther, parseUnits } from "ethers/lib/utils";
import { ethers } from "hardhat";
import { ItemType, MAX_INT, OrderType } from "../constants";
import { TestERC1155 } from "../typechain-types";
import { CreateOrderInput, CurrencyItem } from "../types";
import * as fulfill from "../utils/fulfill";
import { ItemType, MAX_INT, OrderType } from "../src/constants";
import { TestERC1155 } from "../src/typechain-types";
import { CreateOrderInput, CurrencyItem } from "../src/types";
import * as fulfill from "../src/utils/fulfill";
import {
getBalancesForFulfillOrder,
verifyBalancesAfterFulfill,
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/sign-order.spec.ts → test/sign-order.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { expect } from "chai";
import { ethers } from "hardhat";
import { ItemType, MAX_INT, NO_CONDUIT, OrderType } from "../constants";
import { ConsiderationItem, OfferItem } from "../types";
import { generateRandomSalt } from "../utils/order";
import { ItemType, MAX_INT, NO_CONDUIT, OrderType } from "../src/constants";
import { ConsiderationItem, OfferItem } from "../src/types";
import { generateRandomSalt } from "../src/utils/order";
import { describeWithFixture } from "./utils/setup";

describeWithFixture("As a user I want to sign an order", (fixture) => {
Expand Down
12 changes: 8 additions & 4 deletions src/__tests__/swap.spec.ts → test/swap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { expect } from "chai";
import { parseEther } from "ethers/lib/utils";
import { ethers } from "hardhat";
import { ItemType, MAX_INT } from "../constants";
import { TestERC1155, TestERC721 } from "../typechain-types";
import { ApprovalAction, CreateOrderAction, CreateOrderInput } from "../types";
import * as fulfill from "../utils/fulfill";
import { ItemType, MAX_INT } from "../src/constants";
import { TestERC1155, TestERC721 } from "../src/typechain-types";
import {
ApprovalAction,
CreateOrderAction,
CreateOrderInput,
} from "../src/types";
import * as fulfill from "../src/utils/fulfill";
import {
getBalancesForFulfillOrder,
verifyBalancesAfterFulfill,
Expand Down
11 changes: 7 additions & 4 deletions src/__tests__/utils/balance.ts → test/utils/balance.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { BigNumber, BigNumberish, ContractReceipt, providers } from "ethers";
import { parseEther } from "ethers/lib/utils";
import { Item, Order, OrderStatus } from "../../types";
import { balanceOf } from "../../utils/balance";
import { Item, Order, OrderStatus } from "../../src/types";
import { balanceOf } from "../../src/utils/balance";

import { getPresentItemAmount, TimeBasedItemParams } from "../../utils/item";
import {
getPresentItemAmount,
TimeBasedItemParams,
} from "../../src/utils/item";
import { providers as multicallProviders } from "@0xsequence/multicall";
import { expect } from "chai";
import { ethers } from "hardhat";
import {
mapOrderAmountsFromFilledStatus,
mapOrderAmountsFromUnitsToFill,
} from "../../utils/order";
} from "../../src/utils/order";

export const setBalance = async (
address: string,
Expand Down
Loading

0 comments on commit 763efaf

Please sign in to comment.