fix signLockOrder

This commit is contained in:
TheJuze
2024-01-24 12:23:14 +03:00
parent a5ab496475
commit c6be1c73c3
8 changed files with 44 additions and 75 deletions

View File

@@ -0,0 +1,11 @@
export const LOCK_ORDER_TYPES = {
Order: [
{ name: 'user', type: 'address' },
{ name: 'sender', type: 'address' },
{ name: 'expiration', type: 'uint64' },
{ name: 'asset', type: 'string' },
{ name: 'amount', type: 'uint64' },
{ name: 'targetChainId', type: 'uint64' },
{ name: 'secretHash', type: 'string' },
],
};

View File

@@ -0,0 +1,25 @@
import { ethers } from 'ethers';
import type { LockOrder } from '../types.js';
export const hashLockOrder = (order: LockOrder) => ethers.solidityPackedKeccak256(
[
'uint8',
'address',
'uint64',
'string',
'uint64',
'uint64',
'uint64',
'string'
],
[
'0x03',
order.user,
order.sender,
order.expiration,
order.asset,
order.amount,
order.targetChainId,
order.secretHash
],
);

View File

@@ -14,8 +14,6 @@ const hashOrder = (order: Order) => ethers.solidityPackedKeccak256(
'uint64',
'uint64',
'uint64',
'uint64',
'uint64',
'uint8',
],
[
@@ -26,12 +24,10 @@ const hashOrder = (order: Order) => ethers.solidityPackedKeccak256(
order.quoteAsset,
order.matcherFeeAsset,
order.amount,
order.targetChainId,
order.price,
order.matcherFee,
order.nonce,
order.expiration,
order.secretHash,
order.buySide === 1 ? '0x01' : '0x00',
],
);

View File

@@ -1,11 +1,8 @@
import type { TypedDataSigner } from '@ethersproject/abstract-signer';
import { ethers } from 'ethers';
import CANCEL_ORDER_TYPES from '../constants/cancelOrderTypes.js';
import type { CancelOrderRequest, SignedCancelOrderRequest, SupportedChainId } from '../types.js';
import getDomainData from './getDomainData.js';
type SignerWithTypedDataSign = ethers.Signer & TypedDataSigner;
const signCancelOrder = async (
senderAddress: string,
id: string,
@@ -16,10 +13,8 @@ const signCancelOrder = async (
id,
senderAddress,
};
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const typedDataSigner = signer as SignerWithTypedDataSign;
const signature = await typedDataSigner.signTypedData(
const signature = await signer.signTypedData(
getDomainData(chainId),
CANCEL_ORDER_TYPES,
cancelOrderRequest,

View File

@@ -1,17 +1,14 @@
import type { TypedDataSigner } from '@ethersproject/abstract-signer';
import { ethers } from 'ethers';
import ORDER_TYPES from '../constants/orderTypes.js';
import type { LockOrder, SignedLockOrder, SupportedChainId } from '../types.js';
import getDomainData from './getDomainData.js';
import generateSecret from '../utils/generateSecret';
import { BigNumber } from 'bignumber.js';
import normalizeNumber from '../utils/normalizeNumber';
import { INTERNAL_PROTOCOL_PRECISION } from '../constants';
import { LOCK_ORDER_TYPES } from '../constants/lockOrderTypes';
const DEFAULT_EXPIRATION = 29 * 24 * 60 * 60 * 1000; // 29 days
type SignerWithTypedDataSign = ethers.Signer & TypedDataSigner;
export type LockOrderProps = {
userAddress: string // адрес юзера который хочет сделать лок
senderAddress: string // broker
@@ -20,7 +17,6 @@ export type LockOrderProps = {
signer: ethers.Signer
chainId: SupportedChainId
targetChainId: SupportedChainId
logger?: (message: string) => void
}
export const signLockOrder = async ({
@@ -31,7 +27,6 @@ export const signLockOrder = async ({
targetChainId,
asset,
signer,
logger
}: LockOrderProps) => {
const nonce = Date.now();
const expiration = nonce + DEFAULT_EXPIRATION;
@@ -51,23 +46,16 @@ export const signLockOrder = async ({
targetChainId,
secretHash,
};
logger?.('❌ test1');
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const typedDataSigner = signer as SignerWithTypedDataSign;
logger?.('❌ test2');
const signature = await typedDataSigner.signTypedData(
const signature = await signer.signTypedData(
getDomainData(chainId),
ORDER_TYPES,
LOCK_ORDER_TYPES,
order,
);
logger?.('❌ test3');
// https://github.com/poap-xyz/poap-fun/pull/62#issue-928290265
// "Signature's v was always send as 27 or 28, but from Ledger was 0 or 1"
const fixedSignature = ethers.Signature.from(signature).serialized;
logger?.('❌ test4');
// if (!fixedSignature) throw new Error("Can't sign order");

View File

@@ -1,4 +1,3 @@
import type { TypedDataSigner } from '@ethersproject/abstract-signer';
import { BigNumber } from 'bignumber.js';
import { ethers } from 'ethers';
import { INTERNAL_PROTOCOL_PRECISION } from '../constants';
@@ -11,8 +10,6 @@ import generateSecret from '../utils/generateSecret';
const DEFAULT_EXPIRATION = 29 * 24 * 60 * 60 * 1000; // 29 days
type SignerWithTypedDataSign = ethers.Signer & TypedDataSigner;
export const signOrder = async (
baseAssetAddr: string,
quoteAssetAddr: string,
@@ -66,10 +63,7 @@ export const signOrder = async (
buySide: side === 'BUY' ? 1 : 0,
};
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const typedDataSigner = signer as SignerWithTypedDataSign;
const signature = await typedDataSigner.signTypedData(
const signature = await signer.signTypedData(
getDomainData(chainId),
ORDER_TYPES,
order,