File structure improvements

This commit is contained in:
Aleksandr Kraiz
2022-04-22 07:27:24 +04:00
parent c92b277d80
commit 5c96476849
10 changed files with 176 additions and 154 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@orionprotocol/sdk",
"version": "0.2.1",
"version": "0.2.2",
"description": "Orion Protocol SDK",
"main": "lib/index.js",
"types": "lib/index.d.ts",

View File

@@ -1,151 +0,0 @@
/* eslint-disable no-underscore-dangle */
import { TypedDataSigner } from '@ethersproject/abstract-signer';
import BigNumber from 'bignumber.js';
import { ethers } from 'ethers';
import {
CancelOrderRequest, Order, SignedCancelOrderRequest, SignedOrder, SupportedChainId,
} from './types';
import eip712DomainData from './config/eip712DomainData.json';
import eip712DomainSchema from './config/schemas/eip712DomainSchema';
import { hashOrder, normalizeNumber } from './utils';
import { INTERNAL_ORION_PRECISION } from './constants/precisions';
import ORDER_TYPES from './constants/orderTypes';
import CANCEL_ORDER_TYPES from './constants/cancelOrderTypes';
import signOrderPersonal from './utils/signOrderPersonal';
const DEFAULT_EXPIRATION = 29 * 24 * 60 * 60 * 1000; // 29 days
type SignerWithTypedDataSign = ethers.Signer & TypedDataSigner;
const EIP712Domain = eip712DomainSchema.parse(eip712DomainData);
const { arrayify, joinSignature, splitSignature } = ethers.utils;
/**
* See {@link https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator}
*/
const getDomainData = (chainId: SupportedChainId) => ({
...EIP712Domain,
chainId,
});
export const signOrder = async (
baseAssetAddr: string,
quoteAssetAddr: string,
side: 'BUY' | 'SELL',
price: BigNumber.Value,
amount: BigNumber.Value,
matcherFee: BigNumber.Value,
senderAddress: string,
matcherAddress: string,
orionFeeAssetAddr: string,
usePersonalSign: boolean,
signer: ethers.Signer,
chainId: SupportedChainId,
) => {
const nonce = Date.now();
const expiration = nonce + DEFAULT_EXPIRATION;
const order: Order = {
senderAddress,
matcherAddress,
baseAsset: baseAssetAddr,
quoteAsset: quoteAssetAddr,
matcherFeeAsset: orionFeeAssetAddr,
amount: normalizeNumber(
amount,
INTERNAL_ORION_PRECISION,
BigNumber.ROUND_FLOOR,
).toNumber(),
price: normalizeNumber(
price,
INTERNAL_ORION_PRECISION,
BigNumber.ROUND_FLOOR,
).toNumber(),
matcherFee: normalizeNumber(
matcherFee,
INTERNAL_ORION_PRECISION,
BigNumber.ROUND_CEIL, // ROUND_CEIL because we don't want get "not enough fee" error
).toNumber(),
nonce,
expiration,
buySide: side === 'BUY' ? 1 : 0,
isPersonalSign: usePersonalSign,
};
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const typedDataSigner = signer as SignerWithTypedDataSign;
const signature = usePersonalSign
? await signOrderPersonal(order, signer)
: await typedDataSigner._signTypedData(
getDomainData(chainId),
ORDER_TYPES,
order,
);
// 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 = joinSignature(splitSignature(signature));
if (!fixedSignature) throw new Error("Can't sign order");
const signedOrder: SignedOrder = {
...order,
id: hashOrder(order),
signature: fixedSignature,
};
return signedOrder;
};
export const signCancelOrderPersonal = async (
cancelOrderRequest: CancelOrderRequest,
signer: ethers.Signer,
) => {
const types = ['string', 'string', 'address'];
const message = ethers.utils.solidityKeccak256(
types,
['cancelOrder', cancelOrderRequest.id, cancelOrderRequest.senderAddress],
);
const signature = await signer.signMessage(arrayify(message));
// NOTE: metamask broke sig.v value and we fix it in next line
return joinSignature(splitSignature(signature));
};
export const signCancelOrder = async (
senderAddress: string,
id: string,
usePersonalSign: boolean,
signer: ethers.Signer,
chainId: SupportedChainId,
) => {
const cancelOrderRequest: CancelOrderRequest = {
id,
senderAddress,
isPersonalSign: usePersonalSign,
};
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const typedDataSigner = signer as SignerWithTypedDataSign;
const signature = usePersonalSign
? await signCancelOrderPersonal(cancelOrderRequest, signer)
// https://docs.ethers.io/v5/api/signer/#Signer-signTypedData
: await typedDataSigner._signTypedData(
getDomainData(chainId),
CANCEL_ORDER_TYPES,
cancelOrderRequest,
);
// 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 = joinSignature(splitSignature(signature));
if (!fixedSignature) throw new Error("Can't sign order cancel");
const signedCancelOrderReqeust: SignedCancelOrderRequest = {
...cancelOrderRequest,
signature: fixedSignature,
};
return signedCancelOrderReqeust;
};

