@onekeyfe/hd-core 0.2.7 → 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.
- package/dist/api/BaseMethod.d.ts.map +1 -1
- package/dist/api/cardano/CardanoGetAddress.d.ts +8 -0
- package/dist/api/cardano/CardanoGetAddress.d.ts.map +1 -0
- package/dist/api/cardano/CardanoGetPublicKey.d.ts +8 -0
- package/dist/api/cardano/CardanoGetPublicKey.d.ts.map +1 -0
- package/dist/api/cardano/CardanoSignTransaction.d.ts +9 -0
- package/dist/api/cardano/CardanoSignTransaction.d.ts.map +1 -0
- package/dist/api/cardano/helper/addressParameters.d.ts +7 -0
- package/dist/api/cardano/helper/addressParameters.d.ts.map +1 -0
- package/dist/api/cardano/helper/auxiliaryData.d.ts +5 -0
- package/dist/api/cardano/helper/auxiliaryData.d.ts.map +1 -0
- package/dist/api/cardano/helper/cardanoInputs.d.ts +14 -0
- package/dist/api/cardano/helper/cardanoInputs.d.ts.map +1 -0
- package/dist/api/cardano/helper/cardanoOutputs.d.ts +11 -0
- package/dist/api/cardano/helper/cardanoOutputs.d.ts.map +1 -0
- package/dist/api/cardano/helper/certificate.d.ts +9 -0
- package/dist/api/cardano/helper/certificate.d.ts.map +1 -0
- package/dist/api/cardano/helper/token.d.ts +3 -0
- package/dist/api/cardano/helper/token.d.ts.map +1 -0
- package/dist/api/cardano/helper/utils.d.ts +3 -0
- package/dist/api/cardano/helper/utils.d.ts.map +1 -0
- package/dist/api/cardano/helper/witnesses.d.ts +5 -0
- package/dist/api/cardano/helper/witnesses.d.ts.map +1 -0
- package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
- package/dist/api/helpers/paramsValidator.d.ts +2 -1
- package/dist/api/helpers/paramsValidator.d.ts.map +1 -1
- package/dist/api/index.d.ts +6 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/sui/SuiGetAddress.d.ts +18 -0
- package/dist/api/sui/SuiGetAddress.d.ts.map +1 -0
- package/dist/api/sui/SuiGetPublicKey.d.ts +21 -0
- package/dist/api/sui/SuiGetPublicKey.d.ts.map +1 -0
- package/dist/api/sui/SuiSignTransaction.d.ts +15 -0
- package/dist/api/sui/SuiSignTransaction.d.ts.map +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/device/Device.d.ts +1 -0
- package/dist/device/Device.d.ts.map +1 -1
- package/dist/index.d.ts +233 -3
- package/dist/index.js +1203 -97
- package/dist/inject.d.ts.map +1 -1
- package/dist/types/api/cardano.d.ts +158 -0
- package/dist/types/api/cardano.d.ts.map +1 -0
- package/dist/types/api/cardanoGetAddress.d.ts +33 -0
- package/dist/types/api/cardanoGetAddress.d.ts.map +1 -0
- package/dist/types/api/cardanoGetPublicKey.d.ts +23 -0
- package/dist/types/api/cardanoGetPublicKey.d.ts.map +1 -0
- package/dist/types/api/cardanoSignTransaction.d.ts +4 -0
- package/dist/types/api/cardanoSignTransaction.d.ts.map +1 -0
- package/dist/types/api/export.d.ts +3 -0
- package/dist/types/api/export.d.ts.map +1 -1
- package/dist/types/api/index.d.ts +12 -0
- package/dist/types/api/index.d.ts.map +1 -1
- package/dist/types/api/suiGetAddress.d.ts +15 -0
- package/dist/types/api/suiGetAddress.d.ts.map +1 -0
- package/dist/types/api/suiGetPublicKey.d.ts +14 -0
- package/dist/types/api/suiGetPublicKey.d.ts.map +1 -0
- package/dist/types/api/suiSignTransaction.d.ts +11 -0
- package/dist/types/api/suiSignTransaction.d.ts.map +1 -0
- package/dist/utils/patch.d.ts +1 -1
- package/dist/utils/patch.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/api/BaseMethod.ts +7 -1
- package/src/api/btc/helpers/signtxLegacy.ts +1 -1
- package/src/api/cardano/CardanoGetAddress.ts +87 -0
- package/src/api/cardano/CardanoGetPublicKey.ts +55 -0
- package/src/api/cardano/CardanoSignTransaction.ts +309 -0
- package/src/api/cardano/helper/addressParameters.ts +118 -0
- package/src/api/cardano/helper/auxiliaryData.ts +74 -0
- package/src/api/cardano/helper/cardanoInputs.ts +56 -0
- package/src/api/cardano/helper/cardanoOutputs.ts +89 -0
- package/src/api/cardano/helper/certificate.ts +199 -0
- package/src/api/cardano/helper/token.ts +43 -0
- package/src/api/cardano/helper/utils.ts +17 -0
- package/src/api/cardano/helper/witnesses.ts +63 -0
- package/src/api/firmware/uploadFirmware.ts +1 -3
- package/src/api/helpers/paramsValidator.ts +19 -1
- package/src/api/index.ts +8 -0
- package/src/api/sui/SuiGetAddress.ts +97 -0
- package/src/api/sui/SuiGetPublicKey.ts +63 -0
- package/src/api/sui/SuiSignTransaction.ts +48 -0
- package/src/core/index.ts +1 -0
- package/src/data/messages/messages.json +217 -69
- package/src/device/Device.ts +5 -0
- package/src/inject.ts +14 -0
- package/src/types/api/cardano.ts +186 -0
- package/src/types/api/cardanoGetAddress.ts +47 -0
- package/src/types/api/cardanoGetPublicKey.ts +33 -0
- package/src/types/api/cardanoSignTransaction.ts +8 -0
- package/src/types/api/export.ts +4 -0
- package/src/types/api/index.ts +22 -0
- package/src/types/api/suiGetAddress.ts +24 -0
- package/src/types/api/suiGetPublicKey.ts +23 -0
- package/src/types/api/suiSignTransaction.ts +17 -0
package/src/api/BaseMethod.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
supportInputPinOnSoftware,
|
|
3
|
+
supportModifyHomescreen,
|
|
4
|
+
getDeviceType,
|
|
5
|
+
} from '../utils/deviceFeaturesUtils';
|
|
2
6
|
import { createDeviceMessage } from '../events/device';
|
|
3
7
|
import { UI_REQUEST } from '../constants/ui-request';
|
|
4
8
|
import { Device } from '../device/Device';
|
|
@@ -124,9 +128,11 @@ export abstract class BaseMethod<Params = undefined> {
|
|
|
124
128
|
checkDeviceSupportFeature() {
|
|
125
129
|
if (!this.device || !this.device.features) return;
|
|
126
130
|
const inputPinOnSoftware = supportInputPinOnSoftware(this.device.features);
|
|
131
|
+
const modifyHomescreen = supportModifyHomescreen(this.device.features);
|
|
127
132
|
this.postMessage(
|
|
128
133
|
createDeviceMessage(DEVICE.SUPPORT_FEATURES, {
|
|
129
134
|
inputPinOnSoftware,
|
|
135
|
+
modifyHomescreen,
|
|
130
136
|
device: this.device.toMessageObject(),
|
|
131
137
|
})
|
|
132
138
|
);
|
|
@@ -159,7 +159,7 @@ const saveTxSignatures = (
|
|
|
159
159
|
if (!signature) {
|
|
160
160
|
throw ERRORS.TypedError(
|
|
161
161
|
HardwareErrorCode.RuntimeError,
|
|
162
|
-
'saveTxSignatures: Unexpected null in
|
|
162
|
+
'saveTxSignatures: Unexpected null in oneKey:TxRequestSerialized signature.'
|
|
163
163
|
);
|
|
164
164
|
}
|
|
165
165
|
signatures[signature_index] = signature;
|
|
@@ -0,0 +1,87 @@
|
|
|
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
|
+
validateAddressParameters,
|
|
7
|
+
addressParametersToProto,
|
|
8
|
+
addressParametersFromProto,
|
|
9
|
+
} from './helper/addressParameters';
|
|
10
|
+
import { serializedPath } from '../helpers/pathUtils';
|
|
11
|
+
import { CardanoGetAddressParams, CardanoAddress } from '../../types/api/cardanoGetAddress';
|
|
12
|
+
|
|
13
|
+
export default class CardanoGetAddress extends BaseMethod<CardanoGetAddressParams[]> {
|
|
14
|
+
hasBundle?: boolean;
|
|
15
|
+
|
|
16
|
+
init() {
|
|
17
|
+
this.checkDeviceId = true;
|
|
18
|
+
this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.INITIALIZE];
|
|
19
|
+
|
|
20
|
+
this.hasBundle = !!this.payload?.bundle;
|
|
21
|
+
const payload = this.hasBundle ? this.payload : { bundle: [this.payload] };
|
|
22
|
+
|
|
23
|
+
this.params = payload.bundle.map((batch: any) => {
|
|
24
|
+
// validate incoming parameters for each batch
|
|
25
|
+
validateParams(batch, [
|
|
26
|
+
{ name: 'addressParameters', type: 'object', required: true },
|
|
27
|
+
{ name: 'networkId', type: 'number', required: true },
|
|
28
|
+
{ name: 'protocolMagic', type: 'number', required: true },
|
|
29
|
+
{ name: 'derivationType', type: 'number' },
|
|
30
|
+
{ name: 'address', type: 'string' },
|
|
31
|
+
{ name: 'showOnTrezor', type: 'boolean' },
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
validateAddressParameters(batch.addressParameters);
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
address_parameters: addressParametersToProto(batch.addressParameters),
|
|
38
|
+
address: batch.address,
|
|
39
|
+
protocol_magic: batch.protocolMagic,
|
|
40
|
+
network_id: batch.networkId,
|
|
41
|
+
derivation_type:
|
|
42
|
+
typeof batch.derivationType !== 'undefined'
|
|
43
|
+
? batch.derivationType
|
|
44
|
+
: PROTO.CardanoDerivationType.ICARUS_TREZOR,
|
|
45
|
+
show_display: typeof batch.showOnOneKey === 'boolean' ? !!batch.showOneKey : true,
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async run() {
|
|
51
|
+
const responses: CardanoAddress[] = [];
|
|
52
|
+
|
|
53
|
+
for (const batch of this.params) {
|
|
54
|
+
const { address_parameters, protocol_magic, network_id, derivation_type, show_display } =
|
|
55
|
+
batch;
|
|
56
|
+
const response = await this.device.commands.typedCall('CardanoGetAddress', 'CardanoAddress', {
|
|
57
|
+
address_parameters,
|
|
58
|
+
protocol_magic,
|
|
59
|
+
network_id,
|
|
60
|
+
derivation_type,
|
|
61
|
+
show_display,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const publicKeyRes = await this.device.commands.typedCall(
|
|
65
|
+
'CardanoGetPublicKey',
|
|
66
|
+
'CardanoPublicKey',
|
|
67
|
+
{
|
|
68
|
+
address_n: address_parameters.address_n.slice(0, 3),
|
|
69
|
+
derivation_type,
|
|
70
|
+
show_display,
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
responses.push({
|
|
75
|
+
addressParameters: addressParametersFromProto(batch.address_parameters),
|
|
76
|
+
protocolMagic: batch.protocol_magic,
|
|
77
|
+
networkId: batch.network_id,
|
|
78
|
+
serializedPath: serializedPath(batch.address_parameters.address_n),
|
|
79
|
+
serializedStakingPath: serializedPath(batch.address_parameters.address_n_staking),
|
|
80
|
+
address: response.message.address,
|
|
81
|
+
xpub: publicKeyRes.message.xpub,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return this.hasBundle ? responses : responses[0];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
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 { serializedPath, validatePath } from '../helpers/pathUtils';
|
|
6
|
+
import { CardanoPublicKeyParams, CardanoPublicKey } from '../../types/api/cardanoGetPublicKey';
|
|
7
|
+
|
|
8
|
+
export default class CardanoGetPublicKey extends BaseMethod<CardanoPublicKeyParams[]> {
|
|
9
|
+
hasBundle?: boolean;
|
|
10
|
+
|
|
11
|
+
init() {
|
|
12
|
+
this.checkDeviceId = true;
|
|
13
|
+
this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.INITIALIZE];
|
|
14
|
+
|
|
15
|
+
this.hasBundle = !!this.payload?.bundle;
|
|
16
|
+
const payload = this.hasBundle ? this.payload : { bundle: [this.payload] };
|
|
17
|
+
|
|
18
|
+
validateParams(payload, [{ name: 'bundle', type: 'array' }]);
|
|
19
|
+
|
|
20
|
+
this.params = payload.bundle.map((batch: any) => {
|
|
21
|
+
// validate incoming parameters for each batch
|
|
22
|
+
validateParams(batch, [
|
|
23
|
+
{ name: 'path', required: true },
|
|
24
|
+
{ name: 'derivationType', type: 'number' },
|
|
25
|
+
{ name: 'showOnTrezor', type: 'boolean' },
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
const path = validatePath(batch.path, 3);
|
|
29
|
+
return {
|
|
30
|
+
address_n: path,
|
|
31
|
+
derivation_type:
|
|
32
|
+
typeof batch.derivationType !== 'undefined'
|
|
33
|
+
? batch.derivationType
|
|
34
|
+
: PROTO.CardanoDerivationType.ICARUS_TREZOR,
|
|
35
|
+
show_display: typeof batch.showOnOneKey === 'boolean' ? batch.showOnOneKey : false,
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async run() {
|
|
41
|
+
const responses: CardanoPublicKey[] = [];
|
|
42
|
+
const cmd = this.device.getCommands();
|
|
43
|
+
for (let i = 0; i < this.params.length; i++) {
|
|
44
|
+
const batch = this.params[i];
|
|
45
|
+
const { message } = await cmd.typedCall('CardanoGetPublicKey', 'CardanoPublicKey', batch);
|
|
46
|
+
responses.push({
|
|
47
|
+
path: batch.address_n,
|
|
48
|
+
serializedPath: serializedPath(batch.address_n),
|
|
49
|
+
publicKey: message.xpub,
|
|
50
|
+
node: message.node,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return this.hasBundle ? responses : responses[0];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -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
|
+
};
|