text:
OP_CHECKLOCKTIMEVERIFY generates non standard transaction
I create a transaction in my regtest enviroment with OP_CHECKLOCKTIMEVERIFY
it's my transaction data:
0200000001b949410322a4af7c5b24d367aad9ac186902d4aa15b79ced3f821ae772aaabf70000000085483045022100ded7ee34b12e829a0957a852049546ff909d33129c8c1d3769db227cafffcbdd022033b1e9ed37682470b77450d791c8b5dde67f4de2d14b772d2bbcfbe22b3a453b012103f99b855c961eb142cc237d37db22ae67b4a80f2d2f178762110087c1e54f90c51976a914d1d03865cf22d10d9d1ed68889be6bc99513cd6d88ACfeffffff016036f829010000001E04ACFCD95EB175a9141a759e10e92cd4d855ba8bee9cd4f4a276033d1087ACFCD95E
{
  "txid": "55bfb3b57ad2fe3d3c70d5696e3a7cd0fd4e25c8bf41d7bfa3ab57a457b31776",
  "hash": "55bfb3b57ad2fe3d3c70d5696e3a7cd0fd4e25c8bf41d7bfa3ab57a457b31776",
  "version": 2,
  "size": 223,
  "vsize": 223,
  "weight": 892,
  "locktime": 1591344300,
  "vin": [
    {
      "txid": "f7abaa72e71a823fed9cb715aad4026918acd9aa67d3245b7cafa422034149b9",
      "vout": 0,
      "scriptSig": {
        "asm": "3045022100ded7ee34b12e829a0957a852049546ff909d33129c8c1d3769db227cafffcbdd022033b1e9ed37682470b77450d791c8b5dde67f4de2d14b772d2bbcfbe22b3a453b[ALL] 03f99b855c961eb142cc237d37db22ae67b4a80f2d2f178762110087c1e54f90c5 76a914d1d03865cf22d10d9d1ed68889be6bc99513cd6d88ac",
        "hex": "483045022100ded7ee34b12e829a0957a852049546ff909d33129c8c1d3769db227cafffcbdd022033b1e9ed37682470b77450d791c8b5dde67f4de2d14b772d2bbcfbe22b3a453b012103f99b855c961eb142cc237d37db22ae67b4a80f2d2f178762110087c1e54f90c51976a914d1d03865cf22d10d9d1ed68889be6bc99513cd6d88ac"
      },
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 49.991,
      "n": 0,
      "scriptPubKey": {
        "asm": "1591344300 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_HASH160 1a759e10e92cd4d855ba8bee9cd4f4a276033d10 OP_EQUAL",
        "hex": "04acfcd95eb175a9141a759e10e92cd4d855ba8bee9cd4f4a276033d1087",
        "type": "nonstandard"
      }
    }
  ]
}

Is it normal that If I use OP_CHECKLOCKTIMEVERIFY I get a non standard transaction? I'm able to relay it if I change acceptnonstdtxn=1 but it is not my purpose.

You should create a P2SH address containing the hash of that script, and when you want to spend, you can spend from that P2SH address. This'll make your transactions standard.

thanks for quickly answer. is It for security reason? I have some difficulties to understand How CLTV works. The sender (A) send 49 btc to recipient (B) and A want to lock the amount for 1 month. A asks to B, Can you create an address with this kind of Timelock?

All vout scripts that are not P2PK, P2PKH, P2SH, some RETURN, or up to 3 CHECKMULTISIGVERIFY are nonstandard. All scripts not in this category need to be wrapped in P2SH.

As you can see from here and here and here

To create a standard transaction I have to use at least 1-3? Because right now the sender is P2SH-P2PKH and the receiver is P2SH 1-1. The scenario of A and B (previous comment) remains? A needs ask to B, can you create this kind of address to Lock my transaction until 1 Jan (for example)

No, I meant if the output is a CHECKMULTISIGVERIFY it can't have more than 3 n. No, assuming who you were sending to in the original transaction was person B, your transaction's vout should be HASH160 XXXXX... EQUAL where XXXX is the hash of the script 1591344300 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_HASH160 1a759e10e92cd4d855ba8bee9cd4f4a276033d10 OP_EQUAL and you should share this script with person B, and when time comes they should be able to spend from it to their address.

Ok, for that reason is not standard because I'm try to set a CLTV where B can't resolve it (the hash is different). thanks


text:
Create master public key after HMAC-SHA512
I have that result after hash_pbkdf2
5B56C417303FAA3FCBA7E57400E120A0CA83EC5A4FC9FFBA757FBE63FBD77A89A1A3BE4C67196F57C39A88B76373733891BFABA16ED27A813CEED498804C0570

left 256 bits are:
5B56C417303FAA3FCBA7E57400E120A0CA83EC5A4FC9FFBA757FBE63FBD77A89

in base 10 are
m = 41313771436092106966070828753784402438291933698697412643075684398900444625545

right 256 bits are
A1A3BE4C67196F57C39A88B76373733891BFABA16ED27A813CEED498804C0570

in base 10 are
G = 73111678085084231450767103284505018606851115862905010375565411946552667932016

To calculate Master Public Key I have to do
m*G

bc <<< “73111678085084231450767103284505018606851115862905010375565411946552667932016 * 41313771436092106966070828753784402438291933698697412643075684398900444625545”

result
3020519157716314193817478999423070077945694756523017007189820738431399197829981163447928137705627795431573290757393848092663806903883822516032876636948720

that result is not correct

See https://bitcoin.stackexchange.com/questions/84116/get-master-private-key-from-bip39-seed.

m*G
bc <<<
  “73111678085084231450767103284505018606851115862905010375565411946552667932016
  * 41313771436092106966070828753784402438291933698697412643075684398900444625545”

Careful, G is a secp256k1 elliptic curve generator point. You cannot multiply it as you would a scalar. You need a secp256k1 library to perform scalar x EC-Point operations.

I suggest you try a command line tool, such as Libbitcoin-BX for EC-point operations in command line: https://github.com/libbitcoin/libbitcoin-explorer/wiki/bx-ec-to-public

thanks james, I'd like to do without library if possible

that library is very powerful, with bx ec-to-public 5B56C417303FAA3FCBA7E57400E120A0CA83EC5A4FC9FFBA757FBE63FBD77A89 I can find Public Key compressed, But i'd like to use even A1A3BE4C67196F57C39A88B76373733891BFABA16ED27A813CEED498804C0570 and understand how it works

Have a look at the operations necessary for scalar x EC point operation, it's pretty complex with just basic CL tools: https://en.wikipedia.org/wiki/Elliptic_curve

Yep you are right :)
Thanks again James for your time

source: Stack Exchange

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/

sort: votes


text:
Change address after payment
Presume that I have 1 BTC (the total of my UTXO) and I have to pay Bob With 1 BTC.
I set 0 for fees.
My Input is 1 and my Output is 1 versus Bob or there are always 2 outputs?
one versus Bob
and
one versus my change address with 0 btc?

There will be no change in this scenario on only one output sent to bob's address. Note that 0 fee transactions are unlikely to be mined these days so do include a fee. Usually you let your wallet calculate a reasonable fee for you.

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/4.0/


text:
Converting WIF as Base58 string to byte array in bash
I want to convert wif as Base58 string to byte array.
Private key to WIF
printf 800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D507A5B8D | xxd -r -p | base58

5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ

Now I want reverse this (WIF to private key), form 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ to 800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D507A5B8D
Im using that library https://github.com/keis/base58 But I can change or better if I can use only bash without extra library
full example https://en.bitcoin.it/wiki/Wallet_import_format

Are you looking for an alternative tool to decode base58 to hex?

no the alternative. with this tool I can't to  base58 to hex, or I don't understand How can I that

You need to use the decode flag -d:
printf "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ" | base58 -c -d | xxd -p
800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d
72aa1d

To have xxd output all on one line, give it a large column number -c flag:
$ printf "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ" | base58 -c -d | xxd -p -c 1000
800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d

If you really want the checksum included 507A5B8D (the last 4 bytes), omit the -c flag which denotes checksum encoding:
$ printf "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ" | base58 -d | xxd -p -c 1000
800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d507a5b8d

Here's a full script which reads the key from standard input so it doesn't get stored in a file or command history, verifies the checksum and key length, and then prints the network byte, secret key, and compressed key flag.
Requires base58 package to be installed.
#!/bin/bash
read -p "Enter WIF key: " wif_key
decoded_key=$(echo "$wif_key" | base58 -dc | xxd -p -c 256)
if [ ! -z $decoded_key ]
then
    keylen=$(echo -n $decoded_key | wc -c)
    if [ "$keylen" -ge 66 ] && [ "$keylen" -le 68 ]
    then
        echo "Network byte: ${decoded_key:0:2}";
        echo "Secret key: ${decoded_key:2:64}";
        if [ -z ${decoded_key:66:2} ]; then cpk="false"; else cpk="true"; fi
        echo "Compressed public key: $cpk";
    else
        echo "Invalid decoded key length" >&2
        $(exit 1)
    fi
else
    $(exit 1)
fi

Example
Prompt:
Enter WIF key: 5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ

Output:
Network byte: 80
Secret key: 0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d
Compressed public key: false


text:
checksum in address
I want to create a address bitcoin.
I have a payload with version like this:
0050d512c984b1656ef47c145c8fd5d5262d3904b1

where 00 in my version prefix, and 50d512c984b1656ef47c145c8fd5d5262d3904b1 is my payload.
Now I Want to add checksum, with sha256 twice, the result is
4550e2041dd936f17fd8a0d60f17c69faea8ef0e7d3ee82383153d93c2e837a8

where the first 4 bytes is checksum 4550e204
Now I can convert to base58
0050d512c984b1656ef47c145c8fd5d5262d3904b1**4550e204**

And I Get the right address.
I can read that

These four bytes serve as the error-checking code, or checksum. The
  checksum is concatenated (appended) to the end.

How Can I check if my checksum (4550e204) is correct before base58 ?

Sorry I don't understand your question. You have described the derivation for the checksum given version&payload. Given a base58 address, you decode the address for the version+payload+checksum. From that you can verify the version+payload. What other scenario do you mean?

One way to check is to use a tool like https://github.com/keis/base58 that implements base58check (-c) encoding and decoding (-d) with checksum verification to try to decode the final address:
$ printf "0050d512c984b1656ef47c145c8fd5d5262d3904b14550e204" | xxd -r -p | base58
18NQJwokxLGA7KsHJdQWh6ANB46K6nP7Hm
$ printf "18NQJwokxLGA7KsHJdQWh6ANB46K6nP7Hm" | base58 -c -d | xxd -p
0050d512c984b1656ef47c145c8fd5d5262d3904b1

Now, change the checksum and see if it is able to decode:
$ printf "0050d512c984b1656ef47c145c8fd5d5262d3904b14550e203" | xxd -r -p | base58
18NQJwokxLGA7KsHJdQWh6ANB46K6nP7Hk
Jordans-MacBook-Pro:~ Jbaczuk$ printf "18NQJwokxLGA7KsHJdQWh6ANB46K6nP7Hk" | base58 -c -d | xxd -p
Invalid checksum

