Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Filecoin.PHP package

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to use the Filecoin.PHP package". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the Filecoin.PHP package.

The Filecoin.PHP package is suitable for rapidly increasing support for Filecoin/FIL digital assets for PHP applications, that is, application scenarios that use their own Filecoin block chain nodes, as well as lightweight deployment scenarios based on third-party common nodes.

1. Overview of Filecoin.PHP development package

The Filecoin.PHP development package mainly includes the following features:

Support offline generation of Filecoin addresses to facilitate management and maintenance

Offline signature of Filecoin messages is supported to better protect the private key.

Automatically estimate the GAS parameters of Filecoin messages to avoid manual adjustment

Support for the use of own nodes or third-party nodes, such as public nodes provided by Infura

Perfect API encapsulation of Filecoin nodes to support all RPC API calls, such as querying address history messages, etc.

The Filecoin.PHP package runs in Php 7.1 + environment, and the current version 1.0.0. The main classes / interfaces and relationships are shown in the following figure:

For a list of the main code files of the Filecoin.PHP development package, see the official website description: http://sc.hubwiz.com/codebag/filecoin-php-lib/

2. Create a new address using sample code 2.1

Enter the demo code directory at the terminal and execute the following command:

~ $cd ~ / filecoin.php/demo~/filecoin.php/demo$ php NewAddressDemo.php

The implementation results are as follows:

2.2 recover addresses using private keys

Enter the demo code directory at the terminal and execute the following command:

~ $cd ~ / filecoin.php/demo~/filecoin.php/demo$ php RestoreAddressDemo.php

The implementation results are as follows:

2.3 FIL transfer and balance inquiry

Enter the demo code directory at the terminal and execute the following command:

~ $cd ~ / filecoin.php/demo~/filecoin.php/demo$ php FilTransferDemo.php

The implementation results are as follows:

2.4 RPC client invocation example

Enter the demo code directory at the terminal and execute the following command:

~ $cd ~ / filecoin.php/demo~/filecoin.php/demo$ php demo-rpc-client.php

The implementation results are as follows:

3. Use Filecoin.PHP

FilKit is the entrance to the development package, and you can use this class to quickly implement functions such as FIL transfer, transaction confirmation waiting, and balance query.

2.1 instantiate FilKit

To instantiate FilKit, you need to pass in a RpcClient object and a Credential object, which encapsulate the API provided by the Filecoin node and the user identity information for signing the transaction.

For example, the following code creates a FilKit instance of a Filecoin node connected to Infura and signs the transaction with the specified private key:

Use Filecoin\ FilKit;use Filecoin\ RpcClient;use Filecoin\ Credential;$client = new RpcClient (/ / create the filecoin node URL of the RPC client instance 'https://filecoin.infura.io', / / INFURA [' PROJECT_ID', 'PROJECT_SECRET'] / / project ID and password assigned by INFURA) $credential = Credential::fromKeyBase64 (/ / create identity certificate 'AacNySnfq9cdInB1ZUUvJJVTeqaI7LOW9EcX3UEDFfE=' / / base64-encoded private key with existing private key); $kit = new FilKit ($client, $credential); / / create FilKit instance

The Credential object specified when the FilKit instance is created will be used as the default identity certificate to perform subsequent operations such as transfer transactions.

RpcClient / RPC client

If you are using a Filecoin node that does not require authentication, you only need to pass in RPC URL when you create the RpcClient. For example, using a native filecoin node:

$client = new RpcClient ('http://127.0.0.1/rpc/v0'); / / connect to the native node

If the Filecoin node you are using enables the authentication mechanism of the authorized TOKEN, you need to pass in the authorized TOKEN when creating the RpcClient, for example:

$client = new RpcClient ('http://234.10.58.147/rpc/v0', / / node RPC API URL' Ynl0ZSBhcnJheQ==' / / authorization TOKEN assigned by the node)

Credential / identity credential

