Skip to content

Commit

Permalink
更新bch tansaction
Browse files Browse the repository at this point in the history
  • Loading branch information
GalaxySciTech committed Sep 16, 2020
1 parent f9ee479 commit 1b1d293
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ buildscript {
dependencies {
compile 'io.github.qyvlik:io.eblock.eos-eos4j:1.0.1'
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.0'
compile 'org.bitcoinj:bitcoinj-core:0.14.3'
compile 'org.bitcoinj:bitcoinj-core:0.14.7'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.+'
// compile group: 'com.google.protobuf', name: 'protobuf-lite', version: '3.0.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,72 @@ public TxSignResult signTransaction(String chainID, String password, Wallet wall
return new TxSignResult(signedHex, txHash);
}

public TxSignResult signBitcoinCashTransaction(String chainID, String password, Wallet wallet) {
collectPrvKeysAndAddress(Metadata.NONE, password, wallet);

Transaction tran = new Transaction(network);
//FORKID_VERSION
tran.setVersion(2);

byte SIGHASH_FORKID = (byte) 0x40;

long totalAmount = 0L;

for (UTXO output : getOutputs()) {
totalAmount += output.getAmount();
}

if (totalAmount < getAmount()) {
throw new TokenException(Messages.INSUFFICIENT_FUNDS);
}

//add send to output
tran.addOutput(Coin.valueOf(getAmount()), Address.fromBase58(network, getTo()));

//add change output
long changeAmount = totalAmount - (getAmount() + getFee());
if (changeAmount >= DUST_THRESHOLD) {
tran.addOutput(Coin.valueOf(changeAmount), changeAddress);
}

for (UTXO output : getOutputs()) {
tran.addInput(Sha256Hash.wrap(output.getTxHash()), output.getVout(), new Script(NumericUtil.hexToBytes(output.getScriptPubKey())));
}

for (int i = 0; i < getOutputs().size(); i++) {
UTXO output = getOutputs().get(i);

BigInteger privateKey = wallet.getMetadata().getSource().equals(Metadata.FROM_WIF) ? prvKeys.get(0) : prvKeys.get(i);
ECKey ecKey;
if (output.getAddress().equals(ECKey.fromPrivate(privateKey).toAddress(network).toBase58())) {
ecKey = ECKey.fromPrivate(privateKey);
} else if (output.getAddress().equals(ECKey.fromPrivate(privateKey, false).toAddress(network).toBase58())) {
ecKey = ECKey.fromPrivate(privateKey, false);
} else {
throw new TokenException(Messages.CAN_NOT_FOUND_PRIVATE_KEY);
}


TransactionInput transactionInput = tran.getInput(i);
Script scriptPubKey = ScriptBuilder.createOutputScript(Address.fromBase58(network, output.getAddress()));
Sha256Hash hash = tran.hashForSignature(i, scriptPubKey.getProgram(), SIGHASH_FORKID);
ECKey.ECDSASignature ecSig = ecKey.sign(hash);
TransactionSignature txSig = new TransactionSignature(ecSig.r, ecSig.s, SIGHASH_FORKID);
if (scriptPubKey.isSentToRawPubKey()) {
transactionInput.setScriptSig(ScriptBuilder.createInputScript(txSig));
} else {
if (!scriptPubKey.isSentToAddress()) {
throw new TokenException(Messages.UNSUPPORT_SEND_TARGET);
}
transactionInput.setScriptSig(ScriptBuilder.createInputScript(txSig, ecKey));
}
}

String signedHex = NumericUtil.bytesToHex(tran.bitcoinSerialize());
String txHash = NumericUtil.beBigEndianHex(Hash.sha256(Hash.sha256(signedHex)));
return new TxSignResult(signedHex, txHash);
}

public TxSignResult signUsdtTransaction(String chainID, String password, Wallet wallet) {
collectPrvKeysAndAddress(Metadata.NONE, password, wallet);

Expand Down

0 comments on commit 1b1d293

Please sign in to comment.