Skip to content

Commit

Permalink
fix(connect): preferHexDisplay flag and pubKey return
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskp committed Feb 27, 2024
1 parent c07c5c8 commit 77cfd51
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 26 deletions.
9 changes: 5 additions & 4 deletions docs/packages/connect/methods/cardanoSignMessage.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const result = await TrezorConnect.cardanoSignMessage(params);
- `signingPath` - _required_ `string | Array<number>` minimum length is `5`. [read more](../path.md)
- `payload` - _required_ `string` message bytes in hex.
- `hashPayload` - _required_ `boolean` if true, device will hash the payload before signing. Must be enabled if payload exceeds 1024 bytes.
- `displayAscii` - _required_ `boolean` if true, device will decode payload as ASCII.
- `preferHexDisplay` - _optional_ `boolean` if true, device will always decode payload as hex bytes.
- `networkId` - _optional_ `number` network identifier. Required if `addressParameters` are set.
- `protocolMagic` - _optional_ `number` protocol magic. Required if `addressParameters` are set.
- `addressParameters` - _optional_ `CardanoAddressParameters` object. [read more](./cardanoGetAddress.md#address-parameters) Used to derive address for message header. If not set then the key hash given by `signingPath` will be used instead.
Expand All @@ -25,7 +25,7 @@ const result = await TrezorConnect.cardanoSignMessage(params);

If `hashPayload` is `true`, the device will display just the first 56 bytes of the payload. Otherwise 1024 bytes are displayed.

The payload is decoded as a hex string if `displayAscii` is set to `false`. Otherwise it is decoded as an ASCII string, which succeeds only given the following conditions are met:
By default, the payload is decoded as ASCII given the conditions below are satisfied. If they are not satisfied or `preferHexDisplay` is `true`, the payload is displayed as hex bytes. The ASCII conditions are:

- The payload is a valid ASCII string.
- It must be clear to the user what the contents of the payload are, specifically there is:
Expand All @@ -42,7 +42,7 @@ TrezorConnect.cardanoSignMessage({
signingPath: "m/1852'/1815'/0'/0/0",
payload: '48656c6c6f205472657a6f7221', // "Hello Trezor!" in hex
hashPayload: true,
displayAscii: true,
preferHexDisplay: false,
});
```

Expand All @@ -53,7 +53,7 @@ TrezorConnect.cardanoSignMessage({
signingPath: "m/1852'/1815'/0'/0/0",
payload: '48656c6c6f205472657a6f7221', // "Hello Trezor!" in hex
hashPayload: true,
displayAscii: true,
preferHexDisplay: false,
networkId: 1,
protocolMagic: 764824073,
addressParameters: {
Expand All @@ -74,6 +74,7 @@ TrezorConnect.cardanoSignMessage({
payload: {
signature: string,
payload: string,
pubKey: string,
headers: {
protected: {
1: -8, // EdDSA algorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const batch = [
value: true,
},
{
name: 'displayAscii',
label: 'Display payload as ASCII on Trezor',
name: 'preferHexDisplay',
label: 'Display payload as hex bytes on Trezor even if it is valid ASCII',
type: 'checkbox',
value: false,
},
Expand Down
43 changes: 32 additions & 11 deletions packages/connect/e2e/__fixtures__/cardanoSignMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,30 @@ export default {
signingPath: "m/1852'/1815'/0'/0/0",
payload: '54657374', // "Test" hex
hashPayload: false,
displayAscii: true,
},
result: {
payload: '54657374',
signature:
'1c2c7612840654a56d61b58df36f41a4b47ad4034140ea369269c143f2732b2702c42fa753a8c52a9b662ba02944e43ec95c59cb892bf01cdd4a7f1c9397490c',
headers: headerUnhashed('80f9e2c88e6c817008f3a812ed889b4a4da8e0bd103f86e7335422aa'),
pubKey: '5d010cf16fdeff40955633d6c565f3844a288a24967cf6b76acbeb271b4f13c1',
},
legacyResults: [legacyResults.beforeMessageSigning],
},
{
description: 'Sign short ASCII payload and display as hex',
params: {
signingPath: "m/1852'/1815'/0'/0/0",
payload: '54657374', // "Test" hex
hashPayload: false,
preferHexDisplay: true,
},
result: {
payload: '54657374',
signature:
'1c2c7612840654a56d61b58df36f41a4b47ad4034140ea369269c143f2732b2702c42fa753a8c52a9b662ba02944e43ec95c59cb892bf01cdd4a7f1c9397490c',
headers: headerUnhashed('80f9e2c88e6c817008f3a812ed889b4a4da8e0bd103f86e7335422aa'),
pubKey: '5d010cf16fdeff40955633d6c565f3844a288a24967cf6b76acbeb271b4f13c1',
},
legacyResults: [legacyResults.beforeMessageSigning],
},
Expand All @@ -56,7 +73,6 @@ export default {
signingPath: "m/1852'/1815'/0'/0/0",
payload: '54657374', // "Test" hex
hashPayload: false,
displayAscii: true,
networkId: 1,
protocolMagic: 764824073,
addressParameters: {
Expand All @@ -72,6 +88,7 @@ export default {
headers: headerUnhashed(
'0180f9e2c88e6c817008f3a812ed889b4a4da8e0bd103f86e7335422aa122a946b9ad3d2ddf029d3a828f0468aece76895f15c9efbd69b4277',
),
pubKey: '5d010cf16fdeff40955633d6c565f3844a288a24967cf6b76acbeb271b4f13c1',
},
legacyResults: [legacyResults.beforeMessageSigning],
},
Expand All @@ -81,13 +98,13 @@ export default {
signingPath: "m/1852'/1815'/0'/0/0",
payload: 'ff',
hashPayload: false,
displayAscii: false,
},
result: {
payload: 'ff',
signature:
'003a3631d6c7509c2ebfbeb955c7f6a6b214c4283c2cbc10fc7eda6f2237881c7b219e4b28f3004d50cf528ad325b2d4f10425003096f80db58fc160365d920d',
headers: headerUnhashed('80f9e2c88e6c817008f3a812ed889b4a4da8e0bd103f86e7335422aa'),
pubKey: '5d010cf16fdeff40955633d6c565f3844a288a24967cf6b76acbeb271b4f13c1',
},
legacyResults: [legacyResults.beforeMessageSigning],
},
Expand All @@ -97,34 +114,38 @@ export default {
signingPath: "m/1852'/1815'/0'/0/0",
payload: HELLO_TREZOR_86,
hashPayload: true,
displayAscii: true,
},
result: {
payload: HELLO_TREZOR_86,
signature:
'39dba8107fb840b0aeff3f45eaddf9612cd4fd640a18cbe28ea2448b8ba2fea99b67cd9662a46cc7a70e1ad0d6399008d5fad9d67ddb437a623b594bf93b8e0f',
headers: headerHashed('80f9e2c88e6c817008f3a812ed889b4a4da8e0bd103f86e7335422aa'),
pubKey: '5d010cf16fdeff40955633d6c565f3844a288a24967cf6b76acbeb271b4f13c1',
},
legacyResults: [legacyResults.beforeMessageSigning],
},
{
description: 'Sign long ASCII payload without hashing',
description: 'Display ambigous-looking " " ASCII payload as hex',
params: {
signingPath: "m/1852'/1815'/0'/0/0",
payload: HELLO_TREZOR_86,
payload: '20', // " " (single space) hex
hashPayload: false,
displayAscii: true,
},
result: false,
result: {
payload: '20',
signature:
'5f3c4a4240b48686cb3ee95ed75c9152222023b630c6a0daab3c2a028ba484e98114451aa707139d65e2e6b9af7f9d45f82bfc75c752179877aee2675a662d05',
headers: headerUnhashed('80f9e2c88e6c817008f3a812ed889b4a4da8e0bd103f86e7335422aa'),
pubKey: '5d010cf16fdeff40955633d6c565f3844a288a24967cf6b76acbeb271b4f13c1',
},
legacyResults: [legacyResults.beforeMessageSigning],
},
{
description: 'Display ambigous-looking " " ASCII payload',
description: 'Sign long ASCII payload without hashing',
params: {
signingPath: "m/1852'/1815'/0'/0/0",
payload: '20', // " " (single space) hex
payload: HELLO_TREZOR_86,
hashPayload: false,
displayAscii: true,
},
result: false,
legacyResults: [legacyResults.beforeMessageSigning],
Expand Down
9 changes: 5 additions & 4 deletions packages/connect/src/api/cardano/api/cardanoSignMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type CardanoSignMessageParams = {
signingPath: Path;
payload: string;
hashPayload: boolean;
displayAscii: boolean;
preferHexDisplay: boolean;
networkId?: number;
protocolMagic?: number;
addressParameters?: PROTO.CardanoAddressParametersType;
Expand Down Expand Up @@ -54,7 +54,7 @@ export default class CardanoSignMessage extends AbstractMethod<
signingPath: validatePath(payload.signingPath, 5),
payload: payload.payload,
hashPayload: payload.hashPayload,
displayAscii: payload.displayAscii,
preferHexDisplay: payload.preferHexDisplay ?? false,
networkId: payload.networkId,
protocolMagic: payload.protocolMagic,
addressParameters:
Expand All @@ -76,7 +76,7 @@ export default class CardanoSignMessage extends AbstractMethod<
network_id: this.params.networkId,
protocol_magic: this.params.protocolMagic,
address_parameters: this.params.addressParameters,
display_ascii: this.params.displayAscii,
prefer_hex_display: this.params.preferHexDisplay,
derivation_type: this.params.derivationType,
});

Expand All @@ -89,13 +89,14 @@ export default class CardanoSignMessage extends AbstractMethod<
);

const {
message: { signature, address },
message: { signature, address, pub_key },
} = await typedCall('CardanoMessageItemHostAck', 'CardanoSignMessageFinished');

return {
signature,
payload: this.params.payload,
headers: this._createHeaders(address),
pubKey: pub_key,
};
}

Expand Down
3 changes: 2 additions & 1 deletion packages/connect/src/types/api/__tests__/cardano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ export const cardanoSignMessage = async (api: TrezorConnect) => {
signingPath: 'm/44',
payload: 'Test..',
hashPayload: true,
displayAscii: true,
preferHexDisplay: false,
networkId: 0,
protocolMagic: 0,
addressParameters: {
Expand All @@ -450,6 +450,7 @@ export const cardanoSignMessage = async (api: TrezorConnect) => {
const { payload } = sign;
payload.payload.toLowerCase();
payload.signature.toLowerCase();
payload.pubKey.toLowerCase();
const { headers } = payload;
headers.protected[1].toFixed();
headers.protected.address.toLowerCase();
Expand Down
3 changes: 2 additions & 1 deletion packages/connect/src/types/api/cardano/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export const CardanoSignMessage = Type.Object({
signingPath: DerivationPath,
payload: Type.String(),
hashPayload: Type.Boolean(),
displayAscii: Type.Boolean(),
preferHexDisplay: Type.Optional(Type.Boolean()),
networkId: Type.Optional(Type.Number()),
protocolMagic: Type.Optional(Type.Number()),
addressParameters: Type.Optional(CardanoAddressParameters),
Expand Down Expand Up @@ -324,4 +324,5 @@ export const CardanoSignedMessage = Type.Object({
headers: CardanoMessageHeaders,
payload: Type.String(),
signature: Type.String(),
pubKey: Type.String(),
});
7 changes: 6 additions & 1 deletion packages/protobuf/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,7 @@
"type": "bool",
"id": 5
},
"display_ascii": {
"prefer_hex_display": {
"rule": "required",
"type": "bool",
"id": 6
Expand Down Expand Up @@ -2544,6 +2544,11 @@
"rule": "required",
"type": "bytes",
"id": 2
},
"pub_key": {
"rule": "required",
"type": "bytes",
"id": 3
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion packages/protobuf/src/messages-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ export const CardanoSignMessageInit = Type.Object({
signing_path: Type.Array(Type.Number()),
payload_size: Type.Number(),
hash_payload: Type.Boolean(),
display_ascii: Type.Boolean(),
prefer_hex_display: Type.Boolean(),
address_parameters: Type.Optional(CardanoAddressParametersType),
derivation_type: EnumCardanoDerivationType,
});
Expand All @@ -1056,6 +1056,7 @@ export type CardanoSignMessageFinished = Static<typeof CardanoSignMessageFinishe
export const CardanoSignMessageFinished = Type.Object({
signature: Type.String(),
address: Type.String(),
pub_key: Type.String(),
});

export type Success = Static<typeof Success>;
Expand Down
3 changes: 2 additions & 1 deletion packages/protobuf/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ export type CardanoSignMessageInit = {
signing_path: number[];
payload_size: number;
hash_payload: boolean;
display_ascii: boolean;
prefer_hex_display: boolean;
address_parameters?: CardanoAddressParametersType;
derivation_type: CardanoDerivationType;
};
Expand All @@ -972,6 +972,7 @@ export type CardanoMessageItemHostAck = {};
export type CardanoSignMessageFinished = {
signature: string;
address: string;
pub_key: string;
};

// Success
Expand Down

0 comments on commit 77cfd51

Please sign in to comment.