View File

@@ -0,0 +1,15 @@
import { SupportedChainId } from '../types';
import eip712DomainData from '../config/eip712DomainData.json';
import eip712DomainSchema from '../config/schemas/eip712DomainSchema';
const EIP712Domain = eip712DomainSchema.parse(eip712DomainData);
/**
* See {@link https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator}
*/
const getDomainData = (chainId: SupportedChainId) => ({
...EIP712Domain,
chainId,
});
export default getDomainData;

4
src/crypt/index.ts Normal file
View File

@@ -0,0 +1,4 @@
export { default as signCancelOrder } from './signCancelOrder';
export { default as signCancelOrderPersonal } from './signCancelOrderPersonal';
export { default as signOrder } from './signOrder';
export { default as signOrderPersonal } from './signOrderPersonal';

View File

@@ -0,0 +1,49 @@
/* eslint-disable no-underscore-dangle */
import { TypedDataSigner } from '@ethersproject/abstract-signer';
import { ethers } from 'ethers';
import { joinSignature, splitSignature } from 'ethers/lib/utils';
import CANCEL_ORDER_TYPES from '../constants/cancelOrderTypes';
import { CancelOrderRequest, SignedCancelOrderRequest, SupportedChainId } from '../types';
import getDomainData from './getDomainData';
import signCancelOrderPersonal from './signCancelOrderPersonal';
type SignerWithTypedDataSign = ethers.Signer & TypedDataSigner;
const signCancelOrder = async (
senderAddress: string,
id: string,
usePersonalSign: boolean,
signer: ethers.Signer,
chainId: SupportedChainId,
) => {
const cancelOrderRequest: CancelOrderRequest = {
id,
senderAddress,
isPersonalSign: usePersonalSign,
};
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const typedDataSigner = signer as SignerWithTypedDataSign;
const signature = usePersonalSign
? await signCancelOrderPersonal(cancelOrderRequest, signer)
// https://docs.ethers.io/v5/api/signer/#Signer-signTypedData
: await typedDataSigner._signTypedData(
getDomainData(chainId),
CANCEL_ORDER_TYPES,
cancelOrderRequest,
);
// 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 = joinSignature(splitSignature(signature));
if (!fixedSignature) throw new Error("Can't sign order cancel");
const signedCancelOrderReqeust: SignedCancelOrderRequest = {
...cancelOrderRequest,
signature: fixedSignature,
};
return signedCancelOrderReqeust;
};
export default signCancelOrder;

View File

