OmniTool development kit for PHP application applies to a rapid increase in the ability to support Omni Layer / USDT digital assets that support the use of scenarios Own Omni Layer node also supports lightweight deployment scenarios based on third party API services and offline transactions bare . Download: Omni / USDT PHP development package .
1, OmniTool Development Kit Introduction
OmniTool Development Kit includes the following main features:
- Omni Layer comprehensive RPC package node
- Support Node using its own or third-party service to obtain a set of specified address utxo
- Support for offline generation omni transfer tokens naked trading
- Support Node using its own or third-party service broadcasting bare transaction
OmniTool support Omnicored local node deployments, and also supports blockchain.info, btc.com other open API provides, to add support for other third-party service is also very simple, only need to refer to code that implements the following interfaces:
- UtxoCollectorInterface: utxo collector
- UtxoSelectorInterface: utxo Filter
- BroadcasterInterface: bare transaction broadcaster
- ExplorerInterface: Data Query Interface
OmniTool package running Php 7.1+ environment, current version 1.0.0, the main classes / interfaces and relationships as shown below:
The main code file list OmniTool see: http://sc.hubwiz.com/codebag/omni-php-lib/
2, RpcClient class instructions
RpcClient class encapsulates Omni Layer RPC interface protocol. When you create a RpcClient objects need to pass contain valid identity information node RPC URL. For example, assume omnicored node software is installed in the machine is as follows:
- rpcuser:user
- rpcpassword:123456
- rpcport:8332
You can use the following code to instantiate RpcClient:
use \OmniTool\RpcClient;
$client = new RpcClient(
'http://user:123456@localhost:8332' /*节点RPC接口的URL*/
);
Omni Core nodes existing outside Bitcoin RPC interface, additional expansion interface for operating the data Omni layer, these extended RPC interface uses omni_
a prefix to the original segment in the Bitcoin RPC interface. In order to facilitate the two layers of RPC call segment, we are introduced RpcClient protocol submodule concept, the original Bitcoin RPC interface and Omni extended RPC interface to attach a sub-module and omni btc submodules.
For example, obtaining an address token USDT balance requires Omni layer omni_getbalance call, the RPC call instance corresponding RpcClient omni
submodule getBalance()
method. The following code fetches the address 1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P
of USDT (Asset ID: 31) Balance:
$ret = $client->omni->getBalance(
'1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P', /*地址*/
31 /*资产ID:USDT*/
);
Similarly, the use omni_send call to perform simple USDT transfers the call to the corresponding RpcClient omni sub-module instance send()
method. The following code from the address 3M9qvHKtgARhqcMtM5cRT9VaiDJ5PSfQGY
to the address 37FaKponF7zqoMLUjEiko25pDiuVH5YLEa
transferred 100.0 USDT tokens:
$ret = $client->omni->send(
'3M9qvHKtgARhqcMtM5cRT9VaiDJ5PSfQGY', /*代币转出地址*/
'37FaKponF7zqoMLUjEiko25pDiuVH5YLEa', /*代币转入地址*/
31, /*代币ID:USDT*/
"100.00" /*转移的代币数量*/
);
Original bitoin RPC interface layer can be accessed by the RpcClient btc submodule. For example, using listunspent call to get the local node in the specified address utxo:
$ret = $client->btc->listUnspent(
6, /*最小确认数*/
999999, /*最大确认数*/
['mgnucj8nYqdrPFh2JfZSB1NmUThUGnmsqe'] /*地址清单*/
);
Development package demo/rpc-demo.php
sample code uses RpcClient class full presentation in the issuance and transfer of tokens Omni functional layer, if you plan to build your own Omni Core nodes, we believed that this example will be very helpful.
3, Wallet-based instructions
If you do not want to build your own Omni Core nodes, but hope based on third party API for your PHP applications to increase support for Omni Layer / USDT, then the easiest way is to use an offline transaction entry class Wallet .
Wallet-based primary role is to create and broadcast according Omni transaction token transfer bare or naked bit transfer transaction credits, using its basic steps are as follows:
- Use
Wallet::cloud()
to create an instance of a Wallet API support cloud services static method - Using the
addKey()
method of the necessary private key Wallet added to the instance, for example, turn out private address, the private key because the Wallet needs to sign the transaction bare - Using the
omniSendTx()
method of generating bare Omni token transfer transactions, or usingbtcSendTx()
methods bare bit transfer transaction credits - Using the
broadcast()
method broadcast naked trading
3.1 Omni token transfer
Omni token transfer using Wallet sample code to achieve the following, description See note:
<?php
require('../vendor/autoload.php');
use OmniTool\Wallet; /*引入开发包*/
$wallet = Wallet::cloud(
'./demo.wallet', /*钱包文件地址,自动创建*/
'testnet' /*网络ID*/
);
$prvKey = '4aec8e45106....00d5c5af494a4e05b'; /*私钥:16进制字符串*/
$wallet->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);
note:
- Wallet wallet examples of the use of the private key to generate a list of addresses, and uses these to get utxo information from a third party service. Requiring the private key corresponding to the address wallet on chain has utxo exist, Wallet object is to be able to successfully construct bare transaction.
- Chain transfer same ID should be the target address specified by the Wallet when creating an object, e.g. p2pkh mainnet address, the prefix should be 1
Fee payment address specified Omni trading 3.2
In the Omni protocol layer without having to pay transaction fees, but Omni Exchange embedded bit currency trading still need to pay the fee. By default, the omniSendTx()
method uses the sender address Bitcoin payment transaction fees, but can pass additional parameters to specify a different address to pay transaction fees when your PHP applications require multi-account when imputation function, use a unified fee payment address will be easier to manage some.
For example, the following code uses the address mnRo8JyTHDd5NxRb3UvGbAhCBPQTQ4UZ8W
premium omni trading:
$rawtx = $wallet->omniSendTx(
$addressList[0], /*发送方地址,私钥必须已经加入钱包*/
'mgYPLmNuZymK...e2XUNF6VFnT', /*接收方地址*/
2, /*转账OMNI代币ID,2:TOMN*/
'0.000001', /*转账OMNI代币数量*/
'mnRo8JyTHDd5...CBPQTQ4UZ8W' /*交易手续费支付地址*/
);
note:
- Even if you specify adequate balance of fee payment address, the sender Omni transaction still must have a small amount of Bitcoin balance (546 SATOSHI), because Omni transaction protocol requires the sender has at least one available UTXO.
- Fee payment address also addresses the change, the excess will be returned to the token bit address
Bitcoin specified number of transfers 3.3 Omni transactions
Since the transaction request Omni UTXO sender must have available, and therefore in order to facilitate Omni tokens received address may continue to circulate Omni held token, omniSendTx()
the method will party address received in the default currency into trace bit (546 SATOSHI), you can modify this default value when calling the method.
For example, the following code into the receiver 1000 SATOSHI:
$rawtx = $wallet->omniSendTx(
$addressList[0], /*发送方地址,私钥必须已经加入钱包*/
'mgYPLmNuZymK...e2XUNF6VFnT', /*接收方地址
2, /*转账OMNI代币ID,2:TOMN*/
'0.000001', /*转账OMNI代币数量*/
'mnRo8JyTHDd5...CBPQTQ4UZ8W', /*交易手续费支付地址*/
1000 /*转账比特币数量,单位:SATOSHI*/
);
3.4 Bitcoin transfers
OmniTool also supports the generation and transfer Bitcoin broadcast naked transactions.
For example, the following code into SATOSHI 1000 to a specified address from the first address receiving purse:
<?php
require('../vendor/autoload.php');
use OmniTool\Wallet;
$wallet = Wallet::cloud('./demo.wallet','testnet');
$addressList = $wallet->getAddressList();
$rawtx = $wallet->btcSendTx(
$addressList[0], /*发送方地址*/
'moneyqMan7u...8qVrc9ikLP', /*接收方地址*/
1000, /*转账比特币数量,单位:SATOSHI*/
500 /*手续费,单位:SATOSHI*/
);
echo 'btc rawtx => ' . $rawtx . PHP_EOL;
$ret = $wallet->broadcast($rawtx); /*广播裸交易*/
By default, btcSendTx()
using the change address as the sender address, other addresses can also be specified as the change address when invoked, for example, the following code creates a new address received the change:
$changeAddress = $wallet->getNewAddress(); /*创建新地址*/
$rawtx = $wallet->btcSendTx(
$addressList[0], /*发送方地址*/
'moneyqMan7u...8qVrc9ikLP', /*接收方地址*/
1000, /*转账比特币数量,单位:SATOSHI*/
500, /*手续费,单位:SATOSHI*/
$changeAddress /*找零地址*/
);
4, UTXO collector
OmniTool use the interface UtxoCollectorInterface
to the convention collection capabilities UTXO. Implementation of this interface needs to support the specified address acquired UTXO candidate set, a plurality of addresses can be specified.
Interface method:
- collect ($ addressList): Extraction and return candidate set UTXO
Parameters $addressList
used to declare the list of addresses to collect UTXO, the type of array.
The current implementation class:
- CloudUtxoCollector: API based on open blockchain.com the implementation Utxo collector
- LocalUtxoCollector: omnicored node based RPC API implemented Utxo collector
For example, the following code uses the acquired address CloudUtxoCollector mi8BvbK73nDQfaN3acpaFGYQKhfQ5ysKRn
UTXO of:
use OmniTool\CloudUtxoCollector;
$collector = new CloudUtxoCollector(
'testnet' /*测试网*/
);
$candidateBag = $collector->collect(
['mi8BvbK73nDQ...KhfQ5ysKRn'] /*地址清单*/
);
5, UTXO filters
OmniTool use UtxoSelectorInterface
to agree UTXO filtering capabilities. The need to implement the interface to select the target value from the available candidates UTXO UTXO and returns a new instance UtxoBag.
Interface method:
- select ($ target, $ candidates): Select consumable UTXO, the object is returned UtxoBag
Parameters $target
declared goal to reach the minimum amount, unit: wei.
Parameter $candidates
is set utxo candidates, the object is generally achieved UtxoCollectorInterface collect () UtxoBag the object returned by the call.
The current implementation class:
- DefaultUtxoSelector
The following code example, using the example of selecting at least 100000 wei DefaultUtxoSelector of UTXO UTXO candidate is deleted from:
use OmniTool\DefaultUtxoSelector;
$selector = new DefaultUtxoSelector();
$selectedBag = $selector->select(
100000, /*最低目标金额*/
$candidateBag /*候选UTXO集合*/
);
Considering the indivisibility UTXO the sum of several UTXO screened out, it is likely to exceed the target amount. You can use UtxoBag example of getTotal()
ways to view the total UTXO collection:
echo 'total wei in bag => ' . $selectedBag->getTotal() . PHP_EOL;
6, bare transaction broadcaster
OmniTool use BroadcasterInterface
to the agreed transaction bare broadcast function. Implementation of this interface should bare the transaction will be broadcast to Omni network.
Interface method:
- broadcast ($ rawtx): Broadcast bare Trading
Parameters $rawtx
used bare transaction statement to be broadcast, the type of hexadecimal string.
The current implementation class:
- CloudBroadcaster
- LocalBroadcaster
For example, the following code uses the transaction code CloudBroadcaster bare Omni stream broadcast to the network:
use OmniTool\CloudBroadcaster;
$broadcaster = new CloudBroadcaster(
'testnet' /*测试网*/
);
$ret = $broadcaster->broadcast(
'01000000011da9283b4...59f58488ac00000000' /*裸交易*/
);
7, data query interface
OmniTool use ExplorerInterface
to agree Omni data query functions.
Interface method:
- getBtcBalance ($ address): Bitcoin balance query specified address
- getOmniBalance ($ address, $ propertyId): Omni query specified address token balance
The current implementation class:
- CloudBroadcaster
- LocalBroadcaster
For example, the following code uses CloudExplorer inquiry address 1Jekm8ZswQmDhLFMp9cuYb1Kcq26riFp6m
bit credits and USDT token Balance Balance:
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;
OmniTool Download: http://sc.hubwiz.com/codebag/omni-php-lib/