wealdtech / go-eth2-util

Apache License 2.0
0 stars 4 forks source link

panic: invalid private key: err blsSecretKeyDeserialize #3

Closed gg718 closed 11 months ago

gg718 commented 11 months ago

I can't figure out why the following is panicking.

I generated a throwaway mnemonic for this example:

package main

import (
  "fmt"

  "github.com/wealdtech/go-ed25519hd"
  util "github.com/wealdtech/go-eth2-util"
)

func main() {
  mnemonic := "olympic salt stereo force iron tornado pilot inhale mandate banner play review auto tape lion absorb print nature element timber float identify world satoshi"

  seed, err := ed25519hd.SeedFromMnemonic(mnemonic, "")
  if err != nil {
    panic(err)
  }

  sk, err := util.PrivateKeyFromSeedAndPath(seed, "m/12381/3600/0/0/0")
  if err != nil {
    panic(err)
  }

  fmt.Printf("%x\n", sk.Marshal())
}

Output:

panic: invalid private key: err blsSecretKeyDeserialize 15db0f933b9c0f77602fbc57845810427a6d7bc9b79673bb213bc8cfda7ff690

But the key in the error message seems valid, so I'm not sure what the issue is?

mcdee commented 11 months ago

You must initialize the BLS system before using it.

package main

import (
    "fmt"

    "github.com/wealdtech/go-ed25519hd"
    e2types "github.com/wealdtech/go-eth2-types/v2"
    util "github.com/wealdtech/go-eth2-util"
)

func main() {
    mnemonic := "olympic salt stereo force iron tornado pilot inhale mandate banner play review auto tape lion absorb print nature element timber float identify world satoshi"

    seed, err := ed25519hd.SeedFromMnemonic(mnemonic, "")
    if err != nil {
        panic(err)
    }

    if err := e2types.InitBLS(); err != nil {
        panic(err)
    }

    sk, err := util.PrivateKeyFromSeedAndPath(seed, "m/12381/3600/0/0/0")
    if err != nil {
        panic(err)
    }

    fmt.Printf("%x\n", sk.Marshal())
}
gg718 commented 11 months ago

My bad. All good now, thanks a lot!