text:
What happens when hash160 is same
There are two bitcoin address:
(A) 1111111111111111111114oLvT2
(B) 31h1vYVSYuKP6AhS86fbRdMw9XHieotbST
which has the same hash160:
0000000000000000000000000000000000000000
How is this possible and what are the consequences?
https://www.blockchain.com/btc/address/1111111111111111111114oLvT2
and
https://www.blockchain.com/btc/address/31h1vYVSYuKP6AhS86fbRdMw9XHieotbST

Technically speaking, these are not identical hashes, because they are not hashes. They're just addresses someone created by encoding the value 0000...0000 into a P2PKH and a P2SH address.
Funds sent to those addresses cannot be spent, for two reasons.

It would require finding a public key or script whose hash is 0000...0000, which is assumed to be impossible (SHA256 or RIPEMD160 would be broken if that was the case).
Even the value wasn't 0000...0000, but the hash of an actual public key, it would still be impossible to find a script whose hash was equal. Alternatively, if it was the hash of a script, it would be impossible to find a public key with the same hash.

Finding a pubkey which shares a hash160 with a valid script should be easy :
02****1D********************************************************** , 03******1C******************************************************** , and 04********3B**********************************************************************************************************************.  Replace ** with any byte and there you have a valid pubkey which is also a valid script, although it is not a very interesting script :)

It just means someone created 2 different address types with the same hash160 value. They are still valid addresses, and would be considered different (because of the different prefix, see below). I was amazed for a moment, but it looks like the 2 addresses have never spent any bitcoin :) It's almost certain the creator did not create this from a valid key pair.
If you take the hash160 value 0000000000000000000000000000000000000000 and append 00, the standard P2PKH address prefix, and then BASE58CHECK encode it, you get the first address 1111111111111111111114oLvT2.
Then, change that prefix to 05, the standard P2SH address prefix, you get the second address: 31h1vYVSYuKP6AhS86fbRdMw9XHieotbST.
$ printf "000000000000000000000000000000000000000000" | xxd -r -p | base58 -c
1111111111111111111114oLvT2
$ printf "050000000000000000000000000000000000000000" | xxd -r -p | base58 -c
31h1vYVSYuKP6AhS86fbRdMw9XHieotbST


text:
Exchange development good practices
Is there any guide or good practice rules to follow when developing a service that uses the Bitcoin and other cryptocurrencies protocols? For instance, how is the best way to know if a user deposited coins to an address.

To enable deposits, first have a way to generate address for each user, save this to local DB , subscribe to your fullnode txnotify and blocknotify events , then once user send coins to one of his addresses your full node will notify your app on incoming tx, you can get the complete details with rpc call view the address that received the crypto , save it as waiting for confirmation and then wait for blocks to confirm.

source: Stack Exchange

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

sort: votes


text:
What happens if I use same k twice in DSA algorithm?
To sign a message , we choose nonce k but what happens if we use same k twice to sign a message using DSA algorithm?

If you sign two messages using the same nonce, then it is possible for someone to derive your private key. See How do you derive the private key from two signatures that share the same k value?

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


text:
Calculate the difficulty of Bitcoin network
Which variable among the components of the block header is computed (or rather obtained) to achieve the difficulty posed by the bitcoin network?
Nonce or Previous block hash?

The question is twisted here.Miners has to find nonce with their computation power inorder to solve the difficult puzzle and add the block to blockchain.

I think you're confusing terms here.

Which variable among the components of the block header is computed

The block components which are computed (hashed) are all of the components which make up the block header: version, previous block hash, merkle root, unix epoch time, difficulty target (bits), and nonce.
Difficulty
A value which represents how hard it will be to mine a block. Higher difficulty value = harder to solve. The difficulty of the genesis block is 1. All block difficulties are calculated as a ratio to the difficulty of the genesis block.
Proof of Work
When you calculate a proof of work, you must meet the minimum difficulty posed by the network, as you state. This validation step makes sure that the value of the block hash you provide is less than or equal to the difficulty target.
Example
To illustrate this concept, you can validate one of the block hashes manually.

Grab a block (eg. block 3): Block 3 in json or Block 3 in hex
Concatenate (in this order, little endian, hex format) the version, previous block hash, merkle root, unix epoch time, difficulty target (bits), and nonce. You’ll see you can copy the first 160 characters from the Block 3 in hex link.
01000000bddd99ccfda39da1b108ce1a5d70038d0a967bacb68b6b63065f626a0000000044f672226090d85db9a9f2fbfe5f0f9609b387af7be5b7fbb7a1767c831c9e995dbe6649ffff001d05e0ed6d
Run the command:
$ echo 01000000bddd99ccfda39da1b108ce1a5d70038d0a967bacb68b6b63065f626a0000000044f672226090d85db9a9f2fbfe5f0f9609b387af7be5b7fbb7a1767c831c9e995dbe6649ffff001d05e0ed6d | xxd -r -p | openssl sha -sha256 | xxd -r -p | openssl sha -sha256
Reverse the output from
4944469562ae1c2c74d9a535e00b6f3e40ffbad4f2fda3895501b58200000000 to
0000000082b5015589a3fdf2d4baff403e6f0be035a5d9742c1cae6295464449

And there’s the block hash!


text:
What is the official explorer and value API for Bitcoin?
I am new to cryptocurrency development, and I have noticed that Bitcoin has an official wallet for both Desktop and Mobile (correct me if I am wrong).
My question is, does Bitcoin have an official explorer and value API (such as Bitcoin to fiat conversion) too? If not, are there services or APIs for developers that are recommended and widely used?


text:
Bitcoin - Read transaction details from LevelDB
I am new to LevelDB and the way BTC keeps everything in the database. I went through article How does Bitcoin read from/write to LevelDB but was not able to get to the first step -
Transaction
246c5a81b6ad0dfc0dbc0b2ff5bde65ee1913f75a47d409b8ff8074a27ec1000

is identified in the LevelDB by:
c0010ec274a07f88f9b407da4753f91e15ee6bdf52f0bbc0dfc0dadb6815a6c24

How does the first string translates back to c0010ec274a07f88f9b407da4753f91e15ee6bdf52f0bbc0dfc0dadb6815a6c24? I understand the first character is c but what calculation makes the second string?
I am using .Net Core to read level DB using LevelDB.Standard nuget package (Assembly LevelDB.NET, Version=2.1.6.0)

The two strings are the same, but with the bytes (hex digit pairs) in reversed order (with an extra c at the beginning).  The first string starts with 24 6c 5a ... and the second string ends with ... 5a 6c 24.
It looks like an endianness flip to me (network order is big-endian, but  your machine is presumably using little-endian order).  I don't know what the meaning of the extra c is.

The 'c' simply stands for "coin", FWIW; it distinguishes it from a few meta records in the database that are not UTXOs.


text:
BTC - Block Index - Reading the flag values in Level DB
Following the article I tried reading the flag value for txindex in the Block Index database but was not able to fetch the values. I tried fetching value for all below but its NULL always. Am I doing something wrong?
    private readonly byte[] TRANSACTIONINDEX_ENABLED = new byte[] { 70, 8, 116, 120, 105, 110, 100, 101, 120, 1 }; // F8txindex
    private readonly byte[] TRANSACTIONINDEX_ENABLED1 = new byte[] { 70, 7, 116, 120, 105, 110, 100, 101, 120, 1 }; // F7txindex
    private readonly byte[] TRANSACTIONINDEX_ENABLED2 = new byte[] { 70, 7, 1, 120, 101, 100, 110, 105, 120, 116 }; // F7txindex little endian
    private readonly byte[] TRANSACTIONINDEX_ENABLED21 = new byte[] { 70, 7, 120, 101, 100, 110, 105, 120, 116, 1 }; // F7txindex little endian
    private readonly byte[] TRANSACTIONINDEX_ENABLED3 = new byte[] { 70, 8, 116, 120, 105, 110, 100, 101, 120, 1 }; // F8txindex little endian
    private readonly byte[] TRANSACTIONINDEX_ENABLED31 = new byte[] { 70, 8, 120, 101, 100, 110, 105, 120, 116, 1 }; // F8txindex little endian
    private readonly byte[] TRANSACTIONINDEX_DISABLED = new byte[] { 70, 8, 116, 120, 105, 110, 100, 101, 120, 0 }; // F8txindex
    private readonly byte[] TRANSACTIONINDEX_DISABLED1 = new byte[] { 70, 7, 116, 120, 105, 110, 100, 101, 120, 0 }; // F7txindex
    private readonly byte[] TRANSACTIONINDEX_DISABLED2 = new byte[] { 70, 7, 0, 120, 101, 100, 110, 105, 120, 116 }; // F7txindex little endian
    private readonly byte[] TRANSACTIONINDEX_DISABLED21 = new byte[] { 70, 7, 120, 101, 100, 110, 105, 120, 116, 0 }; // F7txindex little endian
    private readonly byte[] TRANSACTIONINDEX_DISABLED3 = new byte[] { 70, 8, 116, 120, 105, 110, 100, 101, 120, 0 }; // F8txindex little endian
    private readonly byte[] TRANSACTIONINDEX_DISABLED31 = new byte[] { 70, 8, 120, 101, 100, 110, 105, 120, 116, 0 }; // F8txindex little endian

Additionally, how to fetch the mapping between data fetched from block index and the bat file where details of the block are. I assumed that Block Index record will have details of the file (blk000????.dat) where to look for the block but it doesnt have anything

I assumed that Block Index record will have details of the file (blk000????.dat) where to look for the block but it doesn't have anything

As described in the answer by a Bitcoin Core developer, the Block Index database contains all the information of the block and where the block is located (in which file blk). This is required because the is no order of the blocks received from the bitcoin network.

I tried fetching value for all below but its NULL always. Am I doing something wrong?

Read the information from the level DB is a little bit tricky because there is a special format as described in the link post. However, with your example, there is no way to find the problem in your code, because there is no code posted.

I fetched the block data from Index DB and it doesnt have any reference to the blk file. Is that a derived information?

you have sored the number of the file, like in the source code is reported   https://github.com/bitcoin/bitcoin/blob/f914f1a746d7f91951c1da262a4a749dd3ebfa71/src/chain.h#L111

The value in block index does not actually contains nFile. I believe itit must be calculated field but not sure about how it is done https://github.com/bitcoin/bitcoin/blob/f914f1a746d7f91951c1da262a4a749dd3ebfa71/src/chain.h#L171

The value is 88 bytes and the values it has are MagicId, Length, Version, PrevBlockHash, MarkleRoot, Timestamp, Difficulty, Nounce


