UNPKG

@f5i23q999d/cow-sdk

Version:

<p align="center"> <img width="400" src="https://github.com/cowprotocol/cow-sdk/raw/main/docs/images/CoW.png" /> </p>

156 lines 7.46 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OrderSigningUtils = void 0; const getSignUtils = () => Promise.resolve().then(() => __importStar(require('./utils.js'))); const ethersUtils = () => Promise.resolve().then(() => __importStar(require('ethers/lib/utils'))); /** * Utility class for signing order intents and cancellations. * * @remarks This class only supports `eth_sign` and wallet-native EIP-712 signing. For use of * `presign` and `eip1271` {@link https://docs.cow.fi/ | see the docs}. * @example * * ```typescript * import { OrderSigningUtils, SupportedChainId, UnsignedOrder } from '@cowprotocol/cow-sdk' * import { Web3Provider } from '@ethersproject/providers' * * const account = 'YOUR_WALLET_ADDRESS' * const chainId = 100 // Gnosis chain * const provider = new Web3Provider(window.ethereum) * const signer = provider.getSigner() * * async function main() { * const orderToSign: UnsignedOrder = { ... } * const orderSigningResult = await OrderSigningUtils.signOrder(orderToSign, chainId, signer) * * const orderId = await orderBookApi.sendOrder({ ...orderToSign, ...orderSigningResult }) * * const order = await orderBookApi.getOrder(orderId) * * const trades = await orderBookApi.getTrades({ orderId }) * * const orderCancellationSigningResult = await OrderSigningUtils.signOrderCancellations([orderId], chainId, signer) * * const cancellationResult = await orderBookApi.sendSignedOrderCancellations({...orderCancellationSigningResult, orderUids: [orderId] }) * * console.log('Results: ', { orderId, order, trades, orderCancellationSigningResult, cancellationResult }) * } * ``` */ class OrderSigningUtils { /** * Sign the order intent with the specified signer. * * @remarks If the API reports an error with the signature, it is likely to be due to an incorrectly * specified `chainId`. Please ensure that the `chainId` is correct for the network you are * using. * @param {UnsignedOrder} order The unsigned order intent to be placed. * @param {SupportedChainId} chainId The CoW Protocol `chainId` context that's being used. * @param {Signer} signer The signer who is placing the order intent. * @returns {Promise<SigningResult>} Encoded signature including signing scheme for the order. */ static async signOrder(order, chainId, signer) { const { signOrder } = await getSignUtils(); return signOrder(order, chainId, signer); } /** * Sign a cancellation message of an order intent with the specified signer. * @param {string} orderUid The unique identifier of the order to cancel. * @param {SupportedChainId} chainId The CoW Protocol `chainid` context that's being used. * @param {Signer} signer The signer who initially placed the order intent. * @returns {Promise<SigningResult>} Encoded signature including signing scheme for the cancellation. */ static async signOrderCancellation(orderUid, chainId, signer) { const { signOrderCancellation } = await getSignUtils(); return signOrderCancellation(orderUid, chainId, signer); } /** * Sign a cancellation message of multiple order intents with the specified signer. * @param {string[]} orderUids An array of `orderUid` to cancel. * @param {SupportedChainId} chainId The CoW Protocol `chainId` context that's being used. * @param {Signer} signer The signer who initially placed the order intents. * @returns {Promise<SigningResult>} Encoded signature including signing scheme for the cancellation. */ static async signOrderCancellations(orderUids, chainId, signer) { const { signOrderCancellations } = await getSignUtils(); return signOrderCancellations(orderUids, chainId, signer); } /** * Get the EIP-712 typed domain data being used for signing. * @param {SupportedChainId} chainId The CoW Protocol `chainId` context that's being used. * @return The EIP-712 typed domain data. * @see https://eips.ethereum.org/EIPS/eip-712 */ static async getDomain(chainId) { const { getDomain } = await getSignUtils(); return getDomain(chainId); } /** * Hashes the order intent and generate deterministic order ID. * @param {SupportedChainId} chainId The CoW Protocol `chainId` context that's being used. * @param {Order} order order to sign * @param {Pick<OrderUidParams, 'owner'>} params order unique identifier parameters. */ static async generateOrderId(chainId, order, params) { const { generateOrderId } = await getSignUtils(); return generateOrderId(chainId, order, params); } /** * Get the domain separator hash for the EIP-712 typed domain data being used for signing. * @param chainId {SupportedChainId} chainId The CoW Protocol protocol `chainId` context that's being used. * @returns A string representation of the EIP-712 typed domain data hash. */ static async getDomainSeparator(chainId) { const { getDomain } = await getSignUtils(); const { _TypedDataEncoder } = await ethersUtils(); return _TypedDataEncoder.hashDomain(getDomain(chainId)); } /** * Get the EIP-712 types used for signing a GPv2Order.Data struct. This is useful for when * signing orders using smart contracts, whereby this SDK cannot do the EIP-1271 signing for you. * @returns The EIP-712 types used for signing. */ static getEIP712Types() { return { Order: [ { name: 'sellToken', type: 'address' }, { name: 'buyToken', type: 'address' }, { name: 'receiver', type: 'address' }, { name: 'sellAmount', type: 'uint256' }, { name: 'buyAmount', type: 'uint256' }, { name: 'validTo', type: 'uint32' }, { name: 'appData', type: 'bytes32' }, { name: 'feeAmount', type: 'uint256' }, { name: 'kind', type: 'string' }, { name: 'partiallyFillable', type: 'bool' }, { name: 'sellTokenBalance', type: 'string' }, { name: 'buyTokenBalance', type: 'string' }, ], }; } } exports.OrderSigningUtils = OrderSigningUtils; //# sourceMappingURL=orderSigningUtils.js.map