Search in sources :

Example 1 with SECPPrivateKey

use of org.hyperledger.besu.crypto.SECPPrivateKey in project besu by hyperledger.

the class PublicKeySubCommandTest method callingPublicKeyExportAddressSubCommandWithPrivateKeyFileMustWriteKeyToStandardOutput.

@Test
public void callingPublicKeyExportAddressSubCommandWithPrivateKeyFileMustWriteKeyToStandardOutput() throws IOException {
    final SECPPrivateKey privateKey = SECPPrivateKey.create(Bytes32.fromHexString("0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63"), ALGORITHM);
    final KeyPair keyPair = KeyPair.create(privateKey, curve, ALGORITHM);
    final Path privateKeyFile = Files.createTempFile("private", "address");
    Files.writeString(privateKeyFile, privateKey.toString());
    parseCommand(PUBLIC_KEY_SUBCOMMAND_NAME, PUBLIC_KEY_EXPORT_ADDRESS_SUBCOMMAND_NAME, "--node-private-key-file", privateKeyFile.toString());
    final String expectedOutputStart = Util.publicKeyToAddress(keyPair.getPublicKey()).toString();
    assertThat(commandOutput.toString(UTF_8)).startsWith(expectedOutputStart);
    assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}
Also used : Path(java.nio.file.Path) KeyPair(org.hyperledger.besu.crypto.KeyPair) SECPPrivateKey(org.hyperledger.besu.crypto.SECPPrivateKey) Test(org.junit.Test)

Example 2 with SECPPrivateKey

use of org.hyperledger.besu.crypto.SECPPrivateKey in project besu by hyperledger.

the class PublicKeySubCommandTest method callingPublicKeyExportAddressSubCommandWithoutEcCurveNameDoesNotConfiguresSignatureAlgorithmFactory.

@Test
public void callingPublicKeyExportAddressSubCommandWithoutEcCurveNameDoesNotConfiguresSignatureAlgorithmFactory() throws Exception {
    assertThat(SignatureAlgorithmFactory.isInstanceSet()).isFalse();
    final SECPPrivateKey privateKey = SECPPrivateKey.create(Bytes32.fromHexString("0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63"), ALGORITHM);
    final Path privateKeyFile = Files.createTempFile("private", "address");
    Files.writeString(privateKeyFile, privateKey.toString());
    parseCommand(PUBLIC_KEY_SUBCOMMAND_NAME, PUBLIC_KEY_EXPORT_ADDRESS_SUBCOMMAND_NAME, "--node-private-key-file", privateKeyFile.toString());
    assertThat(SignatureAlgorithmFactory.isInstanceSet()).isFalse();
}
Also used : Path(java.nio.file.Path) SECPPrivateKey(org.hyperledger.besu.crypto.SECPPrivateKey) Test(org.junit.Test)

Example 3 with SECPPrivateKey

use of org.hyperledger.besu.crypto.SECPPrivateKey in project besu by hyperledger.

the class OperatorSubCommandTest method checkPublicKey.

private void checkPublicKey(final File dir, final SignatureAlgorithm signatureAlgorithm) throws IOException {
    String publicKeyHex = readPubFile(dir);
    String privateKeyHex = readPrivFile(dir);
    SECPPrivateKey privateKey = signatureAlgorithm.createPrivateKey(Bytes32.fromHexString(privateKeyHex));
    SECPPublicKey expectedPublicKey = signatureAlgorithm.createPublicKey(privateKey);
    assertThat(publicKeyHex).isEqualTo(expectedPublicKey.getEncodedBytes().toHexString());
}
Also used : SECPPrivateKey(org.hyperledger.besu.crypto.SECPPrivateKey) SECPPublicKey(org.hyperledger.besu.crypto.SECPPublicKey)

Example 4 with SECPPrivateKey

use of org.hyperledger.besu.crypto.SECPPrivateKey in project besu by hyperledger.

the class TestSigningPrivateMarkerTransactionFactory method setSigningKeyEnbaled.

public void setSigningKeyEnbaled(final String privateMarkerTransactionSigningKey) {
    final SignatureAlgorithm algorithm = SignatureAlgorithmFactory.getInstance();
    final SECPPrivateKey privateKey = algorithm.createPrivateKey(Bytes32.fromHexString(privateMarkerTransactionSigningKey));
    aliceFixedSigningKey = algorithm.createKeyPair(privateKey);
    sender = extract(Hash.hash(aliceFixedSigningKey.getPublicKey().getEncodedBytes()));
}
Also used : SECPPrivateKey(org.hyperledger.besu.crypto.SECPPrivateKey) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm)

Example 5 with SECPPrivateKey

use of org.hyperledger.besu.crypto.SECPPrivateKey in project besu by hyperledger.

the class SignUtil method signTransaction.

public static byte[] signTransaction(final RawTransaction transaction, final Account sender, final SignatureAlgorithm signatureAlgorithm, final Optional<BigInteger> chainId) {
    byte[] encodedTransaction = TransactionEncoder.encode(transaction);
    Credentials credentials = sender.web3jCredentialsOrThrow();
    SECPPrivateKey privateKey = signatureAlgorithm.createPrivateKey(credentials.getEcKeyPair().getPrivateKey());
    byte[] transactionHash = org.web3j.crypto.Hash.sha3(encodedTransaction);
    SECPSignature secpSignature = signatureAlgorithm.sign(Bytes32.wrap(transactionHash), signatureAlgorithm.createKeyPair(privateKey));
    Sign.SignatureData signature = new Sign.SignatureData(calculateV(secpSignature, chainId), secpSignature.getR().toByteArray(), secpSignature.getS().toByteArray());
    List<RlpType> values = getTxRlpValues(transaction, signature, secpSignature);
    RlpList rlpList = new RlpList(values);
    byte[] encoded = RlpEncoder.encode(rlpList);
    if (transaction.getType().equals(TransactionType.LEGACY)) {
        return encoded;
    }
    return ByteBuffer.allocate(encoded.length + 1).put(transaction.getType().getRlpType()).put(encoded).array();
}
Also used : SECPSignature(org.hyperledger.besu.crypto.SECPSignature) SECPPrivateKey(org.hyperledger.besu.crypto.SECPPrivateKey) Sign(org.web3j.crypto.Sign) RlpType(org.web3j.rlp.RlpType) RlpList(org.web3j.rlp.RlpList) Credentials(org.web3j.crypto.Credentials)

Aggregations

SECPPrivateKey (org.hyperledger.besu.crypto.SECPPrivateKey)9 Path (java.nio.file.Path)5 Test (org.junit.Test)5 KeyPair (org.hyperledger.besu.crypto.KeyPair)3 SECPSignature (org.hyperledger.besu.crypto.SECPSignature)2 SignatureAlgorithm (org.hyperledger.besu.crypto.SignatureAlgorithm)2 Stopwatch (com.google.common.base.Stopwatch)1 BigInteger (java.math.BigInteger)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 SECPPublicKey (org.hyperledger.besu.crypto.SECPPublicKey)1 Credentials (org.web3j.crypto.Credentials)1 Sign (org.web3j.crypto.Sign)1 RlpList (org.web3j.rlp.RlpList)1 RlpType (org.web3j.rlp.RlpType)1