Skip to content

Commit e824ed4

Browse files
committed
refactor: Clean scripts
1 parent 2f9d130 commit e824ed4

File tree

2 files changed

+10
-82
lines changed

2 files changed

+10
-82
lines changed

utils/createOrders.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ export async function signOrder(
275275
domain: TypedDataDomain,
276276
order: Record<string, any>,
277277
signer: SignerWithAddress,
278-
) {
279-
return signStruct(getTypeOf(order), order, domain, signer);
278+
): Promise<void> {
279+
order.sign = await signStruct(getTypeOf(order), order, domain, signer);
280280
}
281281

282282
/**
@@ -287,8 +287,8 @@ export async function signOrderOperation(
287287
domain: TypedDataDomain,
288288
orderOperation: OrderOperation,
289289
signer: SignerWithAddress,
290-
) {
291-
return signStruct(
290+
): Promise<void> {
291+
orderOperation.sign = await signStruct(
292292
getTypeOf(orderOperation.order) + 'Operation',
293293
orderOperation,
294294
domain,

utils/odb-tools.ts

Lines changed: 6 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
// SPDX-FileCopyrightText: 2020-2025 IEXEC BLOCKCHAIN TECH <[email protected]>
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { TypedDataDomain, TypedDataEncoder, TypedDataField, ethers } from 'ethers';
5-
import hre from 'hardhat';
6-
import { IexecInterfaceToken__factory, IexecLibOrders_v5 } from '../typechain';
7-
8-
interface WalletInfo {
9-
privateKey?: string;
10-
address?: string;
11-
}
4+
import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers';
5+
import { TypedDataDomain, TypedDataEncoder, TypedDataField } from 'ethers';
126

137
interface Types {
148
[key: string]: Array<TypedDataField>;
@@ -103,52 +97,14 @@ function buildTypes(primaryType: string): Types {
10397
return types;
10498
}
10599

106-
async function eth_signTypedData(
107-
primaryType: string,
108-
message: Record<string, any>,
109-
domain: TypedDataDomain,
110-
wallet: WalletInfo,
111-
): Promise<string> {
112-
return new Promise((resolve, reject) => {
113-
const typedDataDomain = {
114-
name: domain.name,
115-
version: domain.version,
116-
chainId: domain.chainId,
117-
verifyingContract: domain.verifyingContract,
118-
};
119-
const types = buildTypes(primaryType);
120-
121-
let signerPromise;
122-
123-
if (wallet.privateKey) {
124-
const walletInstance = new ethers.Wallet(wallet.privateKey, hre.ethers.provider);
125-
signerPromise = Promise.resolve(walletInstance);
126-
} else {
127-
if (wallet.address) {
128-
signerPromise = hre.ethers.getSigner(wallet.address);
129-
} else {
130-
reject(new Error('Wallet address is undefined'));
131-
return;
132-
}
133-
}
134-
135-
signerPromise
136-
.then((signer) => signer.signTypedData(typedDataDomain, types, message))
137-
.then(resolve)
138-
.catch(reject);
139-
});
140-
}
141-
142100
export async function signStruct(
143101
primaryType: string,
144102
message: Record<string, any>,
145103
domain: TypedDataDomain,
146-
wallet: WalletInfo,
147-
): Promise<Record<string, any>> {
148-
return eth_signTypedData(primaryType, message, domain, wallet).then((sign) => {
149-
message.sign = sign;
150-
return message;
151-
});
104+
signer: SignerWithAddress,
105+
): Promise<string> {
106+
const types = buildTypes(primaryType);
107+
return await signer.signTypedData(domain, types, message);
152108
}
153109

154110
export function hashStruct(
@@ -168,31 +124,3 @@ export function hashStruct(
168124

169125
return TypedDataEncoder.hash(typedDataDomain, types, message);
170126
}
171-
172-
/**
173-
* Encode orders with matchOrders selector for receiveApproval callback.
174-
*
175-
* The encoded data includes the function selector as the first 4 bytes, which allows
176-
* the generalized receiveApproval implementation to:
177-
* 1. Extract the selector to identify the operation (matchOrders in this case)
178-
* 2. Call the appropriate validator (_validateMatchOrders for permission checks)
179-
*
180-
* @param appOrder App order struct
181-
* @param datasetOrder Dataset order struct
182-
* @param workerpoolOrder Workerpool order struct
183-
* @param requestOrder Request order struct
184-
* @returns ABI-encoded calldata with matchOrders selector + encoded order structs
185-
*/
186-
export function encodeMatchOrdersCalldata(
187-
appOrder: IexecLibOrders_v5.AppOrderStruct,
188-
datasetOrder: IexecLibOrders_v5.DatasetOrderStruct,
189-
workerpoolOrder: IexecLibOrders_v5.WorkerpoolOrderStruct,
190-
requestOrder: IexecLibOrders_v5.RequestOrderStruct,
191-
): string {
192-
return IexecInterfaceToken__factory.createInterface().encodeFunctionData('matchOrders', [
193-
appOrder,
194-
datasetOrder,
195-
workerpoolOrder,
196-
requestOrder,
197-
]);
198-
}

0 commit comments

Comments
 (0)