text:
What are These Long Addresses in the Loyce.com Address List?
I'm trying to understand Bitcoin address encodings. I understand there's three (four?) different address encodings P2PKH, P2SH, Bech32, and Bech32m (Bech32m just uses a different constant when XORing). They all encode the public key using Ripe160 (RIPEMD160?). I was able to use some libraries to extract the ripe160 encoded address, which should be 20 bytes, but there are some longer strings in this list of addresses with currency:
Bitcoin addresses with currency from loyce.com
34xp4vRoCGJym3xR7yCVPFHoCNxv4Twseo
**bc1qgdjqv0av3q56jvd82tkdjpy7gdp9ut8tlqmgrpmv24sq90ecnvqqjwvw97**
1P5ZEDWTKTFGxQjZphgWPQUpe554WKDfHQ
37XuVSEpWW4trkfmvWzegTHQt7BdktSKUs
38UmuUqPCrFmQo4khkomQwZ4VbY2nZMJ67
3Kzh9qAqVWQhEsfQz7zEQL1EuSx5tyNLNS
1FeexV6bAHb8ybZjqQMjJrcCrHGW9sb6uF
3LYJfcfHPXYJreMsASk2jkn69LWEYKzexb
bc1qa5wkgaew2dkv56kfvj49j0av5nml45x9ek9hz6
1LdRcdxfbSnmCYYNdeYpUnztiYzVfBEQeC
1AC4fMwgY8j9onSbXEWeH6Zan8QGMSdmtA
1LruNZjwamWJXThX2Y8C2d47QqhAkkc5os
1NDyJtNTjmwk5xPNhjgAMu4HDHigtobu1s
3Gpex6g5FPmYWm26myFq7dW12ntd8zMcCY
bc1q5shngj24323nsrmxv99st02na6srekfctt30ch
385cR5DM96n1HvBDMzLHPYcw89fZAXULJP
3LQUu4v9z6KNch71j7kbj8GPeAGUo1FW6a
**bc1q5pucatprjrqltdp58f92mhqkfuvwpa43vhsjwpxlryude0plzyhqjkqazp**
...

They start with bc1 so that indicates Bech32, correct? When decoding using something like bech32-buffer I get 32 bytes.
In doing some more digging, there's also this scriptPubKey encoding. Is that what these long encodings are? How do you differentiate them? I assume since the site is listing address with currency, I should be able to pull the ripe160 address from the script, are there any recommended parsers?
I found another post which suggests doing pattern matching, and pulling the parsing code from Bitcoin Core. Am I heading down the correct path here?

What are you trying to do with these addresses? The answer very much depends on that.

@PieterWuille Learning, just want to understand all the different forms.

They are bech32 addresses that encode a P2WSH scriptPubKey. The script that maps to those addresses contain the SHA256 of a script that will be revealed when the output is spent.

They all encode the public key using Ripe160 (RIPEMD160?)

No. Addresses do not just encode public keys, nor do they necessarily encode a particular hash. It depends on the address type as it is dependent on the script that will be created.
Addresses beginning with a 1 encode the RIPEMD160 of the SHA256 of a public key and create P2PKH output scripts.
Addresses beginning with a 3 encode the RIPEMD160 of the SHA256 of a script and create P2SH output scripts.
bc1q addresses that are 42 characters long encode the RIPEMD160 of the SHA256 of a public key and create P2WPKH output scripts.
bc1q addresses that are 62 characters long encode the SHA256 of a script and create P2WSH output scripts.
bc1p addresses that are 62 characters long encode an X-only public key and create P2TR (Taproot) output scripts.


text:
What do I tell my company IT dept so they allow my bitcoin core container to connect to other nodes
I work for a corporation therefore work behind a corporate firewall. I login to their vpn then I access company resources like mails, IMs etc.
As a internal team project, I'm trying to setup a btc full node on a k8s platform. I created a local docker image using a simple dockerfile (very simplified version of https://github.com/ruimarinho/docker-bitcoin-core/blob/master/0.21/Dockerfile), where I extract btc core binary and execute bitcoind. When I run the container (and bitcoind inside it), I see it abruptly stops after dns seed query. Below is log
2021-11-03T18:16:34Z Bitcoin Core version v22.0.0 (release build)
2021-11-03T18:16:34Z Assuming ancestors of block 00000000000000000008a89e854d57e5667df88f1cdef6fde2fbca1de5b639ad have valid signatures.
2021-11-03T18:16:34Z Setting nMinimumChainWork=00000000000000000000000000000000000000001fa4663bbbe19f82de910280
2021-11-03T18:16:34Z Using the 'sse4(1way),sse41(4way),avx2(8way)' SHA256 implementation
2021-11-03T18:16:34Z Using RdRand as an additional entropy source
2021-11-03T18:16:34Z Default data directory /root/.bitcoin
2021-11-03T18:16:34Z Using data directory /root/.bitcoin
2021-11-03T18:16:34Z Config file: /root/.bitcoin/bitcoin.conf (not found, skipping)
2021-11-03T18:16:34Z Using at most 125 automatic connections (1048576 file descriptors available)
2021-11-03T18:16:34Z Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements
2021-11-03T18:16:34Z Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements
2021-11-03T18:16:34Z Script verification uses 5 additional threads
2021-11-03T18:16:34Z scheduler thread start
2021-11-03T18:16:34Z libevent: getaddrinfo: address family for nodename not supported
2021-11-03T18:16:34Z Binding RPC on address ::1 port 8332 failed.
2021-11-03T18:16:34Z HTTP: creating work queue of depth 16
2021-11-03T18:16:34Z Using random cookie authentication.
2021-11-03T18:16:34Z Generated RPC authentication cookie /root/.bitcoin/.cookie
2021-11-03T18:16:34Z HTTP: starting 4 worker threads
2021-11-03T18:16:34Z Using wallet directory /root/.bitcoin/wallets
2021-11-03T18:16:34Z init message: Verifying wallet(s)…
2021-11-03T18:16:34Z init message: Loading banlist…
2021-11-03T18:16:34Z Missing or invalid file /root/.bitcoin/banlist.dat
2021-11-03T18:16:34Z Recreating the banlist database
2021-11-03T18:16:34Z SetNetworkActive: true
2021-11-03T18:16:34Z Failed to read fee estimates from /root/.bitcoin/fee_estimates.dat. Continue anyway.
2021-11-03T18:16:34Z Using /16 prefix for IP bucketing
2021-11-03T18:16:34Z Cache configuration:
2021-11-03T18:16:34Z * Using 2.0 MiB for block index database
2021-11-03T18:16:34Z * Using 8.0 MiB for chain state database
2021-11-03T18:16:34Z * Using 440.0 MiB for in-memory UTXO set (plus up to 286.1 MiB of unused mempool space)
2021-11-03T18:16:34Z init message: Loading block index…
2021-11-03T18:16:34Z Switching active chainstate to Chainstate [ibd] @ height -1 (null)
2021-11-03T18:16:34Z Opening LevelDB in /root/.bitcoin/blocks/index
2021-11-03T18:16:34Z Opened LevelDB successfully
2021-11-03T18:16:34Z Using obfuscation key for /root/.bitcoin/blocks/index: 0000000000000000
2021-11-03T18:16:34Z LoadBlockIndexDB: last block file = 0
2021-11-03T18:16:34Z LoadBlockIndexDB: last block file info: CBlockFileInfo(blocks=0, size=0, heights=0...0, time=1970-01-01...1970-01-01)
2021-11-03T18:16:34Z Checking all blk files are present...
2021-11-03T18:16:34Z Initializing databases...
2021-11-03T18:16:34Z Opening LevelDB in /root/.bitcoin/chainstate
2021-11-03T18:16:34Z Opened LevelDB successfully
2021-11-03T18:16:34Z Wrote new obfuscate key for /root/.bitcoin/chainstate: 0ee90f79911b43fb
2021-11-03T18:16:34Z Using obfuscation key for /root/.bitcoin/chainstate: 0ee90f79911b43fb
2021-11-03T18:16:34Z  block index              16ms
2021-11-03T18:16:34Z loadblk thread start
2021-11-03T18:16:34Z UpdateTip: new best=000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f height=0 version=0x00000001 log2_work=32.000022 tx=1 date='2009-01-03T18:15:05Z' progress=0.000000 cache=0.0MiB(0txo)
2021-11-03T18:16:34Z block tree size = 1
2021-11-03T18:16:34Z nBestHeight = 0
2021-11-03T18:16:34Z Failed to open mempool file from disk. Continuing anyway.
2021-11-03T18:16:34Z loadblk thread exit
2021-11-03T18:16:34Z torcontrol thread start
2021-11-03T18:16:34Z Bound to 127.0.0.1:8334
2021-11-03T18:16:34Z Bound to [::]:8333
2021-11-03T18:16:34Z Bound to 0.0.0.0:8333
2021-11-03T18:16:34Z init message: Loading P2P addresses…
2021-11-03T18:16:34Z Missing or invalid file /root/.bitcoin/peers.dat
2021-11-03T18:16:34Z Recreating peers.dat
2021-11-03T18:16:34Z Missing or invalid file /root/.bitcoin/anchors.dat
2021-11-03T18:16:34Z 0 block-relay-only anchors will be tried for connections.
2021-11-03T18:16:34Z init message: Starting network threads…
2021-11-03T18:16:34Z net thread start
2021-11-03T18:16:34Z dnsseed thread start
2021-11-03T18:16:34Z addcon thread start
2021-11-03T18:16:34Z opencon thread start
2021-11-03T18:16:34Z msghand thread start
2021-11-03T18:16:34Z Loading addresses from DNS seed seed.bitcoin.sipa.be
2021-11-03T18:16:34Z init message: Done loading
2021-11-03T18:16:35Z Loading addresses from DNS seed dnsseed.bitcoin.dashjr.org
2021-11-03T18:16:35Z Loading addresses from DNS seed seed.btc.petertodd.org
2021-11-03T18:16:36Z Loading addresses from DNS seed seed.bitcoinstats.com
2021-11-03T18:16:36Z Loading addresses from DNS seed seed.bitcoin.jonasschnelli.ch
2021-11-03T18:16:36Z Loading addresses from DNS seed dnsseed.emzy.de
2021-11-03T18:16:37Z Loading addresses from DNS seed dnsseed.bluematt.me
2021-11-03T18:16:37Z Loading addresses from DNS seed seed.bitcoin.wiz.biz
2021-11-03T18:16:38Z Loading addresses from DNS seed seed.bitcoin.sprovoost.nl
2021-11-03T18:16:38Z 209 addresses found from DNS seeds
2021-11-03T18:16:38Z dnsseed thread exit

So it's able to query dnsseed but then just stays here forever. If then I disconnect from my vpn in my mac, the service becomes alive and is able to connect to peers and starts fetching the blocks.
This makes sense because obviously something with my corporate firewall prevents further connection.
Now I can contact IT dept and ask them to make some exceptions because of this project, but I don't know what to tell them. Is it simply asking them to ignore all firewall rules for this container or is it some specific IP/servers we can whitelist?
Is there a proxy server that we need to whitelist to enable btc core to function?
I don't think I need to make changes in my container config since when vpn is disconnected it works as expected.
I mean if I ask IT dept to take my container and run in their in-house k8s platform, what should they (or I) be doing extra, network firewall-wise, so that the container works?


