cc.youchain.tx.RawTransactionManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
YOUChain Common library in JAVA.
package cc.youchain.tx;
import java.io.IOException;
import java.math.BigInteger;
import cc.youchain.crypto.Hash;
import cc.youchain.protocol.YOUChain;
import cc.youchain.protocol.core.DefaultBlockParameterName;
import cc.youchain.protocol.core.methods.response.TransactionReceipt;
import cc.youchain.protocol.core.methods.response.YOUGetTransactionCount;
import cc.youchain.protocol.core.methods.response.YOUSendTransaction;
import cc.youchain.protocol.exceptions.TransactionException;
import cc.youchain.tx.exceptions.TxHashMismatchException;
import cc.youchain.tx.response.TransactionReceiptProcessor;
import cc.youchain.utils.Numeric;
import cc.youchain.utils.TxHashVerifier;
import cc.youchain.crypto.Credentials;
import cc.youchain.crypto.RawTransaction;
import cc.youchain.crypto.TransactionEncoder;
/**
* TransactionManager implementation using YOUChain wallet file to create and sign transactions locally.
*/
public class RawTransactionManager extends TransactionManager {
private final YOUChain youChain;
final Credentials credentials;
private final byte chainId;
protected TxHashVerifier txHashVerifier = new TxHashVerifier();
public RawTransactionManager(YOUChain youChain, Credentials credentials, byte chainId) {
super(youChain, credentials.getAddress());
this.youChain = youChain;
this.credentials = credentials;
this.chainId = chainId;
}
public RawTransactionManager(
YOUChain youChain, Credentials credentials, byte chainId,
TransactionReceiptProcessor transactionReceiptProcessor) {
super(transactionReceiptProcessor, credentials.getAddress());
this.youChain = youChain;
this.credentials = credentials;
this.chainId = chainId;
}
public RawTransactionManager(
YOUChain youChain, Credentials credentials, byte chainId, int attempts, long sleepDuration) {
super(youChain, attempts, sleepDuration, credentials.getAddress());
this.youChain = youChain;
this.credentials = credentials;
this.chainId = chainId;
}
public RawTransactionManager(YOUChain youChain, Credentials credentials) {
this(youChain, credentials, ChainId.NONE);
}
public RawTransactionManager(
YOUChain youChain, Credentials credentials, int attempts, int sleepDuration) {
this(youChain, credentials, ChainId.NONE, attempts, sleepDuration);
}
protected BigInteger getNonce() throws IOException {
YOUGetTransactionCount youGetTransactionCount = youChain.youGetTransactionCount(
credentials.getAddress(), DefaultBlockParameterName.PENDING).send();
return youGetTransactionCount.getTransactionCount();
}
public TxHashVerifier getTxHashVerifier() {
return txHashVerifier;
}
public void setTxHashVerifier(TxHashVerifier txHashVerifier) {
this.txHashVerifier = txHashVerifier;
}
@Override
public YOUSendTransaction sendTransaction(
BigInteger gasPrice, BigInteger gasLimit, String to,
String data, BigInteger value) throws IOException {
BigInteger nonce = getNonce();
RawTransaction rawTransaction = RawTransaction.createTransaction(
nonce,
gasPrice,
gasLimit,
to,
value,
data);
return signAndSend(rawTransaction);
}
/*
* @param rawTransaction a RawTransaction istance to be signed
* @return The transaction signed and encoded without ever broadcasting it
*/
public String sign(RawTransaction rawTransaction) {
byte[] signedMessage;
if (chainId > ChainId.NONE) {
signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
} else {
signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
}
return Numeric.toHexString(signedMessage);
}
public YOUSendTransaction signAndSend(RawTransaction rawTransaction)
throws IOException {
String hexValue = sign(rawTransaction);
YOUSendTransaction youSendTransaction = youChain.youSendRawTransaction(hexValue).send();
if (youSendTransaction != null && !youSendTransaction.hasError()) {
String txHashLocal = Hash.sha3(hexValue);
String txHashRemote = youSendTransaction.getTransactionHash();
if (!this.getTxHashVerifier().verify(txHashLocal, txHashRemote)) {
throw new TxHashMismatchException(txHashLocal, txHashRemote);
}
}
return youSendTransaction;
}
}