thanks, for do checksum the operation is  decode base58 -and reply  printf 0050d512c984b1656ef47c145c8fd5d5262d3904b1 |xxd -r -p | sha256sum -b | xxd -r -p | sha256sum -b | head -c 8and Check the output with last 4 bytes ?

Yes, the first four bytes of the HASH256 is the checksum, which is appended to the end of the value to encode.


text:
Lightning Invoice QR-Code minimum size/display resolution?
what would be the minimum screen size to display lightning invoices as a qr-code?
For the Raspberry Pi Zero I found these displays:

128x128 pixel LCD display (1.44 inch)
200x200 pixel e-paper/e-Ink display (1.54 inch)
128x64 pixel OLED display (0.96 inch)

I saw the examples in BOLT#11 and know that it depends on the input type and error correction setting when creating the qr-code.
Would the qr-code be readable if every black dot of the qr-code would be respresented with only one dot? Or do I need for example 4 dots, resulting in a 32x32 qr-code? Would that be enough for lightning invoices? Is it possible to convert the lightning invoice in all uppercase for generating the qr-code in alphanumeric mode which only uses 5.5 bits per character instead of 8?
With the Google Charts API it is possible to generate QR-Codes, this link generates a QR-Code with 128x128 pixel with the first example invoice from BOLT#11 and an error correction of M (allows recovery of up to 15% data loss): https://chart.googleapis.com/chart?cht=qr&chl=lnbc1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdpl2pkx2ctnv5sxxmmwwd5kgetjypeh2ursdae8g6twvus8g6rfwvs8qun0dfjkxaq8rkx3yf5tcsyz3d73gafnh3cax9rn449d9p5uxz9ezhhypd0elx87sjle52x86fux2ypatgddc6k63n7erqz25le42c4u4ecky03ylcqca784w&chs=128x128&chld=M|0

Just stumbled upon the answer to this on Twitter myself: https://twitter.com/sakak_musdom/status/1084857547947880448
So the 128x64 pixel OLED display (0.96 inch) is apparently enough to display a lightning invoice.

First of all yes it is possible to convert all characters to uppercase a trick some users of lightning already use.
If you look in the lightning rfc you can see that the length of an invoice can become quite large as the discriptions, routing hints and fallback addresses are not fixed I think there is not even an upper limit defined. For example this test invoice has 439 characters.

lnbc20m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqhp58yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqsfpp3qjmp7lwpagxun9pygexvgpjdc4jdj85fr9yq20q82gphp2nflc7jtzrcazrra7wwgzxqc8u7754cdlpfrmccae92qgzqvzq2ps8pqqqqqqpqqqqq9qqqvpeuqafqxu92d8lr6fvg0r5gv0heeeqgcrqlnm6jhphu9y00rrhy4grqszsvpcgpy9qqqqqqgqqqqq7qqzqj9n4evl6mr5aj9f58zp6fyjzup6ywn3x6sk8akg5v4tgn2q8g4fhx05wf6juaxu9760yp46454gpg5mtzgerlzezqcqvjnhjh8z3g2qqdhhwkj

QR Codes themselves come at different versions which define how much data can be stored in them depending on how many pixels are used.
What I can't tell u if one pixel of a display would be sufficient. I guess even with error correction this would not be enough. For example the RaspiBlitz uses a 320 * 480 display


text:
Incorrectly counts the we have added a generator point to itself in a loop (SECP256K1, Python),
I have a script that converts a bitcoin private key into x and y coordinate with parameters of a Secp256k1 curve. Here's the script:
a = 0
b = 7
#Order of the finite field
prime = 2**256 - 2**32 - 977
#G coordinates
gx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
#Order of the group G
n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
#n -1 => is the number of all possible private keys

def addition(currentX, currentY, gx, gy, a, b, prime):
    if gy == 0:
        return (None, None)
    elif currentX is None and currentY is None:
        return (gx, gy)
    elif currentX == gx and currentY != gy:
        return (None, None)
    elif currentX == gx and currentY == gy and currentY == 0:
        return (None, None)
    elif currentX == gx and currentY == gy:
        s1 = (3 * pow(gx, 2, prime) + a) % prime
        s2 = (gy * 2) % prime
        s = (s1 * pow(s2, (prime - 2), prime)) % prime
        currentX = (s ** 2 - 2 * gx) % prime
        currentY = (s * (gx - currentX) - gy) % prime
    elif currentX != gx:
        s1 = (currentY - gy)
        s2 = (currentX - gx)
        s = (s1 * pow(s2, (prime - 2), prime)) % prime
        currentX = ((s ** 2) - gx - currentX) % prime
        currentY = ((s * (gx - currentX)) - gy) % prime

    return (currentX, currentY)

def secp256k1BinaryExpansion(privateKey, gx, gy, a, b, prime):
    if pow(gy, 2, prime) != (pow(gx, 3, prime) + a * gx + b) % prime:
        return "The point is not on the curve"
    counter = 0
    coef = privateKey
    currentX, currentY = gx, gy
    resultX, resultY = None, None
    while coef:
        if coef & 1:
            resultX, resultY = addition(resultX, resultY, currentX, currentY, a, b, prime)
            counter += 1
        currentX, currentY = addition(currentX, currentY, currentX, currentY, a, b, prime)
        counter <<= 1
        coef >>= 1
    print("Counter", hex(counter))
    return (resultX, resultY)

priv = 0x45300f2b990d332c0ee0efd69f2c21c323d0e2d20e7bfa7b1970bbf169174c82
xPub, yPub = secp256k1BinaryExpansion(priv, gx, gy, a, b, prime)

However I cannot correctly count how many addition there has been after the function is completed. By definition, there has to be
0x45300f2b990d332c0ee0efd69f2c21c323d0e2d20e7bfa7b1970bbf169174c82

additions (The same number as the private key). The counter variable in the function counts how many times the addition has been made. However the print("Counter", hex(counter)) outputs:
0x4132e8968fdd0e98de5fde704b470bc4c38434f96bf7077034ccb099d4f00ca2

Which is incorrect. How to fix that error?

You are both incrementing and bit-shifting counter - was that deliberate? (bit-shifting a counter rarely makes sense IMHO)


text:
Is it true that Public keys with even y coordinate correspond to private key that are less than n/2 and vice versa? (Secp256k1)
The question is somewhat complex and directed to clearing thing out.
Suppose that n is the order of the cyclic group. It n - 1 is the number of all private keys possible
n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141

We also know that every private and public key has its modular inverse.
To get a modular inverse of a private key, we need to subtract the private key from n.
n - privKey
To get a modular inverse of a public key, we'll have to multiply its y coordinate by -1 and modulo by the p - order of the finite field.
x,y = x, -y % p

A modular inversed public key has the same x coordinate as original public key, but different y coordinate, and the y coordinate is always different in its polarity.
If the original y was odd, in a modular inversed key it will be even, and vice versa.
If a compressed public key has "02" index at the biggining then it has even y. If it is "03" then it is odd.
The question is, if the y coordinate of a public key is even, does it mean that the corresponding private key is less than n/2 by its value? If the y is odd, the private key is more than n/2 ?
Is there any relationship between the eveness/oddness of the y (or x) coordinate and the value of the corresponding private key?
Is there any way to know that the private key is more or less than n/2 while not knowing the private key itself?
Is there a way to find out the public key of an address that never sent Bitcoin but only received it?

It took me 2 tries to find a counter example to your claim: privatekey = 0d6a35eec96a4526450edf6cf0acbedcfec82133d6086efe5a1a8a0d9897d1e5 < N/2, compressedpub = 031ce10ff151c1f0ea39b8cdd49cce5d386a69a7d4af139feba4ea1086de71abc4.

We also know that every private and public key has its modular inverse. To get a modular inverse of a private key, we need to subtract the private key from n.

Just a terminology note: that's a modular negation, not a modular inverse. The modular inverse of v mod n would be the number a such that a*v = 1 mod n. Modular inverses aren't relevant to your question I believe, just wanted to clarify this.

A modular inversed public key has the same x coordinate as original public key, but different y coordinate, and the y coordinate is always different in its polarity. If the original y was odd, in a modular inversed key it will be even, and vice versa.

That's all correct. Negating the private key (in the finite field modulo n) corresponds to negating the point, and negating the point negates the Y coordinate, and given that the coordinate field size is odd, negation modulo it changes the parity.

The question is, if the y coordinate of a public key is even, does it mean that the corresponding private key is less than n/2 by its value? If the y is odd, the private key is more than n/2 ?

No, there is no such relation.

Is there any relationship between the eveness/oddness of the y (or x) coordinate and the value of the corresponding private key?

No, seeing the public key should not let you infer anything about the corresponding private key. If there was a way to learn anything, the ECDLP (elliptic curve discrete logarithm problem) assumption would be broken (that assumption roughly says that a computationally-bounded attacker who sees a public key cannot guess the private key meaningfully better than random).

Is there any way to know that the private key is more or less than n/2 while not knowing the private key itself?

No.

Is there a way to find out the public key of an address that never sent Bitcoin but only received it?

That's an unrelated question, but in short: no. It does depend on the type of address you're talking about. For P2PKH, P2SH, or P2WPKH addresses clearly not, as a hash function is involved. For P2TR (taproot) addresses an X coordinate of a (tweaked) public key is encoded directly (unhashed), but without Y coordinate.

Thanks. Clears things out

I got a few more questions. Suppose that we have a Public Key that is generated by an "odd" private key like113*G.
What will happen if we try to "half" this public key using these methods?
https://crypto.stackexchange.com/questions/59972/half-of-any-bitcoin-crypto-public-key-public-key-half-is-possible
https://bitcointalk.org/index.php?topic=4455904.0
Does "halving" an even Public Key makes any difference?

Then you obtain the public key corresponding to the private key (113/2 mod n)*G. Halving is just multiplying by (1/2 mod n) = 57896044618658097711785492504343953926418782139537452191302581570759080747169 (both the private and public key). Whether the resulting private key is odd or even depends on how many times that multiplication wraps around n.


text:
Transaction keep beeing rejected "non-mandatory-script-verify-flag"
I'm developping Bitcoin integration for Microsoft Dynamics Business Central to offer enterprise capability to apply Bitcoin receivable and payable to accounting.
To send a transaction, I'm using NBitcoin C# lib to prepare and sign raw transaction data before broadcasting but I keep having bellow error on Main Net.
It is the most basic transaction with one input and one output.
Both addresses are P2SH.
The transaction signature verification pass correctly in NBitcoin but the node keep returning the error "Code: -26, Error: non-mandatory-script-verify-flag (Witness requires empty scriptSig)".
Raw Transaction Data :
01000000000101701c2bcfe6bc56becaf1c58a45f57de5558107a3f1fdd51bb4a0c1ce27dff30a0900000017160014e38512a037ee9199b5dd5e7af4b928453b523cc2ffffffff01701101000000000016001425ce1fbd74537b45319cb0553deec89f4289430102473044022012e3cee7d952acf046e3e282162d1cb4e6ff954a01b19fba8d3606b63256c9eb022079099799cb58d3b45ca85c678336c802889fe7d6c18012c5a1509d1b54441492012103220927928080eecf1af164e3413ed6733cfc27172174683aecb3bd2041d06a0000000000
When I decode the transaction I see the witness on input correctly and the output adress look fine too.
If I try to send the same transaction but using P2WPKH address I'm having a different error : 'non-mandatory-script-verify-flag (Script failed an OP_EQUALVERIFY operation)"'
Could someone know whats wrong in my transaction ? Anyhow I'm pretty sure I'm just not passing the right parameter to the library but idk wich

