__________     __ __     __  _______    ________
  / ____/ __ \   / // /    / / / /  _/ |  / / ____/
 / / __/ / / /  / // /_   / /_/ // / | | / / __/
/ /_/ / /_/ /  /__  __/  / __  // /  | |/ / /___
\____/\____/     /_/    /_/ /_/___/  |___/_____/

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Hive-PHP - A real PHP library for Hive

BY: @mahdiyari | CREATED: July 26, 2022, 6:47 p.m. | VOTES: 817 | PAYOUT: $717.48 | [ VOTE ]

[IMAGE: https://files.peakd.com/file/peakd-hive/mahdiyari/23wC1ZxrRS6LyojwUDQp45BLcjrcuFXSybyB3FVSdhKD4voCnu1ctuaQvWVpdyZFwKZbF.png]

Hive-PHP

This one took a while despite having some demand from PHP developers. But it is finally here.

The serialization and signing are done natively on PHP. Every line of code has been written by me.

I did not test all the operations but all the operations should work. Please do test if you can and let me know if there are any problems.

The documentation is available on both gitlab and packagist. But for the sake of keeping them on blockchain will put them here too.

Installation

composer require mahdiyari/hive-php

Initialization

$hive = new Hive($options?);

Example:

include 'vendor/autoload.php';

use Hive\Hive;

$hive = new Hive();

// or

// default options - these are already configured
$options = array(
  'rpcNodes'=> [
    'https://api.hive.blog',
    'https://rpc.ausbit.dev',
    'https://rpc.ecency.com',
    'https://api.pharesim.me',
    'https://api.deathwing.me'
  ],
  'chainId'=> 'beeab0de00000000000000000000000000000000000000000000000000000000',
  'timeout'=> 7
);
// Will try the next node after 7 seconds of waiting for response
// Or on a network failure

$hive = new Hive($options);

Usage

API calls:

$hive->call($method, $params);

Example:

$result = $hive->call('condenser_api.get_accounts', '[["mahdiyari"]]');
// returns the result as an array
echo $result[0]['name']; // "mahdiyari"
echo $result[0]['hbd_balance']; // "123456.000 HBD"

Private Key:

$hive->privateKeyFrom($string);
$hive->privateKeyFromLogin($username, $password, $role);

Example:

$privateKey = $hive->privateKeyFrom('5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg');
// or
$privateKey = $hive->privateKeyFromLogin('username', 'hive password', 'role');
// role: "posting" or "active" or etc
echo $privateKey->stringKey; // 5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg

Public Key:

$hive->publicKeyFrom($string);
$privateKey->createPublic()

Example:

$publicKey = $hive->publicKeyFrom('STM6aGPtxMUGnTPfKLSxdwCHbximSJxzrRjeQmwRW9BRCdrFotKLs');
// or
$publicKey = $privateKey->createPublic();


echo $publicKey->toString(); // STM6aGPtxMUGnTPfKLSxdwCHbximSJxzrRjeQmwRW9BRCdrFotKLs

Signing

$privateKey->sign($hash256);

Example:

$message = hash('sha256', 'My super cool message to be signed');
$signature = $privateKey->sign($message);
echo $signature;
// 1f8e46aa5cbc215f82119e172e3dd73396ad0d2231619d3d71688eff73f2b83474084eb970955d1f1f9c2a7281681d138ca49fe90ac58bf069549afe961685d932

Verifying

$publicKey->verify($hash256, $signature);

Example:

$verified = $publicKey->verify($message, $signature);
var_dump($verified); // bool(true)

Transactions

There are two ways to broadcast a transaction.

Manual broadcast

Example:

$vote = new stdClass;
$vote->voter = 'guest123';
$vote->author = 'blocktrades';
$vote->permlink = '11th-update-of-2022-on-blocktrades-work-on-hive-software';
$vote->weight = 5000;
$op = array("vote", $vote);

// transaction built
$trx = $hive->createTransaction([$op]);

// transaction signed
$hive->signTransaction($trx, $privateKey);

// will return trx_id on success
$result = $hive->broadcastTransaction($trx);
var_dump($result);
// array(1) {
//   'trx_id' =>
//   string(40) "2062bb47ed0c4c001843058129470fe5a8211735"
// }

Inline broadcast

Example:

$result = $hive->broadcast($privateKey, 'vote', ['guest123', 'blocktrades', '11th-update-of-2022-on-blocktrades-work-on-hive-software', 5000]);
var_dump($result);
// array(1) {
//   'trx_id' =>
//   string(40) "2062bb47ed0c4c001843058129470fe5a8211735"
// }

Notes for Transactions

Operations are in the following format when broadcasting manually:

array('operation_name', object(operation_params))

example:

$vote = new stdClass;
$vote->voter = 'guest123';
$vote->author = 'blocktrades';
$vote->permlink = '11th-update-of-2022-on-blocktrades-work-on-hive-software';
$vote->weight = 5000;

$op = array("vote", $vote);

Any parameter in opreation that is JSON (not to be confused with json strings), should be passed as an object.

Example:

The beneficiaries field in comment_options is in JSON format like this:

$beneficiaries = '[0, {"beneficiaries": [{"account": "mahdiyari","weight": 10000}]}]';

We should convert that into an object.

$beneficiaries = json_decode($beneficiaries);

(beneficiaries go inside the extensions)

$result = $hive->broadcast($privateKey, 'comment_options', ['author', 'permlink', '1000000.000 HBD', 10000, true, true, [$beneficiaries]]);

How to know the operation parameters

Easiest way is to find the operation on a block explorer.

e.g. https://hiveblocks.com/tx/2062bb47ed0c4c001843058129470fe5a8211735

You can also search the operation in /lib/Helpers/Serializer.php to get an idea of what parameters it requires.

Any missing features?

Create an issue or reach out to me.

License

MIT

I hope PHP developers enjoy this and get away from the "hacks" they used so far to interact with Hive blockchain.

Composer package: https://packagist.org/packages/mahdiyari/hive-php
Gitlab repository: https://gitlab.com/mahdiyari/hive-php

Almost forgot the bonus content!
[IMAGE: https://files.peakd.com/file/peakd-hive/mahdiyari/23u69hbZXgiGLqYX8oRkxmTH1CFYbSFSZfNfTABRG8u349MPiZi6j3GrsJwM1kUyDhAfP.jpg]
Made with ❤️ by @mahdiyari
Image source: pixabay.com

TAGS: [ #hive-php ] [ #php ] [ #hive ] [ #dev ] [ #library ]

Replies

@poshtoken | July 26, 2022, 7:09 p.m. | Votes: 11 | [ VOTE ]

https://twitter.com/MahdiYari4/status/1552007676242034689
https://twitter.com/YanPatrick_/status/1552036131616821249
The rewards earned on this comment will go directly to the people( @mahdiyari, @shiftrox ) sharing the post on Twitter as long as they are registered with @poshtoken. Sign up at https://hiveposh.com.

@emeka4 | July 26, 2022, 8:01 p.m. | Votes: 1 | [ VOTE ]

This definitely have a lot of work been put into it which is awesome. Keep up the good work progressing.

@itsostylish | July 26, 2022, 8:08 p.m. | Votes: 2 | [ VOTE ]

Oh, wow, now, that was a lot of work, but you can expect devs to storm Hive. There are so many PHP devs

@ackza | July 29, 2022, 11:59 a.m. | Votes: 0 | [ VOTE ]

And build what? Everything on hive all the dapps...they come snd they go...its like the steemit dot com effect... we need one large stakeholder who is famous to give out huge upvotes to make this work again

@ecency | July 26, 2022, 10:18 p.m. | Votes: 0 | [ VOTE ]

Your content has been voted as a part of Encouragement program. Keep up the good work! Use Ecency daily to boost your growth on platform! Support EcencyVote for new ProposalDelegate HP and earn more

@hivebuzz | July 27, 2022, 1:43 a.m. | Votes: 0 | [ VOTE ]

Congratulations @mahdiyari! Your post has been a top performer on the Hive blockchain and you have been rewarded with the following badge:

Post with the highest payout of the day.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Support the HiveBuzz project. Vote for our proposal!
@innerwebbp | July 27, 2022, 2:23 a.m. | Votes: 3 | [ VOTE ]

OMG THANKK YOU THANK YOU!!! This is SO GREAT!!! ❤️💛💚💙💜❣️

I just updated my post about verifying signatures in php to point here to your post!!! This is so much better and cleaner if you can install ext-gmp!!! This is so GREAT!!! Thank you again!!!

https://peakd.com/stem/@innerwebbp/solved-php-native-hive-signature-validation-and-the-hivewordpress-sso-single-sign-on

@cryptosimplify | July 27, 2022, 9:39 a.m. | Votes: 2 | [ VOTE ]

That is nice.

Currently, I am working on @hiveland.dapp that uses PHP. In this project we don't need to interact with Hive but we need to interact with Hive Engine and in the beginning it was hard because I needed to implement all the interactions with a Hive Engine node that we need.

It is nice to see some "libraries" be implemented on PHP and other langaunge to help developers to interact with our ecosystem.

@mireyalara | July 27, 2022, 11:02 a.m. | Votes: 1 | [ VOTE ]

Wow! This is a great work. Thanks for sharing it here.

@fernandosoder | July 27, 2022, 5:35 p.m. | Votes: 0 | [ VOTE ]

LOVED IT!

@destampid | July 27, 2022, 5:47 p.m. | Votes: 0 | [ VOTE ]

I didn’t understand a thing but the cat deserve a lovelly Up 💓

@ismaelrd04 | July 28, 2022, 1:22 a.m. | Votes: 0 | [ VOTE ]

Wooooooow, this is very wonderful buddy. I love you work and the cat

@vaitengewon | July 28, 2022, 4:04 a.m. | Votes: 0 | [ VOTE ]

Hello @mahdiyari
I don't know how much time you put into this work, but it is certainly very valuable.
Thanks for sharing.

@immanuel94 | July 28, 2022, 10:34 a.m. | Votes: 0 | [ VOTE ]

Amazing work, thank you! This will help a lot! 👍

@katirayo | July 28, 2022, 6:53 p.m. | Votes: 0 | [ VOTE ]

This is very beautiful, I love your work and too be honest, I wish you could teach me this beautiful hand work of yours it will be a Honor to receive teachings from you.

@reanbooks | July 29, 2022, 2:38 a.m. | Votes: 1 | [ VOTE ]

as a beginner doing activities in hive, i have to study this more deeply because it is very important to develop writing performance in hive. Growth in the hive is my goal and the unknown ways will always be learned. thank you friend this is very useful for me.

@ernestoacostame | July 29, 2022, 10:27 a.m. | Votes: 0 | [ VOTE ]

Awesome!!

@ackza | July 29, 2022, 11:58 a.m. | Votes: 0 | [ VOTE ]

Lol is the php elephant related to India?

@mahdiyari | July 29, 2022, 12:03 p.m. | Votes: 0 | [ VOTE ]

https://docs.php.earth/php/community/elephpant/

@hivebuzz | Aug. 1, 2022, 12:51 a.m. | Votes: 0 | [ VOTE ]

Congratulations @mahdiyari! Your post has been a top performer on the Hive blockchain and you have been rewarded with the following badge:

Post with the highest payout of the week.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

The 8th edition of the Hive Power Up Month starts today!Hive Power Up Day - August 1st 2022

Support the HiveBuzz project. Vote for our proposal!
@azamsohrabi | Aug. 4, 2022, 9:18 a.m. | Votes: 0 | [ VOTE ]

Hello good time. Dear friend, is it possible to talk to you on WhatsApp or Telegram? Or should I have a contact number?

@mahdiyari | Aug. 4, 2022, 9:23 a.m. | Votes: 0 | [ VOTE ]

telegram @mahdi_Edw

@silviafx | Aug. 8, 2022, 4:34 p.m. | Votes: 0 | [ VOTE ]

Wow. This is so much effort put into one piece. Well done @mahdiyari

@sora-hara | Nov. 11, 2022, 7:56 a.m. | Votes: 0 | [ VOTE ]

Hello.
I am using your library in my project. Does my project need delegation. I tried $hive->broadcast($privateKey ...) for delegation and it didn't work when Amount delegation > 10000 VEST. The error I get is: missing required active authority:Missing Active Authority sora-haraTransaction failed to validate using both new (hf26) and legacy serialization. The key I use is Active Key private. Can you share with me why? Thank you.

@mahdiyari | Nov. 11, 2022, 8:45 a.m. | Votes: 1 | [ VOTE ]

That error can be because of wrong parameters of the transaction. Send me the full code.

@sora-hara | Nov. 11, 2022, 9:04 a.m. | Votes: 0 | [ VOTE ]

Thanks for your answer. This is my code.
$ops = [
"sora-hara",
"tuanbh",
"5000.000000 VESTS"
];
$hive = new Hive();
// create Obj privateKey
$key = $hive->privateKeyFrom(HIVE_DELEGATION_KEY);
// call API
$request = $hive->broadcast($key, 'delegate_vesting_shares', $ops);
It succeeds when VEST = 2000. AND >= 5000 then error.

@sora-hara | Nov. 11, 2022, 10:56 a.m. | Votes: 0 | [ VOTE ]

I checked with the js library that the sign function to create a signature is giving a different value than your library when putting in the same Transaction. But I can't tell where it's coming from because the encryption libraries of the 2 are different. You code you are correct. Can you give me more information about the sign signature? Version of the library that is associated with it.

@mahdiyari | Nov. 12, 2022, 9:49 a.m. | Votes: 1 | [ VOTE ]

When you create a transaction, its parameters are dynamic so the signature will be different every time you make the same exact transaction. Also, for the same exact transaction (assuming the dynamic parts are fixed), it is completely fine to have multiple unique signatures.
Make sure the account you are delegating has enough VESTS.
The code seems fine but I will do some testing later to confirm it is fine.

@sora-hara | Nov. 17, 2022, 1:09 a.m. | Votes: 0 | [ VOTE ]

Can you tell me your test results with delegation > 5000 VESTS? If it's completely fine. Can you share with me the versions of the libraries involved in the sign key? thank you so much.

@sora-hara | Nov. 17, 2022, 1:24 a.m. | Votes: 0 | [ VOTE ]

About testing. I got a transaction and set them directly to the param of the JS and PHP sign functions. I then used the generated signature of JS for PHP and the transaction was successful. When I pass specific param the JS signature result always returns a value so I don't think there will be any other interference resulting in the 2 different results.

@szejq | Aug. 29, 2023, 9:53 p.m. | Votes: 0 | [ VOTE ]

Hellow @mahdiyari
I test signing account_update2 but i keep getting 🙂

Fatal error: Uncaught TypeError: property_exists(): Argument #2 ($property) must be of type string, stdClass given in vendor/mahdiyari/hive-php/lib/Helpers/Serializer.php:43
        $hive = new Hive();
            #hive settings
            $privateKey = $hive->privateKeyFrom(pass(1)[1]);
            $account_update2 = new stdClass;
            $account_update2->account = ''.pass(1)[0].'';
            $account_update2->json_metadata = '';
            $account_update2->posting_json_metadata = '{"profile":{"about":"'.$text.'","version":2}}';
            $account_update2->extensions = array();
            $op = array("account_update2", $account_update2);

            // transaction built
            $trx = $hive->createTransaction([$op]);

            // transaction signed
            $hive->signTransaction($trx, $privateKey);

            // will return trx_id on success
            return $hive->broadcastTransaction($trx);

After modifying Serializer.php I can sign it properly. But this workaround is not good practice, I want to keep your original code. I have PHP 8.1

        if($serializer[0] == 'optional') {
          $valueSerializer = $serializer[1];
          if (false) {
            $this->Int8Serializer($buffer, 1);
            $this->$valueSerializer($buffer, $data->$param);
          } else {
            $this->Int8Serializer($buffer, 0);
          }

Your original code

        if($serializer[0] == 'optional') {
          $valueSerializer = $serializer[1];
          if (\property_exists($param, $data)) {
            $this->Int8Serializer($buffer, 1);
            $this->$valueSerializer($buffer, $data->$param);
          } else {
            $this->Int8Serializer($buffer, 0);
          }

Is my query incorrect that this error occurs?
Do I have to provide these parameters, if so, what form is correct? I tried different options and I keep getting the same error.

        ['owner', ['optional', 'AuthoritySerializer']],
        ['active', ['optional', 'AuthoritySerializer']],
        ['posting', ['optional', 'AuthoritySerializer']],
        ['memo_key', ['optional', 'PublicKeySerializer']],

Thank you for your help 😊

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>