text:
existing solution to build a bitcoin address-balance index
I've a full node running in a AWS vm. I want to develop an app which would be able to provide balance for a given btc address. I know blockchain.info and blockexplorer.com exists but I want to do my own but a simple one, just to do the balance query. I want to support a high request bandwidth and each request to work in O(1) complexity.
Towards that goal, I'm thinking if

I can extract out all the unique btc addresses from blockchain
run a script which takes btc address and runs "bitcoin-cli getbalance" from my node to obtain balance for that address
Build a in-memory cache of address->balance or in mongodb,
then I can do it.
Does this plan look good? Is there a better alternative? Are there existing open-source solutions that I can use to do this all, saving me lot of development work? How do I go about each step?
Once the infra is set, it should be easy to keep it uptodate since blocks are added every 10mins. I think a event based system can be setup to trigger when blocks added. But setting up the infra is the problem here.

Please help. Much appreciated.

You only need to getbalance for addresses in the UTXO. All other addresses found on the blockchain will have balance 0.

this required years of calculation :)

there are parser of the bitcoin block data that can do somethings like that, or maybe could be do something like that faster. A worked on a draft and it is available https://github.com/vincenzopalazzo/SpyCblock and an analysis of the bitcoin scrip with the result it did with https://github.com/vincenzopalazzo/AnalyticsPyBlock

Hi @bordalix How can I obtain all the addresses in UTXO? Is there a script that I can run on the bitcoin data I've in my node?

@kdas, check this answer: https://bitcoin.stackexchange.com/questions/51387/how-does-bitcoin-read-from-write-to-leveldb/52167#52167


text:
can i diy an asic miner?
Iam interested in mining bitcoin so Iam wondering if I can get a pcb for hashing and bring asic chip that algorithms are sha-256 and stick them together with some other electronics and have a control board with power supply?
If you are interested too lets make a group and start searching together to get everything done.

hey i am interested can you drop a link?

what about making a discord group to make some action

perhaps doing that would be best.


text:
I own a Blockchain wallet and wishest put mneyon it from my visa debt card how do I do that
I own a blockchain wallet how do I fund it using my credit card


text:
Why blockchain new node verify all transactions in blocks?
Hello I'm the student studying blockchain and bitcoin.
I have a question that why all node verify all transactions in blocks.
I check if new node connect bitcoin network, then the all transactions in blocks are verified.
The transactions before connecting new node are already verified, but why new node check all transactions in blocks???
I feel that "new node doesn't check previous transactions" have some problems, but clear idea doesn't know.
Please tell me why. ㅠ.ㅠ

Bitcoin is designed to work in an antagonistic environment. If our node trusts what other nodes tell it blindly, it is trivial to lie to it. Instead, our node processes the complete blockchain itself and checks that all rules were adhered to. This way, it cannot fall for invalid transaction or block data.
More generally, since there is no single leader or decider, the network needs a mechanism how to converge on a single ground truth. If every node believed everything other nodes told it, it would be probably easy to split the network, waste bandwidth, and delay or prevent convergence. Making it costly to produce the canonical truth and everyone checking its validity inundates the network against many of these attacks.

What happen in that case?? I want to know precise situation

Fore example my node tells you that I have 1,000 BTC and then pays you from that 1,000 BTC. You give me some other good for my payment, but then realize that the bitcoins don't actually exist.

why all node verify all transactions in blocks.

So that they don't have to trust anyone else.
This way each node knows for certain which "coins" are already spent and thereby avoid fraud.


text:
Why MakeNewKey in key.cpp isn't called when getnewaddress or generate
Hi I'm student studying bitcoin-core.
I tested bitcoin regtest mode, and I want to know when CKey::MakeNewKey() is called.
when I do this,
bitcoin-cli -regtest createwallet "wallet1"
MakeNewKey() function was called, but
bitcoin-cli -regtest getnewaddress or bitcoin-cli -regtest -generate 1
didn't call MakeNewKey() and only call  GetPrivKey() and GetPubKey() in key.cpp
and when GetPrivKey() was called, the part of key32 in serialized key doesn't match of wallet's private key
My questions are,

Is only one private key of my wallet?
Blocks have their own private keys?
If #2 is right, what file make private key and public key of the block

Since Bitcoin Core 0.13, the wallet uses deterministic key derivation.
That means that the wallet stores a single seed, and uses that to derive keys whenever needed. See BIP32 for details.
This has the advantage that a backup of the wallet lasts forever, at least as no other keys are imported. Since the same seed gives rise to the same keys, an older backup will produce the same keys as the real wallet generated after the backup was made, and thus the same payments will be discovered after restoring the backup.
MakeNewKey is for generating a new random key. For deriving keys from seeds, see the CExtKey functions in key.cpp.


text:
Does running a node benefit the network?
There is a 10-year old question asking this. The sole answer to that question at the time was essentially a yes, and I've seen people say that running a node makes the network more "secure" and "decentralized". However, I've also seen prominent Bitcoin developers [1][2] say that a full node that isn't actually used by its user to verify the chain is basically useless. So how is it really?
To expand on the original question, assuming some nodes do actually contribute to the network, not all nodes obviously contribute the same. I would like to know how some factors might play into this:

running a pruned vs unpruned node
running over clearnet, Tor or both
whether incoming connections are allowed
available bandwidth and hardware

Related: Should I leave the Bitcoin client open?

I don't think I'm qualified to give a full answer, but I do think a full node which doesn't actually have an "economically active user" behind it could still be a benefit to the network.
The Bitcoin wiki says:

Myth: I can just set up a node on a cloud server instance and leave it
To get the benefits of running a full node, you must use it as your wallet, preferably on hardware you control.
Most people who do this do not use a full node as their wallet. Unfortunately because Bitcoin has a similar name to Bittorrent, some people believe that upload capacity is the most important thing for a healthy network. As I've explained above: bandwidth and connections are not a problem today; trust, security and privacy are.

I read Jameson Lopp's recent article "Is the Bitcoin Network slowing down?" which suggests that initial block download speeds could be affected by the restricted upload bandwidth of many full nodes, particularly those on asymmetric residential connections:

Finally, there's a question of actual bandwidth. Per Marco Falke's original suggestion, there could just be a lot of slow or even malicious peers on the network that deliver block data slowly or not at all.

So I think an otherwise "unused" node sitting on a cloud server or other high-bandwidth connection which is willing to serve historical blocks is probably performing a useful service for the network these days.


text:
What are some good solutions for analyzing Bitcoin script patterns?
I'm interested in analyzing the Bitcoin blockchain for occurrences of particular scripts and spending patterns. What are some good solutions for this? An ideal solution would let me filter transactions and outputs based on the contents of the locking and redeem scripts of single outputs as well as chains of outputs spending each other.
I've thought about looping over blocks and transactions with the Bitcoin Core RPC interface. This strikes me as probably not the most efficient solution, as I/O with bitcoind would probably be a bottleneck. Another solution would be to parse the raw block files, load them into memory (in a streaming fashion, as I don't have 200+ GB of memory...) and operate on them there. That sounds like a lot of work, so I was hoping there are some existing solutions for that out there.

It seems that bitcoin-iterate implements your second suggestion:

This is some fast code to iterate over bitcoind's block files to extract blockchain data from the main blockchain.

But as far as I know it doesn't allow your to filter by script. That would be a nice addition to the project, I think. Or you could write something similar that did that.

That seems really helpful! I'll probably need to add some code for transferring the hex scripts into op-codes. My C is a bit rusty (!), so I'll probably do that in a different language.

bitcoin-cli decodescript does that. And it also works if the script is in the witness data (but if you apply it to witness data that isn't script you'll get the most bizarre results with opcodes never imagined and completely nonsensical).

I want to add my propose to this solution.
I have released the new parser called SpyCBlock, this is the simple parser blk file (multiprocessor).
With this parser is possible to serialize the blockchain into JSON, so blkxxxxx.dat -> blkxxxxx.json (also another type of serialization).
I think with the JSON version is possible to run your analysis and I have run the simple analisis on the type of script used on the Bitcoin network, this is an example.
I think the version JSON is a propose to simplification the analysis of the bitcoin blockchain.

You may try blockchain-parser.
It converts raw blockchain data to the simple text files.


text:
Circular rebalance through 1 of 7 channels always times out
I have the following 8 channels open

I am able to do a circular rebalance through the first 6 as expected
The bottom 2 channels are to the same node
Every time I try to do a circular rebalance (tried all paths through the other channels) through either of the bottom 2 channels, the payment times out

Has 1ml.com done something to prevent me from using these channels?
If so, leaving these channels open could lead to unnecessary failures through those routes when someone attempts to use my node for routing?


text:
How do I export a wallet from Wasabi Wallet to Samurai?
I'm quite new to this business, and I have a question.
Here I have 12 words and a passphrase, there is a Wasabi Wallet application on the computer and created a wallet based on this data.
Can I use the same 12 words and passphrase to restore my Android wallet using Samurai Wallet?
When I try to enter the same data as in Wasabi Wallet, I have a different wallet (I compare the private keys and they do not match).
Can someone explain to me what I'm doing wrong? I want to have the same wallet on all platforms, knowing only 12 phrases and a passphrase.
Thank you in advance for your answers, I will be glad to everyone.

I enter 12 phrases, enter the BIP39 password, click restore, compare keys and see discrepancies.

First, a word of caution: unlike how it's advertised, Samourai is not a privacy wallet, so be prepared to give up your privacy, which defeats the point of using Wasabi. Samourai sends your xpub - which is your past, present and future financial history - to their servers, which retroactively deanonymizes the coinjoins you did with Wasabi in the past.
Assuming you don't care about privacy and you succeed, there'll still be a host of wallet balance issues on both sides of the coin. The red wallet doesn't index deep enough keys and the green wallet is bech32 only, which means if you spend with Samourai, the changes often aren't bech32 outputs, so they won't always appear in Wasabi.
If you still would like to proceed, then it should be possible as both wallet uses the same key derivation strategy. I also did the other way around (samuri -> wasabi) a few years ago and apparently this guy too :)
I can think of 2 things you are doing wrong here:

you're trying testnet: Wasabi uses mainnet paths for testnet wallets.
you're not aware of the BIP39 passphrase: in Wasabi your chosen wallet password is also used as the 13th mnemonic word.

If this doesn't help and you'd like to continue hacking around, you may read this great guide on restoring wasabi in electrum, which isn't the same that you're asking, but close enough and points out to many pitfalls.

samurai wallet is just an example.
I want to understand exactly how to use my 12 phrases and a passphrase for ALL wallets. That is, in my case, I want to use my phrases and passphrase to restore the wallet on the PC and on Android. So that my balance is displayed both there and there.

