@@ -8,17 +8,72 @@ import { AleoNetworkClient } from "./network-client.js";
8
8
* implementations.
9
9
*/
10
10
interface RecordSearchParams {
11
+ unspent : boolean ;
11
12
[ key : string ] : any ; // This allows for arbitrary keys with any type values
12
13
}
13
14
15
+ type RecordsResponseFilter = {
16
+ program : boolean ;
17
+ record : boolean ;
18
+ function : boolean ;
19
+ transition : boolean ;
20
+ blockHeight : boolean ;
21
+ transactionId : boolean ;
22
+ transitionId : boolean ;
23
+ ioIndex : boolean ;
24
+ }
25
+
26
+ type EncryptedRecord = {
27
+ commitment : string ;
28
+ checksum ?: string ;
29
+ blockHeight ?: number ;
30
+ programName ?: string ;
31
+ functionName ?: string ;
32
+ outputIndex ?: number ;
33
+ owner ?: string ;
34
+ recordCiphertext ?: string ;
35
+ recordName ?: string ;
36
+ recordNonce ?: string ;
37
+ transactionId ?: string ;
38
+ transitionId ?: string ;
39
+ transactionIndex ?: number ;
40
+ transitionIndex ?: number ;
41
+ }
42
+
43
+ type OwnedRecord = {
44
+ blockHeight ?: number ;
45
+ commitment ?: string ;
46
+ functionName ?: string ;
47
+ outputIndex ?: number ;
48
+ owner ?: string ;
49
+ programName ?: string ;
50
+ recordCiphertext ?: string ;
51
+ recordPlaintext ?: string ;
52
+ recordName ?: string ;
53
+ spent ?: boolean ;
54
+ tag ?: string ;
55
+ transactionId ?: string ;
56
+ transitionId ?: string ;
57
+ transactionIndex ?: number ;
58
+ transitionIndex ?: number ;
59
+ }
60
+
61
+ interface RecordResponse {
62
+ owner ?: string ;
63
+ }
64
+
14
65
/**
15
66
* Interface for a record provider. A record provider is used to find records for use in deployment and execution
16
67
* transactions on the Aleo Network. A default implementation is provided by the NetworkRecordProvider class. However,
17
68
* a custom implementation can be provided (say if records are synced locally to a database from the network) by
18
69
* implementing this interface.
19
70
*/
20
71
interface RecordProvider {
21
- account : Account
72
+ getEncryptedRecords ( recordsFilter : RecordSearchParams , responseFilter : RecordsResponseFilter ) : Promise < EncryptedRecord [ ] > ;
73
+
74
+ serialNumbersExist ( serialNumbers : string [ ] ) : Promise < Record < string , boolean > > ;
75
+
76
+ tagsExist ( tags : string [ ] ) : Promise < Record < string , boolean > > ;
22
77
23
78
/**
24
79
* Find a credits.aleo record with a given number of microcredits from the chosen provider
@@ -42,7 +97,7 @@ interface RecordProvider {
42
97
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
43
98
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
44
99
*/
45
- findCreditsRecord ( microcredits : number , unspent : boolean , nonces ?: string [ ] , searchParameters ?: RecordSearchParams ) : Promise < RecordPlaintext > ;
100
+ findCreditsRecord ( microcredits : number , searchParameters : RecordSearchParams , nonces ?: string [ ] ) : Promise < OwnedRecord > ;
46
101
47
102
/**
48
103
* Find a list of credit.aleo records with a given number of microcredits from the chosen provider
@@ -68,7 +123,7 @@ interface RecordProvider {
68
123
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
69
124
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
70
125
*/
71
- findCreditsRecords ( microcreditAmounts : number [ ] , unspent : boolean , nonces ?: string [ ] , searchParameters ?: RecordSearchParams ) : Promise < RecordPlaintext [ ] > ;
126
+ findCreditsRecords ( microcreditAmounts : number [ ] , searchParameters : RecordSearchParams , nonces ?: string [ ] ) : Promise < OwnedRecord [ ] > ;
72
127
73
128
/**
74
129
* Find an arbitrary record
@@ -101,7 +156,7 @@ interface RecordProvider {
101
156
*
102
157
* const record = await recordProvider.findRecord(true, [], params);
103
158
*/
104
- findRecord ( unspent : boolean , nonces ?: string [ ] , searchParameters ?: RecordSearchParams ) : Promise < RecordPlaintext > ;
159
+ findRecord ( searchParameters : RecordSearchParams , filterFn ?: ( record : RecordPlaintext ) => boolean , nonces ?: string [ ] ) : Promise < OwnedRecord > ;
105
160
106
161
/**
107
162
* Find multiple records from arbitrary programs
@@ -135,7 +190,7 @@ interface RecordProvider {
135
190
* const params = new CustomRecordSearch(0, 100, 5000, 2, "credits.aleo", "credits");
136
191
* const records = await recordProvider.findRecord(true, [], params);
137
192
*/
138
- findRecords ( unspent : boolean , nonces ?: string [ ] , searchParameters ?: RecordSearchParams ) : Promise < RecordPlaintext [ ] > ;
193
+ findRecords ( searchParameters ?: RecordSearchParams , filterFn ?: ( record : RecordPlaintext ) => boolean , nonces ?: string [ ] , ) : Promise < OwnedRecord [ ] > ;
139
194
}
140
195
141
196
/**
@@ -187,7 +242,7 @@ class NetworkRecordProvider implements RecordProvider {
187
242
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
188
243
*
189
244
* */
190
- async findCreditsRecords ( microcredits : number [ ] , unspent : boolean , nonces ?: string [ ] , searchParameters ?: RecordSearchParams ) : Promise < RecordPlaintext [ ] > {
245
+ async findCreditsRecords ( microcredits : number [ ] , searchParameters : RecordSearchParams , nonces ?: string [ ] ) : Promise < OwnedRecord [ ] > {
191
246
let startHeight = 0 ;
192
247
let endHeight = 0 ;
193
248
let maxAmount = undefined ;
@@ -208,10 +263,6 @@ class NetworkRecordProvider implements RecordProvider {
208
263
if ( "maxAmount" in searchParameters && typeof searchParameters [ "maxAmount" ] == "number" ) {
209
264
maxAmount = searchParameters [ "maxAmount" ] ;
210
265
}
211
-
212
- if ( "unspent" in searchParameters && typeof searchParameters [ "unspent" ] == "boolean" ) {
213
- unspent = searchParameters [ "unspent" ]
214
- }
215
266
}
216
267
217
268
// If the end height is not specified, use the current block height
@@ -225,7 +276,15 @@ class NetworkRecordProvider implements RecordProvider {
225
276
logAndThrow ( "Start height must be less than end height" ) ;
226
277
}
227
278
228
- return await this . networkClient . findRecords ( startHeight , endHeight , unspent , [ "credits.aleo" ] , microcredits , maxAmount , nonces , this . account . privateKey ( ) ) ;
279
+ const recordsPts = await this . networkClient . findRecords ( startHeight , endHeight , searchParameters . unspent , [ "credits.aleo" ] , microcredits , maxAmount , nonces , this . account . privateKey ( ) ) ;
280
+ return recordsPts . map ( ( record ) => ( {
281
+ commitment : record . commitment ( ) . toString ( ) ,
282
+ owner : record . owner ( ) . toString ( ) ,
283
+ programName : 'credits.aleo' ,
284
+ recordName : 'credits' ,
285
+ recordPlaintext : record . to_string ( ) ,
286
+ tag : record . tag ( ) . toString ( ) ,
287
+ } ) ) ;
229
288
}
230
289
231
290
/**
@@ -255,11 +314,11 @@ class NetworkRecordProvider implements RecordProvider {
255
314
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
256
315
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
257
316
*/
258
- async findCreditsRecord ( microcredits : number , unspent : boolean , nonces ?: string [ ] , searchParameters ?: RecordSearchParams ) : Promise < RecordPlaintext > {
317
+ async findCreditsRecord ( microcredits : number , searchParameters : RecordSearchParams , nonces ?: string [ ] ) : Promise < OwnedRecord > {
259
318
let records = null ;
260
319
261
320
try {
262
- records = await this . findCreditsRecords ( [ microcredits ] , unspent , nonces , searchParameters ) ;
321
+ records = await this . findCreditsRecords ( [ microcredits ] , searchParameters , nonces ) ;
263
322
} catch ( e ) {
264
323
console . log ( "No records found with error:" , e ) ;
265
324
}
@@ -275,14 +334,27 @@ class NetworkRecordProvider implements RecordProvider {
275
334
/**
276
335
* Find an arbitrary record. WARNING: This function is not implemented yet and will throw an error.
277
336
*/
278
- async findRecord ( unspent : boolean , nonces ?: string [ ] , searchParameters ?: RecordSearchParams ) : Promise < RecordPlaintext > {
279
- throw new Error ( "Not implemented" ) ;
337
+ async findRecord ( searchParameters : RecordSearchParams , filterFn ?: ( record : RecordPlaintext ) => boolean , nonces ?: string [ ] ) : Promise < OwnedRecord > {
338
+ let records ;
339
+
340
+ try {
341
+ records = await this . findRecords ( searchParameters , filterFn , nonces ) ;
342
+ } catch ( e ) {
343
+ console . log ( "No records found with error:" , e ) ;
344
+ }
345
+
346
+ if ( records && records . length > 0 ) {
347
+ return records [ 0 ] ;
348
+ }
349
+
350
+ console . error ( "Record not found with error:" , records ) ;
351
+ throw new Error ( "Record not found" ) ;
280
352
}
281
353
282
354
/**
283
355
* Find multiple records from a specified program.
284
356
*/
285
- async findRecords ( unspent : boolean , nonces ?: string [ ] , searchParameters ?: RecordSearchParams ) : Promise < RecordPlaintext [ ] > {
357
+ async findRecords ( searchParameters : RecordSearchParams , filterFn ?: ( record : RecordPlaintext ) => boolean , nonces ?: string [ ] ) : Promise < OwnedRecord [ ] > {
286
358
let startHeight = 0 ;
287
359
let endHeight = 0 ;
288
360
let amounts = undefined ;
@@ -317,10 +389,6 @@ class NetworkRecordProvider implements RecordProvider {
317
389
if ( "programs" in searchParameters && Array . isArray ( searchParameters [ "programs" ] ) && searchParameters [ "programs" ] . every ( ( item : any ) => typeof item === "string" ) ) {
318
390
programs = searchParameters [ "programs" ] ;
319
391
}
320
-
321
- if ( "unspent" in searchParameters && typeof searchParameters [ "unspent" ] == "boolean" ) {
322
- unspent = searchParameters [ "unspent" ]
323
- }
324
392
}
325
393
326
394
// If the end height is not specified, use the current block height
@@ -334,9 +402,26 @@ class NetworkRecordProvider implements RecordProvider {
334
402
logAndThrow ( "Start height must be less than end height" ) ;
335
403
}
336
404
337
- return await this . networkClient . findRecords ( startHeight , endHeight , unspent , programs , amounts , maxAmount , nonces , this . account . privateKey ( ) ) ;
405
+ const recordPts = await this . networkClient . findRecords ( startHeight , endHeight , searchParameters . unspent , programs , amounts , maxAmount , nonces , this . account . privateKey ( ) ) ;
406
+ return recordPts . map ( ( record ) => ( {
407
+ commitment : record . commitment ( ) . toString ( ) ,
408
+ owner : record . owner ( ) . toString ( ) ,
409
+ programName : record . program ( ) . toString ( ) ,
410
+ recordName : record . name ( ) . toString ( ) ,
411
+ } ) ) ;
338
412
}
339
413
414
+ async getEncryptedRecords ( recordsFilter : RecordSearchParams , responseFilter : RecordsResponseFilter ) : Promise < EncryptedRecord [ ] > {
415
+ throw new Error ( "Not implemented" ) ;
416
+ }
417
+
418
+ async serialNumbersExist ( serialNumbers : string [ ] ) : Promise < Record < string , boolean > > {
419
+ throw new Error ( "Not implemented" ) ;
420
+ }
421
+
422
+ async tagsExist ( tags : string [ ] ) : Promise < Record < string , boolean > > {
423
+ throw new Error ( "Not implemented" ) ;
424
+ }
340
425
}
341
426
342
427
/**
@@ -360,10 +445,19 @@ class NetworkRecordProvider implements RecordProvider {
360
445
class BlockHeightSearch implements RecordSearchParams {
361
446
startHeight : number ;
362
447
endHeight : number ;
363
- constructor ( startHeight : number , endHeight : number ) {
448
+ unspent : boolean ;
449
+ constructor ( startHeight : number , endHeight : number , unspent : boolean ) {
364
450
this . startHeight = startHeight ;
365
451
this . endHeight = endHeight ;
452
+ this . unspent = unspent ;
366
453
}
367
454
}
368
455
369
- export { BlockHeightSearch , NetworkRecordProvider , RecordProvider , RecordSearchParams } ;
456
+ export {
457
+ BlockHeightSearch ,
458
+ EncryptedRecord ,
459
+ OwnedRecord ,
460
+ RecordProvider ,
461
+ RecordSearchParams ,
462
+ RecordsResponseFilter ,
463
+ } ;
0 commit comments