diff --git a/.gitignore b/.gitignore index c291bca..a3d9f97 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ package-lock.json .DS_Store .AppleDouble .LSOverride +.vs # Icon must end with two \r Icon diff --git a/src/ApostilleAccount.ts b/src/ApostilleAccount.ts index 7a5c84e..1837ee1 100644 --- a/src/ApostilleAccount.ts +++ b/src/ApostilleAccount.ts @@ -1,15 +1,15 @@ -import { findIndex, sortBy, toUpper } from 'lodash'; -import { AccountHttp, BlockchainHttp, PublicAccount } from 'nem2-sdk'; +import { sortBy } from 'lodash'; +import { AccountHttp, Address, BlockchainHttp, PublicAccount, TransactionType } from 'nem2-sdk'; export class ApostilleAccount { /** - * @param account + * @param {PublicAccount} publicAccount */ constructor( /** * The account apostille public account. */ - public readonly account: PublicAccount, + public readonly publicAccount: PublicAccount, ) { } @@ -24,7 +24,7 @@ export class ApostilleAccount { public getCosignatories(urls: string): Promise { const accountHttp = new AccountHttp(urls); return new Promise(async (resolve, reject) => { - accountHttp.getMultisigAccountInfo(this.account.address).subscribe( + accountHttp.getMultisigAccountInfo(this.publicAccount.address).subscribe( (accountInfo) => { resolve(accountInfo.cosignatories); }, @@ -43,20 +43,44 @@ export class ApostilleAccount { public getCreationTransaction(urls: string): Promise { const accountHttp = new AccountHttp(urls); return new Promise(async (resolve, reject) => { - accountHttp.getAccountInfo(this.account.address).subscribe( + accountHttp.getAccountInfo(this.publicAccount.address).subscribe( (accountInfo) => { const blockchainHttp = new BlockchainHttp(urls); const firstTransactionBlock = accountInfo.addressHeight.lower; + // find the first block of this account blockchainHttp.getBlockTransactions(firstTransactionBlock).subscribe( (block) => { + const filteredTransaction: any[] = []; + for (const transaction of block) { + if (transaction.type === TransactionType.TRANSFER) { + const address = Address.createFromRawAddress(transaction.recipient.address); + if (this.equals(address)) { + filteredTransaction.push(transaction); + } + } else if (transaction.type === TransactionType.AGGREGATE_COMPLETE) { + for (const innerTransaction of transaction.innerTransactions) { + if (innerTransaction.type === TransactionType.TRANSFER) { + const address = Address.createFromRawAddress( + innerTransaction.recipient.address); + if (this.equals(address)) { + filteredTransaction.push(transaction); + } + break; + } + } + } + } + // sort the block by index - const sortedBlock = sortBy(block, ['transactionInfo.index']); - // find the first index of current address from the blockTransactions - const creationTransactionIndex = findIndex(sortedBlock, (o) => { - return o.recipient.address === toUpper(this.account.address.plain()); - }); + const sortedTransaction = sortBy(filteredTransaction, ['transactionInfo.index']); + if (sortedTransaction[0].type === TransactionType.AGGREGATE_COMPLETE) { + // if the smallest index is aggregate transaction, then sort innertransaction by index + const sortedAggregateTransaction = sortBy( + sortedTransaction[0].innerTransactions, ['transactionInfo.index']); + resolve(sortedAggregateTransaction[0]); + } - resolve(sortedBlock[creationTransactionIndex]); + resolve(sortedTransaction[0]); }, (err) => { console.error(err.message); @@ -70,4 +94,13 @@ export class ApostilleAccount { }); } + /** + * Compares address for equality. + * @param Address + * @returns {boolean} + */ + public equals(address: Address) { + return this.publicAccount.address.plain() === address.plain(); + } + }