In response to your assumptions.
No, I use the main network in wassabi.
13 phrase I know, because I have no goal to hack anything, I'm still trying to understand at least the basic things about storing your bitcoins.

This inherent privacy flaw does not only apply to samuri, but to every Bitcoin wallet with the exception of full nodes, Wasabi Wallet and some PoC code. The only difference with samuri is that they market themselves as a privacy centric. You may be interested in learning more about Network Level Privacy.

A single mnemonic seed phrase can be used to derive many different addresses, either via different derivation paths, different address types, etc.
It may often be possible to create multiple instances of the same wallet using different software, but the two wallet applications you've mentioned have nuanced operation and likely use different derivation schemes for certain wallet functions (eg mixed output change, etc). I doubt that there is the option to have them derive all of the same addresses. Perhaps it is possible via advanced settings, but at the least I hope my answer can help to highlight the nature of the situation.


text:
Do all of the records in the same block share the same hash?
From what I have read, when a transaction is settled, the record is added to a block among with other records. The block is given a new hash as well as a hash from previous block.
So does that means that all the records in the same block share the same hash? Why isn't this significant at all?

There are transactions, and blocks. Both have their own unique identifiers, which are calculated as a hash of all the data in it.
When a transaction is included in a block, its hash (txid) doesn't change. It just becomes part of a block. That block also has a hash, which depends on the hash of the parent block, as well as on the hash of all transactions included in it.
So no, not all transactions share the same hash. Their hash is simply the hash of their data, and once created and signed, that hash cannot change without invalidating the signatures. Including it in a block doesn't change that.

Hi Pieter, thanks for answering. So each transaction has its own hash?

Yes, exactly. It's called the txid, and it only depends on the transaction itself (not on what block it's included in).


text:
Transfert d'adresse BTC Segwit vers adresse BTC non segwit
Bonjour, je viens de faire un transfert de BTC segwits vers une adresse BTC non segwit, pensez vous que cela pose un problèmes ???

Non, tout va bien ! C'est le même réseau « Bitcoin ».

Nope, all is fine ! They're both part of the same network.


text:
Teslacoin sync error - No Suitable Inputs Found
Not sure this is the right forum for this, but I thought I'd ask out of desperation.
I have the following error when trying to sync my teslacoin wallet.
I managed to sunc about 6 weeks ago, however haven't been able to sync since then. I left it running overnight and get the error "No Suitable Inputs Found". I'm connected to the tesla network (I think, because I have 8 active connections).
My teslacoin version is v3.2.0.0
The QT version it uses is 5.3.2
Hope that's enough information, if there's anything else required, please let me know and excuse my limited knowledge on crypto, still getting my head around it :)
Thanks
Norrinrad

I'm voting to close this question as off-topic because it is not about bitcoin and has no applicability to other topics.

Updated wallet to v3.3 and re-ran synch :)

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


text:
Estimating Lightning Network's theoretical transaction throughput
I've seen theoretical estimates of Lightning Network's potential transaction throughput range from 100k or more to millions of transactions per second. My question is, how do you come up with an estimate like that? How does the number of active nodes in the network play into it? And what ultimately causes the limit? Is it network speeds, hardware limitations or something else?

It really depends on whether you model random payments (or actual economic activity), some of which will need only a hop or two, while others may need upwards of 6 hops between nodes on different continents. Alternatively you could push the theoretical limit to its maximum and choose payments that only need a single hop between two peers who are close geographically. This gets your theoretical limit up but doesn't resemble reality. A single channel between two geographically close peers using the Lightning protocol just pinging satoshis back and forth is technically transaction throughput but observing this kind of behavior driven by actual economic activity would be rare. If this is the kind of theoretical limit you are interested in though then the bottleneck is network latency and geographical distance between channel counterparties.

I'm afraid this doesn't answer the question. Estimating the throughput of a "network" where nodes connected pairwise constantly ping satoshis back and forth is not what I'm after and probably not how the mentioned estimates were made.

That's the upper limit though right? If you're not happy with that you introduce a second hop and just ping satoshis between 3 nodes that are close geographically. The reason why estimates range from hundreds of thousands to millions of transactions per second is how close you model actual economic activity or random payments versus setting up unrealisitic scenarios to get those numbers up.


text:
What is the practical link between the hashing power in the bitcoin network and its security?
Several books claim that as the hashing/computing power increases, the bitcoin network is securer. How can a non-techie sees that from a practical standpoint?

Several books claim that as the hashing/computing power increases, the bitcoin network is securer. How can a non-techie sees that from a practical standpoint?

Bitcoin has the world's largest/most-energy efficient hashing infrastructure when considered as a whole.
Otherwise, someone would fire up some new miners, make most of the new blocks, don't accept transactions, and then turn off the hashrate to cause transactions to be unconfirmed for long.
But if your hashrate is high, this won't happen.
If the whole miner network were consisting of two computers and some smartphones, NSA or Google would be able to do what I just described above with their computing power.


text:
How do you get "the raw bytes for the genesis transaction"?
This post
How to retrieve the from and to wallet addresses of a transaction?
contains code by Nick ODell which refers to a file called 'genesis.bin' which contains "the raw bytes for the genesis transaction". I wonder if anyone knows how to retrieve that data?

Using blockchain.info:
1) Go to genesis block:
https://blockchain.info/block-height/0
2) Click on block hash:
https://blockchain.info/block/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
3) Copy hash of the first transaction, use rawtx api:
https://blockchain.info/rawtx/4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b?format=hex
Also see https://github.com/joric/pyblockchain

For regular transactions, if you have txindex=1 enabled on your node, you can call getrawtransaction to get the tx hex.
Interestingly, the genesis of the blockchain has a bug, and the genesis tx is not included in the UTXO set. Thus, you must get the genesis block, and take the transaction from that. You can do this using the getblock command, which outputs
0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000

In this, the genesis tx is:
01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000


text:
How do you extract the sending and receiving addresses, and the amount, from a Bitcoin transaction?
I'm trying to write software in Elixir to do it. I have the source code for BitcoinJ, but it's quite laborious figuring out what it does. If I look on bitcoin.info, I can see this block
https://blockchain.info/block/00000000000000000028d1186d6dc9191a6ec93624141031e31814e22e75e0c5
has transactions including 2c1a83d5a511399f151571ff26925764ef73081251c239dd2eb6904304d7bcba
from which blockchain.info has deduced
1PSfpNCcY9uNh1QEitLLLGaDX5LicnBJhx sent 12 BTC to
3PK9abqzd2oK3yWgESSZ6Ps1wPBBu222VW and 0.0789 BTC to
12AE3fQj1CsSGL9c3aZpkSDz991KUxntvB
I want to find out how it deduced it. Thanks for any information.

Happy to see that my answer helped :) If you could mark it as 'answered' that would be great!

Bitcoin transactions are serialized into a hex string that contains all the necessary information to execute the transaction such as input and output values and origin and destination addresses. Blockchain.info decodes transaction hex strings to show this information.'
We can take the transaction you listed as an example and decode it:
Starting with the transaction hash:
2c1a83d5a511399f151571ff26925764ef73081251c239dd2eb6904304d7bcba
We get the hex string using blockchain.info's api:
https://blockchain.info/rawtx/2c1a83d5a511399f151571ff26925764ef73081251c239dd2eb6904304d7bcba?format=hex
Which gives us the hex:
0100000001923f8a599a04a21ef10da6330a21d29fe162d653267da012d6eededb55702e4e010000008a47304402203207e257255a21ac32fe2203f3356f6a5303b8cfca6a6b66c16a23d68f818ff902206a83cce8450aac66f300f0579d4274c6d9852ef3ea74826c9817b87dd9a0abec0141049778e7a85cad5d71cec7fde276842f744f0615897a3f28015489bf2b4bbfda11227e84ff553d7d0762baf5ff50371dfec0c1bf6f5a8ff9937e6bc9c431297981ffffffff02008c86470000000017a914ed3001d36afe91700b20d07bef089323daacc6788750647800000000001976a9140cb6c275be7f179883bb821ef1dfd6b520fc656988ac00000000
Almost all bitcoin libraries (in whatever language) have functions to decode hex strings. Here's an online service that lets you decode the hex:
http://chainquery.com/bitcoin-api/decoderawtransaction
Pasting the hex from above will give you information about the transaction.

Happy to see that my answer helped :) If you could mark it as 'answered' that would be great!

Um - how do you mark it as 'answered'?

There should be a tick/check that you can press on the left hand side of my answer :)

I'd like to just add to the answer above. I'm writing a Phoenix/Elixir app. I found that I can do all of the above using Blockchain RPC commands.
blockchain.ex:
      def getrawtransaction( trans ), do: bitcoin_rpc( "getrawtransaction", [trans] )

      def decoderawtransaction( hex ), do: bitcoin_rpc( "decoderawtransaction", [hex] )

transaction_view.ex:
transaction is a string like this:  "79895513e10d92fee5be47ec11c02bb29078ee0f7f3ca29e16afe113ff94c3d3"
      def mark_up_transaction( transaction ) do
        result = get_transaction( transaction )
        case elem( result, 0 ) do
          :ok -> Poison.encode!( elem( result, 1 ), pretty: true )
          :error -> Poison.encode!( String.downcase( elem( result, 1 )[ "message" ] ), pretty: true )
          _ -> Poison.encode!( "unknown error", pretty: true )
        end
      end

      defp get_hex( transaction ) do
        result = Blockchain.getrawtransaction transaction
        case result do
          {:ok, hex } -> hex
          {:invalid, {:ok, hex }} -> hex # Why it does this I don't know, but it did
          {_, {:ok, hex }} -> hex
          _ -> nil
        end
      end

      defp get_transaction( transaction ) do
        hex = get_hex transaction
        Blockchain.decoderawtransaction( hex )
      end


text:
How can I generate bitcoin cash addresses programmatically?
If I'm a mobile developer who wants to integrate bitcoin cash in an app, how can I generate Bitcoin Cash addresses programmatically, without relying on a 3rd party service, like Coinbase?

You can use the exact same method used to generate Bitcoin addresses.  There is one parameter to change, as far as I know, and that is the part of the "path" that identifies the type of cryptocurrency the address is for.

mobile developer? which language? there are many libraries to generate bitcoin addresses.

@Adam: iOS, Swift would be preferable.

@DaveScotese: The exact method being.....?

@p3scobar HD Protocol, HD Wallet, BIP32, which is described at https://bitcoin.org/en/glossary/hd-protocol with links to some helpful resources.

You might also want to see https://github.com/satoshilabs/slips/blob/master/slip-0044.md to verify that your addresses should be generated at the m/44/145/ derivation path

