mahdiyari/hive-php

Hive 区块链的 PHP 库

v1.1.1 2023-02-13 20:59 UTC

This package is auto-updated.

Last update: 2024-09-14 00:29:04 UTC


README

一个(真实)的 Hive 区块链 PHP 库

安装

composer require mahdiyari/hive-php

初始化

$hive = new Hive($options?);

示例

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);

用法

API 调用

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

示例

$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"

私钥

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

示例

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

公钥

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

示例

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


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

签名

$privateKey->sign($hash256);

示例

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

验证

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

示例

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

交易

广播交易有两种方式。

手动广播

示例

$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"
// }

内联广播

示例

$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"
// }

交易注意事项

手动广播时的操作格式如下

array('operation_name', object(operation_params))

示例

$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);

操作中任何 JSON 参数(不要与 JSON 字符串混淆),都应该作为对象传递。

示例

comment_options 中的 beneficiaries 字段是如下 JSON 格式

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

我们应该将其转换为对象。

$beneficiaries = json_decode($beneficiaries);

(受益人放在扩展中)

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

如何知道操作参数

最简单的方法是在区块浏览器上找到操作。

例如 https://hiveblocks.com/tx/2062bb47ed0c4c001843058129470fe5a8211735

您也可以在 /lib/Helpers/Serializer.php 中搜索操作,以了解它需要的参数。

缺少任何功能吗?

创建一个问题或联系我。

作者

https://hive.blog/@mahdiyari

许可协议

MIT