Bigzhu's den

bitcoin

bigzhu

install

要先安装 Bitcoin Core https://bitcoin.org/zh_CN/download

路径

默认使用路径 ~/Library/Application Support/Bitcoin/

启动一次后关了, 会看到 Bitcoin 里生成了很多文件

bitcoin.conf

bitcoin.conf 是配置文件, 放在这个目录下, 要自已去创件

testnet

testnet 是为了开发建立的测试网络

testnet=1 加入到 bitcoin.conf 里

命令行

bitcoin-qt 有一个调试窗口

也可以按装 bitcoin-cli: sudo npm install -g bitcoin-cli

bitcoin-cli 要打开 rpc 服务

打开 rpc 服务

server=1
rpcuser=bigzhu
rpcpassword=the_password

加入 bitcoin.conf

bitcoin-cli

用 bitcoin-qt 的控制台还更方便一些

命令

https://bitcoin.org/en/developer-reference#abandontransaction

可以看说明

多重签名

getnewaddress 拿到一个地址 mq6t3Yydf16ucXGE3LFW8Z11zVBWYCnZzQ 把这个地址当做管理者1的地址

validateaddress mq6t3Yydf16ucXGE3LFW8Z11zVBWYCnZzQ 得到:

{
  "isvalid": true,
  "address": "mq6t3Yydf16ucXGE3LFW8Z11zVBWYCnZzQ",
  "scriptPubKey": "76a9146923ba1d16614652181323a8298b009eba50eb2288ac",
  "ismine": true,
  "iswatchonly": false,
  "isscript": false,
  "pubkey": "03a1ab793b9970346c8d4154b5917ac5c5aa026e064a10c5b360eb21780dd3ad02",
  "iscompressed": true,
  "account": "",
  "timestamp": 1512456587,
  "hdkeypath": "m/0'/0'/6'",
  "hdmasterkeyid": "5a7dbd581dbae4c5526a56729016b3f65380904d"
}

pubkey 是对应公钥

按这个操作3次, 拿到3个公钥 * mq6t3Yydf16ucXGE3LFW8Z11zVBWYCnZzQ 03a1ab793b9970346c8d4154b5917ac5c5aa026e064a10c5b360eb21780dd3ad02 * mkBvyDfpKfXdy1KA2ye9kSaFhZbaKGi6ck 03fe639fea58cecb658dea75e154f620fd27dcbfcc10bdab0b5f774ab6bec2b308 * mtFgoExrt9mKe4aLTghavuCX3fxJnLzvzi 0341be3a1e78a322da8b1314854a6d9fb1e067e6dc931cddfcc97bdbded66f0b34

createmultisig 是用来生成多重签名的, 填入公钥列表:

createmultisig 2 '["03a1ab793b9970346c8d4154b5917ac5c5aa026e064a10c5b360eb21780dd3ad02", "03fe639fea58cecb658dea75e154f620fd27dcbfcc10bdab0b5f774ab6bec2b308", "0341be3a1e78a322da8b1314854a6d9fb1e067e6dc931cddfcc97bdbded66f0b34"]'

返回

{
  "address": "2MvXnpbHrNNoZVtND63K9qBqQRjmre1cydq",
  "redeemScript": "522103a1ab793b9970346c8d4154b5917ac5c5aa026e064a10c5b360eb21780dd3ad022103fe639fea58cecb658dea75e154f620fd27dcbfcc10bdab0b5f774ab6bec2b308210341be3a1e78a322da8b1314854a6d9fb1e067e6dc931cddfcc97bdbded66f0b3453ae"
}

返回的 address 就是生成的多重签名地址

添加到钱包里

addmultisigaddress 2 '["03a1ab793b9970346c8d4154b5917ac5c5aa026e064a10c5b360eb21780dd3ad02", "03fe639fea58cecb658dea75e154f620fd27dcbfcc10bdab0b5f774ab6bec2b308", "0341be3a1e78a322da8b1314854a6d9fb1e067e6dc931cddfcc97bdbded66f0b34"]'

同样会返回那一个多重签名地址: 2MvXnpbHrNNoZVtND63K9qBqQRjmre1cydq

向这个地址付款, 那么钱就进入多重签名的这个地址了.

用多重签名地址付款

付款要用最少2个人的私钥签名, 前面不是用3个address 生成公钥了么, 生成私钥也要用到 address

dumpprivkey mq6t3Yydf16ucXGE3LFW8Z11zVBWYCnZzQ

类似这样, 就拿到了私钥

用 listunspent 取交易信息, 应该在这里指定和过滤, 一般来说, 如果钱不是在多重签名的这个地址下的话, 不要纳入考虑

比如有一条交易:

{
    "txid": "8272cc20930467a714a2de2444d073faf15c038e6df3204f49bfb2ea2d540328",
    "vout": 0,
    "address": "2MvXnpbHrNNoZVtND63K9qBqQRjmre1cydq",
    "account": "",
    "redeemScript": "522103a1ab793b9970346c8d4154b5917ac5c5aa026e064a10c5b360eb21780dd3ad022103fe639fea58cecb658dea75e154f620fd27dcbfcc10bdab0b5f774ab6bec2b308210341be3a1e78a322da8b1314854a6d9fb1e067e6dc931cddfcc97bdbded66f0b3453ae",
    "scriptPubKey": "a914240a70b5123ef31640d067d0b42bc971a0c06fef87",
    "amount": 0.00031737,
    "confirmations": 2,
    "spendable": true,
    "solvable": true,
    "safe": true
}

由这条有效交易信息来构建一个新的交易单:

