-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<feat>(driver): complete driver code
- Loading branch information
lbhan2
committed
Aug 13, 2024
1 parent
f371b12
commit 3191463
Showing
9 changed files
with
550 additions
and
5 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
421 changes: 421 additions & 0 deletions
421
src/main/java/com/webank/wecross/stub/web3/Web3Driver.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/webank/wecross/stub/web3/common/Web3SignatureException.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,11 @@ | ||
package com.webank.wecross.stub.web3.common; | ||
|
||
public class Web3SignatureException extends RuntimeException { | ||
public Web3SignatureException(String message) { | ||
super(message); | ||
} | ||
|
||
public Web3SignatureException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
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
71 changes: 71 additions & 0 deletions
71
src/main/java/com/webank/wecross/stub/web3/contract/BlockUtility.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,71 @@ | ||
package com.webank.wecross.stub.web3.contract; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.webank.wecross.stub.Block; | ||
import com.webank.wecross.stub.BlockHeader; | ||
import com.webank.wecross.stub.Transaction; | ||
import com.webank.wecross.stub.web3.common.ObjectMapperFactory; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.web3j.protocol.core.methods.response.EthBlock; | ||
|
||
public class BlockUtility { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(BlockUtility.class); | ||
|
||
public static BlockHeader convertToBlockHeader(EthBlock.Block block) { | ||
BlockHeader blockHeader = new BlockHeader(); | ||
blockHeader.setHash(block.getHash()); | ||
blockHeader.setPrevHash(block.getParentHash().isEmpty() ? null : block.getParentHash()); | ||
blockHeader.setNumber(block.getNumber().longValue()); | ||
blockHeader.setReceiptRoot(block.getReceiptsRoot()); | ||
blockHeader.setStateRoot(block.getStateRoot()); | ||
blockHeader.setTransactionRoot(block.getTransactionsRoot()); | ||
blockHeader.setTimestamp(block.getTimestamp().longValue()); | ||
return blockHeader; | ||
} | ||
|
||
public static Block convertToBlock(EthBlock.Block block, boolean onlyHeader) | ||
throws JsonProcessingException { | ||
Block stubBlock = new Block(); | ||
|
||
/** BlockHeader */ | ||
BlockHeader blockHeader = convertToBlockHeader(block); | ||
stubBlock.setBlockHeader(blockHeader); | ||
|
||
List<Transaction> transactionList = new ArrayList<>(); | ||
if (!onlyHeader | ||
&& !block.getTransactions().isEmpty() | ||
&& block.getTransactions().get(0) instanceof EthBlock.TransactionObject) { | ||
for (int i = 0; i < block.getTransactions().size(); i++) { | ||
EthBlock.TransactionObject transactionObject = | ||
(EthBlock.TransactionObject) block.getTransactions().get(i); | ||
byte[] txBytes = ObjectMapperFactory.getObjectMapper().writeValueAsBytes(transactionObject); | ||
String transactionHash = transactionObject.getHash(); | ||
Transaction transaction = new Transaction(); | ||
transaction.setTxBytes(txBytes); | ||
transaction.setAccountIdentity(transactionObject.getFrom()); | ||
transaction.setTransactionByProxy(false); | ||
transaction.getTransactionResponse().setHash(transactionHash); | ||
transaction.getTransactionResponse().setBlockNumber(block.getNumber().longValue()); | ||
transactionList.add(transaction); | ||
} | ||
} | ||
stubBlock.setTransactionsWithDetail(transactionList); | ||
return stubBlock; | ||
} | ||
|
||
public static Block convertToBlock(byte[] blockBytes, boolean onlyHeader) throws IOException { | ||
EthBlock.Block block = | ||
ObjectMapperFactory.getObjectMapper().readValue(blockBytes, EthBlock.Block.class); | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("blockNumber: {}, blockHash: {}", block.getNumber(), block.getHash()); | ||
} | ||
Block stubBlock = convertToBlock(block, onlyHeader); | ||
stubBlock.setRawBytes(blockBytes); | ||
return stubBlock; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/webank/wecross/stub/web3/uaproof/Signer.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,42 @@ | ||
package com.webank.wecross.stub.web3.uaproof; | ||
|
||
import com.webank.wecross.stub.web3.common.Web3SignatureException; | ||
import java.math.BigInteger; | ||
import java.util.Arrays; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.web3j.crypto.ECKeyPair; | ||
import org.web3j.crypto.Hash; | ||
import org.web3j.crypto.Keys; | ||
import org.web3j.crypto.Sign; | ||
import org.web3j.utils.Numeric; | ||
|
||
public class Signer { | ||
public static byte[] sign(ECKeyPair keyPair, byte[] srcData) { | ||
byte[] hashData = Hash.sha3(srcData); | ||
Sign.SignatureData signatureData = Sign.signPrefixedMessage(hashData, keyPair); | ||
byte[] r = signatureData.getR(); | ||
byte[] s = signatureData.getS(); | ||
byte[] v = signatureData.getV(); | ||
byte[] signByte = Arrays.copyOf(r, v.length + r.length + s.length); | ||
System.arraycopy(s, 0, signByte, r.length, s.length); | ||
System.arraycopy(v, 0, signByte, r.length + s.length, v.length); | ||
return signByte; | ||
} | ||
|
||
public static boolean verify(byte[] signData, byte[] srcData, String address) { | ||
byte[] hashData = Hash.sha3(srcData); | ||
Sign.SignatureData signatureData = | ||
new Sign.SignatureData( | ||
signData[64], | ||
Arrays.copyOfRange(signData, 0, 32), | ||
Arrays.copyOfRange(signData, 32, 64)); | ||
BigInteger publicKey; | ||
try { | ||
publicKey = Sign.signedPrefixedMessageToKey(hashData, signatureData); | ||
} catch (Exception e) { | ||
throw new Web3SignatureException("verify failed: " + e.getMessage()); | ||
} | ||
return StringUtils.equalsIgnoreCase( | ||
Keys.getAddress(publicKey), Numeric.cleanHexPrefix(address)); | ||
} | ||
} |