Skip to content

Commit

Permalink
<feat>(driver): complete driver code
Browse files Browse the repository at this point in the history
  • Loading branch information
lbhan2 committed Aug 13, 2024
1 parent f371b12 commit 3191463
Show file tree
Hide file tree
Showing 9 changed files with 550 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ dependencies {
exclude group: "io.netty"
exclude group: 'org.fisco-bcos', module: 'tcnative'
}

implementation('org.fisco-bcos.java-sdk:fisco-bcos-sdk-abi:2.10.0-SNAPSHOT')
implementation 'org.web3j:core:4.9.8'

// implementation('org.fisco-bcos.java-sdk:fisco-bcos-sdk-abi:2.10.0-SNAPSHOT')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void init(WeCrossContext weCrossContext) {}

@Override
public Driver newDriver() {
return null;
return new Web3Driver();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ private void handleAsyncCallRequest(Request request, Callback callback) {
String from = transactionParams.getFrom();
String to = transactionParams.getTo();
String data = transactionParams.getData();
BigInteger nonce = clientWrapper.getNonce(from);

// build Transaction
org.web3j.protocol.core.methods.request.Transaction transaction =
Expand Down Expand Up @@ -337,7 +336,7 @@ private void handleAsyncTransactionRequest(Request request, Callback callback) {
}
}

private synchronized void refreshStubConfig(Web3StubConfig web3StubConfig) throws IOException {
private synchronized void refreshStubConfig(Web3StubConfig web3StubConfig) {
this.resourceInfoList = web3StubConfig.convertToResourceInfos();

addProperty(Web3Constant.WEB3_PROPERTY_CHAIN_ID, chainId.toString());
Expand Down
421 changes: 421 additions & 0 deletions src/main/java/com/webank/wecross/stub/web3/Web3Driver.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ private static ObjectMapper configureObjectMapper(ObjectMapper objectMapper) {
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(EthBlock.TransactionHash.class, new TransactionHashSerialize());
simpleModule.addSerializer(Transaction.class, new TransactionSerialize());
objectMapper.registerModule(simpleModule);
return objectMapper;
}

Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Web3StatusCode {
public static final int ABINotExist = 2040;
public static final int EncodeAbiFailed = 2041;
public static final int MethodNotExist = 2042;

public static final int AddressNotExist = 2043;
public static final int UnsupportedRPC = 2050;
public static final int UnclassifiedError = 2100;

Expand Down
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 src/main/java/com/webank/wecross/stub/web3/uaproof/Signer.java
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));
}
}

0 comments on commit 3191463

Please sign in to comment.