update signOrder

This commit is contained in:
TheJuze
2024-05-29 14:40:11 +03:00
parent 0c99c52d69
commit f76b493daf
7 changed files with 93 additions and 105 deletions

View File

@@ -1,10 +1,4 @@
export const ORDER_TYPES = {
CrossChainOrder: [
{ name: 'limitOrder', type: 'Order' },
{ name: 'chainId', type: 'uint24' },
{ name: 'secretHash', type: 'bytes32' },
{ name: 'lockOrderExpiration', type: 'uint64' },
],
Order: [
{ name: 'senderAddress', type: 'address' },
{ name: 'matcherAddress', type: 'address' },
@@ -17,6 +11,12 @@ export const ORDER_TYPES = {
{ name: 'nonce', type: 'uint64' },
{ name: 'expiration', type: 'uint64' },
{ name: 'buySide', type: 'uint8' },
],
CrossChainOrder: [
{ name: 'limitOrder', type: 'Order' },
{ name: 'chainId', type: 'uint24' },
{ name: 'secretHash', type: 'bytes32' },
{ name: 'lockOrderExpiration', type: 'uint64' },
]
}

View File

@@ -1,56 +1,59 @@
import { ethers, keccak256 } from 'ethers';
import type { Order } from '../types.js';
import generateSecret from '../utils/generateSecret';
import getDomainData from './getDomainData';
import type { SupportedChainId } from '../../lib';
import type { SupportedChainId, SignedOrder } from '../types.js';
const CROSS_CHAIN_ORDER_TYPEHASH = '0xcb145a2347f48eab4e3341a245f53da2e686e47ef421c89a6b40dde27a063c3f'
const EIP712_DOMAIN_TYPEHASH = '0xa604fff5a27d5951f334ccda7abff3286a8af29caeeb196a6f2b40a1dce7612b';
const ORDER_TYPEHASH =
'0xb5132db62dfceb466f2f8aee7a039db36a99772e5a9771d28388a5f9baad7c54';
const CROSS_CHAIN_ORDER_TYPEHASH =
'0xc4666edeecc42a94cf6b87f39e1ca967792e6d738224365e54d7d06ec632b05c';
export default function getOrderHash(order: Order, chainId: SupportedChainId) {
const abiCoder = ethers.AbiCoder.defaultAbiCoder()
const secret = generateSecret();
const secretHash = ethers.keccak256(secret);
export function getOrderHash(order: Omit<SignedOrder, 'id'>, chainId: SupportedChainId) {
const abiCoder = ethers.AbiCoder.defaultAbiCoder();
// Generate the orderParamsHash
const orderParamsHash = keccak256((abiCoder.encode(
['address', 'address', 'address', 'address', 'address', 'uint64', 'uint64', 'uint64', 'uint64', 'uint64', 'uint8'],
[
order.senderAddress,
order.matcherAddress,
order.baseAsset,
order.quoteAsset,
order.matcherFeeAsset,
order.amount,
order.price,
order.matcherFee,
order.nonce,
order.expiration,
order.buySide
]
)));
// Generate the full crossChainOrder hash
const orderHash = ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(
['bytes32', 'bytes32', 'uint24', 'bytes32', 'uint64'],
[CROSS_CHAIN_ORDER_TYPEHASH, orderParamsHash, 97, '0x74a00e5cceb68d791486ddb9ea83bb8245eca22f67cb0ea81342f6eff8bf6e51', 1718955340461]
));
const domainData = getDomainData(chainId);
// Generate the full crossChainOrder hash
const domainSeparator = ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(
['bytes32', 'bytes32', 'bytes32', 'uint256', 'bytes32'],
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
[EIP712_DOMAIN_TYPEHASH, ethers.keccak256(ethers.toUtf8Bytes(domainData?.name as string)), ethers.keccak256(ethers.toUtf8Bytes(domainData?.version as string)), Number(domainData.chainId), domainData.salt]
));
const digest = ethers.solidityPackedKeccak256(
['bytes', 'bytes32', 'bytes32'], ['0x1901', domainSeparator, orderHash]
const limitOrderHash = keccak256(
abiCoder.encode(
[
'bytes32',
'address',
'address',
'address',
'address',
'address',
'uint64',
'uint64',
'uint64',
'uint64',
'uint64',
'uint8',
],
[
ORDER_TYPEHASH,
order.senderAddress,
order.matcherAddress,
order.baseAsset,
order.quoteAsset,
order.matcherFeeAsset,
order.amount,
order.price,
order.matcherFee,
order.nonce,
order.expiration,
order.buySide,
]
)
);
const orderHash = keccak256(
abiCoder.encode(
['bytes32', 'bytes32', 'uint24', 'bytes32', 'uint64'],
[
CROSS_CHAIN_ORDER_TYPEHASH,
limitOrderHash,
Number(chainId),
order.secretHash,
order.lockOrderExpiration,
]
)
);
console.log({ secretHash }, { orderParamsHash }, { orderHash }, { domainData }, { domainSeparator }, { digest });
return { secret, secretHash, orderHash };
return orderHash
}

View File

@@ -6,6 +6,7 @@ import type { Order, SignedOrder, SupportedChainId } from '../types.js';
import normalizeNumber from '../utils/normalizeNumber.js';
import getDomainData from './getDomainData.js';
import generateSecret from '../utils/generateSecret';
import { getOrderHash } from './hashOrder';
const DEFAULT_EXPIRATION = 29 * 24 * 60 * 60 * 1000; // 29 days
@@ -68,7 +69,7 @@ export const signOrder = async ({
expiration,
...(isCrossChain
? {
targetChainId
targetChainId: Number(targetChainId)
}
: {}),
buySide: side === 'BUY' ? 1 : 0
@@ -77,26 +78,6 @@ export const signOrder = async ({
const secret = generateSecret();
const secretHash = keccak256(secret);
const abiCoder = ethers.AbiCoder.defaultAbiCoder();
// Generate the orderParamsHash
const limitOrderHash = keccak256((abiCoder.encode(
['address', 'address', 'address', 'address', 'address', 'uint64', 'uint64', 'uint64', 'uint64', 'uint64', 'uint8'],
[
order.senderAddress,
order.matcherAddress,
order.baseAsset,
order.quoteAsset,
order.matcherFeeAsset,
order.amount,
order.price,
order.matcherFee,
order.nonce,
order.expiration,
order.buySide
]
)));
const crossChainOrder = {
limitOrder: order,
chainId: Number(chainId),
@@ -104,19 +85,11 @@ export const signOrder = async ({
lockOrderExpiration: expiration // TODO: change to fillAndLockAtomic data
}
// Generate the full crossChainOrder hash
// const orderHash = ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(
// ['bytes32', 'bytes32', 'uint24', 'bytes32', 'uint64'],
// [CROSS_CHAIN_ORDER_TYPEHASH, orderParamsHash, 97, '0x74a00e5cceb68d791486ddb9ea83bb8245eca22f67cb0ea81342f6eff8bf6e51', 1718955340461]
// ));
// TODO: change what to show
const signature = await signer.signTypedData(
getDomainData(chainId),
ORDER_TYPES,
{
crossChainOrder,
}
crossChainOrder
);
// https://github.com/poap-xyz/poap-fun/pull/62#issue-928290265
@@ -124,20 +97,23 @@ export const signOrder = async ({
const fixedSignature = ethers.Signature.from(signature).serialized;
// if (!fixedSignature) throw new Error("Can't sign order");
// const { orderHash, secret, secretHash } = hashOrder(limitOrder, chainId);
const signedOrder: SignedOrder = {
const signedOrderWithoutId: Omit<SignedOrder, 'id'> = {
...order,
id: limitOrderHash, // TODO: change to orderHash
signature: fixedSignature,
secret,
secretHash,
...(isCrossChain
? {
secret,
secretHash,
targetChainId: Number(targetChainId),
lockOrderExpiration: expiration
targetChainId: Number(targetChainId)
}
: {})
: {}),
lockOrderExpiration: expiration
}
const orderHash = getOrderHash(signedOrderWithoutId, chainId);
const signedOrder: SignedOrder = {
...signedOrderWithoutId,
id: orderHash
};
return signedOrder;
};

View File

@@ -231,6 +231,14 @@ class Aggregator {
const url = new URL(`${this.apiUrl}/api/v1/order/${isCreateInternalOrder ? 'internal' : ''}`);
const body = {
...signedOrder,
lockExpiration: signedOrder.lockOrderExpiration,
rawExchangeRestrictions
}
delete body.lockOrderExpiration;
return fetchWithValidation(
url.toString(),
z.object({
@@ -246,7 +254,7 @@ class Aggregator {
{
headers,
method: 'POST',
body: JSON.stringify({ ...signedOrder, rawExchangeRestrictions }),
body: JSON.stringify(body),
},
errorSchema,
);

View File

@@ -70,6 +70,7 @@ type SignedOrderAdditionalProps = {
secret?: string
secretHash?: string // bytes32
needWithdraw?: boolean // bool (not supported yet by smart-contract)
lockOrderExpiration?: number
}
export type SignedOrder = SignedOrderAdditionalProps & (Order | CrossOrder) & {