比特币RPC API

    2020-05-04 23:48:46 #比特币RPC API


    BTC在金融危机中诞生,但却将在这个时代进入成熟。今天,BTC减半正式进入一周倒计时,BTC算力暴涨,矿工们陷入狂热状态,纷纷想要在BTC 减半之前获取尽可能多的奖励。同样,作为比特币资产管理的区块链钱包也备受关注。今天,就开发者比较关心的比特币 RPC API话题我们进行一次梳理。

     

    1、listtransactions

    调用返回最近发生的与钱包有关的交易清单。该调用需要节点启用钱包功能。

    参数

    Account:区块链钱包账户名

    Count:需要提取的某些交易数量,默认值为10

    Skip:需要跳过的某些交易数量,默认值为0

    IncludeWatchOnly:是不是包含watch-only地址,默认值为false

     

     返回值

     

    listtransactions调用返回指定数量的交易数组,结构组成成员有:

     

    account:区块链钱包账户名

    address:钱包地址

    category:数字货币交易类别

    amount:金额

    label:数字货币交易备注

    vout:输出序号

    fee:数字货币交易费

    confirmations:确认数量

    trusted:是不是可用,只是对未确认交易有效

    generated:是不是币基交易

    blockhash:区块哈希

    blockindex:区块序号

    blocktime:区块时间戳

    txid:交易id

    walletconflicts:冲突交易

    数组time:交易打包时间戳

    timereceived:收到交易的时间戳

    comment:备注

    to:目标备注

    otheraccount:对端账户

    bip125-replaceable:是否可替换交易

    abandoned:是否废弃交易

     

    输出结果示例:

     

    [

        {

            "involvesWatchonly" : true,

            "account" : "",

            "address" : "1GeDA9rRpqaCdsdkTzGtbajt6jPvn3pg2N",

            "category" : "send",

            "amount" : -3.45902877,

            "vout" : 0,

            "fee" : -0.00032890,

            "confirmations" : 29710,

            "blockhash" : "0000000000000000008b9cb38cd3105e75af94b3af79d0a59cbe4edb618fb814",

            "blockindex" : 1705,

            "blocktime" : 1463173519,

            "txid" : "9b32d4315ac4c5e0d3a5fb947b9a198d3641698badc820643a7df23081f99695e",

            "walletconflicts" : [

            ],

            "time" : 1418695703,

            "timereceived" : 1418925580,

        "bip125-replaceable" : "no",

        "abandoned": false

        }

    ]

     

      


    2、importpreimage

     

    【Hash(散列),把任意长度的输入(又叫做预映射,pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值。】


    3、getnewaddress

     

    调用返回一个用于接收支付的新的BTC 地址,如果调用时指定了 账户,那么该地址接收到的支付将计入该账户。getnewaddress调用需要节点启用钱包支持。


    参数

    Account:新地址所属账户,可选,默认值:""

    AddressType:地址类型,可以是legacy、p2sh-segwit和bech32,可以 使用-addresstype设置默认地址类型

     

    返回值

    getnewaddress调用返回一个新的地址。

    输出结果示例:

    mft61jjkmiEJwJ7Zw3r1h344D6aL1xwhma

     

     

    4、createrawtransaction

     

    createrawtransaction调用创建一个未签名的序列化交易,该交易可以将一个UTXO 转让给指定的P2PKH地址或P2SH地址。该交易不会存储在钱包里,也不会发送到网络 中。


    参数

    Inputs:交易输入数组,每个成员对象的结构如下:

    txid:UTXO的交易id

    vout:UTXO的输出序号

    Sequence:序列号,可选

    Outputs:交易输出对象,键为地址,值为金额

    Locktime:声明该交易生效的最早时间,可选

     

    返回值

    createrawtransaction调用返回生成的未签名交易的序列化字符串。




    5、signrawtransaction

     signrawtransaction调用使用钱包中的私钥对指定的序列化交易字符串进行签名。


    6、sendrawtransaction

    sendrawtransaction调用验证指定交易并将其广播到P2P网络中。


    参数

    Transaction:要签名的交易字符串

    Dependencies:依赖的UTXO详情数组,成员结构如下:

    txid:UTXO所在的交易ID

    vout:UTXO在输出中的序号

    scriptPubKey:输出的公钥脚本

    redeemScript:如果公钥脚本对应于脚本哈希,则本字段指向完整的赎回脚本PrivateKeys:用于签名的私钥数组

    SigHashType:签名哈希类型,可以是:

    ALL

    NONE

    SINGLEALL | ANYONECANPAY

    NONE | ANYONECANPAY

    SINGLE | ANYONECANPAY

     

    返回值

    signrawtransaction调用返回签名后的交易信息,结构如下:

    hex: 签名后的交易序列字符串,16进制表示

    complete:交易是否具备完整签名,false表示还需要更多的签名

     

     

    7、sendtoaddress

     sendtoaddress调用向指定的地址发送指定数量的比特币。该调用 需要节点启用钱包功能。


    参数

    ToAddress:接收地址

    Amount:发送的比特币数量

    Comment:备注文本

    CommentTo:备注接收人

    AutoFeeSubtract:是否自动扣除手续费,默认值:false

     

     

    返回值

    sendtoaddress调用返回交易ID。


    8、createhtlc

     

    This adds support for timelocked contracts which can be used to atomically

    swap hash preimages over the blockchain. It is very similar to the script

    used by the lightning network.


    9、importaddress

     importaddress调用将指定的地址或公钥脚本添加到钱包中,而不包含 其对应的私钥。你可以监听影响该地址或公钥脚本的交易,但由于没有私钥 就不能消费与其相关的UTXO。

    参数

    AddressOrScript:要添加的地址或公钥脚本

    label:(string, optional, default="") An optional label

    Rescan:是否重新扫描整个区块链,一次完整的扫描可能需要几分钟时间

    p2sh:(boolean, optional, default=false) Add the P2SH version of the script as well

     

    Note: This call can take over an hour to complete if rescan is true, during that time, other rpc calls

    may report that the imported address exists but related transactions are still missing, leading to temporarily incorrect/bogus balances and unspent outputs until rescan completes.

    If you have the full public key, you should call importpubkey instead of this.

     

    Note: If you import a non-standard raw script in hex form, outputs sending to it will be treated

    as change, and not show up in many RPCs.


    返回值成功时

     importaddress调用返回null。


    10、getinfo

     

    The ·getinfo· RPC prints various information about the node and the network.

     

    {

         "result": null,

         "error": {

              "code": -32601,

             "message": "getinfo\n\nThis call was removed in version 0.16.0. Use the appropriate fields from:\n- getblockchaininfo: blocks, difficulty, chain\n- getnetworkinfo: version, protocolversion, timeoffset, connections, proxy, relayfee, warnings\n- getwalletinfo: balance, keypoololdest, keypoolsize, paytxfee, unlocked_until, walletversion\n\nbitcoin-cli has the option -getinfo to collect and format these in the old format."

          },

         "id": null

    }

     

     


    11、validateaddress

    validateaddress调用返回指定比特币地址的相关信息。


    参数

    Address:地址

     

    返回值

    validateaddress调用返回关于指定地址的信息,其结构如下:

    isvalid:是否有效

    address:地址

    scriptPubKey:公钥脚本

    ismine:是否属于节点钱包

    iswatchonly:是否watch-only地址

    isscript:是否P2SH地址

    script:P2SH地址的赎回脚本类型,可以是:

    pubkey:P2PK脚本

    pubkeyhash:P2PKH脚本

    multisig:多重签名脚本

    nonstandard:非标脚本

    hex:P2SH地址对应的赎回脚本,仅对P2SH地址有效

    addresses:P2SH脚本使用的地址数组

    sigrequired:P2SH脚本需要的签名数量

    pubkey:地址对应的公钥

    iscompressed:公钥是否压缩格式

    account:关联的钱包账户

    hdkeypath:层级确定性密钥路径

    hdmasterkeyid:层级确定性主密钥id

     

     

     

    输出结果示例:

     

    {

        "isvalid": true,

        "address": "17fshh33qUze2yifiJ2sXgijSMzJ2KNEwu",

        "scriptPubKey": "76a914492ae280d70af33acf0ae7cd329b961e65e9cbd888ac",

        "ismine": true,

        "iswatchonly": false,

        "isscript": false,

        "pubkey": "0312eeb9ae5f14c3cf43cece11134af860c2ef7d775060e3a578ceec888acada31",

        "iscompressed": true,

        "account": "Test"

    }


    对于交易所这种业务型系统,选择钱包的时候需要考虑的问题,应该是安全的同时也需要快速的业务对接。相关业务上难点有:安全、跨链整合、庞大的数据量、还有其他的业务需求比如生成地址、地址管理、资金管理、交易记录管理、充提币及回调等等。以优盾钱包这个全球首款区块链交易所钱包对接开放平台为例,以上的难点都得到了解决,且业务上有主流语言的SDK可实现快速的对接: ①Java版 https://github.com/uduncloud/udun-sdk-java ②.Net版 https://github.com/uduncloud/udun-sdk-csharp ③Php版 https://github.com/uduncloud/udun-sdk-php 就安全而言,提供了Client-Gateway Solution(CGS)、Hareware-Gateway Solution(HGS)两套解决方案。


    相关文章:

    1、区块链钱包之多重签名技术开发

    2、ETH转账交易离线签名教程

    3、EOS区块链钱包开发教程

    4、以太坊 JSON-RPC接口 Java 调用教程

    在線客服

    申請試用

    申請試用

    設置