You can use the following library to generate bitcoin address using Swfit on IOS
https://github.com/onmyway133/AddressGenerator
Please note that the legacy bitcoin cash and bitcoin address is the same the only difference is the network.
if you're looking for cashAddr (qq address) format, take a look on these open source libraries:
https://github.com/bitcoincashjs/cashaddrjs (javascript)
https://cashaddr.bitcoincash.org/
https://github.com/schancel/cashaddr-converter
Original cashAddr algorithm (C++)
https://github.com/Bitcoin-ABC/bitcoin-abc/blob/master/src/cashaddr.cpp
it's pretty easy to read and understand, you can translate it to any language.

I believe OP may be asking for a way to generate cashaddr addresses.

Thanks for mention that, I've edited my question by adding more details.


text:
How is the USD price on Binance calculated since there is no USD involved in the whole exchange?
I'm talking about this: https://i.sstatic.net/p2RzI.png
How is it calculated?

Maybe it uses composite score of other exchanges specially if it never trades a crypto to dollar or vice versa?

There are USDT markets in binance. If you are unaware, USDT is backed by USD and binance can use their own market's USDT value.


text:
Hyperledger consensus protocol
I'm soo deep inside the consensus protocols like PoW, PoS, PoA or other variants used on permissioned/private blockchains such as JPMorgan's Quorum (Based on Raft), Istambul etc..
But I've not entered yet on the Hyperledger consensus protocols and it's implementations specifications and differences.
So my questions are:

What is the consensus protocol used on Hyperledger? It's inherited from any of the mentioned above? Which are it's main properties that make the difference from another consensus protocols?
Which are the main differences between it's variants (Fabric, Iroha, Sawtooth, and Indy)?

Please, it'll be fantastic if useful documentation links/articles/lectures were added with the answers.
Thanks.

First, lets get the terminology straight. Hyperledger is composed of 5 different blockchain technologies, each with it's own consensus algorithm.
Here are two supported by Hyperledger Sawtooth blockchains:

PoET Proof of Elapsed Time (optional Nakamoto-style consensus algorithm used for Sawtooth). PoET with SGX has BFT. PoET Simulator has CFT. Not CPU-intensive as with PoW-style algorithms, although it still can fork and have stale blocks . See PoET specification at https://sawtooth.hyperledger.org/docs/core/releases/latest/architecture/poet.html
RAFT Consensus algorithm that elects a leader for a term of arbitrary time. Leader replaced if it times-out. Raft is faster than PoET, but is not BFT (Raft is CFT). Also Raft does not fork.
Hyperledger Sawtooth has the advantage of having Unpluggable Consensus. An algorithm can be changed without reinitializing the blockchain or even restarting the software.

Here are some other consensus algorithms:

PoW Proof of Work. Completing work (CPU-intensive Nakamoto-style consensus algorithm). Usually used in permissionless blockchains
PoS Proof of Stake. Nakamoto-style consensus algorithm based on the most wealth or age (stake)
PBFT Practical Byzantine Fault Tolerance. A "classical" consensus algorithm that uses a state machine. Uses leader and block election. PBFT is a three-phase, network-intensive algorithm (n^2 messages), so is not scalable to large networks

why would the same umbrella project have 5 different blockchains under it?

Because Hyperledger is not a blockchain standard. One solution does not fit all use cases. If one solution is imposed, who is to choose? Having multiple blockchains allows a diverse environment where the best blockchain technologies may succeed.

is there a table somewhere comparing the 5 blockchains under hyperledger?

No table. Here are summaries:
https://medium.com/practical-blockchain/5-hyperledger-projects-in-depth-3d14c41f902b
https://blog.codecentric.de/en/2018/03/blockchain-project-hyperledger/

in https://softwareengineering.stackexchange.com/questions/388638/hyperledger-fabric-does-it-really-come-with-consensus i argue that hyperledger fabric doesn't really come with any consensus


text:
bitcoind getrawtransaction returning numbers and letters
I’m not sure how to get this data in a format I can use and consume? I was expecting some kind of JSON structure, hopefully there’s a way to get this info easily?

To clarify what's happening:
getrawtransaction 0cd7d76e5...7ecb84755a - returns serialized, hex-encoded data for transaction txid.
getrawtransaction 0cd7d76e5...7ecb84755a 1 - verbose flag, which returns a JSON Object containing information about the transaction.

getrawtransaction 0cd7d76e5666a066ba7e7ecb84755a1c58bee6456de76b2516bb039589c9081d
￼
020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff4b03c02008040ce3505b662f4254432e434f4d2ffabe6d6d8f19ce5133a77536eee1867e96d87b190c3f589b41e7fa55440de72880bdf54e010000000000000004a5f53ed70d000000000000ffffffff026d9bd14a000000001976a91478ce48f88c94df3762da89dc8498205373a8ce6f88ac0000000000000000266a24aa21a9edf6addde9f85e6eb9d990f21898e3dc174ce2f36fb831a9ba2d1205291ec8a1e40120000000000000000000000000000000000000000000000000000000000000000000000000

and
getrawtransaction 0cd7d76e5666a066ba7e7ecb84755a1c58bee6456de76b2516bb039589c9081d 1

{
  "hex": "020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff4b03c02008040ce3505b662f4254432e434f4d2ffabe6d6d8f19ce5133a77536eee1867e96d87b190c3f589b41e7fa55440de72880bdf54e010000000000000004a5f53ed70d000000000000ffffffff026d9bd14a000000001976a91478ce48f88c94df3762da89dc8498205373a8ce6f88ac0000000000000000266a24aa21a9edf6addde9f85e6eb9d990f21898e3dc174ce2f36fb831a9ba2d1205291ec8a1e40120000000000000000000000000000000000000000000000000000000000000000000000000",
  "txid": "0cd7d76e5666a066ba7e7ecb84755a1c58bee6456de76b2516bb039589c9081d",
  "hash": "1d6ec45dd2a63099fbb11adc08f76e2022d014572a0cedc7d6124918ad56ffa8",
  "size": 243,
  "vsize": 216,
  "version": 2,
  "locktime": 0,
  "vin": [
    {
      "coinbase": "03c02008040ce3505b662f4254432e434f4d2ffabe6d6d8f19ce5133a77536eee1867e96d87b190c3f589b41e7fa55440de72880bdf54e010000000000000004a5f53ed70d000000000000",
      "txinwitness": [
        "0000000000000000000000000000000000000000000000000000000000000000"
      ],
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 12.55250797,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_DUP OP_HASH160 78ce48f88c94df3762da89dc8498205373a8ce6f OP_EQUALVERIFY OP_CHECKSIG",
        "hex": "76a91478ce48f88c94df3762da89dc8498205373a8ce6f88ac",
        "reqSigs": 1,
        "type": "pubkeyhash",
        "addresses": [
          "1C1mCxRukix1KfegAY5zQQJV7samAciZpv"
        ]
      }
    },
    {
      "value": 0.00000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_RETURN aa21a9edf6addde9f85e6eb9d990f21898e3dc174ce2f36fb831a9ba2d1205291ec8a1e4",
        "hex": "6a24aa21a9edf6addde9f85e6eb9d990f21898e3dc174ce2f36fb831a9ba2d1205291ec8a1e4",
        "type": "nulldata"
      }
    }
  ],
  "blockhash": "000000000000000000158c2c1ad9a1fcddf8a771fde6b6b4fcf7b443b310cc7b",
  "confirmations": 1,
  "time": 1532027659,
  "blocktime": 1532027659
}

As an alternative to Amaclin’s answer:
Once you use getrawtransaction, you can copy the raw transaction hex string into a decoderawtransactioncommand to return the detailed transaction info.
ie:
bitcoin-cli decoderawtransaction [raw transaction hex string]


text:
what are the dangers of transfering bitcoins between addresses?
So I heard stories about people losing bitcoins because their wallet didn't support a cerain feature.. or bitcoins getting sent to wrong address etc.. how does that happen?
As a software developer I'll be using bitcoin in my application for payments.
What should I be looking for? security wise..
I see that sending between legacy and segwit addresses is not a problem...

Beyond the technical considerations, keep UX in mind - a lot of BTC has been lost just due to user error! Read up on best practices when transacting, and try to steer users towards those good habits (eg, visually double checking that you have copied the correct address, etc). A lot of it is somewhat basic, but very important to consider.

The dangers are minimal.
There is no restriction on which addresses can send to which other addresses.
Most common issues arise from wallets failing to support different address encodings (many wallets still do not support bech32), which results in them rejecting valid addresses.
Other, rarer, issues include mixing up address types - a few wallets in the past have interpreted p2sh addresses as p2pkh addresses and sent coins to the correct HASH160 value embedded in an incorrect locking script, effectively burning them.


text:
Bitcoinjs-lib help with deterministc wallet
i'm learning about bitcoin, and bitcoinjs-lib (https://github.com/bitcoinjs/bitcoinjs-lib)
I created a dummy wallet on coinomi using this mnemonic ('eight better wealth display father cave suffer game sign health fit exchange noble tunnel poet remember owner letter submit illness cage calm dry noble')
and i'm trying to generate a Deterministic wallet with it.
Here is my code

const bip39 = require('bip39')
  const bip32 = require('bip32')
  const bitcoin = require('bitcoinjs-lib')
  const network = bitcoin.networks.bitcoin;
  function getAddress (node) {
  return bitcoin.payments.p2pkh({ pubkey: node.publicKey, network}).address
  }
  const mnemonic = 'eight better wealth display father cave suffer game
  sign health fit exchange noble tunnel poet remember owner letter
  submit illness cage calm dry noble'
  console.log('Testing mnemonic: ',bip39.validateMnemonic(mnemonic))
  const seed = bip39.mnemonicToSeed(mnemonic)
  const root = bip32.fromSeed(seed)
  console.log("m/0'/0/1 ->",getAddress(root.derivePath("m/0'/0/1")));
  console.log("m/0'/0/2 ->",getAddress(root.derivePath("m/0'/0/2")));

.
and this is the output that i got
.

Testing mnemonic: true
  m/0'/0/1 -> 17tdSW83Vjw23TdQsmZDtBAupan9WiGGd5
  m/0'/0/2 -> 17bYSjEXwZxMgVkRRt9pJgDgExiX8Xht3b

