EOS Chain/Wallet RPC API的PHP開發包

介紹一個EOS Chain/Wallet RPC API的PHP開發包。php

開始

你能夠查看EOS的RPC API參考,但要注意缺乏一些較新的方法。Wallet RPC API實現EOS v1.1.0 of RPC API reference。此外,這些文檔中的一些示例也使用了過期的語法。html

安裝java

composer require manamine/php-eos-rpc-sdk

配置node

使用你最喜歡的RPC API主機和KEOSD在項目根目錄中建立一個dotenv.env文件。你可使用env.example做爲模板:python

cp .env.example .env

有一個漂亮的工廠方法來自動化實例全部依賴項:android

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

示例程序員

爲了幫助你入門,運行一個簡單的示例,它涵蓋了全部API命令。web

只需經過cli運行此命令便可查看全部命令的示例輸出:mongodb

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

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

另外,若是你們以爲開源後續支持和服務有問題,也能夠選擇這個商業的PHP EOS SDK 開發服務

分享一些以太坊、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語言工程師快速入門區塊鏈開發的最佳選擇。

匯智網原創翻譯,轉載請標明出處。這裏是原文EOS Chain/Wallet RPC API的PHP開發包

相關文章
相關標籤/搜索