mirror of https://github.com/hyperledger/besu
Fixed elliptic curve configuration in genesis file (#2066)
* refactored code to always set an instance in SignatureAlgorithmFactory in BesuCommand Signed-off-by: Daniel Lehrner <daniel@io.builders>pull/2067/head
parent
7c8633ad5a
commit
17cf72b916
@ -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(); |
||||
} |
||||
} |
@ -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")); |
||||
} |
||||
} |
@ -0,0 +1,38 @@ |
||||
/* |
||||
* 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.Assertions.assertThatThrownBy; |
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
public class SignatureAlgorithmTypeTest { |
||||
@Test |
||||
public void shouldReturnSECP256K1Instance() { |
||||
SignatureAlgorithmType signatureAlgorithmType = SignatureAlgorithmType.create("secp256k1"); |
||||
|
||||
assertThat(signatureAlgorithmType.getInstance().getClass().getSimpleName()) |
||||
.isEqualTo(SECP256K1.class.getSimpleName()); |
||||
} |
||||
|
||||
@Test |
||||
public void shouldThrowExceptionWhenInvalidParameterIsGiven() { |
||||
assertThatThrownBy(() -> SignatureAlgorithmType.create("abcd")) |
||||
.hasMessage( |
||||
"Invalid genesis file configuration. Elliptic curve (ecCurve) abcd is not in the list" |
||||
+ " of valid elliptic curves [secp256k1]"); |
||||
} |
||||
} |
Loading…
Reference in new issue