# 比特币交互 ## bitcoin wiki https://en.bitcoin.it/ ## Bitcoins the hard way: Using the raw Bitcoin protocol http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html ## 如何用go语言生成比特币地址 如何生成比特币公私钥 https://medium.com/coinmonks/how-to-generate-a-bitcoin-address-step-by-step-9d7fcbf1ad0b 比特币地址生成、签名并发起交易(good one) https://www.thepolyglotdeveloper.com/2018/02/generate-cryptocurrency-private-keys-public-addresses-golang/ https://www.jianshu.com/p/37ec9c70a516 压缩和非压缩key的区别 https://bitcoin.stackexchange.com/questions/3059/what-is-a-compressed-bitcoin-key ## 如何在测试网测试 https://support.bitpay.com/hc/en-us/articles/360004102011-What-is-testnet-How-do-I-avoid-testnet-Bitcoin-scams- ## bitcoin testnet https://en.bitcoin.it/wiki/Testnet ## bitcoin testnet explorer https://live.blockcypher.com/btc-testnet/ ## private key 和wifkey转换 https://en.bitcoin.it/wiki/Wallet_import_format ## how to spend bitcoin https://gocoin.pl/gocoin_manual_spending.html ## The Challenges of Optimizing Unspent Output Selection https://medium.com/@lopp/the-challenges-of-optimizing-unspent-output-selection-a3e5d05d13ef ## btcwallet https://www.jianshu.com/p/6436495ffda2 ## btcrpc quick reference https://bitcoin.org/en/developer-reference#rpc-quick-reference https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list ## bitcoind config https://en.bitcoin.it/wiki/Running_Bitcoin ## bitcoin config generator https://jlopp.github.io/bitcoin-core-config-generator/ ## install bitcoind https://bitcoin.org/en/full-node#ubuntu-1604 ## sample bitcoind config ``` bash ## ## bitcoin.conf configuration file. Lines beginning with # are comments. ## # Network-related settings: # Run on the test network instead of the real bitcoin network. #testnet=0 # Run a regression test network #regtest=0 # Connect via a SOCKS5 proxy #proxy=127.0.0.1:9050 # Bind to given address and always listen on it. Use [host]:port notation for IPv6 #bind=<addr> # Bind to given address and whitelist peers connecting to it. Use [host]:port notation for IPv6 #whitebind=<addr> ############################################################## ## Quick Primer on addnode vs connect ## ## Let's say for instance you use addnode=4.2.2.4 ## ## addnode will connect you to and tell you about the ## ## nodes connected to 4.2.2.4. In addition it will tell ## ## the other nodes connected to it that you exist so ## ## they can connect to you. ## ## connect will not do the above when you 'connect' to it. ## ## It will *only* connect you to 4.2.2.4 and no one else.## ## ## ## So if you're behind a firewall, or have other problems ## ## finding nodes, add some using 'addnode'. ## ## ## ## If you want to stay private, use 'connect' to only ## ## connect to "trusted" nodes. ## ## ## ## If you run multiple nodes on a LAN, there's no need for ## ## all of them to open lots of connections. Instead ## ## 'connect' them all to one node that is port forwarded ## ## and has lots of connections. ## ## Thanks goes to [Noodle] on Freenode. ## ############################################################## # Use as many addnode= settings as you like to connect to specific peers #addnode=69.164.218.197 #addnode=10.0.0.2:8333 # Alternatively use as many connect= settings as you like to connect ONLY to specific peers #connect=69.164.218.197 #connect=10.0.0.1:8333 # Listening mode, enabled by default except when 'connect' is being used #listen=1 # Maximum number of inbound+outbound connections. #maxconnections= # # JSON-RPC options (for controlling a running Bitcoin/bitcoind process) # # server=1 tells Bitcoin-Qt and bitcoind to accept JSON-RPC commands #server=0 # Bind to given address to listen for JSON-RPC connections. Use [host]:port notation for IPv6. # This option can be specified multiple times (default: bind to all interfaces) #rpcbind=<addr> # If no rpcpassword is set, rpc cookie auth is sought. The default `-rpccookiefile` name # is .cookie and found in the `-datadir` being used for bitcoind. This option is typically used # when the server and client are run as the same user. # # If not, you must set rpcuser and rpcpassword to secure the JSON-RPC api. The first # method(DEPRECATED) is to set this pair for the server and client: #rpcuser=Ulysseys #rpcpassword=YourSuperGreatPasswordNumber_DO_NOT_USE_THIS_OR_YOU_WILL_GET_ROBBED_385593 # # The second method `rpcauth` can be added to server startup argument. It is set at intialization time # using the output from the script in share/rpcuser/rpcuser.py after providing a username: # # ./share/rpcuser/rpcuser.py alice # String to be appended to bitcoin.conf: # rpcauth=alice:f7efda5c189b999524f151318c0c86$d5b51b3beffbc02b724e5d095828e0bc8b2456e9ac8757ae3211a5d9b16a22ae # Your password: # DONT_USE_THIS_YOU_WILL_GET_ROBBED_8ak1gI25KFTvjovL3gAM967mies3E= # # On client-side, you add the normal user/password pair to send commands: #rpcuser=alice #rpcpassword=DONT_USE_THIS_YOU_WILL_GET_ROBBED_8ak1gI25KFTvjovL3gAM967mies3E= # # You can even add multiple entries of these to the server conf file, and client can use any of them: # rpcauth=bob:b2dd077cb54591a2f3139e69a897ac$4e71f08d48b4347cf8eff3815c0e25ae2e9a4340474079f55705f40574f4ec99 # How many seconds bitcoin will wait for a complete RPC HTTP request. # after the HTTP connection is established. #rpcclienttimeout=30 # By default, only RPC connections from localhost are allowed. # Specify as many rpcallowip= settings as you like to allow connections from other hosts, # either as a single IPv4/IPv6 or with a subnet specification. # NOTE: opening up the RPC port to hosts outside your local trusted network is NOT RECOMMENDED, # because the rpcpassword is transmitted over the network unencrypted. # server=1 tells Bitcoin-Qt to accept JSON-RPC commands. # it is also read by bitcoind to determine if RPC should be enabled #rpcallowip=10.1.1.34/255.255.255.0 #rpcallowip=1.2.3.4/24 #rpcallowip=2001:db8:85a3:0:0:8a2e:370:7334/96 # Listen for RPC connections on this TCP port: #rpcport=8332 # You can use Bitcoin or bitcoind to send commands to Bitcoin/bitcoind # running on another host using this option: #rpcconnect=127.0.0.1 # Create transactions that have enough fees so they are likely to begin confirmation within n blocks (default: 6). # This setting is over-ridden by the -paytxfee option. #txconfirmtarget=n # Miscellaneous options # Pre-generate this many public/private key pairs, so wallet backups will be valid for # both prior transactions and several dozen future transactions. #keypool=100 # Pay an optional transaction fee every time you send bitcoins. Transactions with fees # are more likely than free transactions to be included in generated blocks, so may # be validated sooner. #paytxfee=0.00 # Enable pruning to reduce storage requirements by deleting old blocks. # This mode is incompatible with -txindex and -rescan. # 0 = default (no pruning). # 1 = allows manual pruning via RPC. # >=550 = target to stay under in MiB. #prune=550 # User interface options # Start Bitcoin minimized #min=1 # Minimize to the system tray #minimizetotray=1 ``` ## testnet config ``` bash # Generated by https://jlopp.github.io/bitcoin-core-config-generator/ # This config should be placed in following path: # ~/.bitcoin/bitcoin.conf # [core] # Specify the location of the configuration file. To use non-default location, create a default location config file containing this setting. conf=~/.bitcoind/bitcoin.conf # Specify a non-default location to store blockchain and other data. datadir=/mnt/bitcoin/bitcoind/testnet # [debug] # Run this node on the Bitcoin Test Network. testnet=1 # [rpc] # Accept command line and JSON-RPC commands. server=1 # Accept public REST requests. rest=1 # RPC user rpcuser=rpcuser # RPC password rpcpassword=rpcpass # RPC allow ip (allow all) rpcallowip=0.0.0.0/0 ``` ## chainquery online api http://chainquery.com/ ## bitcoin core doc https://bitcoincore.org/en/doc/ ## bitcoin address prefix https://en.bitcoin.it/wiki/List_of_address_prefixes ## What is “Script Hash” address exactly and how does it work? https://bitcoin.stackexchange.com/questions/9678/what-is-script-hash-address-exactly-and-how-does-it-work ## How do I get my public address in Bitcoin Core? https://bitcoin.stackexchange.com/questions/42682/how-do-i-get-my-public-address-in-bitcoin-core ## Technical_background_of_version_1_Bitcoin_addresses https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses ## sendfrom to send bitcoin from an address ``` bash ➜ ~ bitcoin-cli sendfrom importaccount1 mxZ6V6CSftcznexXRRcqf8SikCoVyx8oC8 0.001 e2ec8ab71e9b4df4ee8a50d6ea31a77b9614ce39adf7918af01c513b72866ce8 ``` ## dumpprivkey ``` bitcoin-cli dumpprivkey "n1ZpFDuoyVqX4HKdaFibz4Nt2ktcmptwKx" 935TVEJEGP4rPz8Zbpo6FF9oenA21twzeYNiM3ykLGTazsjJMj2 ``` ## listaccout ``` ➜ ~ bitcoin-cli listaccounts 6 { "": 0.00120000, "2N9mTCamT4NX4HUJaKXVYYDD6T7is7KeECc": 0.00000000, "importaccount1": 0.57795013, "importaccount2": 0.00000000, "testaccount1": 0.00000000, "testaccount2": 0.00000000 } ``` ## listreceived by address ``` bitcoin-cli listreceivedbyaddress 1 true ``` ## rename account ``` ➜ ~ bitcoin-cli setaccount "2Mup1QpVkvWyaupHQGBhW15Pw8dXGUaNaYR" renameaccount1 ``` ## how-exactly-does-rescan-work https://bitcoin.stackexchange.com/questions/1249/how-exactly-does-rescan-work ## txindex option to get full transactions synced ``` Transaction Database Index and txindex Option By default, Bitcoin Core builds a database containing only the transactions related to the user’s wallet. If you want to be able to access any transaction with commands like gettransaction, you need to configure Bitcoin Core to build a complete transaction index, which can be achieved with the txindex option. Set txindex=1 in the Bitcoin Core configuration file ``` https://bitcoin.stackexchange.com/questions/35707/what-are-pros-and-cons-of-txindex-option 在配置中开启后,使用命令 bitcoind -reindex启动,重建数据库,然后用getrawtransaction才能查到数据 ``` ➜ ~ bitcoin-cli getrawtransaction "d148e683448a59e7ebdbaba9852345ac918244077d88bb4fe95fa2d1b515c6c6" 0100000001cf0efe3065cf54411396711cfcb3c1e5b9acbee36c65cafc3ddc5b59c68ccb6f010000006b4830450221008a3877ee9afda8f8814587ca47141a8fbaadfbc582534107eccc18ef9fcf2f2002201da2cc76c9fbeaba82253e65c5013dd26eb32557f85f5dad2780db23ce0e04a901210242769b585aea2678249f21e4969a7498788089442e8f131f2f7083772bddc256ffffffff02e0930400000000001976a914c5ff9d2f9b1f0cdee1483778261a27466542d95288ac247f1102000000001976a914991b366d19907b4c65891a663e024cf28d6dc09f88ac00000000 ``` gettransaction只能查钱包中相关账号的交易: ``` bitcoin-cli gettransaction d79cbf3bcaacbf48e0a10eb7b0e1d3c57215cfea81d1c284d855c04192863934 true { "amount": 0.10476174, "confirmations": 84, "blockhash": "00000000000006ebe50430e54f1a23d8c579dbb67e81dd3835522a321db85c06", "blockindex": 22, "blocktime": 1539226723, "txid": "d79cbf3bcaacbf48e0a10eb7b0e1d3c57215cfea81d1c284d855c04192863934", "walletconflicts": [ ], "time": 1539226723, "timereceived": 1539231228, "bip125-replaceable": "no", "details": [ { "account": "n4eiNFdutY4uRaBkeYFdMtJ6sptvgBc21i", "address": "n4eiNFdutY4uRaBkeYFdMtJ6sptvgBc21i", "category": "receive", "amount": 0.10476174, "label": "n4eiNFdutY4uRaBkeYFdMtJ6sptvgBc21i", "vout": 1 } ], "hex": "020000000001015e81c691192685a06fc26b95b5b4b82538dea246b2c4a1c9506ba78520b9041a00000000171600144c31a4fc1a182aedb403ef36563d801a5f04c8d0feffffff02ba3c8a256900000017a914b2ee27fe15d0d395078be0ad8fd0bb9ccc678c4d878eda9f00000000001976a914fdc298342183b33c5da1387b1361c861fab4bbcf88ac02483045022100c410c05d0f1abd766c9b31b53d6f67fb23145642b6fd70f9d97e4df653b8ffbb022027d1801f348bb82fadf35364e791f07a0d99d56d730fbd618cd465fb9a93b4710121033cbc325fe58a14481e6cae55a4fdd29161067afa156ec4aa78030e732fb967f6d0e71500" } ``` ## 查询进账交易 ```~bitcoin-cli listreceivedbyaddress 0 true``` ## list transaction ``` bash ➜ bitcoin-cli listtransactions ``` ## blockcypher 开发者资料 https://www.blockcypher.com/dev/ ## bitcoind启动项 使用`bitcoind -daemon`就好,不要总是使用`bitcoind -reindex -daemon` 因为reindex会重新获取链上数据: -reindex Rebuild chain state and block index from the blk*.dat files on disk bitcoin-cli stop停止比特币节点 ## Learning-Bitcoin-from-the-Command-Line https://github.com/ChristopherA/Learning-Bitcoin-from-the-Command-Line ## bitcoin from address, no from address concepts https://en.bitcoin.it/wiki/From_address https://bitcoin.stackexchange.com/questions/28182/how-to-find-the-change-sender-address-given-a-txid ###### tags: `区块链` `BTC`