forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar.service.ts
More file actions
202 lines (183 loc) · 7.24 KB
/
Copy pathstellar.service.ts
File metadata and controls
202 lines (183 loc) · 7.24 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
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Horizon, Networks } from '@stellar/stellar-sdk';
import { HorizonApi } from '@stellar/stellar-sdk/lib/horizon/horizon_api';
@Injectable()
export class StellarService {
private readonly logger = new Logger(StellarService.name);
private readonly horizonServer: Horizon.Server;
constructor(private readonly configService: ConfigService) {
const horizonUrl =
this.configService.get<string>('STELLAR_HORIZON_URL') ||
(this.configService.get<string>('STELLAR_NETWORK') === 'mainnet'
? 'https://horizon.stellar.org'
: 'https://horizon-testnet.stellar.org');
this.horizonServer = new Horizon.Server(horizonUrl);
}
/**
* Verify a Stellar transaction by its hash
* @param txHash The transaction hash to verify
* @returns Transaction details or null if not found
*/
async verifyTransaction(txHash: string): Promise<{
valid: boolean;
amount: number;
asset: string;
sender: string;
receiver: string;
timestamp: string;
sourceAmount?: number;
sourceAsset?: string;
isPathPayment?: boolean;
path?: string[];
} | null> {
try {
this.logger.log(`Verifying transaction: ${txHash}`);
// Fetch the transaction from Horizon
const transaction = await this.horizonServer.transactions()
.transaction(txHash)
.call();
// Check if transaction exists and is successful
if (!transaction) {
this.logger.warn(`Transaction not found: ${txHash}`);
return null;
}
// Verify transaction is successful (not failed)
if (transaction.successful !== true) {
this.logger.warn(`Transaction failed: ${txHash}`);
return {
valid: false,
amount: 0,
asset: '',
sender: '',
receiver: '',
timestamp: transaction.created_at,
};
}
// Parse transaction operations to find payment details
const operations = await this.horizonServer.operations()
.forTransaction(txHash)
.limit(100) // Limit to reasonable number of operations
.call();
// Find the payment operation (typically the first one)
const paymentOp = operations.records.find((op: HorizonApi.BaseOperationResponse) =>
op.type === 'payment' || op.type === 'path_payment_strict_receive' || op.type === 'path_payment_strict_send'
);
if (!paymentOp) {
this.logger.warn(`No payment operation found in transaction: ${txHash}`);
return {
valid: false,
amount: 0,
asset: '',
sender: transaction.source_account,
receiver: '',
timestamp: transaction.created_at,
};
}
// Extract details based on operation type
let amount = 0;
let asset = '';
let receiver = '';
let sourceAmount: number | undefined;
let sourceAsset: string | undefined;
let isPathPayment = false;
let path: string[] | undefined;
if (paymentOp.type === 'payment') {
const paymentOperation = paymentOp as HorizonApi.PaymentOperationResponse;
amount = parseFloat(paymentOperation.amount);
asset = paymentOperation.asset_type === 'native'
? 'XLM'
: `${paymentOperation.asset_code}:${paymentOperation.asset_issuer}`;
receiver = paymentOperation.to;
} else if (paymentOp.type.includes('path_payment')) {
isPathPayment = true;
// For path payments, extract both source and destination details
if (paymentOp.type === 'path_payment_strict_receive') {
const strictReceiveOp = paymentOp as any; // Using any due to SDK type inconsistencies
amount = parseFloat(strictReceiveOp.amount);
asset = (strictReceiveOp.dest_asset_type || strictReceiveOp.asset_type) === 'native'
? 'XLM'
: `${strictReceiveOp.dest_asset_code || strictReceiveOp.asset_code}:${strictReceiveOp.dest_asset_issuer || strictReceiveOp.asset_issuer}`;
receiver = strictReceiveOp.to;
sourceAmount = parseFloat(strictReceiveOp.source_amount);
sourceAsset = strictReceiveOp.source_asset_type === 'native'
? 'XLM'
: `${strictReceiveOp.source_asset_code}:${strictReceiveOp.source_asset_issuer}`;
// Extract path
if (strictReceiveOp.path && strictReceiveOp.path.length > 0) {
path = strictReceiveOp.path.map((p: any) =>
p.asset_type === 'native' ? 'XLM' : `${p.asset_code}:${p.asset_issuer}`
);
}
} else if (paymentOp.type === 'path_payment_strict_send') {
const strictSendOp = paymentOp as any; // Using any due to SDK type inconsistencies
amount = parseFloat(strictSendOp.destination_amount || strictSendOp.amount);
asset = (strictSendOp.destination_asset_type || strictSendOp.asset_type) === 'native'
? 'XLM'
: `${strictSendOp.destination_asset_code || strictSendOp.asset_code}:${strictSendOp.destination_asset_issuer || strictSendOp.asset_issuer}`;
receiver = strictSendOp.destination || strictSendOp.to;
sourceAmount = parseFloat(strictSendOp.amount || strictSendOp.source_amount);
sourceAsset = strictSendOp.asset_type === 'native'
? 'XLM'
: `${strictSendOp.asset_code}:${strictSendOp.asset_issuer}`;
// Extract path
if (strictSendOp.path && strictSendOp.path.length > 0) {
path = strictSendOp.path.map((p: any) =>
p.asset_type === 'native' ? 'XLM' : `${p.asset_code}:${p.asset_issuer}`
);
}
}
}
this.logger.log(
`Successfully verified transaction: ${txHash}, amount: ${amount} ${asset}${isPathPayment ? ` (path payment from ${sourceAmount} ${sourceAsset})` : ''}`,
);
return {
valid: true,
amount,
asset,
sender: transaction.source_account,
receiver,
timestamp: transaction.created_at,
sourceAmount,
sourceAsset,
isPathPayment,
path,
};
} catch (error) {
this.logger.error(`Error verifying transaction ${txHash}:`, error);
return null;
}
}
getNetworkPassphrase(): string {
return this.configService.get<string>('STELLAR_NETWORK_PASSPHRASE') ?? Networks.TESTNET;
}
/**
* Get account details from Stellar
* @param accountId The Stellar account ID
* @returns Account details
*/
async getAccountDetails(accountId: string) {
try {
return await this.horizonServer.accounts().accountId(accountId).call();
} catch (error) {
this.logger.error(`Error fetching account details for ${accountId}:`, error);
throw error;
}
}
/**
* Check if an account exists and is active
* @param accountId The Stellar account ID
* @returns Boolean indicating if account exists and is active
*/
async isAccountActive(accountId: string): Promise<boolean> {
try {
const account = await this.getAccountDetails(accountId);
return !!account && account.subentry_count >= 0;
} catch (error: any) {
if (error.response?.status === 404) {
return false;
}
throw error;
}
}
}