Code: -26, Error: non-mandatory-script-verify-flag (Witness requires empty scriptSig)

bitcoin-cli is telling you that, in order for a SegWit transaction to be valid, the scripSig field must be empty (the data is placed in the witness instead). Assuming your witness is right, you should see something like this when decoding the raw transaction:
"vin": [
  {
    "txid": "0af3df27cec1a0b41bd5fdf1a3078155e57df5458ac5f1cabe56bce6cf2b1c70",
    "vout": 9,
    "scriptSig": {
      "asm": "",
      "hex": ""
    },
    "txinwitness": [
      "3044022012e3cee7d952acf046e3e282162d1cb4e6ff954a01b19fba8d3606b63256c9eb022079099799cb58d3b45ca85c678336c802889fe7d6c18012c5a1509d1b5444149201",
      "03220927928080eecf1af164e3413ed6733cfc27172174683aecb3bd2041d06a00"
    ],
    "sequence": 4294967295
}
]

However, yours does have data in the scriptSig:
"scriptSig": {
      "asm": "0014e38512a037ee9199b5dd5e7af4b928453b523cc2",
      "hex": "160014e38512a037ee9199b5dd5e7af4b928453b523cc2"
}

Anyhow I'm pretty sure I'm just not passing the right parameter to the library but idk wich

I'm not familiar with NBitcoin, but I'm pretty sure you're right in assuming you may be passing a wrong parameter to your transaction constructor.

Thank, it you make sense.
Actually I find out was just using a wrong Private Key to sign the transaction... And the library was building this weird input script/witness because the PK did not match the UTXO unlocking script.


text:
Running lightning in prune mode
I am running lightning with the LND implementation and a full node. Nevertheless, I heard that it is possible to use Lighting in pruned mode.
How is this possible?

I read in the LND install documentation that you need the -txindex flag:

In order to run lnd with a bitcoind back-end, the bitcoind instance must be configured with --txindex just like btcd above.

In the Wikipage of Bitcoin it says for pruned mode:

This mode is incompatible with -txindex

I also read here and here the same arguments.
Is this outdated or is it not possible to use prune mode in lightning?

You are correct in that you cannot run a LND node with a pruned bitcoind server.
Pruning isn't compatible with transaction indexing because the index would point to transactions which have been pruned (no longer in store).
Each lightning node requires transaction indexing, because it will be looking for specific txid's on-chain throughout the lifetime of the payment channel.
Broadcast and confirmation of:

Funding transaction
Commitment transactions in a unilateral close
Closing transaction

The only way to determine whether any of the transactions above have been confirmed, is to query an up-to-date transaction index of the strong chain, which is not possible with a pruned node.
Lightning nodes also subscribe to the ZeroMQ subscription service of the bitcoind node, but this only tells the node about newly broadcast transactions accepted by in the mempool, not whether they have been confirmed yet.

I've not tried it, but what happens when I start a lightning node in pruned mode in practice?

The lightning node will not be able to query bitcoind for transactions (by txid).

When would I get aware of this?

I dont know, the lnd node will through an error, I dont know exactly how the error msg will look like.

Ok, thx for response!

I keep getting conflicting information about this topic. Here it says that LND will just be slower, but it will still work: https://medium.com/lightning-power-users/284bd5034340 Which one is true?

I researched the issue a bit more. The answer from James C. is correct when working with older versions of Bitcoin Core. 0.16 supports txindex in pruned mode.

LND supports pruned nodes as of https://github.com/lightningnetwork/lnd/releases/tag/v0.13.0-beta.rc5

Update(2021): Since version 0.13 pruned nodes are officially supported although they cause performance penalty and higher network usage.
It is possible to run Lightning (both LND and c-lightning) with bitcoind pruned mode.
There are already packages turning the pruned mode on when disk space is limited: Lightning Power Node Launcher (works with LND) and BTCPayServer docker (works with c-lightnig).
There is a lot of conflicting information online, because it became possible only after Bitcoin Core 0.16.0 was released.
Update from the official lnd documentation:

We don't require --txindex when running with bitcoind but
activating the txindex will generally make lnd run faster.
NOTE: WE DO NOT FULLY SUPPORT PRUNED OPERATING MODES FOR FULL NODES.
It's possible to run a node in a pruned mode and have it serve lnd,
however one must take care to ensure that lnd has all blocks on disk
since the birth of the wallet, and the age of the earliest channels
(which were created around March 2018).

I believe your answer is either incorrect or at least misleading. I tried running bitcoind in pruned mode with txindex enabled, but its startup just fails with message "Error: Prune mode is incompatible with -txindex.".
On the other hand it is possible running bitcoind with pruned mode and txindex disabled. This will however make lnd fallback to lookup and download the missing tx info from peers by itself.

I read through BIP159, but all it does is signalling, that the node is running in pruned mode hence will provide only latest blocks. It even recommends hiding prune depth by refusing to provide blocks older than 288 blocks from the tip. If the actual bitcoind implementation obeys this recommendation, it would make bitcoind even less usable for tx info lookup after BIP159 implementation.

You are certainly right: BIP159 reference is confusing, I will edit the answer to remove it.


text:
How does `bitcoind` generate the private key?
My question is how the bitcoin core client generates a (master) private key(s)?
I know from other wallets, that they take the mnemonic and generate a HD wallet master key. With the master key, the other keys can be generated.
To my question. How does the bitcoin core software do it? I've never seen a mnemonic there.

Does it use a mnemonic?
I know about the backupwallet command to make a backup.
Is there nothing like a mnemonic I can use? If there isn't, do I have to constantly do backups?

Bitcoind does not support BIP39, which encodes a seed (128, 160, 192, 224, 256 bits) in a mnemonic phrase, from which the 512b HD root seed is later generated via PBKDF2 (Password-Based Key Derivation Function 2). This 512b root seed is used to derive the (extended) master keys.
For wallets without BIP39, the 512b HD root seed is directly supplied as entropy, without the intermediary step involving an encoded mnemonic phrase and PBKDF2.
Backing up the wallet.dat file.
https://bitcoin.stackexchange.com/a/22722/90088
Amended:
Bitcoin Core wallets will deterministically derive new extended key pairs and addresses as wallet usage progresses, which is reflected and updated in the wallet.dat file. However, when restoring a wallet from an older wallet file, the wallet will (re)derive all standard child key derivation paths, whilst (re)scanning the blockchain to determine past usage of these keys in transactions, thereby recreating the last used hd wallet state. The wallet can be fully restored, even if the wallet file backup is not the most recent one because of this rescan.

So could you expand on  the 512b HD root seed is directly supplied as entropy? The 512b HD root seed is randomly generated.  What's used for the randomness(entropy)?

And that means that I have to continously make backups from my keys because I don't have a seed ?

@dvg New Bitcoin Core wallets do use BIP32 (with hardened derivation), so the keys are deterministic, and you don't need a new backup all the time. There just is no text form of the master key.

@PieterWuille Thanks. I have amended the answer accordingly.

Thx @PieterWuille & JamesC.  for the helpful responses!


text:
How to add multihreading to a continuous SECP256K1 Python function?
I have an infinite loop Python function for measuring how fast are SECP256K1 public Keys are generated.
The script:
from time import time

a = 0
b = 7
n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
gx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
prime = 2**256 - 2**32 - 977

def addition(currentX, currentY, gx, gy, a, b, prime):
    if gy == 0:
        return (None, None)
    elif currentX is None and currentY is None:
        return (gx, gy)
    elif currentX == gx and currentY != gy:
        return (None, None)
    elif currentX == gx and currentY == gy and currentY == 0:
        return (None, None)
    elif currentX == gx and currentY == gy:
        s1 = (3 * pow(gx, 2, prime) + a) % prime
        s2 = (gy * 2) % prime
        s = (s1 * pow(s2, (prime - 2), prime)) % prime
        currentX = (s ** 2 - 2 * gx) % prime
        currentY = (s * (gx - currentX) - gy) % prime
    elif currentX != gx:
        s1 = (currentY - gy)
        s2 = (currentX - gx)
        s = (s1 * pow(s2, (prime - 2), prime)) % prime
        currentX = ((s ** 2) - gx - currentX) % prime
        currentY = ((s * (gx - currentX)) - gy) % prime

    return (currentX, currentY)

def secp256k1BinaryExpansion(privateKey, gx, gy, a, b, prime):
    #if pow(gy, 2, prime) != (pow(gx, 3, prime) + a * gx + b) % prime:
        #return "The point is not on the curve"
    coef = privateKey
    currentX, currentY = gx, gy
    resultX, resultY = None, None
    while coef:
        if coef & 1:
            resultX, resultY = addition(resultX, resultY, currentX, currentY, a, b, prime)
        currentX, currentY = addition(currentX, currentY, currentX, currentY, a, b, prime)
        coef >>= 1
    return (resultX, resultY)

def testLoop(gx, gy, a, b, prime):
    count = 1 #Count is the number of all calculations
    counter = 0 #Counter is for measuring the speed of the function
    timeOne = time()
    pubX, pubY = None, None
    while True:
        pubX, pubY = secp256k1BinaryExpansion(count, gx, gy, a, b, prime)
        #print("Case ", count,":", pubX,pubY)
        count += 1
        counter += 1
        timeTwo = time()
        if (timeTwo - timeOne) >= 10:
            print("The speed is: ", counter / (timeTwo - timeOne), "c/s")
            timeOne = time()
            counter = 0

testLoop(gx, gy, a, b, prime)

Whenever I am launching the script on Pycharm, it outputs aroud 100 c/s on Windows and 300 c/s on Ubuntu.
When it happens, on both os, only 1 core out ouf 4 gets loaded with this task for 100%, hence only 25% of CPU power is allocated to this. The CPU: intel core i5-4440 cpu @ 3.10ghz
I'd like to allocate 2-3 cores to the task, so it gets loaded like: 50-75%.
The truth is I've read documentation and watched tutorials on Python Parallelism/Multithreading and it's confusing.
Not really sure how to allocate a single job across the cores.
May be you could help out?

If you just want to benchmark to see the performance when loading multiple cores, just run multiple instances of the benchmark simultaneously, and sum their rates.
Other than that, you're out of luck. While Python supports multiple threads within a single process, only one can actually be executing Python code at a time.

You think that C++ is much better and easier for speeding up a computation like this?

Python just isn't low-level enough to get good performance; even ignoring issues with parallellism, Python will be orders of magnitude slower than what you can do in C-like languages.


