gmajor/substrate-codec-php
Composer 安装命令:
composer require gmajor/substrate-codec-php
包简介
PHP SCALE Codec for Substrate - v2.0
README 文档
README
PHP implementation of the SCALE (Simple Concatenated Aggregate Little-Endian) codec used in Substrate-based blockchains like Polkadot, Kusama, and Substrate-native chains.
Features
- ✅ Full SCALE codec implementation
- ✅ All primitive types (U8-U128, I8-I128, Bool, String)
- ✅ Compact integer encoding
- ✅ Compound types (Vec, Option, Tuple, Struct, Enum)
- ✅ Metadata v12-v15 support
- ✅ Extrinsic building and signing
- ✅ Event parsing
- ✅ polkadot.js compatible
Installation
composer require gmajor/substrate-codec-php
Requirements
- PHP 8.2+
- ext-gmp (for large integer handling)
- ext-json
- ext-sodium
Quick Start
Basic Encoding/Decoding
<?php require_once 'vendor/autoload.php'; use Substrate\ScaleCodec\Types\TypeRegistry; use Substrate\ScaleCodec\Bytes\ScaleBytes; // Create type registry $registry = new TypeRegistry(); // Encode a U32 value $u32 = $registry->get('U32'); $encoded = $u32->encode(12345); echo $encoded->toHex(); // 0x39300000 // Decode it back $bytes = ScaleBytes::fromHex('0x39300000'); $decoded = $u32->decode($bytes); echo $decoded; // 12345
Compact Integers
$compact = $registry->get('Compact'); // Small values (0-63): 1 byte echo $compact->encode(42)->toHex(); // 0xa8 // Medium values (64-16383): 2 bytes echo $compact->encode(100)->toHex(); // 0x9101 // Large values echo $compact->encode(1000000)->toHex(); // 0x02c0843d00
Boolean
$bool = $registry->get('Bool'); $encoded = $bool->encode(true); // 0x01 $encoded = $bool->encode(false); // 0x00
String/Text
$string = $registry->get('String'); $encoded = $string->encode('Hello, Substrate!'); // 0x2048656c6c6f2c2053756273747261746521
Vectors
// Vec<U8> $vecU8 = (new VecType($registry))->setElementType($registry->get('U8')); $encoded = $vecU8->encode([1, 2, 3, 4, 5]); // 0x140102030405 $decoded = $vecU8->decode(ScaleBytes::fromHex('0x140102030405')); // [1, 2, 3, 4, 5]
Options
$optionU32 = (new OptionType($registry))->setInnerType($registry->get('U32')); // Some value $encoded = $optionU32->encode(42); // 0x012a000000 // None value $encoded = $optionU32->encode(null); // 0x00
Structs
$struct = (new StructType($registry))->setFields([ 'id' => $registry->get('U32'), 'name' => $registry->get('String'), 'active' => $registry->get('Bool'), ]); $encoded = $struct->encode([ 'id' => 1, 'name' => 'Alice', 'active' => true, ]);
Advanced Usage
Metadata Parsing
use Substrate\ScaleCodec\Metadata\MetadataParser; $parser = new MetadataParser(); $metadata = $parser->parse($metadataHex); // Access pallets $systemPallet = $metadata->getPallet('System'); // Get events $events = $metadata->getPalletEvents('Balances');
Extrinsic Building
use Substrate\ScaleCodec\Extrinsic\ExtrinsicBuilder; $builder = new ExtrinsicBuilder($registry); $extrinsic = $builder ->setVersion(4) ->setPallet('Balances') ->setFunction('transfer') ->setArgs([ 'dest' => $accountId, 'value' => 1000000000, ]) ->sign($keypair) ->build();
Working with Large Integers
U64 and U128 return strings for values exceeding PHP_INT_MAX:
$u64 = $registry->get('U64'); // Max U64 value $encoded = $u64->encode('18446744073709551615'); $decoded = $u64->decode($bytes); echo $decoded; // '18446744073709551615' (string)
Documentation
- API Reference - Complete API documentation
- Types Reference - Detailed type documentation
- Static Analysis - PHPStan configuration
Testing
# Run unit tests make test # Run with coverage make coverage # Run compatibility tests make compat # Run static analysis make stan # Run code style check make sniff
Compatibility
This library maintains compatibility with polkadot.js SCALE codec. Run compatibility tests:
php compat/tests/php-compatibility-test.php
Benchmarks
make bench # Standard benchmarks make bench-quick # Quick benchmarks make bench-full # Full benchmarks
Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Run tests:
make test - Run static analysis:
make stan - Run code style:
make sniff - Commit changes:
git commit -am "feat: my feature" - Push and create a PR
License
MIT License. See LICENSE for details.
Related
gmajor/substrate-codec-php 适用场景与选型建议
gmajor/substrate-codec-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.69k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2020 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「blockchain」 「scale」 「codec」 「Substrate」 「polkadot」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gmajor/substrate-codec-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gmajor/substrate-codec-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gmajor/substrate-codec-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The BlockTrail PHP SDK, for integration of Bitcoin functionality through the BlockTrail API
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
Solana PHP SDK for interacting with the Solana blockchain
A fast and dynamic Merkle tree implementation
Binary codec for encoding multiple structures with predefined config
Multichain JSON-RPC Client
统计信息
- 总下载量: 21.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 9
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-04-13