Home  >  Article  >  Backend Development  >  Introduction to USDT PHP development package OmniTool

Introduction to USDT PHP development package OmniTool

步履不停
步履不停Original
2019-06-06 13:29:443761browse

Introduction to USDT PHP development package OmniTool

The OmniTool development kit is suitable for quickly adding support for Omni Layer/USDT digital assets to PHP applications. It supports application scenarios using its own Omni Layer nodes and third-party based ones. Lightweight deployment scenarios for API services and offline naked transactions.

1. Introduction to OmniTool development kit

The OmniTool development kit mainly includes the following features:

The complete Omni Layer node RPC encapsulation supports the use of own nodes or third-party services to obtain specified The utxo collection of addresses supports offline generation of omni token transfers. Naked transactions support the use of own nodes or third-party services to broadcast naked transactions.

OmniTool supports locally deployed Omnicored nodes and also supports blockchain.info, btc.com, etc. With the open API, it is very simple to add support for other third-party services. You only need to refer to the code to implement the following interface:

UtxoCollectorInterface: utxo collector UtxoSelectorInterface: utxo filter BroadcasterInterface: naked transaction broadcaster ExplorerInterface: data Query interface

The OmniTool software package runs in the **Php 7.1 ** environment, the current version is 1.0.0, the main classes/interfaces and relationships are as shown in the figure below:

Introduction to USDT PHP development package OmniTool

2. Instructions for using the RpcClient class

The RpcClient class encapsulates the RPC interface protocol of Omni Layer. When creating an RpcClient object, you need to pass in the node RPC URL containing valid identity information. For example, assume that the omnicored node software installed on this machine is configured as follows:

rpcuser: userrpcpassword: 123456rpcport: 8332

Then you can use the following code to instantiate RpcClient:

use \OmniTool\RpcClient;

$client = new RpcClient(            'http://user:123456@localhost:8332'   /*节点RPC接口的URL*/
          );

Omni Core node has expanded additional interfaces in addition to Bitcoin's original RPC interface to operate Omni layer data. These extended RPC interfaces use the omni_ prefix to distinguish them from Bitcoin's original RPC interface. In order to facilitate the separation of RPC calls at these two layers, RpcClient introduces the concept of protocol submodule, and hooks Bitcoin's original RPC interface and Omni's extended RPC interface to the btc submodule and omni submodule respectively.

For example, to obtain the USDT token balance of a certain address, you need to use the omni_getbalance call of the Omni layer. This RPC call corresponds to the getBalance() method of the omni submodule of the RpcClient instance. The following code obtains the USDT (asset ID: 31) balance of the address 1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P:

$ret = $client->omni->getBalance(          '1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P',   /*地址*/
          31                                      /*资产ID:USDT*/
       );

Similarly, you can use the omni_send call to perform a simple USDT transfer. This call corresponds to the send( of the omni submodule of the RpcClient instance )method. The following code transfers 100.0 USDT tokens from the address 3M9qvHKtgARhqcMtM5cRT9VaiDJ5PSfQGY to the address 37FaKponF7zqoMLUjEiko25pDiuVH5YLEa:

$ret = $client->omni->send(          '3M9qvHKtgARhqcMtM5cRT9VaiDJ5PSfQGY',    /*代币转出地址*/
          '37FaKponF7zqoMLUjEiko25pDiuVH5YLEa',    /*代币转入地址*/
          31,                                      /*代币ID:USDT*/
          "100.00"                                 /*转移的代币数量*/
       );

The RPC interface of the original bitoin layer can be accessed through the btc submodule of RpcClient. For example, use the listunspent call to obtain the utxo of the specified address in the local node:

$ret = $client->btc->listUnspent(          6,                                        /*最小确认数*/
          999999,                                   /*最大确认数*/
          ['mgnucj8nYqdrPFh2JfZSB1NmUThUGnmsqe']    /*地址清单*/  
       );

The demo/rpc-demo.php sample code in the development package uses the RpcClient class to fully demonstrate the token issuance and transfer in the Omni layer. Function, if you plan to build your own Omni Core node, I believe this example will be of great help.

3. Wallet class usage instructions

If you are not willing to build your own Omni Core node, but want to add support for Omni Layer/USDT to your PHP application based on third-party APIs, then The simplest method is to use the entry class Wallet for offline transactions.

The main function of the Wallet class is to create and broadcast Omni token transfer naked transactions or Bitcoin transfer naked transactions. Its basic usage steps are as follows:

