Monero Block Chain Php Development Kit

MoneroTool is a PHP development kit for docking Monero block chains, which can quickly add support for Monero/XMR to PHP applications.Official download address: http://sc.hubwiz.com/codebag/monero-php-lib/

The main features of MoneroTool are as follows:

  • All RPC API calling interfaces that support Monero
  • Supports offline creation of Monero key pairs and addresses
  • Supports Monero proprietary format mnemonic generation and import

MoneroTool runs in Php7.1+ environment, current version 1.0.0, see main code file list Official Web.

1. Gateway - Monero docking gateway

Gateway is the top class that joins the Monero block chain and is suitable for managing multi-user addresses and transactions. It mainly includes the following methods:

  • newAddress(): Create a new address
  • balance(): Gets the XMR balance for the specified address
  • transfer(): transfer

Instantiation of Gateway requires specifying the URL s of service and wallet nodes, such as the following code to create a Gateway instance using a native Monero node:

$opts = [
  'daemon_url' => 'http://localhost:28081', //Service Node URL
  'wallet_url' => 'http://localhost:28083'//Wallet Node URL
];
$gateway = new Gateway($opts);                      //Return Gateway instance

1.1 Create a new address

Use Gateway's newAddress() method to create a new Monero block chain address for the user, for example:

$addr = $gateway->newAddress();                     //Create a new XMR address
echo 'new address => ' . $addr . PHP_EOL;

1.2 Get address balance

Use Gateway's balance() method to get the balance for the specified Monero address, for example:

$balance = $gateway->balance('9wviCeWe2D8XS82k2o....RrAotYPwq9Gm8');
echo 'total => ' . $balance->total . PHP_EOL;          //Total balance in piconero
echo 'unlocked => ' . $balance->unlocked . PHP_EOL;    //Thaw balance in piconero

The balance() method returns an object, and the total and unlocked fields represent the total and unlocked balances of the addresses queried, respectively, in piconero or pico.

1.3 Transfer

Transfer between specified addresses using Gateway's transfer() method.For example, the following code transfers 1000000 piconero from account from to account to:

$from = '9wviCeWe2D8XS82k2ovp5...7X1D7Geoo2RrAotYPwq9Gm8';
$to = 'A2GmyHHJ9jtUhPiwoAbR2tX...uzKf6RGGgZTFTpVC4QxAiAX';
$amount = 1000000                                     //Unit: piconero
$txid = $gateway->transfer($from,$to,$amount);
echo 'tx hash => ' . $txid . PHP_EOL;

The transfer() method returns the hash of the transfer transaction.

Pionero is the smallest unit of Monroe currency, 1 minero = 10^12 piconero.

Monroe Dollar Php Package Download Address: http://sc.hubwiz.com/codebag/monero-php-lib/

2. RpcClient - Rpc Api Client

