从命令行开始解析以太坊新建账户过程(geth account new命令)

从命令行开始解析以太坊新建账户过程(geth account new命令)

如上图是生成一个账户的过程,最后的账户表现为一个地址那么,这个过程的代码是怎么实现的呢,接下来我们从main函数命令行开始解析这个过程的代码。

在cmd/geth/main.go中的init函数中,有一个accountCommand的命令行参数

func init() {
  // Initialize the CLI app and start Geth
  app.Action = geth
  app.HideVersion = true // we have a command to print the version
  app.Copyright = "Copyright 2013-2018 The go-ethereum Authors"
  app.Commands = []cli.Command{
    // See chaincmd.go:
    initCommand,
    importCommand,
    exportCommand,
    importPreimagesCommand,
    exportPreimagesCommand,
    copydbCommand,
    removedbCommand,
    dumpCommand,
    // See monitorcmd.go:
    monitorCommand,
    // See accountcmd.go:
    accountCommand,
    walletCommand,
    // See consolecmd.go:
    consoleCommand,
    attachCommand,
    javascriptCommand,
    // See misccmd.go:
    makecacheCommand,
    makedagCommand,
    versionCommand,
    bugCommand,
    licenseCommand,
    // See config.go
    dumpConfigCommand,
  }
  sort.Sort(cli.CommandsByName(app.Commands))

  app.Flags = append(app.Flags, nodeFlags...)
  app.Flags = append(app.Flags, rpcFlags...)
  app.Flags = append(app.Flags, consoleFlags...)
  app.Flags = append(app.Flags, debug.Flags...)
  app.Flags = append(app.Flags, whisperFlags...)

  app.Before = func(ctx *cli.Context) error {
    runtime.GOMAXPROCS(runtime.NumCPU())
    if err := debug.Setup(ctx); err != nil {
      return err
    }
    // Start system runtime metrics collection
    go metrics.CollectProcessMetrics(3 * time.Second)

    utils.SetupNetwork(ctx)
    return nil
  }

  app.After = func(ctx *cli.Context) error {
    debug.Exit()
    console.Stdin.Close() // Resets terminal mode.
    return nil
  }
}

账户相关的命令在cmd/geth/accountcmd.go里,新建账户命令为new,其会调accountCreate方法,我们继续看:

accountCommand = cli.Command{
	Name:     "account",
	Usage:    "Manage accounts",
	Category: "ACCOUNT COMMANDS",  

子命令:

{
        Name:   "new",
        Usage:  "Create a new account",
        Action: utils.MigrateFlags(accountCreate),
        Flags: []cli.Flag{
          utils.DataDirFlag,
          utils.KeyStoreDirFlag,
          utils.PasswordFileFlag,
          utils.LightKDFFlag,
        },
        Description: `
    geth account new

Creates a new account and prints the address.

The account is saved in encrypted format, you are prompted for a passphrase.

You must remember this passphrase to unlock your account in the future.

For non-interactive use the passphrase can be specified with the --password flag:

Note, this is meant to be used for testing only, it is a bad idea to save your
password to file or expose in any other way.
`,
      }

账户相关的命令在cmd/geth/accountcmd.go里,新建账户命令为new,其会调accountCreate方法,accountCreate在CLI标志定义的密钥库中创建一个新帐户

func accountCreate(ctx *cli.Context) error {
  cfg := gethConfig{Node: defaultNodeConfig()}
  // Load config file.
  if file := ctx.GlobalString(configFileFlag.Name); file != "" {
    if err := loadConfig(file, &cfg); err != nil {
      utils.Fatalf("%v", err)
    }
  }
  utils.SetNodeConfig(ctx, &cfg.Node)
  scryptN, scryptP, keydir, err := cfg.Node.AccountConfig()

  if err != nil {
    utils.Fatalf("Failed to read configuration: %v", err)
  }

  password := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))

  address, err := keystore.StoreKey(keydir, password, scryptN, scryptP)

  if err != nil {
    utils.Fatalf("Failed to create account: %v", err)
  }
  fmt.Printf("Address: {%x}\n", address)
  return nil
}

获取配置并解析用户的密码, StoreKey生成一个密钥,用’auth’加密并存储在给定的目录中

func StoreKey(dir, auth string, scryptN, scryptP int) (common.Address, error) {
  _, a, err := storeNewKey(&keyStorePassphrase{dir, scryptN, scryptP}, crand.Reader, auth)
  return a.Address, err
}

storeNewKey()来创建一个新的账户,具体表现为生成一对公私钥,再由私钥算出地址并构建一个自定义的Key

func storeNewKey(ks keyStore, rand io.Reader, auth string) (*Key, accounts.Account, error) {
  key, err := newKey(rand)
  if err != nil {
    return nil, accounts.Account{}, err
  }
  a := accounts.Account{Address: key.Address, URL: accounts.URL{Scheme: KeyStoreScheme, Path: ks.JoinPath(keyFileName(key.Address))}}
  if err := ks.StoreKey(a.URL.Path, key, auth); err != nil {
    zeroKey(key.PrivateKey)
    return nil, a, err
  }
  return key, a, err
}

Key的生成函数,通过椭圆曲线加密生成的私钥,生成Key

func newKey(rand io.Reader) (*Key, error) {
  privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), rand)
  if err != nil {
    return nil, err
  }
  return newKeyFromECDSA(privateKeyECDSA), nil
}

生成公钥和私钥对,ecdsa.GenerateKey(crypto.S256(), rand) 以太坊采用了椭圆曲线数字签名算法(ECDSA)生成一对公私钥,并选择的是secp256k1曲线

func GenerateKey(c elliptic.Curve, rand io.Reader) (*PrivateKey, error) {
  k, err := randFieldElement(c, rand)
  if err != nil {
    return nil, err
  }

  priv := new(PrivateKey)
  priv.PublicKey.Curve = c
  priv.D = k
  priv.PublicKey.X, priv.PublicKey.Y = c.ScalarBaseMult(k.Bytes())
  return priv, nil
}

以太坊使用私钥通过 ECDSA算法推导出公钥,继而经过 Keccak-256 单向散列函数推导出地址

func newKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key {           
  id := uuid.NewRandom()                                               
  key := &Key{                                                         
    Id:         id,                                                  
    Address:    crypto.PubkeyToAddress(privateKeyECDSA.PublicKey),   
    PrivateKey: privateKeyECDSA,                                     
  }                                                                    
  return key                                                           
}                                                                        

func PubkeyToAddress(p ecdsa.PublicKey) common.Address {
  pubBytes := FromECDSAPub(&p)
  return common.BytesToAddress(Keccak256(pubBytes[1:])[12:])
}

地址代表以太坊帐户的20字节地址

const (
  HashLength    = 32
  AddressLength = 20
)

type Address [AddressLength]byte

func BytesToAddress(b []byte) Address {
  var a Address
  a.SetBytes(b)
  return a
}
func BigToAddress(b *big.Int) Address { return BytesToAddress(b.Bytes()) }
func HexToAddress(s string) Address   { return BytesToAddress(FromHex(s)) }

总结,整个过程如下:

  • 创建随机私钥 (64 位 16 进制字符 / 32 字节)
  • 从私钥推导出公钥 (128 位 16 进制字符 / 64 字节)
  • 从公钥推导出地址 (40 位 16 进制字符 / 20 字节)

GitHub地址:

https://github.com/guoshijiang/go-ethereum-code-analysis
关注问我技术公众号,加小问,拉您入技术交流群:
从命令行开始解析以太坊新建账户过程(geth account new命令)_第1张图片

你可能感兴趣的:(etherenum,区块链技术)