1
- import { AbstractSigner , TypedDataField , getAddress , Signature , verifyTypedData } from 'ethers' ;
1
+ import {
2
+ BigNumberish ,
3
+ AbstractSigner ,
4
+ TypedDataField ,
5
+ getAddress ,
6
+ Signature ,
7
+ verifyTypedData ,
8
+ } from 'ethers' ;
9
+ import {
10
+ BuildRequestOptions ,
11
+ CancelOption ,
12
+ GenericOfferOptions ,
13
+ GenericQuery ,
14
+ OfferData ,
15
+ PaymentOption ,
16
+ RequestData ,
17
+ UnsignedOfferPayload ,
18
+ } from './types.js' ;
2
19
import { ContractConfig } from '../utils/contract.js' ;
3
- import { hashObject } from '../utils/hash.js' ;
4
- import { uuid4 } from '../utils/uid.js' ;
5
- import { nowSec , parseSeconds } from '../utils/time.js' ;
6
-
7
- /**
8
- * Generic message data type
9
- */
10
- export interface GenericMessage {
11
- /** Unique message Id */
12
- id : string ;
13
- /** Expiration time in seconds */
14
- expire : number ;
15
- /** A number that reflects the version of the message */
16
- nonce : number ;
17
- }
18
-
19
- /**
20
- * Generic query type
21
- */
22
- export type GenericQuery = Record < string , unknown > ;
23
-
24
- /**
25
- * Request data type
26
- */
27
- export interface RequestData < CustomRequestQuery extends GenericQuery > extends GenericMessage {
28
- /** Request topic */
29
- topic : string ;
30
-
31
- /** Custom query validation schema */
32
- query : CustomRequestQuery ;
33
- }
34
-
35
- /**
36
- * buildRequest method options type
37
- */
38
- export interface BuildRequestOptions < CustomRequestQuery extends GenericQuery > {
39
- /** Expiration time */
40
- expire : string | number ;
41
- /** Nonce */
42
- nonce : number ;
43
- /** Topic */
44
- topic : string ;
45
- /** Request query */
46
- query : CustomRequestQuery ;
47
- /** If allowed request Id override */
48
- idOverride ?: string ;
49
- }
20
+ import { hashCancelOptionArray , hashObject , hashPaymentOptionArray } from '../utils/hash.js' ;
21
+ import { randomSalt } from '../utils/uid.js' ;
22
+ import { parseExpire } from '../utils/time.js' ;
50
23
51
24
/**
52
25
* Builds a request
@@ -62,84 +35,14 @@ export const buildRequest = async <CustomRequestQuery extends GenericQuery>(
62
35
const { expire, nonce, topic, query, idOverride } = requestOptions ;
63
36
// @todo Validate request options
64
37
return {
65
- id : idOverride ?? uuid4 ( ) ,
66
- expire : typeof expire === 'number' ? parseSeconds ( expire ) : nowSec ( ) + parseSeconds ( expire ) ,
38
+ id : idOverride ?? randomSalt ( ) ,
39
+ expire : parseExpire ( expire ) ,
67
40
nonce,
68
41
topic,
69
42
query,
70
43
} ;
71
44
} ;
72
45
73
- /**
74
- * Offered payment option type
75
- */
76
- export interface PaymentOption {
77
- /** Unique payment option Id */
78
- id : string ;
79
- /** Asset price in WEI */
80
- price : string ;
81
- /** ERC20 asset contract address */
82
- asset : string ;
83
- }
84
-
85
- /**
86
- * Offered cancellation option type
87
- */
88
- export interface CancelOption {
89
- /** Seconds before checkIn */
90
- time : number ;
91
- /** Percents of total sum */
92
- penalty : number ;
93
- }
94
-
95
- /**
96
- * Unsigned offer payload type
97
- */
98
- export interface UnsignedOfferPayload {
99
- /** Unique supplier Id registered on the protocol contract */
100
- supplierId : string ;
101
- /** Target network chain Id */
102
- chainId : number ;
103
- /** <keccak256(request.hash())> */
104
- requestHash : string ;
105
- /** <keccak256(JSON.stringify(offer.options))> */
106
- optionsHash : string ;
107
- /** <keccak256(JSON.stringify(offer.payment))> */
108
- paymentHash : string ;
109
- /** <keccak256(JSON.stringify(offer.cancel(sorted by time DESC) || []))> */
110
- cancelHash : string ;
111
- /** makes the deal NFT transferable or not */
112
- transferable : boolean ;
113
- /** check-in time in seconds */
114
- checkIn : number ;
115
- }
116
-
117
- /**
118
- * Generic offer is just an object with props type
119
- */
120
- export type GenericOfferOptions = Record < string , unknown > ;
121
-
122
- /**
123
- * Offer data type
124
- */
125
- export interface OfferData <
126
- CustomRequestQuery extends GenericQuery ,
127
- CustomOfferOptions extends GenericOfferOptions ,
128
- > extends GenericMessage {
129
- /** Copy of request */
130
- request : RequestData < CustomRequestQuery > ;
131
- /** Offer options */
132
- options : CustomOfferOptions ;
133
- /** Payment options */
134
- payment : PaymentOption [ ] ;
135
- /** Cancellation options */
136
- cancel : CancelOption [ ] ;
137
- /** Raw offer payload */
138
- payload : UnsignedOfferPayload ;
139
- //** EIP-712 TypedSignature(UnsignedOffer) */
140
- signature : string ;
141
- }
142
-
143
46
/**
144
47
* EIP-712 JSON schema types for offer
145
48
*/
@@ -194,7 +97,7 @@ export interface BuildOfferOptions<
194
97
/** Unique supplier Id */
195
98
supplierId : string ;
196
99
/** Expiration time: duration (string) or seconds (number) */
197
- expire : string | number ;
100
+ expire : string | BigNumberish ;
198
101
/** Request data */
199
102
request : RequestData < CustomRequestQuery > ;
200
103
/** Offer options */
@@ -203,8 +106,10 @@ export interface BuildOfferOptions<
203
106
payment : PaymentOption [ ] ;
204
107
/** Offer cancellation options */
205
108
cancel : CancelOption [ ] ;
206
- /** Check In time in seconds */
207
- checkIn : number ;
109
+ /** Check-in time in seconds */
110
+ checkIn : BigNumberish ;
111
+ /** Check-out time in seconds */
112
+ checkOut : BigNumberish ;
208
113
/** Transferrable offer flag */
209
114
transferable ?: boolean ;
210
115
/** The possibility to override an offer Id flag */
@@ -235,23 +140,30 @@ export const buildOffer = async <
235
140
payment,
236
141
cancel,
237
142
checkIn,
143
+ checkOut,
238
144
transferable,
239
145
signer,
240
146
signatureOverride,
241
147
idOverride,
242
- expire,
243
148
} = offerOptions ;
149
+ let { expire } = offerOptions ;
244
150
245
151
// @todo Validate offer options
246
152
153
+ const id = idOverride ?? randomSalt ( ) ;
154
+ expire = parseExpire ( expire ) ;
155
+
247
156
const unsignedOfferPayload : UnsignedOfferPayload = {
157
+ id,
158
+ expire,
248
159
supplierId,
249
- chainId : Number ( contract . chainId ) ,
160
+ chainId : BigInt ( contract . chainId ) ,
250
161
requestHash : hashObject ( request ) ,
251
162
optionsHash : hashObject ( options ) ,
252
- paymentHash : hashObject ( payment ) ,
253
- cancelHash : hashObject ( cancel ) ,
254
- checkIn,
163
+ paymentHash : hashPaymentOptionArray ( payment ) ,
164
+ cancelHash : hashCancelOptionArray ( cancel ) ,
165
+ checkIn : BigInt ( checkIn ) ,
166
+ checkOut : BigInt ( checkOut ) ,
255
167
transferable : transferable ?? true ,
256
168
} ;
257
169
@@ -262,7 +174,7 @@ export const buildOffer = async <
262
174
{
263
175
name : contract . name ,
264
176
version : contract . version ,
265
- chainId : contract . chainId ,
177
+ chainId : BigInt ( contract . chainId ) ,
266
178
verifyingContract : contract . address ,
267
179
} ,
268
180
offerEip712Types ,
@@ -275,9 +187,9 @@ export const buildOffer = async <
275
187
}
276
188
277
189
return {
278
- id : idOverride ?? uuid4 ( ) ,
279
- expire : typeof expire === 'number' ? parseSeconds ( expire ) : nowSec ( ) + parseSeconds ( expire ) ,
280
- nonce : 1 ,
190
+ id,
191
+ expire,
192
+ nonce : BigInt ( 1 ) ,
281
193
request,
282
194
options,
283
195
payment,
0 commit comments