SDK 命令签名
测试覆盖率
| 发言 | 分支 | 职能 | 行数 |
|---|---|---|---|
这个软件包为CoW协议提供EIP-712订单签名和取消功能. 它负责所有根据CoW协议的规格签署订单和注销所需的密码操作.
安装
npm install @cowprotocol/sdk-order-signing
or
pnpm add @cowprotocol/sdk-order-signing
or
yarn add @cowprotocol/sdk-order-signing
使用量
import { OrderSigningUtils, SupportedChainId, UnsignedOrder, SigningResult } from '@cowprotocol/sdk-order-signing'
import { EthersV6Adapter } from '@cowprotocol/sdk-ethers-v6-adapter'
import { JsonRpcProvider, Wallet } from 'ethers'
// Configure the adapter
const provider = new JsonRpcProvider('YOUR_RPC_URL')
const wallet = new Wallet('YOUR_PRIVATE_KEY', provider)
const adapter = new EthersV6Adapter({ provider, signer: wallet })
// Configure the global adapter
setGlobalAdapter(adapter)
// Sign an order using static methods
const orderToSign: UnsignedOrder = {
sellToken: '0xA0b86a33E6417b528874E10EB3a95beb4F25A0E3',
buyToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
sellAmount: '1000000000000000000',
buyAmount: '1000000000000000000',
validTo: Math.floor(Date.now() / 1000) + 3600,
appData: '0x0000000000000000000000000000000000000000000000000000000000000000',
feeAmount: '0',
kind: 'sell',
partiallyFillable: false,
sellTokenBalance: 'erc20',
buyTokenBalance: 'erc20',
receiver: '0x0000000000000000000000000000000000000000',
}
const signingResult: SigningResult = await OrderSigningUtils.signOrder(
orderToSign,
SupportedChainId.MAINNET,
adapter.signer,
)
// Sign order cancellation
const orderId = 'ORDER_UID_TO_CANCEL'
const cancellationResult = await OrderSigningUtils.signOrderCancellation(
orderId,
SupportedChainId.MAINNET,
adapter.signer,
)
// Generate order ID
const { orderId: generatedId, orderDigest } = await OrderSigningUtils.generateOrderId(
SupportedChainId.MAINNET,
orderToSign as any,
{ owner: await adapter.signer.getAddress() },
)
使用 CoW SDK 软件
import { CowSdk, OrderSigningUtils, SupportedChainId, UnsignedOrder } from '@cowprotocol/cow-sdk'
import { EthersV6Adapter } from '@cowprotocol/sdk-ethers-v6-adapter'
import { JsonRpcProvider, Wallet } from 'ethers'
// Configure the adapter
const provider = new JsonRpcProvider('YOUR_RPC_URL')
const wallet = new Wallet('YOUR_PRIVATE_KEY', provider)
const adapter = new EthersV6Adapter({ provider, signer: wallet })
// Initialize the unified SDK
const sdk = new CowSdk({
chainId: SupportedChainId.MAINNET,
adapter,
})
// Use static methods through sdk.orderSigning
const signingResult = await sdk.orderSigning.signOrder(orderToSign, SupportedChainId.MAINNET, adapter.signer)
// Sign cancellation
const cancellationResult = await sdk.orderSigning.signOrderCancellation(
orderId,
SupportedChainId.MAINNET,
adapter.signer,
)
签名计划
SDK支持多个签名方案:
EIP-712(过失)
使用 EIP-712 输入数据的标准钱包签名 :
// Automatically uses EIP-712 for wallet signers
const result = await OrderSigningUtils.signOrder(order, chainId, signer)
智能合同钱包( 预签名)
对于Safe这样的智能合同钱包:
import { SigningScheme } from '@cowprotocol/sdk-order-signing'
// For smart contract wallets, specify the signing scheme
// Then execute the pre-sign transaction on-chain
EIP-1271 (中文(简体) )
对于执行EIP-1271的合同:
// Used for contracts that can validate signatures
// The SDK automatically detects and handles this
与顺序书合并
本套件与"秩序书"API无缝:
// 1. Sign the order
const signingResult = await OrderSigningUtils.signOrder(order, chainId, signer)
// 2. Submit to order book
const orderId = await orderBookApi.sendOrder({ ...order, ...signingResult })
// 3. Cancel if needed
const cancellation = await OrderSigningUtils.signOrderCancellation(orderId, chainId, signer)
await orderBookApi.sendSignedOrderCancellations({ ...cancellation, orderUids: [orderId] })
说明: 这个软件包处理 CoW 协议订单的密码方面. 它通常与Trading SDK或Order Book API一起用于完整的订单管理工作流程.