Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
khawarizmus committed Jul 12, 2018
2 parents 155cc6e + b67c976 commit fa06bd1
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 123 deletions.
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export * from './src/Apostille';
export * from './src/ApostilleAccount';
export * from './src/Errors';
export * from './src/hashFunctions';
export * from './src/Initiator';
export * from './src/PublicApostille';
export * from './src/Sinks';
export * from './src/HistoricalEndpoints';
export * from './src/TransactionsStreams';
export * from './src/Verifier';
47 changes: 17 additions & 30 deletions src/Apostille.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { drop, uniqBy } from 'lodash';
import * as nemSDK from 'nem-sdk';
import { Account, AccountHttp, Address, AggregateTransaction, Deadline, InnerTransaction, Listener, LockFundsTransaction, ModifyMultisigAccountTransaction, Mosaic, MultisigCosignatoryModification, MultisigCosignatoryModificationType, NetworkType, PlainMessage, PublicAccount, QueryParams, SignedTransaction, TransactionAnnounceResponse, TransactionHttp, TransactionType, TransferTransaction, UInt64, XEM } from 'nem2-sdk';
import { filter, flatMap } from 'rxjs/operators';
import { ApostilleAccount } from './ApostilleAccount';
import { ApostilleAccount, HistoricalEndpoints, Initiator, TransactionsStreams } from '../index';
import { Errors } from './Errors';
import { IReadyTransaction } from './ReadyTransaction';
import { SHA256 } from './hashFunctions';
import { HashFunction } from './hashFunctions/HashFunction';
import { Initiator } from './Initiator';
import { IReadyTransaction } from './ReadyTransaction';
import { TransactionsStreams } from './TransactionsStreams';

