EOS Chain/Wallet RPC API的PHP开发包

栏目: PHP · 发布时间: 5年前

内容简介:介绍一个EOS Chain/Wallet RPC API的PHP开发包。你可以查看EOS的使用你最喜欢的RPC API主机和KEOSD在项目根目录中创建一个

介绍一个EOS Chain/Wallet RPC API的 PHP 开发包。

开始

你可以查看EOS的 RPC API参考 ,但要注意缺少一些较新的方法。Wallet RPC API实现 EOS v1.1.0 of RPC API reference 。此外,这些文档中的一些示例也使用了过时的语法。

安装

composer require manamine/php-eos-rpc-sdk

配置

使用你最喜欢的RPC API主机和KEOSD在项目根目录中创建一个 dotenv.env 文件。你可以使用 env.example 作为模板:

cp .env.example .env

有一个漂亮的工厂方法来自动化实例所有依赖项:

$api = (new ChainFactory)->api();
$walapi = (new WalletFactory)->api();
$eos = (new EosRpc($api, $walapi));

示例

为了帮助你入门,运行一个简单的示例,它涵盖了所有API命令。

只需通过cli运行此命令即可查看所有命令的示例输出:

cd examples
php chain.php
php wallet.php
php eosrpc.php

API方法

涵盖了几乎所有区块链和钱包API方法。

区块链API

Get Info

获取与节点相关的最新信息

echo $api->getInfo();

Get Block

获取与区块相关的信息

echo $api->getBlock("1337");

Get Block Header State

获取与块头状态相关的信息

echo $api->getBlockHeaderState("0016e48707b181d93117b07451d9837526eba34a9a37125689fb5a73a5d28a38");

Get Account

获取与帐户相关的信息

$api->getAccount("blockmatrix1");

Get Code

获取智能合约代码

echo $api->getCode("eosio.token");

Get Table Rows

从帐户中获取智能合约数据

echo $api->getTableRows("eosio", "eosio", "producers", ["limit" => 10]);

Get Currency Balance

获取帐户的货币余额

echo $api->getCurrencyBalance("eosio.token", "eosdacserver");

Get Currency Stats

获取货币状态

echo $api->getCurrencyStats("eosio.token", "EOS");

Get ABI

获取账户ABI

echo $api->getAbi("eosio.token");

Get Raw Code and ABI

获取原生代码和ABI

echo $api->getRawCodeAndAbi("eosio.token");

Get Producers

列出生产者

echo $api->getProducers(10);

ABI JSON To Bin

将json序列化为二进制十六进制

echo $api->abiJsonToBin("eosio.token", "transfer", ["blockmatrix1", "blockmatrix1", "7.0000 EOS", "Testy McTest"]);

ABI Bin To JSON

将二进制十六进制序列化为json

echo $api->abiBinToJson("eosio.token", "transfer", "10babbd94888683c10babbd94888683c701101000000000004454f53000000000c5465737479204d6354657374");

Get Required Keys

获取签署交易所需的密钥

echo $api->getRequiredKeys(
     [
         "expiration" => "2018-08-23T05.00.00",
         "ref_block_num" => 15078,
         "ref_block_prefix" => 1071971392,
         "max_net_usage_words" => 0,
         "delay_sec" => 0,
         "context_free_actions" => [],
         "actions" => [
             [
                 "account" => "eosio.token",
                 "name" => "transfer",
                 "authorization" => [
                     [
                         "actor" => "user",
                         "permission" => "active"
                     ]
                 ],
                 "data" => "00000000007015d6000000005c95b1ca102700000000000004454f53000000000c757365722d3e746573746572"
             ]
         ],
         "transaction_extensions" => []
     ],
     [
         "EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4"
     ]
 );
