-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/364 integretion with hsm #1602
Merged
AlexandrouR
merged 5 commits into
hyperledger-web3j:master
from
andrii-kl:feature/364_integretion_with_HSM
Jan 20, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fcfb408
364 integration with HMS
andrii-kl 5d641d2
364 Add unit tests, small refactoring, add java docs
andrii-kl a96d9d5
364 Add unit test CryptoUtilsTest, small refactoring
andrii-kl 9b697f6
364 fix misprint
andrii-kl a1323b2
364 delete empty test class
andrii-kl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2022 Web3 Labs Ltd. | ||
* | ||
* 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 org.web3j.dto; | ||
|
||
public class HSMHTTPRequestDTO { | ||
private String message; | ||
|
||
public HSMHTTPRequestDTO(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
core/src/main/java/org/web3j/service/HSMHTTPRequestProcessor.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,82 @@ | ||
/* | ||
* Copyright 2022 Web3 Labs Ltd. | ||
* | ||
* 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 org.web3j.service; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import okhttp3.MediaType; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.ResponseBody; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.web3j.crypto.CryptoUtils; | ||
import org.web3j.crypto.ECDSASignature; | ||
import org.web3j.crypto.HSMHTTPPass; | ||
import org.web3j.crypto.Sign; | ||
import org.web3j.protocol.exceptions.ClientConnectionException; | ||
import org.web3j.utils.Numeric; | ||
|
||
/** | ||
* Request processor to a HSM through the HTTP | ||
* | ||
* @param <T> Object with required parameters to perform request to a HSM | ||
*/ | ||
public abstract class HSMHTTPRequestProcessor<T extends HSMHTTPPass> | ||
implements HSMRequestProcessor<HSMHTTPPass> { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(HSMHTTPRequestProcessor.class); | ||
|
||
public static final MediaType JSON = MediaType.parse("application/json"); | ||
|
||
private final OkHttpClient client; | ||
|
||
public HSMHTTPRequestProcessor(OkHttpClient okHttpClient) { | ||
this.client = okHttpClient; | ||
} | ||
|
||
@Override | ||
public Sign.SignatureData callHSM(byte[] dataToSign, HSMHTTPPass pass) { | ||
Request request = createRequest(dataToSign, pass); | ||
|
||
try (okhttp3.Response response = client.newCall(request).execute()) { | ||
ResponseBody responseBody = response.body(); | ||
if (response.isSuccessful()) { | ||
if (responseBody != null) { | ||
String signHex = readResponse(responseBody.byteStream()); | ||
byte[] signBytes = Numeric.hexStringToByteArray(signHex); | ||
ECDSASignature signature = CryptoUtils.fromDerFormat(signBytes); | ||
|
||
return Sign.createSignatureData(signature, pass.getPublicKey(), dataToSign); | ||
} else { | ||
return null; | ||
} | ||
} else { | ||
int code = response.code(); | ||
String text = responseBody == null ? "N/A" : responseBody.string(); | ||
throw new ClientConnectionException( | ||
"Invalid response received: " + code + "; " + text); | ||
} | ||
} catch (IOException e) { | ||
log.error(e.getMessage(), e); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected abstract Request createRequest(byte[] dataToSign, HSMHTTPPass pass); | ||
|
||
protected abstract String readResponse(InputStream responseData); | ||
} |
33 changes: 33 additions & 0 deletions
33
core/src/main/java/org/web3j/service/HSMRequestProcessor.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,33 @@ | ||
/* | ||
* Copyright 2022 Web3 Labs Ltd. | ||
* | ||
* 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 org.web3j.service; | ||
|
||
import org.web3j.crypto.HSMPass; | ||
import org.web3j.crypto.Sign; | ||
|
||
/** | ||
* Request processor to a HSM (hardware security module). | ||
* | ||
* @param <T> Object with required parameters to perform request to a HSM. | ||
*/ | ||
public interface HSMRequestProcessor<T extends HSMPass> { | ||
|
||
/** | ||
* Call a HSM (hardware security module) | ||
* | ||
* @param dataToSign message hash to sign. | ||
* @param pass Object with required parameters to perform request to a HSM. | ||
* @return SignatureData v | r | s | ||
*/ | ||
Sign.SignatureData callHSM(byte[] dataToSign, T pass); | ||
} |
67 changes: 67 additions & 0 deletions
67
core/src/main/java/org/web3j/service/TxHSMSignService.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,67 @@ | ||
/* | ||
* Copyright 2022 Web3 Labs Ltd. | ||
* | ||
* 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 org.web3j.service; | ||
|
||
import org.web3j.crypto.HSMPass; | ||
import org.web3j.crypto.Hash; | ||
import org.web3j.crypto.RawTransaction; | ||
import org.web3j.crypto.Sign; | ||
import org.web3j.crypto.transaction.type.TransactionType; | ||
import org.web3j.tx.ChainId; | ||
|
||
import static org.web3j.crypto.TransactionEncoder.createEip155SignatureData; | ||
import static org.web3j.crypto.TransactionEncoder.encode; | ||
|
||
/** Service to sign transaction with HSM (hardware security module). */ | ||
public class TxHSMSignService<T extends HSMPass> implements TxSignService { | ||
|
||
private final T hsmPass; | ||
private final HSMRequestProcessor<T> hsmRequestProcessor; | ||
|
||
public TxHSMSignService(HSMRequestProcessor<T> hsmRequestProcessor, T hsmPass) { | ||
this.hsmPass = hsmPass; | ||
this.hsmRequestProcessor = hsmRequestProcessor; | ||
} | ||
|
||
@Override | ||
public byte[] sign(RawTransaction rawTransaction, long chainId) { | ||
byte[] finalBytes; | ||
byte[] encodedTransaction; | ||
Sign.SignatureData signatureData; | ||
boolean isNewTx = | ||
chainId > ChainId.NONE && rawTransaction.getType().equals(TransactionType.LEGACY); | ||
|
||
if (isNewTx) { | ||
encodedTransaction = encode(rawTransaction, chainId); | ||
} else { | ||
encodedTransaction = encode(rawTransaction); | ||
} | ||
|
||
byte[] messageHash = Hash.sha3(encodedTransaction); | ||
|
||
signatureData = hsmRequestProcessor.callHSM(messageHash, hsmPass); | ||
|
||
if (isNewTx) { | ||
signatureData = createEip155SignatureData(signatureData, chainId); | ||
} | ||
|
||
finalBytes = encode(rawTransaction, signatureData); | ||
|
||
return finalBytes; | ||
} | ||
|
||
@Override | ||
public String getAddress() { | ||
return hsmPass.getAddress(); | ||
} | ||
} |
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,35 @@ | ||
/* | ||
* Copyright 2022 Web3 Labs Ltd. | ||
* | ||
* 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 org.web3j.service; | ||
|
||
import org.web3j.crypto.RawTransaction; | ||
|
||
/** Service to sign transaction. */ | ||
public interface TxSignService { | ||
|
||
/** | ||
* Sign raw transaction. | ||
* | ||
* @param rawTransaction Raw transaction | ||
* @param chainId Ethereum chain id, -1 is NONE | ||
* @return Transaction signature | ||
*/ | ||
byte[] sign(RawTransaction rawTransaction, long chainId); | ||
|
||
/** | ||
* Get key address of the current wallet | ||
* | ||
* @return Wallet address | ||
*/ | ||
String getAddress(); | ||
} |
45 changes: 45 additions & 0 deletions
45
core/src/main/java/org/web3j/service/TxSignServiceImpl.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,45 @@ | ||
/* | ||
* Copyright 2022 Web3 Labs Ltd. | ||
* | ||
* 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 org.web3j.service; | ||
|
||
import org.web3j.crypto.Credentials; | ||
import org.web3j.crypto.RawTransaction; | ||
import org.web3j.crypto.TransactionEncoder; | ||
import org.web3j.tx.ChainId; | ||
|
||
/** Service to base sign transaction. */ | ||
public class TxSignServiceImpl implements TxSignService { | ||
|
||
private final Credentials credentials; | ||
|
||
public TxSignServiceImpl(Credentials credentials) { | ||
this.credentials = credentials; | ||
} | ||
|
||
@Override | ||
public byte[] sign(RawTransaction rawTransaction, long chainId) { | ||
final byte[] signedMessage; | ||
|
||
if (chainId > ChainId.NONE) { | ||
signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials); | ||
} else { | ||
signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); | ||
} | ||
return signedMessage; | ||
} | ||
|
||
@Override | ||
public String getAddress() { | ||
return credentials.getAddress(); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @andrii-kl , I think this change is not ok since it gets rid of the
PrivateTransactionEncoder
to replace it with the regularTransactionEncoder
withinTxSignServiceImpl
.I have run some tests in Besu using
4.9.3
and indeed the signature is different.Let me know if I am missing something, otherwise probably it would be good to open an issue for the fix.
Thanks in advance, Miguel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just spotted this also! #1747
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for creating the issue @antonydenyer ! If @andrii-kl is not available I could take the implementation of the fix.