@@ -138,27 +138,28 @@ public static String getPostboxKey(TorusKey torusKey) {
138
138
139
139
public CompletableFuture <TorusKey > retrieveShares (String [] endpoints , BigInteger [] indexes , String verifier , HashMap <String , Object > verifierParams ,
140
140
String idToken , HashMap <String , Object > extraParams , String networkMigrated ,
141
- @ Nullable ImportedShare [] importedShares ) {
141
+ @ Nullable ImportedShare [] importedShares ) { // TODO: Rename to retrieveOrImportShare
142
142
try {
143
143
144
- if (endpoints .length != indexes .length ) {
144
+ if (endpoints .length != indexes .length ) { // TODO: Fix params for this function, indexes are no longer needed here, other params are missing.
145
145
throw new IllegalArgumentException ("Length of endpoints must be the same as length of nodeIndexes" );
146
146
}
147
147
148
148
APIUtils .get (this .options .getAllowHost (), new Header []{new Header ("Origin" , this .options .getOrigin ()), new Header ("verifier" , verifier ), new Header ("verifierid" , verifierParams .get ("verifier_id" ).toString ()), new Header ("network" , networkMigrated ),
149
- new Header ("clientid" , this .options .getClientId ()), new Header ("enablegating" , "true" )}, true ).get ();
149
+ new Header ("clientid" , this .options .getClientId ()), new Header ("enablegating" , "true" )}, true ).get (); // TODO: Check these headers
150
150
List <CompletableFuture <String >> promiseArr = new ArrayList <>();
151
151
Set <SessionToken > sessionTokenData = new HashSet <>();
152
152
Set <BigInteger > nodeIndexs = new HashSet <>();
153
153
// generate temporary private and public key that is used to secure receive shares
154
- ECKeyPair sessionAuthKey = Keys .createEcKeyPair ();
155
- String pubKey = Utils .padLeft (sessionAuthKey .getPublicKey ().toString (16 ), '0' , 128 );
154
+ ECKeyPair sessionAuthKey = Keys .createEcKeyPair (); // TODO: Change this to indicate it is a secp256k1 key
155
+ String pubKey = Utils .padLeft (sessionAuthKey .getPublicKey ().toString (16 ), '0' , 128 ); // TODO: Refactor this to common class, avoid padding manually, this also helps with indicating where padding should not be used.
156
156
String pubKeyX = pubKey .substring (0 , pubKey .length () / 2 );
157
157
String pubKeyY = pubKey .substring (pubKey .length () / 2 );
158
158
159
159
String tokenCommitment = Hash .sha3String (idToken );
160
- int t = endpoints .length / 4 ;
161
- int k = t * 2 + 1 ;
160
+
161
+ int minRequiredCommitmments = (endpoints .length * 3 / 4 ) + 1 ;
162
+ int threshold = (endpoints .length * 2 ) + 1 ;
162
163
163
164
boolean isImportShareReq = false ;
164
165
if (importedShares != null && importedShares .length > 0 ) {
@@ -173,33 +174,37 @@ public CompletableFuture<TorusKey> retrieveShares(String[] endpoints, BigInteger
173
174
CompletableFuture <String > p = APIUtils .post (endpoints [i ], APIUtils .generateJsonRPCObject ("CommitmentRequest" , new CommitmentRequestParams ("mug00" , tokenCommitment .substring (2 ), pubKeyX , pubKeyY , String .valueOf (System .currentTimeMillis ()), verifier )), false );
174
175
promiseArr .add (i , p );
175
176
}
176
- // send share request once k + t number of commitment requests have completed
177
+
178
+ // send share request once minRequiredCommitmments number of commitment requests have completed
177
179
boolean finalIsImportShareReq = isImportShareReq ;
178
180
return new Some <>(promiseArr , (resultArr , commitmentsResolved ) -> {
179
181
List <String > completedRequests = new ArrayList <>();
180
- for (String result : resultArr ) {
181
- if (result != null && !result .isEmpty ()) {
182
- completedRequests .add (result );
182
+ int received = 0 ;
183
+ for (CompletableFuture <String > result : promiseArr ) {
184
+ try {
185
+ if (result .get () != null && !result .get ().isEmpty ()) {
186
+ received += 1 ;
187
+ completedRequests .add (result .get ());
188
+ if (!finalIsImportShareReq ) {
189
+ if (received >= minRequiredCommitmments ) {
190
+ break ;
191
+ }
192
+ }
193
+ }
194
+ } catch (Exception ex ) {
195
+ // ignore ex
183
196
}
184
197
}
198
+
199
+ // Return List<String> instead
185
200
CompletableFuture <List <String >> completableFuture = new CompletableFuture <>();
186
- if (importedShares != null && importedShares .length > 0 && completedRequests .size () == endpoints .length ) {
187
- completableFuture .complete (Arrays .asList (resultArr ));
188
- } else if (importedShares != null && importedShares .length == 0 && completedRequests .size () >= k + t ) {
189
- Gson gson = new Gson ();
190
- // we don't consider commitment to succeed unless node 1 responds
191
- boolean requiredNodeResultFound = completedRequests .stream ().anyMatch (x -> {
192
- if (x == null || x .isEmpty ()) return false ;
193
- JsonRPCResponse nodeSigResponse = gson .fromJson (x , JsonRPCResponse .class );
194
- if (nodeSigResponse == null || nodeSigResponse .getResult () == null ) return false ;
195
- NodeSignature nodeSignature = gson .fromJson (Utils .convertToJsonObject (nodeSigResponse .getResult ()), NodeSignature .class );
196
- return nodeSignature != null && nodeSignature .getNodeindex ().equals ("1" );
197
- });
198
- if (requiredNodeResultFound ) completableFuture .complete (completedRequests );
199
- else completableFuture .completeExceptionally (new PredicateFailedException ("commitment from node 1 not found" ));
200
- } else {
201
+ if (!finalIsImportShareReq && completedRequests .size () < minRequiredCommitmments ) {
202
+ completableFuture .completeExceptionally (new PredicateFailedException ("insufficient responses for commitments" ));
203
+ } else if (finalIsImportShareReq && completedRequests .size () < Arrays .stream (endpoints ).count ()) {
201
204
completableFuture .completeExceptionally (new PredicateFailedException ("insufficient responses for commitments" ));
202
205
}
206
+
207
+ completableFuture .complete (Arrays .asList (resultArr ));
203
208
return completableFuture ;
204
209
}).getCompletableFuture ().thenComposeAsync (responses -> {
205
210
try {
@@ -318,7 +323,7 @@ public CompletableFuture<TorusKey> retrieveShares(String[] endpoints, BigInteger
318
323
}
319
324
}
320
325
321
- String thresholdPublicKeyString = Utils .thresholdSame (completedResponsesPubKeys , k );
326
+ String thresholdPublicKeyString = Utils .thresholdSame (completedResponsesPubKeys , threshold );
322
327
PubKey thresholdPubKey = null ;
323
328
324
329
if (thresholdPublicKeyString == null ) {
@@ -464,15 +469,15 @@ public CompletableFuture<TorusKey> retrieveShares(String[] endpoints, BigInteger
464
469
.filter (Objects ::nonNull )
465
470
.collect (Collectors .toList ());
466
471
467
- if (validSigs .size () < k ) {
472
+ if (validSigs .size () < threshold ) {
468
473
throw new RuntimeException ("Insufficient number of signatures from nodes" );
469
474
}
470
475
471
476
List <String > validTokens = sessionTokens .stream ()
472
477
.filter (Objects ::nonNull )
473
478
.collect (Collectors .toList ());
474
479
475
- if (validTokens .size () < k ) {
480
+ if (validTokens .size () < threshold ) {
476
481
throw new RuntimeException ("Insufficient number of tokens from nodes" );
477
482
}
478
483
@@ -503,7 +508,7 @@ public CompletableFuture<TorusKey> retrieveShares(String[] endpoints, BigInteger
503
508
elements .add (i );
504
509
}
505
510
506
- List <List <Integer >> allCombis = Utils .kCombinations (elements , k );
511
+ List <List <Integer >> allCombis = Utils .kCombinations (elements , threshold );
507
512
508
513
for (List <Integer > currentCombi : allCombis ) {
509
514
Map <Integer , String > currentCombiShares = decryptedShares .entrySet ().stream ()
@@ -557,7 +562,7 @@ public CompletableFuture<TorusKey> retrieveShares(String[] endpoints, BigInteger
557
562
}
558
563
}
559
564
}
560
- String thresholdPublicKeyString = Utils .thresholdSame (completedResponsesPubKeys , k );
565
+ String thresholdPublicKeyString = Utils .thresholdSame (completedResponsesPubKeys , threshold );
561
566
PubKey thresholdPubKey = null ;
562
567
563
568
if (thresholdPublicKeyString == null ) {
@@ -582,7 +587,7 @@ public CompletableFuture<TorusKey> retrieveShares(String[] endpoints, BigInteger
582
587
} else {
583
588
responsesSize = completedResponses .size ();
584
589
}
585
- if (responsesSize >= k && thresholdPubKey != null && (thresholdNonceData != null || verifierParams .get ("extended_verifier_id" ) != null ||
590
+ if (responsesSize >= threshold && thresholdPubKey != null && (thresholdNonceData != null || verifierParams .get ("extended_verifier_id" ) != null ||
586
591
LEGACY_NETWORKS_ROUTE_MAP .containsKey (networkMigrated ))) {
587
592
List <DecryptedShare > decryptedShares = new ArrayList <>();
588
593
List <CompletableFuture <byte []>> sharePromises = new ArrayList <>();
@@ -827,7 +832,7 @@ public CompletableFuture<TorusKey> retrieveShares(String[] endpoints, BigInteger
827
832
}
828
833
}
829
834
830
- List <List <Integer >> allCombis = Utils .kCombinations (decryptedShares .size (), k );
835
+ List <List <Integer >> allCombis = Utils .kCombinations (decryptedShares .size (), threshold );
831
836
for (List <Integer > currentCombi : allCombis ) {
832
837
List <BigInteger > currentCombiSharesIndexes = new ArrayList <>();
833
838
List <BigInteger > currentCombiSharesValues = new ArrayList <>();
@@ -983,11 +988,11 @@ public CompletableFuture<TorusKey> retrieveShares(String[] endpoints, BigInteger
983
988
}
984
989
985
990
public CompletableFuture <TorusKey > retrieveShares (String [] endpoints , BigInteger [] indexes , String verifier , HashMap <String , Object > verifierParams , String idToken , @ Nullable ImportedShare [] importedShares ) {
986
- return this .retrieveShares (endpoints , indexes , verifier , verifierParams , idToken , null , getMigratedNetworkInfo (), importedShares );
991
+ return this .retrieveShares (endpoints , indexes , verifier , verifierParams , idToken , null , getMigratedNetworkInfo (), importedShares ); // TODO: extraParams should be passed here and not ignored
987
992
}
988
993
989
994
public CompletableFuture <TorusKey > retrieveShares (String [] endpoints , BigInteger [] indexes , String verifier , HashMap <String , Object > verifierParams , String idToken ) {
990
- return this .retrieveShares (endpoints , indexes , verifier , verifierParams , idToken , null , getMigratedNetworkInfo (), new ImportedShare []{});
995
+ return this .retrieveShares (endpoints , indexes , verifier , verifierParams , idToken , null , getMigratedNetworkInfo (), new ImportedShare []{}); // TODO: extraParams should be passed here and not ignored
991
996
}
992
997
993
998
public CompletableFuture <BigInteger > getMetadata (MetadataPubKey data ) {
@@ -1267,10 +1272,11 @@ public CompletableFuture<TorusKey> importPrivateKey(
1267
1272
BigInteger [] nodeIndexes ,
1268
1273
TorusNodePub [] nodePubKeys ,
1269
1274
String verifier ,
1270
- HashMap <String , Object > verifierParams ,
1275
+ HashMap <String , Object > verifierParams , // TODO: This must be strongly typed to VerifierParams
1271
1276
String idToken ,
1272
1277
String newPrivateKey ,
1273
- TorusUtilsExtraParams extraParams ) throws Exception {
1278
+ TorusUtilsExtraParams extraParams // TODO: This must have a default value
1279
+ ) throws Exception {
1274
1280
1275
1281
if (endpoints .length != nodeIndexes .length ) {
1276
1282
CompletableFuture <TorusKey > future = new CompletableFuture <>();
0 commit comments