@@ -11,9 +11,15 @@ import {
1111 VerifyAddressOptions ,
1212 VerifyTransactionOptions ,
1313} from '@bitgo/sdk-core' ;
14- import { BaseCoin as StaticsBaseCoin } from '@bitgo/statics' ;
15- import { KeyPair as AptKeyPair } from './lib' ;
14+ import { BaseCoin as StaticsBaseCoin , coins } from '@bitgo/statics' ;
15+ import { KeyPair as AptKeyPair , TransactionBuilderFactory , TransferTransaction } from './lib' ;
1616import utils from './lib/utils' ;
17+ import * as _ from 'lodash' ;
18+ import BigNumber from 'bignumber.js' ;
19+
20+ export interface AptParseTransactionOptions extends ParseTransactionOptions {
21+ txHex : string ;
22+ }
1723
1824export class Apt extends BaseCoin {
1925 protected readonly _staticsCoin : Readonly < StaticsBaseCoin > ;
@@ -64,7 +70,40 @@ export class Apt extends BaseCoin {
6470 }
6571
6672 async verifyTransaction ( params : VerifyTransactionOptions ) : Promise < boolean > {
67- throw new Error ( 'Method not implemented.' ) ;
73+ const coinConfig = coins . get ( this . getChain ( ) ) ;
74+ const { txPrebuild : txPrebuild , txParams : txParams } = params ;
75+ const transaction = new TransferTransaction ( coinConfig ) ;
76+ const rawTx = txPrebuild . txHex ;
77+ if ( ! rawTx ) {
78+ throw new Error ( 'missing required tx prebuild property txHex' ) ;
79+ }
80+ transaction . fromRawTransaction ( rawTx ) ;
81+ const explainedTx = transaction . explainTransaction ( ) ;
82+ if ( txParams . recipients !== undefined ) {
83+ const filteredRecipients = txParams . recipients ?. map ( ( recipient ) => {
84+ return {
85+ address : recipient . address , // TODO: check this
86+ amount : BigInt ( recipient . amount ) ,
87+ } ;
88+ } ) ;
89+ const filteredOutputs = explainedTx . outputs . map ( ( output ) => {
90+ return {
91+ address : output . address ,
92+ amount : BigInt ( output . amount ) ,
93+ } ;
94+ } ) ;
95+ if ( ! _ . isEqual ( filteredOutputs , filteredRecipients ) ) {
96+ throw new Error ( 'Tx outputs does not match with expected txParams recipients' ) ;
97+ }
98+ let totalAmount = new BigNumber ( 0 ) ;
99+ for ( const recipients of txParams . recipients ) {
100+ totalAmount = totalAmount . plus ( recipients . amount ) ;
101+ }
102+ if ( ! totalAmount . isEqualTo ( explainedTx . outputAmount ) ) {
103+ throw new Error ( 'Tx total amount does not match with expected total amount field' ) ;
104+ }
105+ }
106+ return true ;
68107 }
69108
70109 async isWalletAddress ( params : VerifyAddressOptions ) : Promise < boolean > {
@@ -76,8 +115,26 @@ export class Apt extends BaseCoin {
76115 return true ;
77116 }
78117
79- parseTransaction ( params : ParseTransactionOptions ) : Promise < ParsedTransaction > {
80- throw new Error ( 'Method not implemented.' ) ;
118+ async parseTransaction ( params : AptParseTransactionOptions ) : Promise < ParsedTransaction > {
119+ const coinConfig = coins . get ( this . getChain ( ) ) ;
120+ const factory = new TransactionBuilderFactory ( coinConfig ) ;
121+ const transactionBuilder = factory . from ( params . txHex ) ;
122+ const rebuiltTransaction = await transactionBuilder . build ( ) ;
123+ const parsedTransaction = rebuiltTransaction . toJson ( ) ;
124+ return {
125+ inputs : [
126+ {
127+ address : parsedTransaction . sender ,
128+ value : parsedTransaction . recipient . amount ,
129+ } ,
130+ ] ,
131+ outputs : [
132+ {
133+ address : parsedTransaction . recipient . address ,
134+ value : parsedTransaction . recipient . amount ,
135+ } ,
136+ ] ,
137+ } ;
81138 }
82139
83140 generateKeyPair ( seed ?: Buffer ) : KeyPair {
0 commit comments