使用Wallet::cloud()静态方法创建一个支持云端API服务的Wallet实例使用addKey()方法将必要的私钥加入该Wallet实例,例如转出地址的私钥,因为Wallet需要利用私钥对裸交易进行签名使用omniSendTx()方法生成Omni代币转账裸交易,或者使用btcSendTx()方法比特币转账裸交易使用broadcast()方法广播裸交易

3.1 Omni代币转账

使用Wallet实现的Omni代币转账示例代码如下,说明见注释:

addKey($prvKey);                         /*将私钥加入钱包,只需加入一次*/$addressList = $wallet->getAddressList();         /*返回钱包管理的所有地址,数组*/$rawtx = $wallet->omniSendTx(
            $addressList[0],                      /*发送方地址,私钥必须已经加入钱包*/
            'mgYPLmNuZymK...e2XUNF6VFnT',         /*接收方地址*/
            2,                                    /*转账OMNI代币ID,2:TOMN*/
            '0.000001'                            /*转账OMNI代币数量*/
         );

$ret = $wallet->broadcast($rawtx);                /*广播OMNI裸交易*/var_dump($ret);

注意:

Wallet实例利用钱包中的私钥生成地址列表,并利用这些地址从第三方服务获取utxo信息。 因此需要钱包中 的私钥对应地址在链上有utxo存在,Wallet对象才能够成功构造裸交易。转账目标地址应当与创建Wallet对象时指定的链ID一致,例如mainnet的p2pkh地址,前缀应当为1

3.2 指定Omni交易的手续费支付地址

在Omni协议层不需要支付交易手续费,但是Omni交易所嵌入的比特币交易依然需要支付手续费。默认情况下omniSendTx()方法使用发送方地址支付比特币交易手续费,但可以传入额外的参数来指定其他地址支付交易手续费,当你的PHP应用需要实现多账户归集功能时,使用统一的手续费支付地址会更容易管理一些。

例如,下面的代码使用地址mnRo8JyTHDd5NxRb3UvGbAhCBPQTQ4UZ8W支付omni交易的手续费:

$rawtx = $wallet->omniSendTx(
            $addressList[0],                      /*发送方地址,私钥必须已经加入钱包*/
            'mgYPLmNuZymK...e2XUNF6VFnT',         /*接收方地址*/
            2,                                    /*转账OMNI代币ID,2:TOMN*/
            '0.000001',                           /*转账OMNI代币数量*/
            'mnRo8JyTHDd5...CBPQTQ4UZ8W'          /*交易手续费支付地址*/
         );

注意:

即使指定了余额充足的手续费支付地址,Omni交易的发送方依然必须有微量的比特币 余额(546 SATOSHI),因为Omni协议需要交易发送方至少有一个可用UTXO。手续费支付地址同时也是找零地址,多余的比特币将返回至该地址

3.3 指定Omni交易的比特币转账数量

由于Omni交易要求发送方必须有可用的UTXO,因此为了便于接收Omni代币的地址可以继续流通所持有的Omni代币,omniSendTx()方法在默认情况下将向接收方地址转入微量的比特币(546 SATOSHI),可以在调用该方法时修改这个默认数值。

例如,下面的代码转入接收方1000个SATOSHI:

$rawtx = $wallet->omniSendTx(
            $addressList[0],                      /*发送方地址,私钥必须已经加入钱包*/
            'mgYPLmNuZymK...e2XUNF6VFnT',         /*接收方地址
            2,                                    /*转账OMNI代币ID,2:TOMN*/
            '0.000001',                           /*转账OMNI代币数量*/
            'mnRo8JyTHDd5...CBPQTQ4UZ8W',         /*交易手续费支付地址*/
            1000                                  /*转账比特币数量,单位:SATOSHI*/
         );

3.4 比特币转账

OmniTool也支持比特币转账裸交易的生成与广播。

例如,下面的代码从钱包的第一个地址向指定接受地址转入1000个SATOSHI:

getAddressList();

$rawtx = $wallet->btcSendTx(
                    $addressList[0],                /*发送方地址*/
                    'moneyqMan7u...8qVrc9ikLP',     /*接收方地址*/
                    1000,                           /*转账比特币数量,单位:SATOSHI*/
                    500                             /*手续费,单位:SATOSHI*/
                  );                       
echo 'btc rawtx => ' . $rawtx . PHP_EOL;

$ret = $wallet->broadcast($rawtx);                  /*广播裸交易*/

