forked from hyperledger/besu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed elliptic curve configuration in genesis file (hyperledger#2066)
* refactored code to always set an instance in SignatureAlgorithmFactory in BesuCommand Signed-off-by: Daniel Lehrner <daniel@io.builders>
- Loading branch information
1 parent
7c8633a
commit 17cf72b
Showing
10 changed files
with
305 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
crypto/src/main/java/org/hyperledger/besu/crypto/SignatureAlgorithmType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.crypto; | ||
|
||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
public class SignatureAlgorithmType { | ||
|
||
private static final Map<String, Supplier<SignatureAlgorithm>> SUPPORTED_ALGORITHMS = | ||
Map.of("secp256k1", SECP256K1::new); | ||
|
||
public static final Supplier<SignatureAlgorithm> DEFAULT_SIGNATURE_ALGORITHM_TYPE = | ||
SUPPORTED_ALGORITHMS.get("secp256k1"); | ||
|
||
private final Supplier<SignatureAlgorithm> instantiator; | ||
|
||
private SignatureAlgorithmType(final Supplier<SignatureAlgorithm> instantiator) { | ||
this.instantiator = instantiator; | ||
} | ||
|
||
public static SignatureAlgorithmType create(final String ecCurve) | ||
throws IllegalArgumentException { | ||
if (!isValidType(ecCurve)) { | ||
throw new IllegalArgumentException( | ||
new StringBuilder() | ||
.append("Invalid genesis file configuration. Elliptic curve (ecCurve) ") | ||
.append(ecCurve) | ||
.append(" is not in the list of valid elliptic curves ") | ||
.append(getEcCurvesListAsString()) | ||
.toString()); | ||
} | ||
|
||
return new SignatureAlgorithmType(SUPPORTED_ALGORITHMS.get(ecCurve)); | ||
} | ||
|
||
public static SignatureAlgorithmType createDefault() { | ||
return new SignatureAlgorithmType(DEFAULT_SIGNATURE_ALGORITHM_TYPE); | ||
} | ||
|
||
public SignatureAlgorithm getInstance() { | ||
return instantiator.get(); | ||
} | ||
|
||
public static boolean isValidType(final String ecCurve) { | ||
return SUPPORTED_ALGORITHMS.containsKey(ecCurve); | ||
} | ||
|
||
private static String getEcCurvesListAsString() { | ||
Iterator<Map.Entry<String, Supplier<SignatureAlgorithm>>> it = | ||
SUPPORTED_ALGORITHMS.entrySet().iterator(); | ||
|
||
StringBuilder ecCurveListBuilder = new StringBuilder(); | ||
ecCurveListBuilder.append("["); | ||
|
||
while (it.hasNext()) { | ||
ecCurveListBuilder.append(it.next().getKey()); | ||
|
||
if (it.hasNext()) { | ||
ecCurveListBuilder.append(", "); | ||
} | ||
} | ||
ecCurveListBuilder.append("]"); | ||
|
||
return ecCurveListBuilder.toString(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
crypto/src/test/java/org/hyperledger/besu/crypto/SignatureAlgorithmFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.crypto; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import org.junit.Test; | ||
|
||
public class SignatureAlgorithmFactoryTest { | ||
|
||
@Test | ||
public void shouldReturnSECP256K1InstanceByDefault() { | ||
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance(); | ||
assertThat(signatureAlgorithm.getClass().getSimpleName()) | ||
.isEqualTo(SECP256K1.class.getSimpleName()); | ||
} | ||
|
||
@Test | ||
public void shouldReturnSECP256K1InstanceWhenSet() { | ||
SignatureAlgorithmFactory.setInstance(SignatureAlgorithmType.create("secp256k1")); | ||
|
||
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithmFactory.getInstance(); | ||
assertThat(signatureAlgorithm.getClass().getSimpleName()) | ||
.isEqualTo(SECP256K1.class.getSimpleName()); | ||
} | ||
|
||
@Test(expected = RuntimeException.class) | ||
public void shouldThrowExceptionWhenSetMoreThanOnce() { | ||
SignatureAlgorithmFactory.setInstance(SignatureAlgorithmType.create("secp256k1")); | ||
assertThat(SignatureAlgorithmFactory.isInstanceSet()).isTrue(); | ||
|
||
SignatureAlgorithmFactory.setInstance(SignatureAlgorithmType.create("secp256k1")); | ||
} | ||
} |
Oops, something went wrong.