@@ -7,20 +7,17 @@ import {
77 TransactionRecipient ,
88 TransactionType ,
99} from '@bitgo/sdk-core' ;
10- import { BaseCoin as CoinConfig , NetworkType } from '@bitgo/statics' ;
10+ import { BaseCoin as CoinConfig } from '@bitgo/statics' ;
1111import {
1212 AccountAddress ,
1313 AccountAuthenticatorEd25519 ,
14- Aptos ,
15- AptosConfig ,
1614 DEFAULT_MAX_GAS_AMOUNT ,
1715 Ed25519PublicKey ,
1816 Ed25519Signature ,
1917 FeePayerRawTransaction ,
2018 generateSigningMessage ,
2119 generateUserTransactionHash ,
2220 Hex ,
23- Network ,
2421 RAW_TRANSACTION_SALT ,
2522 RAW_TRANSACTION_WITH_DATA_SALT ,
2623 RawTransaction ,
@@ -31,6 +28,7 @@ import {
3128import { DEFAULT_GAS_UNIT_PRICE , SECONDS_PER_WEEK , UNAVAILABLE_TEXT } from '../constants' ;
3229import utils from '../utils' ;
3330import BigNumber from 'bignumber.js' ;
31+ import { AptTransactionExplanation } from '../iface' ;
3432
3533export abstract class Transaction extends BaseTransaction {
3634 protected _rawTransaction : RawTransaction ;
@@ -214,43 +212,7 @@ export abstract class Transaction extends BaseTransaction {
214212 ] ;
215213 }
216214
217- fromRawTransaction ( rawTransaction : string ) : void {
218- let signedTxn : SignedTransaction ;
219- try {
220- signedTxn = utils . deserializeSignedTransaction ( rawTransaction ) ;
221- } catch ( e ) {
222- console . error ( 'invalid raw transaction' , e ) ;
223- throw new Error ( 'invalid raw transaction' ) ;
224- }
225- this . fromDeserializedSignedTransaction ( signedTxn ) ;
226- }
227-
228- fromDeserializedSignedTransaction ( signedTxn : SignedTransaction ) : void {
229- try {
230- const rawTxn = signedTxn . raw_txn ;
231- this . _sender = rawTxn . sender . toString ( ) ;
232- this . _recipient = utils . getRecipientFromTransactionPayload ( rawTxn . payload ) ;
233- this . _sequenceNumber = utils . castToNumber ( rawTxn . sequence_number ) ;
234- this . _maxGasAmount = utils . castToNumber ( rawTxn . max_gas_amount ) ;
235- this . _gasUnitPrice = utils . castToNumber ( rawTxn . gas_unit_price ) ;
236- this . _expirationTime = utils . castToNumber ( rawTxn . expiration_timestamp_secs ) ;
237- this . _rawTransaction = rawTxn ;
238-
239- this . loadInputsAndOutputs ( ) ;
240- const authenticator = signedTxn . authenticator as TransactionAuthenticatorFeePayer ;
241- this . _feePayerAddress = authenticator . fee_payer . address . toString ( ) ;
242- const senderAuthenticator = authenticator . sender as AccountAuthenticatorEd25519 ;
243- const senderSignature = Buffer . from ( senderAuthenticator . signature . toUint8Array ( ) ) ;
244- this . addSenderSignature ( { pub : senderAuthenticator . public_key . toString ( ) } , senderSignature ) ;
245-
246- const feePayerAuthenticator = authenticator . fee_payer . authenticator as AccountAuthenticatorEd25519 ;
247- const feePayerSignature = Buffer . from ( feePayerAuthenticator . signature . toUint8Array ( ) ) ;
248- this . addFeePayerSignature ( { pub : feePayerAuthenticator . public_key . toString ( ) } , feePayerSignature ) ;
249- } catch ( e ) {
250- console . error ( 'invalid signed transaction' , e ) ;
251- throw new Error ( 'invalid signed transaction' ) ;
252- }
253- }
215+ abstract fromRawTransaction ( rawTransaction : string ) : void ;
254216
255217 /**
256218 * Deserializes a signed transaction hex string
@@ -266,27 +228,7 @@ export abstract class Transaction extends BaseTransaction {
266228 }
267229 }
268230
269- protected async buildRawTransaction ( ) {
270- const network : Network = this . _coinConfig . network . type === NetworkType . MAINNET ? Network . MAINNET : Network . TESTNET ;
271- const aptos = new Aptos ( new AptosConfig ( { network } ) ) ;
272- const senderAddress = AccountAddress . fromString ( this . _sender ) ;
273- const recipientAddress = AccountAddress . fromString ( this . _recipient . address ) ;
274-
275- const simpleTxn = await aptos . transaction . build . simple ( {
276- sender : senderAddress ,
277- data : {
278- function : '0x1::aptos_account::transfer' ,
279- functionArguments : [ recipientAddress , this . recipient . amount ] ,
280- } ,
281- options : {
282- maxGasAmount : this . maxGasAmount ,
283- gasUnitPrice : this . gasUnitPrice ,
284- expireTimestamp : this . expirationTime ,
285- accountSequenceNumber : this . sequenceNumber ,
286- } ,
287- } ) ;
288- this . _rawTransaction = simpleTxn . rawTransaction ;
289- }
231+ protected abstract buildRawTransaction ( ) : void ;
290232
291233 public getFee ( ) : string {
292234 return new BigNumber ( this . gasUsed ) . multipliedBy ( this . gasUnitPrice ) . toString ( ) ;
@@ -296,6 +238,35 @@ export abstract class Transaction extends BaseTransaction {
296238 return this . feePayerAddress ? this . getSignablePayloadWithFeePayer ( ) : this . getSignablePayloadWithoutFeePayer ( ) ;
297239 }
298240
241+ /** @inheritDoc */
242+ explainTransaction ( ) : AptTransactionExplanation {
243+ const displayOrder = [
244+ 'id' ,
245+ 'outputs' ,
246+ 'outputAmount' ,
247+ 'changeOutputs' ,
248+ 'changeAmount' ,
249+ 'fee' ,
250+ 'withdrawAmount' ,
251+ 'sender' ,
252+ 'type' ,
253+ ] ;
254+
255+ const outputs : TransactionRecipient [ ] = [ this . recipient ] ;
256+ const outputAmount = outputs [ 0 ] . amount ;
257+ return {
258+ displayOrder,
259+ id : this . id ,
260+ outputs,
261+ outputAmount,
262+ changeOutputs : [ ] ,
263+ changeAmount : '0' ,
264+ fee : { fee : this . getFee ( ) } ,
265+ sender : this . sender ,
266+ type : this . type ,
267+ } ;
268+ }
269+
299270 private getSignablePayloadWithFeePayer ( ) : Buffer {
300271 const feePayerRawTxn = new FeePayerRawTransaction (
301272 this . _rawTransaction ,
0 commit comments