Error sending signed transaction: rpc error: code = Internal desc = invalid sender

Hi everyone, can please help me check my code i getting this error but I cant found the root cause. for the privateKeyHex is correct because I got transfer to other wallet successfully but is using other module which is iotex.NewAuthedClient.Transfer.

func SendSignedTransaction2(privateKeyHex string) {
	// Recipient address
	recipientAddress := "io1d6gajpq9j06u45dpz6z3l2ty95l8scwal2azul"

	// Amount to transfer (in Rau)
	amount := big.NewInt(100000000000000000) // 0.1 IOTX

	// Gas limit and gas price
	gasLimit := uint64(21000)
	gasPrice := "2200000000000" // 2.2 Qev

	conn, err := iotex.NewDefaultGRPCConn(testnetRPC)
	if err != nil {
		log.Fatalf("connection error : %v", err)
	}
	defer conn.Close()

	client := iotexapi.NewAPIServiceClient(conn)

	// Your private key (for signing the transaction)
	// Create an account from the private key
	acc, err := account.HexStringToAccount(privateKeyHex)
	if err != nil {
		log.Fatalf("create account from private key error : %v", err)
	}

	senderAddress := acc.Address().String()
	// Fetch nonce for the sender
	accountResp, err := client.GetAccount(context.Background(), &iotexapi.GetAccountRequest{
		Address: senderAddress,
	})
	if err != nil {
		log.Fatalf("Failed to fetch account details: %v", err)
	}

	nonce := accountResp.GetAccountMeta().GetPendingNonce()

	// Create transfer action
	transfer := &iotextypes.Transfer{
		Amount:    amount.String(),
		Recipient: recipientAddress,
	}

	actionCore := &iotextypes.ActionCore{
		Nonce:    nonce,
		GasLimit: gasLimit,
		GasPrice: gasPrice,
		ChainID:  testnetChainID, // Testnet
		Action: &iotextypes.ActionCore_Transfer{
			Transfer: transfer,
		},
	}

	// Serialize the ActionCore and sign it
	rawTxBytes, err := proto.Marshal(actionCore)
	if err != nil {
		log.Fatalf("Error serializing action core: %v", err)
	}
	rawTxHash := sha256.Sum256(rawTxBytes)
	signedTx, err := acc.PrivateKey().Sign(rawTxHash[:])
	if err != nil {
		log.Fatalf("Error signing transaction: %v", err)
	}

	// Create the full action
	action := &iotextypes.Action{
		Core:         actionCore,
		SenderPubKey: acc.PublicKey().Bytes(),
		Signature:    signedTx,
	}

	fmt.Printf("Sender Address: %s\n", acc.Address().String())
	fmt.Printf("Public Key: %x\n", acc.PublicKey().Hash())
	fmt.Printf("Serialized ActionCore: %x\n", rawTxBytes)
	fmt.Printf("ActionCore Hash: %x\n", rawTxHash)
	fmt.Printf("Signature: %x\n", signedTx)
	fmt.Printf("Account Meta: %+v\n", accountResp.GetAccountMeta())
	fmt.Printf("Action: %+v\n", action)

	// Send the action
	resp, err := client.SendAction(context.Background(), &iotexapi.SendActionRequest{
		Action: action,
	})
	if err != nil {
		log.Fatalf("Error sending signed transaction: %v", err)
	}

	fmt.Println("Transaction sent successfully, action hash:", resp.ActionHash)
}

Hi Mammamia, does this mean that this feature is not working properly?

problem solved.
root cause: rawTxHash := sha256.Sum256(rawTxBytes) cannot use this module to convert it to 32 bytes.
solution: rawTxHash := hash.Hash256b(rawTxBytes)github.com/iotexproject/go-pkgs/hash

1 Like