@@ -0,0 +1,20 @@
import { ethers } from 'ethers';
import { arrayify, joinSignature, splitSignature } from 'ethers/lib/utils';
import { CancelOrderRequest } from '../types';
export const signCancelOrderPersonal = async (
cancelOrderRequest: CancelOrderRequest,
signer: ethers.Signer,
) => {
const types = ['string', 'string', 'address'];
const message = ethers.utils.solidityKeccak256(
types,
['cancelOrder', cancelOrderRequest.id, cancelOrderRequest.senderAddress],
);
const signature = await signer.signMessage(arrayify(message));
// NOTE: metamask broke sig.v value and we fix it in next line
return joinSignature(splitSignature(signature));
};
export default signCancelOrderPersonal;

87
src/crypt/signOrder.ts Normal file
View File

@@ -0,0 +1,87 @@
/* eslint-disable no-underscore-dangle */
import { TypedDataSigner } from '@ethersproject/abstract-signer';
import BigNumber from 'bignumber.js';
import { ethers } from 'ethers';
import { joinSignature, splitSignature } from 'ethers/lib/utils';
import { INTERNAL_ORION_PRECISION } from '../constants';
import ORDER_TYPES from '../constants/orderTypes';
import { Order, SignedOrder, SupportedChainId } from '../types';
import normalizeNumber from '../utils/normalizeNumber';
import getDomainData from './getDomainData';
import hashOrder from './hashOrder';
import signOrderPersonal from './signOrderPersonal';
const DEFAULT_EXPIRATION = 29 * 24 * 60 * 60 * 1000; // 29 days
type SignerWithTypedDataSign = ethers.Signer & TypedDataSigner;
export const signOrder = async (
baseAssetAddr: string,
quoteAssetAddr: string,
side: 'BUY' | 'SELL',
price: BigNumber.Value,
amount: BigNumber.Value,
matcherFee: BigNumber.Value,
senderAddress: string,
matcherAddress: string,
orionFeeAssetAddr: string,
usePersonalSign: boolean,
signer: ethers.Signer,
chainId: SupportedChainId,
) => {
const nonce = Date.now();
const expiration = nonce + DEFAULT_EXPIRATION;
const order: Order = {
senderAddress,
matcherAddress,
baseAsset: baseAssetAddr,
quoteAsset: quoteAssetAddr,
matcherFeeAsset: orionFeeAssetAddr,
amount: normalizeNumber(
amount,
INTERNAL_ORION_PRECISION,
BigNumber.ROUND_FLOOR,
).toNumber(),
price: normalizeNumber(
price,
INTERNAL_ORION_PRECISION,
BigNumber.ROUND_FLOOR,
).toNumber(),
matcherFee: normalizeNumber(
matcherFee,
INTERNAL_ORION_PRECISION,
BigNumber.ROUND_CEIL, // ROUND_CEIL because we don't want get "not enough fee" error
).toNumber(),
nonce,
expiration,
buySide: side === 'BUY' ? 1 : 0,
isPersonalSign: usePersonalSign,
};
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const typedDataSigner = signer as SignerWithTypedDataSign;
const signature = usePersonalSign
? await signOrderPersonal(order, signer)
: await typedDataSigner._signTypedData(
getDomainData(chainId),
ORDER_TYPES,
order,
);
// 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 = joinSignature(splitSignature(signature));
if (!fixedSignature) throw new Error("Can't sign order");
const signedOrder: SignedOrder = {
...order,
id: hashOrder(order),
signature: fixedSignature,
};
return signedOrder;
};
export default signOrder;

View File

@@ -2,9 +2,7 @@ export { default as calculateFeeInFeeAsset } from './calculateFeeInFeeAsset';
export { default as calculateNetworkFee } from './calculateNetworkFee';
export { default as calculateNetworkFeeInFeeAsset } from './calculateNetworkFeeInFeeAsset';
export { default as calculateOrionFeeInFeeAsset } from './calculateOrionFeeInFeeAsset';
export { default as hashOrder } from './hashOrder';
export { default as checkIsToken } from './checkIsToken';
export { default as signOrderPersonal } from './signOrderPersonal';
export { default as generateSecret } from './generateSecret';
export { default as isValidChainId } from './isValidChainId';
export { default as denormalizeNumber } from './denormalizeNumber';