@onekeyfe/hd-core 0.2.8 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/dist/api/BaseMethod.d.ts.map +1 -1
  2. package/dist/api/cardano/CardanoGetAddress.d.ts +8 -0
  3. package/dist/api/cardano/CardanoGetAddress.d.ts.map +1 -0
  4. package/dist/api/cardano/CardanoGetPublicKey.d.ts +8 -0
  5. package/dist/api/cardano/CardanoGetPublicKey.d.ts.map +1 -0
  6. package/dist/api/cardano/CardanoSignTransaction.d.ts +9 -0
  7. package/dist/api/cardano/CardanoSignTransaction.d.ts.map +1 -0
  8. package/dist/api/cardano/helper/addressParameters.d.ts +7 -0
  9. package/dist/api/cardano/helper/addressParameters.d.ts.map +1 -0
  10. package/dist/api/cardano/helper/auxiliaryData.d.ts +5 -0
  11. package/dist/api/cardano/helper/auxiliaryData.d.ts.map +1 -0
  12. package/dist/api/cardano/helper/cardanoInputs.d.ts +14 -0
  13. package/dist/api/cardano/helper/cardanoInputs.d.ts.map +1 -0
  14. package/dist/api/cardano/helper/cardanoOutputs.d.ts +11 -0
  15. package/dist/api/cardano/helper/cardanoOutputs.d.ts.map +1 -0
  16. package/dist/api/cardano/helper/certificate.d.ts +9 -0
  17. package/dist/api/cardano/helper/certificate.d.ts.map +1 -0
  18. package/dist/api/cardano/helper/token.d.ts +3 -0
  19. package/dist/api/cardano/helper/token.d.ts.map +1 -0
  20. package/dist/api/cardano/helper/utils.d.ts +3 -0
  21. package/dist/api/cardano/helper/utils.d.ts.map +1 -0
  22. package/dist/api/cardano/helper/witnesses.d.ts +5 -0
  23. package/dist/api/cardano/helper/witnesses.d.ts.map +1 -0
  24. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  25. package/dist/api/helpers/paramsValidator.d.ts +2 -1
  26. package/dist/api/helpers/paramsValidator.d.ts.map +1 -1
  27. package/dist/api/index.d.ts +3 -0
  28. package/dist/api/index.d.ts.map +1 -1
  29. package/dist/core/index.d.ts.map +1 -1
  30. package/dist/device/Device.d.ts +1 -0
  31. package/dist/device/Device.d.ts.map +1 -1
  32. package/dist/index.d.ts +192 -0
  33. package/dist/index.js +850 -18
  34. package/dist/inject.d.ts.map +1 -1
  35. package/dist/types/api/cardano.d.ts +158 -0
  36. package/dist/types/api/cardano.d.ts.map +1 -0
  37. package/dist/types/api/cardanoGetAddress.d.ts +33 -0
  38. package/dist/types/api/cardanoGetAddress.d.ts.map +1 -0
  39. package/dist/types/api/cardanoGetPublicKey.d.ts +23 -0
  40. package/dist/types/api/cardanoGetPublicKey.d.ts.map +1 -0
  41. package/dist/types/api/cardanoSignTransaction.d.ts +4 -0
  42. package/dist/types/api/cardanoSignTransaction.d.ts.map +1 -0
  43. package/dist/types/api/index.d.ts +6 -0
  44. package/dist/types/api/index.d.ts.map +1 -1
  45. package/package.json +4 -4
  46. package/src/api/BaseMethod.ts +7 -1
  47. package/src/api/btc/helpers/signtxLegacy.ts +1 -1
  48. package/src/api/cardano/CardanoGetAddress.ts +87 -0
  49. package/src/api/cardano/CardanoGetPublicKey.ts +55 -0
  50. package/src/api/cardano/CardanoSignTransaction.ts +309 -0
  51. package/src/api/cardano/helper/addressParameters.ts +118 -0
  52. package/src/api/cardano/helper/auxiliaryData.ts +74 -0
  53. package/src/api/cardano/helper/cardanoInputs.ts +56 -0
  54. package/src/api/cardano/helper/cardanoOutputs.ts +89 -0
  55. package/src/api/cardano/helper/certificate.ts +199 -0
  56. package/src/api/cardano/helper/token.ts +43 -0
  57. package/src/api/cardano/helper/utils.ts +17 -0
  58. package/src/api/cardano/helper/witnesses.ts +63 -0
  59. package/src/api/firmware/uploadFirmware.ts +1 -3
  60. package/src/api/helpers/paramsValidator.ts +19 -1
  61. package/src/api/index.ts +4 -0
  62. package/src/core/index.ts +1 -0
  63. package/src/data/messages/messages.json +107 -68
  64. package/src/device/Device.ts +5 -0
  65. package/src/inject.ts +7 -0
  66. package/src/types/api/cardano.ts +186 -0
  67. package/src/types/api/cardanoGetAddress.ts +47 -0
  68. package/src/types/api/cardanoGetPublicKey.ts +33 -0
  69. package/src/types/api/cardanoSignTransaction.ts +8 -0
  70. package/src/types/api/index.ts +11 -0