默认情况下,btcSendTx()使用发送方地址作为找零地址,也可以在调用时指定其他地址作为找零地址,例如,下面的代码创建一个新地址接收找零:

$changeAddress = $wallet->getNewAddress();          /*创建新地址*/$rawtx = $wallet->btcSendTx(
                    $addressList[0],                /*发送方地址*/
                    'moneyqMan7u...8qVrc9ikLP',     /*接收方地址*/
                    1000,                           /*转账比特币数量,单位:SATOSHI*/
                    500,                            /*手续费,单位:SATOSHI*/
                    $changeAddress                  /*找零地址*/
                  );

4、UTXO收集器

OmniTool使用接口UtxoCollectorInterface来约定UTXO的收集功能。该接口的实现需要支持获取指定地址的候选UTXO集合,可指定多个地址。

接口方法:

collect($addressList):提取并返回候选UTXO集合

参数$addressList用来声明要收集UTXO的地址清单,类型为数组。

当前实现类:

CloudUtxoCollector:基于blockchain.com的开放API实现的Utxo收集器LocalUtxoCollector:基于omnicored节点RPC API实现的Utxo收集器

例如,下面的代码使用CloudUtxoCollector获取地址mi8BvbK73nDQfaN3acpaFGYQKhfQ5ysKRn的UTXO:

use OmniTool\CloudUtxoCollector;

$collector = new CloudUtxoCollector(                    'testnet'                       /*测试网*/
                 );
$candidateBag = $collector->collect(
                    ['mi8BvbK73nDQ...KhfQ5ysKRn']   /*地址清单*/
                );

5、UTXO筛选器

OmniTool使用UtxoSelectorInterface来约定UTXO筛选功能。该接口的实现需要根据目标金额从候选UTXO中选择可用UTXO,并返回新的UtxoBag实例。

接口方法:

select($target,$candidates):选择可消费UTXO,返回UtxoBag对象

参数$target声明要达成的最低金额目标,单位:wei。

参数$candidates是候选的utxo集合,通常是UtxoCollectorInterface实现对象的collect()调用返回的UtxoBag对象。

当前实现类:

DefaultUtxoSelector

例如下面的代码使用DefaultUtxoSelector实例从候选UTXO中删选出至少100000 wei 的UTXO:

use OmniTool\DefaultUtxoSelector;

$selector = new DefaultUtxoSelector();
$selectedBag = $selector->select(                  100000,                         /*最低目标金额*/
                  $candidateBag                   /*候选UTXO集合*/
               );

考虑到UTXO的不可分割性,筛选出的若干UTXO的总和,有可能超过目标金额。可以使用UtxoBag实例的getTotal()方法查看集合中的UTXO总额:

echo 'total wei in bag => ' . $selectedBag->getTotal() . PHP_EOL;

6、裸交易广播器

OmniTool使用BroadcasterInterface来约定裸交易广播的功能。该接口的实现应当将裸交易广播到Omni网络中。

接口方法:

broadcast($rawtx):广播裸交易

参数$rawtx用来声明要广播的裸交易,类型为16进制字符串。

当前实现类:

CloudBroadcasterLocalBroadcaster

例如,下面的代码使用CloudBroadcaster将裸交易码流广播到Omni网络中:

use OmniTool\CloudBroadcaster;

$broadcaster = new CloudBroadcaster(                      'testnet'                     /*测试网*/
                   );
$ret = $broadcaster->broadcast(        '01000000011da9283b4...59f58488ac00000000'  /*裸交易*/
       );

7、数据查询接口

OmniTool使用ExplorerInterface来约定Omni数据查询功能。

接口方法:

getBtcBalance($address):查询指定地址的比特币余额getOmniBalance($address,$propertyId):查询指定地址的Omni代币余额

当前实现类:

CloudBroadcasterLocalBroadcaster

例如,下面的代码使用CloudExplorer查询地址1Jekm8ZswQmDhLFMp9cuYb1Kcq26riFp6m的比特币余额与USDT代币余额:

use OmniTool\CloudExplorer;

$explorer = new CloudExplorer('mainnet');

$address = '1Jekm8ZswQmDhLFMp9cuYb1Kcq26riFp6m';

$balance = $explorer->getBtcBalance($address);echo 'btc balance => ' . PHP_EOL;

$balance = $explorer->getOmniBalance($address,31);echo 'usdt balance => ' . $balance['balance']. PHP_EOL;

推荐教程:支付宝即时到账在线支付实战项目开发视频教程

The above is the detailed content of Introduction to USDT PHP development package OmniTool. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn