Hive-PHP - A real PHP library for Hive

in HiveDevs2 years ago (edited)

mugs-hive.png

<h1>Hive-PHP <p dir="auto">This one took a while despite having some demand from PHP developers. But it is finally here. <p dir="auto">The serialization and signing are done natively on PHP. Every line of code has been written by me. <p dir="auto">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. <p dir="auto">The documentation is available on both gitlab and packagist. But for the sake of keeping them on blockchain will put them here too. <hr /> <h3>Installation <pre><code>composer require mahdiyari/hive-php <h3>Initialization <pre><code>$hive = new Hive($options?); <p dir="auto">Example: <pre><code>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); <h3>Usage <h3>API calls: <pre><code>$hive->call($method, $params); <p dir="auto">Example: <pre><code>$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" <h3>Private Key: <pre><code>$hive->privateKeyFrom($string); $hive->privateKeyFromLogin($username, $password, $role); <p dir="auto">Example: <pre><code>$privateKey = $hive->privateKeyFrom('5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg'); // or $privateKey = $hive->privateKeyFromLogin('username', 'hive password', 'role'); // role: "posting" or "active" or etc echo $privateKey->stringKey; // 5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg <h3>Public Key: <pre><code>$hive->publicKeyFrom($string); $privateKey->createPublic() <p dir="auto">Example: <pre><code>$publicKey = $hive->publicKeyFrom('STM6aGPtxMUGnTPfKLSxdwCHbximSJxzrRjeQmwRW9BRCdrFotKLs'); // or $publicKey = $privateKey->createPublic(); echo $publicKey->toString(); // STM6aGPtxMUGnTPfKLSxdwCHbximSJxzrRjeQmwRW9BRCdrFotKLs <h3>Signing <pre><code>$privateKey->sign($hash256); <p dir="auto">Example: <pre><code>$message = hash('sha256', 'My super cool message to be signed'); $signature = $privateKey->sign($message); echo $signature; // 1f8e46aa5cbc215f82119e172e3dd73396ad0d2231619d3d71688eff73f2b83474084eb970955d1f1f9c2a7281681d138ca49fe90ac58bf069549afe961685d932 <h3>Verifying <pre><code>$publicKey->verify($hash256, $signature); <p dir="auto">Example: <pre><code>$verified = $publicKey->verify($message, $signature); var_dump($verified); // bool(true) <h3>Transactions <p dir="auto">There are two ways to broadcast a transaction. <h4>Manual broadcast <p dir="auto">Example: <pre><code>$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" // } <h4>Inline broadcast <p dir="auto">Example: <pre><code>$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" // } <h3>Notes for Transactions <p dir="auto">Operations are in the following format when broadcasting manually: <pre><code>array('operation_name', object(operation_params)) <p dir="auto">example: <pre><code>$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); <p dir="auto">Any parameter in opreation that is JSON (not to be confused with json strings), should be passed as an object. <p dir="auto">Example: <p dir="auto">The <code>beneficiaries field in <code>comment_options is in JSON format like this: <pre><code>$beneficiaries = '[0, {"beneficiaries": [{"account": "mahdiyari","weight": 10000}]}]'; <p dir="auto">We should convert that into an object. <pre><code>$beneficiaries = json_decode($beneficiaries); <p dir="auto">(beneficiaries go inside the extensions) <pre><code>$result = $hive->broadcast($privateKey, 'comment_options', ['author', 'permlink', '1000000.000 HBD', 10000, true, true, [$beneficiaries]]); <hr /> <h3>How to know the operation parameters <p dir="auto">Easiest way is to find the operation on a block explorer. <p dir="auto"><span>e.g. <a href="https://hiveblocks.com/tx/2062bb47ed0c4c001843058129470fe5a8211735" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://hiveblocks.com/tx/2062bb47ed0c4c001843058129470fe5a8211735 <p dir="auto">You can also search the operation in <code>/lib/Helpers/Serializer.php to get an idea of what parameters it requires. <hr /> <h3>Any missing features? <p dir="auto">Create an issue or reach out to me. <hr /> <h3>License <p dir="auto">MIT <hr /> <p dir="auto">I hope PHP developers enjoy this and get away from the "hacks" they used so far to interact with Hive blockchain. <p dir="auto"><br /><span>Composer package: <a href="https://packagist.org/packages/mahdiyari/hive-php" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://packagist.org/packages/mahdiyari/hive-php<span> Gitlab repository: <a href="https://gitlab.com/mahdiyari/hive-php" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://gitlab.com/mahdiyari/hive-php <hr /> <p dir="auto">Almost forgot the bonus content!<br /> <center><img src="https://images.hive.blog/768x0/https://files.peakd.com/file/peakd-hive/mahdiyari/23u69hbZXgiGLqYX8oRkxmTH1CFYbSFSZfNfTABRG8u349MPiZi6j3GrsJwM1kUyDhAfP.jpg" alt="cute-kitten.jpg" srcset="https://images.hive.blog/768x0/https://files.peakd.com/file/peakd-hive/mahdiyari/23u69hbZXgiGLqYX8oRkxmTH1CFYbSFSZfNfTABRG8u349MPiZi6j3GrsJwM1kUyDhAfP.jpg 1x, https://images.hive.blog/1536x0/https://files.peakd.com/file/peakd-hive/mahdiyari/23u69hbZXgiGLqYX8oRkxmTH1CFYbSFSZfNfTABRG8u349MPiZi6j3GrsJwM1kUyDhAfP.jpg 2x" /><br /> <sub><span>Made with ❤️ by <a href="/@mahdiyari">@mahdiyari<br /> <sub>Image source: pixabay.com
Sort:  

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

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

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

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

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.

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

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.


