3
3
VerifyingKey ,
4
4
CREDITS_PROGRAM_KEYS ,
5
5
KEY_STORE ,
6
+ Key ,
6
7
PRIVATE_TRANSFER ,
7
8
PRIVATE_TO_PUBLIC_TRANSFER ,
8
9
PUBLIC_TRANSFER ,
@@ -32,6 +33,7 @@ interface KeySearchParams {
32
33
* verifierUri to fetch keys via HTTP from a remote resource as well as a unique cacheKey to store the keys in memory.
33
34
*/
34
35
class AleoKeyProviderParams implements KeySearchParams {
36
+ name : string | undefined ;
35
37
proverUri : string | undefined ;
36
38
verifierUri : string | undefined ;
37
39
cacheKey : string | undefined ;
@@ -44,10 +46,11 @@ class AleoKeyProviderParams implements KeySearchParams {
44
46
*
45
47
* @param { AleoKeyProviderInitParams } params - Optional search parameters
46
48
*/
47
- constructor ( params : { proverUri ?: string , verifierUri ?: string , cacheKey ?: string } ) {
49
+ constructor ( params : { proverUri ?: string , verifierUri ?: string , cacheKey ?: string , name ?: string } ) {
48
50
this . proverUri = params . proverUri ;
49
51
this . verifierUri = params . verifierUri ;
50
52
this . cacheKey = params . cacheKey ;
53
+ this . name = params . name ;
51
54
}
52
55
}
53
56
@@ -328,6 +331,13 @@ class AleoKeyProvider implements FunctionKeyProvider {
328
331
let proverUrl ;
329
332
let verifierUrl ;
330
333
let cacheKey ;
334
+ if ( "name" in params && typeof params [ "name" ] == "string" ) {
335
+ let key = CREDITS_PROGRAM_KEYS . getKey ( params [ "name" ] ) ;
336
+ if ( ! ( key instanceof Error ) ) {
337
+ return this . fetchCreditsKeys ( key ) ;
338
+ }
339
+ }
340
+
331
341
if ( "proverUri" in params && typeof params [ "proverUri" ] == "string" ) {
332
342
proverUrl = params [ "proverUri" ] ;
333
343
}
@@ -341,7 +351,7 @@ class AleoKeyProvider implements FunctionKeyProvider {
341
351
}
342
352
343
353
if ( proverUrl && verifierUrl ) {
344
- return await this . fetchKeys ( proverUrl , verifierUrl , cacheKey ) ;
354
+ return await this . fetchRemoteKeys ( proverUrl , verifierUrl , cacheKey ) ;
345
355
}
346
356
347
357
if ( cacheKey ) {
@@ -376,7 +386,7 @@ class AleoKeyProvider implements FunctionKeyProvider {
376
386
* CREDITS_PROGRAM_KEYS.transfer_private.verifier,
377
387
* );
378
388
*/
379
- async fetchKeys ( proverUrl : string , verifierUrl : string , cacheKey ?: string ) : Promise < FunctionKeyPair | Error > {
389
+ async fetchRemoteKeys ( proverUrl : string , verifierUrl : string , cacheKey ?: string ) : Promise < FunctionKeyPair | Error > {
380
390
try {
381
391
// If cache is enabled, check if the keys have already been fetched and return them if they have
382
392
if ( this . cacheOption ) {
@@ -406,16 +416,67 @@ class AleoKeyProvider implements FunctionKeyProvider {
406
416
}
407
417
}
408
418
409
- bondPublicKeys ( ) : Promise < FunctionKeyPair | Error > {
410
- return this . fetchKeys ( CREDITS_PROGRAM_KEYS . bond_public . prover , CREDITS_PROGRAM_KEYS . bond_public . verifier , CREDITS_PROGRAM_KEYS . bond_public . locator )
419
+ /***
420
+ * Fetches the proving key from a remote source.
421
+ *
422
+ * @param proverUrl
423
+ * @param cacheKey
424
+ *
425
+ * @returns {Promise<ProvingKey | Error> } Proving key for the specified program
426
+ */
427
+ async fetchProvingKey ( proverUrl : string , cacheKey ?: string ) : Promise < ProvingKey | Error > {
428
+ try {
429
+ // If cache is enabled, check if the keys have already been fetched and return them if they have
430
+ if ( this . cacheOption ) {
431
+ if ( ! cacheKey ) {
432
+ cacheKey = proverUrl ;
433
+ }
434
+ const value = this . cache . get ( cacheKey ) ;
435
+ if ( typeof value !== "undefined" ) {
436
+ return ProvingKey . fromBytes ( value [ 0 ] ) ;
437
+ } else {
438
+ console . debug ( "Fetching proving keys from url " + proverUrl ) ;
439
+ const provingKey = < ProvingKey > ProvingKey . fromBytes ( await this . fetchBytes ( proverUrl ) ) ;
440
+ return provingKey ;
441
+ }
442
+ }
443
+ else {
444
+ const provingKey = < ProvingKey > ProvingKey . fromBytes ( await this . fetchBytes ( proverUrl ) ) ;
445
+ return provingKey ;
446
+ }
447
+ } catch ( error ) {
448
+ throw new Error ( `Error: ${ error } fetching fee proving keys from ${ proverUrl } ` ) ;
449
+ }
450
+ }
451
+
452
+ async fetchCreditsKeys ( key : Key ) : Promise < FunctionKeyPair | Error > {
453
+ try {
454
+ if ( ! this . cache . has ( key . locator ) || ! this . cacheOption ) {
455
+ const verifying_key = key . verifyingKey ( )
456
+ const proving_key = < ProvingKey > await this . fetchProvingKey ( key . prover , key . locator ) ;
457
+ if ( this . cacheOption ) {
458
+ this . cache . set ( CREDITS_PROGRAM_KEYS . bond_public . locator , [ proving_key . toBytes ( ) , verifying_key . toBytes ( ) ] ) ;
459
+ }
460
+ return [ proving_key , verifying_key ] ;
461
+ } else {
462
+ const keyPair = < CachedKeyPair > this . cache . get ( key . locator ) ;
463
+ return [ ProvingKey . fromBytes ( keyPair [ 0 ] ) , VerifyingKey . fromBytes ( keyPair [ 1 ] ) ] ;
464
+ }
465
+ } catch ( error ) {
466
+ throw new Error ( `Error: fetching credits.aleo keys: ${ error } ` ) ;
467
+ }
468
+ }
469
+
470
+ async bondPublicKeys ( ) : Promise < FunctionKeyPair | Error > {
471
+ return this . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . bond_public ) ;
411
472
}
412
473
413
474
bondValidatorKeys ( ) : Promise < FunctionKeyPair | Error > {
414
- return this . fetchKeys ( CREDITS_PROGRAM_KEYS . bond_validator . prover , CREDITS_PROGRAM_KEYS . bond_validator . verifier , CREDITS_PROGRAM_KEYS . bond_validator . locator )
475
+ return this . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . bond_validator ) ;
415
476
}
416
477
417
478
claimUnbondPublicKeys ( ) : Promise < FunctionKeyPair | Error > {
418
- return this . fetchKeys ( CREDITS_PROGRAM_KEYS . claim_unbond_public . prover , CREDITS_PROGRAM_KEYS . claim_unbond_public . verifier , CREDITS_PROGRAM_KEYS . claim_unbond_public . locator )
479
+ return this . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . claim_unbond_public )
419
480
}
420
481
421
482
/**
@@ -438,15 +499,15 @@ class AleoKeyProvider implements FunctionKeyProvider {
438
499
*/
439
500
async transferKeys ( visibility : string ) : Promise < FunctionKeyPair | Error > {
440
501
if ( PRIVATE_TRANSFER . has ( visibility ) ) {
441
- return await this . fetchKeys ( CREDITS_PROGRAM_KEYS . transfer_private . prover , CREDITS_PROGRAM_KEYS . transfer_private . verifier , CREDITS_PROGRAM_KEYS . transfer_private . locator ) ;
502
+ return await this . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . transfer_private ) ;
442
503
} else if ( PRIVATE_TO_PUBLIC_TRANSFER . has ( visibility ) ) {
443
- return await this . fetchKeys ( CREDITS_PROGRAM_KEYS . transfer_private_to_public . prover , CREDITS_PROGRAM_KEYS . transfer_private_to_public . verifier , CREDITS_PROGRAM_KEYS . transfer_private_to_public . locator ) ;
504
+ return await this . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . transfer_private_to_public ) ;
444
505
} else if ( PUBLIC_TRANSFER . has ( visibility ) ) {
445
- return await this . fetchKeys ( CREDITS_PROGRAM_KEYS . transfer_public . prover , CREDITS_PROGRAM_KEYS . transfer_public . verifier , CREDITS_PROGRAM_KEYS . transfer_public . locator ) ;
506
+ return await this . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . transfer_public ) ;
446
507
} else if ( PUBLIC_TRANSFER_AS_SIGNER . has ( visibility ) ) {
447
- return await this . fetchKeys ( CREDITS_PROGRAM_KEYS . transfer_public_as_signer . prover , CREDITS_PROGRAM_KEYS . transfer_public_as_signer . verifier , CREDITS_PROGRAM_KEYS . transfer_public_as_signer . locator ) ;
508
+ return await this . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . transfer_public_as_signer ) ;
448
509
} else if ( PUBLIC_TO_PRIVATE_TRANSFER . has ( visibility ) ) {
449
- return await this . fetchKeys ( CREDITS_PROGRAM_KEYS . transfer_public_to_private . prover , CREDITS_PROGRAM_KEYS . transfer_public_to_private . verifier , CREDITS_PROGRAM_KEYS . transfer_public_to_private . locator ) ;
510
+ return await this . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . transfer_public_to_private ) ;
450
511
} else {
451
512
throw new Error ( "Invalid visibility type" ) ;
452
513
}
@@ -458,7 +519,7 @@ class AleoKeyProvider implements FunctionKeyProvider {
458
519
* @returns {Promise<FunctionKeyPair | Error> } Proving and verifying keys for the join function
459
520
*/
460
521
async joinKeys ( ) : Promise < FunctionKeyPair | Error > {
461
- return await this . fetchKeys ( CREDITS_PROGRAM_KEYS . join . prover , CREDITS_PROGRAM_KEYS . join . verifier , CREDITS_PROGRAM_KEYS . join . locator ) ;
522
+ return await this . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . join ) ;
462
523
}
463
524
464
525
/**
@@ -467,7 +528,7 @@ class AleoKeyProvider implements FunctionKeyProvider {
467
528
* @returns {Promise<FunctionKeyPair | Error> } Proving and verifying keys for the split function
468
529
* */
469
530
async splitKeys ( ) : Promise < FunctionKeyPair | Error > {
470
- return await this . fetchKeys ( CREDITS_PROGRAM_KEYS . split . prover , CREDITS_PROGRAM_KEYS . split . verifier , CREDITS_PROGRAM_KEYS . split . locator ) ;
531
+ return await this . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . split ) ;
471
532
}
472
533
473
534
/**
@@ -476,7 +537,7 @@ class AleoKeyProvider implements FunctionKeyProvider {
476
537
* @returns {Promise<FunctionKeyPair | Error> } Proving and verifying keys for the fee function
477
538
*/
478
539
async feePrivateKeys ( ) : Promise < FunctionKeyPair | Error > {
479
- return await this . fetchKeys ( CREDITS_PROGRAM_KEYS . fee_private . prover , CREDITS_PROGRAM_KEYS . fee_private . verifier , CREDITS_PROGRAM_KEYS . fee_private . locator ) ;
540
+ return await this . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . fee_private ) ;
480
541
}
481
542
482
543
/**
@@ -485,7 +546,7 @@ class AleoKeyProvider implements FunctionKeyProvider {
485
546
* @returns {Promise<FunctionKeyPair | Error> } Proving and verifying keys for the fee function
486
547
*/
487
548
async feePublicKeys ( ) : Promise < FunctionKeyPair | Error > {
488
- return await this . fetchKeys ( CREDITS_PROGRAM_KEYS . fee_public . prover , CREDITS_PROGRAM_KEYS . fee_public . verifier , CREDITS_PROGRAM_KEYS . fee_public . locator ) ;
549
+ return await this . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . fee_public ) ;
489
550
}
490
551
491
552
/**
@@ -544,7 +605,7 @@ class AleoKeyProvider implements FunctionKeyProvider {
544
605
}
545
606
546
607
unBondPublicKeys ( ) : Promise < FunctionKeyPair | Error > {
547
- return this . fetchKeys ( CREDITS_PROGRAM_KEYS . unbond_public . prover , CREDITS_PROGRAM_KEYS . unbond_public . verifier , CREDITS_PROGRAM_KEYS . unbond_public . locator ) ;
608
+ return this . fetchCreditsKeys ( CREDITS_PROGRAM_KEYS . unbond_public ) ;
548
609
}
549
610
}
550
611
0 commit comments