```

Push Transaction

提交推送一个交易

echo $api->pushTransaction("2018-08-23T05:29:39", "15780", "90170226",
    [
        "actions" => [
            [
                "account" => "eosio.token",
                "name" => "transfer",
                "authorization" => [
                    [
                        "actor" => "user",
                        "permission" => "active"
                    ]
                ],
                "data" => "00000000007015d6000000005c95b1ca102700000000000004454f53000000000c757365722d3e746573746572"
            ]
        ],
        "signatures" => [
            "SIG_K1_KaGHyi59BRqfaDUK6424TYEWcUhWxAG7BLCgYC8vwYNgaHgGLpduTUbNQEsfL8xLzboK8W9T2X69bNpqozTQVCbRSNJWFd"
        ]
    ]
);

Push transactions

提交推送一些交易

echo $api->pushTransactions(
    [
        [
            "compression" => "none",
            "transaction" => [
                "expiration" => "2018-08-23T06:27:26",
                "ref_block_num" => 22017,
                "ref_block_prefix" => 3920123292,
                "context_free_actions" => [],
                "actions" => [
                    [
                        "account" => "eosio.token",
                        "name" => "transfer",
                        "authorization" => [
                            [
                                "actor" => "user",
                                "permission" => "active"
                            ]
                        ],
                        "data" => "00000000007015d6000000005c95b1ca102700000000000004454f53000000000c757365722d3e746573746572"
                    ]
                ],
                "transaction_extensions" => []
            ],
            "signatures" => [
                "SIG_K1_JzN9DnpyhKfjoef3C2TZBTPA5b6ftwuEBnBpvzkueVXThJ34PFFpUFgqyayfXjeLRc15JmZmDiMYAFX99hUgX8vkGAYcnx"
            ]
        ],
        [
            "compression" => "none",
            "transaction" => [
                "expiration" => "2018-08-23T06:27:26",
                "ref_block_num" => 22017,
                "ref_block_prefix" => 3920123292,
                "context_free_actions" => [],
                "actions" => [
                    [
                        "account" => "eosio.token",
                        "name" => "transfer",
                        "authorization" => [
                            [
                                "actor" => "tester",
                                "permission" => "active"
                            ]
                        ],
                        "data" => "000000005c95b1ca00000000007015d6881300000000000004454f53000000000c7465737465722d3e75736572"
                    ]
                ],
                "transaction_extensions" => []
            ],
            "signatures" => [
                "SIG_K1_KZ2M4AG59tptdRCpqbwzMQvBv1dce5btJCJiCVVy96fTGepApGXqJAwsi17g8AQdJjUQB4R62PprfdUdRYHGdBqK1z9Sx9"
            ]
        ]
    ]
);

钱包 Wallet APIs

Create

创建具有给定名称的新钱包

echo $walapi->create("testwallet");

Open

打开给定名称的现有钱包

echo $walapi->open("testwallet");

Lock

锁定给定名称的现有钱包

echo $walapi->lock("testwallet");

Lock All

锁定所有现有钱包

echo $walapi->lockAll();

Unlock

使用给定的名称和密码解锁钱包

echo $ walapi-> unlock([“testwallet”,“PW5Jb8RAZP6CBjjMLPser3T8i8k9hZXZkMBJ8kb1p6f6hAg2n68jY”]);

Import Key

将私钥导入给定名称的钱包

echo $walapi->importKey(["testwallet", "5Jmsawgsp1tQ3GD6JyGCwy1dcvqKZgX6ugMVMdjirx85iv5VyPR"]);

Remove Key

从给定名称的钱包中删除密钥对

echo $walapi->removeKey(["testwallet", "PW5Jb8RAZP6CBjjMLPser3T8i8k9hZXZkMBJ8kb1p6f6hAg2n68jY", "EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4"]);

Create Key

创建密钥对并导入

echo $walapi->createKey(["testwallet", "K1"]);

List Wallets

列出所有钱包

echo $walapi->listWallets();

List Keys

列出给定名称和密码的钱包中的所有密钥对

echo $walapi->listKeys(["testwallet", "PW5Jb8RAZP6CBjjMLPser3T8i8k9hZXZkMBJ8kb1p6f6hAg2n68jY"]);

Get Public Keys

列出所有钱包中的所有公钥

echo $walapi->getPublicKeys();

Set Timeout

设置钱包自动锁定超时(以秒为单位)

echo $walapi->setTimeout(60);

Sign Transaction

签署交易

echo $walapi->signTransaction(
    [
        "expiration" => "2018-08-23T06:35:30",
        "ref_block_num" => 22985,
        "ref_block_prefix" => 3016594541,
        "max_net_usage_workds" => 0,
        "delay_sec" => 0,
        "context_free_actions" => [],
        "actions" => [
            [
                "account" => "eosio.token",
                "name" => "transfer",
                "authorization" => [
                    [
                        "actor" => "user",
                        "permission" => "active"
                    ]
                ],
                "data" => "00000000007015d6000000005c95b1ca102700000000000004454f53000000000c757365722d3e746573746572"
            ]
        ],
        "transaction_extensions" => []
    ],
    [
        "EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4"
    ],
    "cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f"
);

EOS Wrapper APIs

Prerequisites

需要设置钱包名称和密码

$eos->setWalletInfo("testwallet", "PW5Jb8RAZP6CBjjMLPser3T8i8k9hZXZkMBJ8kb1p6f6hAg2n68jY");

Push Transaction

推送交易

echo $eos->pushTransaction(
    [
        [
            "account" => "eosio.token",
            "name" => "transfer",
            "authorization" => [
                [
                    "actor" => "user",
                    "permission" => "active"
                ]
            ],
            "data" => [
                "from" => "user",
                "to" => "tester",
                "quantity" => "1.0000 EOS",
                "memo" => "memo"
            ]
        ]
    ]
);

Make Transaction

进行交易(对pushTransactions有用)

$trx = $eos->makeTransaction(
    [
        [
            "account" => "eosio.token",
            "name" => "transfer",
            "authorization" => [
                [
                    "actor" => "user",
                    "permission" => "active"
                ]
            ],
            "data" => [
                "from" => "user",
                "to" => "tester",
                "quantity" => "1.0000 EOS",
                "memo" => "memo"
            ]
        ]
    ]
);

Push Transactions

推送一些交易

$trx_ids = $eos->pushTransactions(
    [
        $eos->makeTransaction(
            [
                [
                    "account" => "eosio.token",
                    "name" => "transfer",
                    "authorization" => [
                        [
                            "actor" => "user",
                            "permission" => "active"
                        ]
                    ],
                    "data" => [
                        "from" => "user",
                        "to" => "tester",
                        "quantity" => "1.0000 EOS",
                        "memo" => "memo"
                    ]
                ]
            ]
        ),
        $eos->makeTransaction(
            [
                [
                    "account" => "eosio.token",
                    "name" => "transfer",
                    "authorization" => [
                        [
                            "actor" => "tester",
                            "permission" => "active"
                        ]
                    ],
                    "data" => [
                        "from" => "tester",
                        "to" => "user",
                        "quantity" => "0.5000 EOS",
                        "memo" => "memo"
                    ]
                ]
            ]
        )
    ]
);
foreach ($trx_ids as $key => $value) {
    echo $trx_ids[$key]['transaction_id'] . PHP_EOL;
}

Push Action

推动一个操作

echo $eos->pushAction("eosio", "buyram", ["payer"=>"tester","receiver"=>"tester","quant"=>"1.0000 EOS"], ["actor"=>"tester","permission"=>"active"]);

Transfer

交易支付

echo $eos->transfer("user", "tester", "1.0000 EOS", "memo");

Create Key Pair

创建密钥对并返回

$keyPair = $eos->createKeyPair("K1");
echo "$keyPair[0], $keyPair[1]";

Tests

要运行测试套件,只需执行:

vendor/bin/phpunit

如果您想获得想象并检查代码覆盖率:

vendor/bin/phpunit --coverage-html tests/coverage

如果你真的很无聊,你可能想要运行一些静态分析:

vendor/bin/phpmetrics --report-html="tests/static" .

======================================================================

分享一些以太坊、EOS、比特币等区块链相关的交互式在线编程实战教程:

  • java以太坊开发教程,主要是针对 java 和android程序员进行区块链以太坊开发的web3j详解。
  • python以太坊,主要是针对 python 工程师使用web3.py进行区块链以太坊开发的详解。
  • php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和交易等内容。
  • 以太坊入门教程,主要介绍智能合约与dapp应用开发,适合入门。
  • 以太坊开发进阶教程,主要是介绍使用node.js、 mongodb 、区块链、ipfs实现去中心化电商DApp实战,适合进阶。
  • C#以太坊,主要讲解如何使用C#开发基于.Net的以太坊应用,包括账户管理、状态与交易、智能合约开发与交互、过滤器和交易等。
  • EOS教程,本课程帮助你快速入门EOS区块链去中心化应用的开发,内容涵盖EOS工具链、账户与钱包、发行代币、智能合约开发与部署、使用代码与智能合约交互等核心知识点,最后综合运用各知识点完成一个便签DApp的开发。
  • java比特币开发教程,本课程面向初学者,内容即涵盖比特币的核心概念,例如区块链存储、去中心化共识机制、密钥与脚本、交易与UTXO等,同时也详细讲解如何在Java代码中集成比特币支持功能,例如创建地址、管理钱包、构造裸交易等,是Java工程师不可多得的比特币开发学习课程。
  • php比特币开发教程,本课程面向初学者,内容即涵盖比特币的核心概念,例如区块链存储、去中心化共识机制、密钥与脚本、交易与UTXO等,同时也详细讲解如何在Php代码中集成比特币支持功能,例如创建地址、管理钱包、构造裸交易等,是Php工程师不可多得的比特币开发学习课程。
  • tendermint区块链开发详解 ,本课程适合希望使用tendermint进行区块链开发的工程师,课程内容即包括tendermint应用开发模型中的核心概念,例如ABCI接口、默克尔树、多版本状态库等,也包括代币发行等丰富的实操代码,是 go 语言工程师快速入门区块链开发的最佳选择。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Nature of Code

The Nature of Code

Daniel Shiffman / The Nature of Code / 2012-12-13 / GBP 19.95

How can we capture the unpredictable evolutionary and emergent properties of nature in software? How can understanding the mathematical principles behind our physical world help us to create digital w......一起来看看 《The Nature of Code》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具