mirror of https://github.com/hyperledger/besu
Separate NodeKey from implementation (#690)
This change allows the newly defined "SecurityModule" to work against a generic API, while the NodeKey interface adapts these generic types to the types already used throughout Besu (i.e. those defined in Secp256k1). Signed-off-by: Trent Mohay <trent.mohay@consensys.net>pull/704/head
parent
36bdae457b
commit
57d0379bf5
@ -0,0 +1,34 @@ |
||||
/* |
||||
* 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 org.apache.tuweni.bytes.Bytes; |
||||
|
||||
public class PublicKey { |
||||
|
||||
private final Bytes encoded; |
||||
|
||||
public PublicKey(final Bytes encoded) { |
||||
this.encoded = encoded; |
||||
} |
||||
|
||||
public static PublicKey create(final Bytes encoded) { |
||||
return new PublicKey(encoded); |
||||
} |
||||
|
||||
public Bytes getEncoded() { |
||||
return encoded; |
||||
} |
||||
} |
@ -0,0 +1,40 @@ |
||||
/* |
||||
* 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 org.apache.tuweni.bytes.Bytes32; |
||||
|
||||
/** |
||||
* Provides a generic interface for classes which wrap/hide a cryptographic private key. This |
||||
* interface ensures cryptographic functions required by Ethereum are available to the application |
||||
* at large, without releasing the content of the private key. |
||||
*/ |
||||
public interface SecurityModule { |
||||
|
||||
/** |
||||
* @param dataHash The Keccack hash of a set of data, which is to be signed. |
||||
* @return the signature (R, S, recId) generated by signing the hash with the node key |
||||
*/ |
||||
Signature sign(Bytes32 dataHash); |
||||
|
||||
/** @return the public key associated with the key stored behind this interface. */ |
||||
PublicKey getPublicKey(); |
||||
|
||||
/** |
||||
* @param partyKey the key with which an agreement is to be created. |
||||
* @return The bytes forming the agreement |
||||
*/ |
||||
Bytes32 calculateECDHKeyAgreement(PublicKey partyKey); |
||||
} |
@ -0,0 +1,51 @@ |
||||
/* |
||||
* 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.math.BigInteger; |
||||
|
||||
public final class Signature { |
||||
|
||||
/** |
||||
* The recovery id to reconstruct the public key used to create the signature. |
||||
* |
||||
* <p>The recId is an index from 0 to 3 which indicates which of the 4 possible keys is the |
||||
* correct one. Because the key recovery operation yields multiple potential keys, the correct key |
||||
* must either be stored alongside the signature, or you must be willing to try each recId in turn |
||||
* until you find one that outputs the key you are expecting. |
||||
*/ |
||||
private final byte recoveryId; |
||||
|
||||
private final BigInteger r; |
||||
private final BigInteger s; |
||||
|
||||
public Signature(final BigInteger r, final BigInteger s, final byte recoveryId) { |
||||
this.r = r; |
||||
this.s = s; |
||||
this.recoveryId = recoveryId; |
||||
} |
||||
|
||||
public byte getRecoveryId() { |
||||
return recoveryId; |
||||
} |
||||
|
||||
public BigInteger getR() { |
||||
return r; |
||||
} |
||||
|
||||
public BigInteger getS() { |
||||
return s; |
||||
} |
||||
} |
@ -0,0 +1,26 @@ |
||||
/* |
||||
* 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; |
||||
|
||||
public class NodeKeyUtils { |
||||
|
||||
public static NodeKey createFrom(final SECP256K1.KeyPair keyPair) { |
||||
return new NodeKey(new BouncyCastleSecurityModule(keyPair)); |
||||
} |
||||
|
||||
public static NodeKey generate() { |
||||
return new NodeKey(new BouncyCastleSecurityModule(SECP256K1.KeyPair.generate())); |
||||
} |
||||
} |
Loading…
Reference in new issue