vout 的说明可以看这里: https://xiaofandh12.github.io/Bitcoin

vout 指有没有交易输出

createrawtransaction '''
  [
    {
      "txid": "8272cc20930467a714a2de2444d073faf15c038e6df3204f49bfb2ea2d540328",
      "vout" : 0,
      "scriptPubKey": "a914240a70b5123ef31640d067d0b42bc971a0c06fef87",
      "redeemScript": "522103a1ab793b9970346c8d4154b5917ac5c5aa026e064a10c5b360eb21780dd3ad022103fe639fea58cecb658dea75e154f620fd27dcbfcc10bdab0b5f774ab6bec2b308210341be3a1e78a322da8b1314854a6d9fb1e067e6dc931cddfcc97bdbded66f0b3453ae"
    }
  ]''' '{ "2N26fmeNECuRzebEsTcsQUzsapeEBCN84Yc": 0.0003 , "mq6t3Yydf16ucXGE3LFW8Z11zVBWYCnZzQ":  0.00000737}'

第一个参数为交易列表, 第二个为一个 map, 转到哪里: 多少钱, 找零到哪里: 多少钱

中间要有一部分差值, 做为手续费

createrawtransaction 得到一个串:

02000000012803542deab2bf494f20f36d8e035cf1fa73d04424dea214a767049320cc72820000000000ffffffff02307500000000000017a914611ae902f14f4d1c88a0f06bbb9c6b3c1091fdeb87e1020000000000001976a9146923ba1d16614652181323a8298b009eba50eb2288ac00000000

用 signrawtransaction 来做签名:

signrawtransaction "02000000012803542deab2bf494f20f36d8e035cf1fa73d04424dea214a767049320cc72820000000000ffffffff02307500000000000017a914611ae902f14f4d1c88a0f06bbb9c6b3c1091fdeb87e1020000000000001976a9146923ba1d16614652181323a8298b009eba50eb2288ac00000000" '[
    {
      "txid": "8272cc20930467a714a2de2444d073faf15c038e6df3204f49bfb2ea2d540328",
      "vout" : 0,
      "scriptPubKey": "a914240a70b5123ef31640d067d0b42bc971a0c06fef87",
      "redeemScript": "522103a1ab793b9970346c8d4154b5917ac5c5aa026e064a10c5b360eb21780dd3ad022103fe639fea58cecb658dea75e154f620fd27dcbfcc10bdab0b5f774ab6bec2b308210341be3a1e78a322da8b1314854a6d9fb1e067e6dc931cddfcc97bdbded66f0b3453ae"
    }
  ]' '["cPqaHZK7hWgVXSTDUYxeX4xuJK7Y964P3ZRKaPt4XD8LsJJTtkc6", "cQjR52L8KcERK1DGHmn7stEdBT4KXgXr4rN4jZHE97Kdj1EJNnxo"]'

第一个参数是createrawtransaction返回的串, 第二个要把交易列表再传一次, 第三个就是至少2个私钥

得到返回

{
  "hex": "02000000012803542deab2bf494f20f36d8e035cf1fa73d04424dea214a767049320cc728200000000fdfe0000483045022100d742dea426879466bf3a5afa1c6bb9731793e298cfce9d716b0d71a92a27bda90220776c74fc801ce9490b50e7d37a58fd843631040666ceb0f1732e78b74694e93c01483045022100d2e8a2e972bdb25149ecc83213aa3d300928ff72a3fe570f522eac0cf4511f2502205797f3a864c97059edba0031b49b45e71f8abe41a8c3c7a69da6ed82dfd6deda014c69522103a1ab793b9970346c8d4154b5917ac5c5aa026e064a10c5b360eb21780dd3ad022103fe639fea58cecb658dea75e154f620fd27dcbfcc10bdab0b5f774ab6bec2b308210341be3a1e78a322da8b1314854a6d9fb1e067e6dc931cddfcc97bdbded66f0b3453aeffffffff02307500000000000017a914611ae902f14f4d1c88a0f06bbb9c6b3c1091fdeb87e1020000000000001976a9146923ba1d16614652181323a8298b009eba50eb2288ac00000000",
  "complete": true
}

complete 表示签名完成

用 sendrawtransaction 把 hex 发出去, 完成交易

sendrawtransaction "02000000012803542deab2bf494f20f36d8e035cf1fa73d04424dea214a767049320cc728200000000fdfe0000483045022100d742dea426879466bf3a5afa1c6bb9731793e298cfce9d716b0d71a92a27bda90220776c74fc801ce9490b50e7d37a58fd843631040666ceb0f1732e78b74694e93c01483045022100d2e8a2e972bdb25149ecc83213aa3d300928ff72a3fe570f522eac0cf4511f2502205797f3a864c97059edba0031b49b45e71f8abe41a8c3c7a69da6ed82dfd6deda014c69522103a1ab793b9970346c8d4154b5917ac5c5aa026e064a10c5b360eb21780dd3ad022103fe639fea58cecb658dea75e154f620fd27dcbfcc10bdab0b5f774ab6bec2b308210341be3a1e78a322da8b1314854a6d9fb1e067e6dc931cddfcc97bdbded66f0b3453aeffffffff02307500000000000017a914611ae902f14f4d1c88a0f06bbb9c6b3c1091fdeb87e1020000000000001976a9146923ba1d16614652181323a8298b009eba50eb2288ac00000000"

反回了一个串 ae8f469e2540c88c23b77a615dc461f71d1a7af5b952bb985d0338f2f3da2ecd

bigzhu
Everything is learnable