text:
Wrong hashes for blocks when parsing from blk
Hello I'm using a python script to actually retrieve information from a blk file and to read data.
import datetime

# Read block !
file = open("REDACTED\blocks\\blk00000.dat", 'rb')

numberOfBlocks = 0
i = 0
toAnalyse = 5000

while i < toAnalyse:

    block_magic_bytes = file.read(4)

    if b''==block_magic_bytes :
        break

    block_size = file.read(4)
    block_size_int = int.from_bytes(block_size,'little')
    block_version = file.read(4)
    block_lastblock = file.read(32)
    block_lastblock_little_endian = int.from_bytes(block_lastblock,'little')

    # merkle root
    file.seek(32,1)

    block_time = int.from_bytes(file.read(4),'little')

    #bits
    file.seek(4,1)

    block_nonce = int.from_bytes(file.read(4),'little')

    # Prints some information
    print("Block number   : " + str(numberOfBlocks))
    print(numberOfBlocks)

    print("Magic bytes    : " + block_magic_bytes.hex(" ").upper())
    print("Block size hex : " + block_size.hex(" ").upper())
    print("Block size int : " + str(block_size_int))
    print("Block version  : " + block_version.hex(" "))
    print( f'Block preBlock : {block_lastblock_little_endian:064x}' )
    print("Block time     : " + str(datetime.datetime.fromtimestamp(block_time)))
    print("Block nonce    : " + str(block_nonce))
    print()

    # Jumps to next magic_bytes
    file.seek(block_size_int-4-32-32-4-4-4,1)

    numberOfBlocks+=1
    i+=1
print("Last block read")

But when I read the block 486 in the blk file I get this :
Block number   : 486
486
Magic bytes    : F9 BE B4 D9
Block size hex : D8 00 00 00
Block size int : 216
Block version  : 01 00 00 00
Block preBlock : 00000000806df68baab17e49e567d4211177fef4849ffd8242d095c6a1169f45
Block time     : 2009-01-14 22:27:29
Block nonce    : 2063568441

As I understand the BTC blockchain the previous hash should be the hash of the block with height 485, but when I look using an explorer this is the hash of block 499 : https://www.blockchain.com/btc/block/00000000806df68baab17e49e567d4211177fef4849ffd8242d095c6a1169f45
Is it that blocks aren't in order in blk files ? Are my blk files wrong ? I downloaded them using bitcoin-core. Are some blocks actually not included in the overall blockchain ?
I would appreciate any pointers,tips or help of course ^^

Is it that blocks aren't in order in blk files ?

This is true. For example, during the initial-block-loading phase, your node may be concurrently collecting different ranges of blocks from multiple peers. So they can arrive out of order. You also have dead forks.
As Murch noted: "the .blk files store block data in the order that it is received by the node, rather than in the order it was requested or the order of their heights."
So nodes, or other programs processing the block data files probably all need to create indexes of blocks by block-ID and height as well as indexes of transactions by transaction-ID etc. Some indexes might be of all blocks and others might be only of blocks in the currently active chain.
I can't tell whether the above accounts for the whole of your issue.

Thanks for the explanation, so theoritically I have to parse and hash every block. And then looking into another block if the hash matches starting from block with height 0 ?
But dead forks aren't on the main net are they ?
And thanks for the python tip :D ^^

Yes, you need to do something like build an index of blocks with the block hash as key. Since occasionally two miners produce blocks at the same height, your BLK files will contain both even after one is selected as constituting part of the main chain and the other marked stale.

It's in there, but I feel it could be perhaps stated a bit more clearly that the .blk files store block data in the order that it is received by the node, rather than in the order it was requested or the order of their heights.


text:
How is a block found in pools via “shares” given to miners?
It is stated that the mining pool’s server assigns each miner a specific set of cryptographic calculations called “shares.
Can someone explain in detail how these “shares” fit into finding a solution or block?

The pool sets its members an easier target than the current Bitcoin network target. Every time a pool member finds a block with a hash less than the pool target they submit the block to the pool controller and thereby earn a "share". The pool controller checks that the submitted block has a hash less than the pool target and that the block's coinbase pays the pool controller (or some equivalent rule).
So the "share" is just a way of keeping track of hashrate contributed to the pool by each member.
The process for mining is independent of target. So occasionally, the hash of one of the submitted blocks is not only less than the easy pool target, it is also less than the harder Bitcoin network target, so the pool controller can submit that block to the Bitcoin network, receive the block reward and pay out pool members based on the number of shares won by each pool member recently.
The exact details of pool operation depend on the specific network protocols used by the pool. A popular protocol is stratum v2. You'll see that the submitted block itself is sometimes referred to as a "share" or "job". 1 completed job == 1 submitted block == 1 share.

Great answer !  Thanks !  If you like to add more details anywhere please do so…again thanks that is certainly something I did not know.

Follow up question…is the pool target the same for all miners?

I think the pool target would have to be the same for all members of a pool, otherwise I believe it would unfairly disadvantage some and drive them to other fairer pools.


text:
Can I use Copay wallet from Iran?
Can I use Copay wallet from Iran? Or Iran IP is blocked? Because some wallets blocked money of Iranian users because of sanctions programs administered by the U.S. I need to know if my money will be safe in my copay wallet?

Yes you can but I recommend Electrum instead because with Electrum you are not relying on one server for your view of the blockchain.
Copay and Electrum are both wallets that give you control of and responsibility for your private keys. Your private keys are all generated from your seed which will be displayed to you during wallet creation as 12 random English words. Only someone with knowledge of your seed will be able to move your bitcoins and since you are the only one with that knowledge only you control them. No government or corporation can freeze your bitcoins or censor your transactions.
You can find a list of other such wallets here.

Hello, thanks a lot for your attention. do you know about GreenAddres? is it good too?

@HosseinMN yes it's reputable however like copay you rely on one server for transaction data.

I need to know if my money will be safe in my copay wallet?

Your money is never in the wallet. The only important thing that is in a wallet is the private key.
Your money is actually in the bitcoin "Blockchain" ledger that is copied in every full bitcoin node (wallets, miners, exchanges, etc) worldwide.
So long as you have a copy of the private key in a safe place (e.g. written on paper in a locked safe), you can always regain access to your money by importing the private key into any type of wallet that supports bitcoin (BTC) or whatever similar crypto-currency you are  using.
So if you have done this, it doesn't matter if the US government shutdown Copay.
If you don't have your private key (or, where applicable, your recovery phrase) written down somewhere safe, and have not practised recovery, you, or your heirs, are much much more likely to lose all your money.


text:
In case of a grouped transaction, how to determine who sent what to whom?
Trying to understand how grouped transactions work, I came accross the following transaction on blockchain.com :

There are many senders, and even more receivers. For instance, the first sender on the picture could have sent 1 BTC to the first receiver, 10 to the fifth, and so on...
How can I determine (if it is possible) what amount sender A sent to receiver B , instead having the balance for each participant ?

How can I determine (if it is possible) what amount sender A sent to receiver B , instead having the balance for each participant ?

You pretty much can't. Often times, transactions like these are created for the purpose of making it difficult to determine which inputs are sending money to which outputs. Such transactions are called CoinJoins, so named because multiple people are joining their coins together in one transaction.
There are some techniques for determining which outputs are created by which inputs, but these techniques are not foolproof and often make assumptions that are not guaranteed. In fact, many software that make Coinjoin transactions are designed to make those assumptions non-viable and thus break such analysis so that the participants in the CoinJoin have more financial privacy.

It should be viewed as a single transaction and not who sent bitcoins to whom. On the left hand side you have the inputs to the transaction, and on the right hand side you have the outputs. In Bitcoin, you do not have the concept of account balance. Each output of a transaction is locked with a script and anyone who provides solution to that script can spend those bitcoins.
Here is an example. Let us assume you run a business and have few employees. Your business takes only bitcoins as a form of payment and at the end of the day you make provide daily wages to your employees. So you would have your customers sending bitcoins to your company's address when they use your product. You present a different address to your customers every time so as that you know which customer sent how many bitcoins and also to maintain privacy. Now when you are making payments to your employees at the end of the day, you do not have enough bitcoins earned from one customer so you have to aggregate a few transactions from many customers. That is an example of what you are seeing.
In a generic scenario, even if all the customers make payment to a single address, each transaction that your customer will send you will have a different txid and output index which you will have to provide when you are spending those bitcoins. So you will still have to provide multiple inputs to your transaction when making payments where one single customer transactions does not cover it.
EDIT: Also as @AndrewChow mentioned in his answer, even if this was a CoinJoin transaction (multiple people joining inputs so as to mask who sent to whom), it is still very difficult to determine which outputs are created by which inputs and this method is also not fullproof.

As you were talking about customers and business, a question came to my mind : in both real and online shops (that accept bitcoin), do sellers generate a new address for each customer or each purchase ?

@EinderJam it depends. If it is an online transaction it is much easier to produce a different address everytime. Also, some vendors offer wallet service, and as that is centralized they can track payments. But if you are visiting a brick and mortar store, the address is generally static, however, the store can create different addresses if they install some software.

If a store has only one address to receive payments from customers, how can it identify each customer and verify he paid ?

@EinderJam There is no way to know it for sure. The best way is to generate a unique address for each transaction. There can be scripts generated to use a P2SH transaction, but that would also mean generating unique scripts for each customer.

the left list are not the senders, these are transactions inputs and not the sender, to the right those are the outputs (receivers addresses).
Please read more about Bitcoin transactions if you're interested https://en.bitcoin.it/wiki/Transaction

Thank you for your answer. If I understand correctly, the receivers are listed on the right, but senders are not listed on the left. However, the left list (transactions inputs) contains addresses, so what are these addresses if they aren't senders addresses ? So, how can I find the sender(s) on a given transaction ?

Multiple "sender addresses" does not mean that multiple people are sending. It could be the same person that has many addresses and inputs.


text:
Can't connect to rpc.blockchain.info with bitcoin-cli
I'm trying to, given a bitcoin address, retrieve the last X transactions to this address. After researches, I found this to be an interesting solution, as I don't want to download and validate the entire blockchain for this simple purpose.
On my Windows 10 machine, I downloaded bitcoin-core and tried to send a getinfo (I didn't execute bitcoin-qt as I don't want it to start syncing with the blockchain) :
./bitcoin-cli -rpcconnect=rpc.blockchain.info -rpcport=80 -rpcuser=my_id -rpcpassword=my_password getinfo

I got an error message complaining about bitcoin.conf and credentials (indeed, there was no bitcoin.conf file in %APPDATA%\Roaming\Bitcoin\)
So I created a bitcoin.conf file :
server=1
rpcuser=my_id
rpcpassword=my_password
rpcport=80
rpcconnect=rpc.blockchain.info
#rpcssl=1        #SSL mode for RPC is no longer supported

However, I get this error (bitcoin-cli has been allowed to access the web in Windows Firewall) :
error: couldn't connect to server: EOF reached (code 1)
(make sure server is running and you are connecting to the correct RPC port)

Has the service been discontinued ? Or is there a problem with my configuration ?

