区块链基础 - 比特币

1、搭建私链,能够挖矿。

# cat /etc/bitcoin/bitcoin.conf
rpcuser=bitcoinrpc
rpcpassword=12345
maxconnections=1
regtest=1
datadir=/data/bitcoin
fallbackfee=0.000001
# bitcoind -conf=/etc/bitcoin/bitcoin.conf

2、掌握节点api。怎么查询最新高度,区块详情,交易详情。

查询最新高度

# bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf -rpcwallet=test1 getdifficulty   挖坑难度
# bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf -rpcwallet=test1  getblockcount  查询最新高度

查询区块详情

# bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf -rpcwallet=test1 getnewaddress test2
bcrt1q7hpsknkqyh4m8jqcl2t52x5wn8klhm03f6nnht
root@vultr:~#
root@vultr:~# bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf -rpcwallet=test1 generatetoaddress 11 "bcrt1q7hpsknkqyh4m8jqcl2t52x5wn8klhm03f6nnht"
[
  "17324140909ec40a44313e5a6ee41b0f6131016528ea1ff2d20ecb905e8b9c52",
  "755e0e405404b279492df7fb092ced8ba5e86b26dd29e1e3ad4a11262b305baf",
  "04ba1b23aff993daefd488a9166ce3e018dfc4ccc776038c8b9454561310f51b",
  "2ee0208500d4728af8ca6fcc4661fc765756d6712764fe4bd35023d877171305",
  "2d91a89bf9af919fac403dbd932ac61304fcfa05c4c0b5736a189e83a3e962cb",
  "5c1e0682d0db47a248d08db7b6e6d24484521bfddeb775c0c2c1195f62b1c749",
  "6589049ca69ed37575fdf8ad29dac6d57694acc78d7cb81b2d9e44ebf5c05bb7",
  "083f1bf74b23de3b1440bd1f1b4c9fbf68d229f71d5043a6476938ee3be3bd3a",
  "42f01de172eeacbdea8d74a0ae3fe56764e2ea7b42b3e72d879211478fdb0676",
  "4abc4eadc98d0b0d6bcbe67249ac3bc5c70744d3160c554fe28e71856efe5824",
  "3d5214aa36c0093e7de1b0aa7dc09ef4187b8adf3c59e3ffd3dac8a770232a61"
]
# bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf -rpcwallet=test1 getblock 17324140909ec40a44313e5a6ee41b0f6131016528ea1ff2d20ecb905e8b9c52

转账

bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf  -rpcwallet=test2 sendtoaddress bcrt1q8lpak6qy7utddhhkg429e5ssvd6ce0406jfq06 10.0
bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf  -rpcwallet=test2 getbalance 

查询交易

bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf -rpcwallet=test1  getrawtransaction "78111018919acfc817f6c32b7f47d316254b2e0b063fbd013cf252a11b990629"
bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf -rpcwallet=test1  decoderawtransaction "0200000000010129c8422ea7b73248ea874776e3123ad16a975367214555754f1ce9cd152f7be10000000000feffffff0273276bee00000000160014d7287b7ae63090f85a1dc6c38eeb790459742f8b00ca9a3b00000000160014d0d7b628576e034e5e63824a5c110af94a97a39e02473044022075071a978865a15ef5ee005a369fa077541a3285dadd7f9961bb56364efdc48c0220656b2cd3513d54d189ffc74287643b0f4e10a54ce0463ff36c1e5c6cc471591d0121024aff2008f342230b7c63f119ec80e3c08408656d4e09c447f3a2285e7fa942440d000000"
{
  "txid": "78111018919acfc817f6c32b7f47d316254b2e0b063fbd013cf252a11b990629",  交易id
  "hash": "3bd203bd0fc8cf020ad7f8c2ff57462053220b73169d9f3b67c080f40896c3ad",  交易哈希
  "version": 2,  交易版本
  "size": 222,   交易大小
  "vsize": 141,  虚拟交易大小
  "weight": 561, 交易权重      
  "locktime": 13,  锁定时间
  "vin": [
    {
      "txid": "e17b2f15cde91c4f755545216753976ad13a12e3764787ea4832b7a72e42c829",  交易索引
      "vout": 0, 输出序号
      "scriptSig": { 脚本
        "asm": "", 脚本公钥
        "hex": ""  
      },
      "txinwitness": [
        "3044022075071a978865a15ef5ee005a369fa077541a3285dadd7f9961bb56364efdc48c0220656b2cd3513d54d189ffc74287643b0f4e10a54ce0463ff36c1e5c6cc471591d01",  16进制编码见证数据
        "024aff2008f342230b7c63f119ec80e3c08408656d4e09c447f3a2285e7fa94244"
      ],
      "sequence": 4294967294  脚本序列号
    }
  ],
  "vout": [
    {
      "value": 39.99999859,   TBC数量
      "n": 0,   索引
      "scriptPubKey": {
        "asm": "0 d7287b7ae63090f85a1dc6c38eeb790459742f8b",
        "hex": "0014d7287b7ae63090f85a1dc6c38eeb790459742f8b",
        "reqSigs": 1, 
        "type": "witness_v0_keyhash", 类型
        "addresses": [
          "bcrt1q6u58k7hxxzg0sksacmpca6meq3vhgtutxa9n2q"  比特币地址
        ]
      }
    },
    {
      "value": 10.00000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "0 d0d7b628576e034e5e63824a5c110af94a97a39e",
        "hex": "0014d0d7b628576e034e5e63824a5c110af94a97a39e",
        "reqSigs": 1,
        "type": "witness_v0_keyhash",
        "addresses": [
          "bcrt1q6rtmv2zhdcp5uhnrsf99cyg2l99f0gu72e9eu2"
        ]
      }
    }
  ]
}

 

载入钱包

~# bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf  loadwallet test1
~# bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf  -rpcwallet=test2 gettransaction 8fcc2aab2bc7a75a4d716b97658e152bf11061320615e68ac35eb13ab8cfad26

获取钱包地址

bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf -rpcwallet="test2"  getaddressesbylabel test2

获取未使用的转账

未使用的转账,这个概念是UTXO模型造成的。

bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf -rpcwallet="test2"  listunspent 1 99999999 '''["bcrt1q9r5us2eg3mkxf5skmkp2sfxathhjemg73kseud"]'''

生成地址

bitcoin-cli -conf=/etc/bitcoin/bitcoin.conf -rpcwallet=test1 getnewaddress

5、使用任何编程语言,完成生成地址、构造交易、离线签名、广播并上链。不准使用钱包功能。

from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
import logging
import json
logging.basicConfig()
# logging.getLogger("BitcoinRPC").setLevel(logging.DEBUG)
# rpc_user and rpc_password are set in the bitcoin.conf file
rpc_connection_wallet2 = AuthServiceProxy("http://%s:%s@127.0.0.1:18443/wallet/test2"%("bitcoinrpc", "12345"))
# print(rpc_connection_wallet2.listunspent())
#
a = rpc_connection_wallet2.createrawtransaction(
    [{
        "txid":"a03deed9352436f7c2bd21322c5b868f0676b032545dcfd6ce91759459acdfff", #找出一笔收入
        "vout":0,
    }],
    [
        {
            "bcrt1qmzpnlfzuukcmqmav4jnp8z2dm0cv3n87v3j0eh":49.999 #地址和金额
        }
    ]
)
# # # 0200000001ffdfac59947591ced6cf5d5432b076068f865b2c3221bdc2f7362435d9ee3da00000000000ffffffff0100ca9a3b00000000160014d8833fa45ce5b1b06facaca613894ddbf0c8ccfe00000000
# # print(a)
# #
# # # 公钥bcrt1q9r5us2eg3mkxf5skmkp2sfxathhjemg73kseud
# # # 私钥cTxgv9551mPCPzfmFUbyDD3PkWHR3sZt7wAzSx6wC617y8BMFi5X
b = rpc_connection_wallet2.signrawtransactionwithkey(
    a,
    ["cTxgv9551mPCPzfmFUbyDD3PkWHR3sZt7wAzSx6wC617y8BMFi5X"]
)
print(b)

res = rpc_connection_wallet2.sendrawtransaction(
    b["hex"]
)
print(res)

 

posted @ 2020-10-27 16:56  Redheat  阅读(466)  评论(0编辑  收藏  举报