kadena-php / client
用于与Kadena Pact API一起使用的PHP客户端
Requires
- ext-sodium: *
- nesbot/carbon: ^2.64
- paragonie/halite: ^5.1
- symfony/http-client: ^6.2
Requires (Dev)
- phpunit/phpunit: ~9.0
- roave/security-advisories: dev-latest
- symplify/easy-coding-standard: 11.1.33.72
This package is auto-updated.
Last update: 2024-09-10 22:51:40 UTC
README
此包包含一个易于使用的客户端,用于与Pact API通信,以及用于生成密钥、准备和创建Pact命令的类,以及在后端签名命令的能力。
使用此包,您可以通过创建和签名命令从PHP后端调用Pact中的admin函数,但它还允许您在后端创建命令,在前端由钱包签名,然后在后端调用Pact API。
如果您的用户需要签名一个命令,前端仍然需要类似Kadena.js的东西。这不是一个完全的替代品。
⚠️ 此包正在积极开发中,尚未发布稳定的生产版本
安装
通过Composer
composer require kadena-php/client
用法
密钥对
密钥对用于签名您的Pact命令。您可以使用以下方式生成新的密钥对
$keyPair = KeyFactory::generate();
元数据
发送到Kadena API的每个命令都需要一个元数据对象。此对象可以手动创建,或使用MetadataFactory
构建。如果未提供某些选项,工厂将设置预定义的默认值。默认值和选项如下
creationTime: Carbon::now(), ttl: 7200, gasLimit: 10000, chainId: '0', gasPrice: 1e-8, sender: ''
如果我们想在不同的链上创建具有默认选项的对象,我们可以这样做
$factory = new MetadataFactory(); $metadata = $factory->withOptions([ 'chainId' => '1', ])->make();
如果没有需要自定义的选项,只需调用$factory->make()
即可创建您的Metadata
对象。
签名者
在将命令发送到Kadena API之前,必须对命令进行签名。为此,需要创建一个或多个Signer
(签名者)。签名者由一个公钥和可选的权限列表组成。
让我们创建一个公钥为example-key
且具有coin.transfer
权限的签名者。由于签名者可以有多个或没有权限,所有Capability
对象都应该被包装在CapabilityCollection
对象中
$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) )
多个签名者可以包装在SignerCollection
对象中。
有效载荷
有效载荷是Pact将要执行代码。有两种类型的有效载荷:执行有效载荷和继续有效载荷。
$executePayload new ExecutePayload( code: '(+ 1 2)' ); $continuePayload = new ContinuePayload( pactId: 'pact-id', rollback: false, step: 0 );
命令
命令封装了发送到Kadena API的所有数据,可以手动创建Command
对象,但建议使用CommandFactory
。工厂将设置某些默认值,并可以这样使用
$factory = new CommandFactory(); $factory->withExecutePayload($executePayload) ->withMetadata($metadata) ->withSigners(new SignerCollection($signer)) ->withNetworkId('mainnet0') ->withNonce('nonce-string') ->make();
创建Command
对象时,始终需要withExecutePayload
或withContinuePayload
选项,但所有其他选项都是可选的。
签名命令
创建命令后,可以使用任意数量的密钥对进行签名。为此,首先从您拥有的密钥对中创建一个KeyPairCollection
。这些密钥对应于您账户中添加的签名者。
$kpc = new KeyPairCollection($keypair);
现在,使用这些密钥对,我们可以对之前创建的命令进行签名
$signedCommand = CommandSigner::sign($command, $kpc);
这返回一个新实例的SignedCommand
从字符串构造命令
除了在后端签名命令外,命令可能在其他地方(用户钱包)签名。可以使用以下方式使用有效的Pact命令字符串重新构造签名命令
$signedCommand = SignedCommandMapper::fromString($commandString)
签名命令也可以转换为字符串或数组
$commandString = SignedCommandMapper::toString($signedCommand); $commandArray = SignedCommandMapper::toArray($signedCommand);
使用客户端
现在我们已经了解了如何创建命令并签名它们,现在是时候使用它们来调用Pact API了。
首先,创建一个新的API客户端
$client = new \Kadena\Client('https://:8888'); // or whatever local config you have
客户端提供了一些方法,有关不同用例和预期响应的更多信息,请参阅Pact API文档。
local
接受一个SignedCommand
作为参数,并返回一个ResponseInterface
。
$local = $client->local($signedCommand);
send
接受一个包装在SignedCommandCollection
中的多个SignedCommand
作为参数,并返回一个RequestKeyCollection
。
$send = $client->send(new SignedCommandCollection($signedCommand));
listen
接受一个RequestKey
作为参数,并返回一个ResponseInterface
。
$requestKey = $send->first(); // Get a RequestKey from the send response above $listen = $client->single($requestKey);
poll
接受一个RequestKeyCollection
作为参数,并返回一个ResponseInterface
。
$requestKeyCollection = $send; // The send() method above returned a RequestKeyCollection $poll = $client->poll($requestKeyCollection);
spv
接受一个RequestKey
和一个string $targetChainId
作为参数,并返回一个string
。
$spv = $client->spv($requestKey, '2');
变更日志
请参阅变更日志以获取最近更改的更多信息。
测试
./vendor/bin/phpunit
贡献
有关详细信息和工作待办事项,请参阅contributing.md。
安全
如果您发现任何与安全相关的问题,请通过电子邮件而不是使用问题跟踪器来报告。
致谢
许可证
MIT。请参阅许可证文件以获取更多信息。