Проект

Общее

Профиль

Bitcoin

pkg install -y bitcoin-daemon bitcoin-utils
pkg install -y jq gnupg wget curl
#export BITCOIN_DATA_DIR=/blockchain/bitcoin/data
cat > bitcoin.conf.tmp << EOF
#datadir=${BITCOIN_DATA_DIR}
printtoconsole=1
rpcallowip=127.0.0.1
rpcuser=${BITCOIN_RPC_USER:-bitcoin}
rpcpassword=${BITCOIN_RPC_PASSWORD:-$(openssl rand -hex 24)}
testnet=1
prune=1000
[test]
rpcbind=127.0.0.1
rpcport=18332
EOF

Initial Block Download

Once bitcoind process has started, the initial block download will start and you can get the progress as the bitcoin user using the cli:

bitcoin-cli -getinfo

CLI Usage

To get the current block count:

bitcoin-cli getblockcount

2091215

To get some basic info about the first block ever created on the bitcoin blockchain. As the genesis block, it has the index value 0. We can use getblockhas to get the hash value for the first block ever created:

bitcoin-cli getblockhash 0

000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943

We can now use getblock with the hash value to retrieve details about the block:

bitcoin-cli getblock 000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943
{
 “hash”: “000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943”,
 “confirmations”: 2004218,
 “strippedsize”: 285,
 “size”: 285,
 “weight”: 1140,
 “height”: 0,
 “version”: 1,
 “versionHex”: “00000001”,
 “merkleroot”: “4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b”,
 “tx”: [
 “4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b”
 ],
 “time”: 1296688602,
 “mediantime”: 1296688602,
 “nonce”: 414098458,
 “bits”: “1d00ffff”,
 “difficulty”: 1,
 “chainwork”: “0000000000000000000000000000000000000000000000000000000100010001”,
 “nTx”: 1,
 “nextblockhash”: “00000000b873e79784647a6c82962c70d228557d24a747ea4d1b8bbe878e1206”
}
cat ~/.bitcoin/bitcoin.conf | grep -E '(rpcuser|rpcpassword)'
rpcuser=bitcoin
rpcpassword=xxxxxxxxxxxxxx
export bitcoinauth="bitcoin:xxxxxxxxxxxxxx"

Create wallet

curl -u "$bitcoinauth" \
-d '{"jsonrpc": "1.0", "id": "curltest", "method": "createwallet", "params": ["wallet"]}' \
-H 'content-type: text/plain;' http://127.0.0.1:18332/
{"result":{"name":"wallet","warning":""},"error":null,"id":"curltest"}

List wallets

curl -u "$bitcoinauth" \
-d '{"jsonrpc": "1.0", "id": "tutorial", "method": "listwallets", "params": []}' \
 -H 'content-type: text/plain;' http://127.0.0.1:18332/
{"result":["wallet"],"error":null,"id":"tutorial"}

get balances

curl -u "$bitcoinauth" \
-d '{"jsonrpc": "1.0", "id": "tutorial", "method": "getbalances", "params": []}'  \
-H 'content-type: text/plain;' http://127.0.0.1:18332/ | jq .

Get available wallet balance with at least 6 confirmations,:

curl -u "$bitcoinauth" \
-d '{"jsonrpc": "1.0", "id": "tutorial", "method": "getbalance", "params": ["*", 6]}'  \
-H 'content-type: text/plain;' http://127.0.0.1:18332/ | jq .

walletinfo

curl -u "$bitcoinauth" \
-d '{"jsonrpc": "1.0", "id": "tutorial", "method": "getwalletinfo", "params": []}'  \
-H 'content-type: text/plain;' http://127.0.0.1:18332/ | jq .

create new address

curl -u "$bitcoinauth" \
-d '{"jsonrpc": "1.0", "id": "tutorial", "method": "getnewaddress", "params": []}'  \
-H 'content-type: text/plain;' http://127.0.0.1:18332/ | jq .

setlabel

