@@ -412,67 +412,69 @@ public static void setParam(final Config config) {
412
412
PARAMETER .cryptoEngine = config .hasPath (Constant .CRYPTO_ENGINE ) ? config
413
413
.getString (Constant .CRYPTO_ENGINE ) : Constant .ECKey_ENGINE ;
414
414
415
- if ( StringUtils . isNoneBlank ( PARAMETER . privateKey )) {
416
- localWitnesses = ( new LocalWitnesses ( PARAMETER .privateKey ));
415
+ localWitnesses = new LocalWitnesses ();
416
+ if ( PARAMETER .isWitness ()) {
417
417
byte [] witnessAddress = null ;
418
- if (StringUtils .isNoneBlank (PARAMETER .witnessAddress )) {
419
- witnessAddress = Commons .decodeFromBase58Check (PARAMETER .witnessAddress );
420
- if (witnessAddress != null ) {
421
- logger .debug ("Got localWitnessAccountAddress from cmd" );
422
- } else {
423
- PARAMETER .witnessAddress = "" ;
424
- logger .warn (IGNORE_WRONG_WITNESS_ADDRESS_FORMAT );
425
- }
426
- }
427
- localWitnesses .initWitnessAccountAddress (witnessAddress , PARAMETER .isECKeyCryptoEngine ());
428
- logger .debug ("Got privateKey from cmd" );
429
- } else if (config .hasPath (Constant .LOCAL_WITNESS )) {
430
- localWitnesses = new LocalWitnesses ();
431
- byte [] witnessAddress = getWitnessAddress (config );
432
- List <String > localwitness = config .getStringList (Constant .LOCAL_WITNESS );
433
- if (!localwitness .isEmpty ()) {
434
- localWitnesses .setPrivateKeys (localwitness );
435
- logger .debug ("Got privateKey from config.conf" );
436
- }
437
- localWitnesses .initWitnessAccountAddress (witnessAddress , PARAMETER .isECKeyCryptoEngine ());
438
- } else if (config .hasPath (Constant .LOCAL_WITNESS_KEYSTORE )) {
439
- localWitnesses = new LocalWitnesses ();
440
- List <String > privateKeys = new ArrayList <String >();
441
- if (PARAMETER .isWitness ()) {
442
- List <String > localwitness = config .getStringList (Constant .LOCAL_WITNESS_KEYSTORE );
443
- if (!localwitness .isEmpty ()) {
444
- String fileName = System .getProperty ("user.dir" ) + "/" + localwitness .get (0 );
445
- String password ;
446
- if (StringUtils .isEmpty (PARAMETER .password )) {
447
- System .out .println ("Please input your password." );
448
- password = WalletUtils .inputPassword ();
418
+ if (StringUtils .isNoneBlank (PARAMETER .privateKey )) {
419
+ localWitnesses = new LocalWitnesses (PARAMETER .privateKey );
420
+ if (StringUtils .isNoneBlank (PARAMETER .witnessAddress )) {
421
+ witnessAddress = Commons .decodeFromBase58Check (PARAMETER .witnessAddress );
422
+ if (witnessAddress != null ) {
423
+ logger .debug ("Got localWitnessAccountAddress from cmd" );
449
424
} else {
450
- password = PARAMETER . password ;
451
- PARAMETER . password = null ;
425
+ throw new TronError ( "LocalWitnessAccountAddress format from cmd is incorrect" ,
426
+ TronError . ErrCode . WITNESS_INIT ) ;
452
427
}
453
-
454
- try {
455
- Credentials credentials = WalletUtils
456
- .loadCredentials (password , new File (fileName ));
457
- SignInterface sign = credentials .getSignInterface ();
458
- String prikey = ByteArray .toHexString (sign .getPrivateKey ());
459
- privateKeys .add (prikey );
460
- } catch (IOException | CipherException e ) {
461
- logger .error ("Witness node start failed!" );
462
- throw new TronError (e , TronError .ErrCode .WITNESS_KEYSTORE_LOAD );
428
+ }
429
+ localWitnesses .initWitnessAccountAddress (witnessAddress , PARAMETER .isECKeyCryptoEngine ());
430
+ logger .debug ("Got privateKey from cmd" );
431
+ } else if (config .hasPath (Constant .LOCAL_WITNESS ) && !config .getStringList (
432
+ Constant .LOCAL_WITNESS ).isEmpty ()) {
433
+ List <String > localWitness = config .getStringList (Constant .LOCAL_WITNESS );
434
+ localWitnesses .setPrivateKeys (localWitness );
435
+ logger .debug ("Got privateKey from config.conf" );
436
+ witnessAddress = getWitnessAddress (config );
437
+ localWitnesses .initWitnessAccountAddress (witnessAddress , PARAMETER .isECKeyCryptoEngine ());
438
+ } else if (config .hasPath (Constant .LOCAL_WITNESS_KEYSTORE )) {
439
+ List <String > privateKeys = new ArrayList <String >();
440
+ List <String > localWitness = config .getStringList (Constant .LOCAL_WITNESS_KEYSTORE );
441
+ if (!localWitness .isEmpty ()) {
442
+ int i = 1 ;
443
+ System .out .printf ("There are %d keystore in the config.%n" , localWitness .size ());
444
+ for (String keystore : localWitness ) {
445
+ String fileName = System .getProperty ("user.dir" ) + "/" + keystore ;
446
+ String password ;
447
+ if (StringUtils .isEmpty (PARAMETER .password )) {
448
+ System .out .printf ("Please input your %d-th keystore password: %n" , i );
449
+ password = WalletUtils .inputPassword ();
450
+ } else {
451
+ password = PARAMETER .password ;
452
+ PARAMETER .password = null ;
453
+ }
454
+
455
+ try {
456
+ Credentials credentials = WalletUtils
457
+ .loadCredentials (password , new File (fileName ));
458
+ SignInterface sign = credentials .getSignInterface ();
459
+ String prikey = ByteArray .toHexString (sign .getPrivateKey ());
460
+ privateKeys .add (prikey );
461
+ } catch (IOException | CipherException e ) {
462
+ logger .error ("Witness node start failed!" );
463
+ throw new TronError (e , TronError .ErrCode .WITNESS_KEYSTORE_LOAD );
464
+ }
465
+ i ++;
463
466
}
467
+ localWitnesses .setPrivateKeys (privateKeys );
468
+ witnessAddress = getWitnessAddress (config );
469
+ localWitnesses .initWitnessAccountAddress (witnessAddress , PARAMETER .isECKeyCryptoEngine ());
470
+ logger .debug ("Got privateKey from keystore" );
464
471
}
465
472
}
466
- localWitnesses .setPrivateKeys (privateKeys );
467
- byte [] witnessAddress = getWitnessAddress (config );
468
- localWitnesses .initWitnessAccountAddress (witnessAddress , PARAMETER .isECKeyCryptoEngine ());
469
- logger .debug ("Got privateKey from keystore" );
470
- }
471
473
472
- if (PARAMETER . isWitness ()
473
- && CollectionUtils . isEmpty ( localWitnesses . getPrivateKeys ())) {
474
- throw new TronError ( "This is a witness node, but localWitnesses is null" ,
475
- TronError . ErrCode . WITNESS_INIT );
474
+ if (CollectionUtils . isEmpty ( localWitnesses . getPrivateKeys ())) {
475
+ throw new TronError ( "This is a witness node, but localWitnesses is null" ,
476
+ TronError . ErrCode . WITNESS_INIT );
477
+ }
476
478
}
477
479
478
480
if (config .hasPath (Constant .VM_SUPPORT_CONSTANT )) {
@@ -1822,12 +1824,18 @@ public static void logConfig() {
1822
1824
private static byte [] getWitnessAddress (Config config ) {
1823
1825
byte [] witnessAddress = null ;
1824
1826
if (config .hasPath (Constant .LOCAL_WITNESS_ACCOUNT_ADDRESS )) {
1827
+ if (localWitnesses .getPrivateKeys ().size () != 1 ) {
1828
+ throw new TronError (
1829
+ "LocalWitnessAccountAddress can only be set when there is only one private key" ,
1830
+ TronError .ErrCode .WITNESS_INIT );
1831
+ }
1825
1832
witnessAddress = Commons
1826
1833
.decodeFromBase58Check (config .getString (Constant .LOCAL_WITNESS_ACCOUNT_ADDRESS ));
1827
1834
if (witnessAddress != null ) {
1828
1835
logger .debug ("Got localWitnessAccountAddress from config.conf" );
1829
1836
} else {
1830
- logger .warn (IGNORE_WRONG_WITNESS_ADDRESS_FORMAT );
1837
+ throw new TronError ("LocalWitnessAccountAddress format from config is incorrect" ,
1838
+ TronError .ErrCode .WITNESS_INIT );
1831
1839
}
1832
1840
}
1833
1841
return witnessAddress ;
0 commit comments