@@ -4,11 +4,17 @@ import { randomBytes } from "crypto";
4
4
import { FastifyInstance } from "fastify" ;
5
5
import { StatusCodes } from "http-status-codes" ;
6
6
import { Hex , ZERO_ADDRESS , getContract } from "thirdweb" ;
7
+ import {
8
+ primarySaleRecipient as getDefaultPrimarySaleRecipient ,
9
+ getDefaultRoyaltyInfo ,
10
+ } from "thirdweb/extensions/common" ;
7
11
import { GenerateMintSignatureOptions } from "thirdweb/extensions/erc721" ;
8
12
import { upload } from "thirdweb/storage" ;
9
13
import { getChain } from "../../../../../../utils/chain" ;
14
+ import { logger } from "../../../../../../utils/logger" ;
10
15
import { maybeBigInt } from "../../../../../../utils/primitiveTypes" ;
11
16
import { thirdwebClient } from "../../../../../../utils/sdk" ;
17
+ import { createCustomError } from "../../../../../middleware/error" ;
12
18
import {
13
19
signature721InputSchemaV5 ,
14
20
signature721OutputSchemaV5 ,
@@ -180,9 +186,6 @@ export async function erc721SignaturePrepare(fastify: FastifyInstance) {
180
186
price,
181
187
priceInWei,
182
188
currency,
183
- primarySaleRecipient,
184
- royaltyRecipient,
185
- royaltyBps,
186
189
validityStartTimestamp,
187
190
validityEndTimestamp,
188
191
uid,
@@ -195,6 +198,54 @@ export async function erc721SignaturePrepare(fastify: FastifyInstance) {
195
198
address : contractAddress ,
196
199
} ) ;
197
200
201
+ let primarySaleRecipient = request . body . primarySaleRecipient ;
202
+ let royaltyRecipient = request . body . royaltyRecipient ;
203
+ let royaltyBps = request . body . royaltyBps ;
204
+
205
+ if ( ! royaltyRecipient || ! royaltyBps ) {
206
+ try {
207
+ const [ defaultRoyaltyRecipient , defaultRoyaltyBps ] =
208
+ await getDefaultRoyaltyInfo ( {
209
+ contract,
210
+ } ) ;
211
+
212
+ royaltyRecipient = royaltyRecipient ?? defaultRoyaltyRecipient ;
213
+ royaltyBps = royaltyBps ?? defaultRoyaltyBps ;
214
+ } catch ( e ) {
215
+ logger ( {
216
+ level : "error" ,
217
+ message : "Could not get default royalty info." ,
218
+ service : "server" ,
219
+ error : e ,
220
+ } ) ;
221
+ throw createCustomError (
222
+ "Could not get default royalty info." ,
223
+ StatusCodes . BAD_REQUEST ,
224
+ "DEFAULT_ROYALTY_INFO" ,
225
+ ) ;
226
+ }
227
+ }
228
+
229
+ if ( ! primarySaleRecipient ) {
230
+ try {
231
+ primarySaleRecipient = await getDefaultPrimarySaleRecipient ( {
232
+ contract,
233
+ } ) ;
234
+ } catch ( e ) {
235
+ logger ( {
236
+ level : "error" ,
237
+ message : "Could not get default primary sale recipient." ,
238
+ service : "server" ,
239
+ error : e ,
240
+ } ) ;
241
+ throw createCustomError (
242
+ "Could not get default primary sale recipient." ,
243
+ StatusCodes . BAD_REQUEST ,
244
+ "DEFAULT_PRIMARY_SALE_RECIPIENT" ,
245
+ ) ;
246
+ }
247
+ }
248
+
198
249
const mintPayload = await generateMintSignaturePayload ( {
199
250
metadata,
200
251
to,
@@ -249,12 +300,21 @@ export async function erc721SignaturePrepare(fastify: FastifyInstance) {
249
300
} ) ;
250
301
}
251
302
303
+ type GenerateMintSignaturePayloadOptions = Omit <
304
+ GenerateMintSignatureOptions [ "mintRequest" ] ,
305
+ "royaltyRecipient" | "primarySaleRecipient" | "royaltyBps"
306
+ > & {
307
+ royaltyRecipient : string ;
308
+ primarySaleRecipient : string ;
309
+ royaltyBps : number ;
310
+ } ;
311
+
252
312
/**
253
313
* Helper functions copied from v5 SDK.
254
314
* The logic to generate a mint signature is not exported.
255
315
*/
256
316
export async function generateMintSignaturePayload (
257
- mintRequest : GenerateMintSignatureOptions [ "mintRequest" ] ,
317
+ mintRequest : GenerateMintSignaturePayloadOptions ,
258
318
) {
259
319
const currency = mintRequest . currency || ZERO_ADDRESS ;
260
320
const [ price , uri , uid ] = await Promise . all ( [
@@ -288,25 +348,15 @@ export async function generateMintSignaturePayload(
288
348
const startTime = mintRequest . validityStartTimestamp || new Date ( 0 ) ;
289
349
const endTime = mintRequest . validityEndTimestamp || tenYearsFromNow ( ) ;
290
350
291
- const saleRecipient = mintRequest . primarySaleRecipient ;
292
- if ( ! saleRecipient ) {
293
- throw new Error ( "@UNIMPLEMENTED" ) ;
294
- }
295
-
296
- const royaltyRecipient = mintRequest . royaltyRecipient ;
297
- if ( ! royaltyRecipient ) {
298
- throw new Error ( "@UNIMPLEMENTED" ) ;
299
- }
300
-
301
351
return {
302
352
uri,
303
353
currency,
304
354
uid,
305
355
price,
306
356
to : mintRequest . to ,
307
- royaltyRecipient : royaltyRecipient ,
308
- royaltyBps : BigInt ( mintRequest . royaltyBps || 0 ) ,
309
- primarySaleRecipient : saleRecipient ,
357
+ royaltyRecipient : mintRequest . royaltyRecipient ,
358
+ royaltyBps : mintRequest . royaltyBps ,
359
+ primarySaleRecipient : mintRequest . primarySaleRecipient ,
310
360
validityStartTimestamp : dateToSeconds ( startTime ) ,
311
361
validityEndTimestamp : dateToSeconds ( endTime ) ,
312
362
} ;
0 commit comments