fix(protobuf): improve message type and schema generating

This commit is contained in:
Marek Polak
2025-03-12 13:15:28 +01:00
committed by martin
parent c16b585b4b
commit c3f1750def
6 changed files with 22 additions and 31 deletions

View File

@@ -47,18 +47,18 @@ export type CardanoSignTransactionParams = {
signingMode: PROTO.CardanoTxSigningMode;
inputsWithPath: InputWithPath[];
outputsWithData: OutputWithData[];
fee: PROTO.UintType;
ttl?: PROTO.UintType;
fee: PROTO.CardanoSignTxInit['fee'];
ttl?: PROTO.CardanoSignTxInit['ttl'];
certificatesWithPoolOwnersAndRelays: CertificateWithPoolOwnersAndRelays[];
withdrawals: PROTO.CardanoTxWithdrawal[];
mint: AssetGroupWithTokens[];
auxiliaryData?: PROTO.CardanoTxAuxiliaryData;
validityIntervalStart?: PROTO.UintType;
validityIntervalStart?: PROTO.CardanoSignTxInit['validity_interval_start'];
scriptDataHash?: string;
collateralInputsWithPath: CollateralInputWithPath[];
requiredSigners: PROTO.CardanoTxRequiredSigner[];
collateralReturnWithData?: OutputWithData;
totalCollateral?: PROTO.UintType;
totalCollateral?: PROTO.CardanoSignTxInit['total_collateral'];
referenceInputs: PROTO.CardanoTxReferenceInput[];
protocolMagic: number;
networkId: number;

View File

@@ -252,7 +252,9 @@ export const TYPE_PATCH = {
};
export const readPatch = (file: string) =>
fs.readFileSync(path.join(__dirname, file), 'utf8').replace(/^\/\/ @ts-nocheck.*\n?/gm, '');
fs
.readFileSync(path.join(__dirname, file), 'utf8')
.replace(/^\/\/ (@ts-nocheck|eslint-disable-next-line).*\n?/gm, '');
export const DEFINITION_PATCH = {
TxInputType: () => readPatch('./TxInputType.ts'),

View File

@@ -181,9 +181,9 @@ const createCustomTypes = () => {
const lines: string[] = [];
lines.push('// This file is auto generated by @trezor/protobuf package', '');
lines.push('// custom type uint32/64 may be represented as string');
lines.push(`export type ${UINT_TYPE} = string | number;`, '');
lines.push(`type ${UINT_TYPE} = string | number;`, '');
lines.push('// custom type sint32/64');
lines.push(`export type ${SINT_TYPE} = string | number;`, '');
lines.push(`type ${SINT_TYPE} = string | number;`, '');
lines.push(readPatch(`../../../device-utils/src/deviceModelInternal.ts`), '');
return lines;
@@ -200,6 +200,7 @@ const createMessageType = (types: TypeItem[]) => {
});
lines.push('};');
lines.push('\n// @COPY from this marker to the EOF, types are copied into messages-schema');
lines.push(readPatch('MessageType.ts'));
return lines;

View File

@@ -3820,8 +3820,7 @@ export const MessageType = Type.Object(
{ $id: 'MessageType' },
);
// custom type uint32/64 may be represented as string
export type UintType = string | number;
// @COPY from this marker to the EOF, types are copied into messages-schema
export type MessageKey = keyof MessageType;

View File

@@ -1,10 +1,10 @@
// This file is auto generated by @trezor/protobuf package
// custom type uint32/64 may be represented as string
export type UintType = string | number;
type UintType = string | number;
// custom type sint32/64
export type SintType = string | number;
type SintType = string | number;
export enum DeviceModelInternal {
T1B1 = 'T1B1',
@@ -2569,6 +2569,8 @@ export type MessageType = {
TezosSignedTx: TezosSignedTx;
};
// @COPY from this marker to the EOF, types are copied into messages-schema
export type MessageKey = keyof MessageType;
export type MessageResponse<T extends MessageKey> = {

View File

@@ -7,9 +7,12 @@ export function generate(code: string) {
// Since there are some issues with typeof
code = code.replace(/typeof undefined/g, 'undefined');
code = code.replace(/keyof typeof/g, 'keyof');
// Ignore types added at end of message.ts, these are too complex for the generator
if (code.indexOf('export type MessageKey = keyof MessageType') >= 0) {
code = code.substring(0, code.indexOf('export type MessageKey = keyof MessageType'));
let helpers = '';
// Duplicate types added at end of message.ts, as these are too complex for the generator
const helpersIndex = code.indexOf('// @COPY');
if (helpersIndex >= 0) {
helpers = code.substring(helpersIndex);
code = code.substring(0, helpersIndex);
}
// Make generator aware of custom types
const customTypesMapping = {
@@ -59,23 +62,7 @@ export function generate(code: string) {
output = `/* eslint-disable camelcase */\n${output}`;
// Add types for message schema
if (output.indexOf('export type MessageType =') > -1) {
output = `${output}\n
// custom type uint32/64 may be represented as string
export type UintType = string | number;
export type MessageKey = keyof MessageType;
export type MessageResponse<T extends MessageKey> = {
type: T;
message: MessageType[T];
};
export type TypedCall = <T extends MessageKey, R extends MessageKey>(
type: T,
resType: R,
message?: MessageType[T],
) => Promise<MessageResponse<R>>;
`;
output = `${output}\n\n${helpers}`;
}
return output;