@@ -0,0 +1,309 @@
1
+ import { BaseMethod } from '../BaseMethod';
2
+ import { PROTO } from '../../constants';
3
+ import { UI_REQUEST } from '../../constants/ui-request';
4
+ import { validateParams } from '../helpers/paramsValidator';
5
+ import {
6
+ transformInput,
7
+ Path,
8
+ CollateralInputWithPath,
9
+ transformCollateralInput,
10
+ transformReferenceInput,
11
+ } from './helper/cardanoInputs';
12
+ import { transformOutput, sendOutput } from './helper/cardanoOutputs';
13
+ import { transformCertificate } from './helper/certificate';
14
+ import { tokenBundleToProto } from './helper/token';
15
+ import {
16
+ transformAuxiliaryData,
17
+ modifyAuxiliaryDataForBackwardsCompatibility,
18
+ } from './helper/auxiliaryData';
19
+ import { gatherWitnessPaths } from './helper/witnesses';
20
+ import { validatePath } from '../helpers/pathUtils';
21
+ import type {
22
+ CertificateWithPoolOwnersAndRelays,
23
+ AssetGroupWithTokens,
24
+ CardanoSignedTxData,
25
+ CardanoSignedTxWitness,
26
+ CardanoAuxiliaryDataSupplement,
27
+ } from '../../types/api/cardano';
28
+
29
+ export default class CardanoSignTransaction extends BaseMethod<any> {
30
+ hasBundle?: boolean;
31
+
32
+ init() {
33
+ this.checkDeviceId = true;
34
+ this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.INITIALIZE];
35
+
36
+ this.hasBundle = !!this.payload?.bundle;
37
+
38
+ const { payload } = this;
39
+
40
+ // validate incoming parameters
41
+ validateParams(payload, [
42
+ { name: 'signingMode', type: 'number', required: true },
43
+ { name: 'inputs', type: 'array', required: true },
44
+ { name: 'outputs', type: 'array', required: true, allowEmpty: true },
45
+ { name: 'fee', type: 'uint', required: true },
46
+ { name: 'ttl', type: 'uint' },
47
+ { name: 'certificates', type: 'array', allowEmpty: true },
48
+ { name: 'withdrawals', type: 'array', allowEmpty: true },
49
+ { name: 'mint', type: 'array', allowEmpty: true },
50
+ { name: 'validityIntervalStart', type: 'uint' },
51
+ { name: 'scriptDataHash', type: 'string' },
52
+ { name: 'collateralInputs', type: 'array', allowEmpty: true },
53
+ { name: 'requiredSigners', type: 'array', allowEmpty: true },
54
+ { name: 'totalCollateral', type: 'uint' },
55
+ { name: 'referenceInputs', type: 'array', allowEmpty: true },
56
+ { name: 'protocolMagic', type: 'number', required: true },
57
+ { name: 'networkId', type: 'number', required: true },
58
+ { name: 'additionalWitnessRequests', type: 'array', allowEmpty: true },
59
+ { name: 'derivationType', type: 'number' },
60
+ { name: 'includeNetworkId', type: 'boolean' },
61
+ ]);
62
+
63
+ const inputsWithPath = payload.inputs.map(transformInput);
64
+ const outputsWithData = payload.outputs.map(transformOutput);
65
+
66
+ let certificatesWithPoolOwnersAndRelays: CertificateWithPoolOwnersAndRelays[] = [];
67
+ if (payload.certificates) {
68
+ certificatesWithPoolOwnersAndRelays = payload.certificates.map(transformCertificate);
69
+ }
70
+
71
+ let withdrawals: PROTO.CardanoTxWithdrawal[] = [];
72
+ if (payload.withdrawals) {
73
+ withdrawals = payload.withdrawals.map((withdrawal: any) => {
74
+ validateParams(withdrawal, [
75
+ { name: 'amount', type: 'uint', required: true },
76
+ { name: 'scriptHash', type: 'string' },
77
+ { name: 'keyHash', type: 'string' },
78
+ ]);
79
+ return {
80
+ path: withdrawal.path ? validatePath(withdrawal.path, 5) : undefined,
81
+ amount: withdrawal.amount,
82
+ script_hash: withdrawal.scriptHash,
83
+ key_hash: withdrawal.keyHash,
84
+ };
85
+ });
86
+ }
87
+
88
+ let mint: AssetGroupWithTokens[] = [];
89
+ if (payload.mint) {
90
+ mint = tokenBundleToProto(payload.mint);
91
+ }
92
+
93
+ let auxiliaryData;
94
+ if (payload.auxiliaryData) {
95
+ auxiliaryData = transformAuxiliaryData(payload.auxiliaryData);
96
+ }
97
+
98
+ let additionalWitnessRequests: Path[] = [];
99
+ if (payload.additionalWitnessRequests) {
100
+ additionalWitnessRequests = payload.additionalWitnessRequests.map((witnessRequest: any) =>
101
+ validatePath(witnessRequest, 3)
102
+ );
103
+ }
104
+
105
+ let collateralInputsWithPath: CollateralInputWithPath[] = [];
106
+ if (payload.collateralInputs) {
107
+ collateralInputsWithPath = payload.collateralInputs.map(transformCollateralInput);
108
+ }
109
+
110
+ // @ts-expect-error
111
+ let requiredSigners: PROTO.CardanoTxRequiredSigner[] = [];
112
+ if (payload.requiredSigners) {
113
+ requiredSigners = payload.requiredSigners.map((requiredSigner: any) => {
114
+ validateParams(requiredSigner, [{ name: 'keyHash', type: 'string' }]);
115
+ return {
116
+ key_path: requiredSigner.keyPath ? validatePath(requiredSigner.keyPath, 3) : undefined,
117
+ key_hash: requiredSigner.keyHash,
118
+ // @ts-expect-error
119
+ } as PROTO.CardanoTxRequiredSigner;
120
+ });
121
+ }
122
+
123
+ const collateralReturnWithData = payload.collateralReturn
124
+ ? transformOutput(payload.collateralReturn)
125
+ : undefined;
126
+
127
+ // @ts-expect-error
128
+ let referenceInputs: PROTO.CardanoTxReferenceInput[] = [];
129
+ if (payload.referenceInputs) {
130
+ referenceInputs = payload.referenceInputs.map(transformReferenceInput);
131
+ }
132
+
133
+ this.params = {
134
+ signingMode: payload.signingMode,
135
+ inputsWithPath,
136
+ outputsWithData,
137
+ fee: payload.fee,
138
+ ttl: payload.ttl,
139
+ certificatesWithPoolOwnersAndRelays,
140
+ withdrawals,
141
+ mint,
142
+ auxiliaryData,
143
+ validityIntervalStart: payload.validityIntervalStart,
144
+ scriptDataHash: payload.scriptDataHash,
145
+ collateralInputsWithPath,
146
+ requiredSigners,
147
+ collateralReturnWithData,
148
+ totalCollateral: payload.totalCollateral,
149
+ referenceInputs,
150
+ protocolMagic: payload.protocolMagic,
151
+ networkId: payload.networkId,
152
+ witnessPaths: gatherWitnessPaths(
153
+ inputsWithPath,
154
+ certificatesWithPoolOwnersAndRelays,
155
+ withdrawals,
156
+ collateralInputsWithPath,
157
+ requiredSigners,
158
+ additionalWitnessRequests,
159
+ payload.signingMode
160
+ ),
161
+ additionalWitnessRequests,
162
+ derivationType:
163
+ typeof payload.derivationType !== 'undefined'
164
+ ? payload.derivationType
165
+ : PROTO.CardanoDerivationType.ICARUS_TREZOR,
166
+ includeNetworkId: payload.includeNetworkId,
167
+ };
168
+ }
169
+
170
+ async signTx(): Promise<CardanoSignedTxData> {
171
+ const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands());
172
+
173
+ const hasAuxiliaryData = !!this.params.auxiliaryData;
174
+
175
+ const signTxInitMessage = {
176
+ signing_mode: this.params.signingMode,
177
+ protocol_magic: this.params.protocolMagic,
178
+ network_id: this.params.networkId,
179
+ inputs_count: this.params.inputsWithPath.length,
180
+ outputs_count: this.params.outputsWithData.length,
181
+ fee: this.params.fee,
182
+ ttl: this.params.ttl,
183
+ certificates_count: this.params.certificatesWithPoolOwnersAndRelays.length,
184
+ withdrawals_count: this.params.withdrawals.length,
185
+ has_auxiliary_data: hasAuxiliaryData,
186
+ validity_interval_start: this.params.validityIntervalStart,
187
+ witness_requests_count: this.params.witnessPaths.length,
188
+ minting_asset_groups_count: this.params.mint.length,
189
+ script_data_hash: this.params.scriptDataHash,
190
+ collateral_inputs_count: this.params.collateralInputsWithPath.length,
191
+ required_signers_count: this.params.requiredSigners.length,
192
+ has_collateral_return: this.params.collateralReturnWithData != null,
193
+ total_collateral: this.params.totalCollateral,
194
+ reference_inputs_count: this.params.referenceInputs.length,
195
+ derivation_type: this.params.derivationType,
196
+ include_network_id: this.params.includeNetworkId,
197
+ };
198
+
199
+ // init
200
+ await typedCall('CardanoSignTxInit', 'CardanoTxItemAck', signTxInitMessage);
201
+ // inputs
202
+ for (const { input } of this.params.inputsWithPath) {
203
+ await typedCall('CardanoTxInput', 'CardanoTxItemAck', input);
204
+ }
205
+ // outputs and tokens
206
+ for (const outputWithData of this.params.outputsWithData) {
207
+ await sendOutput(typedCall, outputWithData);
208
+ }
209
+ // certificates, owners and relays
210
+ for (const { certificate, poolOwners, poolRelays } of this.params
211
+ .certificatesWithPoolOwnersAndRelays) {
212
+ await typedCall('CardanoTxCertificate', 'CardanoTxItemAck', certificate);
213
+ for (const poolOwner of poolOwners) {
214
+ await typedCall('CardanoPoolOwner', 'CardanoTxItemAck', poolOwner);
215
+ }
216
+ for (const poolRelay of poolRelays) {
217
+ await typedCall('CardanoPoolRelayParameters', 'CardanoTxItemAck', poolRelay);
218
+ }
219
+ }
220
+ // withdrawals
221
+ for (const withdrawal of this.params.withdrawals) {
222
+ await typedCall('CardanoTxWithdrawal', 'CardanoTxItemAck', withdrawal);
223
+ }
224
+ // auxiliary data
225
+ let auxiliaryDataSupplement: CardanoAuxiliaryDataSupplement | undefined;
226
+ if (this.params.auxiliaryData) {
227
+ const { catalyst_registration_parameters } = this.params.auxiliaryData;
228
+ if (catalyst_registration_parameters) {
229
+ this.params.auxiliaryData = modifyAuxiliaryDataForBackwardsCompatibility(
230
+ this.params.auxiliaryData
231
+ );
232
+ }
233
+
234
+ const { message } = await typedCall(
235
+ 'CardanoTxAuxiliaryData',
236
+ 'CardanoTxAuxiliaryDataSupplement',
237
+ this.params.auxiliaryData
238
+ );
239
+
240
+ const auxiliaryDataType: any = PROTO.CardanoTxAuxiliaryDataSupplementType[message.type];
241
+ if (auxiliaryDataType !== PROTO.CardanoTxAuxiliaryDataSupplementType.NONE) {
242
+ auxiliaryDataSupplement = {
243
+ type: auxiliaryDataType,
244
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
245
+ auxiliaryDataHash: message.auxiliary_data_hash!,
246
+ catalystSignature: message.catalyst_signature,
247
+ };
248
+ }
249
+ await typedCall('CardanoTxHostAck', 'CardanoTxItemAck');
250
+ }
251
+ // mint
252
+ if (this.params.mint.length > 0) {
253
+ await typedCall('CardanoTxMint', 'CardanoTxItemAck', {
254
+ asset_groups_count: this.params.mint.length,
255
+ });
256
+ for (const assetGroup of this.params.mint) {
257
+ await typedCall('CardanoAssetGroup', 'CardanoTxItemAck', {
258
+ policy_id: assetGroup.policyId,
259
+ tokens_count: assetGroup.tokens.length,
260
+ });
261
+ for (const token of assetGroup.tokens) {
262
+ await typedCall('CardanoToken', 'CardanoTxItemAck', token);
263
+ }
264
+ }
265
+ }
266
+ // collateral inputs
267
+ for (const { collateralInput } of this.params.collateralInputsWithPath) {
268
+ // @ts-expect-error
269
+ await typedCall('CardanoTxCollateralInput', 'CardanoTxItemAck', collateralInput);
270
+ }
271
+ // required signers
272
+ for (const requiredSigner of this.params.requiredSigners) {
273
+ // @ts-expect-error
274
+ await typedCall('CardanoTxRequiredSigner', 'CardanoTxItemAck', requiredSigner);
275
+ }
276
+ // collateral return
277
+ if (this.params.collateralReturnWithData) {
278
+ await sendOutput(typedCall, this.params.collateralReturnWithData);
279
+ }
280
+ // reference inputs
281
+ for (const referenceInput of this.params.referenceInputs) {
282
+ // @ts-expect-error
283
+ await typedCall('CardanoTxReferenceInput', 'CardanoTxItemAck', referenceInput);
284
+ }
285
+ // witnesses
286
+ const witnesses: CardanoSignedTxWitness[] = [];
287
+ for (const path of this.params.witnessPaths) {
288
+ const { message } = await typedCall('CardanoTxWitnessRequest', 'CardanoTxWitnessResponse', {
289
+ path,
290
+ });
291
+ witnesses.push({
292
+ type: PROTO.CardanoTxWitnessType[message.type] as any,
293
+ pubKey: message.pub_key,
294
+ signature: message.signature,
295
+ chainCode: message.chain_code,
296
+ });
297
+ }
298
+ // tx hash
299
+ const { message: txBodyHashMessage } = await typedCall('CardanoTxHostAck', 'CardanoTxBodyHash');
300
+ // finish
301
+ await typedCall('CardanoTxHostAck', 'CardanoSignTxFinished');
302
+
303
+ return { hash: txBodyHashMessage.tx_hash, witnesses, auxiliaryDataSupplement };
304
+ }
305
+
306
+ run() {
307
+ return this.signTx();
308
+ }
309
+ }
@@ -0,0 +1,118 @@
1
+ import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
2
+ import { validatePath } from '../../helpers/pathUtils';
3
+ import { validateParams } from '../../helpers/paramsValidator';
4
+ import { PROTO } from '../../../constants';
5
+ import type { CardanoAddressParameters } from '../../../types/api/cardano';
6
+
7
+ export const validateAddressParameters = (addressParameters: CardanoAddressParameters) => {
8
+ validateParams(addressParameters, [
9
+ { name: 'addressType', type: 'number', required: true },
10
+ { name: 'stakingKeyHash', type: 'string' },
11
+ { name: 'paymentScriptHash', type: 'string' },
12
+ { name: 'stakingScriptHash', type: 'string' },
13
+ ]);
14
+
15
+ if (addressParameters.path) {
16
+ validatePath(addressParameters.path);
17
+ }
18
+ if (addressParameters.stakingPath) {
19
+ validatePath(addressParameters.stakingPath);
20
+ }
21
+
22
+ if (addressParameters.certificatePointer) {
23
+ validateParams(addressParameters.certificatePointer, [
24
+ { name: 'blockIndex', type: 'number', required: true },
25
+ { name: 'txIndex', type: 'number', required: true },
26
+ { name: 'certificateIndex', type: 'number', required: true },
27
+ ]);
28
+ }
29
+ };
30
+
31
+ export const addressParametersToProto = (
32
+ addressParameters: CardanoAddressParameters
33
+ ): PROTO.CardanoAddressParametersType => {
34
+ let path: number[] = [];
35
+ if (addressParameters.path) {
36
+ path = validatePath(addressParameters.path, 3);
37
+ }
38
+
39
+ let stakingPath: number[] = [];
40
+ if (addressParameters.stakingPath) {
41
+ stakingPath = validatePath(addressParameters.stakingPath, 3);
42
+ }
43
+
44
+ let certificatePointer;
45
+ if (addressParameters.certificatePointer) {
46
+ certificatePointer = {
47
+ block_index: addressParameters.certificatePointer.blockIndex,
48
+ tx_index: addressParameters.certificatePointer.txIndex,
49
+ certificate_index: addressParameters.certificatePointer.certificateIndex,
50
+ };
51
+ }
52
+
53
+ return {
54
+ address_type: addressParameters.addressType,
55
+ address_n: path,
56
+ address_n_staking: stakingPath,
57
+ staking_key_hash: addressParameters.stakingKeyHash,
58
+ certificate_pointer: certificatePointer,
59
+ script_payment_hash: addressParameters.paymentScriptHash,
60
+ script_staking_hash: addressParameters.stakingScriptHash,
61
+ };
62
+ };
63
+
64
+ export const addressParametersFromProto = (
65
+ addressParameters: PROTO.CardanoAddressParametersType
66
+ ): CardanoAddressParameters => {
67
+ let certificatePointer;
68
+ if (addressParameters.certificate_pointer) {
69
+ certificatePointer = {
70
+ blockIndex: addressParameters.certificate_pointer.block_index,
71
+ txIndex: addressParameters.certificate_pointer.tx_index,
72
+ certificateIndex: addressParameters.certificate_pointer.certificate_index,
73
+ };
74
+ }
75
+
76
+ return {
77
+ addressType: addressParameters.address_type,
78
+ path: addressParameters.address_n,
79
+ stakingPath: addressParameters.address_n_staking,
80
+ stakingKeyHash: addressParameters.staking_key_hash,
81
+ certificatePointer,
82
+ };
83
+ };
84
+
85
+ export const modifyAddressParametersForBackwardsCompatibility = (
86
+ address_parameters: PROTO.CardanoAddressParametersType
87
+ ): PROTO.CardanoAddressParametersType => {
88
+ if (address_parameters.address_type === PROTO.CardanoAddressType.REWARD) {
89
+ // older firmware expects reward address path in path field instead of staking path
90
+ let { address_n, address_n_staking } = address_parameters;
91
+
92
+ if (address_n.length > 0 && address_n_staking.length > 0) {
93
+ throw ERRORS.TypedError(
94
+ HardwareErrorCode.CallMethodInvalidParameter,
95
+ `Only stakingPath is allowed for CardanoAddressType.REWARD`
96
+ );
97
+ }
98
+
99
+ if (address_n.length > 0) {
100
+ address_n_staking = address_n;
101
+ address_n = [];
102
+ }
103
+ // TODO: version check
104
+ // if (device.atLeast(['0', '2.4.3'])) {
105
+ // } else if (address_n_staking.length > 0) {
106
+ // address_n = address_n_staking;
107
+ // address_n_staking = [];
108
+ // }
109
+
110
+ return {
111
+ ...address_parameters,
112
+ address_n,
113
+ address_n_staking,
114
+ };
115
+ }
116
+
117
+ return address_parameters;
118
+ };
@@ -0,0 +1,74 @@
1
+ import {
2
+ validateAddressParameters,
3
+ addressParametersToProto,
4
+ modifyAddressParametersForBackwardsCompatibility,
5
+ } from './addressParameters';
6
+ import { validateParams } from '../../helpers/paramsValidator';
7
+ import { validatePath } from '../../helpers/pathUtils';
8
+ import type {
9
+ CardanoAuxiliaryData,
10
+ CardanoCatalystRegistrationParameters,
11
+ } from '../../../types/api/cardano';
12
+ import { PROTO } from '../../../constants';
13
+
14
+ const transformCatalystRegistrationParameters = (
15
+ catalystRegistrationParameters: CardanoCatalystRegistrationParameters
16
+ ): PROTO.CardanoCatalystRegistrationParametersType => {
17
+ validateParams(catalystRegistrationParameters, [
18
+ { name: 'votingPublicKey', type: 'string', required: true },
19
+ { name: 'stakingPath', required: true },
20
+ { name: 'nonce', type: 'uint', required: true },
21
+ ]);
22
+ validateAddressParameters(catalystRegistrationParameters.rewardAddressParameters);
23
+
24
+ return {
25
+ voting_public_key: catalystRegistrationParameters.votingPublicKey,
26
+ staking_path: validatePath(catalystRegistrationParameters.stakingPath, 3),
27
+ reward_address_parameters: addressParametersToProto(
28
+ catalystRegistrationParameters.rewardAddressParameters
29
+ ),
30
+ nonce: catalystRegistrationParameters.nonce,
31
+ };
32
+ };
33
+
34
+ export const transformAuxiliaryData = (
35
+ auxiliaryData: CardanoAuxiliaryData
36
+ ): PROTO.CardanoTxAuxiliaryData => {
37
+ validateParams(auxiliaryData, [
38
+ {
39
+ name: 'hash',
40
+ type: 'string',
41
+ },
42
+ ]);
43
+
44
+ let catalystRegistrationParameters;
45
+ if (auxiliaryData.catalystRegistrationParameters) {
46
+ catalystRegistrationParameters = transformCatalystRegistrationParameters(
47
+ auxiliaryData.catalystRegistrationParameters
48
+ );
49
+ }
50
+
51
+ return {
52
+ hash: auxiliaryData.hash,
53
+ catalyst_registration_parameters: catalystRegistrationParameters,
54
+ };
55
+ };
56
+
57
+ export const modifyAuxiliaryDataForBackwardsCompatibility = (
58
+ auxiliary_data: PROTO.CardanoTxAuxiliaryData
59
+ ): PROTO.CardanoTxAuxiliaryData => {
60
+ const { catalyst_registration_parameters } = auxiliary_data;
61
+ if (catalyst_registration_parameters) {
62
+ catalyst_registration_parameters.reward_address_parameters =
63
+ modifyAddressParametersForBackwardsCompatibility(
64
+ catalyst_registration_parameters.reward_address_parameters
65
+ );
66
+
67
+ return {
68
+ ...auxiliary_data,
69
+ catalyst_registration_parameters,
70
+ };
71
+ }
72
+
73
+ return auxiliary_data;
74
+ };
@@ -0,0 +1,56 @@
1
+ import { validateParams } from '../../helpers/paramsValidator';
2
+ import { validatePath } from '../../helpers/pathUtils';
3
+ import type { PROTO } from '../../../constants';
4
+
5
+ export type Path = number[];
6
+
7
+ export type InputWithPath = {
8
+ input: PROTO.CardanoTxInput;
9
+ path?: Path;
10
+ };
11
+
12
+ export type CollateralInputWithPath = {
13
+ // @ts-expect-error
14
+ collateralInput: PROTO.CardanoTxCollateralInput;
15
+ path?: Path;
16
+ };
17
+
18
+ export const transformInput = (input: any): InputWithPath => {
19
+ validateParams(input, [
20
+ { name: 'prev_hash', type: 'string', required: true },
21
+ { name: 'prev_index', type: 'number', required: true },
22
+ ]);
23
+ return {
24
+ input: {
25
+ prev_hash: input.prev_hash,
26
+ prev_index: input.prev_index,
27
+ },
28
+ path: input.path ? validatePath(input.path, 5) : undefined,
29
+ };
30
+ };
31
+
32
+ export const transformCollateralInput = (collateralInput: any): CollateralInputWithPath => {
33
+ validateParams(collateralInput, [
34
+ { name: 'prev_hash', type: 'string', required: true },
35
+ { name: 'prev_index', type: 'number', required: true },
36
+ ]);
37
+ return {
38
+ collateralInput: {
39
+ prev_hash: collateralInput.prev_hash,
40
+ prev_index: collateralInput.prev_index,
41
+ },
42
+ path: collateralInput.path ? validatePath(collateralInput.path, 5) : undefined,
43
+ };
44
+ };
45
+
46
+ // @ts-expect-error
47
+ export const transformReferenceInput = (referenceInput: any): PROTO.CardanoTxReferenceInput => {
48
+ validateParams(referenceInput, [
49
+ { name: 'prev_hash', type: 'string', required: true },
50
+ { name: 'prev_index', type: 'number', required: true },
51
+ ]);
52
+ return {
53
+ prev_hash: referenceInput.prev_hash,
54
+ prev_index: referenceInput.prev_index,
55
+ };
56
+ };
@@ -0,0 +1,89 @@
1
+ import { validateParams } from '../../helpers/paramsValidator';
2
+ import { tokenBundleToProto } from './token';
3
+ import { addressParametersToProto, validateAddressParameters } from './addressParameters';
4
+ import { hexStringByteLength, sendChunkedHexString } from './utils';
5
+ import type { PROTO } from '../../../constants';
6
+ import type { AssetGroupWithTokens } from '../../../types/api/cardano';
7
+
8
+ export type OutputWithData = {
9
+ output: PROTO.CardanoTxOutput;
10
+ tokenBundle?: AssetGroupWithTokens[];
11
+ inlineDatum?: string;
12
+ referenceScript?: string;
13
+ };
14
+
15
+ export const transformOutput = (output: any): OutputWithData => {
16
+ validateParams(output, [
17
+ { name: 'address', type: 'string' },
18
+ { name: 'amount', type: 'uint', required: true },
19
+ { name: 'tokenBundle', type: 'array', allowEmpty: true },
20
+ { name: 'datumHash', type: 'string' },
21
+ { name: 'format', type: 'number' },
22
+ { name: 'inlineDatum', type: 'string' },
23
+ { name: 'referenceScript', type: 'string' },
24
+ ]);
25
+
26
+ const result: OutputWithData = {
27
+ output: {
28
+ amount: output.amount,
29
+ asset_groups_count: 0,
30
+ // @ts-expect-error
31
+ datum_hash: output.datumHash,
32
+ format: output.format,
33
+ inline_datum_size: output.inlineDatum ? hexStringByteLength(output.inlineDatum) : undefined,
34
+ reference_script_size: output.referenceScript
35
+ ? hexStringByteLength(output.referenceScript)
36
+ : undefined,
37
+ },
38
+ inlineDatum: output.inlineDatum,
39
+ referenceScript: output.referenceScript,
40
+ };
41
+
42
+ if (output.addressParameters) {
43
+ validateAddressParameters(output.addressParameters);
44
+ result.output.address_parameters = addressParametersToProto(output.addressParameters);
45
+ } else {
46
+ result.output.address = output.address;
47
+ }
48
+
49
+ if (output.tokenBundle) {
50
+ result.tokenBundle = tokenBundleToProto(output.tokenBundle);
51
+ result.output.asset_groups_count = result.tokenBundle.length;
52
+ } else {
53
+ result.output.asset_groups_count = 0;
54
+ }
55
+
56
+ return result;
57
+ };
58
+
59
+ export const sendOutput = async (typedCall: any, outputWithData: OutputWithData) => {
60
+ const MAX_CHUNK_SIZE = 1024 * 2; // 1024 hex-encoded bytes
61
+
62
+ const { output, tokenBundle, inlineDatum, referenceScript } = outputWithData;
63
+
64
+ await typedCall('CardanoTxOutput', 'CardanoTxItemAck', output);
65
+ if (tokenBundle) {
66
+ for (const assetGroup of tokenBundle) {
67
+ await typedCall('CardanoAssetGroup', 'CardanoTxItemAck', {
68
+ policy_id: assetGroup.policyId,
69
+ tokens_count: assetGroup.tokens.length,
70
+ });
71
+ for (const token of assetGroup.tokens) {
72
+ await typedCall('CardanoToken', 'CardanoTxItemAck', token);
73
+ }
74
+ }
75
+ }
76
+
77
+ if (inlineDatum) {
78
+ await sendChunkedHexString(typedCall, inlineDatum, MAX_CHUNK_SIZE, 'CardanoTxInlineDatumChunk');
79
+ }
80
+
81
+ if (referenceScript) {
82
+ await sendChunkedHexString(
83
+ typedCall,
84
+ referenceScript,
85
+ MAX_CHUNK_SIZE,
86
+ 'CardanoTxReferenceScriptChunk'
87
+ );
88
+ }
89
+ };