Skip to content

Commit

Permalink
fix(ApostilleAccounts): getCreationTransaction is working properly
Browse files Browse the repository at this point in the history
getCreationTransaction now can differentiate transaction type
  • Loading branch information
aizaiz committed Jul 10, 2018
1 parent da707c1 commit 435b126
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package-lock.json
.DS_Store
.AppleDouble
.LSOverride
.vs

# Icon must end with two \r
Icon
Expand Down
57 changes: 45 additions & 12 deletions src/ApostilleAccount.ts
Original file line number Diff line number Diff line change
@@ -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,
) {
}

Expand All @@ -24,7 +24,7 @@ export class ApostilleAccount {
public getCosignatories(urls: string): Promise<object[]> {
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);
},
Expand All @@ -43,20 +43,44 @@ export class ApostilleAccount {
public getCreationTransaction(urls: string): Promise<any> {
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);
Expand All @@ -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();
}

}

0 comments on commit 435b126

Please sign in to comment.