forked from trezor/trezor-suite
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(connect): Cardano message signing
- Loading branch information
Showing
16 changed files
with
445 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ALGORITHM_IDS } from '../../src/constants/cardano'; | ||
|
||
const legacyResults = { | ||
beforeMessageSigning: { | ||
rules: ['<2.6.5', '1'], | ||
success: false, | ||
}, | ||
}; | ||
|
||
export default { | ||
method: 'cardanoSignMessage', | ||
setup: { | ||
mnemonic: 'mnemonic_all', | ||
}, | ||
tests: [ | ||
{ | ||
description: 'Sign short ASCII payload hash', | ||
params: { | ||
signingPath: "m/1852'/1815'/0'/0/0", | ||
payload: '54657374', | ||
hashPayload: true, | ||
displayAscii: true, | ||
}, | ||
result: { | ||
payload: '54657374', | ||
signature: | ||
'cde9451e081f325ed9991b5c20f22c7220526f97e646abee71b8fe232e475b8b06a98df28fdec911e81a050d47c0fcbe3b629d38fc12730fb74ab0a5f56f7f05', | ||
headers: { | ||
protected: { | ||
1: ALGORITHM_IDS.EdDSA, | ||
address: '80f9e2c88e6c817008f3a812ed889b4a4da8e0bd103f86e7335422aa', | ||
}, | ||
unprotected: { | ||
hashed: true, | ||
version: 1, | ||
}, | ||
}, | ||
}, | ||
legacyResults: [legacyResults.beforeMessageSigning], | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/connect/src/api/cardano/api/cardanoSignMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { AbstractMethod } from '../../../core/AbstractMethod'; | ||
import { PROTO, CARDANO, ERRORS } from '../../../constants'; | ||
import { getFirmwareRange } from '../../common/paramsValidator'; | ||
import { getMiscNetwork } from '../../../data/coinInfo'; | ||
import { Path } from '../cardanoInputs'; | ||
import { validatePath } from '../../../utils/pathUtils'; | ||
import { hexStringByteLength, sendChunkedHexString } from '../cardanoUtils'; | ||
import { | ||
CardanoSignMessage as CardanoSignMessageSchema, | ||
CardanoMessageHeaders, | ||
CardanoSignedMessage, | ||
} from '../../../types/api/cardano'; | ||
import { addressParametersToProto } from '../cardanoAddressParameters'; | ||
import { Assert } from '@trezor/schema-utils'; | ||
import { hasHexPrefix, isHexString } from '../../../utils/formatUtils'; | ||
|
||
export type CardanoSignMessageParams = { | ||
signingPath: Path; | ||
payload: string; | ||
hashPayload: boolean; | ||
displayAscii: boolean; | ||
networkId?: number; | ||
protocolMagic?: number; | ||
addressParameters?: PROTO.CardanoAddressParametersType; | ||
derivationType: PROTO.CardanoDerivationType; | ||
}; | ||
|
||
export default class CardanoSignMessage extends AbstractMethod< | ||
'cardanoSignMessage', | ||
CardanoSignMessageParams | ||
> { | ||
static readonly VERSION = 1; | ||
|
||
init(): void { | ||
this.requiredPermissions = ['read', 'write']; | ||
this.firmwareRange = getFirmwareRange( | ||
this.name, | ||
getMiscNetwork('Cardano'), | ||
this.firmwareRange, | ||
); | ||
|
||
const { payload } = this; | ||
|
||
Assert(CardanoSignMessageSchema, payload); | ||
|
||
if (!isHexString(payload.payload) || hasHexPrefix(payload.payload)) { | ||
throw ERRORS.TypedError( | ||
'Method_InvalidParameter', | ||
'Message payload must be a hexadecimal string without a "0x" prefix.', | ||
); | ||
} | ||
|
||
this.params = { | ||
signingPath: validatePath(payload.signingPath, 5), | ||
payload: payload.payload, | ||
hashPayload: payload.hashPayload, | ||
displayAscii: payload.displayAscii, | ||
networkId: payload.networkId, | ||
protocolMagic: payload.protocolMagic, | ||
addressParameters: | ||
payload.addressParameters && addressParametersToProto(payload.addressParameters), | ||
derivationType: payload.derivationType ?? PROTO.CardanoDerivationType.ICARUS_TREZOR, | ||
}; | ||
} | ||
|
||
async run(): Promise<CardanoSignedMessage> { | ||
const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands()); | ||
|
||
const payloadSize = hexStringByteLength(this.params.payload); | ||
const MAX_CHUNK_SIZE = 1024 * 2; // 1024 hex-encoded bytes | ||
|
||
await typedCall('CardanoSignMessageInit', 'CardanoMessageItemAck', { | ||
signing_path: this.params.signingPath, | ||
payload_size: payloadSize, | ||
hash_payload: this.params.hashPayload, | ||
network_id: this.params.networkId, | ||
protocol_magic: this.params.protocolMagic, | ||
address_parameters: this.params.addressParameters, | ||
display_ascii: this.params.displayAscii, | ||
derivation_type: this.params.derivationType, | ||
}); | ||
|
||
await sendChunkedHexString( | ||
typedCall, | ||
this.params.payload, | ||
MAX_CHUNK_SIZE, | ||
'CardanoMessagePayloadChunk', | ||
'CardanoMessageItemAck', | ||
); | ||
|
||
const { | ||
message: { signature, address }, | ||
} = await typedCall('CardanoMessageItemHostAck', 'CardanoSignMessageFinished'); | ||
|
||
return { | ||
signature, | ||
payload: this.params.payload, | ||
headers: this._createHeaders(address), | ||
}; | ||
} | ||
|
||
_createHeaders(address: string): CardanoMessageHeaders { | ||
return { | ||
protected: { | ||
1: CARDANO.ALGORITHM_IDS.EdDSA, | ||
address, | ||
}, | ||
unprotected: { | ||
hashed: this.params.hashPayload, | ||
version: CardanoSignMessage.VERSION, | ||
}, | ||
}; | ||
} | ||
|
||
get info() { | ||
return 'Sign Cardano message'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,7 @@ export enum NETWORK_IDS { | |
mainnet = 1, | ||
testnet = 0, | ||
} | ||
|
||
export enum ALGORITHM_IDS { | ||
EdDSA = -8, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { Params, Response } from '../params'; | ||
import type { CardanoSignMessage, CardanoSignedMessage } from './cardano'; | ||
|
||
export declare function cardanoSignMessage( | ||
params: Params<CardanoSignMessage>, | ||
): Response<CardanoSignedMessage>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.