kadena-php/client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kadena-php/client

Composer 安装命令:

composer require kadena-php/client

包简介

PHP Client for use with the Kadena Pact API

关键字:

README 文档

README

Latest Version on Packagist Total Downloads

This package includes a simple-to-use client to communicate with the Pact API, as well as classes to generate keys, prepare and create Pact commands and the ability to sign commands in your backend.

Using this package allows you to call things like admin functions in Pact from your PHP backend by creating and signing commands, but it also allows you to create a command in the backend, have a wallet sign it on the frontend, and then call the Pact API in the backend.

If your users have to sign a command, something like Kadena.js would still be required on the frontend. This is not a complete replacement.

⚠️ This package is under active development and has no stable production release yet

Installation

Via Composer

composer require kadena-php/client

Usage

Key Pairs

Key Pairs are used to sign your Pact commands. You can generate a new KeyPair using

$keyPair = KeyFactory::generate();

Metadata

Every command sent to the Kadena API requires a metadata object to be present. This object can be created manually, or be constructed using the MetadataFactory. The factory will set predefined defaults if certain options are no provided. The defaults and options are as follows:

creationTime: Carbon::now(),
ttl: 7200,
gasLimit: 10000,
chainId: '0',
gasPrice: 1e-8,
sender: ''

If we want to create an object with the default options but on a different chain, we can do it like this:

$factory = new MetadataFactory();
$metadata = $factory->withOptions([
    'chainId' => '1',
])->make();

If no custom options are required, you can just call $factory->make() to create your Metadata object.

Signers

Commands have to be signed before sending them to the Kadena API. To support this, a Signer (one or many) has to be created. A signer consists of a public key and optionally a list of capabilities.

Let's create a signer with a public key of example-key and the coin.transfer capability. As a signer can have multiple or no capabilities, all Capability objects should be wrapped in a CapabilityCollection object:

$publicKey = KeyFactory::publicKeyFromHex('not-a-real-key');

$transferCapability = new Capability(
    name: 'coin.transfer',
    arguments: [
        'address-from',
        'address-to',
        5
    ]    
);

$signer = new Signer(
    publicKey: $publicKey,
    capabilities: new CapabilityCollection($transferCapability) 
)

Multiple signers can be wrapped in the SignerCollection object.

Payloads

Payloads are the code to be executed by pact. There are two types of payloads: and execute and a continue payload.

$executePayload new ExecutePayload(
    code: '(+ 1 2)'
);

$continuePayload =  new ContinuePayload(
    pactId: 'pact-id',
    rollback: false,
    step: 0
);

Commands

Commands wrap all data sent to the Kadena API, a Command object can be created manually, but it is recommended to use the CommandFactory for this. The factory will set certain defaults, and can be used like this:

$factory = new CommandFactory();

$factory->withExecutePayload($executePayload)
    ->withMetadata($metadata)
    ->withSigners(new SignerCollection($signer))
    ->withNetworkId('mainnet0')
    ->withNonce('nonce-string')
    ->make();

The withExecutePayload or the withContinuePayload options are always required to create a Command object, but all others are optional.

Signing Commands

After creating a command, you can sign it using any number of key pairs. To do this, first, create a KeyPairCollection from the key pairs you have. These key pairs should correspond to the signers you added to your account.

$kpc = new KeyPairCollection($keypair);

Now using these key pairs, we can sign the previously created command

$signedCommand = CommandSigner::sign($command, $kpc);

This returns a new instance of a SignedCommand

Constructing commands from a string

Instead of signing the command in the backend, a command might be signed elsewhere (user wallet). A signed command can be reconstructed from a valid Pact command string using:

$signedCommand = SignedCommandMapper::fromString($commandString)

A signed command can also be cast to a string or an array using

$commandString = SignedCommandMapper::toString($signedCommand);
$commandArray = SignedCommandMapper::toArray($signedCommand);

Using the Client

Now we have figured out how to create commands and sign them, it's time to use them to make calls to the Pact API.

First, create a new API client:

$client = new \Kadena\Client('http://localhost:8888'); // or whatever local config you have

The client has a few methods available, see the Pact API docs for more information on the different use-cases and expected responses.

local

Takes a single SignedCommand as a parameter and returns a ResponseInterface.

$local = $client->local($signedCommand);

send

Takes a multiple SignedCommand wrapped in a SignedCommandCollection as a parameter and returns a RequestKeyCollection.

$send = $client->send(new SignedCommandCollection($signedCommand));

listen

Takes a single RequestKey as a parameter and returns a ResponseInterface.

$requestKey = $send->first(); // Get a RequestKey from the send response above
$listen = $client->single($requestKey);

poll

Takes a RequestKeyCollection as a parameter and returns a ResponseInterface.

$requestKeyCollection = $send; // The send() method above returned a RequestKeyCollection
$poll = $client->poll($requestKeyCollection);

spv

Takes a RequestKey and a string $targetChainId as parameters and returns a string.

$spv = $client->spv($requestKey, '2');

Change log

Please see the changelog for more information on what has changed recently.

Testing

./vendor/bin/phpunit

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security-related issues, please send an email instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.

kadena-php/client 适用场景与选型建议

kadena-php/client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 01 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「Kadena」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kadena-php/client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kadena-php/client 我们能提供哪些服务?
定制开发 / 二次开发

基于 kadena-php/client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 44
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 16
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-05