Class: Registry
@shareledgerjs/client.Registry
Constructors
constructor
• new Registry(customTypes?
)
Creates a new Registry for mapping protobuf type identifiers/type URLs to actual implementations. Those implementations are typically generated with ts-proto but we also support protobuf.js as a type generator.
By default, a new Registry()
constains amost no types. Coin
and MsgSend
are in there
for historic reasons but this does not make a lot of sense.
There are currently two methods for adding new types:
- Using the
register()
method - Passing custom types to the constructor.
This only creates confusion for users. The reason here is historical.
Using
register()
is recommended and 2. is deprecated because its behaviour will change in https://github.com/cosmos/cosmjs/issues/994.
There is currently no way to unregister/override the default types. We should
change the customTypes
argument to override the default types if set.
See https://github.com/cosmos/cosmjs/issues/994
Parameters
Name | Type |
---|---|
customTypes? | Iterable <[string , GeneratedType ]> |
Properties
lookupTypeWithError
• Private
lookupTypeWithError: any
types
• Private
Readonly
types: any
Methods
decode
▸ decode(__namedParameters
): any
Parameters
Name | Type |
---|---|
__namedParameters | DecodeObject |
Returns
any
decodeTxBody
▸ decodeTxBody(txBody
): TxBody
Parameters
Name | Type |
---|---|
txBody | Uint8Array |
Returns
TxBody
encode
▸ encode(encodeObject
): Uint8Array
Takes a typeUrl/value pair and encodes the value to protobuf if the given type was previously registered.
If the value has to be wrapped in an Any, this needs to be done
manually after this call. Or use encodeAsAny
instead.
Parameters
Name | Type |
---|---|
encodeObject | EncodeObject |
Returns
Uint8Array
encodeAsAny
▸ encodeAsAny(encodeObject
): Any
Takes a typeUrl/value pair and encodes the value to an Any if the given type was previously registered.
Parameters
Name | Type |
---|---|
encodeObject | EncodeObject |
Returns
Any
encodeTxBody
▸ encodeTxBody(txBodyFields
): Uint8Array
Parameters
Name | Type |
---|---|
txBodyFields | TxBodyValue |
Returns
Uint8Array
lookupType
▸ lookupType(typeUrl
): undefined
| GeneratedType
Looks up a type that was previously added to the registry.
The generator information (ts-proto or pbjs) gets lost along the way. If you need to work with the result type in TypeScript, you can use:
import { assert } from "@cosmjs/utils";
const Coin = registry.lookupType("/cosmos.base.v1beta1.Coin");
assert(Coin); // Ensures not unset
assert(isTsProtoGeneratedType(Coin)); // Ensures this is the type we expect
// Coin is typed TsProtoGeneratedType now.
Parameters
Name | Type |
---|---|
typeUrl | string |
Returns
undefined
| GeneratedType
register
▸ register(typeUrl
, type
): void
Parameters
Name | Type |
---|---|
typeUrl | string |
type | GeneratedType |
Returns
void