looks like you're not the only one having problems: https://bitcointalk.org/index.php?topic=1761533.0

Oh, the topic has been created in january 2017..... I think they stopped providing this service, but didn't removed the doc webpage... Is there any alternative ? All I want is basically getting the result of  listtransactions and parse the result to get senders and transaction amount (in a C++ app if it matters). By the way, replacing .info with .com doesn't help either...

You mean like this? https://blockchain.info/rawaddr/$bitcoin_address

This is P-E-R-F-E-C-T ! And I don't need to install anything on my system... Thank you !

cool let's make an answer so people can find it more easily

It appears this service is no longer available per other discussions (and a failed ping of rpc.blockchain.info).
An alternative would be to use the address query on blockchain.info's api:
https://blockchain.info/rawaddr/$bitcoin_address
Example
Single Address
https://blockchain.info/rawaddr/1AJbsFZ64EpEfS5UAjAfcUG8pH8Jn3rn1F
Address can be base58 or hash160

Optional limit parameter to show n transactions e.g. &limit=50 (Default: 50, Max: 50)
Optional offset parameter to skip the first n transactions e.g. &offset=100 (Page 2 for limit 50)

{
    "hash160":"660d4ef3a743e3e696ad990364e555c271ad504b",
    "address":"1AJbsFZ64EpEfS5UAjAfcUG8pH8Jn3rn1F",
    "n_tx":17,
    "n_unredeemed":2,
    "total_received":1031350000,
    "total_sent":931250000,
    "final_balance":100100000,
    "txs":[--Array of Transactions--]
}


text:
Trying to make SIGHASH_SINGLE transaction
I am trying to make a SIGHASH_SINGLE transaction.
I have two inputs, one output. I understand that second input can be not signed (really?).
Script I have is:
#!/usr/bin/env python3

import hashlib
import subprocess
from bitcoin import SelectParams
from bitcoin.core import b2x, lx, COIN, COutPoint, CMutableTxOut, CMutableTxIn, CMutableTransaction, Hash160
from bitcoin.core.script import CScript, OP_DUP, OP_HASH160, OP_EQUALVERIFY, OP_CHECKSIG, SignatureHash, SIGHASH_ALL, SIGHASH_SINGLE
from bitcoin.core.scripteval import VerifyScript, SCRIPT_VERIFY_P2SH
from bitcoin.wallet import CBitcoinAddress, CBitcoinSecret

SelectParams('mainnet')
h = b'private key bytes in python hex format'
seckey = CBitcoinSecret.from_secret_bytes(h)
txid = lx('tx number 1')
vout = 0
txid2 = lx('tx number 2')
vout2 = 1
txin = CMutableTxIn(COutPoint(txid, vout))
txin2 = CMutableTxIn(COutPoint(txid2, vout2))
txin_scriptPubKey = CScript([OP_DUP, OP_HASH160, Hash160(seckey.pub), OP_EQUALVERIFY, OP_CHECKSIG])
txout = CMutableTxOut(10000, CBitcoinAddress('output address').to_scriptPubKey())
tx = CMutableTransaction([txin]+[txin2], [txout])
sighash = SignatureHash(txin_scriptPubKey, tx, 0, SIGHASH_SINGLE)
sig = seckey.sign(sighash) + bytes([SIGHASH_SINGLE])
txin.scriptSig = CScript([sig, seckey.pub])
VerifyScript(txin.scriptSig, txin_scriptPubKey, tx, 0, (SCRIPT_VERIFY_P2SH,))
print(b2x(tx.serialize()))
r=subprocess.run(["/mnt/c/Program Files/Bitcoin/daemon/bitcoin-cli.exe", "sendrawtransaction", b2x(tx.serialize())])

I am getting error:
error code: -25
error message:
bad-txns-inputs-missingorspent

Both inputs have balance, I am sure of that. Script is run from WSL1 Bash on Windows.
What am I missing here?

I understand that second input can be not signed (really?).

No, it can and still must be signed. The use of SIGHASH_SINGLE does not remove the need to produce a valid signature. The script interpreter will not know or care that you wanted to use SIGHASH_SINGLE there or that other signatures in the transaction used SIGHASH_SINGLE. All it wants is a signature, the sighash type doesn't matter. Keep in mind that each signature can use a different sighash type, they don't all need to be the same.
SIGHASH_SINGLE for an input that does not have an output at the same index use the number 1 (serialized as a 256 bit little endian integer) as the sighash. Python-bitcoinlib's RawSignatureHash will do this for you. SignatureHash does not because it's generally unsafe to produce such a signature since the input can be trivially included in another transaction by a someone else and your funds taken.

Do I need to make separate CMutableTxIn, CScript, SignatureHash and RawSignatureHash for second input?

Yes. Inputs are independent of each other.


text:
Check public address balance
I am willing to write Python script to check for some public addresses balances. I understand that I need to access API of known exchanges (Coinbase, Binance). I also know that there will be limit of requests sent in time.
As far as I can see there is possibility to check for wallet balances. But what about checking some public BTC addresses?

Solution script:
#!/usr/bin/env python3

import requests

addr=input('Enter address to check: ')

if addr=='':
    exit()

r=requests.get('https://blockchain.info/q/addressbalance/'+addr)

if not r.status_code==200:
    print('Error',r.status_code)
    exit()

b=int(r.text)

print(addr,'\t',r.text,'sat\t',b/100000,'mBTC\t',b/100000000,'BTC')

You can use services like blockchain.com, blockchair.com or tokenview.io to explore crypto addresses. It will show you balance, a list of transactions, date and time and some additional info.
As for your script, you can use requests library to send requests to API and receive JSON data or scrape the web-sites directly using requests and beautifulsoup.

You can access an API and be limited by their ToS but you don't have to.
All transactions (TX) and BTC addresses (pubKey) are public on the blockchain with the distinction that all TX are recorded, but pubKeys show up only once mentioned in a TX.
Assuming you're talking about bitcoin and not something else you should run bitcoind on your machine and query it locally without limitations.
bitcoin-cli getbalance

getbalance is about wallet, not transactions in the blockchain...


text:
Can LN clog main chain BTC?
Main chain BTC can handle something about 300k tx per day without increasing fees, open and closing channels need to be done using standard tx on main chain. So the question is if we will have large world adoption, can LN can produce 300k tx per day (sum of opening and closing txs)?

The answer is yes, but not anytime soon.  Also, it's a function of how frequently channels are opened and closed, which can be estimated, but is not "really" predictable.
If you read the LN whitepaper [link], it mentions in the conclusion that "to enable 7 billion people to make two channels per year with unlimited transactions inside the channel, it would require 133 MB blocks"

As Wikipedia notes, "the Lightning Network ... has been touted as a solution to the bitcoin scalability problem".
So part of the objective of the Lightning network is to reduce load on the Bitcoin blockchain in order to allow a higher rate of Bitcoin transactions than would otherwise be possible.
Without LN, or some major changes with similar effect, Bitcoin would likely lose market share to other crypto-currencies with better scalability and faster, cheaper confirmation of transactions.


text:
Is there any tool to check sum of all BTC based on transactions?
Is there any tool to check sum of all BTC based on transactions? We can easily get this amonut by sum up mining rewards, but what about sum up all non zero wallet balances?

I don't know of an existing tool that does this, but it should be easy enough to iterate over each block and sum up the outputs.

are you sure that method? when I send you 1 BTC, and in next block you will send me 1BTC sum of our outputs will be 2 BTC, but we was operate using only 1 BTC

Ah, yes, you are right. You will to sum all inputs and all outputs, and subtract at the end.

but each transaction input need to be equal output right? so the result will be 0

Since all coins start from a coinbase tx with a 0 input, at the end your outputs will exceed your inputs by the amount of BTC created

Sort of.
Bitcoin Core's gettxoutsetinfo RPC command iterates through the entire UTXO set (the set of outputs that haven't been spent yet by another transaction).
Currently, it reports:
"height": 539935
"total_amount": 17249017.33071828

This is the approach you're looking for as it is computed from the actual outputs and inputs, though with a few caveats:

It is not computed on-the-fly from the blockchain; instead, it uses a database with unspent outputs that is always maintained by the validation code.
It excludes provably unspendable outputs (OP_RETURN outputs or outputs with a script longer than 10000 characters).

Note the discrepancy with the expected amount in circulation at that height (17249200.00000000). There are various reasons for this. My answer https://bitcoin.stackexchange.com/a/38998/208 goes into the details.


text:
Instant BTC to USD transfer?
is there a way to have BTC sent from somewhere to my wallet and then that amount is immediately transferred to USD? I know that Coinbase offered this once but they have since removed that feature. I am just wondering if there is a similar service out there.
Thanks.

This is a product recommendation question, which isn't in the scope of this site. You can search the many bitcoin exchanges there are several that allow transferring between fiat and bitcoin. Note this depends on the country you live in as well.

You may want to check BTCPay Server
More links for source and documentation.
In the past I would have recommended BitPay, but they've had too many red-flags that show that this company does not have the best interest of Bitcoin in mind.


text:
Do private blockchains need consensus protocols?
My understanding is that in a private blockchain, the participating nodes verify and agree that the block is valid and only then add it to the blockchain.In this scenario, are consensus protocols (such as RAFT, PAXOS, etc.) needed? Am I missing anything here?

TL;DR Yes, consensus protocols are always needed in distributed systems where there could be multiple writers to the same data. When it is possible for multiple participants to simultaneously update their local state, it can result in two inconsistent versions which can't be merged together into a globally consistent state.
In the context of a blockchain, publishing a block is analogous to reading and writing to balances, and we wish to enforce the constraint that every account balance >= 0 to prevent double spending.

