Introduction to USDT PHP development package OmniTool

步履不停
Release: 2023-04-06 20:26:02
Original
3962 people have browsed it

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*/
          );
Copy after login

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*/
       );
Copy after login

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"                                 /*转移的代币数量*/
       );
Copy after login

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']    /*地址清单*/  
       );
Copy after login

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代币转账示例代码如下,说明见注释:

<?phprequire(&#39;../vendor/autoload.php&#39;);use OmniTool\Wallet;                              /*引入开发包*/$wallet = Wallet::cloud(            &#39;./demo.wallet&#39;,                      /*钱包文件地址,自动创建*/
            &#39;testnet&#39;                             /*网络ID*/
          );
$prvKey = &#39;4aec8e45106....00d5c5af494a4e05b&#39;;     /*私钥:16进制字符串*/            $wallet->addKey($prvKey);                         /*将私钥加入钱包,只需加入一次*/$addressList = $wallet->getAddressList();         /*返回钱包管理的所有地址,数组*/$rawtx = $wallet->omniSendTx(
            $addressList[0],                      /*发送方地址,私钥必须已经加入钱包*/
            &#39;mgYPLmNuZymK...e2XUNF6VFnT&#39;,         /*接收方地址*/
            2,                                    /*转账OMNI代币ID,2:TOMN*/
            &#39;0.000001&#39;                            /*转账OMNI代币数量*/
         );

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

注意:

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

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

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

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

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

注意:

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

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

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

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

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

3.4 比特币转账

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

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

<?phprequire(&#39;../vendor/autoload.php&#39;);use OmniTool\Wallet;

$wallet = Wallet::cloud(&#39;./demo.wallet&#39;,&#39;testnet&#39;);
$addressList = $wallet->getAddressList();

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

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

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

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

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(                    &#39;testnet&#39;                       /*测试网*/
                 );
$candidateBag = $collector->collect(
                    [&#39;mi8BvbK73nDQ...KhfQ5ysKRn&#39;]   /*地址清单*/
                );
Copy after login

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集合*/
               );
Copy after login

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

echo &#39;total wei in bag => &#39; . $selectedBag->getTotal() . PHP_EOL;
Copy after login

6、裸交易广播器

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

接口方法:

broadcast($rawtx):广播裸交易

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

当前实现类:

CloudBroadcasterLocalBroadcaster

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

use OmniTool\CloudBroadcaster;

$broadcaster = new CloudBroadcaster(                      &#39;testnet&#39;                     /*测试网*/
                   );
$ret = $broadcaster->broadcast(        &#39;01000000011da9283b4...59f58488ac00000000&#39;  /*裸交易*/
       );
Copy after login

7、数据查询接口

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

接口方法:

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

当前实现类:

CloudBroadcasterLocalBroadcaster

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

use OmniTool\CloudExplorer;

$explorer = new CloudExplorer(&#39;mainnet&#39;);

$address = &#39;1Jekm8ZswQmDhLFMp9cuYb1Kcq26riFp6m&#39;;

$balance = $explorer->getBtcBalance($address);echo &#39;btc balance => &#39; . PHP_EOL;

$balance = $explorer->getOmniBalance($address,31);echo &#39;usdt balance => &#39; . $balance[&#39;balance&#39;]. PHP_EOL;
Copy after login

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template