forked from hyperledger/besu
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SECP256R1 support (hyperledger#2008)
Add SECP256R1 support Signed-off-by: Daniel Lehrner <daniel@io.builders>
- Loading branch information
1 parent
df6912a
commit 8a178a4
Showing
20 changed files
with
839 additions
and
346 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
68 changes: 68 additions & 0 deletions
68
...sts/dsl/src/main/java/org/hyperledger/besu/tests/acceptance/dsl/transaction/SignUtil.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,68 @@ | ||
/* | ||
* 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.tests.acceptance.dsl.transaction; | ||
|
||
import org.hyperledger.besu.crypto.SECPPrivateKey; | ||
import org.hyperledger.besu.crypto.SECPSignature; | ||
import org.hyperledger.besu.crypto.SignatureAlgorithm; | ||
import org.hyperledger.besu.tests.acceptance.dsl.account.Account; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.tuweni.bytes.Bytes32; | ||
import org.web3j.crypto.Credentials; | ||
import org.web3j.crypto.RawTransaction; | ||
import org.web3j.crypto.Sign; | ||
import org.web3j.crypto.TransactionEncoder; | ||
import org.web3j.rlp.RlpEncoder; | ||
import org.web3j.rlp.RlpList; | ||
import org.web3j.rlp.RlpType; | ||
import org.web3j.utils.Numeric; | ||
|
||
public class SignUtil { | ||
|
||
private SignUtil() {} | ||
|
||
public static String signTransaction( | ||
final RawTransaction transaction, | ||
final Account sender, | ||
final SignatureAlgorithm signatureAlgorithm) { | ||
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( | ||
// In Ethereum transaction 27 is added to recId (v) | ||
// See https://ethereum.github.io/yellowpaper/paper.pdf | ||
// Appendix F. Signing Transactions (281) | ||
(byte) (secpSignature.getRecId() + 27), | ||
secpSignature.getR().toByteArray(), | ||
secpSignature.getS().toByteArray()); | ||
List<RlpType> values = TransactionEncoder.asRlpValues(transaction, signature); | ||
RlpList rlpList = new RlpList(values); | ||
final byte[] encodedSignedTransaction = RlpEncoder.encode(rlpList); | ||
|
||
return Numeric.toHexString(encodedSignedTransaction); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
.../hyperledger/besu/tests/acceptance/dsl/transaction/TransactionWithSignatureAlgorithm.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,23 @@ | ||
/* | ||
* 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.tests.acceptance.dsl.transaction; | ||
|
||
import org.hyperledger.besu.crypto.SignatureAlgorithm; | ||
|
||
@FunctionalInterface | ||
public interface TransactionWithSignatureAlgorithm<T> { | ||
T execute(final NodeRequests node, final SignatureAlgorithm signatureAlgorithm); | ||
} |
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
57 changes: 57 additions & 0 deletions
57
...s/src/test/java/org/hyperledger/besu/tests/acceptance/crypto/SECP256R1AcceptanceTest.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,57 @@ | ||
/* | ||
* 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.tests.acceptance.crypto; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.hyperledger.besu.crypto.SECP256R1; | ||
import org.hyperledger.besu.ethereum.core.Hash; | ||
import org.hyperledger.besu.tests.acceptance.dsl.AcceptanceTestBase; | ||
import org.hyperledger.besu.tests.acceptance.dsl.account.Account; | ||
import org.hyperledger.besu.tests.acceptance.dsl.node.Node; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class SECP256R1AcceptanceTest extends AcceptanceTestBase { | ||
private Node minerNode; | ||
private Node fullNode; | ||
|
||
protected static final String GENESIS_FILE = "/crypto/secp256r1.json"; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
minerNode = besu.createCustomGenesisNode("node1", GENESIS_FILE, true, true); | ||
fullNode = besu.createCustomGenesisNode("node2", GENESIS_FILE, false); | ||
cluster.start(minerNode, fullNode); | ||
} | ||
|
||
@Test | ||
public void shouldConnectToOtherPeer() { | ||
minerNode.verify(net.awaitPeerCount(1)); | ||
fullNode.verify(net.awaitPeerCount(1)); | ||
} | ||
|
||
@Test | ||
public void transactionShouldBeSuccessful() { | ||
final Account recipient = accounts.createAccount("recipient"); | ||
|
||
final Hash transactionHash = | ||
minerNode.execute(accountTransactions.createTransfer(recipient, 5), new SECP256R1()); | ||
assertThat(transactionHash).isNotNull(); | ||
cluster.verify(recipient.balanceEquals(5)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
acceptance-tests/tests/src/test/resources/crypto/secp256r1.json
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,23 @@ | ||
{ | ||
"config": { | ||
"chainId": 1981, | ||
"ecCurve": "secp256r1", | ||
"constantinoplefixblock": 0, | ||
"ethash": { | ||
"fixeddifficulty": 1000 | ||
} | ||
}, | ||
"nonce": "0x0", | ||
"timestamp": "0x58ee40ba", | ||
"gasLimit": "0x1fffffffffffff", | ||
"difficulty": "0x1", | ||
"mixHash": "0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365", | ||
"alloc": { | ||
"91240f5b6994c7ed80f9f94b1aa847880ad3b150": { | ||
"privateKey": "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63", | ||
"info": "This genesis file uses SECP256R1 as elliptic curve. The address is only valid for this curve and invalid with the default SECP256K1 curve.", | ||
"comment": "private key and this comment are ignored. In a real chain, the private key should NOT be stored", | ||
"balance": "0xad78ebc5ac6200000" | ||
} | ||
} | ||
} |
Oops, something went wrong.