0

I use go-ethereum.Error: nonce generation function failed or private key is invalid

[Execution environment]
・node:11.6.0-alpine ・ganache-cli@6.3.0

Please tell the person who understands the cause.

client, err := ethclient.Dial("http://localhost:8545")
if err != nil {
  fmt.Println("error: client.Dial(http://localhost:8545)")
  log.Fatal(err)
}

privateKey, err := crypto.HexToECDSA("ed19d0e3fc1e8d3bb92389bf993943949c6c96f17f4bf506bb0b5c5194ee780b")
if err != nil {
  fmt.Println("error: crypto.HexToECDSA")
  log.Fatal(err)
}

publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
  fmt.Println("error: publicKey.(*ecdsa.PublicKey)")
  log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}

fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
  fmt.Println("error: client.PendingNonceAt(context.Background(), fromAddress)")
  log.Fatal(err)
}

value := big.NewInt(0)
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
  fmt.Println("error: client.SuggestGasPrice(context.Background())")
  log.Fatal(err)
}

toAddress := common.HexToAddress("0x9e26730bf972d6ae8840b8f6003a61f57e25ff92")
tokenAddress := common.HexToAddress("0x213C11560828125125844A1942B26A29A98cfDBf")

transferFromFnSignature := []byte("transferFrom(address,address,uint256)")
hash := sha3.NewLegacyKeccak256()
hash.Write(transferFromFnSignature)
methodID := hash.Sum(nil)[:4]
fmt.Println(hexutil.Encode(methodID))

paddedFromAddress := common.LeftPadBytes(fromAddress.Bytes(), 32)
fmt.Println(hexutil.Encode(paddedFromAddress))

paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32)
fmt.Println(hexutil.Encode(paddedAddress))

amount := new(big.Int)
amount.SetString("10", 10)

paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
fmt.Println(hexutil.Encode(paddedAmount))

var data []byte
data = append(data, methodID...)
data = append(data, paddedFromAddress...)
data = append(data, paddedAddress...)
data = append(data, paddedAmount...)

gasLimit, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
  To:   &tokenAddress,
  Data: data,
})
ganache_1    | Error: nonce generation function failed or private key is invalid
ganache_1    |     at Object.t.sign (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:52:261396)
ganache_1    |     at Object.sign (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:52:250488)
ganache_1    |     at Object.t.ecsign (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:2:17297)
ganache_1    |     at e.sign (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:47:108278)
ganache_1    |     at e.sign (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:10:85047)
ganache_1    |     at e.hash (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:52:418505)
ganache_1    |     at e.d [as hash] (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:10:82328)
ganache_1    |     at o.combine (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:10:81361)
ganache_1    |     at new o (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:10:81037)
ganache_1    |     at Function.o.fromResults (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:10:82120)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.