Hi Guest

More contents, please log on!

Bitmere.com 区块链技术 Content
比原项目仓库:
Github地址:https://github.com/Bytom/bytom
Gitee地址:https://gitee.com/BytomBlockchain/bytom
背景知识
Bytom 使用的 密钥类型为基于 ed25519 的 chainkd.XPub
  • 代码见 bytom/crypto/ed25519/chainkd
  • 文档见 https://chain.com/docs/1.2/protocol/specifications/chainkd`

    预备
    代码修改
    首先适当修改代码,添加一些打印输出,使得我们更好的进行验证。 如果只是想看 xpub ,而不关心 xprv(对于验证导入导出助记词功能来说已经够了)的话可以跳过这一步,不必修改源码
    在 bytom/blockchain/pseudohsm/pseudohsm.go 中的
    func (h *HSM) createKeyFromMnemonic(alias string, auth string, mnemonic string) (*XPub, error)
    添加输出打印 root XPub 对应的 私钥 和 公钥
    func (h *HSM) createKeyFromMnemonic(alias string, auth string, mnemonic string) (*XPub, error) {
        // Generate a Bip32 HD wallet for the mnemonic and a user supplied password
        seed := mnem.NewSeed(mnemonic, "")
        xprv, xpub, err := chainkd.NewXKeys(bytes.NewBuffer(seed))
        if err != nil {
            return nil, err
        }
            
        fmt.Println(hex.EncodeToString(xprv[:])) // Add info printing
        fmt.Println(hex.EncodeToString(xpub[:])) // Add info printing
        id := uuid.NewRandom()
        key := &XKey{
            ID:      id,
            KeyType: "bytom_kd",
            XPub:    xpub,
            XPrv:    xprv,
            Alias:   alias,
        }
        file := h.keyStore.JoinPath(keyFileName(key.ID.String()))
        if err := h.keyStore.StoreKey(file, key, auth); err != nil {
            return nil, errors.Wrap(err, "storing keys")
        }
        return &XPub{XPub: xpub, Alias: alias, File: file}, nil
    }
    工具准备
    因为 bytomd 目前 dashboard 钱包图形界面还没有助记词相关功能,我们需要准备工具使用 POST 请求来使用。比如 curl 或者 postman
    获取密钥对应助记词
    dashboard 目前还没有 从 密钥导出助记词的功能,现有只是在创建密钥时有助记词相应输出信息。可以修改源码在查看密钥时 打印对应助记词。
    因为只是测试,这里为了方便,直接查看在创建密钥时返回的助记词。
    对 bytomd 发起 post 请求 /create-key
    {
      "alias": "create_key_test",
      "password": "createkeytest",
      "language": "en"
    }

    可以看到 响应中 显示了 xpub 和 助记词
    由于修改了源码,添加了打印信息,bytomd 命令行也打印出了相印的 xprv 和 xpub

    步骤
    如何通过导入助记词恢复密钥呢?
    假设目前 已经有一个 xprv
    50db5bfe21b08462972eadbce08ec92d078a45fa7a280d175a823f9e457faf447d1f501b69f895b830138fabc6f91e2b3b3c8df26642a34be4af27886b9134dc
    对应的 助记词为
    pudding
    room
    business
    river
    pattern
    box
    snap
    merit
    unfold
    speak
    hat
    task
    发起 post 请求 /create-key
    {
      "alias": "nnemonic_test",
      "password": "nnemonicnnemonic",
      "nnemonic": "pudding room business river pattern box snap merit unfold speak hat task",
      "language": "en"
    }
    返回相应:

    bytomd 输出

    可以看到,恢复出来的 xprv 和 我们本来的 xprv 一致,验证成功。
    dashaboard 中也能见到我们恢复的 密钥。
  • BitMere.com is Information release platform,just provides information storage space services.
    The opinions expressed are solely those of the author,Does not constitute advice, please treat with caution.
    You have to log in before you can reply Login | 立即注册

    Points Rules

    Write the first review

    用香烟做的云 小学生
    • Follow

      0

    • Following

      0

    • Articles

      2

    Promoted