nyra/bip39 问题修复 & 功能扩展

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

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

nyra/bip39

Composer 安装命令:

composer require nyra/bip39

包简介

An implementation of BIP-39 (Deterministic Mnemonic Code Words) in PHP

README 文档

README

PHP implementation of BIP-0039 supporting:

  • Generate mnemonic (12–24 words) from secure random entropy
  • Convert provided entropy (hex) to mnemonic
  • Convert mnemonic back to original entropy (checksum verified)
  • Derive seed (512-bit) from mnemonic + optional passphrase (PBKDF2-HMAC-SHA512 / 2048 rounds)
  • Validate mnemonics
  • All official BIP-39 wordlists (English, Chinese (Simplified & Traditional), Czech, French, Italian, Japanese, Korean, Portuguese, Spanish)
  • Unicode NFKD normalization (when ext-intl is available)

Installation

composer require nyra/bip39

(For development / running tests clone the repo and run composer install.)

Optional: install ext-intl for full Unicode NFKD normalization; a graceful fallback is used if missing.

Usage

<?php
require __DIR__ . '/vendor/autoload.php';

use Nyra\Bip39\Bip39;

// 1. Generate a random 12-word mnemonic (128 bits entropy, English)
$mnemonic = Bip39::generateMnemonic();

// 2. Generate with different entropy (e.g. 256 bits => 24 words)
$mnemonic24 = Bip39::generateMnemonic(256);

// 3. Convert existing entropy (hex) to mnemonic
$entropyHex = '00000000000000000000000000000000';
$mnemonicFromEntropy = Bip39::entropyToMnemonic($entropyHex);

// 4. Convert mnemonic back to entropy
$roundTripEntropy = Bip39::mnemonicToEntropy($mnemonicFromEntropy); // == $entropyHex

// 5. Derive seed (binary or hex) with passphrase (e.g. official test vectors use "TREZOR")
$seedBin = Bip39::mnemonicToSeed($mnemonic, 'TREZOR');
$seedHex = Bip39::mnemonicToSeedHex($mnemonic, 'TREZOR');

// 6. Validate mnemonic
if (!Bip39::isValidMnemonic($mnemonic)) {
    throw new RuntimeException('Invalid mnemonic');
}

// 7. List supported languages
$languages = Bip39::languages(); // [ 'chinese_simplified', 'chinese_traditional', 'czech', ... ]

// 8. Generate mnemonics in other languages
$frenchMnemonic = Bip39::generateMnemonic(128, 'french');
$koreanMnemonic = Bip39::generateMnemonic(256, 'korean');

// 9. Work with Japanese (uses full-width ideographic separator U+3000 between words)
$japaneseEntropy = '00000000000000000000000000000000';
$japaneseMnemonic = Bip39::entropyToMnemonic($japaneseEntropy, 'japanese');
// Example (fragment): "あいこくしん あいこくしん ..."
$back = Bip39::mnemonicToEntropy($japaneseMnemonic, 'japanese');

Multi-language Notes

  • Pass the language identifier as the second argument to generateMnemonic, entropyToMnemonic, mnemonicToEntropy, mnemonicToSeed, and mnemonicToSeedHex.
  • Language identifiers correspond to the .txt filenames in resources/wordlist/ (without extension).
  • Japanese mnemonics are serialized with an ideographic space (U+3000) between words per BIP-39. This library:
    • Uses the proper full-width space when generating Japanese mnemonics.
    • Accepts input containing only ASCII spaces OR only full-width spaces, but rejects mixed usage to avoid ambiguous normalization.
    • Normalizes ASCII spaces to the full-width separator internally.
  • All other languages use a standard ASCII space.

API Summary

Method Description
Bip39::generateMnemonic(int $entropyBits = 128, string $language = 'english') Secure random mnemonic (entropy bits: 128,160,192,224,256) in given language
Bip39::entropyToMnemonic(string $entropyHex, string $language = 'english') Hex entropy to mnemonic
Bip39::mnemonicToEntropy(string $mnemonic, string $language = 'english') Mnemonic back to entropy hex (validates checksum)
Bip39::mnemonicToSeed(string $mnemonic, string $passphrase = '', string $lang='english') 64-byte binary seed
Bip39::mnemonicToSeedHex(string $mnemonic, string $passphrase = '', string $lang='english') Seed as hex
Bip39::isValidMnemonic(string $mnemonic, string $language = 'english') Quick validation helper
Bip39::languages(): array List of supported language identifiers

Testing

composer install
composer test

Security Notes

  • Only the checksum distinguishes valid from invalid mnemonics with same word count; always validate before using.
  • A wrong passphrase yields a different (valid) seed (plausible deniability).
  • Keep entropy and mnemonic generation on secure / trusted systems.

Normalization & Unicode

If ext-intl is installed, both mnemonic and passphrase are normalized with Unicode NFKD before seed derivation. This ensures canonically equivalent sequences (e.g. composed vs decomposed accents) produce identical seeds.

License

MIT

Acknowledgements

Wordlists & reference vectors: Trezor team & BIP authors. This implementation reuses the publicly specified word lists ( MIT License).

nyra/bip39 适用场景与选型建议

nyra/bip39 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 76 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 nyra/bip39 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-22