const nem = nemSDK.default;
// TODO: add tx hash of creation
Expand Down Expand Up @@ -106,13 +105,13 @@ class Apostille {
hashFunction?: HashFunction,
): Promise<void> {
if (initiatorAccount.network !== this.networkType) {
throw new Error('Netrowk type miss matched!');
throw new Error(Errors[Errors.NETWORK_TYPE_MISMATCHED]);
}
// check if the apostille was already created locally or on chain
await this.isAnnouced().then(() => {
if (this._created) {
this._created = true;
throw new Error('you have already created this apostille');
throw new Error(Errors[Errors.APOSTILLE_ALREADY_CREATED]);
}
this.creatorAccount = initiatorAccount;
let creationTransaction: TransferTransaction;
Expand Down Expand Up @@ -184,7 +183,7 @@ class Apostille {
// we test locally first to avoid testing on chain evrytime we update
await this.isAnnouced();
if (!this._created) {
throw new Error('Apostille not created yet!');
throw new Error(Errors[Errors.APOSTILLE_NOT_CREATED]);
}
}
// we create the update transaction
Expand Down Expand Up @@ -335,7 +334,7 @@ class Apostille {
public async announce(urls?: string): Promise<void> {
await this.isAnnouced().then(async () => {
if (!this._created) {
throw new Error('Apostille not created yet!');
throw new Error(Errors[Errors.APOSTILLE_NOT_CREATED]);
}
let transactionHttp: TransactionHttp;
let listener: Listener;
Expand All @@ -346,18 +345,11 @@ class Apostille {
transactionHttp = new TransactionHttp(urls);
listener = new Listener(urls);
} else {
if (this.networkType === NetworkType.MAIN_NET) {
transactionHttp = new TransactionHttp('http://88.99.192.82:7890');
listener = new Listener('http://88.99.192.82:7890');
} else if (this.networkType === NetworkType.TEST_NET) {
transactionHttp = new TransactionHttp('http://104.128.226.60:7890');
listener = new Listener('http://104.128.226.60:7890');
} else if (this.networkType === NetworkType.MIJIN) {
throw new Error('Missing Endpoint argument!');
} else {
transactionHttp = new TransactionHttp('http://api.beta.catapult.mijin.io:3000');
listener = new Listener('http://api.beta.catapult.mijin.io:3000');
if (this.networkType === NetworkType.MIJIN) {
throw new Error(Errors[Errors.MIJIN_ENDPOINT_NEEDED]);
}
transactionHttp = new TransactionHttp(HistoricalEndpoints[this.networkType]);
listener = new Listener(HistoricalEndpoints[this.networkType]);
}
let readyTransfer: IReadyTransaction[] = [];
this.transactions.forEach(async (readyTransaction) => {
Expand All @@ -368,7 +360,7 @@ class Apostille {
} else if (readyTransaction.type === TransactionType.AGGREGATE_COMPLETE) {
// if aggregate complete check if trensfer transaction has transaction to announce
if (!readyTransaction.initiator.multisigAccount) {
throw Error('This aggregate compleet needs a multisig account');
throw Error(Errors[Errors.AGGREGATE_COMPLETE_NEED_MULTISIG_ACCOUNT]);
}
if (readyTransfer.length > 0) {
await this.announceTransfer(readyTransfer, transactionHttp);
Expand Down Expand Up @@ -399,7 +391,7 @@ class Apostille {

} else if (readyTransaction.type === TransactionType.AGGREGATE_BONDED) {
if (!readyTransaction.initiator.multisigAccount) {
throw Error('This aggregate bounded needs a multisig account');
throw Error(Errors[Errors.AGGREGATE_BOUNDED_NEED_MULTISIG_ACCOUNT]);
}
if (readyTransfer.length > 0) {
await this.announceTransfer(readyTransfer, transactionHttp);
Expand Down Expand Up @@ -440,7 +432,7 @@ class Apostille {
(x) => console.log(x),
(err) => console.error(err));
listener.confirmed(readyTransaction.initiator.account.address).pipe(
filter((transaction) => transaction.transactionInfo !== undefined
filter((transaction: any) => transaction.transactionInfo !== undefined
&& transaction.transactionInfo.hash === signedLock.hash),
flatMap(() => transactionHttp.announceAggregateBonded(signedTransaction)),
).subscribe(
Expand Down Expand Up @@ -563,15 +555,10 @@ class Apostille {
}
accountHttp = new AccountHttp(urls);
} else {
if (this.networkType === NetworkType.MAIN_NET) {
accountHttp = new AccountHttp('http://88.99.192.82:7890');
} else if (this.networkType === NetworkType.TEST_NET) {
accountHttp = new AccountHttp('http://104.128.226.60:7890');
} else if (this.networkType === NetworkType.MIJIN) {
throw new Error('Missing Endpoint argument!');
} else {
accountHttp = new AccountHttp('http://api.beta.catapult.mijin.io:3000');
if (this.networkType === NetworkType.MIJIN) {
throw new Error(Errors[Errors.MIJIN_ENDPOINT_NEEDED]);
}
accountHttp = new AccountHttp(HistoricalEndpoints[this.networkType]);
}
return new Promise(async (resolve, reject) => {
await accountHttp.transactions(
Expand Down
88 changes: 65 additions & 23 deletions src/ApostilleAccount.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,88 @@
import { sortBy } from 'lodash';
import { AccountHttp, Address, BlockchainHttp, PublicAccount, TransactionType } from 'nem2-sdk';
import { TransactionsStreams } from './TransactionsStreams';
import { AccountHttp, Address, BlockchainHttp, MultisigAccountInfo, NetworkType, PublicAccount, Transaction, TransactionType } from 'nem2-sdk';
import { Errors, HistoricalEndpoints, TransactionsStreams } from '../index';

export class ApostilleAccount {
/**
* @description the network type of the HD apostille account
* @type {NetworkType}
* @memberof Apostille
*/
public urls: string;

/**
* @param {PublicAccount} publicAccount
*/
constructor(
/**
* The account apostille public account.
*/
public readonly publicAccount: PublicAccount,
urls?: string,
) {
if (urls) {
this.urls = urls;
} else {
if (publicAccount.address.networkType === NetworkType.MIJIN) {
throw new Error(Errors[Errors.MIJIN_ENDPOINT_NEEDED]);
}
this.urls = HistoricalEndpoints[publicAccount.address.networkType];
}
}

public async isOwned(urls: string) {
const cossignatories = await this.getCosignatories(urls);
/**
* @description - get first transaction
* @static
* @param {string} urls
* @returns {Promise<boolean>}
* @memberof ApostilleAccount
*/
public async isOwned() {
const cossignatories = await this.getCosignatories();
if (cossignatories.length > 0) {
return true;
}
return false;
}

public getCosignatories(urls: string): Promise<object[]> {
const accountHttp = new AccountHttp(urls);
/**
* @description - get cosignatories of the account
* @static
* @param {string} urls
* @returns {Promise<PublicAccount[]>}
* @memberof ApostilleAccount
*/
public getCosignatories(): Promise<PublicAccount[]> {
const accountHttp = new AccountHttp(this.urls);
return new Promise(async (resolve, reject) => {
accountHttp.getMultisigAccountInfo(this.publicAccount.address).subscribe(
(accountInfo) => {
resolve(accountInfo.cosignatories);
const multisigAccountInfo: MultisigAccountInfo = Object.assign(MultisigAccountInfo, accountInfo);

resolve(multisigAccountInfo.cosignatories);
},
(err) => reject(err),
);
});
}

public getCreationTransactionHash(): Promise<string> {
// still on progress
return new Promise((resolve, reject) => {
this.getCreationTransaction();
});
}

/**
* @description - get first transaction
* @static
* @param {string} urls
* @returns {Promise<any>}
* @memberof Verifier
* @returns {Promise<Transaction>}
* @memberof ApostilleAccount
*/
public getCreationTransaction(urls: string): Promise<any> {
const accountHttp = new AccountHttp(urls);
return new Promise(async (resolve, reject) => {
public getCreationTransaction(): Promise<Transaction> {
const accountHttp = new AccountHttp(this.urls);
return new Promise((resolve, reject) => {
accountHttp.getAccountInfo(this.publicAccount.address).subscribe(
(accountInfo) => {
const blockchainHttp = new BlockchainHttp(urls);
const blockchainHttp = new BlockchainHttp(this.urls);
const firstTransactionBlock = accountInfo.addressHeight.lower;
// find the first block of this account
blockchainHttp.getBlockTransactions(firstTransactionBlock).subscribe(
Expand Down Expand Up @@ -78,10 +114,11 @@ export class ApostilleAccount {
// if the smallest index is aggregate transaction, then sort innertransaction by index
const sortedAggregateTransaction = sortBy(
sortedTransaction[0].innerTransactions, ['transactionInfo.index']);
resolve(sortedAggregateTransaction[0]);
resolve(Object.assign(
Transaction, sortedAggregateTransaction[0]));
}

resolve(sortedTransaction[0]);
resolve(Object.assign(Transaction, sortedTransaction[0]));
},
(err) => {
console.error(err.message);
Expand All @@ -99,16 +136,21 @@ export class ApostilleAccount {
* Compares address for equality.
* @param Address
* @returns {boolean}
* @memberof ApostilleAccount
*/
public equals(address: Address) {
return this.publicAccount.address.plain() === address.plain();
}

public monitor(urls?: string): TransactionsStreams {
if (urls) {
return new TransactionsStreams(this, urls);
}
return new TransactionsStreams(this);
/**
* @description - get transaction streams from the account
* @static
* @param {string} urls
* @returns {TransactionsStreams}
* @memberof ApostilleAccount
*/
public monitor(): TransactionsStreams {
return new TransactionsStreams(this, this.urls);
}

}
25 changes: 25 additions & 0 deletions src/Errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @description - Errors
* @export
* @enum {number}
*/
export enum Errors {

MISSING_ENDPOINT_ARGUMENT,

MIJIN_ENDPOINT_NEEDED,

MISSING_IS_COMPLETE_ARGUMENT,

NETWORK_TYPE_MISMATCHED,

APOSTILLE_ALREADY_CREATED,

APOSTILLE_NOT_CREATED,

AGGREGATE_COMPLETE_NEED_MULTISIG_ACCOUNT,

AGGREGATE_BOUNDED_NEED_MULTISIG_ACCOUNT,

FILE_ALREADY_ANNOUNCED,
}
16 changes: 16 additions & 0 deletions src/HistoricalEndpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @description - best endpoints right now
* @export
* @enum {string}
*/
export enum HistoricalEndpoints {
// mainet
'http://88.99.192.82:7890' = 0x68,
// testnet
'http://104.128.226.60:7890' = 0x98,
// mijinet
MIJIN = 0x60,
// mijin_test
'http://api.beta.catapult.mijin.io:3000' = 0x90,
}
// should be used as HistoricalEndpoints[network] // get the address
3 changes: 2 additions & 1 deletion src/Initiator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Account, NetworkType, PublicAccount } from 'nem2-sdk';
import { Errors } from './Errors';
/**
* @description - a class wrapping the transaction initiator account
* @class Initiator
Expand All @@ -23,7 +24,7 @@ class Initiator {
) {
if (multisigAccount) {
if (isComplete === undefined) {
throw new Error('Missing argument "isCompleet"');
throw new Error(Errors[Errors.MISSING_IS_COMPLETE_ARGUMENT]);
}
}
}
Expand Down
23 changes: 8 additions & 15 deletions src/PublicApostille.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, AggregateTransaction, Deadline, Listener, LockFundsTransaction, NetworkType, PlainMessage, SignedTransaction, TransactionHttp, TransferTransaction, UInt64, XEM } from 'nem2-sdk';
import { Initiator } from './Initiator';
import { Errors, HistoricalEndpoints, Initiator } from '../index';
import { Sinks } from './Sinks';
import { HashFunction } from './hashFunctions/HashFunction';

Expand Down Expand Up @@ -51,7 +51,7 @@ class PublicApostille {
if (sinkAddress) {
const newSink = Address.createFromRawAddress(sinkAddress);
if (newSink.networkType !== networkType) {
throw new Error('the address is of a wrong network type');
throw new Error(Errors[Errors.NETWORK_TYPE_MISMATCHED]);
}
this.address = newSink;
} else {
Expand Down Expand Up @@ -85,7 +85,7 @@ class PublicApostille {
*/
public announce(urls?: string): Promise<void> {
if (this.announced) {
throw new Error('This File has already been anounced to the network');
throw new Error(Errors[Errors.FILE_ALREADY_ANNOUNCED]);
}
let transactionHttp;
let listener;
Expand All @@ -98,21 +98,14 @@ class PublicApostille {
transactionHttp = new TransactionHttp(urls);
listener = new Listener(urls);
} else {
if (this.networkType === NetworkType.MAIN_NET) {
transactionHttp = new TransactionHttp('http://88.99.192.82:7890');
listener = new Listener('http://88.99.192.82:7890');
} else if (this.networkType === NetworkType.TEST_NET) {
transactionHttp = new TransactionHttp('http://104.128.226.60:7890');
listener = new Listener('http://104.128.226.60:7890');
} else if (this.networkType === NetworkType.MIJIN) {
throw new Error('Missing Endpoint argument!');
} else {
transactionHttp = new TransactionHttp('http://api.beta.catapult.mijin.io:3000');
listener = new Listener('http://api.beta.catapult.mijin.io:3000');
if (this.networkType === NetworkType.MAIN_NET || this.networkType === NetworkType.TEST_NET) {
throw new Error(Errors[Errors.MIJIN_ENDPOINT_NEEDED]);
}
transactionHttp = new TransactionHttp(HistoricalEndpoints[this.networkType]);
listener = new Listener(HistoricalEndpoints[this.networkType]);
}
if (this.initiatorAccount.network !== this.networkType) {
throw new Error('Netrowk type miss matched!');
throw new Error(Errors[Errors.NETWORK_TYPE_MISMATCHED]);
}

if (this.initiatorAccount.multisigAccount) {
Expand Down
Loading

0 comments on commit fa06bd1

Please sign in to comment.