bitcoinj / bitcoinj/bitcoinj

spend founds from p2wsh address,non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation) (code 64)

Open
#1,880 16 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
5.2k
Forks
2.6k
Avg merge
10h 56m
Merged PRs (30d)
18

Description

i want spent coin from p2wsh address on btc testnet3 network, i use bitcoinj 0.15.2 build transaction and sign it, use 2/3 multiSig

when i send raw transaction by bitcoin core 0.17.0 command line by use ./bitcoin-cli sendrawtransaction, i get error
`error code: -26`
`error message:non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation) (code 64)`

For now, I don't known why my signed tx broadcast fali?
please help me,Thanks in advance.

**relevant information**
private_key1:cUkNoCzNXMXzN4JhYYbgiWAaKQEwfgUAzW2kogRNhUqGaiZ2AdUh
private_key2:cUpdkS6ekPkQj1RJWtjbLbYyDwszb3nEBbRkeRf9QHVkBBijEdgv
private_key3:cU4tWJk3BGymoJgbGbxNA6NJapTwrbfWWaPsz1bCZBzkoeszb4ML

p2wsh address:tb1qrgwja0j7447fz66gufzuj4awed63xm5xpgs57qwpmm0ujc0x7vusdrjmw6

witness script str:2 PUSHDATA(33)[037a5929bacc6251ace6fb688c7de760b047f03ef1d23f86bbbf61c152c16a4409] PUSHDATA(33)[02764c145abc62bf1c3786136c7020b4cef227102f458aa607afc6afda7b578ce6] PUSHDATA(33)[02f06485529af46c93b4bd6ab68f44dc58241176a989cca77fe052644a7602d048] 3 CHECKMULTISIG

witness script encode str:5221037a5929bacc6251ace6fb688c7de760b047f03ef1d23f86bbbf61c152c16a44092102764c145abc62bf1c3786136c7020b4cef227102f458aa607afc6afda7b578ce62102f06485529af46c93b4bd6ab68f44dc58241176a989cca77fe052644a7602d04853ae

witness hash:0 PUSHDATA(32)[1a1d2ebe5ead7c916b48e245c957aecb75136e860a214f01c1dedfc961e6f339]

witness hash encode str:00201a1d2ebe5ead7c916b48e245c957aecb75136e860a214f01c1dedfc961e6f339

**signed transaction**
01000000000101282f723b362a588f35fee66719c8d739779942b3401995e8e7b3b1f062188c390000000000ffffffff01c8af0000000000002200201a1d2ebe5ead7c916b48e245c957aecb75136e860a214f01c1dedfc961e6f3390400483045022100d6373e87fcebb273c08496e042856acf8068c8685ab3f387151caffb957a828802206ee1488853578b58bdeaba3d47470e2bf58d5a3592b610e505fb31c6cc0f1e780147304402202e9b5c156fef699d9d1c60a22be863a4eb08f01fe86592ade3847d7e7ff9d6b9022077557ce221915b3e2b7c995ba994737f7214a68a0accf6ebb4a259224bc778d601695221037a5929bacc6251ace6fb688c7de760b047f03ef1d23f86bbbf61c152c16a44092102764c145abc62bf1c3786136c7020b4cef227102f458aa607afc6afda7b578ce62102f06485529af46c93b4bd6ab68f44dc58241176a989cca77fe052644a7602d04853ae00000000