Then I go to (https://www.coinomi.com/recovery-phrase-tool.html) to test if my results are correct, and it isn't, could someone help me with this please. How to fix the code?

It looks like it is working. Go to https://www.coinomi.com/recovery-phrase-tool.html and make sure to enter the following:
BIP39 Recovery Phrase : eight better wealth display father cave suffer game sign health fit exchange noble tunnel poet remember owner letter submit illness cage calm dry noble
Coin : Bitcoin
BIP32 Derivation Path (In BIP32 Tab): m/0'/0
Result:
Derived Addresses
m/0'/0/0`   169zFhK1WTuxfhgY6bmzE9zP1godCfZr2V
m/0'/0/1    17tdSW83Vjw23TdQsmZDtBAupan9WiGGd5
m/0'/0/2    17bYSjEXwZxMgVkRRt9pJgDgExiX8Xht3b

hmm thanks, i was using the site wrongly. but just another question, I can get the public key from this code root.derivePath("m/0'/0/0").publicKey.toString('hex') and I get "02db987662b3f1a84c8465ac710de96106ba65aef63d0967e605b802d5fd2579e5" with is correct, but with private key, that doens't work, do you know how I can display my private key for path 0/0? (I resolved it by myself, the solution was to use root.derivePath("m/0'/0/0").toWIF()


text:
Does two witness addresses with same witnessProgram but with different witness version has the same owner
With BIP 350 a new version of witness is introduced.
Take these two address from the mainnet:
bc1pmfr3p9j00pfxjh0zmgp99y8zftmd3s5pmedqhyptwy6lm87hf5sspknck9 and bc1qmfr3p9j00pfxjh0zmgp99y8zftmd3s5pmedqhyptwy6lm87hf5sstpn3we.
These two addresses has the same witness program. Does this mean they have the same owner ?

It depends.
In general, this isn't the case. It could that this holds true for hypothetical witness versions that could be introduced in the future.
However, it is not true specifically for P2WSH (the bc1q address you posted) and P2TR (the bc1p address you posted). In fact, if you ever see two such address, it is almost certainly the case that one is unspendable.

P2WSH programs encode a SHA256 hash of a script.
P2TR programs encode the X coordinate of a (possibly, and likely) tweaked public key.

It is unfathomably unlikely that someone could find a public key they have the private key for, whose X coordinates match the hash of a script they know and can spend.


text:
How do i create/implement a midstate function (One that can accurately calculate the midstate of already mined blocks)
I'm creating an AI controlled bitcoin miner but I'm having trouble manually & locally creating a midstate function to be used for optimizing the mining process. Basically I've created independent functions that handle the steps involved in mining bitcoin manually.... all except the midstate creation function - inline std::string GenerateMidstate(nVersion, nPrevHash, nMerkleRoot);.
I've searched everywhere and have been unable to find a detailed implementation guide and I've also been unsuccessful in doing it myself. The closest I came to understanding the logic behind it was THIS - of which my untested attempt has been wiped along with my laptop and android. Just got a Desktop and I REALLY NEED HELP!!!!
All I really need is an accurate implementation of the midstate function using C++
#include <iostream>
#include ALL LIBRARIES USED IN SOLUTION CODE

// DD - Declare and Define
// DD Variable, DD Function, DD Class, DD Struct ....
// Got the idea from Luffy from ONE PIECE - Gum Gum: Jet Pistol, Gum Gum: Gattling Gun ....
// Anyways .... MOVING ON

// Assume all data types are std::string
// This should help with faster debugging/testing of code/function

std::string GenerateMidstate(std::string nVersion, std::string nPrevHash, std::string nMerkleRoot)
{
  // DD variables
  std::string midstate_hash;

  // Concatenate chunks of data -- 4 bytes, 32 bytes , 32 bytes
  midstate_hash = nVersion + nPrevHash + nMerkleRoot;

  // Then call sha256 to do whatever vodoo it does
  sha256_Magically_Transform_BitChunks_To_Usable_MIDSTATE(midstate_hash);

  // ---- OR ----
  midstate_hash = sha256_Magically_Transform_BitChunks_To_Usable_MIDSTATE(nVersion + nPrevHash + nMerkleRoot);

  // Then return string to caller function
  return midstate_hash;

}

// SAME FUNCTION BUT OPTIMIZED
std::string GenerateMidstate(std::string nVersion, std::string nPrevHash, std::string nMerkleRoot)
{
  // DD variable, Concatenate data and assign variable to the result of midstate function
  std::string midstate_hash = sha256_Magically_Transform_BitChunks_To_Usable_MIDSTATE(nVersion + nPrevHash + nMerkleRoot);

  return midstate_hash; // return midstate
}

// The implementation of the **sha256 midstate** generation can be done within same function, independent function e.g **SHA256_TRANSFORM_MIDSTATE()** or it can exist within an external header file.
// But please all header files used in creating a working function should be clearly stated and not be left to my imagination.. Please and THANKS in advance.

int main()
{
  std::string MIDSTATE, nVersion, nPrevHash, nMerkleRoot, nTime, nBits, nNonce;
  nVersion = 00000002;
  nPrevHash = "8975c5797431243........";
  nMerkleRoot = "ac363c68a97320f039ef4392ac2337754b65bfbb4790faf84d3a27c974c3ab31";
  nTime = "3ab1dd30";
  nBits = "1b9033c2";
  nNonce = "001b32bc";

  MIDSTATE = GenerateMidstate(nVersion, nPrevHash, nMerkleRoot);
  std::cout<< "Midstate of block data is "<< MIDSTATE << std::endl;
  std::cout << "Begin mining attempts" << std::endl;

  return 0;

}

At the end of the day the output should have successfully and accurately generated a midstate which obviously should not remain the same when merkleRootHash is changed/updated.
That's it guys... It's really that simple. The code template is as straight forward as shown above. The rest of the program of course is as technical as you can imagine (lol...)
And Oh.. please do feel free to point out any and all possible issue(s)(and their solutions) that might come up when converting std::string to uint32_t, int32_t, uint256 ..... I wouldn't mind optimizing my untested fix for the possible and inevitable incompatible conversion of string to BTYE error flags
This is really URGENT and extremely important to me. Thanks
ORIGINAL BITCOIN DATA TYPES
int32_t nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
uint32_t nTime;
uint32_t nBits;
uint32_t nNonce;

A side question which can be ignored... P.S please don't ignore
Q. If a midstate is made up of only (nVersion & nPrevHash) or (nPrevHash & nMerkleRoot) - will it still be an accurate midstate if hashing function called
sha256(sha256(midstate, nMerkleRoot, nTime, nBits, nNonce)); or sha256(sha256(nVersion, midstate, nTime, nBits, nNonce));. Would such a midtate be valid??

Probably not the right stack exchange for this question. It is only tangentially related to bitcoin. You probably want Programming, or Computer Science.

You can check this github project for the midstate optimization code. The project is in C# but you can easily port to c++. Works well for existing blocks.
https://github.com/aseemgautam/bitcoin-sha256


text:
MerkleRoot requires transaction hash to build a merkle tree.... Is that the "hash" key/value within the getblocktemplate response or the "data"?
I've created a function for parsing Json Objects and values and just when I think nothing can stop me, just when I feel like I'm the king of the blockchain and I can finally join in on the mining race ..... I get a painful reminder/reality check...
The transactions within the getblocktemplate each have these as keys and thier values.

Question
Which Key value from the list of contents within each transaction array is my merkleroot function supposed to use to build a merklerot Hash.... Is it data or hash ? Or is it the txid?
Merkleroot needs transaction hash....so is it hash Json Key or is it the data Json Key ?

Thanks in advance

Just as Pieter-Wuille explained but in clearer detail........

The transaction data needed to compute a merkleroot in regards to bitcoins mining process is the "txid" key returned by the getblocktemplate response. This applies to non witness transaction processing.
In order to process transactions with witness data, you would need to gat the "hash" key value instead!
It all depends on whether or not you want your miner to process witness tagged transactions.

From getblocktemplate response

data - Raw transaction data.
txid - Double hashed output of raw transaction data.
hash - Witness hash output for transactions with witness data.

This is a working merkleRoot function I developed myself. Works as intended.

#include <iostream>
#include <vector>
#include "sha256.h" // Your sha256 solution with hex parsing function

using std::string;
using std::vector;
using std::cout;

string MKccx(vector<string> &transactions_vector)
{
    string hash;
    string temp1;
    int x = 0, d = 0;
    vector<string> calculate_tx;
    // calculate_tx[0] = SwapPerRound_2bits(coinbase_hash); // Store swapped endian format in vector index 0
    if (transactions_vector.empty())
    {
        std::cout << "\n" << "ERROR! NO TRANSACTION FOUND IN ARRAY!!... NOT EVEN A COINBASE TRANSACTION!!!... EXITING FUNCTION!" << "\n \n";
        throw;
    }
    else if (transactions_vector.size() == 1)
    {
        hash = transactions_vector[0];
        return hash;
    }
    for(int i = 0; i < transactions_vector.size(); i++) // Swap 2 bytes and reverse order of all txid's within transactions array
        transactions_vector[i] = SwapPerRound_2bits(transactions_vector[i]);

    while(true)
    {
        int z = 0;
        if (transactions_vector.size()%2 == 1) // IF SIZE OF VECTOR % 2 == 1
        {
            temp1 = transactions_vector[transactions_vector.size()-1]; // STORE VALUE OF LAST VECTOR INDEX TO TEMP1
            transactions_vector.push_back(temp1); // ADD THAT VALUE INTO VECTOR
            for(int i = 0, z = i+1; i < transactions_vector.size(); i+=2)
            {
                hash = transactions_vector[i] + transactions_vector[z]; // Concatenate txid pairs together
                hash = double_Hex_sha256(hash); // DoubleHash concatenated txid's
                calculate_tx.push_back(hash);
                z+=2;
            }
            for (int i = 0; i < calculate_tx.size(); i++)
            {
                temp1 = calculate_tx[i];
                transactions_vector[i] = temp1;
            }
            transactions_vector.resize(calculate_tx.size());
            if (calculate_tx.size() == 2)
            {
                hash = calculate_tx[0] + calculate_tx[1];
                hash = double_Hex_sha256(hash);
                hash = SwapPerRound_2bits(hash);
                return hash;
            }
            calculate_tx.clear();
        }
        else if (transactions_vector.size()%2 == 0) // IF SIZE OF VECTOR % 2 == 0
        {
            for(int i = 0, z = i+1; i < transactions_vector.size(); i+=2)
            {
                hash = transactions_vector[i] + transactions_vector[z]; // Concatenate txid pairs together
                hash = double_Hex_sha256(hash); // DoubleHash concatenated txid's
                calculate_tx.push_back(hash);
                z+=2;
            }
            for (int i = 0; i < calculate_tx.size(); i++)
            {
                temp1 = calculate_tx[i];
                transactions_vector[i] = temp1;
            }
            transactions_vector.resize(calculate_tx.size());
            if (calculate_tx.size() == 2)
            {
                hash = calculate_tx[0] + calculate_tx[1];
                hash = double_Hex_sha256(hash);
                hash = SwapPerRound_2bits(hash);
                return hash;
            }
            calculate_tx.clear();
        }
    }
}// Successful compilation on 06/02/2022

int main(int argc, char const *argv[])
{
    // HARDCODED TXID'S
    vector<string> transaction = {"8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87","fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4","6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4","e9a66845e05d5abc0ad04ec80f774a7e585c6e8db975962d069a522137b80c1d","c02092b00aa0aea8463c624ccebdb0efdb18347068aa48c3d1b42766ca7e92ba"};

    vector<string> block100000 = {"8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87","fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4","6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4","e9a66845e05d5abc0ad04ec80f774a7e585c6e8db975962d069a522137b80c1d"};

    vector<string> transaction2 = {"8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87"};

    vector<string> transaction3 = {};
    string merk;
    merk = MKccx(transaction3);
    std::cout << "MerkleRoot Hash is : " << merk << "\n \n";
    // std::cout << "Merkle hash of Block 100,000 is : " << merk << "\n \n";

    return 0;
}

SwapPerRound_2bits() - Whatever solution you use for swapping endianness
double_Hex_sha256() - Equivalent of sha256(sha256());

WONDERING HOW IT ALL COMES TOGETHER??
YOU'LL NEED A JSON LIBRARY TO GET THE TRANSACTIONS INTO YOUR C++ PROGRAM
IF YOU DON'T ALREADY HAVE A WORKING JSON PARSER/SERIALIZATION LIBRARY THEN I RECOMMEND nlohmann json library FOR JSON PARSING & SERIALIZATION.
Just include its single header ".hpp" file in project folder and you're good to go!
A little batch script used but nothing advanced.
#include "json.hpp" // for json serialization parser
#include <fstream> // for ifstream
#include <stdlib.h> // for system calls
#include "windows.h" // if on windows
using namespace nlohmann;
using std::ifstream;

inline void RunCommand_With_Output_Without_SYMBOL_Defined(string Command_To_Run, string Output_FileName)
{
    string xx_combine = Command_To_Run + " >" + Output_FileName;
    char run_command[xx_combine.length()];
    strcpy(run_command, xx_combine.c_str());
    system(run_command); // execute the command
} // Successfully compiled on 20/01/2022 10:20PM

void GETBLOCKTEMPLATE()
{
    string getblocktemplate_syntax = "cd \"C:\\Users\\YOUR DESKTOP NAME\\Desktop\\Z Code\" && call gt.bat";
    string filename = "getblocktemplate_Response.json"; // Create file Name
    RunCommand_With_Output_Without_SYMBOL_Defined(getblocktemplate_syntax, filename); // create json file with getblockresponse output
} // Successfully compiled on 31/01/2022 11:30AM

int main(int argc, char const *argv[])
{
    // DYNAMIC APPROACH FOR USAGE
    string merk;
    GETBLOCKTEMPLATE(); // Call getblocktemplate()
    ifstream file_input("getblocktemplate_Response.json"); // Open file
    json object = json::parse(file_input); // Parse json data
    file_input.close(); // close file
    vector<string> Txid_Retreiver; // vector for holding transactions

    auto &Array_Txid = object["transactions"]; // Create object pointing to 'transactions' array
    for(auto &target : Array_Txid) // Traverse through transaction array
        Txid_Retreiver.push_back(target["txid"].get<std::string>());

    merk = MKccx(Txid_Retreiver);
    std::cout << "MerkleRoot Hash is : " << merk << "\n \n";

    return 0;
}

And that's pretty much how I pulled it off.
Hope this helps someone.
Cheers....

The transaction Merkle tree, whose root is stored in the block header, is computed over the individual transactions' txid value (which are the double-SHA256 of their data after stripping the witness).
The witness Merkle tree, whose root is stored in a coinbase output (only required for blocks containing segwit transactions, see BIP141), is computed over the transactions' hash value (which are the double-SHA256 of their data including the witness).
I recommend you study the source code of existing mining software, instead of asking questions about every tiny detail of the process.

Okay thanks... So which I'm I to feed to the merkleroot function to build a valid merklehash.. Is it hash or txid.. Remember that I'm manually placing coinbase as index 0 within list of transactions so this is pretty much the lingering difficulties.

Also if you noticed, most of my questions are from this angle only..... Its really the only part that both vital and somewhat hard to understand. Every other part I've implemented.... Thanks for your patience and answers.. They've been really helpful.

Please re-read my answer. I have answered your question exactly. You need both a Merkle tree of txids, and one of hashes. If these details confuse you, you'll have 100s more questions before you have a functioning miner. You really should look at example mining code instead.


text:
Which rpc command is used when solo mining to submit successful hash <= target?
Basically I am trying to know which command is to be used when attempting to submit a block to the network (amongst other things).
Q1. Is the command bitcoin-cli submitheader or bitcoin-cli submitblock ?
Q2. Whatever the answer is in Q1, is the data to be passed to be formated within " " (double quotes) or can it be passed without the need of " " (double quotes).
I personally lean towards Q1 being bitcoin-cli submitblock  but then again I am to send the block header combination that generates a successful hash to the network for verification   (0000002fa7931a0f87d08733874323.............) so I start thinking if bitcoin-cli submitheader is the right API call.
This is very Important. I wouldn't want to successfully mine a block and be unable to submit it... That would be just hilariously sad o_o
Thanks in advance.

Q1. Is the command bitcoin-cli submitheader or bitcoin-cli submitblock ?

Blocks are more than just a header, they contain transactions too. submitheader just takes a header, not the transactions. So it is not sufficient to just submit the header in order to relay a block to the network. Thus the correct RPC is submitblock.
As far as I can tell, submitheader does not actualy send the header to any peers either, while submitblock will.

Q2. Whatever the answer is in Q1, is the data to be passed to be formated within " " (double quotes) or can it be passed without the need of " " (double quotes).

If you are using a typical shell, it does not matter. If you are doing it programatically, then do not use double quotes.
bitcoind expects the argument to either RPC to be a string, although it is not receiving the string directly from your shell but rather via HTTP from bitcoin-cli. bitcoin-cli will take the string as provided by the shell and format it for use in the JSON-RPC protocol bitcoind uses. If you are writing a program, you need to follow the JSON-RPC protocol for how to format data.
But when using bitcoin-cli, it takes strings as provided by the shell. As the argument to both submitblock and submitheader is a hexadecimal string, there is no need to use any form of quotes (either single ' or double ") as there are no special characters in hex strings that shells would need to escape or otherwise be informed to not interpret. However, single or double quoting the string is completely harmless because shells will interpret those quotes and remove them before providing the string to bitcoin-cli.

Thanks for this explanation.... Helps a great deal

The simplicity of your explanation is gratifying and very clear... Would you mind dropping your btc address for a Thank You token once I make a profit? It doesn't have to be now though, I could comment here when i'm all set.


text:
What is the actual working implementation code used in double hashing a block header in c++?
I need to successfully generate a hash value based on what i feel so far has been described as a hexadecimal filtered sha256 implementation... or something like that. I've been here , here , here, here and here ... amongst many other places.

Example Case:
Using "abc" as the input string,
AnyHash.com with the 'Hex' checkbox ticked produces a hash - 15f27c6b8828e62cd3d09e8b4de5230844facfc88f1b9c34129cab7706f2f561. And when 15f27c6b8828e62cd3d09e....... is fed as the input it produces the hash - 42a5aa8cd08811f43428c42f76279a5a92e12b3e3f7080b87804a05135773487.
Following the comment by dave_thompson_085 i've been trying to make it work.
The sha256 implementation in use is the one provided by Zedwood and below is my unfruitful struggle to recreate the hash output as generated by AnyHash.com.

//  -----------------------                 --------------------------- //
//  -----------------------                 --------------------------- //
//  -----------------------                 --------------------------- //
// COMMENT BY DAVE_THOMPSON_085

// That code is okay except the silly std::string wrapper is useless for bitcoin. Do something like :
// unsigned char hdr[80], hash[32];
// SHA256 ctx = SHA256();

/* fill in hdr */
// ctx.init();
// ctx.update(hdr,80);
// ctx.final(hash);
// ctx.init();
// ctx.update(hash,32);
// ctx.final(hash);
/* (reverse and) use results in hash, possibly converted to hex or whatever you need */

//  -----------------------                 --------------------------- //
//  -----------------------                 --------------------------- //
//  -----------------------                 --------------------------- //

std::string sha256(std::string input)
{
    string kk;
    // header_data is "160" in size because midstate optimization is not implemented
    unsigned char header_data[160], hash[32];
    char hash2[32], hash3[32];
    char buf[2*32+1];
    buf[2*32] = 0;

    // input = GetBinaryStringFromHexString(input); // convert input data to binary format
    init();
    update((unsigned char*)input.c_str(), input.length());
    final(hash);

    // Iterate and store values from hash[] to hash2[]
    for (int i = 0; i < 32; i++)
        hash2[i] = hash[i];

    // kk = final2(hash); // MODIFIED FUNCTION THAT RETURNS A STRING
    // //kk = char_ToString(hash2); // Converts char array to string variable
    kk = GetBinaryStringFromHexString(char_ToString(hash2)); // convert data to binary format
    input = kk; // Store data in kk to input
    init();
    update((unsigned char*)input.c_str(), input.length());
    final((unsigned char *)hash3); // Modified as char* to unsigned char* conversion is not allowed

    for (int i = 0; i < 32; i++)
        sprintf(buf+i*2, "%02x", hash3[i]); // ORIGINAL : sprintf(buf+i*2, "%02x", digest[i])

    return std::string(buf); // Return buf as string instead of char
}

int main(int argc, char const *argv[])
{
    std::string data = "abc";
    std::cout << "sha256(data) : " << sha256(data) << "\n";
    std::cout << "sha256(sha256(data)) :  " << sha256(sha256(data)) << "\n";
    return 0;
}

//  Input : abc
//  Expecting first hash output from sha256 ( with hex conversion ) to be :
// 15f27c6b8828e62cd3d09e8b4de5230844facfc88f1b9c34129cab7706f2f561 - single sha

//  And second output to be :
// 42a5aa8cd08811f43428c42f76279a5a92e12b3e3f7080b87804a05135773487 - double sha

//  As generated byanyhash.com

It's always the obvious implementations that are sometimes the hardest to implement. - in my opinion.
The same hash generator with the 'Hex' ticked successfully generates the correct hash of block 100000 existing in the bitcoin blockchain. Block 100,000 hashed by AnyHash.com
Block header 0100000050120119172a610421a6c3011dd330d9df07b63616c2cc1f1cd00200000000006657a9252aacd5c0b2940996ecff952228c3067cc38d4885efb5a4ac4247e9f337221b4d4c86041b0f2b5710
I tried getting the code from bitcoin source code hahahahahahahahahahahaha....... need i say more?
With so many headers included and dependencies beyond what is needed, just thinking about it was a nightmare. So please, if anyone knows how to actually make this code work... please assist a fellow programmer..
Lord knows all things bitcoin make most programmers look like a novice. A great technology nonetheless!
Below are the other functions used(in the event it has something wrong with it) Thanks in advance!
sing std::hex;

string GetBinaryStringFromHexString(string hexData);
// std::string char_ToString(char data[]);

std::string char_ToString(char data[])
{
    std::string stringData = ""; //declare string variable
    // int size = sizeof(data)/sizeof(*data);
    int size =  strlen(data); //get amount of characters in char array
    for (int i = 0; i < size; i++) // for elements in char array
        stringData = data; // store elements in string variable "stringData"
    return stringData; // return stringData
}

string GetBinaryStringFromHexString(string hexData)
{
    string binaryResult = "";
    for (int i = 0; i < hexData.length(); ++i)
    {
        switch (hexData[i])
        {
        case '0':
            binaryResult.append("0000");
            break;
        case '1':
            binaryResult.append("0001");
            break;
        case '2':
            binaryResult.append("0010");
            break;
        case '3':
            binaryResult.append("0011");
            break;
        case '4':
            binaryResult.append("0100");
            break;
        case '5':
            binaryResult.append("0101");
            break;
        case '6':
            binaryResult.append("0110");
            break;
        case '7':
            binaryResult.append("0111");
            break;
        case '8':
            binaryResult.append("1000");
            break;
        case '9':
            binaryResult.append("1001");
            break;
        case 'a':
            binaryResult.append("1010");
            break;
        case 'b':
            binaryResult.append("1011");
            break;
        case 'c':
            binaryResult.append("1100");
            break;
        case 'd':
            binaryResult.append("1101");
            break;
        case 'e':
            binaryResult.append("1110");
            break;
        case 'f':
            binaryResult.append("1111");
            break;
        }
    }
    return binaryResult;
}

Note that I am analyzing your code as if I compiled it as is. That means anything that is commented out is ignored.

The first major issue is this line:
    kk = GetBinaryStringFromHexString(char_ToString(hash2)); // convert data to binary format

The earlier final(hash); will have produced a char array filled with the SHA256 hash as binary data. Thus there is no need to convert that to a std::string and subsequently try to convert that to a binary string. This line is unnecessary and hash could be provided directly as input to the next update(). Fixing this should allow you to find the double SHA256 of abc.
The second major issue with this code is that GetBinaryStringFromHexString is not doing what you think it is doing. It is converting a string of hex to a string of binary. That is not the same as converting a string of hex to the binary data represented by that hex. What you actually end up with in that function is a much longer string whose data is the ascii characters for 0 and 1, not the data represented by the hex string itself. This StackOverflow answer provides a hex string parsing function that you can use. Fixing this should allow you to find to find the double SHA256 of a block header that is provided as a hex string.

A couple of minor issues:

There is no need to duplicate hash to hash2. You can provide it directly as the parameter to the second update too.
There is no need to have hash2 or hash3. You can use hash in final each time and it will be overwritten with the new hash.
The function char_ToString is not necessary. Besides that you don't need it for your code, std::string has a constructor that can take a char * and its length.

Your final code would be something like:
std::string sha256(std::string input)
{
    string kk;
    // header_data is "160" in size because midstate optimization is not implemented
    unsigned char header_data[160], hash[32];
    char buf[2*32+1];
    buf[2*32] = 0;

    input = ParseHex(input);
    init();
    update((unsigned char*)input.c_str(), input.length());
    final(hash);

    init();
    update(hash, 32);
    final(hash);

    for (int i = 0; i < 32; i++)
        sprintf(buf+i*2, "%02x", hash[i]); // ORIGINAL : sprintf(buf+i*2, "%02x", digest[i])

    return std::string(buf); // Return buf as string instead of char
}

std::string ParseHex(const std::string& s)
{
    assert(s.size() % 2 == 0);
    static const std::size_t symbol_count = 256;
    static const unsigned char hex_to_bin[symbol_count] = {
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 - 0x07
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 - 0x0F
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 - 0x17
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 - 0x1F
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 - 0x27
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 - 0x2F
                 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 0x30 - 0x37
                 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 - 0x3F
                 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, // 0x40 - 0x47
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 - 0x4F
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 - 0x57
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 - 0x5F
                 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, // 0x60 - 0x67
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 - 0x6F
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 - 0x77
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 - 0x7F
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 - 0x87
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 - 0x8F
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 - 0x97
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 - 0x9F
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 - 0xA7
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 - 0xAF
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 - 0xB7
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 - 0xBF
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 - 0xC7
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 - 0xCF
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 - 0xD7
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 - 0xDF
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 - 0xE7
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 - 0xEF
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 - 0xF7
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  // 0xF8 - 0xFF
               };

    std::string out;
    auto& itr = s.begin();
    while (itr != s.end())
    {
       unsigned char b = static_cast<unsigned char>(hex_to_bin[*(itr++)] << 4);
       b |= static_cast<unsigned char>(hex_to_bin[*(itr++)]     );
       out.push_back(b);
    }
    return out;
}

You said .. Does that imply that i need a hex parser??    And if i do, where exactly will that parser be used? What is the simplified pseudo code according to your answer?

Also, the unnecessary hash2 and hash3 as well as the functions char_ToString() and GetBinaryStringFromHexString()  have been removed! The no. 3 on your list of minor issues clearly states that the  char_ToString is not necessary... I'm assuming that your reference to the std::string constructor is merely a reference to a more healthy and accurate approach - in the event that a conversion was actually required .

@AncientTides The hex parser function would be used where you currently have // input = GetBinaryStringFromHexString(input);, once you uncomment that line so you can try to hash the hex for a block header. I will edit with modified code.

Ran the edited code, it says 'ParseHex' was not declared in this scope.   input = ParseHex(input); . I don't have the ParseHex(); function you used. Could you please add it to your answer.... thanks

ParseHex is a placeholder for the hex parsing function from the stack overflow answer I linked. Figuring out how to actually use that function is left as an exercise for the reader.

Jesus Christ.. hahahahaha  ....... I just spent the last 13 days exercising this specific issue... Any more exercise and 3 years of work is going to go down the drain. With absolutely no commenting whatsoever, how am i in my current state of non-stop debugging going to understand this code.  Seriously man, you gotta help me here. Don't leave me hangin on this one.

Pleeeeeeeeeeeeeeeeeeeeeeeeeeeaaaaaaaaaaaaassssssssssssssssssseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee.............. Don't make a grown man cry!

I've added a ParseHex function that might work. I did not compile this code, but logically it should work.

Works like a charm Andrew....... WORKS LIKE A CHARM.

Alright so it's been quite a ride on this specific task but Thanks to Andrew Chow there's finally a working version that I know a lot of newbies and even intermediate developers & programmers will surely find useful.
Had to make slight modifications to the ParseHex() provided by Andrew Chow....
Headers needed are:
#include <string>
#include <cstring>
#include <assert.h> // assert() is used

Sha256 implementation in use is the one provided by Zedwood !!
First is the ParseHex function provided by Andrew Chow

std::string ParseHex(std::string& s)
{
    assert(s.size() % 2 == 0);
    static const std::size_t symbol_count = 256;
    static const unsigned char hex_to_bin[symbol_count] =
    {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 - 0x07
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 - 0x0F
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 - 0x17
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 - 0x1F
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 - 0x27
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 - 0x2F
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 0x30 - 0x37
        0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 - 0x3F
        0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, // 0x40 - 0x47
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 - 0x4F
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 - 0x57
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 - 0x5F
        0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, // 0x60 - 0x67
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 - 0x6F
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 - 0x77
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 - 0x7F
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 - 0x87
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 - 0x8F
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 - 0x97
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 - 0x9F
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 - 0xA7
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 - 0xAF
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 - 0xB7
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 - 0xBF
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 - 0xC7
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 - 0xCF
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 - 0xD7
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 - 0xDF
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 - 0xE7
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 - 0xEF
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 - 0xF7
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  // 0xF8 - 0xFF
    };

    std::string out;
    auto itr = s.begin();
    while (itr != s.end())
    {
       unsigned char b = static_cast<unsigned char>(hex_to_bin[*(itr++)] << 4);
       b |= static_cast<unsigned char>(hex_to_bin[*(itr++)]     );
       out.push_back(b);
    }
    return out;
}

Then next is the modified sha256 function call.

std::string sha256(std::string input)
{
    string kk;
    int size = input.length();

    unsigned char header_data[size], hash[32];// header_data is "160" in size because midstate optimization is not implemented
    char buf[2*32+1];
    buf[2*32] = 0;

    input = ParseHex(input); // ParseHex() is UNDEFINED !!!
    init(); // Initiialize
    update((unsigned char*)input.c_str(), input.length()); // Update
    final(hash); // Finalize

    // Uncomment code below if you want to directly double hash input data using a single "sha256(input)" function call
    // init();
    // update(hash, 32);
    // final(hash);

    for (int i = 0; i < 32; i++)
        sprintf(buf+i*2, "%02x", hash[i]); // feed data from hash into buf and format data

    return std::string(buf); // Return buf as string instead of char
}

THESE FUNCTIONS NEED TO BE PASTED INSIDE THE SHA256 HEADER FILE
YOU'D HAVE TO REVERSE ENDIANESS OF HASH RESULT.
ALSO, THE "assert()" THROWS AN ERROR FOR UNEVEN LENGTHED INPUTS.. SO "abc" WON'T WORK.

Hope this helps you if you ever need this....
Cheers


text:
What "Data" should be inserted into the argument field of the "createrawtransaction" rpc command
This resource states that "data": "hex",(string, required) A key-value pair. The key must be "data", the value is hex-encoded data.

Questions

It says that the field is "required" but what exactly should that data be ??
If the data isn't provided does that invalidate the coinbase transaction (even when block height is encoded into scriptSig)?

I asked the question here but an answer wasn't provided. So I decided to make it a stand-alone question.

Goal

To confirm what data should be inserted within this data field

The data field is only for when you want to create data-carrying transactions (ones with an OP_RETURN). This is generally not needed in normal transactions (and its usage there is somewhat controversial even). For coinbase transactions, you do need an OP_RETURN output with the witness commitment as specified in BIP141, if you want to include segwit transactions in your block.
However, you shouldn't use createrawtransaction and related RPCs for constructing coinbase transactions, as they will miss the necessary scriptSig contents. Even if you could, it would most likely be so slow as to be useless (a modern miner like an AntMiner S17 needs 13000 coinbases per second). Thus, the miner firmware constructs them using hardcoded logic.
The field is marked as required in the RPC documentation for such outputs. If you don't want a data-carrying output, the object simply wouldn't exist.

So the "data" needed is the string "OP_RETURN"?

No, the data is whatever you want it to be, and in your case, you don't want it at all. If used, createrawtransaction uses an OP_RETURN-based scriptPubKey to encode it in the transaction. But again, this is completely unrelated to anything you're trying to do.

Thanks Pieter....... I wish you'd always be online to help me out.....Cheers

is there a function similar to createrawtransaction that allows you to pass a specific scriptsig? I'm dealing with this because I don't know how to encode the transaction in hex without using createrawtransaction