~~~ embed:1552036131616821249 twitter metadata:WWFuUGF0cmlja198fGh0dHBzOi8vdHdpdHRlci5jb20vWWFuUGF0cmlja18vc3RhdHVzLzE1NTIwMzYxMzE2MTY4MjEyNDl8 ~~~
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.

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 Ecency
Vote for new Proposal
Delegate HP and earn more

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

<table><tr><td><img src="https://images.hive.blog/60x60/http://hivebuzz.me/badges/toppayoutday.png" /><td>Post with the highest payout of the day. <p dir="auto"><sub><em>You can view your badges on <a href="https://hivebuzz.me/@mahdiyari" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">your board and compare yourself to others in the <a href="https://hivebuzz.me/ranking" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Ranking<br /> <sub><em>If you no longer want to receive notifications, reply to this comment with the word <code>STOP <h6>Support the HiveBuzz project. <a href="https://hivesigner.com/sign/update_proposal_votes?proposal_ids=%5B%22199%22%5D&approve=true" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Vote for <a href="https://peakd.com/me/proposals/199" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">our proposal!

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

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


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

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

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.

Lol is the php elephant related to India?

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

<table><tr><td><img src="https://images.hive.blog/60x60/http://hivebuzz.me/badges/toppayoutweek.png" /><td>Post with the highest payout of the week. <p dir="auto"><sub><em>You can view your badges on <a href="https://hivebuzz.me/@mahdiyari" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">your board and compare yourself to others in the <a href="https://hivebuzz.me/ranking" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Ranking<br /> <sub><em>If you no longer want to receive notifications, reply to this comment with the word <code>STOP <p dir="auto"><strong><span>Check out the last post from <a href="/@hivebuzz">@hivebuzz: <table><tr><td><a href="/hive-122221/@hivebuzz/pum-202208"><img src="https://images.hive.blog/64x128/https://i.imgur.com/M9RD8KS.png" /><td><a href="/hive-122221/@hivebuzz/pum-202208">The 8th edition of the Hive Power Up Month starts today!<tr><td><a href="/hive-122221/@hivebuzz/pud-202208"><img src="https://images.hive.blog/64x128/https://i.imgur.com/805FIIt.jpg" /><td><a href="/hive-122221/@hivebuzz/pud-202208">Hive Power Up Day - August 1st 2022 <h6>Support the HiveBuzz project. <a href="https://hivesigner.com/sign/update_proposal_votes?proposal_ids=%5B%22199%22%5D&approve=true" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Vote for <a href="https://peakd.com/me/proposals/199" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">our proposal!

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

 2 years ago  

telegram @mahdi_Edw

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

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.

 2 years ago  

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

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.

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.

 2 years ago  

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.

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.

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.

Loading...