@cowprotocol/sdk-order-signing 0.2.2-beta.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -3
- package/dist/index.d.mts +49 -12
- package/dist/index.d.ts +49 -12
- package/dist/index.js +110 -48
- package/dist/index.mjs +112 -49
- package/package.json +14 -9
package/README.md
CHANGED
|
@@ -18,8 +18,6 @@ yarn add @cowprotocol/sdk-order-signing
|
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
|
-
### Individual package usage
|
|
22
|
-
|
|
23
21
|
```typescript
|
|
24
22
|
import { OrderSigningUtils, SupportedChainId, UnsignedOrder, SigningResult } from '@cowprotocol/sdk-order-signing'
|
|
25
23
|
import { EthersV6Adapter } from '@cowprotocol/sdk-ethers-v6-adapter'
|
|
@@ -71,7 +69,7 @@ const { orderId: generatedId, orderDigest } = await OrderSigningUtils.generateOr
|
|
|
71
69
|
)
|
|
72
70
|
```
|
|
73
71
|
|
|
74
|
-
### Usage with
|
|
72
|
+
### Usage with CoW SDK
|
|
75
73
|
|
|
76
74
|
```typescript
|
|
77
75
|
import { CowSdk, OrderSigningUtils, SupportedChainId, UnsignedOrder } from '@cowprotocol/cow-sdk'
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SupportedChainId } from '@cowprotocol/sdk-config';
|
|
1
|
+
import { SupportedChainId, CowEnv, AddressPerChain, ProtocolOptions } from '@cowprotocol/sdk-config';
|
|
2
2
|
import { ContractsOrder, OrderUidParams } from '@cowprotocol/sdk-contracts-ts';
|
|
3
3
|
import { SignerLike, Signer, TypedDataDomain } from '@cowprotocol/sdk-common';
|
|
4
4
|
import { OrderParameters, EcdsaSigningScheme } from '@cowprotocol/sdk-order-book';
|
|
@@ -28,6 +28,8 @@ interface SignOrderParams {
|
|
|
28
28
|
signer: SignerLike;
|
|
29
29
|
order: UnsignedOrder;
|
|
30
30
|
signingScheme: EcdsaSigningScheme;
|
|
31
|
+
env?: CowEnv;
|
|
32
|
+
settlementContractOverride?: Partial<AddressPerChain>;
|
|
31
33
|
}
|
|
32
34
|
/**
|
|
33
35
|
* Parameters for signing an order cancellation.
|
|
@@ -41,6 +43,8 @@ interface SignOrderCancellationParams {
|
|
|
41
43
|
signer: SignerLike;
|
|
42
44
|
orderUid: string;
|
|
43
45
|
signingScheme: EcdsaSigningScheme;
|
|
46
|
+
env?: CowEnv;
|
|
47
|
+
settlementContractOverride?: Partial<AddressPerChain>;
|
|
44
48
|
}
|
|
45
49
|
/**
|
|
46
50
|
* Parameters for signing multiple bulk order cancellations.
|
|
@@ -54,8 +58,21 @@ interface SignOrderCancellationsParams {
|
|
|
54
58
|
signer: SignerLike;
|
|
55
59
|
orderUids: string[];
|
|
56
60
|
signingScheme: EcdsaSigningScheme;
|
|
61
|
+
env?: CowEnv;
|
|
62
|
+
settlementContractOverride?: Partial<AddressPerChain>;
|
|
57
63
|
}
|
|
58
64
|
|
|
65
|
+
declare const ORDER_PRIMARY_TYPE: "Order";
|
|
66
|
+
/**
|
|
67
|
+
* The EIP-712 types used for signing a GPv2Order.Data struct. This is useful for when
|
|
68
|
+
* signing orders using smart contracts, whereby this SDK cannot do the EIP-1271 signing for you.
|
|
69
|
+
*/
|
|
70
|
+
declare const COW_EIP712_TYPES: {
|
|
71
|
+
Order: {
|
|
72
|
+
name: string;
|
|
73
|
+
type: string;
|
|
74
|
+
}[];
|
|
75
|
+
};
|
|
59
76
|
/**
|
|
60
77
|
* Utility class for signing order intents and cancellations.
|
|
61
78
|
*
|
|
@@ -106,7 +123,7 @@ declare class OrderSigningUtils {
|
|
|
106
123
|
* @param {Signer} signer The signer who is placing the order intent.
|
|
107
124
|
* @returns {Promise<SigningResult>} Encoded signature including signing scheme for the order.
|
|
108
125
|
*/
|
|
109
|
-
static signOrder(order: UnsignedOrder, chainId: SupportedChainId, signer: Signer): Promise<SigningResult>;
|
|
126
|
+
static signOrder(order: UnsignedOrder, chainId: SupportedChainId, signer: Signer, options?: ProtocolOptions): Promise<SigningResult>;
|
|
110
127
|
/**
|
|
111
128
|
* Sign a cancellation message of an order intent with the specified signer.
|
|
112
129
|
* @param {string} orderUid The unique identifier of the order to cancel.
|
|
@@ -114,7 +131,7 @@ declare class OrderSigningUtils {
|
|
|
114
131
|
* @param {Signer} signer The signer who initially placed the order intent.
|
|
115
132
|
* @returns {Promise<SigningResult>} Encoded signature including signing scheme for the cancellation.
|
|
116
133
|
*/
|
|
117
|
-
static signOrderCancellation(orderUid: string, chainId: SupportedChainId, signer: Signer): Promise<SigningResult>;
|
|
134
|
+
static signOrderCancellation(orderUid: string, chainId: SupportedChainId, signer: Signer, options?: ProtocolOptions): Promise<SigningResult>;
|
|
118
135
|
/**
|
|
119
136
|
* Sign a cancellation message of multiple order intents with the specified signer.
|
|
120
137
|
* @param {string[]} orderUids An array of `orderUid` to cancel.
|
|
@@ -122,21 +139,21 @@ declare class OrderSigningUtils {
|
|
|
122
139
|
* @param {Signer} signer The signer who initially placed the order intents.
|
|
123
140
|
* @returns {Promise<SigningResult>} Encoded signature including signing scheme for the cancellation.
|
|
124
141
|
*/
|
|
125
|
-
static signOrderCancellations(orderUids: string[], chainId: SupportedChainId, signer: Signer): Promise<SigningResult>;
|
|
142
|
+
static signOrderCancellations(orderUids: string[], chainId: SupportedChainId, signer: Signer, options?: ProtocolOptions): Promise<SigningResult>;
|
|
126
143
|
/**
|
|
127
144
|
* Get the EIP-712 typed domain data being used for signing.
|
|
128
145
|
* @param {SupportedChainId} chainId The CoW Protocol `chainId` context that's being used.
|
|
129
146
|
* @return The EIP-712 typed domain data.
|
|
130
147
|
* @see https://eips.ethereum.org/EIPS/eip-712
|
|
131
148
|
*/
|
|
132
|
-
static getDomain(chainId: SupportedChainId): Promise<TypedDataDomain>;
|
|
149
|
+
static getDomain(chainId: SupportedChainId, options?: ProtocolOptions): Promise<TypedDataDomain>;
|
|
133
150
|
/**
|
|
134
151
|
* Hashes the order intent and generate deterministic order ID.
|
|
135
152
|
* @param {SupportedChainId} chainId The CoW Protocol `chainId` context that's being used.
|
|
136
153
|
* @param {Order} order order to sign
|
|
137
154
|
* @param {Pick<OrderUidParams, 'owner'>} params order unique identifier parameters.
|
|
138
155
|
*/
|
|
139
|
-
static generateOrderId(chainId: SupportedChainId, order: ContractsOrder, params: Pick<OrderUidParams, 'owner'
|
|
156
|
+
static generateOrderId(chainId: SupportedChainId, order: ContractsOrder, params: Pick<OrderUidParams, 'owner'>, options?: ProtocolOptions): Promise<{
|
|
140
157
|
orderId: string;
|
|
141
158
|
orderDigest: string;
|
|
142
159
|
}>;
|
|
@@ -145,13 +162,33 @@ declare class OrderSigningUtils {
|
|
|
145
162
|
* @param chainId {SupportedChainId} chainId The CoW Protocol protocol `chainId` context that's being used.
|
|
146
163
|
* @returns A string representation of the EIP-712 typed domain data hash.
|
|
147
164
|
*/
|
|
148
|
-
static getDomainSeparator(chainId: SupportedChainId): Promise<string>;
|
|
165
|
+
static getDomainSeparator(chainId: SupportedChainId, options?: ProtocolOptions): Promise<string>;
|
|
166
|
+
static getEIP712Types(): typeof COW_EIP712_TYPES;
|
|
149
167
|
/**
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
* @
|
|
168
|
+
* Encodes an order and ECDSA signature for EIP-1271 smart contract signature verification.
|
|
169
|
+
*
|
|
170
|
+
* @remarks This method encodes the order data and ECDSA signature into a format suitable for
|
|
171
|
+
* EIP-1271 signature verification by smart contracts. The order struct is ABI-encoded
|
|
172
|
+
* as a tuple along with the ECDSA signature bytes. String fields in the order are
|
|
173
|
+
* hashed using keccak256 before encoding.
|
|
174
|
+
*
|
|
175
|
+
* @param {UnsignedOrder} orderToSign The unsigned order to encode for EIP-1271 verification.
|
|
176
|
+
* @param {string} ecdsaSignature The ECDSA signature (typically 65 bytes hex-encoded) to include in the encoding.
|
|
177
|
+
* @returns {string} The ABI-encoded order struct and signature, ready for EIP-1271 verification.
|
|
178
|
+
*
|
|
179
|
+
* @see https://eips.ethereum.org/EIPS/eip-1271
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* const orderToSign: UnsignedOrder = { ... }
|
|
184
|
+
* const ecdsaSignature = '0x...' // 65 bytes signature from signing the order
|
|
185
|
+
*
|
|
186
|
+
* const eip1271Signature = OrderSigningUtils.getEip1271Signature(orderToSign, ecdsaSignature)
|
|
187
|
+
* // Use eip1271Signature with a smart contract wallet that implements EIP-1271
|
|
188
|
+
* ```
|
|
153
189
|
*/
|
|
154
|
-
static
|
|
190
|
+
static getEip1271Signature(orderToSign: UnsignedOrder, ecdsaSignature: string): string;
|
|
191
|
+
static encodeUnsignedOrder(orderToSign: UnsignedOrder): Record<string, string>;
|
|
155
192
|
}
|
|
156
193
|
|
|
157
|
-
export { OrderSigningUtils, type SignOrderCancellationParams, type SignOrderCancellationsParams, type SignOrderParams, type SigningResult, type UnsignedOrder };
|
|
194
|
+
export { COW_EIP712_TYPES, ORDER_PRIMARY_TYPE, OrderSigningUtils, type SignOrderCancellationParams, type SignOrderCancellationsParams, type SignOrderParams, type SigningResult, type UnsignedOrder };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SupportedChainId } from '@cowprotocol/sdk-config';
|
|
1
|
+
import { SupportedChainId, CowEnv, AddressPerChain, ProtocolOptions } from '@cowprotocol/sdk-config';
|
|
2
2
|
import { ContractsOrder, OrderUidParams } from '@cowprotocol/sdk-contracts-ts';
|
|
3
3
|
import { SignerLike, Signer, TypedDataDomain } from '@cowprotocol/sdk-common';
|
|
4
4
|
import { OrderParameters, EcdsaSigningScheme } from '@cowprotocol/sdk-order-book';
|
|
@@ -28,6 +28,8 @@ interface SignOrderParams {
|
|
|
28
28
|
signer: SignerLike;
|
|
29
29
|
order: UnsignedOrder;
|
|
30
30
|
signingScheme: EcdsaSigningScheme;
|
|
31
|
+
env?: CowEnv;
|
|
32
|
+
settlementContractOverride?: Partial<AddressPerChain>;
|
|
31
33
|
}
|
|
32
34
|
/**
|
|
33
35
|
* Parameters for signing an order cancellation.
|
|
@@ -41,6 +43,8 @@ interface SignOrderCancellationParams {
|
|
|
41
43
|
signer: SignerLike;
|
|
42
44
|
orderUid: string;
|
|
43
45
|
signingScheme: EcdsaSigningScheme;
|
|
46
|
+
env?: CowEnv;
|
|
47
|
+
settlementContractOverride?: Partial<AddressPerChain>;
|
|
44
48
|
}
|
|
45
49
|
/**
|
|
46
50
|
* Parameters for signing multiple bulk order cancellations.
|
|
@@ -54,8 +58,21 @@ interface SignOrderCancellationsParams {
|
|
|
54
58
|
signer: SignerLike;
|
|
55
59
|
orderUids: string[];
|
|
56
60
|
signingScheme: EcdsaSigningScheme;
|
|
61
|
+
env?: CowEnv;
|
|
62
|
+
settlementContractOverride?: Partial<AddressPerChain>;
|
|
57
63
|
}
|
|
58
64
|
|
|
65
|
+
declare const ORDER_PRIMARY_TYPE: "Order";
|
|
66
|
+
/**
|
|
67
|
+
* The EIP-712 types used for signing a GPv2Order.Data struct. This is useful for when
|
|
68
|
+
* signing orders using smart contracts, whereby this SDK cannot do the EIP-1271 signing for you.
|
|
69
|
+
*/
|
|
70
|
+
declare const COW_EIP712_TYPES: {
|
|
71
|
+
Order: {
|
|
72
|
+
name: string;
|
|
73
|
+
type: string;
|
|
74
|
+
}[];
|
|
75
|
+
};
|
|
59
76
|
/**
|
|
60
77
|
* Utility class for signing order intents and cancellations.
|
|
61
78
|
*
|
|
@@ -106,7 +123,7 @@ declare class OrderSigningUtils {
|
|
|
106
123
|
* @param {Signer} signer The signer who is placing the order intent.
|
|
107
124
|
* @returns {Promise<SigningResult>} Encoded signature including signing scheme for the order.
|
|
108
125
|
*/
|
|
109
|
-
static signOrder(order: UnsignedOrder, chainId: SupportedChainId, signer: Signer): Promise<SigningResult>;
|
|
126
|
+
static signOrder(order: UnsignedOrder, chainId: SupportedChainId, signer: Signer, options?: ProtocolOptions): Promise<SigningResult>;
|
|
110
127
|
/**
|
|
111
128
|
* Sign a cancellation message of an order intent with the specified signer.
|
|
112
129
|
* @param {string} orderUid The unique identifier of the order to cancel.
|
|
@@ -114,7 +131,7 @@ declare class OrderSigningUtils {
|
|
|
114
131
|
* @param {Signer} signer The signer who initially placed the order intent.
|
|
115
132
|
* @returns {Promise<SigningResult>} Encoded signature including signing scheme for the cancellation.
|
|
116
133
|
*/
|
|
117
|
-
static signOrderCancellation(orderUid: string, chainId: SupportedChainId, signer: Signer): Promise<SigningResult>;
|
|
134
|
+
static signOrderCancellation(orderUid: string, chainId: SupportedChainId, signer: Signer, options?: ProtocolOptions): Promise<SigningResult>;
|
|
118
135
|
/**
|
|
119
136
|
* Sign a cancellation message of multiple order intents with the specified signer.
|
|
120
137
|
* @param {string[]} orderUids An array of `orderUid` to cancel.
|
|
@@ -122,21 +139,21 @@ declare class OrderSigningUtils {
|
|
|
122
139
|
* @param {Signer} signer The signer who initially placed the order intents.
|
|
123
140
|
* @returns {Promise<SigningResult>} Encoded signature including signing scheme for the cancellation.
|
|
124
141
|
*/
|
|
125
|
-
static signOrderCancellations(orderUids: string[], chainId: SupportedChainId, signer: Signer): Promise<SigningResult>;
|
|
142
|
+
static signOrderCancellations(orderUids: string[], chainId: SupportedChainId, signer: Signer, options?: ProtocolOptions): Promise<SigningResult>;
|
|
126
143
|
/**
|
|
127
144
|
* Get the EIP-712 typed domain data being used for signing.
|
|
128
145
|
* @param {SupportedChainId} chainId The CoW Protocol `chainId` context that's being used.
|
|
129
146
|
* @return The EIP-712 typed domain data.
|
|
130
147
|
* @see https://eips.ethereum.org/EIPS/eip-712
|
|
131
148
|
*/
|
|
132
|
-
static getDomain(chainId: SupportedChainId): Promise<TypedDataDomain>;
|
|
149
|
+
static getDomain(chainId: SupportedChainId, options?: ProtocolOptions): Promise<TypedDataDomain>;
|
|
133
150
|
/**
|
|
134
151
|
* Hashes the order intent and generate deterministic order ID.
|
|
135
152
|
* @param {SupportedChainId} chainId The CoW Protocol `chainId` context that's being used.
|
|
136
153
|
* @param {Order} order order to sign
|
|
137
154
|
* @param {Pick<OrderUidParams, 'owner'>} params order unique identifier parameters.
|
|
138
155
|
*/
|
|
139
|
-
static generateOrderId(chainId: SupportedChainId, order: ContractsOrder, params: Pick<OrderUidParams, 'owner'
|
|
156
|
+
static generateOrderId(chainId: SupportedChainId, order: ContractsOrder, params: Pick<OrderUidParams, 'owner'>, options?: ProtocolOptions): Promise<{
|
|
140
157
|
orderId: string;
|
|
141
158
|
orderDigest: string;
|
|
142
159
|
}>;
|
|
@@ -145,13 +162,33 @@ declare class OrderSigningUtils {
|
|
|
145
162
|
* @param chainId {SupportedChainId} chainId The CoW Protocol protocol `chainId` context that's being used.
|
|
146
163
|
* @returns A string representation of the EIP-712 typed domain data hash.
|
|
147
164
|
*/
|
|
148
|
-
static getDomainSeparator(chainId: SupportedChainId): Promise<string>;
|
|
165
|
+
static getDomainSeparator(chainId: SupportedChainId, options?: ProtocolOptions): Promise<string>;
|
|
166
|
+
static getEIP712Types(): typeof COW_EIP712_TYPES;
|
|
149
167
|
/**
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
* @
|
|
168
|
+
* Encodes an order and ECDSA signature for EIP-1271 smart contract signature verification.
|
|
169
|
+
*
|
|
170
|
+
* @remarks This method encodes the order data and ECDSA signature into a format suitable for
|
|
171
|
+
* EIP-1271 signature verification by smart contracts. The order struct is ABI-encoded
|
|
172
|
+
* as a tuple along with the ECDSA signature bytes. String fields in the order are
|
|
173
|
+
* hashed using keccak256 before encoding.
|
|
174
|
+
*
|
|
175
|
+
* @param {UnsignedOrder} orderToSign The unsigned order to encode for EIP-1271 verification.
|
|
176
|
+
* @param {string} ecdsaSignature The ECDSA signature (typically 65 bytes hex-encoded) to include in the encoding.
|
|
177
|
+
* @returns {string} The ABI-encoded order struct and signature, ready for EIP-1271 verification.
|
|
178
|
+
*
|
|
179
|
+
* @see https://eips.ethereum.org/EIPS/eip-1271
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* const orderToSign: UnsignedOrder = { ... }
|
|
184
|
+
* const ecdsaSignature = '0x...' // 65 bytes signature from signing the order
|
|
185
|
+
*
|
|
186
|
+
* const eip1271Signature = OrderSigningUtils.getEip1271Signature(orderToSign, ecdsaSignature)
|
|
187
|
+
* // Use eip1271Signature with a smart contract wallet that implements EIP-1271
|
|
188
|
+
* ```
|
|
153
189
|
*/
|
|
154
|
-
static
|
|
190
|
+
static getEip1271Signature(orderToSign: UnsignedOrder, ecdsaSignature: string): string;
|
|
191
|
+
static encodeUnsignedOrder(orderToSign: UnsignedOrder): Record<string, string>;
|
|
155
192
|
}
|
|
156
193
|
|
|
157
|
-
export { OrderSigningUtils, type SignOrderCancellationParams, type SignOrderCancellationsParams, type SignOrderParams, type SigningResult, type UnsignedOrder };
|
|
194
|
+
export { COW_EIP712_TYPES, ORDER_PRIMARY_TYPE, OrderSigningUtils, type SignOrderCancellationParams, type SignOrderCancellationsParams, type SignOrderParams, type SigningResult, type UnsignedOrder };
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
+
COW_EIP712_TYPES: () => COW_EIP712_TYPES,
|
|
24
|
+
ORDER_PRIMARY_TYPE: () => ORDER_PRIMARY_TYPE,
|
|
23
25
|
OrderSigningUtils: () => OrderSigningUtils
|
|
24
26
|
});
|
|
25
27
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -47,18 +49,18 @@ function isProviderRpcError(error) {
|
|
|
47
49
|
return error.code !== void 0 || error.message !== void 0;
|
|
48
50
|
}
|
|
49
51
|
async function _signOrder(params) {
|
|
50
|
-
const { chainId, signer, order, signingScheme } = params;
|
|
51
|
-
const domain = getDomain(chainId);
|
|
52
|
+
const { chainId, signer, order, signingScheme, env, settlementContractOverride } = params;
|
|
53
|
+
const domain = getDomain(chainId, { env, settlementContractOverride });
|
|
52
54
|
return (0, import_sdk_contracts_ts.signOrder)(domain, order, mapSigningSchema[signingScheme], signer);
|
|
53
55
|
}
|
|
54
56
|
async function _signOrderCancellation(params) {
|
|
55
|
-
const { chainId, signer, signingScheme, orderUid } = params;
|
|
56
|
-
const domain = getDomain(chainId);
|
|
57
|
+
const { chainId, signer, signingScheme, orderUid, env, settlementContractOverride } = params;
|
|
58
|
+
const domain = getDomain(chainId, { env, settlementContractOverride });
|
|
57
59
|
return (0, import_sdk_contracts_ts.signOrderCancellation)(domain, orderUid, mapSigningSchema[signingScheme], signer);
|
|
58
60
|
}
|
|
59
61
|
async function _signOrderCancellations(params) {
|
|
60
|
-
const { chainId, signer, signingScheme, orderUids } = params;
|
|
61
|
-
const domain = getDomain(chainId);
|
|
62
|
+
const { chainId, signer, signingScheme, orderUids, env, settlementContractOverride } = params;
|
|
63
|
+
const domain = getDomain(chainId, { env, settlementContractOverride });
|
|
62
64
|
return (0, import_sdk_contracts_ts.signOrderCancellations)(domain, orderUids, mapSigningSchema[signingScheme], signer);
|
|
63
65
|
}
|
|
64
66
|
async function _signPayload(payload, signFn, signer, signingMethod = "v4") {
|
|
@@ -121,24 +123,28 @@ async function _signPayload(payload, signFn, signer, signingMethod = "v4") {
|
|
|
121
123
|
const data = signature?.data;
|
|
122
124
|
return { signature: data?.toString() || "", signingScheme };
|
|
123
125
|
}
|
|
124
|
-
async function signOrder(order, chainId, signer) {
|
|
125
|
-
|
|
126
|
+
async function signOrder(order, chainId, signer, options) {
|
|
127
|
+
const { env, settlementContractOverride } = options ?? {};
|
|
128
|
+
return _signPayload({ order, chainId, env, settlementContractOverride }, _signOrder, signer);
|
|
126
129
|
}
|
|
127
|
-
async function signOrderCancellation(orderUid, chainId, signer) {
|
|
128
|
-
|
|
130
|
+
async function signOrderCancellation(orderUid, chainId, signer, options) {
|
|
131
|
+
const { env, settlementContractOverride } = options ?? {};
|
|
132
|
+
return _signPayload({ orderUid, chainId, env, settlementContractOverride }, _signOrderCancellation, signer);
|
|
129
133
|
}
|
|
130
|
-
async function signOrderCancellations(orderUids, chainId, signer) {
|
|
131
|
-
|
|
134
|
+
async function signOrderCancellations(orderUids, chainId, signer, options) {
|
|
135
|
+
const { env, settlementContractOverride } = options ?? {};
|
|
136
|
+
return _signPayload({ orderUids, chainId, env, settlementContractOverride }, _signOrderCancellations, signer);
|
|
132
137
|
}
|
|
133
|
-
function getDomain(chainId) {
|
|
134
|
-
const
|
|
138
|
+
function getDomain(chainId, options) {
|
|
139
|
+
const { env, settlementContractOverride } = options ?? {};
|
|
140
|
+
const settlementContract = settlementContractOverride?.[chainId] ?? (env === "staging" ? import_sdk_config.COW_PROTOCOL_SETTLEMENT_CONTRACT_ADDRESS_STAGING[chainId] : import_sdk_config.COW_PROTOCOL_SETTLEMENT_CONTRACT_ADDRESS[chainId]);
|
|
135
141
|
if (!settlementContract) {
|
|
136
142
|
throw new import_sdk_common.CowError("Unsupported network. Settlement contract is not deployed");
|
|
137
143
|
}
|
|
138
144
|
return import_sdk_contracts_ts.ContractsTs.domain(chainId, settlementContract);
|
|
139
145
|
}
|
|
140
|
-
async function generateOrderId(chainId, order, params) {
|
|
141
|
-
const domain = await getDomain(chainId);
|
|
146
|
+
async function generateOrderId(chainId, order, params, options) {
|
|
147
|
+
const domain = await getDomain(chainId, options);
|
|
142
148
|
const orderDigest = (0, import_sdk_contracts_ts.hashOrder)(domain, order);
|
|
143
149
|
const orderId = (0, import_sdk_contracts_ts.packOrderUidParams)({
|
|
144
150
|
...params,
|
|
@@ -150,7 +156,25 @@ async function generateOrderId(chainId, order, params) {
|
|
|
150
156
|
}
|
|
151
157
|
|
|
152
158
|
// src/orderSigningUtils.ts
|
|
153
|
-
var
|
|
159
|
+
var import_sdk_order_book2 = require("@cowprotocol/sdk-order-book");
|
|
160
|
+
var ORDER_PRIMARY_TYPE = "Order";
|
|
161
|
+
var COW_EIP712_TYPES = {
|
|
162
|
+
[ORDER_PRIMARY_TYPE]: [
|
|
163
|
+
{ name: "sellToken", type: "address" },
|
|
164
|
+
{ name: "buyToken", type: "address" },
|
|
165
|
+
{ name: "receiver", type: "address" },
|
|
166
|
+
{ name: "sellAmount", type: "uint256" },
|
|
167
|
+
{ name: "buyAmount", type: "uint256" },
|
|
168
|
+
{ name: "validTo", type: "uint32" },
|
|
169
|
+
{ name: "appData", type: "bytes32" },
|
|
170
|
+
{ name: "feeAmount", type: "uint256" },
|
|
171
|
+
{ name: "kind", type: "string" },
|
|
172
|
+
{ name: "partiallyFillable", type: "bool" },
|
|
173
|
+
{ name: "sellTokenBalance", type: "string" },
|
|
174
|
+
{ name: "buyTokenBalance", type: "string" }
|
|
175
|
+
]
|
|
176
|
+
};
|
|
177
|
+
var OrderSigningUtils = class _OrderSigningUtils {
|
|
154
178
|
/**
|
|
155
179
|
* Sign the order intent with the specified signer.
|
|
156
180
|
*
|
|
@@ -162,8 +186,8 @@ var OrderSigningUtils = class {
|
|
|
162
186
|
* @param {Signer} signer The signer who is placing the order intent.
|
|
163
187
|
* @returns {Promise<SigningResult>} Encoded signature including signing scheme for the order.
|
|
164
188
|
*/
|
|
165
|
-
static async signOrder(order, chainId, signer) {
|
|
166
|
-
return signOrder(order, chainId, signer);
|
|
189
|
+
static async signOrder(order, chainId, signer, options) {
|
|
190
|
+
return signOrder(order, chainId, signer, options);
|
|
167
191
|
}
|
|
168
192
|
/**
|
|
169
193
|
* Sign a cancellation message of an order intent with the specified signer.
|
|
@@ -172,8 +196,8 @@ var OrderSigningUtils = class {
|
|
|
172
196
|
* @param {Signer} signer The signer who initially placed the order intent.
|
|
173
197
|
* @returns {Promise<SigningResult>} Encoded signature including signing scheme for the cancellation.
|
|
174
198
|
*/
|
|
175
|
-
static async signOrderCancellation(orderUid, chainId, signer) {
|
|
176
|
-
return signOrderCancellation(orderUid, chainId, signer);
|
|
199
|
+
static async signOrderCancellation(orderUid, chainId, signer, options) {
|
|
200
|
+
return signOrderCancellation(orderUid, chainId, signer, options);
|
|
177
201
|
}
|
|
178
202
|
/**
|
|
179
203
|
* Sign a cancellation message of multiple order intents with the specified signer.
|
|
@@ -182,8 +206,8 @@ var OrderSigningUtils = class {
|
|
|
182
206
|
* @param {Signer} signer The signer who initially placed the order intents.
|
|
183
207
|
* @returns {Promise<SigningResult>} Encoded signature including signing scheme for the cancellation.
|
|
184
208
|
*/
|
|
185
|
-
static async signOrderCancellations(orderUids, chainId, signer) {
|
|
186
|
-
return signOrderCancellations(orderUids, chainId, signer);
|
|
209
|
+
static async signOrderCancellations(orderUids, chainId, signer, options) {
|
|
210
|
+
return signOrderCancellations(orderUids, chainId, signer, options);
|
|
187
211
|
}
|
|
188
212
|
/**
|
|
189
213
|
* Get the EIP-712 typed domain data being used for signing.
|
|
@@ -191,8 +215,8 @@ var OrderSigningUtils = class {
|
|
|
191
215
|
* @return The EIP-712 typed domain data.
|
|
192
216
|
* @see https://eips.ethereum.org/EIPS/eip-712
|
|
193
217
|
*/
|
|
194
|
-
static async getDomain(chainId) {
|
|
195
|
-
return getDomain(chainId);
|
|
218
|
+
static async getDomain(chainId, options) {
|
|
219
|
+
return getDomain(chainId, options);
|
|
196
220
|
}
|
|
197
221
|
/**
|
|
198
222
|
* Hashes the order intent and generate deterministic order ID.
|
|
@@ -200,15 +224,15 @@ var OrderSigningUtils = class {
|
|
|
200
224
|
* @param {Order} order order to sign
|
|
201
225
|
* @param {Pick<OrderUidParams, 'owner'>} params order unique identifier parameters.
|
|
202
226
|
*/
|
|
203
|
-
static async generateOrderId(chainId, order, params) {
|
|
204
|
-
return generateOrderId(chainId, order, params);
|
|
227
|
+
static async generateOrderId(chainId, order, params, options) {
|
|
228
|
+
return generateOrderId(chainId, order, params, options);
|
|
205
229
|
}
|
|
206
230
|
/**
|
|
207
231
|
* Get the domain separator hash for the EIP-712 typed domain data being used for signing.
|
|
208
232
|
* @param chainId {SupportedChainId} chainId The CoW Protocol protocol `chainId` context that's being used.
|
|
209
233
|
* @returns A string representation of the EIP-712 typed domain data hash.
|
|
210
234
|
*/
|
|
211
|
-
static async getDomainSeparator(chainId) {
|
|
235
|
+
static async getDomainSeparator(chainId, options) {
|
|
212
236
|
const adapter = (0, import_sdk_common2.getGlobalAdapter)();
|
|
213
237
|
const types = [
|
|
214
238
|
{ name: "name", type: "string" },
|
|
@@ -216,33 +240,71 @@ var OrderSigningUtils = class {
|
|
|
216
240
|
{ name: "chainId", type: "uint256" },
|
|
217
241
|
{ name: "verifyingContract", type: "address" }
|
|
218
242
|
];
|
|
219
|
-
return adapter.utils.hashDomain(getDomain(chainId), types);
|
|
243
|
+
return adapter.utils.hashDomain(getDomain(chainId, options), types);
|
|
244
|
+
}
|
|
245
|
+
static getEIP712Types() {
|
|
246
|
+
return COW_EIP712_TYPES;
|
|
220
247
|
}
|
|
221
248
|
/**
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* @
|
|
249
|
+
* Encodes an order and ECDSA signature for EIP-1271 smart contract signature verification.
|
|
250
|
+
*
|
|
251
|
+
* @remarks This method encodes the order data and ECDSA signature into a format suitable for
|
|
252
|
+
* EIP-1271 signature verification by smart contracts. The order struct is ABI-encoded
|
|
253
|
+
* as a tuple along with the ECDSA signature bytes. String fields in the order are
|
|
254
|
+
* hashed using keccak256 before encoding.
|
|
255
|
+
*
|
|
256
|
+
* @param {UnsignedOrder} orderToSign The unsigned order to encode for EIP-1271 verification.
|
|
257
|
+
* @param {string} ecdsaSignature The ECDSA signature (typically 65 bytes hex-encoded) to include in the encoding.
|
|
258
|
+
* @returns {string} The ABI-encoded order struct and signature, ready for EIP-1271 verification.
|
|
259
|
+
*
|
|
260
|
+
* @see https://eips.ethereum.org/EIPS/eip-1271
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* ```typescript
|
|
264
|
+
* const orderToSign: UnsignedOrder = { ... }
|
|
265
|
+
* const ecdsaSignature = '0x...' // 65 bytes signature from signing the order
|
|
266
|
+
*
|
|
267
|
+
* const eip1271Signature = OrderSigningUtils.getEip1271Signature(orderToSign, ecdsaSignature)
|
|
268
|
+
* // Use eip1271Signature with a smart contract wallet that implements EIP-1271
|
|
269
|
+
* ```
|
|
225
270
|
*/
|
|
226
|
-
static
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
{
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
{
|
|
241
|
-
]
|
|
271
|
+
static getEip1271Signature(orderToSign, ecdsaSignature) {
|
|
272
|
+
const adapter = (0, import_sdk_common2.getGlobalAdapter)();
|
|
273
|
+
const EIP712Types = COW_EIP712_TYPES[ORDER_PRIMARY_TYPE];
|
|
274
|
+
const components = EIP712Types.map((component) => ({
|
|
275
|
+
name: component.name,
|
|
276
|
+
type: component.type === "string" ? "bytes32" : component.type
|
|
277
|
+
}));
|
|
278
|
+
const values = Object.values(_OrderSigningUtils.encodeUnsignedOrder(orderToSign));
|
|
279
|
+
return adapter.utils.encodeAbi(
|
|
280
|
+
[
|
|
281
|
+
{
|
|
282
|
+
components,
|
|
283
|
+
type: "tuple"
|
|
284
|
+
},
|
|
285
|
+
{ type: "bytes" }
|
|
286
|
+
],
|
|
287
|
+
[values, ecdsaSignature]
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
static encodeUnsignedOrder(orderToSign) {
|
|
291
|
+
const order = {
|
|
292
|
+
...orderToSign,
|
|
293
|
+
sellTokenBalance: orderToSign.sellTokenBalance ?? import_sdk_order_book2.SellTokenSource.ERC20,
|
|
294
|
+
buyTokenBalance: orderToSign.buyTokenBalance ?? import_sdk_order_book2.BuyTokenDestination.ERC20
|
|
242
295
|
};
|
|
296
|
+
const adapter = (0, import_sdk_common2.getGlobalAdapter)();
|
|
297
|
+
const EIP712Types = COW_EIP712_TYPES[ORDER_PRIMARY_TYPE];
|
|
298
|
+
return EIP712Types.reduce((acc, { name, type }) => {
|
|
299
|
+
const value = order[name];
|
|
300
|
+
acc[name] = type === "string" ? String(adapter.utils.id(value)) : value;
|
|
301
|
+
return acc;
|
|
302
|
+
}, {});
|
|
243
303
|
}
|
|
244
304
|
};
|
|
245
305
|
// Annotate the CommonJS export names for ESM import in node:
|
|
246
306
|
0 && (module.exports = {
|
|
307
|
+
COW_EIP712_TYPES,
|
|
308
|
+
ORDER_PRIMARY_TYPE,
|
|
247
309
|
OrderSigningUtils
|
|
248
310
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -15,7 +15,10 @@ import {
|
|
|
15
15
|
} from "@cowprotocol/sdk-contracts-ts";
|
|
16
16
|
import { CowError } from "@cowprotocol/sdk-common";
|
|
17
17
|
import { EcdsaSigningScheme } from "@cowprotocol/sdk-order-book";
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
COW_PROTOCOL_SETTLEMENT_CONTRACT_ADDRESS,
|
|
20
|
+
COW_PROTOCOL_SETTLEMENT_CONTRACT_ADDRESS_STAGING
|
|
21
|
+
} from "@cowprotocol/sdk-config";
|
|
19
22
|
var METAMASK_SIGNATURE_ERROR_CODE = -32603;
|
|
20
23
|
var METHOD_NOT_FOUND_ERROR_CODE = -32601;
|
|
21
24
|
var METHOD_NOT_FOUND_ERROR_MSG_REGEX = /Method not found/i;
|
|
@@ -31,18 +34,18 @@ function isProviderRpcError(error) {
|
|
|
31
34
|
return error.code !== void 0 || error.message !== void 0;
|
|
32
35
|
}
|
|
33
36
|
async function _signOrder(params) {
|
|
34
|
-
const { chainId, signer, order, signingScheme } = params;
|
|
35
|
-
const domain = getDomain(chainId);
|
|
37
|
+
const { chainId, signer, order, signingScheme, env, settlementContractOverride } = params;
|
|
38
|
+
const domain = getDomain(chainId, { env, settlementContractOverride });
|
|
36
39
|
return signOrderGp(domain, order, mapSigningSchema[signingScheme], signer);
|
|
37
40
|
}
|
|
38
41
|
async function _signOrderCancellation(params) {
|
|
39
|
-
const { chainId, signer, signingScheme, orderUid } = params;
|
|
40
|
-
const domain = getDomain(chainId);
|
|
42
|
+
const { chainId, signer, signingScheme, orderUid, env, settlementContractOverride } = params;
|
|
43
|
+
const domain = getDomain(chainId, { env, settlementContractOverride });
|
|
41
44
|
return signOrderCancellationGp(domain, orderUid, mapSigningSchema[signingScheme], signer);
|
|
42
45
|
}
|
|
43
46
|
async function _signOrderCancellations(params) {
|
|
44
|
-
const { chainId, signer, signingScheme, orderUids } = params;
|
|
45
|
-
const domain = getDomain(chainId);
|
|
47
|
+
const { chainId, signer, signingScheme, orderUids, env, settlementContractOverride } = params;
|
|
48
|
+
const domain = getDomain(chainId, { env, settlementContractOverride });
|
|
46
49
|
return signOrderCancellationsGp(domain, orderUids, mapSigningSchema[signingScheme], signer);
|
|
47
50
|
}
|
|
48
51
|
async function _signPayload(payload, signFn, signer, signingMethod = "v4") {
|
|
@@ -105,24 +108,28 @@ async function _signPayload(payload, signFn, signer, signingMethod = "v4") {
|
|
|
105
108
|
const data = signature?.data;
|
|
106
109
|
return { signature: data?.toString() || "", signingScheme };
|
|
107
110
|
}
|
|
108
|
-
async function signOrder(order, chainId, signer) {
|
|
109
|
-
|
|
111
|
+
async function signOrder(order, chainId, signer, options) {
|
|
112
|
+
const { env, settlementContractOverride } = options ?? {};
|
|
113
|
+
return _signPayload({ order, chainId, env, settlementContractOverride }, _signOrder, signer);
|
|
110
114
|
}
|
|
111
|
-
async function signOrderCancellation(orderUid, chainId, signer) {
|
|
112
|
-
|
|
115
|
+
async function signOrderCancellation(orderUid, chainId, signer, options) {
|
|
116
|
+
const { env, settlementContractOverride } = options ?? {};
|
|
117
|
+
return _signPayload({ orderUid, chainId, env, settlementContractOverride }, _signOrderCancellation, signer);
|
|
113
118
|
}
|
|
114
|
-
async function signOrderCancellations(orderUids, chainId, signer) {
|
|
115
|
-
|
|
119
|
+
async function signOrderCancellations(orderUids, chainId, signer, options) {
|
|
120
|
+
const { env, settlementContractOverride } = options ?? {};
|
|
121
|
+
return _signPayload({ orderUids, chainId, env, settlementContractOverride }, _signOrderCancellations, signer);
|
|
116
122
|
}
|
|
117
|
-
function getDomain(chainId) {
|
|
118
|
-
const
|
|
123
|
+
function getDomain(chainId, options) {
|
|
124
|
+
const { env, settlementContractOverride } = options ?? {};
|
|
125
|
+
const settlementContract = settlementContractOverride?.[chainId] ?? (env === "staging" ? COW_PROTOCOL_SETTLEMENT_CONTRACT_ADDRESS_STAGING[chainId] : COW_PROTOCOL_SETTLEMENT_CONTRACT_ADDRESS[chainId]);
|
|
119
126
|
if (!settlementContract) {
|
|
120
127
|
throw new CowError("Unsupported network. Settlement contract is not deployed");
|
|
121
128
|
}
|
|
122
129
|
return ContractsTs.domain(chainId, settlementContract);
|
|
123
130
|
}
|
|
124
|
-
async function generateOrderId(chainId, order, params) {
|
|
125
|
-
const domain = await getDomain(chainId);
|
|
131
|
+
async function generateOrderId(chainId, order, params, options) {
|
|
132
|
+
const domain = await getDomain(chainId, options);
|
|
126
133
|
const orderDigest = hashOrder(domain, order);
|
|
127
134
|
const orderId = packOrderUidParams({
|
|
128
135
|
...params,
|
|
@@ -134,7 +141,25 @@ async function generateOrderId(chainId, order, params) {
|
|
|
134
141
|
}
|
|
135
142
|
|
|
136
143
|
// src/orderSigningUtils.ts
|
|
137
|
-
|
|
144
|
+
import { BuyTokenDestination, SellTokenSource } from "@cowprotocol/sdk-order-book";
|
|
145
|
+
var ORDER_PRIMARY_TYPE = "Order";
|
|
146
|
+
var COW_EIP712_TYPES = {
|
|
147
|
+
[ORDER_PRIMARY_TYPE]: [
|
|
148
|
+
{ name: "sellToken", type: "address" },
|
|
149
|
+
{ name: "buyToken", type: "address" },
|
|
150
|
+
{ name: "receiver", type: "address" },
|
|
151
|
+
{ name: "sellAmount", type: "uint256" },
|
|
152
|
+
{ name: "buyAmount", type: "uint256" },
|
|
153
|
+
{ name: "validTo", type: "uint32" },
|
|
154
|
+
{ name: "appData", type: "bytes32" },
|
|
155
|
+
{ name: "feeAmount", type: "uint256" },
|
|
156
|
+
{ name: "kind", type: "string" },
|
|
157
|
+
{ name: "partiallyFillable", type: "bool" },
|
|
158
|
+
{ name: "sellTokenBalance", type: "string" },
|
|
159
|
+
{ name: "buyTokenBalance", type: "string" }
|
|
160
|
+
]
|
|
161
|
+
};
|
|
162
|
+
var OrderSigningUtils = class _OrderSigningUtils {
|
|
138
163
|
/**
|
|
139
164
|
* Sign the order intent with the specified signer.
|
|
140
165
|
*
|
|
@@ -146,8 +171,8 @@ var OrderSigningUtils = class {
|
|
|
146
171
|
* @param {Signer} signer The signer who is placing the order intent.
|
|
147
172
|
* @returns {Promise<SigningResult>} Encoded signature including signing scheme for the order.
|
|
148
173
|
*/
|
|
149
|
-
static async signOrder(order, chainId, signer) {
|
|
150
|
-
return signOrder(order, chainId, signer);
|
|
174
|
+
static async signOrder(order, chainId, signer, options) {
|
|
175
|
+
return signOrder(order, chainId, signer, options);
|
|
151
176
|
}
|
|
152
177
|
/**
|
|
153
178
|
* Sign a cancellation message of an order intent with the specified signer.
|
|
@@ -156,8 +181,8 @@ var OrderSigningUtils = class {
|
|
|
156
181
|
* @param {Signer} signer The signer who initially placed the order intent.
|
|
157
182
|
* @returns {Promise<SigningResult>} Encoded signature including signing scheme for the cancellation.
|
|
158
183
|
*/
|
|
159
|
-
static async signOrderCancellation(orderUid, chainId, signer) {
|
|
160
|
-
return signOrderCancellation(orderUid, chainId, signer);
|
|
184
|
+
static async signOrderCancellation(orderUid, chainId, signer, options) {
|
|
185
|
+
return signOrderCancellation(orderUid, chainId, signer, options);
|
|
161
186
|
}
|
|
162
187
|
/**
|
|
163
188
|
* Sign a cancellation message of multiple order intents with the specified signer.
|
|
@@ -166,8 +191,8 @@ var OrderSigningUtils = class {
|
|
|
166
191
|
* @param {Signer} signer The signer who initially placed the order intents.
|
|
167
192
|
* @returns {Promise<SigningResult>} Encoded signature including signing scheme for the cancellation.
|
|
168
193
|
*/
|
|
169
|
-
static async signOrderCancellations(orderUids, chainId, signer) {
|
|
170
|
-
return signOrderCancellations(orderUids, chainId, signer);
|
|
194
|
+
static async signOrderCancellations(orderUids, chainId, signer, options) {
|
|
195
|
+
return signOrderCancellations(orderUids, chainId, signer, options);
|
|
171
196
|
}
|
|
172
197
|
/**
|
|
173
198
|
* Get the EIP-712 typed domain data being used for signing.
|
|
@@ -175,8 +200,8 @@ var OrderSigningUtils = class {
|
|
|
175
200
|
* @return The EIP-712 typed domain data.
|
|
176
201
|
* @see https://eips.ethereum.org/EIPS/eip-712
|
|
177
202
|
*/
|
|
178
|
-
static async getDomain(chainId) {
|
|
179
|
-
return getDomain(chainId);
|
|
203
|
+
static async getDomain(chainId, options) {
|
|
204
|
+
return getDomain(chainId, options);
|
|
180
205
|
}
|
|
181
206
|
/**
|
|
182
207
|
* Hashes the order intent and generate deterministic order ID.
|
|
@@ -184,15 +209,15 @@ var OrderSigningUtils = class {
|
|
|
184
209
|
* @param {Order} order order to sign
|
|
185
210
|
* @param {Pick<OrderUidParams, 'owner'>} params order unique identifier parameters.
|
|
186
211
|
*/
|
|
187
|
-
static async generateOrderId(chainId, order, params) {
|
|
188
|
-
return generateOrderId(chainId, order, params);
|
|
212
|
+
static async generateOrderId(chainId, order, params, options) {
|
|
213
|
+
return generateOrderId(chainId, order, params, options);
|
|
189
214
|
}
|
|
190
215
|
/**
|
|
191
216
|
* Get the domain separator hash for the EIP-712 typed domain data being used for signing.
|
|
192
217
|
* @param chainId {SupportedChainId} chainId The CoW Protocol protocol `chainId` context that's being used.
|
|
193
218
|
* @returns A string representation of the EIP-712 typed domain data hash.
|
|
194
219
|
*/
|
|
195
|
-
static async getDomainSeparator(chainId) {
|
|
220
|
+
static async getDomainSeparator(chainId, options) {
|
|
196
221
|
const adapter = getGlobalAdapter();
|
|
197
222
|
const types = [
|
|
198
223
|
{ name: "name", type: "string" },
|
|
@@ -200,32 +225,70 @@ var OrderSigningUtils = class {
|
|
|
200
225
|
{ name: "chainId", type: "uint256" },
|
|
201
226
|
{ name: "verifyingContract", type: "address" }
|
|
202
227
|
];
|
|
203
|
-
return adapter.utils.hashDomain(getDomain(chainId), types);
|
|
228
|
+
return adapter.utils.hashDomain(getDomain(chainId, options), types);
|
|
229
|
+
}
|
|
230
|
+
static getEIP712Types() {
|
|
231
|
+
return COW_EIP712_TYPES;
|
|
204
232
|
}
|
|
205
233
|
/**
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
* @
|
|
234
|
+
* Encodes an order and ECDSA signature for EIP-1271 smart contract signature verification.
|
|
235
|
+
*
|
|
236
|
+
* @remarks This method encodes the order data and ECDSA signature into a format suitable for
|
|
237
|
+
* EIP-1271 signature verification by smart contracts. The order struct is ABI-encoded
|
|
238
|
+
* as a tuple along with the ECDSA signature bytes. String fields in the order are
|
|
239
|
+
* hashed using keccak256 before encoding.
|
|
240
|
+
*
|
|
241
|
+
* @param {UnsignedOrder} orderToSign The unsigned order to encode for EIP-1271 verification.
|
|
242
|
+
* @param {string} ecdsaSignature The ECDSA signature (typically 65 bytes hex-encoded) to include in the encoding.
|
|
243
|
+
* @returns {string} The ABI-encoded order struct and signature, ready for EIP-1271 verification.
|
|
244
|
+
*
|
|
245
|
+
* @see https://eips.ethereum.org/EIPS/eip-1271
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* const orderToSign: UnsignedOrder = { ... }
|
|
250
|
+
* const ecdsaSignature = '0x...' // 65 bytes signature from signing the order
|
|
251
|
+
*
|
|
252
|
+
* const eip1271Signature = OrderSigningUtils.getEip1271Signature(orderToSign, ecdsaSignature)
|
|
253
|
+
* // Use eip1271Signature with a smart contract wallet that implements EIP-1271
|
|
254
|
+
* ```
|
|
209
255
|
*/
|
|
210
|
-
static
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
{
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
{
|
|
225
|
-
]
|
|
256
|
+
static getEip1271Signature(orderToSign, ecdsaSignature) {
|
|
257
|
+
const adapter = getGlobalAdapter();
|
|
258
|
+
const EIP712Types = COW_EIP712_TYPES[ORDER_PRIMARY_TYPE];
|
|
259
|
+
const components = EIP712Types.map((component) => ({
|
|
260
|
+
name: component.name,
|
|
261
|
+
type: component.type === "string" ? "bytes32" : component.type
|
|
262
|
+
}));
|
|
263
|
+
const values = Object.values(_OrderSigningUtils.encodeUnsignedOrder(orderToSign));
|
|
264
|
+
return adapter.utils.encodeAbi(
|
|
265
|
+
[
|
|
266
|
+
{
|
|
267
|
+
components,
|
|
268
|
+
type: "tuple"
|
|
269
|
+
},
|
|
270
|
+
{ type: "bytes" }
|
|
271
|
+
],
|
|
272
|
+
[values, ecdsaSignature]
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
static encodeUnsignedOrder(orderToSign) {
|
|
276
|
+
const order = {
|
|
277
|
+
...orderToSign,
|
|
278
|
+
sellTokenBalance: orderToSign.sellTokenBalance ?? SellTokenSource.ERC20,
|
|
279
|
+
buyTokenBalance: orderToSign.buyTokenBalance ?? BuyTokenDestination.ERC20
|
|
226
280
|
};
|
|
281
|
+
const adapter = getGlobalAdapter();
|
|
282
|
+
const EIP712Types = COW_EIP712_TYPES[ORDER_PRIMARY_TYPE];
|
|
283
|
+
return EIP712Types.reduce((acc, { name, type }) => {
|
|
284
|
+
const value = order[name];
|
|
285
|
+
acc[name] = type === "string" ? String(adapter.utils.id(value)) : value;
|
|
286
|
+
return acc;
|
|
287
|
+
}, {});
|
|
227
288
|
}
|
|
228
289
|
};
|
|
229
290
|
export {
|
|
291
|
+
COW_EIP712_TYPES,
|
|
292
|
+
ORDER_PRIMARY_TYPE,
|
|
230
293
|
OrderSigningUtils
|
|
231
294
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cowprotocol/sdk-order-signing",
|
|
3
|
-
"version": "0.2.2
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "TypeScript order signing for CoW Protocol SDK",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/cowprotocol/cow-sdk.git",
|
|
8
|
+
"directory": "packages/order-signing"
|
|
9
|
+
},
|
|
5
10
|
"main": "./dist/index.js",
|
|
6
11
|
"module": "./dist/index.mjs",
|
|
7
12
|
"types": "./dist/index.d.ts",
|
|
@@ -14,10 +19,10 @@
|
|
|
14
19
|
"access": "public"
|
|
15
20
|
},
|
|
16
21
|
"dependencies": {
|
|
17
|
-
"@cowprotocol/sdk-common": "0.
|
|
18
|
-
"@cowprotocol/sdk-config": "
|
|
19
|
-
"@cowprotocol/sdk-contracts-ts": "2.1.
|
|
20
|
-
"@cowprotocol/sdk-order-book": "
|
|
22
|
+
"@cowprotocol/sdk-common": "0.8.2",
|
|
23
|
+
"@cowprotocol/sdk-config": "1.1.2",
|
|
24
|
+
"@cowprotocol/sdk-contracts-ts": "2.1.2",
|
|
25
|
+
"@cowprotocol/sdk-order-book": "2.0.3"
|
|
21
26
|
},
|
|
22
27
|
"devDependencies": {
|
|
23
28
|
"@types/jest": "^29.5.5",
|
|
@@ -31,10 +36,10 @@
|
|
|
31
36
|
"tsup": "^7.2.0",
|
|
32
37
|
"typescript": "^5.2.2",
|
|
33
38
|
"viem": "^2.28.4",
|
|
34
|
-
"@cowprotocol/sdk-ethers-
|
|
35
|
-
"@
|
|
36
|
-
"@cowprotocol/sdk-
|
|
37
|
-
"@
|
|
39
|
+
"@cowprotocol/sdk-ethers-v5-adapter": "0.3.14",
|
|
40
|
+
"@cowprotocol/sdk-viem-adapter": "0.3.14",
|
|
41
|
+
"@cowprotocol/sdk-ethers-v6-adapter": "0.3.14",
|
|
42
|
+
"@cow-sdk/typescript-config": "0.0.0-beta.0"
|
|
38
43
|
},
|
|
39
44
|
"scripts": {
|
|
40
45
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|