Ubuntu下USDT私有链搭建

本文作者:陈进坚
个人博客:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
简书:https://www.jianshu.com/u/8ba9ac5706b6
联系方式:[email protected]

安装omnicore程序

请前往https://github.com/OmniLayer/omnicore/tags查看最新版本并替换下面的下载链接

jian@ubuntu:/root# wget https://github.com/OmniLayer/omnicore/releases/download/v0.3.1/omnicore-0.3.1-x86_64-linux-gnu.tar.gz
jian@ubuntu:/root# tar -zvxf omnicore-0.3.1-x86_64-linux-gnu.tar.gz 
jian@ubuntu:/root# sudo install -m 0755 -o root -g root -t /usr/local/bin ./omnicore-0.3.1/bin/*

配置bitcoin参数

jian@ubuntu:~$ mkdir .bitcoin	#创建主目录,如果目录已经存在请跳过
jian@ubuntu:~$ mkdir omnidata	#创建区块数据目录
jian@ubuntu:~$ cd .bitcoin/
jian@ubuntu:~/.bitcoin$ vi bitcoin.conf

将下面信息全部复制,并修改rpcuser(RPC用户名),rpcpassword(RPC用户密码),rpcallowip(允许访问的ip地址)然后保存

# Generated by https://jlopp.github.io/bitcoin-core-config-generator/

# This config should be placed in following path:
# ~/.bitcoin/bitcoin.conf

# [rpc]
# Accept command line and JSON-RPC commands.
server=1
txindex=1
# Username for JSON-RPC connections
rpcuser=omnicorerpc

# Password for JSON-RPC connections
rpcpassword=omnicorerpc

# Listen for JSON-RPC connections on this port
rpcport=17443
# 为了不与比特币端口冲突,使用的不是默认端口所以要加上下面的设置
listen=0

# Allow JSON-RPC connections from specified source. Valid for  are a single IP (e.g. 1.2.3.4), 
# a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24). This option 
# can be specified multiple times.
rpcallowip=192.168.1.178

# 设定默认为私有链
regtest=1

# 区块数据目录
datadir=/root/omnidata

启动omnicore程序

请将-conf-datadir 路径修改为你的路径

jian@ubuntu:~/.omni$ omnicored -conf=/root/.omnicore/bitcoin.conf -daemon
2018-11-22 10:46:09 Loading trades database: OK
2018-11-22 10:46:09 Loading send-to-owners database: OK
2018-11-22 10:46:09 Loading tx meta-info database: OK
2018-11-22 10:46:09 Loading smart property database: OK
2018-11-22 10:46:09 Loading master transactions database: OK
2018-11-22 10:46:09 Loading fee cache database: OK
2018-11-22 10:46:09 Loading fee history database: OK
2018-11-22 10:46:09 Loading persistent state: NONE (no usable previous state found)
2018-11-22 10:46:09 Omni Core initialization completed

出现上面的信息然后按Ctrl + C退出即可,初始化1分钟后再查看是否启动成功,要不然不会输出任何信息,需要输入上一步配置的密码,用户名omnirpc改为你bitcoin.conf中配置的用户名:

jian@ubuntu:~$ curl --user omnicorerpc --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getnetworkinfo", "params": [] }' -H 'content-type: text/plain;' http://127.0.0.1:18332

能返回下面的信息表示启动成功

{"result":{"version":130200,"subversion":"/Satoshi:0.13.2/","protocolversion":70015,"localservices":"000000000000000d","localrelay":true,"timeoffset":0,"connections":2,"networks":[{"name":"ipv4","limited":false,"reachable":true,"proxy":"","proxy_randomize_credentials":false},{"name":"ipv6","limited":false,"reachable":true,"proxy":"","proxy_randomize_credentials":false},{"name":"onion","limited":true,"reachable":false,"proxy":"","proxy_randomize_credentials":false}],"relayfee":0.00001000,"localaddresses":[],"warnings":""},"error":null,"id":"curltest"}

停止omnicore程序

jian@ubuntu:~/.omni$ omnicore-cli -conf=/root/.omnicore/bitcoin.conf stop

创建钱包地址

jian@ubuntu:~$ curl --user omnirpc --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getnewaddress", "params": ["test"] }' -H 'content-type: text/plain;' http://127.0.0.1:18332/

{"result":"n1Axxy3WGwFci1GJffwdY55dRem92GKXAm","error":null,"id":"curltest"}

返回结果中的 “n1Axxy3WGwFci1GJffwdY55dRem92GKXAm” 即为新创建的USDT钱包,以m开头表示是测试链的钱包地址,公链上的钱包地址一般是1开头

查询余额

查询比特币余额

jian@ubuntu:~$ curl --user omnicorerpc --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getbalance", "params": [] }' -H 'content-type: text/plain;' http://127.0.0.1:18332/

查询USDT余额,请将钱包地址换成你的

jian@ubuntu:~$ omnicore-cli -conf=/root/.omnicore/bitcoin.conf omni_getbalance "n1Axxy3WGwFci1GJffwdY55dRem92GKXAm" 1