RpcClient is an encapsulation of Monero's official RPC protocol for accessing official Daemon and Wallet nodes in PHP applications. Click here to view [Monero's RPC API]
Chinese Documents] ( http://cw.hubwiz.com/card/c/monero-api-manual/).

It is easy to create an instance of RpcClient by simply accessing the URL with the RPC API of the incoming node.For example, the following code instantiates a RpcClient object whose RPC call requests are sent to the Monero service node specified by the parameter URL:

$daemon = new RpcClient('http://localhost:28081';//Return client instance

2.1 Call JSON RPC API

For JSON RPC API s in nodes, they can be called directly by method name.For example, a service node provides
get_block_count Method to get the number of blocks in the chain, using the RpcClient object as follows:

$ret = $daemon->get_block_count();                    //Call JSON RPC API with the same name
echo 'block count => ' . $ret->count .  PHP_EOL;    

If the JSON RPC API method requires parameters, such as those provided by the service node to obtain block data get_block Method requires an incoming block height or a block hash, then the required parameters are organized into an associated array to be passed in.For example, view height 100#block data:

$ret = $daemon->get_block(['height'=>100]);
echo 'number of txs => ' . $ret->block_header->num_txes . PHP_EOL;

2.2 Call other RPC API s

Monero's service node also provides other access interfaces to non-JSON RPC specifications, such as methods to query transaction data by transaction ID/get_transactions.These non-JSON RPCs use their own specific access endpoints, so we can use the post() method of the RpcClient to specify access endpoints and request parameters.

For example, the following code gets the transaction data for the specified hash a6fa....b8fs:

$inputs = [
  'txs_hashes' => ['a6fa97b7c1d7a4f68a8041a2e7ca7a250d01391f14a0d5947b0936dca1f2b8f3']
];
$ret = $daemon->post('/get_transactions',$inputs);   //Non-JSON RPC APi calls
echo 'number of missed tx => ' . count($ret->missed_tx) . PHP_EOL;
echo 'number of found tx => ' . count($ret->txs) . PHP_EOL;

3. FaceValue-Monroe denomination

Monroe currencies are measured in different units, from the smallest atomic unit piconero to the largest meganero:

The FaceValue class encapsulates the face value calculation logic for Monroe/XMR and can be thought of as representing the number of Monroe currencies containing units of measure.So when instantiating a FaceValue object, you need to specify both the number and the unit, for example, to create a FaceValue object containing 20.34 Monroe coins to represent sales revenue:

$sales = FaceValue::parse('20.34 mo');
echo 'sales => ' . $sales . PHP_EOL;                // 20.34 mo

FaceValue encapsulates the par value conversion logic for Monroe currency, so you can get the number of units where the above sales revenue is converted to other units:

echo 'sales in pico => ' . $sales->pico . PHP_EOL;  // 20.340000000000
echo 'sales in mega => ' . $sales->mega . PHP_EOL;  // 0.00002034 

You can also update a specified unit directly, for example, by updating sales to 203.4 mo:

$sales->mo = 203.4; 
echo 'sales in mega => ' . $sales->mega . PHP_EOL;   // 0.0002034

4. Mnemonic-mnemonic

Use the Mnemonics class to create Monero-specific mnemonics or to convert mnemonics into cryptographic seeds.Monero's Mnemonic consists of 25 words and the word list is different from bitcoin.Here is an example of a Monero Mnemonic:

vipers opposite alpine debut rebel essential enigma irony 
moon incur fugitive tufts touchy moisture hijack chlorine vivid 
textbook chrome bias nimbly hamburger wayside tarnished tarnished

4.1 Generate a new Monero mnemonic

Mnemonic provides a static method, new(), to generate new mnemonics.For example, the following code generates a new set of random mnemonics:

$words = Mnemonic::new();
echo 'mnemonic => ' . $words . PHP_EOL;  

4.2 Convert mnemonic words to cryptographic seeds

Mnemonic provides a static method, seed(), to convert the specified mnemonic into a cryptographic seed for operations such as key pair recovery.For example, the following code converts a mnemonic into a cryptographic seed:

$seed = Mnemonic::seed('vipers opposite alpine debut rebel essential enigma irony moon incur fugitive tufts touchy moisture hijack chlorine vivid textbook chrome bias nimbly hamburger wayside tarnished tarnished');
echo 'seed => ' . $seed . PHP_EOL; //78bf0d6c8e877c8ffbf9701e8063a690a91295d6f3a576e7b61c8c7829d8a7e0

Monroe Dollar Php Package Download Address: http://sc.hubwiz.com/codebag/monero-php-lib/

5,Credential

The credential class Crendential is used to manage personal identity credentials on the Monero block chain - key pairs:

5.1 Generating Random Identity

You can generate a random voucher, for example:

$credential = Credential::new();    //Generate a random key pair

5.2 Rebuilding identity credentials

You can also use previously saved mnemonics to rebuild your identity credentials, for example:

$seed = Mnemonic::seed('vipers opposite alpine debut rebel essential enigma irony moon incur fugitive tufts touchy moisture hijack chlorine vivid textbook chrome bias nimbly hamburger wayside tarnished tarnished');
$credential = Credential::infer($seed);  //Deriving key pairs from seeds

5.3 Consumer Key and View Key

Monero is a private currency, and transactions entering a block chain are confused by encryption, so Monero requires two sets of keys compared to other block chains: a spend key and a view key, which are similar to the identity key pairs in other block chains, while a view key is viewed by the user.Encrypt confused block chain transactions so that wallets can track transaction output.

For example, the following code looks at the consumer key pair and the key pair of the voucher object:

echo 'secret spend key => ' . $credential->secretSpendKey() . PHP_EOL;  
echo 'public spend key => ' . $credential->publicSendKey() . PHP_EOL;
echo 'secret view key => ' . $credential->secretViewKey() . PHP_EOL;
echo 'public view key => ' . $credential->publicViewKey() . PHP_EOL;

6,Address / SubAddress / IntegratedAddress

  • Address:Monero Block Chain Standard Address Implementation
  • SubAddress:The implementation of subaddresses in the Monero block chain derived from standard addresses and two-level index numbers
  • IntegratedAddress:Monero block chain implementation of integrated addresses derived from standard addresses and payment ID s

6.1 Decode Standard Address String

Decode the address string using the static decode() method of the Address class, returning it in the Address object
Contains the decoded information.

For example, the code below decodes the specified address and displays the network to which the address belongs, the consumer public key, and the viewing public key:

$addr = Address::decode('9wviCeWe2D8XS82k2ovp5EUYLzBt9pYNW2LXUFsZiv8S3Mt21FZ5qQaAroko1enzw3eGr9qC7X1D7Geoo2RrAotYPwq9Gm8');
echo 'network => ' . $addr->network() . PHP_EOL;
echo 'spend key => ' . $addr->spendKey() . PHP_EOL;
echo 'view  key => ' . $addr->viewKey() . PHP_EOL;

Derivation of 6.2 Addresses

Call the address() method of the Credential object to obtain the Monroe currency address of the credential object.For example:

$addressMain = $credential->address('main');    //Return to home address
echo 'address@mainnet => ' . $addressMain . PHP_EOL;
$addressMain = $credential->address('main');   //Return to test network address
echo 'address@testnet => ' . $addressTest . PHP_EOL;

The address() method returns an Address object.

6.3 Use an integrated address

Using the generateIntegratedAddress() method of the Address object, you can choose from a standard address
Derived integrated address IntegratedAddress object:

$addr = $credential->address('test');
$ia = $addr->generateIntegratedAddress();
echo 'ia => ' . $ia . PHP_EOL;
echo 'ia payment id => ' . $ia->paymentId() . PHP_EOL;

The integrated address fits the merchant's order fees, and by generating a different payment ID for each order, you can
Avoid generating large numbers of Monero credentials and standard addresses.

Keywords: PHP JSON network

Added by christine75 on Fri, 23 Aug 2019 06:02:32 +0300