diff --git a/packages/protobuf/scripts/protobuf-patches/index.js b/packages/protobuf/scripts/protobuf-patches/index.js index fb75d42578..225879c26a 100644 --- a/packages/protobuf/scripts/protobuf-patches/index.js +++ b/packages/protobuf/scripts/protobuf-patches/index.js @@ -2,6 +2,7 @@ const fs = require('fs'); const path = require('path'); const UINT_TYPE = 'UintType'; +const SINT_TYPE = 'SintType'; const DeviceModelInternal = 'DeviceModelInternal'; // type rule fixes, ideally it should not be here @@ -130,7 +131,7 @@ const TYPE_PATCH = { 'CardanoSignTxInit.ttl': UINT_TYPE, 'CardanoSignTxInit.validity_interval_start': UINT_TYPE, 'CardanoSignTxInit.total_collateral': UINT_TYPE, - 'CardanoToken.mint_amount': 'number | string', // TODO: Sint64 support + 'CardanoToken.mint_amount': SINT_TYPE, 'CardanoNativeScript.invalid_before': UINT_TYPE, 'CardanoNativeScript.invalid_hereafter': UINT_TYPE, 'EosAsset.symbol': 'string', @@ -313,4 +314,5 @@ module.exports = { DEFINITION_PATCH, SKIP, UINT_TYPE, + SINT_TYPE, }; diff --git a/packages/protobuf/scripts/protobuf-types.js b/packages/protobuf/scripts/protobuf-types.js index 2b9d5b8b11..37f301a82e 100644 --- a/packages/protobuf/scripts/protobuf-types.js +++ b/packages/protobuf/scripts/protobuf-types.js @@ -2,7 +2,7 @@ const fs = require('fs'); const path = require('path'); const json = require('../messages.json'); -const { RULE_PATCH, TYPE_PATCH, DEFINITION_PATCH, SKIP, UINT_TYPE } = require('./protobuf-patches'); +const { RULE_PATCH, TYPE_PATCH, DEFINITION_PATCH, SKIP, UINT_TYPE, SINT_TYPE } = require('./protobuf-patches'); const INDENT = ' '.repeat(4); @@ -160,6 +160,8 @@ const lines = []; // string[] lines.push('// This file is auto generated from data/messages/message.json', ''); lines.push('// custom type uint32/64 may be represented as string'); lines.push(`export type ${UINT_TYPE} = string | number;`, ''); +lines.push('// custom type sint32/64'); +lines.push(`export type ${SINT_TYPE} = string | number;`, ''); lines.push( `export enum DeviceModelInternal { T1B1 = 'T1B1', diff --git a/packages/protobuf/src/messages-schema.ts b/packages/protobuf/src/messages-schema.ts index 8cd90aad93..e1c088e2ee 100644 --- a/packages/protobuf/src/messages-schema.ts +++ b/packages/protobuf/src/messages-schema.ts @@ -871,7 +871,7 @@ export type CardanoToken = Static; export const CardanoToken = Type.Object({ asset_name_bytes: Type.String(), amount: Type.Optional(Type.Uint()), - mint_amount: Type.Optional(Type.Uint()), + mint_amount: Type.Optional(Type.Uint({ allowNegative: true })), }); export type CardanoTxInlineDatumChunk = Static; @@ -2279,7 +2279,7 @@ export const StellarSignTx = Type.Object({ timebounds_end: Type.Number(), memo_type: EnumStellarMemoType, memo_text: Type.Optional(Type.String()), - memo_id: Type.Optional(Type.String()), + memo_id: Type.Optional(Type.Uint()), memo_hash: Type.Optional(Type.Union([Type.Buffer(), Type.String()])), num_operations: Type.Number(), }); diff --git a/packages/protobuf/src/messages.ts b/packages/protobuf/src/messages.ts index 7315d96395..d1cde35048 100644 --- a/packages/protobuf/src/messages.ts +++ b/packages/protobuf/src/messages.ts @@ -3,6 +3,9 @@ // custom type uint32/64 may be represented as string export type UintType = string | number; +// custom type sint32/64 +export type SintType = string | number; + export enum DeviceModelInternal { T1B1 = 'T1B1', T2T1 = 'T2T1', @@ -783,7 +786,7 @@ export type CardanoAssetGroup = { export type CardanoToken = { asset_name_bytes: string; amount?: UintType; - mint_amount?: UintType; + mint_amount?: SintType; }; // CardanoTxInlineDatumChunk @@ -2078,7 +2081,7 @@ export type StellarSignTx = { timebounds_end: number; memo_type: StellarMemoType; memo_text?: string; - memo_id?: string; + memo_id?: UintType; memo_hash?: Buffer | string; num_operations: number; }; diff --git a/packages/schema-utils/src/codegen.ts b/packages/schema-utils/src/codegen.ts index fddbb0dc03..d378d9284d 100644 --- a/packages/schema-utils/src/codegen.ts +++ b/packages/schema-utils/src/codegen.ts @@ -13,6 +13,7 @@ export function generate(code: string) { ArrayBuffer: 'Type.ArrayBuffer()', Buffer: 'Type.Buffer()', UintType: 'Type.Uint()', + SintType: 'Type.Uint({ allowNegative: true })', }; const customTypePlaceholder = Object.keys(customTypesMapping).map(t => `type ${t} = any;`); // Run generator diff --git a/packages/schema-utils/tests/__snapshots__/codegen.test.ts.snap b/packages/schema-utils/tests/__snapshots__/codegen.test.ts.snap index 0c17301cc2..cca25bfebd 100644 --- a/packages/schema-utils/tests/__snapshots__/codegen.test.ts.snap +++ b/packages/schema-utils/tests/__snapshots__/codegen.test.ts.snap @@ -5,6 +5,12 @@ exports[`codegen should generate code for protobuf messages example 1`] = ` import { Type, Static } from '@trezor/schema-utils'; +export type Type.Uint() = Static +export const Type.Uint() = Type.Union([ +Type.String(), +Type.Number() +]) + export enum DeviceModelInternal { T1B1 = 'T1B1', T2T1 = 'T2T1', T2B1 = 'T2B1' } export type EnumDeviceModelInternal = Static