或者

jian@ubuntu:~$ curl --user omnicorerpc --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "omni_getbalance", "params": ["n1Axxy3WGwFci1GJffwdY55dRem92GKXAm", 1] }' -H 'content-type: text/plain;' http://127.0.0.1:18332/

获取测试币

在测试网络testnet和私有网络regtest下获取测试币的方式是一样的,私有链使用生成区块命令获得比特币后用下面的命令往moneyqMan7uh8FqdCA2BV5yZ8qVrc9ikLP发送比特币可以得到一点测试的USDT,每1BTC可以获得100个USDT。为了方便让测试币进入我们指定的钱包地址,我们另外生成一个指定账号的钱包地址

jian@ubuntu:~$ omnicore-cli -conf=/root/.omnicore/bitcoin.conf getnewaddress "omni"
n2zPfoSavpGGxZwJf3pya3mXX5HVEG6wmd

执行得到钱包地址n2zPfoSavpGGxZwJf3pya3mXX5HVEG6wmd,然后往这个钱包地址转入比特币

jian@ubuntu:~$ omnicore-cli -conf=/root/.omnicore/bitcoin.conf sendtoaddress "n2zPfoSavpGGxZwJf3pya3mXX5HVEG6wmd" 10

生成一个区块让币到账

jian@ubuntu:~$ omnicore-cli -conf=/root/.omnicore/bitcoin.conf generate 1

用下面的命令查看omni账户的余额可以看到余额已经在该账户所属的n2zPfoSavpGGxZwJf3pya3mXX5HVEG6wmd地址中

jian@ubuntu:~$ omnicore-cli -conf=/root/.omnicore/bitcoin.conf listreceivedbyaddress 1 true

[
 {
    "address": "n2zPfoSavpGGxZwJf3pya3mXX5HVEG6wmd",
    "account": "omni",
    "amount": 10.00000000,
    "confirmations": 1,
    "label": "omni",
    "txids": [
      "c9da93e8a0daad57b7b4702b3ceac3d4dd51cbb563c7e982712c573eaab95715"
    ]
  }
]

然后用omni账户往moneyqMan7uh8FqdCA2BV5yZ8qVrc9ikLP地址转一个比特币

jian@ubuntu:~$ omnicore-cli -conf=/root/.omnicore/bitcoin.conf sendfrom "omni" "moneyqMan7uh8FqdCA2BV5yZ8qVrc9ikLP" 1

再次生成一个区块让币到账

jian@ubuntu:~$ omnicore-cli -conf=/root/.omnicore/bitcoin.conf generate 1

查看余额,可以看到100个USDT已经到账

jian@ubuntu:~$ omnicore-cli -conf=/root/.omnicore/bitcoin.conf "omni_getbalance" "n2zPfoSavpGGxZwJf3pya3mXX5HVEG6wmd" 1
{
  "balance": "100.00000000",
  "reserved": "0.00000000",
  "frozen": "0.00000000"
}

注意:如果你的区块高度没有到达100个以上,你将得不到任何比特币和USDT

使用下面的命令可以查看所有余额大于0的币种和余额

jian@ubuntu:~$ omnicore-cli -conf=/root/.omnicore/bitcoin.conf omni_getwalletaddressbalances

[
  {
    "address": "n2zPfoSavpGGxZwJf3pya3mXX5HVEG6wmd",
    "balances": [
      {
        "propertyid": 1,
        "name": "Omni",
        "balance": "100.00000000",
        "reserved": "0.00000000",
        "frozen": "0.00000000"
      }, 
      {
        "propertyid": 2,
        "name": "Test Omni",
        "balance": "99.00000000",
        "reserved": "0.00000000",
        "frozen": "0.00000000"
      }
    ]
  }
]

可以看到在私有网络下发送比特币会给你返回两种测试币,propertyid分别是1和2,但是在主链,USDT的propertyid分别是31

发送交易

参数分别是from,to,代币id,交易数量,支付btc手续费的地址

jian@ubuntu:~$ omnicore-cli "omni_funded_send" "n1Axxy3WGwFci1GJffwdY55dRem92GKXAm" "n4VTWrjPV1QaLmM1x8ZtyLiJoFk3KaCAsi" 1 "1" "n1Axxy3WGwFci1GJffwdY55dRem92GKXAm"

如果出现报错Error with selected inputs for the send transaction,说明比特币或者USDT余额不足,往钱包转入足够的币即可。

访问JSON-RPC

可以查看:https://github.com/OmniLayer/omnicore/blob/master/src/omnicore/doc/rpc-api.md,需要用编程语言对接的话可以使用api中的函数和参数拼装成json数据,例如本文中的查询余额的 json 为{"jsonrpc": "1.0", "id":"curltest", "method": "getbalance", "params": [] },然后将这串json作为参数post提交到节点的ip和端口即可返回相应的json结果。

你可能感兴趣的:(区块链)