curl -u "$bitcoinauth" \
-d '{"jsonrpc": "1.0", "id": "tutorial", "method": "setlabel", \
"params": ["tb1q8rw5ctkgjenqz9fpwalttqpkk26yhvfzz5nsgp", "green"]}'  \
-H 'content-type: text/plain;' http://127.0.0.1:18332/ | jq .

To backup a wallet, the wallet in this case:

curl -u "$bitcoinauth" \
-d '{"jsonrpc": "1.0", "id": "tutorial", "method": "backupwallet", "params": ["wallet_bak.dat"]}'  \
-H 'content-type: text/plain;' http://127.0.0.1:18332/wallet/wallet | jq .

Listing the transactions over time

curl -u "$bitcoinauth" \
-d '{"jsonrpc": "1.0", "id": "tutorial", "method": "listtransactions", "params": ["*"]}'  \
-H 'content-type: text/plain;' http://127.0.0.1:18332/wallet/wallet | jq .

Sending the amount

see sendtoaddress

curl -u "$bitcoinauth" \
-d '{"jsonrpc": "1.0", "id": "tutorial", "method": "sendtoaddress", \
"params": ["-- TO ADDRESS --", 0.00001, "test comment", "seans outpost"]}'  \
-H 'content-type: text/plain;' http://127.0.0.1:18332/wallet/wallet | jq .

link:

nginx + ssl

upstream bitcoin_testnet {
    server 192.168.0.1:18332;
}

server {
    listen  8.8.8.8:80;
    server_name  blockchain.com;

    location / {
        return 301 https://blockchain.com$request_uri;
    }

    include /usr/local/etc/nginx/include/acme.conf;
}

server {
    server_name blockchain.com;
    listen      8.8.8.8:443 ssl http2;
    set $host_path /usr/local/www/blockchain;
    root $host_path;
    access_log /var/log/nginx/blockchain.access.log;
    error_log /var/log/nginx/blockchain.error.log;
    log_not_found               off;
    client_max_body_size        10m;
    proxy_buffer_size           8k;
    client_body_buffer_size     512k;
    error_page 404              /404.html;
    include /usr/local/etc/nginx/include/ssl.conf;
    gzip on;
    gzip_disable "msie6";
    keepalive_timeout 65;

    location @bitcoin_testnet {
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;
        proxy_read_timeout 300;
        proxy_cache off;
        set $original_uri $uri;
        rewrite /(.*) / break;
        proxy_method POST;
        #proxy_redirect off;
        proxy_set_header Content-Type application/json;
        proxy_pass http://bitcoin_testnet;
    }

    location /testnet {
        try_files $uri @bitcoin_testnet;
    }

}

Electrum Wallet

  • createnewaddress() – генерация нового биткоин-адреса;
  • getbalance() – показ баланса;
  • getconfig('dynamic_fees') – проверка динамической комиссии;
  • getseed() – вывод seed-фразы;
  • history() – история транзакций;
  • importprivkey(privkey) – импорт приватных ключей;
  • is_synchronized() – статус синхронизации;
  • listaddresses() – список адресов биткоина;
  • listrequests() – запрос платежей;
  • make_seed() – создание нового сида (прежний сохраняется);
  • password(opt_old_password=None, opt_new_password=None) – создание нового пароля кошелька;
  • getfeerate() – оптимальная комиссия, которая рассчитывается в реальном времени.

create wallet

bitcoin-cli help createwallet

createwallet "wallet_name" ( disable_private_keys )

Creates and loads a new wallet.

Arguments:

  1. "wallet_name" (string, required) The name for the new wallet. If this is a path, the wallet will be created at the path location.
  2. disable_private_keys (boolean, optional, default: false) Disable the possibility of private keys (only watchonlys are possible in this mode).

We set disable_private_keys to true:

bitcoin-cli createwallet my_wallet true

Next time we run bitcoind we can load the already existing wallet.

bitcoind -wallet=my_wallet -noconnect -printtoconsole=0

or using loadwallet command:

bitcoin-cli loadwallet my_wallet