Skip to content
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

offline sign transaction #84

Open
jack-zhang123 opened this issue Apr 4, 2019 · 1 comment
Open

offline sign transaction #84

jack-zhang123 opened this issue Apr 4, 2019 · 1 comment

Comments

@jack-zhang123
Copy link

jack-zhang123 commented Apr 4, 2019

NetworkParameters networkParameters = TestNet3Params.get();
String toAddress = "mhxZtz7af5DfFhSggJxnzhvzfD67d59XCZ";

String privateKey = "cRDZN9LKkMarsPCS9ZQy7uQobqUyEf6VJt9DKj8UnQa2Zb921FEQ";

DumpedPrivateKey dumpedPrivateKey2 = DumpedPrivateKey.fromBase58(networkParameters, privateKey);
ECKey ecKey2 = dumpedPrivateKey2.getKey();
Address legacyAddress = ecKey2.toAddress(networkParameters);


BigDecimal amountOfSatoshis = new BigDecimal("65300000");
BigDecimal utxoAmount = BigDecimal.ZERO;
BigDecimal fee = new BigDecimal("19191");

//找零钱
BigDecimal changeAmount;

Transaction transaction = new Transaction(networkParameters, 2);
transaction.setVersion(Transaction.FORKID_VERSION);
List<LTCUTXO> needUtxos = new ArrayList<LTCUTXO>();

transaction.addOutput(Coin.valueOf(amountOfSatoshis.longValue()), Address.fromBase58(networkParameters, toAddress));
changeAmount = utxoAmount.subtract(amountOfSatoshis.add(fee));

if (changeAmount.compareTo(BigDecimal.ZERO) > 0) {
    transaction.addOutput(Coin.valueOf(changeAmount.longValue()), ecKey2);
}

TransactionOutPoint transactionOutPoint = new TransactionOutPoint(networkParameters, 1L, Sha256Hash.wrap("f706d2307b08020280051451696f19fc12aa2a72a1d20239883926a3785b0131"));

//key point
transaction.addSignedInput(transactionOutPoint, Coin.parseCoin("65319191"), new Script(Hex.decode("76a914a92fb7ea024aa1098cf41710d536345f85c43c9e88ac")), ecKey2, Transaction.SigHash.ALL, true, true);

byte[] bytes = transaction.bitcoinSerialize();
String sigendTransacitonHash = Hex.toHexString(bytes);
System.out.println(sigendTransacitonHash);

output code
020000000131015b78a32639883902d2a1722aaa12fc196f69511405800202087b30d206f7010000006b483045022100a70c271edf6a8a9158759db0662b5b370b28adaa7f754f3ed02e98a9d396f1c202202376abd5598930f82a9ac3e2baf8858d99794a7775d91503de80278a526b527dc1210341eeed3dee65432a52799d97369ee7866e5fa7bf37ec4754366025b815002e46ffffffff012066e403000000001976a9141ac85eb102de6fe31196615d105828648ecb00c688ac00000000

but I broadcast get errors
https://test-bch-insight.bitpay.com/
An error occurred: 16: mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation). Code:-26

Please help me. I've wasted three days on this problem.

@Mustafa201611
Copy link

NetworkParameters networkParameters = TestNet3Params.get();
String toAddress = "mhxZtz7af5DfFhSggJxnzhvzfD67d59XCZ";

String privateKey = "cRDZN9LKkMarsPCS9ZQy7uQobqUyEf6VJt9DKj8UnQa2Zb921FEQ";

DumpedPrivateKey dumpedPrivateKey2 = DumpedPrivateKey.fromBase58(networkParameters, privateKey);
ECKey ecKey2 = dumpedPrivateKey2.getKey();
Address legacyAddress = ecKey2.toAddress(networkParameters);


BigDecimal amountOfSatoshis = new BigDecimal("65300000");
BigDecimal utxoAmount = BigDecimal.ZERO;
BigDecimal fee = new BigDecimal("19191");

//找零钱
BigDecimal changeAmount;

Transaction transaction = new Transaction(networkParameters, 2);
transaction.setVersion(Transaction.FORKID_VERSION);
List<LTCUTXO> needUtxos = new ArrayList<LTCUTXO>();

transaction.addOutput(Coin.valueOf(amountOfSatoshis.longValue()), Address.fromBase58(networkParameters, toAddress));
changeAmount = utxoAmount.subtract(amountOfSatoshis.add(fee));

if (changeAmount.compareTo(BigDecimal.ZERO) > 0) {
    transaction.addOutput(Coin.valueOf(changeAmount.longValue()), ecKey2);
}

TransactionOutPoint transactionOutPoint = new TransactionOutPoint(networkParameters, 1L, Sha256Hash.wrap("f706d2307b08020280051451696f19fc12aa2a72a1d20239883926a3785b0131"));

//key point
transaction.addSignedInput(transactionOutPoint, Coin.parseCoin("65319191"), new Script(Hex.decode("76a914a92fb7ea024aa1098cf41710d536345f85c43c9e88ac")), ecKey2, Transaction.SigHash.ALL, true, true);

byte[] bytes = transaction.bitcoinSerialize();
String sigendTransacitonHash = Hex.toHexString(bytes);
System.out.println(sigendTransacitonHash);

output code

020000000131015b78a32639883902d2a1722aaa12fc196f69511405800202087b30d206f7010000006b483045022100a70c271edf6a8a9158759db0662b5b370b28adaa7f754f3ed02e98a9d396f1c202202376abd5598930f82a9ac3e2baf8858d99794a7775d91503de80278a526b527dc1210341eeed3dee65432a52799d97369ee7866e5fa7bf37ec4754366025b815002e46ffffffff012066e403000000001976a9141ac85eb102de6fe31196615d105828648ecb00c688ac00000000

but I broadcast get errors

https://test-bch-insight.bitpay.com/

An error occurred: 16: mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation). Code:-26

Please help me. I've wasted three days on this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants