@@ -254,9 +254,96 @@ export class Transaction extends BaseTransaction {
254254 signatures : this . signature ,
255255 outputs : this . outputs ,
256256 changeOutputs : this . changeOutputs ,
257+ sourceChain : this . sourceChain ,
258+ destinationChain : this . destinationChain ,
257259 } ;
258260 }
259261
262+ /**
263+ * Get the source chain id or undefined if it's not a cross chain transfer.
264+ */
265+ get sourceChain ( ) : string | undefined {
266+ const tx = ( this . _flareTransaction as UnsignedTx ) . getTx ( ) ;
267+
268+ switch ( this . type ) {
269+ case TransactionType . Import :
270+ if ( this . isTransactionForCChain ) {
271+ // C-chain Import: source is the chain we're importing FROM (P-chain)
272+ const importTx = tx as evmSerial . ImportTx ;
273+ return this . blockchainIDtoAlias ( Buffer . from ( importTx . sourceChain . toBytes ( ) ) ) ;
274+ } else {
275+ // P-chain Import: source is the chain we're importing FROM (C-chain)
276+ const pvmImportTx = tx as pvmSerial . ImportTx ;
277+ return this . blockchainIDtoAlias ( Buffer . from ( pvmImportTx . sourceChain . toBytes ( ) ) ) ;
278+ }
279+
280+ case TransactionType . Export :
281+ if ( this . isTransactionForCChain ) {
282+ // C-chain Export: source is C-chain (the blockchain ID)
283+ const exportTx = tx as evmSerial . ExportTx ;
284+ return this . blockchainIDtoAlias ( Buffer . from ( exportTx . blockchainId . toBytes ( ) ) ) ;
285+ } else {
286+ // P-chain Export: source is P-chain (the blockchain ID from baseTx)
287+ const pvmExportTx = tx as pvmSerial . ExportTx ;
288+ return this . blockchainIDtoAlias ( Buffer . from ( pvmExportTx . baseTx . BlockchainId . toBytes ( ) ) ) ;
289+ }
290+
291+ default :
292+ return undefined ;
293+ }
294+ }
295+
296+ /**
297+ * Get the destination chain id or undefined if it's not a cross chain transfer.
298+ */
299+ get destinationChain ( ) : string | undefined {
300+ const tx = ( this . _flareTransaction as UnsignedTx ) . getTx ( ) ;
301+
302+ switch ( this . type ) {
303+ case TransactionType . Import :
304+ if ( this . isTransactionForCChain ) {
305+ // C-chain Import: destination is C-chain (the blockchain ID)
306+ const importTx = tx as evmSerial . ImportTx ;
307+ return this . blockchainIDtoAlias ( Buffer . from ( importTx . blockchainId . toBytes ( ) ) ) ;
308+ } else {
309+ // P-chain Import: destination is P-chain (the blockchain ID from baseTx)
310+ const pvmImportTx = tx as pvmSerial . ImportTx ;
311+ return this . blockchainIDtoAlias ( Buffer . from ( pvmImportTx . baseTx . BlockchainId . toBytes ( ) ) ) ;
312+ }
313+
314+ case TransactionType . Export :
315+ if ( this . isTransactionForCChain ) {
316+ // C-chain Export: destination is P-chain (the destination chain)
317+ const exportTx = tx as evmSerial . ExportTx ;
318+ return this . blockchainIDtoAlias ( Buffer . from ( exportTx . destinationChain . toBytes ( ) ) ) ;
319+ } else {
320+ // P-chain Export: destination is C-chain (the destination chain)
321+ const pvmExportTx = tx as pvmSerial . ExportTx ;
322+ return this . blockchainIDtoAlias ( Buffer . from ( pvmExportTx . destination . toBytes ( ) ) ) ;
323+ }
324+
325+ default :
326+ return undefined ;
327+ }
328+ }
329+
330+ /**
331+ * Convert a blockchainId buffer to string and return P or C alias if it matches any of those chains.
332+ * @param {Buffer } blockchainIDBuffer
333+ * @return {string } blockchainID or alias if exists.
334+ * @private
335+ */
336+ private blockchainIDtoAlias ( blockchainIDBuffer : Buffer ) : string {
337+ const blockchainId = utils . cb58Encode ( blockchainIDBuffer ) ;
338+ if ( blockchainId === this . _network . cChainBlockchainID ) {
339+ return 'C' ;
340+ }
341+ if ( blockchainId === this . _network . blockchainID ) {
342+ return 'P' ;
343+ }
344+ return blockchainId ;
345+ }
346+
260347 setTransaction ( tx : Tx ) : void {
261348 this . _flareTransaction = tx as UnsignedTx ;
262349 }
0 commit comments