@apophis-sdk/core/encoding/protobuf/any.ts
@apophis-sdk/cosmos/encoding/amino.ts
Amino
Amino is a legacy JSON sub-specification for blockchains that use the Cosmos SDK. It is superceded by ProtoBuf, but is still widely used, and the only format Ledger hardware wallets support in Cosmos. Amino is defined in the@apophis-sdk/cosmos
package, but listed here for completeness.
ProtoBuf
ProtoBuf is a widely adopted data transmission format developed and primarily maintained by Google. Unlike Amino, it is a size-optimized binary format, and requires more complex de/serialization. Formerly, you had to provide the correct de/serialization for your protobuf types yourself. With v0.3.1, we are now migrating to hiproto. Following is an example of how I’ve implemented theCoin
schema:
./protobuf/schema.ts
registerDefaultProtobufSchema
function:
./messages/bank.ts
Custom Amino Marshaller
You can further apply a custom marshaller for Amino types. This is useful where data is no longer reliably reconstructible from serialized form. For example theStoreCode
message of the CosmWasm
module:
Required Fields
By default, Protobuf message fields are all optional. Even so, clients may require certain fields be present or otherwise transactions are rejected. Cross-compatibility with Amino also may require certain fields to be present, even if empty, such asfunds
on a Contracts.Execute
message.
Unfortunately, for legacy Amino support, you must pay attention to required and optional fields
yourself. Otherwise, the full node may reconstruct the signature payload differently from the data
you have provided to the signer, causing the transaction to be rejected immediately.
The Contracts.Execute
schema may look like this:
Transforms
There are a few general-purpose transforms for protobuf fields available in@apophis-sdk/cosmos/encoding/protobuf/core.js
:
- bigintTransform: Transforms a
string
to abigint
and vice versa. - aminoTransform: Applies
Amino.normalize
to an arbitrary value, including sorting the object’s keys. In general, whenever you usehpb.json
, you should also apply this transform.
Other encodings
Other blockchains such as Solana and Ethereum use different encodings. When the time comes to develop integrations for these ecosystems, I will add corresponding encoding subsystems to this SDK. If you are looking to integrate your own encoding, please follow the above pattern, and develop a corresponding middleware. Your middleware may use the["core", "init"]
hook to register existing
messages (if applicable, e.g. Bank.Send
) for your new encoding so SDK consumers may seamlessly
integrate your blockchain.