proto-signing: Remove functionality ts-proto won’t support 🔥
This commit is contained in:
parent
53f923baea
commit
c0dd55bc94
@ -1,98 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { assert } from "@cosmjs/utils";
|
||||
import Long from "long";
|
||||
import { Message } from "protobufjs";
|
||||
|
||||
import { cosmos, google } from "./codec";
|
||||
import { cosmosField, registered } from "./decorator";
|
||||
import { Registry } from "./registry";
|
||||
|
||||
const { TxBody } = cosmos.tx.v1beta1;
|
||||
const { Any } = google.protobuf;
|
||||
|
||||
describe("decorator demo", () => {
|
||||
it("works with a custom msg", () => {
|
||||
const nestedTypeUrl = "/demo.MsgNestedDemo";
|
||||
const typeUrl = "/demo.MsgDemo";
|
||||
const myRegistry = new Registry();
|
||||
|
||||
@registered(myRegistry, nestedTypeUrl)
|
||||
class MsgNestedDemo extends Message {
|
||||
@cosmosField.string(1)
|
||||
public readonly foo?: string;
|
||||
}
|
||||
|
||||
@registered(myRegistry, typeUrl)
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
class MsgDemo extends Message {
|
||||
@cosmosField.boolean(1)
|
||||
public readonly booleanDemo?: boolean;
|
||||
|
||||
@cosmosField.string(2)
|
||||
public readonly stringDemo?: string;
|
||||
|
||||
@cosmosField.bytes(3)
|
||||
public readonly bytesDemo?: Uint8Array;
|
||||
|
||||
@cosmosField.int64(4)
|
||||
public readonly int64Demo?: number;
|
||||
|
||||
@cosmosField.uint64(5)
|
||||
public readonly uint64Demo?: number;
|
||||
|
||||
@cosmosField.repeatedString(6)
|
||||
public readonly listDemo?: readonly string[];
|
||||
|
||||
@cosmosField.message(7, MsgNestedDemo)
|
||||
public readonly nestedDemo?: MsgNestedDemo;
|
||||
}
|
||||
|
||||
const MsgNestedDemoT = myRegistry.lookupType(nestedTypeUrl)!;
|
||||
const MsgDemoT = myRegistry.lookupType(typeUrl)!;
|
||||
|
||||
const msgNestedDemoFields = {
|
||||
foo: "bar",
|
||||
};
|
||||
const msgNestedDemo = MsgNestedDemoT.create(msgNestedDemoFields);
|
||||
|
||||
const msgDemoFields = {
|
||||
booleanDemo: true,
|
||||
stringDemo: "example text",
|
||||
bytesDemo: Uint8Array.from([1, 2, 3, 4, 5, 6, 7, 8]),
|
||||
int64Demo: -123,
|
||||
uint64Demo: 123,
|
||||
listDemo: ["this", "is", "a", "list"],
|
||||
nestedDemo: msgNestedDemo,
|
||||
};
|
||||
const msgDemo = MsgDemoT.create(msgDemoFields);
|
||||
const msgDemoBytes = MsgDemoT.encode(msgDemo).finish();
|
||||
const msgDemoWrapped = Any.create({
|
||||
type_url: typeUrl,
|
||||
value: msgDemoBytes,
|
||||
});
|
||||
const txBody = TxBody.create({
|
||||
messages: [msgDemoWrapped],
|
||||
memo: "Some memo",
|
||||
timeoutHeight: Long.fromNumber(9999),
|
||||
extensionOptions: [],
|
||||
});
|
||||
const txBodyBytes = TxBody.encode(txBody).finish();
|
||||
|
||||
const txBodyDecoded = TxBody.decode(txBodyBytes);
|
||||
const msg = txBodyDecoded.messages[0];
|
||||
assert(msg.type_url);
|
||||
assert(msg.value);
|
||||
|
||||
const msgDemoDecoded = MsgDemoT.decode(msg.value);
|
||||
expect(msgDemoDecoded.booleanDemo).toEqual(msgDemoFields.booleanDemo);
|
||||
expect(msgDemoDecoded.stringDemo).toEqual(msgDemoFields.stringDemo);
|
||||
// bytesDemo decodes to a Buffer in Node
|
||||
expect(Uint8Array.from(msgDemoDecoded.bytesDemo)).toEqual(msgDemoFields.bytesDemo);
|
||||
// int64Demo and uint64Demo decode to Long in Node
|
||||
expect(Number(msgDemoDecoded.int64Demo)).toEqual(msgDemoFields.int64Demo);
|
||||
expect(Number(msgDemoDecoded.uint64Demo)).toEqual(msgDemoFields.uint64Demo);
|
||||
|
||||
expect(msgDemoDecoded.listDemo).toEqual(msgDemoFields.listDemo);
|
||||
expect(msgDemoDecoded.nestedDemo).toEqual(msgDemoFields.nestedDemo);
|
||||
});
|
||||
});
|
||||
@ -1,40 +0,0 @@
|
||||
import { Constructor, Field, Message, TypeDecorator, util } from "protobufjs";
|
||||
|
||||
import { Registry } from "./registry";
|
||||
|
||||
function getTypeName(typeUrl: string): string {
|
||||
const parts = typeUrl.split(".");
|
||||
return parts[parts.length - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* A class decorator to register this type under the given type URL
|
||||
* in the given registry.
|
||||
*/
|
||||
export function registered(registry: Registry, typeUrl: string): TypeDecorator<any> {
|
||||
return (ctor: Constructor<Message<any>>) => {
|
||||
const typeName = getTypeName(typeUrl);
|
||||
const generatedType = util.decorateType(ctor, typeName);
|
||||
registry.register(typeUrl, generatedType);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Like PropertyDecorator from lib.es5.d.ts but without symbol support in propertyKey.
|
||||
*/
|
||||
export type FieldDecorator = (target: any, propertyKey: string) => void;
|
||||
|
||||
export const cosmosField = {
|
||||
boolean: (id: number): FieldDecorator => Field.d<boolean>(id, "bool"),
|
||||
|
||||
string: (id: number): FieldDecorator => Field.d<string>(id, "string"),
|
||||
bytes: (id: number): FieldDecorator => Field.d<Uint8Array>(id, "bytes"),
|
||||
|
||||
int64: (id: number): FieldDecorator => Field.d<number>(id, "int64"),
|
||||
uint64: (id: number): FieldDecorator => Field.d<number>(id, "uint64"),
|
||||
|
||||
message: (id: number, ctor: Constructor<Message>): FieldDecorator => Field.d(id, ctor),
|
||||
|
||||
repeatedString: (id: number): FieldDecorator => Field.d<string[]>(id, "string", "repeated"),
|
||||
repeatedMessage: (id: number, ctor: Constructor<Message>): FieldDecorator => Field.d(id, ctor, "repeated"),
|
||||
};
|
||||
@ -1,89 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import Long from "long";
|
||||
import { Message } from "protobufjs";
|
||||
|
||||
import { cosmosField, registered } from "./decorator";
|
||||
import { Registry } from "./registry";
|
||||
|
||||
describe("registry magic demo", () => {
|
||||
it("works with a custom msg", () => {
|
||||
const nestedTypeUrl = "/demo.MsgNestedMagic";
|
||||
const typeUrl = "/demo.MsgMagic";
|
||||
const myRegistry = new Registry();
|
||||
|
||||
@registered(myRegistry, nestedTypeUrl)
|
||||
class MsgNestedMagic extends Message {
|
||||
@cosmosField.string(1)
|
||||
public readonly foo?: string;
|
||||
}
|
||||
|
||||
@registered(myRegistry, typeUrl)
|
||||
class MsgMagic extends Message {
|
||||
@cosmosField.boolean(1)
|
||||
public readonly booleanDemo?: boolean;
|
||||
|
||||
@cosmosField.string(2)
|
||||
public readonly stringDemo?: string;
|
||||
|
||||
@cosmosField.bytes(3)
|
||||
public readonly bytesDemo?: Uint8Array;
|
||||
|
||||
@cosmosField.int64(4)
|
||||
public readonly int64Demo?: Long;
|
||||
|
||||
@cosmosField.uint64(5)
|
||||
public readonly uint64Demo?: Long;
|
||||
|
||||
@cosmosField.repeatedString(6)
|
||||
public readonly listDemo?: readonly string[];
|
||||
|
||||
@cosmosField.message(7, MsgNestedMagic)
|
||||
public readonly nestedDemo?: MsgNestedMagic;
|
||||
}
|
||||
|
||||
const msgNestedDemoFields = {
|
||||
foo: "bar",
|
||||
};
|
||||
const msgDemoFields = {
|
||||
booleanDemo: true,
|
||||
stringDemo: "example text",
|
||||
bytesDemo: Uint8Array.from([1, 2, 3, 4, 5, 6, 7, 8]),
|
||||
int64Demo: -123,
|
||||
uint64Demo: 123,
|
||||
listDemo: ["this", "is", "a", "list"],
|
||||
nestedDemo: msgNestedDemoFields,
|
||||
};
|
||||
const txBodyFields = {
|
||||
messages: [{ typeUrl: typeUrl, value: msgDemoFields }],
|
||||
memo: "Some memo",
|
||||
timeoutHeight: 9999,
|
||||
extensionOptions: [],
|
||||
};
|
||||
const txBodyBytes = myRegistry.encode({
|
||||
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
||||
value: txBodyFields,
|
||||
});
|
||||
|
||||
const txBodyDecoded = myRegistry.decode({
|
||||
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
||||
value: txBodyBytes,
|
||||
});
|
||||
expect(txBodyDecoded.memo).toEqual(txBodyFields.memo);
|
||||
// int64Demo and uint64Demo decode to Long
|
||||
expect(txBodyDecoded.timeoutHeight).toEqual(Long.fromNumber(txBodyFields.timeoutHeight, true));
|
||||
expect(txBodyDecoded.extensionOptions).toEqual(txBodyFields.extensionOptions);
|
||||
|
||||
const msgDemoDecoded = txBodyDecoded.messages[0] as MsgMagic;
|
||||
expect(msgDemoDecoded).toBeInstanceOf(MsgMagic);
|
||||
expect(msgDemoDecoded.booleanDemo).toEqual(msgDemoFields.booleanDemo);
|
||||
expect(msgDemoDecoded.stringDemo).toEqual(msgDemoFields.stringDemo);
|
||||
expect(msgDemoDecoded.bytesDemo).toEqual(msgDemoFields.bytesDemo);
|
||||
// int64Demo and uint64Demo decode to Long
|
||||
expect(msgDemoDecoded.int64Demo).toEqual(Long.fromNumber(msgDemoFields.int64Demo));
|
||||
expect(msgDemoDecoded.uint64Demo).toEqual(Long.fromNumber(msgDemoFields.uint64Demo, true));
|
||||
expect(msgDemoDecoded.listDemo).toEqual(msgDemoFields.listDemo);
|
||||
|
||||
expect(msgDemoDecoded.nestedDemo).toBeInstanceOf(MsgNestedMagic);
|
||||
expect(msgDemoDecoded.nestedDemo!.foo).toEqual(msgDemoFields.nestedDemo.foo);
|
||||
});
|
||||
});
|
||||
@ -1,35 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { cosmos } from "./codec";
|
||||
import { Coin, MsgSend } from "./msgs";
|
||||
|
||||
describe("msgs", () => {
|
||||
it("encodes decorated MsgSend equally to static code", () => {
|
||||
const alice = "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6";
|
||||
const bob = "cosmos1qypqxpq9qcrsszg2pvxq6rs0zqg3yyc5lzv7xu";
|
||||
const amount = [
|
||||
new Coin({ denom: "utoken", amount: "123" }),
|
||||
new Coin({ denom: "ustake", amount: "654" }),
|
||||
];
|
||||
const donation = new MsgSend({ from_address: alice, to_address: bob, amount });
|
||||
|
||||
const expected = cosmos.bank.v1beta1.MsgSend.encode(
|
||||
cosmos.bank.v1beta1.MsgSend.create({
|
||||
fromAddress: alice,
|
||||
toAddress: bob,
|
||||
amount: [
|
||||
cosmos.base.v1beta1.Coin.create({
|
||||
denom: "utoken",
|
||||
amount: "123",
|
||||
}),
|
||||
cosmos.base.v1beta1.Coin.create({
|
||||
denom: "ustake",
|
||||
amount: "654",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
).finish();
|
||||
|
||||
const encoded = MsgSend.encode(donation).finish();
|
||||
expect(encoded).toEqual(expected);
|
||||
});
|
||||
});
|
||||
@ -1,28 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { Message } from "protobufjs";
|
||||
|
||||
import { cosmosField, registered } from "./decorator";
|
||||
import { Registry } from "./registry";
|
||||
|
||||
export const defaultRegistry = new Registry();
|
||||
|
||||
@registered(defaultRegistry, "/cosmos.base.v1beta1.Coin")
|
||||
export class Coin extends Message {
|
||||
@cosmosField.string(1)
|
||||
public readonly denom?: string;
|
||||
|
||||
@cosmosField.string(2)
|
||||
public readonly amount?: string;
|
||||
}
|
||||
|
||||
@registered(defaultRegistry, "/cosmos.bank.v1beta1.MsgSend")
|
||||
export class MsgSend extends Message {
|
||||
@cosmosField.string(1)
|
||||
public readonly from_address?: string;
|
||||
|
||||
@cosmosField.string(2)
|
||||
public readonly to_address?: string;
|
||||
|
||||
@cosmosField.repeatedMessage(3, Coin)
|
||||
public readonly amount?: readonly Coin[];
|
||||
}
|
||||
21
packages/proto-signing/types/decorator.d.ts
vendored
21
packages/proto-signing/types/decorator.d.ts
vendored
@ -1,21 +0,0 @@
|
||||
import { Constructor, Message, TypeDecorator } from "protobufjs";
|
||||
import { Registry } from "./registry";
|
||||
/**
|
||||
* A class decorator to register this type under the given type URL
|
||||
* in the given registry.
|
||||
*/
|
||||
export declare function registered(registry: Registry, typeUrl: string): TypeDecorator<any>;
|
||||
/**
|
||||
* Like PropertyDecorator from lib.es5.d.ts but without symbol support in propertyKey.
|
||||
*/
|
||||
export declare type FieldDecorator = (target: any, propertyKey: string) => void;
|
||||
export declare const cosmosField: {
|
||||
boolean: (id: number) => FieldDecorator;
|
||||
string: (id: number) => FieldDecorator;
|
||||
bytes: (id: number) => FieldDecorator;
|
||||
int64: (id: number) => FieldDecorator;
|
||||
uint64: (id: number) => FieldDecorator;
|
||||
message: (id: number, ctor: Constructor<Message>) => FieldDecorator;
|
||||
repeatedString: (id: number) => FieldDecorator;
|
||||
repeatedMessage: (id: number, ctor: Constructor<Message>) => FieldDecorator;
|
||||
};
|
||||
12
packages/proto-signing/types/msgs.d.ts
vendored
12
packages/proto-signing/types/msgs.d.ts
vendored
@ -1,12 +0,0 @@
|
||||
import { Message } from "protobufjs";
|
||||
import { Registry } from "./registry";
|
||||
export declare const defaultRegistry: Registry;
|
||||
export declare class Coin extends Message {
|
||||
readonly denom?: string;
|
||||
readonly amount?: string;
|
||||
}
|
||||
export declare class MsgSend extends Message {
|
||||
readonly from_address?: string;
|
||||
readonly to_address?: string;
|
||||
readonly amount?: readonly Coin[];
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user