Skip to content

Commit

Permalink
Use SignerIdentifier instead of ECPublicKey to load Signers in Signer…
Browse files Browse the repository at this point in the history
…Provider

* Use SignerIdentifier (can be Eth SECP address or ECPublicKey etc.) to use to load signer.
  • Loading branch information
usmansaleem authored Nov 10, 2021
1 parent 0a1f917 commit 6d97c5e
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 66 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#Changelog

## 1.0.20
### Features Added
- SignerProvider to use SignerIdentifier instead of ECPublicKey to obtain Signers.

## 1.0.19
### Features Added
- Allow empty password files to be read when creating a Signer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static tech.pegasys.signers.secp256k1.MultiKeyTomlFileUtil.createAzureTomlFileAt;

import tech.pegasys.signers.secp256k1.EthPublicKeyUtils;
import tech.pegasys.signers.secp256k1.common.PublicKeySignerIdentifier;

import java.nio.file.Path;

Expand Down Expand Up @@ -52,7 +53,9 @@ void azureSignersAreCreatedAndExpectedAddressIsReported(@TempDir Path tomlDirect

setup(tomlDirectory);

assertThat(signerProvider.availablePublicKeys().stream().map(EthPublicKeyUtils::toHexString))
assertThat(
signerProvider.availablePublicKeys(PublicKeySignerIdentifier::new).stream()
.map(EthPublicKeyUtils::toHexString))
.containsOnly("0x" + PUBLIC_KEY_HEX_STRING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static tech.pegasys.signers.secp256k1.MultiKeyTomlFileUtil.createFileBasedTomlFileAt;

import tech.pegasys.signers.secp256k1.EthPublicKeyUtils;
import tech.pegasys.signers.secp256k1.common.PublicKeySignerIdentifier;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -50,7 +51,9 @@ void validFileBasedTomlFileProducesSignerWhichReportsMatchingAddress(@TempDir Pa

setup(tomlDirectory);

assertThat(signerProvider.availablePublicKeys().stream().map(EthPublicKeyUtils::toHexString))
assertThat(
signerProvider.availablePublicKeys(PublicKeySignerIdentifier::new).stream()
.map(EthPublicKeyUtils::toHexString))
.containsOnly("0x" + PUBLIC_KEY_HEX_STRING);
}

Expand All @@ -71,7 +74,9 @@ void validFileBasedTomlFileWithMultineLinePasswordFileProducesSignerWhichReports

setup(tomlDirectory);

assertThat(signerProvider.availablePublicKeys().stream().map(EthPublicKeyUtils::toHexString))
assertThat(
signerProvider.availablePublicKeys(PublicKeySignerIdentifier::new).stream()
.map(EthPublicKeyUtils::toHexString))
.containsOnly("0x" + PUBLIC_KEY_HEX_STRING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import tech.pegasys.signers.secp256k1.EthPublicKeyUtils;
import tech.pegasys.signers.secp256k1.HashicorpSigningParams;
import tech.pegasys.signers.secp256k1.common.PublicKeySignerIdentifier;

import java.nio.file.Path;

Expand All @@ -42,7 +43,9 @@ void hashicorpSignerIsCreatedAndExpectedAddressIsReported(@TempDir final Path te
createHashicorpTomlFileAt(tempDir.resolve(PUBLIC_KEY_HEX_STRING + ".toml"), hashicorpNode);
setup(tempDir);

assertThat(signerProvider.availablePublicKeys().stream().map(EthPublicKeyUtils::toHexString))
assertThat(
signerProvider.availablePublicKeys(PublicKeySignerIdentifier::new).stream()
.map(EthPublicKeyUtils::toHexString))
.containsOnly("0x" + PUBLIC_KEY_HEX_STRING);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import tech.pegasys.signers.secp256k1.EthPublicKeyUtils;
import tech.pegasys.signers.secp256k1.HashicorpSigningParams;
import tech.pegasys.signers.secp256k1.common.PublicKeySignerIdentifier;

import java.nio.file.Path;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -44,7 +45,7 @@ void hashicorpSignerIsCreatedAndExpectedAddressIsReported(@TempDir final Path te
createHashicorpTomlFileAt(tempDir.resolve(PUBLIC_KEY_HEX_STRING + ".toml"), hashicorpNode);
setup(tempDir);
assertThat(
signerProvider.availablePublicKeys().stream()
signerProvider.availablePublicKeys(PublicKeySignerIdentifier::new).stream()
.map(EthPublicKeyUtils::toByteArray)
.collect(Collectors.toList()))
.containsOnly(Numeric.hexStringToByteArray(PUBLIC_KEY_HEX_STRING));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/
package tech.pegasys.signers.secp256k1.tests.multikey;

import tech.pegasys.signers.secp256k1.DefaultFileSelector;
import tech.pegasys.signers.secp256k1.AllTomlConfigFilesSelector;
import tech.pegasys.signers.secp256k1.SignerIdentifierConfigFileSelector;
import tech.pegasys.signers.secp256k1.multikey.MultiKeySignerProvider;

import java.nio.file.Path;
Expand All @@ -22,6 +23,10 @@ public class MultiKeyAcceptanceTestBase {
protected MultiKeySignerProvider signerProvider;

protected void setup(final Path tomlDirectory) {
this.signerProvider = MultiKeySignerProvider.create(tomlDirectory, new DefaultFileSelector());
this.signerProvider =
MultiKeySignerProvider.create(
tomlDirectory,
new AllTomlConfigFilesSelector(),
new SignerIdentifierConfigFileSelector());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import tech.pegasys.signers.secp256k1.EthPublicKeyUtils;
import tech.pegasys.signers.secp256k1.api.Signature;
import tech.pegasys.signers.secp256k1.api.Signer;
import tech.pegasys.signers.secp256k1.common.PublicKeySignerIdentifier;
import tech.pegasys.signers.secp256k1.tests.multikey.MultiKeyAcceptanceTestBase;

import java.math.BigInteger;
Expand All @@ -44,7 +45,7 @@ void verifySignature() {

void verifySignature(String publicKeyHex) {
ECPublicKey pubKey = EthPublicKeyUtils.createPublicKey(Bytes.fromHexString(publicKeyHex));
final Optional<Signer> signer = signerProvider.getSigner(pubKey);
final Optional<Signer> signer = signerProvider.getSigner(new PublicKeySignerIdentifier(pubKey));
assertThat(signer).isNotEmpty();

final Signature signature = signer.get().sign(DATA_TO_SIGN);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 ConsenSys AG.
* Copyright 2021 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
Expand All @@ -14,23 +14,12 @@

import tech.pegasys.signers.secp256k1.api.FileSelector;

import java.nio.file.DirectoryStream.Filter;
import java.nio.file.DirectoryStream;
import java.nio.file.Path;
import java.security.interfaces.ECPublicKey;

public class DefaultFileSelector implements FileSelector<ECPublicKey> {

public class AllTomlConfigFilesSelector implements FileSelector<Void> {
@Override
public Filter<Path> getAllConfigFilesFilter() {
public DirectoryStream.Filter<Path> getConfigFilesFilter(final Void selectionCriteria) {
return entry -> entry.getFileName().toString().endsWith("toml");
}

@Override
public Filter<Path> getSpecificConfigFileFilter(final ECPublicKey publicKey) {
return entry -> {
final String filename =
EthPublicKeyUtils.toHexString(publicKey).substring(2); // remove 0x prefix
return entry.getFileName().toString().equals(filename + ".toml");
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2021 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.
*/
package tech.pegasys.signers.secp256k1;

import tech.pegasys.signers.secp256k1.api.FileSelector;
import tech.pegasys.signers.secp256k1.api.SignerIdentifier;

import java.nio.file.DirectoryStream;
import java.nio.file.Path;

public class SignerIdentifierConfigFileSelector implements FileSelector<SignerIdentifier> {

@Override
public DirectoryStream.Filter<Path> getConfigFilesFilter(
final SignerIdentifier signerIdentifier) {
return entry ->
entry.getFileName().toString().equals(signerIdentifier.toStringIdentifier() + ".toml");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,5 @@
convention.
*/
public interface FileSelector<T> {

Filter<Path> getAllConfigFilesFilter();

Filter<Path> getSpecificConfigFileFilter(T selectionCriterion);
Filter<Path> getConfigFilesFilter(T selectionCriteria);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2021 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.
*/
package tech.pegasys.signers.secp256k1.api;

import java.security.interfaces.ECPublicKey;

public interface SignerIdentifier {
String toStringIdentifier();

boolean validate(ECPublicKey publicKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import java.security.interfaces.ECPublicKey;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

public interface SignerProvider {

Optional<Signer> getSigner(ECPublicKey publicKey);
Optional<Signer> getSigner(SignerIdentifier publicKey);

Set<ECPublicKey> availablePublicKeys();
Set<ECPublicKey> availablePublicKeys(Function<ECPublicKey, SignerIdentifier> identifierFunction);

default void shutdown() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

public class SingleSignerProvider implements SignerProvider {

Expand All @@ -29,16 +30,19 @@ public SingleSignerProvider(final Signer signer) {
}

@Override
public Optional<Signer> getSigner(final ECPublicKey publicKey) {
if ((signer.getPublicKey() != null) && signer.getPublicKey().getW().equals(publicKey.getW())) {
return Optional.of(signer);
} else {
public Optional<Signer> getSigner(final SignerIdentifier signerIdentifier) {
if (signerIdentifier == null) {
return Optional.empty();
}

return signerIdentifier.validate(signer.getPublicKey())
? Optional.of(signer)
: Optional.empty();
}

@Override
public Set<ECPublicKey> availablePublicKeys() {
public Set<ECPublicKey> availablePublicKeys(
final Function<ECPublicKey, SignerIdentifier> identifierFunction) {
return signer.getPublicKey() != null ? Set.of(signer.getPublicKey()) : Collections.emptySet();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2021 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.
*/
package tech.pegasys.signers.secp256k1.api.util;

import java.util.Locale;

public class AddressUtil {
public static String remove0xPrefix(final String address) {
if (address == null) {
return null;
}

return address.toLowerCase(Locale.US).startsWith("0x") ? address.substring(2) : address;
}
}
Loading

0 comments on commit 6d97c5e

Please sign in to comment.