-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsignAndSendTransaction.ts
More file actions
350 lines (327 loc) · 11.7 KB
/
Copy pathsignAndSendTransaction.ts
File metadata and controls
350 lines (327 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { TransactionType } from '@metamask/keyring-api';
import type { CaipAssetType } from '@metamask/utils';
import { parseCaipAssetType } from '@metamask/utils';
import type {
SignAndSendTransactionJsonRpcRequest,
SignAndSendTransactionJsonRpcResponse,
} from './api';
import {
SignAndSendTransactionJsonRpcRequestStruct,
SignAndSendTransactionJsonRpcResponseStruct,
} from './api';
import type {
KnownCaip19AssetIdOrSlip44Id,
KnownCaip2ChainId,
} from '../../api';
import type {
AccountResolver,
ResolvedActivatedAccount,
} from '../accountResolver';
import { BaseClientRequestHandler } from './base';
import { METAMASK_ORIGIN, STELLAR_DECIMAL_PLACES } from '../../constants';
import type { StellarKeyringAccount } from '../../services/account';
import type {
AssetMetadataService,
StellarAssetMetadata,
} from '../../services/asset-metadata';
import {
getNativeAssetMetadata,
toStellarAssetMetadata,
} from '../../services/asset-metadata/utils';
import { StellarOperationType } from '../../services/transaction/api';
import { KeyringTransactionType } from '../../services/transaction/KeyringTransactionBuilder';
import type { Transaction } from '../../services/transaction/Transaction';
import type { TransactionService } from '../../services/transaction/TransactionService';
import { isPathPaymentOperation } from '../../services/transaction/utils';
import {
isClassicAssetId,
isSlip44Id,
parseClassicAssetCodeIssuer,
removeTrailingZeros,
trackErrorIfNeeded,
} from '../../utils';
import { createPrefixedLogger } from '../../utils/logger';
import type { ILogger } from '../../utils/logger';
import { trackTransactionSubmitted } from '../../utils/snap';
import { TrackTransactionHandler } from '../cronjob/trackTransaction';
type SwapAssetIds = {
sourceAssetId: KnownCaip19AssetIdOrSlip44Id;
destAssetId: CaipAssetType;
};
export class SignAndSendTransactionHandler extends BaseClientRequestHandler<
SignAndSendTransactionJsonRpcRequest,
SignAndSendTransactionJsonRpcResponse
> {
readonly #transactionService: TransactionService;
readonly #assetMetadataService: AssetMetadataService;
constructor({
logger,
accountResolver,
transactionService,
assetMetadataService,
}: {
logger: ILogger;
accountResolver: AccountResolver;
transactionService: TransactionService;
assetMetadataService: AssetMetadataService;
}) {
const prefixedLogger = createPrefixedLogger(
logger,
'[👋 SignAndSendTransactionHandler]',
);
super({
accountResolver,
logger: prefixedLogger,
requestStruct: SignAndSendTransactionJsonRpcRequestStruct,
responseStruct: SignAndSendTransactionJsonRpcResponseStruct,
});
this.#transactionService = transactionService;
this.#assetMetadataService = assetMetadataService;
}
/**
* Signs and submits the envelope built by MetaMask CrossChain API and quoted by {@link ComputeFeeHandler}.
*
* Use the **same** `params.transaction` and `params.scope` as **computeFee** so the signed submission
* matches the quoted envelope. The user must remain the transaction source. Decoding and validation
* use {@link TransactionService.createValidatedSwapTransaction}.
*
* CRITICAL SECURITY REQUIREMENT:
* This method does NOT request user confirmation. The caller is responsible
* for obtaining explicit user consent before invoking this method.
*
* The caller MUST:
* - Display transaction details (recipient, amount, fees) to the user
* - Obtain explicit user approval before calling this method
* - Validate transaction authenticity and integrity
*
* Failure to implement caller-side consent will result in transactions being
* signed and broadcast without user knowledge, creating a critical security
* vulnerability.
*
* @param resolved - The resolved and activated account and wallet ({@link ResolvedActivatedAccount}).
* @param request - The JSON-RPC request containing transaction details.
* @param request.params.transaction - The Base64 encoded XDR of the transaction.
* @param request.params.scope - The CAIP-2 chain ID.
* @returns A promise that resolves to the JSON-RPC response ({@link SignAndSendTransactionJsonRpcResponse}).
*/
protected async execute(
resolved: ResolvedActivatedAccount,
request: SignAndSendTransactionJsonRpcRequest,
): Promise<SignAndSendTransactionJsonRpcResponse> {
const { wallet, onChainAccount, account } = resolved;
const {
transaction: transactionBase64Xdr,
scope,
options,
} = request.params;
const transaction =
await this.#transactionService.createValidatedSwapTransaction({
xdr: transactionBase64Xdr,
scope,
onChainAccount,
});
wallet.signTransaction(transaction);
const transactionHash = await this.#transactionService.sendTransaction({
wallet,
onChainAccount,
scope,
transaction,
pollTransaction: false,
});
await trackTransactionSubmitted({
origin: METAMASK_ORIGIN,
accountType: account.type,
chainIdCaip: scope,
});
const { sourceAssetId, destAssetId } = options;
const swapAssetIds = { sourceAssetId, destAssetId };
await this.#savePendingTransaction({
transactionId: transactionHash,
account,
scope,
transaction,
swapAssetIds,
});
// Schedule the track-transaction background event.
await TrackTransactionHandler.scheduleBackgroundEvent({
scope,
txId: transactionHash,
// Same-chain swaps reuse the sender address as the receiver; cross-chain swaps
// use a non-Stellar receiver, so only the sender account id is tracked.
accountIdsOrAddresses: [account.id],
});
return {
transactionId: transactionHash,
};
}
/**
* A swap is cross-chain when its source and destination assets live on
* different chains (e.g. a Stellar asset bridged to an EVM asset).
*
* @param swapAssetIds - The source and destination asset ids.
* @returns True when the two assets belong to different chains.
*/
#isCrossChain(swapAssetIds: SwapAssetIds): boolean {
const { chainId: sourceChainId } = parseCaipAssetType(
swapAssetIds.sourceAssetId,
);
const { chainId: destChainId } = parseCaipAssetType(
swapAssetIds.destAssetId,
);
return sourceChainId !== destChainId;
}
async #savePendingTransaction(params: {
transactionId: string;
scope: KnownCaip2ChainId;
account: StellarKeyringAccount;
transaction: Transaction;
swapAssetIds: SwapAssetIds;
}): Promise<void> {
try {
const { transactionId, scope, account, transaction, swapAssetIds } =
params;
if (this.#isCrossChain(swapAssetIds)) {
await this.#transactionService.savePendingKeyringTransactionSafe({
type: KeyringTransactionType.BridgeSend,
request: {
fees: this.#transactionService.keyringTransactionBuilder.getBaseFees(
transaction.totalFee,
scope,
),
txId: transactionId,
account,
scope,
transactionType: TransactionType.BridgeSend,
// For cross-chain swaps, we don't have any from and to assets,
// the client will map the assets based on the transaction Id.
from: [],
to: [],
},
});
} else {
// If it is a native swap, we can extract the send and dest amounts from the transaction.
// For contract swaps, we will fallback to have sendAmount and destAmount to 0.
const { sendAmount, destAmount } =
this.#resolveSameChainSwapAmounts(transaction);
const { sourceAssetMetadata, destAssetMetadata } =
await this.#resolveSameChainSwapAssetIds(
swapAssetIds.sourceAssetId,
swapAssetIds.destAssetId as KnownCaip19AssetIdOrSlip44Id,
scope,
);
await this.#transactionService.savePendingKeyringTransactionSafe({
type: KeyringTransactionType.Swap,
request: {
fees: this.#transactionService.keyringTransactionBuilder.getBaseFees(
transaction.totalFee,
scope,
),
txId: transactionId,
account,
scope,
toAddress: account.address,
fromAsset: {
unit: sourceAssetMetadata.units[0].symbol,
type: sourceAssetMetadata.assetId,
amount: sendAmount,
fungible: true,
},
toAsset: {
unit: destAssetMetadata.units[0].symbol,
type: destAssetMetadata.assetId,
amount: destAmount,
fungible: true,
},
},
});
}
} catch (error) {
await trackErrorIfNeeded(error);
this.logger.warn('Failed to map a transaction for swap and bridge send', {
error,
});
}
}
#resolveSameChainSwapAmounts(transaction: Transaction): {
sendAmount: string;
destAmount: string;
} {
// for any case we can detect if it is a swap transaction, we force the swap value to 0
// E.g: Swap with contract, Swap with payment.
let sendAmount = '0';
let destAmount = '0';
// In the Request Struct, we already ensure it map to our pattern,
// Either it is using native swap, or it is using contract swap.
// Contract swap will fallback to have sendAmount and destAmount to 0.
const swapOperationIndex = transaction.transactionOperations.findIndex(
(operation) => isPathPaymentOperation(operation),
);
if (swapOperationIndex >= 0) {
const swapOperation =
transaction.transactionOperations[swapOperationIndex];
if (swapOperation && isPathPaymentOperation(swapOperation)) {
// Extract the send/dest amount from the successful transaction result by the index of the swap operation.
if (swapOperation.type === StellarOperationType.PathPaymentStrictSend) {
destAmount = removeTrailingZeros(swapOperation.destMin);
sendAmount = removeTrailingZeros(swapOperation.sendAmount);
} else {
destAmount = removeTrailingZeros(swapOperation.destAmount);
sendAmount = removeTrailingZeros(swapOperation.sendMax);
}
}
}
return {
sendAmount,
destAmount,
};
}
async #resolveSameChainSwapAssetIds(
sourceAssetId: KnownCaip19AssetIdOrSlip44Id,
destAssetId: KnownCaip19AssetIdOrSlip44Id,
scope: KnownCaip2ChainId,
): Promise<{
sourceAssetMetadata: StellarAssetMetadata;
destAssetMetadata: StellarAssetMetadata;
}> {
const [sourceAssetMetadata, destAssetMetadata] = await Promise.all([
this.#resolveAsset(sourceAssetId, scope),
this.#resolveAsset(destAssetId, scope),
]);
return {
sourceAssetMetadata,
destAssetMetadata,
};
}
async #resolveAsset(
assetId: KnownCaip19AssetIdOrSlip44Id,
scope: KnownCaip2ChainId,
): Promise<StellarAssetMetadata> {
if (isSlip44Id(assetId)) {
return getNativeAssetMetadata(scope);
} else if (isClassicAssetId(assetId)) {
const { assetReference } = parseCaipAssetType(assetId);
const { assetCode } = parseClassicAssetCodeIssuer(assetReference);
return toStellarAssetMetadata({
assetId,
decimals: STELLAR_DECIMAL_PLACES,
symbol: assetCode,
name: assetCode,
});
}
try {
return await this.#assetMetadataService.resolve(assetId);
} catch (error) {
this.logger.warn(
'Failed to resolve asset metadata; using fallback symbol',
{ assetId, error },
);
const { assetReference } = parseCaipAssetType(assetId);
return toStellarAssetMetadata({
assetId,
decimals: STELLAR_DECIMAL_PLACES,
symbol: assetReference,
name: assetReference,
});
}
}
}