Use the agent-go to access local wallet canister, reports lookup error, but works if access IC mainnet

=== RUN   Test_WalletBalance_Local
    agent_test.go:203: 
        	Error Trace:	/Users/luokeep/Code/github.com/xxx/agent_test.go:203
        	Error:      	Received unexpected error:
        	            	lookup error (path: "subnet/\xcf\xf2\x80\xe3-\x7f\\\xcd\"F\x88/\x94\xaf\xb2\x0fT\xcaa\xa2\x17e\xe7\x12\xd4='\x89\x02/node/Y\xfb\xf3\xd5\x0e\xc8\xf3\xcb\xed.\xdc)\x03/\x17Q\xb2\xdfM'q=\xa3\xbf\x85\"\x8c:\x02/public_key") at "\xcf\xf2\x80\xe3-\x7f\\\xcd\"F\x88/\x94\xaf\xb2\x0fT\xcaa\xa2\x17e\xe7\x12\xd4='\x89\x02": not found, not present in the tree
        	Test:       	Test_WalletBalance_Local
balance <nil>
--- FAIL: Test_WalletBalance_Local (0.04s)

Works if connect to ICP mainnet


I have two guesses:

  1. Are you running an ‘old’ replica version? What version does dfx --version give? Does updating to the latest version help?
  2. Does the wallet canister even exist? It’s possible that you accidentally restarted the replica with a clean state and the canister doesn’t exist anymore.

Side note: please use FetchRootKey: false for mainnet. Otherwise you give up a lot of security properties

dfx version is 0.22.0 and the wallet canister exist.

To access local network with agent-go, DisableSignedQueryVerification should be set to be true.

func Test_WalletBalance_Local(t *testing.T) {
	var pem = []byte(`
-----BEGIN EC PRIVATE KEY-----

-----END EC PRIVATE KEY-----`)

	id, err := identity.NewSecp256k1IdentityFromPEMWithoutParameters(pem)
	assert.NoError(t, err)

	host, err := url.Parse("http://127.0.0.1:4943/")
	assert.NoError(t, err)

	cfg := agent.Config{
		Identity:                       id,
		ClientConfig:                   &agent.ClientConfig{Host: host},
		FetchRootKey:                   true,
		PollTimeout:                    30 * time.Second,
		DisableSignedQueryVerification: true,
	}

	a, err := wallet.NewAgent(principal.MustDecode("bnz7o-iuaaa-aaaaa-qaaaa-cai"), cfg)
	assert.NoError(t, err)

	balance, err := a.WalletBalance()
	assert.NoError(t, err)
	fmt.Printf("balance:%v\n", balance.Amount)
}