使用Indy Plenum的DidSigner生成DID与VerKey时长度不一致问题
Indy Plenum中DidSigner生成DID/VerKey长度不一致问题的解决方案
问题原因
Indy Plenum的DidSigner生成DID和VerKey的逻辑依赖于Base58编码:
- DID是Ed25519公钥前16字节的Base58编码,理论长度应为22位;
- VerKey是完整Ed25519公钥的Base58编码(前缀加
~),理论长度应为23位。
但Base58编码的长度并非固定值——当输入的字节序列数值较低(比如包含较多前导零或整体数值偏小)时,编码后的字符数会比预期少一位,导致生成21位DID或22位VerKey,触发Indy节点的长度校验报错。
解决方案
通过补位处理强制生成固定长度的DID和VerKey:Base58中的字符1对应字节0,在编码结果前补1不会改变原始数据的语义,同时能保证长度符合要求。
修改后的代码如下:
import sys import argparse from plenum.common.signer_did import DidSigner def generate_did_verkey(seed: str): """ Generate DID and verification key using the provided seed, ensuring fixed lengths. Args: seed (str): A 32-character long seed value. Returns: tuple: A tuple containing the fixed-length DID and verification key. """ if len(seed) != 32: raise ValueError("The seed must be exactly 32 characters long.") seed_value = seed.encode() signer = DidSigner(seed=seed_value) # 确保DID长度为22位 did = signer.identifier if len(did) < 22: did = '1' * (22 - len(did)) + did # 确保VerKey长度为23位(包含~前缀) verkey = signer.verkey if len(verkey) < 23: verkey_body = verkey[1:] verkey_body = '1' * (22 - len(verkey_body)) + verkey_body verkey = f'~{verkey_body}' return did, verkey
额外建议
- 检查当前使用的Indy Plenum版本,若使用较旧版本,建议升级到最新稳定版——部分后续版本已修复了Base58编码长度不一致的底层问题;
- 生成种子时可避免使用易导致低数值字节序列的组合(如大量前导数字或小写字母),但补位处理仍是最可靠的兜底方案。
内容的提问来源于stack exchange,提问作者brenodacosta
相关产品推荐
相关产品推荐

