Create custom CosmJS interfaces to connect to custom Cosmos SDK modules.
Define custom interfaces with Protobuf.
Define custom types and messages.
Integrate with Ignite - previously known as Starport.
CosmJS comes out of the box with interfaces that connect with the standard Cosmos SDK modules such as bank and gov and understand the way their messages are serialized. Since your own blockchain's modules are unique, they need custom CosmJS interfaces. That process consists of several steps:
Creating the Protobuf objects and clients in TypeScript.
Creating extensions that facilitate the use of the above clients.
Any further level of abstraction that you deem useful for integration.
This section assumes that you have a working Cosmos blockchain with its own modules. It is based on CosmJS version v0.28.3(opens new window).
Adjust to your preferred version, operating system, and CPU platform. For instance, on an Apple M1 you would use protoc-21.7-osx-aarch_64.zip.
Copy
FROM --platform=linux node:lts-slim as base
ARG BUILDARCH
ENV PROTOC_VERSION=21.7
ENV TS_PROTO_VERSION=1.121.6
FROM base AS platform-amd64
ENV PROTOC_PLATFORM=x86_64
FROM base AS platform-arm64
ENV PROTOC_PLATFORM=aarch_64
FROM platform-${BUILDARCH}
RUN apt-get update
RUN apt-get install curl unzip --yes
# Install ProtoC
RUN mkdir -p /usr/lib/protoc
WORKDIR /usr/lib/protoc
RUN curl -L https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-${PROTOC_PLATFORM}.zip -o protoc.zip
RUN unzip -o protoc.zip
RUN rm protoc.zip
RUN ln -s /usr/lib/protoc/bin/protoc /usr/local/bin/protoc
# Install ts-proto
RUN npm install --global ts-proto@${TS_PROTO_VERSION} --save-exact
WORKDIR /
ENTRYPOINT [ "protoc" ]
Then build the image:
Copy
$ docker build . -t ts-protoc
You can confirm the version you received. The executable is located in ./node_modules/protoc/protoc/bin/protoc:
Copy
$ protoc --version
Copy
$ docker run --rm -it \
ts-protoc --version
This returns something like:
Copy
libprotoc 3.21.7
The compiler tools are ready. Time to use them.
Create the target folder if it does not exist yet:
You need local copies of the right file versions in the right locations. Pay particular attention to Cosmos SDK's version of your project. You can check by running:
--proto_path is only ./proto so that your imports (such as import "cosmos/base...) can be found.
You should now see your files compiled into TypeScript. They have been correctly filed under their respective folders and contain both types and services definitions. It also created the compiled versions of your third party imports.
Create a script file named ts-proto.sh with the previous command, or create a Makefile target.
Add an npm run target(opens new window) with it, to keep track of how this was done and easily reproduce it in the future when you update a Protobuf file.
The typeUrl is the identifier by which Protobuf identifies the type of the data to serialize or deserialize. It is composed of the type's package and its name. For instance (and see also here(opens new window)):
In the previous code, you cannot reuse your msgSendTypeUrl because it is a value not a type. You can add a type helper, which is useful in an if else situation:
Copy
exportfunctionisMsgSendEncodeObject(encodeObject: EncodeObject): encodeObject is MsgSendEncodeObject {return(encodeObject as MsgSendEncodeObject).typeUrl ==="/cosmos.bank.v1beta1.MsgSend";} packages stargate ... bank messages.ts View source
StargateClient and SigningStargateClient are typically the ultimate abstractions that facilitate the querying and sending of transactions. You are now ready to add your own elements to them. The easiest way is to inherit from them and expose the extra functions you require.
You also need to inform MySigningStargateClient about the extra encodable types it should be able to handle. The list is defined in a registry that you can pass as options(opens new window).
Copy
import{ defaultRegistryTypes }from"@cosmjs/stargate"exportconst myDefaultRegistryTypes: ReadonlyArray<[string, GeneratedType]>=[...defaultRegistryTypes,...myTypes,// As you defined bankTypes earlier]
Now you are ready to combine this into your own MySigningStargateClient. It still takes an optional registry, but if that is missing it adds your newly defined default one:
Think of your functions as examples of proper use, that other developers can reuse when assembling more complex transactions.
You are ready to import and use this in a server script or a GUI.
If you would like to get started on building your own CosmJS elements on your own checkers game, you can go straight to the exercise in CosmJS for Your Chain to start from scratch.
Integrate CosmJS and Keplr, to see how to use and integrate what you prepared into a preexisting Checkers GUI.
synopsis
To summarize, this section has explored:
How CosmJS's out-of-the-box interfaces understand how messages of standard Cosmos SDK modules are serialized, meaning that your unique modules will require custom CosmJS interfaces of their own.
How to create the necessary Protobuf objects and clients in Typescript, the extensions that facilitate the use of these clients, and any further level of abstraction that you deem useful for integration.
How to integrate CosmJS with Ignite's client and signing client, which are typically the ultimate abstractions that facilitate the querying and sending of transactions.