**the decode raw transaction**
{
"txid": "45bb61b82eb70f8d1d52edde904b3402d677910ffdcb987264ec58b44eac525a",
"hash": "4b818b6745f45fdd79f0f54222206ee35de27eb103b8974046e6f6b8a323d2b4",
"version": 1,
"size": 349,
"vsize": 158,
"weight": 631,
"locktime": 0,
"vin": [
{
"txid": "398c1862f0b1b3e7e8951940b342997739d7c81967e6fe358f582a363b722f28",
"vout": 0,
"scriptSig": {
"asm": "",
"hex": ""
},
"txinwitness": [
"",
"3045022100d6373e87fcebb273c08496e042856acf8068c8685ab3f387151caffb957a828802206ee1488853578b58bdeaba3d47470e2bf58d5a3592b610e505fb31c6cc0f1e7801",
"304402202e9b5c156fef699d9d1c60a22be863a4eb08f01fe86592ade3847d7e7ff9d6b9022077557ce221915b3e2b7c995ba994737f7214a68a0accf6ebb4a259224bc778d601",
"5221037a5929bacc6251ace6fb688c7de760b047f03ef1d23f86bbbf61c152c16a44092102764c145abc62bf1c3786136c7020b4cef227102f458aa607afc6afda7b578ce62102f06485529af46c93b4bd6ab68f44dc58241176a989cca77fe052644a7602d04853ae"
],
"sequence": 4294967295
}
],
"vout": [
{
"value": 0.00045000,
"n": 0,
"scriptPubKey": {
"asm": "0 1a1d2ebe5ead7c916b48e245c957aecb75136e860a214f01c1dedfc961e6f339",
"hex": "00201a1d2ebe5ead7c916b48e245c957aecb75136e860a214f01c1dedfc961e6f339",
"reqSigs": 1,
"type": "witness_v0_scripthash",
"addresses": [
"tb1qrgwja0j7447fz66gufzuj4awed63xm5xpgs57qwpmm0ujc0x7vusdrjmw6"
]
}
}
]
}

**the sign code**
public void sign() {
NetworkParameters params = TestNet3Params.get();
Coin fee = Coin.SATOSHI.times(5000L);
Coin fundAmount = Coin.SATOSHI.times(50000L);

List ecKeyList = Arrays.asList(
DumpedPrivateKey.fromBase58(params, "cUkNoCzNXMXzN4JhYYbgiWAaKQEwfgUAzW2kogRNhUqGaiZ2AdUh").getKey(),
DumpedPrivateKey.fromBase58(params, "cUpdkS6ekPkQj1RJWtjbLbYyDwszb3nEBbRkeRf9QHVkBBijEdgv").getKey(),
DumpedPrivateKey.fromBase58(params, "cU4tWJk3BGymoJgbGbxNA6NJapTwrbfWWaPsz1bCZBzkoeszb4ML").getKey()
);

List inTxidList = Arrays.asList(
"398c1862f0b1b3e7e8951940b342997739d7c81967e6fe358f582a363b722f28"
);

List inTxIndexList = Arrays.asList(
0
);

String redeemScriptStr = "5221037a5929bacc6251ace6fb688c7de760b047f03ef1d23f86bbbf61c152c16a44092102764c145abc62bf1c3786136c7020b4cef227102f458aa607afc6afda7b578ce62102f06485529af46c93b4bd6ab68f44dc58241176a989cca77fe052644a7602d04853ae";

Script script = new Script(Utils.HEX.decode(redeemScriptStr));
Script p2wshScript = ScriptBuilder.createP2WSHOutputScript(script);

Transaction tx = new Transaction(params);
for (int i = 0; i < inTxidList.size(); i++) {
TransactionWitness witness = new TransactionWitness(3);

tx.addInput(Sha256Hash.wrap(inTxidList.get(i)), inTxIndexList.get(i), new Script(new byte[0]));

Sha256Hash sigHash = tx.hashForWitnessSignature(i, p2wshScript, fundAmount, Transaction.SigHash.ALL, false);

for (int j = 1; j < ecKeyList.size(); j++) {
ECKey.ECDSASignature sig = ecKeyList.get(j).sign(sigHash);
TransactionSignature txSig = new TransactionSignature(sig, Transaction.SigHash.ALL, false);

witness.setPush(j, txSig.encodeToBitcoin());
}
witness.setPush(ecKeyList.size(), script.getProgram());

tx.getInput(i).setWitness(witness);
}

Address sendTo = Address.fromString(params, "tb1qrgwja0j7447fz66gufzuj4awed63xm5xpgs57qwpmm0ujc0x7vusdrjmw6");
Coin outAmount = fundAmount.minus(fee);
Script outPkScript = ScriptBuilder.createOutputScript(sendTo);
tx.addOutput(outAmount, outPkScript);

log.info("signed tx:{}",Utils.HEX.encode(tx.bitcoinSerialize()));
}

Contributor guide

Open the contributing guide

Research direction

Start with the sign() method and reproduce the Bitcoin Core sendrawtransaction failure using the supplied serialized transaction. Compare the witness data, witness script, and signature-hash inputs against bitcoinj 0.15.2's transaction-signing behavior. Done means identifying the cause of the CHECK(MULTI)SIG failure and documenting or correcting the affected signing path.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
blockchain
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.