WavesKit - PHP framework for working with the Waves blockchain

I like PHP for speed of development and excellent portability. It is very good when you always have a tool ready for solving problems in your pocket.

It was quite a shame when, when getting acquainted with the domestic blockchain Waves Platform he did not have a ready-made PHP SDK in his arsenal. Well, I had to write it.

At first I had to use nodes to sign transactions. So, to manage three addresses, you had to run three nodes ... It was a pitiful sight, although it solved some problems. Until the understanding came that relying on nodes is a dead end. First, due to limited functionality API, secondly, because of the speed (nodes were very slow in those days).

I started two parallel jobs. One is to make a blockchain explorer that is fast and completely independent of the node API. The second is to collect all the functions for working with the Waves Platform in one place. This is how the projects came about. w8io и WavesKit.

The first step behind the scenes of the Waves blockchain was w8io browser. It was not easy, but still managed to write an independent calculation of all balances and even find an error in the calculations on the original nodes (bug bounty program By the way, it works for them, they pay for the errors found). You can learn more about the functionality of the w8io browser in this thread: https://forum.wavesplatform.com/t/w8io-waves-explorer-based-on-php-sqlite

In the process of working on w8io, I already had doubts, but when the work came to a logical end and I started creating the SDK, my doubts were confirmed. I could not find some functions anywhere, including the most important cryptographic ones. Then I started by making my foundation bricks. This is how they were born: ABCode to encode in base58 (actually to encode any alphabet to any), Curve25519 to create and verify compatible signatures (with options on steroids), Blake2b to calculate one of the hashes (which has only been available since PHP 7.2), etc.

Here I have to thank Inala Kardanova for some valuable advice that steered me towards composer instead of the usual for me, but outdated, include files.

After a couple of months WavesKit saw the light of day, came out beta versions and is now ready to work with all the standard functionality of the Waves platform. All available in main network transactions can be easily created, signed and sent with just one package that runs on all 64-bit versions of PHP from 5.6 inclusive.

We connect WavesKit to our project:

composer require deemru/waveskit

We use:

use deemruWavesKit;
$wk = new WavesKit( 'T' );
$wk->setSeed( 'manage manual recall harvest series desert melt police rose hollow moral pledge kitten position add' );
$tx = $wk->txBroadcast( $wk->txSign( $wk->txTransfer( 'test', 1 ) ) );
$tx = $wk->ensure( $tx );

In the example above, we create a WavesKit object that runs on testnet "T". We set the seed phrase, from which the keys and address of the account are automatically calculated based on the public key. Next, we create a transfer transaction 0.00000001 Waves from the address automatically calculated from the seed phrase to the address of the “test” alias, transfer it for signature with the private key and send it to the network. After that, we make sure that the transaction is successfully confirmed by the network.

Transactions are focused on functions starting with tx. For a better understanding of working with transactions, you can study WavesKit documentation or go straight to the illustrative examples in continuous integration tests.

Since WavesKit has been developed in real use, it already has advanced features. The first killer feature is ensure function, which controls the achievement of the required level of confidence that the transaction was not lost, but, on the contrary, was confirmed and reached the required number of confirmations in the network.

Another bulletproof mechanism is how WavesKit communicates with nodes. In greenhouse conditions, the framework works only with the main node, maintaining a constant connection with it, but in case of errors it can automatically switch to backup ones. If you are setting up an array of standby nodes, you can call the function setBestNode to determine the best node as the main node by the maximum value of the current height and response speed. Now add an internal query cache to this and feel the care of both users and node owners.

One of the latest advanced mechanisms is the function txMonitor. It appeared in connection with the need to respond to incoming transactions in real time. This function completely solves all the nuances associated with the processing of transactions in the blockchain. No more pain, just set up your callback function with the options you want and wait for new transactions to start your processes. For example, another one of my projects VECRO completely built around this function, you can easily learn how it works right in the project code.

I love open source, it's one of the greatest achievements of mankind. Since I am the only developer and have reached the state that all my needs are solved, I invite you to use and contribute to WavesKit.

Source: habr.com

Add a comment