Skip to main content
The signing module provides utilities for signing CoW Protocol orders using various signature schemes.

Types

SigningScheme

Enum representing the signature scheme used to sign an order.
enum SigningScheme {
  EIP712 = 0b00,
  ETHSIGN = 0b01,
  EIP1271 = 0b10,
  PRESIGN = 0b11
}
EIP712
0b00
EIP-712 typed data signing (preferred). Provides more information to wallets about the data being signed.
ETHSIGN
0b01
Message signed using eth_sign RPC call.
EIP1271
0b10
Smart contract signatures as defined in EIP-1271. For contract-based wallets.
PRESIGN
0b11
Pre-signed order. The order is signed on-chain before submission.

Signature

Union type representing any valid order signature.
type Signature = EcdsaSignature | Eip1271Signature | PreSignSignature;

EcdsaSignature

ECDSA signature for EIP-712 or ETHSIGN schemes.
interface EcdsaSignature {
  scheme: EcdsaSigningScheme;
  data: SignatureLike;
}
scheme
SigningScheme.EIP712 | SigningScheme.ETHSIGN
required
The ECDSA signing scheme used.
data
SignatureLike
required
The ECDSA signature data (r, s, v components).

Eip1271Signature

EIP-1271 smart contract signature.
interface Eip1271Signature {
  scheme: SigningScheme.EIP1271;
  data: Eip1271SignatureData;
}
scheme
SigningScheme.EIP1271
required
Must be SigningScheme.EIP1271.
data
Eip1271SignatureData
required
The EIP-1271 signature data containing verifier and signature bytes.

Eip1271SignatureData

Data for EIP-1271 signature verification.
interface Eip1271SignatureData {
  verifier: string;
  signature: BytesLike;
}
verifier
string
required
The address of the smart contract that will verify the signature.
signature
BytesLike
required
Arbitrary signature data passed to the verifying contract’s isValidSignature method.

PreSignSignature

Signature data for a pre-signed order.
interface PreSignSignature {
  scheme: SigningScheme.PRESIGN;
  data: string;
}
scheme
SigningScheme.PRESIGN
required
Must be SigningScheme.PRESIGN.
data
string
required
The address of the order signer.

Functions

signOrder

Signs a CoW Protocol order using an externally owned account (EOA).
async function signOrder(
  domain: TypedDataDomain,
  order: Order,
  owner: Signer,
  scheme: EcdsaSigningScheme
): Promise<EcdsaSignature>
domain
TypedDataDomain
required
The EIP-712 domain separator. Ensures orders can’t be replayed across different deployments or chains.
order
Order
required
The order to sign.
owner
Signer
required
The Ethers.js signer (wallet) that will sign the order.
scheme
EcdsaSigningScheme
required
The signing scheme to use: SigningScheme.EIP712 (recommended) or SigningScheme.ETHSIGN.
signature
EcdsaSignature
The ECDSA signature with the signing scheme encoded.

Example

import { signOrder, SigningScheme, domain } from "@cowprotocol/contracts";
import { Wallet } from "ethers";

const wallet = new Wallet("0x...");
const settlementDomain = domain(1, "0x9008D19f58AAbD9eD0D60971565AA8510560ab41");

const signature = await signOrder(
  settlementDomain,
  order,
  wallet,
  SigningScheme.EIP712
);

encodeEip1271SignatureData

Encodes EIP-1271 signature data for use with CoW Protocol contracts.
function encodeEip1271SignatureData(
  data: Eip1271SignatureData
): string
data
Eip1271SignatureData
required
The EIP-1271 signature data to encode.
encoded
string
Hex-encoded signature data with verifier address and signature bytes packed together.

decodeEip1271SignatureData

Decodes a CoW Protocol EIP-1271 signature into its components.
function decodeEip1271SignatureData(
  signature: BytesLike
): Eip1271SignatureData
signature
BytesLike
required
The encoded EIP-1271 signature data.
decoded
Eip1271SignatureData
The decoded signature containing the verifier address and signature bytes.

Constants

EIP1271_MAGICVALUE

The magic value returned by a successful isValidSignature call.
const EIP1271_MAGICVALUE = "0x1626ba7e";
This is bytes4(keccak256("isValidSignature(bytes32,bytes)")) as defined in the EIP-1271 standard.

Usage Notes

EIP-712 is the recommended signing scheme as it provides the best user experience. Wallets can display structured information about what the user is signing.
EIP-1271 is required for smart contract wallets like Gnosis Safe, Argent, or any contract-based wallet that doesn’t have a private key.
Pre-signed orders require an on-chain transaction to authorize the order before it can be settled. This is useful for integration with other smart contracts.

Type Definitions

SignatureLike

A type that can be converted to an Ethers.js signature.
type SignatureLike = string | {
  r: string;
  s: string;
  v: number;
  yParityAndS?: string;
  compact?: string;
};

EcdsaSigningScheme

Type alias for ECDSA signing schemes.
type EcdsaSigningScheme = SigningScheme.EIP712 | SigningScheme.ETHSIGN;

Complete Example

import {
  Order,
  OrderKind,
  signOrder,
  SigningScheme,
  domain,
  Signature,
} from "@cowprotocol/contracts";
import { Wallet } from "ethers";

// Create an order
const order: Order = {
  sellToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
  buyToken: "0x6B175474E89094C44Da98b954EedeAC495271d0F",
  sellAmount: "1000000000000000000",
  buyAmount: "3000000000000000000000",
  validTo: Math.floor(Date.now() / 1000) + 3600,
  appData: 1,
  feeAmount: "5000000000000000",
  kind: OrderKind.SELL,
  partiallyFillable: false,
};

// Sign the order
const wallet = new Wallet(privateKey);
const settlementDomain = domain(1, "0x9008D19f58AAbD9eD0D60971565AA8510560ab41");

const signature: Signature = await signOrder(
  settlementDomain,
  order,
  wallet,
  SigningScheme.EIP712
);

console.log("Signature scheme:", signature.scheme);
console.log("Signature data:", signature.data);

Build docs developers (and LLMs) love