btcwallet icon indicating copy to clipboard operation
btcwallet copied to clipboard

btcwallet go client: received the unexpected content-type "text/plain;

Open huahuayu opened this issue 5 years ago • 9 comments

steps to reproduce:

1. as per the btcwallet readme , get btcd up

➜  ~ btcd -u rpcuser -P rpcpass
Error creating a default config file: open /root/sample-btcd.conf: no such file or directory
2018-09-30 18:36:17.968 [WRN] BTCD: open /root/.btcd/btcd.conf: no such file or directory
2018-09-30 18:36:17.969 [INF] BTCD: Version 0.12.0-beta
2018-09-30 18:36:17.969 [INF] BTCD: Loading block database from '/root/.btcd/data/mainnet/blocks_ffldb'
2018-09-30 18:36:17.988 [INF] BTCD: Block database loaded
2018-09-30 18:36:17.997 [INF] INDX: Committed filter index is enabled
2018-09-30 18:36:17.998 [INF] CHAN: Loading block index...
2018-09-30 18:36:20.094 [INF] CHAN: Chain state (height 237976, hash 000000000000003fb162b0205b02e0b79c0dede88248452266ce17d4c9a2ae76, totaltx 18443938, work 1293243324249034823140)
2018-09-30 18:36:20.104 [WRN] RPCS: Can't listen on [::1]:8334: listen tcp6 [::1]:8334: bind: cannot assign requested address
2018-09-30 18:36:20.104 [INF] RPCS: RPC server listening on 127.0.0.1:8334
2018-09-30 18:36:20.118 [INF] AMGR: Loaded 1935 addresses from file '/root/.btcd/data/mainnet/peers.json'
2018-09-30 18:36:20.118 [INF] CMGR: Server listening on [::]:8333
2018-09-30 18:36:20.118 [INF] CMGR: Server listening on 0.0.0.0:8333
2018-09-30 18:36:20.194 [INF] SYNC: New valid peer 45.77.41.19:8333 (outbound) (/Satoshi:0.16.2/)
2018-09-30 18:36:20.194 [INF] SYNC: Syncing to block height 543737 from peer 45.77.41.19:8333
2018-09-30 18:36:20.194 [INF] SYNC: Downloading headers for blocks 237977 to 250000 from peer 45.77.41.19:8333
2018-09-30 18:36:20.272 [INF] CMGR: 40 addresses found from DNS seed seed.bitcoin.sipa.be
...

2. get btcwallet up(I have created account before)