If the existing private key is a hexadecimal string, use the static method fromKey () of the Credential class to create the instance object, for example:

$credential = Credential::fromKey ('01a70dc929dfabd71d22707565452f2495537aa688ecb396f44717dd410315f1' / / hexadecimal string format private key); 2.2 FIL transfer transaction

Use the transfer () method of FilKit to transfer money to FIL, for example, send 1.23 FIL:

$to = 'f1saxri7cpyz2cm767q77u3mqumrggljrmi5iqdtycow; / / transfer destination address $amount =' 12300000000000000000000; / / the number of transfers in the smallest unit, 1 FIL = 10 ^ 18 UNIT$cid = $kit- > transfer ($to,$amount); / / submit the Trx transfer transaction echo 'txid = >'. $cid- > {'/'}. PHP_EOL; / / Show transaction ID

Note:

The amount of transfer should be converted to an integer string of the smallest unit measurement, 1 FIL = 10 ^ 18 minimum unit.

Various types of receive addresses are supported, such as:

F01729:ID address:

F17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy:SECP256K1 address

F24vg6ut43yw2h3jqydgbg2xq7x6f4kub3bg6as6i:ACTOR address

F3q22fijmmlckhl56rn5nkyamkph4mcfu5ed6dheq53:BLS address

2.3 wait for confirmation of Filecoin message

Use the waitForReceipt () method of FilKit to wait for confirmation of the deal, for example:

$receipt = $kit- > waitForReceipt ($cid); / / waiting for message receipt echo 'exit code = >'. $receipt- > ExitCode. PHP_EOL; / / displays the message execution result code. 0 indicates success.

The default wait time is 60 seconds, during which time the transaction receipt will indicate an error. You can change this default setting by passing in a second parameter. For example, wait 10 seconds:

$receipt = $kit- > waitForReceipt ($cid, 10); / / wait for 10 seconds to query the FIL balance of the specified address

Use the getBalance () method to query the FIL balance at the specified address, for example:

$addr = 'f1saxri7cpyz2cm767q77u3mqumrggljrmi5iqdtychild; / / the Filecoin address to be queried $balance = $kit- > getBlanace ($addr); / / query the FIL balance, the smallest unit represents echo' balance = >'. $balance. PHP_EOL; / / Show FIL balance

Note: the balance returned is expressed in the smallest unit, 1 FIL = 10 ^ 18 minimum unit.

2.5 using the RPC client

The Filecoin node provides a lot of useful functionality through its RPC API interface, and all Filecoin RPC API can be accessed using the RpcClient class of the Filecoin.PHP package.

For example, query the current chain header TipSet, and the corresponding RPC API is Filecoin.ChainHead. The calling code for using RpcClient object is as follows:

/ / $client = $kit- > getClient (); / / get a RpcClient instance from FilKit / / or / / or / / $client = new Client ('http://127.0.0.1:1234/rpc/v0'); / / create a separate RpcClient object $ret = $client- > chainHead () / / call Filecoin.ChainHead APIecho 'height = >'. $ret- > Height. PHP_EOL; / / displays the value of the Height field

It is easy to understand from the above code:

The RPC API name of Filecoin removes Filecoin. Prefix, and then lowercase the first character of the rest to get the method name of RpcClient.

If the params array of RPC API contains multiple parameters, then pass in the corresponding method of RpcClient in turn. For example, when you use Filecoin.ChainGetBlock to query a specified chunk, you need to pass in the CID of the chunk. The call using RpcClient is as follows:

$cid = [/ / CID'/'= > 'bafy2bzacea3wsdh7y3a36tb3skempjoxqpuyompjbmfeyf34fi3uy6uue42v4'] of the block to be queried; $block = $client- > chainGetBlock ($cid); / / call Filecoin.ChainGetBlock API echo' miner = >'. $block- > Miner. PHP_EOL; / / shows the address of the miners in this block. I believe you have a better understanding of "how to use the Filecoin.PHP package". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report