進(jìn)入go-ethereum的代碼目錄
1.1.初始化節(jié)點(diǎn)
創(chuàng)建文件夾dev_data, 子文件夾node1 和文件genesis.json
終端執(zhí)行: ./build/bin/geth --datadir ./dev_data/node1/ init ./dev_data/genesis.json
1.2.啟動(dòng)控制臺(tái)
終端執(zhí)行: ./build/bin/geth --datadir dev_data/node1 --networkid 314590 --ipcdisable --port 61910 --rpcport 8200 console
2.1./go-ethereum/internal/ethapi/api.go
func (s *PrivateAccountAPI) NewAccount(password string) (common.Address, error) {
acc, err := fetchKeystore(s.am).NewAccount(password)
}
2.2.go-ethereum/accounts/keystore/keystore.go
func (ks *KeyStore) NewAccount(passphrase string) (accounts.Account, error) {
_, account, err := storeNewKey(ks.storage, crand.Reader, passphrase) //創(chuàng)建私鑰
// Add the account to the cache immediately rather than waiting for file system notifications to pick it up.
ks.cache.add(account) //accountCache的數(shù)組all: 根據(jù)URL從低到高排列 加入accountCache的map byAddr
ks.refreshWallets()
return account, nil
}
2.3.go-ethereum/accounts/keystore/key.go
func storeNewKey(ks keyStore, rand io.Reader, auth string) (*Key, accounts.Account, error) {
key, err := newKey(rand) //生成私鑰
a := accounts.Account{Address: key.Address,
URL: accounts.URL{Scheme: KeyStoreScheme, Path:ks.JoinPath(keyFileName(key.Address))}}
//將keystore寫入文件go-ethereum/dev_data/node1/
UTC--2018-05-09T09-22-01.825676850Z--d4c3b95bf0bbf0fd00870621
if err := ks.StoreKey(a.URL.Path, key, auth); err != nil { //存儲(chǔ)keystore文件
zeroKey(key.PrivateKey)
return nil, a, err
}
return key, a, err
}
2.4.生成私鑰 go-ethereum/accounts/keystore/key.go
func newKey(rand io.Reader) (*Key, error) {
privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), rand) //生成私鑰
return newKeyFromECDSA(privateKeyECDSA), nil
}
type PrivateKey struct {
PublicKey
D *big.Int //隨機(jī)數(shù)
}
2.5. 生成隨機(jī)數(shù) /usr/lib/go-1.10/src/crypto/ecdsa/ecdsa.go
// GenerateKey generates a public and private key pair.
func GenerateKey(c elliptic.Curve, rand io.Reader) (*PrivateKey, error) {
k, err := randFieldElement(c, rand) //所以以太坊的隨機(jī)數(shù)生成采用的是go的api?
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
}