➜  ~ btcwallet -u rpcuser -P rpcpass
2018-09-30 18:36:28.003 [WRN] BTCW: open /root/.btcwallet/btcwallet.conf: no such file or directory
2018-09-30 18:36:28.003 [INF] BTCW: Version 0.7.0-alpha
2018-09-30 18:36:28.019 [WRN] BTCW: Can't listen on [::1]:8332: listen tcp6 [::1]:8332: bind: cannot assign requested address
2018-09-30 18:36:28.020 [INF] BTCW: Attempting RPC client connection to localhost:8334
2018-09-30 18:36:28.019 [INF] RPCS: Listening on 127.0.0.1:8332
2018-09-30 18:36:28.112 [INF] CHNS: Established connection to RPC server localhost:8334
2018-09-30 18:36:29.408 [INF] WLLT: Opened wallet
2018-09-30 18:36:29.409 [INF] WLLT: Catching up block hashes to height 382320, this will take a while...
2018-09-30 18:36:29.409 [INF] WLLT: RECOVERY MODE ENABLED -- rescanning for used addresses with recovery_window=250
2018-09-30 18:36:29.422 [INF] WLLT: Caught up to height 120000
...
  1. as per offical guide rpc --> document --> clientusage.md(https://github.com/btcsuite/btcwallet/blob/HEAD/rpc/documentation/clientusage.md), I run below code
package main

import (
    "fmt"
    "path/filepath"

    pb "github.com/btcsuite/btcwallet/rpc/walletrpc"
    "golang.org/x/net/context"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials"

    "github.com/btcsuite/btcutil"
)

var certificateFile = filepath.Join(btcutil.AppDataDir("btcwallet", false), "rpc.cert")

func main() {
    creds, err := credentials.NewClientTLSFromFile(certificateFile, "localhost")
    if err != nil {
        fmt.Println(err)
        return
    }
    conn, err := grpc.Dial("localhost:8332", grpc.WithTransportCredentials(creds))
    if err != nil {
        fmt.Println(err)
        return
    }
    defer conn.Close()
    c := pb.NewWalletServiceClient(conn)

    balanceRequest := &pb.BalanceRequest{
        AccountNumber:         0,
        RequiredConfirmations: 1,
    }
    balanceResponse, err := c.Balance(context.Background(), balanceRequest)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println("Spendable balance: ", btcutil.Amount(balanceResponse.Spendable))
}
  1. get error
➜  WalletTest go run main.go
rpc error: code = Internal desc = transport: received the unexpected content-type "text/plain; charset=utf-8"

huahuayu avatar Sep 30 '18 10:09 huahuayu

I don't have an environment to compile and run your code @huahuayu , which line of your code generate that rpc error? What were you trying to do with your code? I know the btcwallet was working on my env couple days ago, I can send/receive tokens in between the addresses I created in a test setup.

totaloutput avatar Oct 04 '18 16:10 totaloutput

@totaloutput thx for reply, all the steps can be reproduced as below:

  1. Get btcd up by command btcd -u rpcuser -P rpcpass;
  2. Get btcwallet up by command btcwallet -u rpcuser -P rpcpass
  3. run the btcwallet client code which is been provide here (go version)https://github.com/btcsuite/btcwallet/blob/HEAD/rpc/documentation/clientusage.md

and you will get the error:

rpc error: code = Internal desc = transport: received the unexpected content-type "text/plain; charset=utf-8"

huahuayu avatar Oct 05 '18 08:10 huahuayu

@huahuayu I checked log from btcwallet, its's this one: 2018-10-05 15:45:07.599 [WRN] RPCS: Unauthorized client connection attempt 2018/10/05 15:45:07 http: TLS handshake error from 127.0.0.1:45222: EOF

are you able to use btcctl wallet command? for example the following should work: btcctl --rpcuser=USER --rpcpass=PASS --wallet getbalance This will help you to see if it's your setup issue or just the demo code issue.

totaloutput avatar Oct 05 '18 15:10 totaloutput

@totaloutput the btcwallet log is exactly as you described, and the btcctl --rpcuser=USER --rpcpass=PASS --wallet getbalance works, it can retrieve the balance which is 0. It seems it's a connection error related to tsl. But even I start the btcwallet with btcwallet -u rpcuser -P rpcpass --noclienttls it's still getting the same error

2018-10-06 00:25:49.997 [WRN] RPCS: Unauthorized client connection attempt
2018/10/06 00:25:50 http: TLS handshake error from 127.0.0.1:43318: EOF

I can locate the err, it's get ErrNoAuth error in checkAuthHeader method(https://raw.githubusercontent.com/btcsuite/btcwallet/HEAD/rpc/legacyrpc/server.go):

func (s *Server) checkAuthHeader(r *http.Request) error {
	authhdr := r.Header["Authorization"]
	if len(authhdr) == 0 {
		return ErrNoAuth
	}

	authsha := sha256.Sum256([]byte(authhdr[0]))
	cmp := subtle.ConstantTimeCompare(authsha[:], s.authsha[:])
	if cmp != 1 {
		return errors.New("bad auth")
	}
	return nil
}

the error description is: // ErrNoAuth represents an error where authentication could not succeed // due to a missing Authorization HTTP header.

so the question becomes to how to add the Authorization header.

huahuayu avatar Oct 05 '18 16:10 huahuayu

@jrick @Roasbeef @aakselrod could you please take a look of this issue: rpc client can't connect to btcwallet .

huahuayu avatar Oct 05 '18 17:10 huahuayu

@huahuayu I don't think it's a real issue, it's just an issue with the demo code in the document.. if you search for "NewWalletServiceClient", it's only called from the demo code. Never from btcwallet and never from btcctl. So it's probably just for demo and might worked once before. I would recommend you to play with btcctl if you are building a wallet app.

totaloutput avatar Oct 05 '18 17:10 totaloutput

@totaloutput As you said command line with option is OK, but I have to use the client to interact with btcwallet, because I intend to use btcwallet to manage, say, more than 10000 accounts, a rpc client is far more convenient

huahuayu avatar Oct 06 '18 01:10 huahuayu

that is great, just use the source code of btcctl as example, don't use the one from the document.

totaloutput avatar Oct 06 '18 01:10 totaloutput

I've got the same situation but I'm trying to use the gRPC server instead of the legacy JSONRPC. The instructions to use btcctl as an example doesn't really solve the problem with the gRPC calls.

smoyer64 avatar Jun 16 '21 11:06 smoyer64