# Completion of the "Support for merkle proof integrations" milestone
Support for merkle proofs has been implemented as part of [CosmJS](https://github.com/CosmWasm/cosmjs) by Ethan Frey, Will Clark and Simon Warta between June and August 2020.
In order to make this integration possible, a Cosmos SDK client that connects to Tendermint directly had to be implemented. This is available as a separate package [@cosmjs/tendermint-rpc](https://www.npmjs.com/package/@cosmjs/tendermint-rpc) to be usable independent of a certain blockchain.
Building on top of Tendermint, there is a new package [@cosmjs/stargate](https://github.com/CosmWasm/cosmjs/tree/merkle-proof-integration/packages/stargate) for connecting to Cosmos SDK 0.40+ blockchains. This package contains a high level [StargateClient](https://cosmwasm.github.io/cosmjs/latest/stargate/classes/stargateclient.html) using a [QueryClient](https://cosmwasm.github.io/cosmjs/latest/stargate/classes/queryclient.html).
QueryClient is the core of the query work. It only contains two methods `queryUnverified` and `queryVerified` but can be extended with module-specific queries. `queryUnverified` submits a gRPC query and returns a result. Since this includes data processing, such queries cannot be verified. `queryVerified` accessed Tendermit state directly and allows us to [verify light client proofs](https://github.com/CosmWasm/cosmjs/blob/merkle-proof-integration/packages/stargate/src/queries/queryclient.ts#L160-L208) provided by Tendermint using [the ICS23 implementation](https://www.npmjs.com/package/@confio/ics23) by Confio.
The extension system for QueryClient works very similar to the one in @cosmjs/launchpad. You create a client with all the extensions you need, and have them available in a type safe way:
```js
const tmClient = await TendermintClient.connect(endpoint);
const queryClient = QueryClient.withExtensions(
tmClient,
setupAuthExtension,
setupBankExtension,
setupIbcExtension
);
// Examples verified queries
const account = await queryClient.auth.account(searchAddress);
const balance = await queryClient.bank.balance(address, searchDenom);
// Examples unverified queries
const balances = await queryClient.bank.unverified.allBalances(address);
const channels = await queryClient.ibc.unverified.channels();
```
Extensions are implemented using auto-generated code from the Cosmos SDK .proto schemas. This makes the setup robust and maintainable. An example for an extension that shows one verified query and multiple gPRC queries is [BankExtension](https://github.com/CosmWasm/cosmjs/blob/merkle-proof-integration/packages/stargate/src/queries/bank.ts#L8-L67).
Code generation from .proto files is powered by protobuf.js and provides a runtime implementation in JavaScript as well as TypeScript type definitions. The process works as follows:
- Fetch .ptoto files from Cosmos SDK: [get-proto.sh](https://github.com/CosmWasm/cosmjs/blob/merkle-proof-integration/packages/stargate/scripts/get-proto.sh)
- Generate .js: [predefine-proto.sh](https://github.com/CosmWasm/cosmjs/blob/merkle-proof-integration/packages/stargate/scripts/predefine-proto.sh)
- Extract type definition from JavaScript comments: [define-proto.sh](https://github.com/CosmWasm/cosmjs/blob/merkle-proof-integration/packages/stargate/scripts/define-proto.sh)
- Wrap generated codec in a module to adjust some protobuf.js settings: [codec/index.ts](https://github.com/CosmWasm/cosmjs/blob/merkle-proof-integration/packages/stargate/src/codec/index.ts)
- Import as `import { cosmos, google } from "../codec";`
The provable (raw) queries need to be manually writen after looking at the relevant Cosmos SDK implementations. We have done the following provable queries (which assert there is a proof to match the result to the AppHash in the header of height H+1):
* [Auth account](https://github.com/CosmWasm/cosmjs/blob/merkle-proof-integration/packages/stargate/src/queries/auth.ts#L30-L43) - only BaseAccount for now
* [Bank balance](https://github.com/CosmWasm/cosmjs/blob/merkle-proof-integration/packages/stargate/src/queries/bank.ts#L34-L44) - one denom
* [IBC](https://github.com/CosmWasm/cosmjs/blob/master/packages/stargate/src/queries/ibc.ts#L86-L115) - channel, packet and acknowledgement queries
The following work was done along with the milestone:
- Support the Cosmos SDK team with the definition of the protobuf based signing mechanism (primarily ADR 020 and ADR 027)
- Testing and bug reporting for the development version of the demo application [simapp](https://github.com/cosmos/cosmos-sdk/tree/master/simapp)
- Creating the transaction signing library [@cosmjs/proto-signing](https://github.com/CosmWasm/cosmjs/tree/merkle-proof-integration/packages/proto-signing) that handles common Cosmos SDK messages types as well as custom message types using an class decorator system
- Basic read and write client functionality for Stargate: [getHeight](https://cosmwasm.github.io/cosmjs/latest/stargate/classes/stargateclient.html#getheight), [getBlock](https://cosmwasm.github.io/cosmjs/latest/stargate/classes/stargateclient.html#getblock), [getChainId](https://cosmwasm.github.io/cosmjs/latest/stargate/classes/stargateclient.html#getchainid), [broadcastTx](https://cosmwasm.github.io/cosmjs/latest/stargate/classes/stargateclient.html#broadcasttx), [searchTx](https://cosmwasm.github.io/cosmjs/latest/stargate/classes/stargateclient.html#searchtx)