mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
demo-protobuf: Add register method to registry
This commit is contained in:
parent
0addb8c67f
commit
362cfdb618
@ -39,13 +39,13 @@ describe("registry demo", () => {
|
||||
});
|
||||
const txBodyBytes = TxBody.encode(txBody).finish();
|
||||
|
||||
const txBodyDecoded = (TxBody.decode(txBodyBytes) as unknown) as cosmosSdk.tx.v1.TxBody;
|
||||
const txBodyDecoded = TxBody.decode(txBodyBytes);
|
||||
const msg = txBodyDecoded.messages[0];
|
||||
assert(msg.type_url);
|
||||
assert(msg.value);
|
||||
|
||||
const decoder = registry.lookupType(msg.type_url)!;
|
||||
const msgSendDecoded = (decoder.decode(msg.value) as unknown) as cosmosSdk.x.bank.v1.MsgSend;
|
||||
const msgSendDecoded = decoder.decode(msg.value);
|
||||
|
||||
// fromAddress and toAddress are now Buffers
|
||||
expect(Uint8Array.from(msgSendDecoded.fromAddress)).toEqual(msgSend.fromAddress);
|
||||
@ -60,9 +60,9 @@ describe("registry demo", () => {
|
||||
const TxBody = registry.lookupType("/cosmos.tx.TxBody")!;
|
||||
const Any = registry.lookupType("/google.protobuf.Any")!;
|
||||
|
||||
const msgDemo = (MsgDemo.create({
|
||||
const msgDemo = MsgDemo.create({
|
||||
example: "Some example text",
|
||||
}) as unknown) as MsgDemo;
|
||||
});
|
||||
const msgDemoBytes = MsgDemo.encode(msgDemo).finish();
|
||||
const msgDemoWrapped = Any.create({
|
||||
type_url: typeUrl,
|
||||
@ -76,13 +76,13 @@ describe("registry demo", () => {
|
||||
});
|
||||
const txBodyBytes = TxBody.encode(txBody).finish();
|
||||
|
||||
const txBodyDecoded = (TxBody.decode(txBodyBytes) as unknown) as cosmosSdk.tx.v1.TxBody;
|
||||
const txBodyDecoded = TxBody.decode(txBodyBytes);
|
||||
const msg = txBodyDecoded.messages[0];
|
||||
assert(msg.type_url);
|
||||
assert(msg.value);
|
||||
|
||||
const decoder = registry.lookupType(msg.type_url)!;
|
||||
const msgDemoDecoded = (decoder.decode(msg.value) as unknown) as MsgDemo;
|
||||
const msgDemoDecoded = decoder.decode(msg.value);
|
||||
expect(msgDemoDecoded.example).toEqual(msgDemo.example);
|
||||
});
|
||||
});
|
||||
|
@ -21,6 +21,10 @@ export class Registry {
|
||||
]);
|
||||
}
|
||||
|
||||
public register(name: string, type: GeneratedType): void {
|
||||
this.types.set(name, type);
|
||||
}
|
||||
|
||||
public lookupType(name: string): GeneratedType | undefined {
|
||||
return this.types.get(name);
|
||||
}
|
||||
|
1
packages/demo-protobuf/types/registry.d.ts
vendored
1
packages/demo-protobuf/types/registry.d.ts
vendored
@ -14,6 +14,7 @@ interface GeneratedType {
|
||||
export declare class Registry {
|
||||
private readonly types;
|
||||
constructor(customTypes?: Iterable<[string, GeneratedType]>);
|
||||
register(name: string, type: GeneratedType): void;
|
||||
lookupType(name: string): GeneratedType | undefined;
|
||||
}
|
||||
export {};
|
||||
|
Loading…
x
Reference in New Issue
Block a user