Imagine that in the ledger state of block 4, I had 10 coins. Then in Branch A, I sent Alice 8 coins, but in Branch B I sent Bob 8 coins. If we tried to reconcile these two updates into one state then either I would have a balance of -6 coins, violating the balance >= 0 constraint, or coins would have to be illegally created.
Consensus algorithms are designed to ensure that each node has a valid state consistent with other nodes. As explained by CAP theorem, during a network partition a consensus algorithm must either make the service unavailable (Read Only) or allow for the two partitions to potentially become inconsistant. This means that when designing or choosing a consensus algorithm, one must decide between either consistency of the data (e.g. preventing double spend) or availability (e.g. making a transaction).
Nakamoto consensus prioritizes availability such that during a network partition it will continue to process transactions, and after a network partition, the global state will eventually be consistent. It quantifies each state's total proof of work to decide which state is the correct state. If our node had previously only observed branch A but then received the blocks from branch B, it would calculate the amount of work done in each state to decide which branch to accept. This means that during a network partition, the state is still available to be updated at the expense of it becoming inconsistent. After the network partition is resolved, the inconsistent states are resolved by reverting some of the updates and applying the ones that lead to a state with more proof of work.
Other consensus protocols such as the proof of stake variant used in Tendermint will prioritize consistency, meaning that during a network partition attempts to update the state may fail, making the service unavailable. To ensure consistent updates to the state (i.e. creating a block) nodes that controll at least 2/3 of the stake, must agree to a proposed block within a timeout period. If too little stake agrees to the proposed update, it will fail and revert and a new proposal will begin. This is somewhat comparable to a 2 phase commit, but differs in that it can tolerate the failure of nodes controlling up to 1/3 of the stake.
Even in a trusted environment where all machines are behaving correctly, inconsistent states can arise due to network latency, as asynchronous messaging is comparable to short-lived network partitions. Keeping state consistent becomes somewhat more difficult where we have more complex constraints to uphold (e.g. ensuring consistent smart contract execution)
Other non-blockchain private distributed systems, must make the same trade-off between Consistency and Availability, Google's BigTable maintains availability and replicates updates to other nodes making it eventually consistent throughout their data centers, for coordination within a BigTable cluster it relies on a fault tolerance lock distributed service named Chubby which uses paxos during leader elections. However, It seems that due to the reliability of Google's private WAN  (>99.9995% availability) and very accurate time-keeping, they have also built Spanner, the accuracy of their timestamps allow for updates to be ordered (or delayed if there is uncertainty about the order) such that an application's view of the data is consistent with the application's constraints and other invariants.
I highlight CAP theorem to explain how Nakamoto Consensus will always be available but subject to the eventual consistency, potentially allowing for a 51% attack. Other consensus protocols can provide finalization, but may not always be available opening them up to a system-halting DDoS attack. In the context of a private system, control of the hardware means that the distributed system can be virtually always available and shielded from DoS attacks, leading companies like Google to prefer the provision of consistency.

Yes consensus is needed and for the consensus RAFT ,PAXOS is used for general node failure.If their is malicious node Byzantine Fault Tolerance can be used.But POW isn't used in case of private blockchain as the participating users are known.


text:
If i pay 0.001 to cloud mining site such az minezone.io or bitbase.club or 99quick.cloud
If i pay 0.001 to cloud mining site such az minezone.io or bitbase.club or 99quick.cloud, they give my balance or no

Pretty much all cloud mining is a scam

I wouldn't trust them at all.
Take minezone.io for example, they claim to be a London-based company. The company number they have on their "FAQ" page is for BITEXPLOSION LIMITED whose registration status is "Active — Active proposal to strike off". Being struck off is a bad thing. Their annual confirmation statement is four months overdue. That also is a bad sign.
Bitbase.club's website is an almost exact copy and also gives the same company number (i.e. BITEXPLOSION LIMITED)
The 99Click.cloud domain doesn't seem to exist.


text:
Minimal value for xprv
If we have "smallest" (all characters minimal ASCII) pub address 1111111111111111111114oLvT2 which does not work in the network,
and smallest pub address 111111111111111111112BEH2ro which does work,
what would be the "smallest" xprv key for Bitcoin mainnet that will work?

What do you mean by "minimal ASCII". Wouldn't that be ASCII character 0x00 "NULL"? Wouldn't the minimal printable ASCII character be 0x20 "SPACE" and the minimal ASCII digit be 0x30 "0"?

@RedGrittyBrick yes, that's what I mean. In this case seems like the minimal will be 1.


