MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.
MIT License
1.86k stars 844 forks source link

Some Problem to Sign transections #1087

Closed erfan132 closed 2 years ago

erfan132 commented 2 years ago

hi i have some problem in sign transection and verify them always in verify we got problem Any idea? did nbitcoin support Testnet Sign Transections? Network= Testnet Btc Address = SegwitP2SH Nbitcoin Version= 7.0.5.0 Sender =2MysNfWboh9x4CuRjw3o1vkJUqExyWwynoZ Reciver ="mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB" Final Transection Output={{ "hash": "ad228e9237d73bd9c26d5e76aed3dadaf3305091532fe47cb06c9aa590fbea32", "ver": 1, "vin_sz": 3, "vout_sz": 2, "lock_time": 0, "size": 199, "in": [ { "prev_out": { "hash": "b17f745dbf11c4e2cd84faffd587ca85aba3f44f0f42fec059b11a12e234ab1e", "n": 0 }, "scriptSig": "" }, { "prev_out": { "hash": "4a7cf9950ade5053186291119ec7381d3bfbcf515466a65bf8d1d375bcd6f2fa", "n": 0 }, "scriptSig": "" }, { "prev_out": { "hash": "191c9395d091cc4d6a9a64bdabeeae6ad6e7b2287630d681a0ae7c09db30f760", "n": 0 }, "scriptSig": "" } ], "out": [ { "value": "0.00020000", "scriptPubKey": "OP_DUP OP_HASH160 9f9a7abd600c0caa03983a77c8c3df8e062cb2fa OP_EQUALVERIFY OP_CHECKSIG" }, { "value": "0.02526033", "scriptPubKey": "OP_HASH160 48a6f8d29c00865a72d9ca757bc2ef23697f813e OP_EQUAL" } ] }} Hex="01000000031eab34e2121ab159c0fe420f4ff4a3ab85ca87d5fffa84cde2c411bf5d747fb10000000000fffffffffaf2d6bc75d3d1f85ba6665451cffb3b1d38c79e119162185350de0a95f97c4a0000000000ffffffff60f730db097caea081d6307628b2e7d66aaeeeabbd649a6a4dcc91d095931c190000000000ffffffff02204e0000000000001976a9149f9a7abd600c0caa03983a77c8c3df8e062cb2fa88ac518b26000000000017a91448a6f8d29c00865a72d9ca757bc2ef23697f813e8700000000" But in verify Its return False And in Broadcasting i got this error (sendrawtransaction RPC error: {"code":-25,"message":"bad-txns-inputs-missingorspent"} Mysource image Where is problem?

erfan132 commented 2 years ago

Any one can help me?

erfan132 commented 2 years ago

Hi again I passed The verify with true but i have this problem in broadcasting transection Mempool accept test failed: 16: mandatory-script-verify-flag-failed (Operation not valid with the current stack size) Any help?

erfan132 commented 2 years ago

image I sign The transection but it doesn't show in decode

erfan132 commented 2 years ago

Nobody |?

dangershony commented 2 years ago

bad-txns-inputs-missingorspent indicate you are using outputs that don't exist are you fetching correctly? are you mixing networks?

erfan132 commented 2 years ago

No I don't think i get Utxo by this function but Script from this api return in hex as you see its convert from hex to asm but i have this problem i think this related to utxo because sigscript its not completed but im not mix network im really confusing about why this happened it take 3 days on this problem do you have any idea ?

received_tran output image received_coins output image

` public async static Task<List> GetUnSpentCoins(string address) {

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri($"https://api.blockcypher.com/v1/btc/test3/");
            var res = await client.GetAsync($"addrs/{address}/full");
            var utxos = new List<ICoin>();
            if (res.IsSuccessStatusCode)
            {
                var resString = await res.Content.ReadAsStringAsync();
                var response = JsonConvert.DeserializeObject<ClsFullAddressesGet.Root>(resString);
                var received_tran = new List<MiniTranCls>();
                var spent_hash = new List<string>();
                foreach (var item in response.txs)
                {
                    //get all money sent out of this address
                    var spent_trans = item.inputs.Where(a => a.addresses.Contains(address));
                    if (spent_trans.Any())
                    {
                        var input = item.inputs.Select(a => a.prev_hash);
                        spent_hash.AddRange(input);
                    }

                    var received = item.outputs.Where(a => a.addresses.Contains(address));
                    if (received.Any())
                    {
                        //get the particular output that the address received
                        var output = received.FirstOrDefault(a => a.addresses.FirstOrDefault() == address);

                        //get the index of the received ouput from the total outputs
                        var index = item.outputs.FindIndex(a => a.addresses.FirstOrDefault() == address);
                        var miniTran = new MiniTranCls
                        {
                            address = address,
                            Hash = item.hash,
                            script = output.script,
                            sats = output.value,
                            index = index
                        };
                        received_tran.Add(miniTran);
                    }
                }
                var received_coins = received_tran.Where(a => !spent_hash.Contains(a.Hash)).ToList()
                                        .Select(a => new Coin(fromTxHash: uint256.Parse(a.Hash),
                                         fromOutputIndex: uint.Parse(a.index.ToString()),
                                         amount: new Money(a.sats, MoneyUnit.Satoshi),
                                          scriptPubKey: new Script(Encoders.Hex.DecodeData(a.script)))).ToList();

                return received_coins;
            }
            return null;

        }

    }`
dangershony commented 2 years ago

Notice the Final Transection Output the scriptsig is empty, is that the trx after you signed it?

erfan132 commented 2 years ago

Yes i think so

erfan132 commented 2 years ago

the output of transaction

{{ "hash": "ea27cc8eb2a385d2750545db3b61aaf0f56046c2f6497812aa7b6166059163e3", "ver": 1, "vin_sz": 2, "vout_sz": 2, "lock_time": 0, "size": 158, "in": [ { "prev_out": { "hash": "b17f745dbf11c4e2cd84faffd587ca85aba3f44f0f42fec059b11a12e234ab1e", "n": 0 }, "scriptSig": "" }, { "prev_out": { "hash": "191c9395d091cc4d6a9a64bdabeeae6ad6e7b2287630d681a0ae7c09db30f760", "n": 0 }, "scriptSig": "" } ], "out": [ { "value": "0.00010000", "scriptPubKey": "OP_DUP OP_HASH160 9f9a7abd600c0caa03983a77c8c3df8e062cb2fa OP_EQUALVERIFY OP_CHECKSIG" }, { "value": "0.00064796", "scriptPubKey": "OP_HASH160 48a6f8d29c00865a72d9ca757bc2ef23697f813e OP_EQUAL" } ] }}

erfan132 commented 2 years ago

Hi again i try to sign my transection with https://tbtc.bitaps.com/constructor its totally work i think this problem related to nbitcoin sign transection function

erfan132 commented 2 years ago

Hi again i found the problem And the problem is in get utxo from blockcypher and the probelm is i put new Coin( script filed with the script thats returner form api but the correct way is set the script coin to the scriptpub key form sender address I will post my Uxo and Transection calss thats is work in blockcypher šŸ˜„šŸ˜ BTW @dangershony thanks for your reply god bless you