text:
Trying to calculate the average fee per tx, but can't get the right value
We are creating a cryptocurrency analytics website and I was wondering how one can calculate the average transaction fee for Bitcoin. For example, like on these three sources.
https://www.blockchain.com/charts/fees-usd-per-transaction
https://ycharts.com/indicators/bitcoin_average_transaction_fee
https://bitinfocharts.com/comparison/bitcoin-transactionfees.html#3m
They indicate that currently the average BTC transaction fee is $1.079, but when I try to calculate it myself I don't get the same result.
I know that the fee is calculated in this way: fee = feerate * tx_size
So I get the rates either from here (https://api.blockchain.info/mempool/fees) or here (https://bitcoinfees.earn.com/api/v1/fees/recommended) and multiply them by the average tx_size of 225 bytes.
However, I either get 70 cents or over $4 depending on what API I use, not $1.079 like in the examples above. What am I doing wrong? Or do those websites above use some other values for calculations?
Thanks a lot and have a great weekend!

This number could be much more easily collected by reading out the total fees collected in a block and dividing it by the count of transactions in that block (deducting one for the coinbase), then translating the resulting average fee (BTC) to a USD amount.

However, I'm going to offer a frame challenge. I do not consider “average fee per transaction (USD)” a useful metric.
First of all, it would be more meaningful, if the data were collected as “average fee per transaction (BTC)” as it would remove the overlying effect of the current exchange rate from the blockspace demand situation. Secondly, the fees shift wildly over the course of the day and week, so how useful is an average even? If you want to pay less, you can get away with the minimum feerate, if you are fine with waiting for a day ~12h. So it should be cast as “average fee to get confirmation within what time”? Thirdly, a transaction that pays 100 recipients, a transaction with a single recipient, and a transaction consolidating two hundred inputs in a self-send are interpreted all the same for the calculation of this figure. If you e.g. consider the four example blocks shown here, you would get wildly different average fees—but mostly because users are trying to achieve wildly different things. Fourthly, transactions pay for blockspace, but the average value of payments on Bitcoin has increased by magnitudes over the project's existence. How comparable are costs per transaction over the years, when it meant sending $1 in average a decade ago and now the average payment is more than $10,000?
What you're trying to express with that metric? Is it a measure of how expensive it is currently to purchase blockspace? That's just the current feerate. Is it a measure of how expensive it is to transfer a certain amount of value? That depends on the user's UTXO pool and their urgency, whether they're sending to a single recipient or batching payments.
Perhaps it would be more interesting to offer figures on what the cost of getting a specific transaction confirmed in various timeframes would be. E.g. a transaction with one P2WPKH input and two P2WPKH outputs is 140.5 vB, and present what it costs to get that confirmed within the next hour vs the next day.

I know that the fee is calculated in this way: fee = feerate * tx_size

Perhaps the sites you mention are calculating the actual fee paid: sum of input amounts minus sum of output amounts.

The Blockchain is public. You can query and analyze all or part of it by running your own node. That would eliminate the unanswered questions about how the explorer APIs you're using define their calculation bases
More importantly, fees are Bitcoin amounts, not dollar amounts. Expressing fees in dollar amounts is complicated by the volatility of the dollar per Bitcoin exchange rate. That is, for a 3-month average, an accurate dollar estimate can only be calculated by knowing the point-in-time exchange rate for every single block in the period
As far as I know, there are no free public sources of block-by-block dollar per Bitcoin exchange rates
The Bitcoin transaction fee is not explicitly recorded. As the @RedGrittyBrick response says, the Bitcoin transaction fee is the sum of the output amounts subtracted from the sum of the input amounts. Note that the input amounts are not stored in the spending transaction. They are discovered by retrieving the amount from the transaction output being spent by each transaction input


text:
Unable to connect Docker based Bitcoin Node
I am using this docker image and trying to use bitcoin-cli commands. When I run `bitcoin-cli help, I get the error:
root@5c7c6e54f180:/# bitcoin-cli help
error: timeout on transient error: Could not connect to the server 127.0.0.1:8332

I am running the following docker command:
docker run --rm -it ruimarinho/bitcoin-core \
  -printtoconsole \
  -regtest=1 \
  -rpcallowip=172.17.0.0/16 \
  -rpcauth='foo:7d9ba5ae63c3d4dc30583ff4fe65a67e$9e3634e81c11659e3de036d0bf88f89cd169c1039e6e09607562d54765c649cc'

docker ps returns the following:
 ~ docker ps
CONTAINER ID   IMAGE                     COMMAND                  CREATED         STATUS         PORTS                                                              NAMES
5c7c6e54f180   ruimarinho/bitcoin-core   "/entrypoint.sh -pri…"   7 minutes ago   Up 7 minutes   8332-8333/tcp, 18332-18333/tcp, 18443-18444/tcp, 38332-38333/tcp   charming_chatterjee

I want to connect either testnet or regtest

You'll need to expose the ports you want to use, Docker doesn't do this automatically. The port depends on which network you want to use. This is documented on the homepage of the Docker image you're using: https://github.com/ruimarinho/docker-bitcoin-core#exposing-ports
Furthermore, when running bitcoin-cli you also need to specify which network you're using, e.g. bitcoin-cli -regtest help.


text:
Mining when there is no transaction
what happens when there is noone doing transactions and miners mine the blocks? Can they mine empty blocks? And how it works when there won't be any hash in empty block or there is a hash?
Thank you

Can they mine empty blocks?

The miner must include a coinbase transaction (which determines the mining reward), or the block will be rejected by the network. That way, the miner is awarded coins for solving a block (even if it contains no other transactions) and coins are distributed according to consensus rules.

And how it works when there won't be any hash in empty block or there
  is a hash?

There is still other data that is hashed, including:
version, previous block hash, merkle root, unix epoch time, difficulty target (bits), and nonce.
The merkle root including only the coinbase transaction in this example.


text:
Number of transactions in block
why sometimes block has 16 transactions and sometimes over 2000?
I checked it on https://blockexplorer.com/blocks.
Thank you

Since the block size is fixed (but has changed since genesis block) there are 2 main reasons:

Transactions are not all the same size in bytes. One transaction can have multiple inputs and outputs, increasing the size of the tx in bytes.
The market might not fill a block with transactions (esp. in the beginning blocks) because usage is low.


text:
Have all hashes of unlocked difficulty levels of cryptocurrencies been mined?
I'm wondering if all the hashes of a previously unlocked difficulty level of a cryptocurrency, such as

0000000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx BTC

, have been completely mined, where x is a placeholder for a constituting HEX digit, such that all the possible combinations of digits in that difficulty level have been mined.

No, and this will not happen in the foreseeable future.
Any given hash calculation will result in any one of 2^256 possible numbers. That means, the odds of finding any one exact number is 1/2^256.
Even at today's hashrate of ~47000000 TH/s, it would take you 7.812×10^49 years to find a preimage for every one of those 2^256 numbers, assuming each hash you find is unique (no collisions).

Then how does the hash difficulty relate to the number of solved blocks?
For instance, currently all blocks begin with 18 zeros. I'm asking specifically about all hashes that start with fewer number of zeros than the current difficulty.


text:
accessing the Litecoin testnet
I am very new to creation of cryptocurrency wallet. how to create and access the litecoin testnet and elaborate the available ways to do that?if possible, provide link to simple sample project.
Thanks

It depends, do you want to run a full node and store the entire blockchain? You could run Litecoin Core: https://github.com/litecoin-project/litecoin. There are other implementations/installation options as well.
For an example of a project with a wallet, explorer, and full node, see:
https://github.com/litecoin-project/litecore
It implements:

Node - A full node with extended capabilities using Litecoin Core
Insight Lite API - A blockchain explorer HTTP API
Insight Lite UI - A blockchain explorer web user interface
Wallet Service - A multisig HD service for wallets
Wallet Client - A client for the wallet service
CLI Wallet - A command-line based wallet client
Angular Wallet Client - An Angular based wallet client
Copay - An easy-to-use, multiplatform, multisignature, secure bitcoin wallet

thanks for your time. is above steps are initial steps to learn from scratch?

how can I open the github project?in visual studio code?

What do you mean? You can clone the repository, then add the folder to your workspace.


text:
How to get output linked to input in SPV node?
We're working on bitcoin SPV wallet. After setting bloom filter, we only get merkle blocks, and transactions that match the hashes we set plus some false positives.
If a transaction is where we SEND coins, we can calculate a fee of that transaction, because we also HAVE previous transaction holding an output we spent.
But, if a transaction is where we RECEIVE coins, then we can't calculate the fee. Because, we DON'T HAVE previous transaction holding someone's output which he spent.
As far as I know, there's no way to request single transaction from peers.
How can we get previous transaction or at least it's output's value?

You can't get a previous transaction via the p2p network based on the txid.
If you use BIP37, fees for received transactions are difficult to calculate in a decentralised way.
Have a look at BIP158 which will probably be available on the network soon.
It has a feature to find the block of your previous txout as well as it doesn't have the privacy problem of BIP37.


text:
Testing Bitcoin Core source code?
What's the recommended way to start testing the Bitcoin Core source code.
Thanks in advance,
Jim

What do you mean by 'testing'? The source is available at github.com/bitcoin/bitcoin/ , do what you will with it

@chytrik I'm talking about testing for verification of bug fixes. I'm assuming that testing code will get me familiar enough with the code to start fixing bugs myself.

What's the recommended way to start testing the Bitcoin Core source code?
testing for verification of bug fixes

See https://github.com/bitcoin/bitcoin/blob/master/src/test/README.md which says:

To run the bitcoind tests manually, launch src/test/test_bitcoin. To recompile after a test file was modified, run make and then run the test again. If you modify a non-test file, use make -C src/test to recompile only what's needed to run the bitcoind tests.
To add more bitcoind tests, add BOOST_AUTO_TEST_CASE functions to the existing .cpp files in the test/ directory or add new .cpp files that implement new BOOST_AUTO_TEST_SUITE sections.
To run the bitcoin-qt tests manually, launch src/qt/test/test_bitcoin-qt
To add more bitcoin-qt tests, add them to the src/qt/test/ directory and the src/qt/test/test_main.cpp file.

There is more.


text:
Create account and transfer usdt into the wallet adress. Used block chain to search address. Why was the adress used before i created my account?
There is an app i was led to called kzzy. You can fund your account with btc, eth, and usdt. All crypto converts to usdt once transferred to account. I am given two wallet addresses to send crypto to. An eth adress which matches usdt adress and a btc adress. When I search these addresses in a blockchain explorer I discovered more transactions than i have made. There are transactions that exist on the wallet adresses before i created the kzzy account. Also its like a futures exchange, you bet a contract high or low on btc with usdt; however i do not have any record of contract transaction #. I also cannot find any record of the contract bids place on this kzzy app on the blockchain. I Only see tranfers from my peronal wallet to the kzzy acount wallet and also gas fees.
My two questions are
-can a wallet be reused safely?
-Will the blockchain have transaction records from futures trade pool contracts?

can a wallet be reused safely?

A Bitcoin address can be re-used safely but doing so damages privacy.

Will the blockchain have transaction records from futures trade pool contracts?

Typically that kind of activity takes place off chain. It may require an initial Bitcoin transaction to transfer funds into an account provided by a market operator of some sort. It may eventually use a Bitcoin transaction when you decide to withdraw funds from that account. But those two events are usually distinct from your "futures trade pool contracts" which typically do not use the Bitcoin blockchain.
Since Bitcoin has a scripting language, there is potential for many sorts of complex arrangement. Some might involve multi-signature transactions etc. But nodes typically don't relay non-standard transactions.
It is also possible to embed arbitrary data in a Bitcoin transaction (for example see BRC-20). So it would perhaps also be possible to embed details of "futures trade pool contracts". Normal Bitcoin nodes would effectively ignore the meaning of this data. Many blockchain explorers might not make it visible in any meaningful way.


text:
Does everyone have a transaction that used a layer 2 protocol?
I'm trying to identify different transactions, and I was wondering if anyone knows a transaction that uses layer 2 protocols so I can further inspect it?

Instead of asking for one, why don't you create one?  Then you'd have an example where you know all the details.

I was wondering if anyone knows a transaction that uses layer 2 protocols

Assuming you may be looking for on-chain Bitcoin transactions created by L2 functions rather than looking for off-chain L2 protocols and transactions themselves.
One example is referenced in Why would a transaction have a locktime of 1987!
However I suspect that many on-chain transactions that are created for L2 purposes may be indistinguishable from ordinary Bitcoin transactions.


text:
Bitcoin API for getting an input script?
I wanted to try talking to my bitcoin node to get the input scripts and put them into a database. Does anyone have good resources on where I can look to get started?

So what I want to get access to is tap scripts. Like the one in mempool where they have:
OP_PUSHBYTES_32 526cfd93ec81e49633486ba01d9c8c3954e6fd755836539b9441f66a32f3beb5
OP_CHECKSIG
OP_0
OP_IF
OP_PUSHBYTES_3 6f7264
OP_PUSHBYTES_1 01
OP_PUSHBYTES_9 696d6167652f706e67
OP_0
OP_PUSHDATA2 89504e470d0a1a0a0000000d4948...(there's more I left out for the sake of space)
OP_ENDIF
Is there a way to get this from the RPC API?

See https://developer.bitcoin.org/reference/rpc/getrawtransaction.html and note the comment about -txindex
The description of the verbose result shows the unlocking script (scriptsig) for each input.
There's a different RPC call noted for wallet transactions .


text:
Recover an implement old Multisignature Wallet with different Keys
I have a problem with a multisig wallet. The keys are no longer accepted by the console. Only a part of the keys can be inserted. The wallet is then also displayed to me. What happened afterwards is inexplicable to me. Blockchain synchronization starts and stops at block 0 and 100% synchronization. I have now tried this on 3 different computers. It's the same every time.
Does anyone have experience with the same problem? Please help. Thanks to all.

What software product are you using?

Bitcoin Core 22.0 aswell Bitcoin Core 0.21.1 released


text:
Withdrawal transaction made 8 hours ago still has not had any confirmations
I made a deposit into a bitcoin exchange on 2018-09-11 06:57:41. The exchange then made a withdrawal transaction on 2018-09-11 13:19.
The time is now 2018-09-11 21:55 but the transaction still has not had any confirmations. The mempool size is currently 12 Mb which should not be large enough to cause such a delay.
The output for the withdrawal is a bech32 address. Has anyone else seen delays in withdrawal transactions, possibly to do with different address formats on the output?
https://blockchair.com/bitcoin/transaction/946d199348cdd21209e1c42f8df1700be9a238bb220d7e03ceff5b136e7cca2b
Thanks

That transaction is 90 KB in size; presumably the exchange is aggregating deposit transactions from many customers.  And its fee is about 1.3 satoshis per byte.  As you can see here, there are currently lots of outstanding transactions offering a higher fee rate; as such, a miner would likely prefer to confirm 90 KB worth of smaller and more lucrative transactions, rather than this one.
The output address type doesn't have any direct effect on confirmation time or miner preference, as far as I know.
The size of the mempool in MB is not a particularly useful statistic in estimating confirmation time (until it gets below the block size, at which point you would roughly expect that the next block will confirm everything).  Transactions don't get confirmed first-come-first-served; a low-fee transaction can languish indefinitely as long as more higher-fee transactions continue to arrive and be given priority.
Note that as a customer, this is not your problem.  Your job was done once your deposit transaction was confirmed, and the exchange should have credited your account at that point.   What they do with the money after that is solely their concern.  It's quite possible that they've intentionally lowballed the fee.  If a lull in transactions occurs sometime soon, maybe they get lucky and it confirms at this low fee.  Maybe they don't care whether this happens anytime soon; after all, it's probably just money moving around inside their organization.  If at some point it becomes more urgent, they can offer a higher fee (e.g. using RBF or CPFP).  But none of that is any concern of yours.


text:
What is the advantage of HD Wallets?
I have read man resources and questions about HD Wallets. As pointed out in this question on SO, it won't be possible to send funds of a derived child address using the master private key.
In my humble opinion exactly this would make sense if I could create a derived child to receive funds for a specific purpose but being able to spend these funds using my master private key without the need to store each child private key.
Do I miss something here? Why should I use HD wallets if I still need to store each single local private key as well?

The answer you linked isn't wrong; you just stopped reading too soon.
Transactions aren't signed by the master private key itself; they are signed by the child private keys.  But those child private keys were generated from the master private key.  So it's true that all you really need to store is the master private key.
If you have the master private key, and you want to sign a transaction, you don't sign it with the master private key directly.  Rather, you first use the master private key to regenerate child private keys until you find the one that matches the address you want to spend from.  Then you use this child private key to sign the transaction.
For day-to-day use, it is convenient to store the child private key after generating it, so that you don't have to go to the trouble of regenerating it each time.  But for backups, all you really need to save is the master private key.

"But for backups, all you really need to save is the master private key." Or the precursor to the master private key i.e. the seed mnemonic.

Do I miss something here? Why should I use HD wallets if I still need to store each single local private key as well?

Before HD wallets, if you wanted to backup your keys, you would have to make a backup after each and every transaction which is obviously annoying/time-consuming.
With HD wallets you only have to backup your seed once. Each key can be recreated deterministically from the seed and the derivation path (you dont have to store individual keys).

The main purpose of HD wallets is to store multiple cryptocurrency addresses. You can have both Ethereum and Bitcoin address in the same HD wallet. You do not need to save the private keys of each child address. All you need to do is to memorize or save the mnemonic words somewhere safe. You can easily recover the private key for your address using your mnemonic words.
https://www.coinomi.com/recovery-phrase-tool.html
The above link comes handy if you want to recover all your Ethereum and Bitcoin address and its corresponding private keys from your mnemonic words.

Does this mean that the marked answer of the quoted SO-question is wrong?

The main purpose is to make backups easier not to support multiple cryptocurrencies in one wallet


text:
How to get public key from a Electrum based wallet?
I have created a wallet with Electrum. Now I want to get the public key (not the address, but the public key for key-derivation).
How can I achieve that?

If you mean the extended public key aka master public key then you can get that via wallet menu > information. If you mean the address specific public key go to view menu > show address, switch to addresses tab, right click on the address in question and choose details to view the public key. You can also search the addresses on that tab by pressing ctrl+f.

With a fresh wallet, the addresses tab is empty.  Does this mean you have to either send or receive BTC at least once in order to see your own public key?

@Markus it shouldn't be empty. Make sure you are using the latest version of electrum and that you see a balance in the bottom left and a green circle in the bottom right. If upgrading electrum doesn't fix it then create a thread on a community forum. Stackexchange is not suitable for a back and forth diagnoses session.

I had to go to the receive tab and Create a request to see a wallet address in the addresses tab FWIW.


text:
How to get all addresses and their transactions from XPUB with blockchain.com API?
I know this question surface couple of times in different forms but people mostly were interested in doing that locally while I'm just starting with the topic and would like to know how to do that with blockchain.com API.
Thank you everyone.

get data from here https://www.blockchain.com/explorer/assets/btc/xpub/{xpub_key}
scrap the results :)


text:
latest UTXO databases received
There are many repositories that allow you to turn blockchain into UTXO. But I can't find where I could download ready-made UTXOs. I only find instructions on how to get such a database myself. Bitcoin Core predicted that it would take 55 hours to download the database, which is certainly a lot, so I'd like to ask if you know any repositories where they put the latest UTXO databases received?

Does this answer your question? What is the best way to download a "recent" UTXO set snapshot?  -- Note that the UTXO set has to be updated every 10 minutes or so and that you can't trust a UTXO set that you haven't created from blocks you have verified yourself.

@RedGrittyBrick
I saw this post from 2017. I'm not chasing an up-to-date base, so even a week (month) old base would be fine with me.

They also suggested downloading UTXOS from Electrum, but how do I do that? I couldn't find anything...

You could ask a friend who uses Bitcoin Core to give you a copy of their chainstate folder - but you'd have to extract data from the LevelDB data structure. Electrum might also have some persistent data storage that mainly consists of UTXO data.


text:
Unable to replicate block header validation errors: time-too-old and time-too-new
we are playing with the regtest and we don't able to replicate the two blocks validation error managed here
https://github.com/bitcoin/bitcoin/blob/5b24f6084ede92d0f493ff416b4726245140b2c1/src/validation.cpp#L3493-L3499
We understand that GetBlockTime and nAdjustedTime have values near os now() time so move manually the os clock can trigger the validation errors.
But we were unable to :(
Step to reproduce "time-too-old"

start bitcoin regtest daemon
mine 101 block
change the os clock 1h behind
mine a new block

we expect to get "time-too-old" validation error but we got no error
Step to reproduce "time-too-new"

start bitcoin regtest daemon
mine 101 blocks
change the os clock 3h forward
mine a new block

we expect to get "time-too-new" validation error but we got no error

Since you say you are changing your OS clock, I am assuming you are using Bitcoin Core's built in miner for regtest. This miner will try to not produce invalid blocks.
To avoid the time-too-old failure, it will take the maximum between the median time + 1, and the network adjusted time (which is basically OS time). So when you set the clock too far back, it will end up creating a block that is 1 second ahead of the median time which is valid.
You aren't getting the time-too-new error because Bitcoin Core is using the system time in that check, not median time. A block that is too new will become valid after time has passed. So it uses the OS time for that check, not median time. As such, when you set your OS clock 3 hours in the future, it produces a block with a timestamp 3 hours in the future and compares that to the OS clock which is also 3 hours in the future. This comparison finds that the block is not too new, so it does not fail.
In order to test these specific errors, you need to implement your own miner to produce blocks with the parameters that you want to use. Bitcoin Core's test suite has an independent miner in python which is how these specific failures are tested.


text:
OP_CHECKSIG NULLFAIL with btcdeb
I'm using btcdeb to understand script.
I pick a old random P2PKH transaction form mainnet, and paste unlocking script e locking script inside.
I know that this script is ok, because I can check on blockchain.
Than I Want to try with btcdeb and it works only if add flag

--modify-flags=-NULLFAIL

I expect the True at the end, instead I get 0x.
Now, I did some research on NULLFAIL, but I don't understand the Problem.
Before BIP 0146 the script could return false?

In order to verify signatures, you need to feed btcdeb the two transactions (the relevant input transaction and the transaction spending the input) for it to be able to calculate the signature hash.
Edit: as of January 18 2020, you can now feed it the signature and pubkey as a pair instead. You can also replace the signature and pubkey in the script with mock values (e.g. "mysig" and "mypubkey").
See https://github.com/kallewoof/btcdeb/blob/master/doc/mock-values.md

Added note about the new mock signature feature in btcdeb.


text:
Calculate block hash is different in regtest?
I'm able to calculate the block hash in mainnet and testnet, but I don't get the right hash in regtest.
I mint +3000 blocks and I get the block with height 3000
bitcoin-cli getblock $(bitcoin-cli getblockhash 3000)

{
  "hash": "4221d8b10b0a7eb5e050337c76819abf71113f17e266e88cb12df4bf5ce24b55",
  "confirmations": 502,
  "strippedsize": 215,
  "size": 251,
  "weight": 896,
  "height": 3000,
  "version": 536870912,
  "versionHex": "20000000",
  "merkleroot": "23b4600b467f18ef0ec18bd73a9b921077486cf67c44c912de114888db184bf9",
  "tx": [
    "23b4600b467f18ef0ec18bd73a9b921077486cf67c44c912de114888db184bf9"
  ],
  "time": 1599147354,
  "mediantime": 1599147353,
  "nonce": 0,
  "bits": "207fffff",
  "difficulty": 4.656542373906925e-10,
  "chainwork": "0000000000000000000000000000000000000000000000000000000000001772",
  "nTx": 1,
  "previousblockhash": "4776b1ccad27e2de9ccbdc5bf4ad4d972a89e27a020db1f7b59abb857d77f3be",
  "nextblockhash": "121512fc6bc1395ada64a6c63ba2c2955ad9cc240a1fc0282b95cf2c2651b381"
}

Now I save the values in env variable
 ver=`printf 20000000 | tac -rs ..| tr -d '\n'`
 prev=`printf 4776b1ccad27e2de9ccbdc5bf4ad4d972a89e27a020db1f7b59abb857d77f3be | tac -rs .. | tr -d '\n'`
 mkl=`printf 23b4600b467f18ef0ec18bd73a9b921077486cf67c44c912de114888db184bf9 | tac -rs .. | tr -d '\n'`
 time=`printf '%x\n' 1599147354 | tac -rs .. | tr -d '\n'`
 bits=`echo 207fffff | tac -rs .. | tr -d '\n'`
 nonce=`printf '%x\n' 0 | tac -rs .. | tr -d '\n'`

After that I can concat and get the little endian representation
printf $ver$prev$mkl$time$bits$nonce | xxd -r -p | sha256sum -b | xxd -r -p | sha256sum -b | awk '{printf $1}' | tac -rs ..

The result is: 09665bcb63b6c279948f93937669bf1c8f36cffda0646180ba523847cb642384. the same operations in mainnet and testnet works properly.

The nonce needs to be 8 hex digits but printf %x only generates the minimum digits needed for the value, which for 0 is only 1 digit. (The same is true of other numeric conversions %d %i %u %o also.) You need to use %08x to force an 8-hexit value:
ver=$(printf 20000000 | tac -rs ..)
prev=$(printf 4776b1ccad27e2de9ccbdc5bf4ad4d972a89e27a020db1f7b59abb857d77f3be | tac -rs .. )
mkl=$(printf 23b4600b467f18ef0ec18bd73a9b921077486cf67c44c912de114888db184bf9 | tac -rs .. )
time=$(printf %x 1599147354 | tac -rs .. )
#should be %08x if any timestamp can be before mid-1978
bits=$(printf 207fffff | tac -rs .. )
nonce=$(printf %08x 0 | tac -rs .. )
printf $ver$prev$mkl$time$bits$nonce | xxd -r -p | openssl sha256 -binary |
 openssl sha256 | awk '{print $2}' | tac -rs..

-->
4221d8b10b0a7eb5e050337c76819abf71113f17e266e88cb12df4bf5ce24b55

Other changes:

most of your items didn't even have a newline, and for those that did it is discarded by the tac -rs.., and so none of them need |tr -d '\n' -- although for consistency I removed the \n in some printf formats, and changed the echo (which adds newline by default) to printf (which doesn't). (And even if the/some/any items did contain newline, if you doublequote the final printf "$ver...$nonce" so they get treated as data and not wordsplits, the xxd -r -p will discard them as if they weren't there at all.)

I used the newer syntax $( ) for process substitution instead of the older backticks (which I can't show in Stack markdown!) because in many cases it has advantages, although not here specifically, and I like to be consistent. (There are dozens of Qs about this on other Stacks.)

I used openssl instead of sha256sum because I have it on my systems and it's a little easier.

This wasn't a problem on mainnet and testnet, at least for most blocks, because they have difficulty high enough that the nonce value will be at least 29 bits most of the time.

thanks for your time and thanks for your super reply!

I put the script on sh file, for that reason I need | tr -d '\n'.
Do you have any advice to avoid | tr -d '\n' in file sh?

The code I posted is in a script file (on my CentOS test system) and works fine without tr -d '\n' for the reasons I explained -- what makes you think it's needed?


text:
How to run Stratum for terracoin?
I have a few asic blades 13 GHs and I am farming BTC on slash pool via stratum.
How to change and what inside stratum to make those asics farm terarcoin on any one pool for TRC?

What do you mean by "inside stratum"?

Stratum is dependent on the pool you are operating with.  First you need to find a pool that supports terracoin, a short list can be found at terracon.org.
Once you decide on a pool, read their "Help" section to find the server settings required.  I works exactly the same as bitcoin, you will enter a server address, a connection port, a username and a password.
Assuming you are using cgminer the parameters will look something like this:
cgminer -o http://pool:port -u username -p password

But plug in the values given from the pool.  Welcome to Stack Exchange and good luck!

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/


text:
Size of Blockchain for huge smart contracts database
Let's say I want to use smart contracts for huge collection of data. Since it will be distributed so downloading on every participant computers isn't this a huge problem ?

Yes. But smart use of smart contacts means you don't need to put all the data on the blockchain. You can keep the actual data offline or only shared between actual participants.
Lightning Network is a nice example of such a system.
Many other examples can be done using simply multisig and a bit of time locking if necessary. More op-codes and more flexible and more efficient scripting execution are in the works too.

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/


text:
What's difference between Multichain, Chain, Hyperledger, Ethereum etc
I stumbled upon multiple Blockchain protocols framework. For example Multichain http://www.multichain.com/
What's difference between this one and the others ?

There are any differences with regard to security, centralization, access, features, purpose, etc. You should ask a more specific question if you want a specific answer.

Multichain is directly built from Bitcoin Core which power the bitcoins. Of course there are modifications to it. Multichain provides direct interface with many parameters and is basically used to make permissioned networks.  And the major difference between multichain and other blockchain technologies is no use of smart contracts in multichain.
Also, Bitcoin is open network, Ethereum allows public network apps, hyperledger is used for building private blockchains and multichain for permissioned blockchains.


text:
Hashflare: cannot go back to Not Reinvest
Does anybody know why I can't go back to "Not reinvest" with hashflare ? Thanks.

You can click on 'Reinvest' and when you see the list for which one you want to reinvest with (SHA-256/ Scrypt) there should be an option at the top that says Do not reinvest. It should be updated within 24 hours!

Please don't, it seems they don't care and honor their contracts. Running lifetime contracts were recently reduced to one year. Looks like they can do anything. Who knows, they might say, you are done after one month on a year contract.

Try not using Save, but Close in the Reinvest dialog. The functions of the buttons seem to be reversed.

You should have read the terms of service contract that you signed.  It states that they have the right to change contract terms. Next time read before you sighn up!