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 11, 2018
2 parents d3e790a + b67bc1f commit 155cc6e
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 61 deletions.
15 changes: 9 additions & 6 deletions src/Apostille.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ const nem = nemSDK.default;
* @class Apostille
*/
class Apostille {
/**
* @description the network type of the HD apostille account
* @type {NetworkType}
* @memberof Apostille
*/
public readonly networkType: NetworkType;

/**
* @description - an array of all the transaction before they get announced to the network
* @private
Expand Down Expand Up @@ -63,22 +70,18 @@ class Apostille {
* Creates an instance of Apostille.
* @param {string} seed - the seed used to generate an initial hash
* @param {Account} generatorAccount - the account used to sign the hash to generate an HD account private key
* @param {NetworkType} networkType - network type of the apostille account
* @memberof Apostille
*/
constructor(
public readonly seed: string,
private generatorAccount: Account,
public readonly networkType: NetworkType,
) {
if (generatorAccount.address.networkType !== networkType) {
throw new Error('network type miss matched!');
}
this.networkType = generatorAccount.address.networkType;
// hash the seed for the apostille account
const hashSeed = SHA256.hash(this.seed);
let privateKey: string;
// signe the hashed seed to get the private key
if (networkType === NetworkType.MAIN_NET || networkType === NetworkType.TEST_NET) {
if (this.networkType === NetworkType.MAIN_NET || this.networkType === NetworkType.TEST_NET) {
const keyPair = nem.crypto.keyPair.create(generatorAccount.privateKey);
privateKey = nem.utils.helpers.fixPrivateKey(keyPair.sign(hashSeed).toString());
} else {
Expand Down
61 changes: 22 additions & 39 deletions tests/unit/Private/Functionalities.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,10 @@ beforeAll(() => {
jest.setTimeout(10000);
});

describe('constructor should work properly', () => {
it('should throw an error if there is a network miss match', () => {
expect(() => {
const privateApostille = new Apostille(seed, generator, NetworkType.TEST_NET);
}).toThrow();
});
});

describe('create functionn should work properly', () => {

it('should throw an error if there is a network missmatch between initiator and apostille', () => {
const privateApostille = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN);
const initiator = new Initiator(creator, NetworkType.MIJIN);
return privateApostille.create(initiator, 'raw').catch((e) => {
expect(e.message).toMatch('Netrowk type miss matched!');
});
});
describe('Create functionn should work properly', () => {

it('should throw an error if you try to create an apostille more than once', async () => {
const privateApostille = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille(seed, generator);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const initiator = new Initiator(creator, NetworkType.MIJIN_TEST);
expect.assertions(1);
Expand All @@ -41,7 +24,7 @@ describe('create functionn should work properly', () => {
});

it('should create a transfer transaction', () => {
const privateApostille = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille(seed, generator);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const initiator = new Initiator(creator, NetworkType.MIJIN_TEST);
return privateApostille.create(initiator, 'raw').then(() => {
Expand All @@ -51,7 +34,7 @@ describe('create functionn should work properly', () => {
});

it('should create an aggregate complete transaction', () => {
const privateApostille = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille(seed, generator);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const initiator = new Initiator(creator, NetworkType.MIJIN_TEST, creator.publicAccount, true);
return privateApostille.create(initiator, 'raw').then(() => {
Expand All @@ -61,7 +44,7 @@ describe('create functionn should work properly', () => {
});

it('should create an aggregate bounded transaction', () => {
const privateApostille = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille(seed, generator);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const initiator = new Initiator(creator, NetworkType.MIJIN_TEST, creator.publicAccount, false);
return privateApostille.create(initiator, 'raw').then(() => {
Expand All @@ -75,7 +58,7 @@ describe('create functionn should work properly', () => {
describe('update function should work properly', () => {

it('should throw an error if we try to update before creating', async () => {
const privateApostille = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille(seed, generator);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const initiator = new Initiator(creator, NetworkType.MIJIN_TEST);
expect.assertions(1);
Expand All @@ -85,7 +68,7 @@ describe('update function should work properly', () => {
});

it('should create a transfer transaction', async () => {
const privateApostille = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille(seed, generator);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const initiator = new Initiator(creator, NetworkType.MIJIN_TEST);
expect.assertions(1);
Expand All @@ -96,7 +79,7 @@ describe('update function should work properly', () => {
});

it('should create an aggregate complete transaction', async () => {
const privateApostille = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille(seed, generator);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const initiator = new Initiator(creator, NetworkType.MIJIN_TEST, creator.publicAccount, true);
expect.assertions(1);
Expand All @@ -107,7 +90,7 @@ describe('update function should work properly', () => {
});

it('should create an aggregate bounded transaction', async () => {
const privateApostille = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille(seed, generator);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const initiator = new Initiator(creator, NetworkType.MIJIN_TEST, creator.publicAccount, false);
expect.assertions(1);
Expand All @@ -122,7 +105,7 @@ describe('update function should work properly', () => {
describe('own function should work properly', () => {

it('should create an aggregate bounded transaction', async () => {
const privateApostille = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille(seed, generator);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const initiator = new Initiator(creator, NetworkType.MIJIN_TEST, creator.publicAccount, false);
expect.assertions(1);
Expand All @@ -138,7 +121,7 @@ describe('own function should work properly', () => {
describe('transafer function should work properly', () => {

it('should create an aggregate complete transaction', async () => {
const privateApostille = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille(seed, generator);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const initiator = new Initiator(creator, NetworkType.MIJIN_TEST, creator.publicAccount, false);
expect.assertions(1);
Expand All @@ -148,7 +131,7 @@ describe('transafer function should work properly', () => {
});

it('should create an aggregate complete transaction', async () => {
const privateApostille = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille(seed, generator);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const initiator = new Initiator(creator, NetworkType.MIJIN_TEST, creator.publicAccount, false);
expect.assertions(1);
Expand All @@ -162,15 +145,15 @@ describe('transafer function should work properly', () => {
describe('isCreated function should work properly', () => {

it('should return false before creation', async () => {
const privateApostille = new Apostille('QUleqZedaOUtlSh', generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille('QUleqZedaOUtlSh', generator);
expect.assertions(1);
return privateApostille.isCreated().then((result) => {
expect(result).toBeFalsy();
});
});

it('should return true after creation', async () => {
const privateApostille = new Apostille('new random seed', generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille('new random seed', generator);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const initiator = new Initiator(creator, NetworkType.MIJIN_TEST);
expect.assertions(1);
Expand All @@ -181,7 +164,7 @@ describe('isCreated function should work properly', () => {
});

it('should return true for an already created apostille', async () => {
const privateApostille = new Apostille('MIJIN_TEST', generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille('MIJIN_TEST', generator);
expect.assertions(1);
return privateApostille.isCreated().then((result) => {
expect(result).toBeTruthy();
Expand All @@ -193,7 +176,7 @@ describe('isCreated function should work properly', () => {
describe('isAnnounced function should work properly', () => {
it('should throw an error if we don\'t specefy mijin endpoint url', () => {
const MJgenerator = Account.createFromPrivateKey(sk, NetworkType.MIJIN);
const apostilleMJ = new Apostille('k7u*VTsVCk6h,FdN', MJgenerator, NetworkType.MIJIN);
const apostilleMJ = new Apostille('k7u*VTsVCk6h,FdN', MJgenerator);
try {
return apostilleMJ.isAnnouced();
} catch (e) {
Expand All @@ -206,10 +189,10 @@ describe('isAnnounced function should work properly', () => {
const MJgenerator = Account.createFromPrivateKey(sk, NetworkType.MIJIN);
const MNgenerator = Account.createFromPrivateKey(sk, NetworkType.MAIN_NET);
const Tgenerator = Account.createFromPrivateKey(sk, NetworkType.TEST_NET);
const apostilleMT = new Apostille('QUleqZedaOUtlSh', MTgenerator, NetworkType.MIJIN_TEST);
const apostilleMJ = new Apostille('QUleqZedaOUtlSh', MJgenerator, NetworkType.MIJIN);
const apostilleMN = new Apostille('QUleqZedaOUtlSh', MNgenerator, NetworkType.MAIN_NET);
const apostilleT = new Apostille('QUleqZedaOUtlSh', Tgenerator, NetworkType.TEST_NET);
const apostilleMT = new Apostille('QUleqZedaOUtlSh', MTgenerator);
const apostilleMJ = new Apostille('QUleqZedaOUtlSh', MJgenerator);
const apostilleMN = new Apostille('QUleqZedaOUtlSh', MNgenerator);
const apostilleT = new Apostille('QUleqZedaOUtlSh', Tgenerator);
return apostilleMT.isAnnouced().then((MT) => {
expect(MT).toBeFalsy();
return apostilleMJ.isAnnouced('http://b1.nem.foundation:7895').then((MJ) => {
Expand All @@ -225,7 +208,7 @@ describe('isAnnounced function should work properly', () => {
});

it('should return true after an announce', async () => {
const privateApostille = new Apostille('_934@Ve*,tM(3MN-', generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille('_934@Ve*,tM(3MN-', generator);
const creator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const initiator = new Initiator(creator, NetworkType.MIJIN_TEST);
privateApostille.created = true;
Expand All @@ -237,7 +220,7 @@ describe('isAnnounced function should work properly', () => {
});

it('should return true for an already announced apostille', () => {
const privateApostille = new Apostille('MIJIN_TEST', generator, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille('MIJIN_TEST', generator);
return privateApostille.isAnnouced().then((result) => {
expect(result).toBeTruthy();
});
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Private/Getters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const hdAccountInformation = {
publicKey: '9C0C770BD1E1506FD207A8D783E0E4AC00D98B6D790401573519D82133474B90'.toUpperCase(),
};

const PrivateApostille1 = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const PrivateApostille2 = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const PrivateApostille1 = new Apostille(seed, generator);
const PrivateApostille2 = new Apostille(seed, generator);

describe('Getters should work properly', () => {
it('should correctly generate a private apostille via a private key', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Private/HDAccount.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const oldPrivateApostille = nem.model.apostille.create(
false, {}, true,
nem.model.network.data.testnet.id);

const newPrivateApostille = new Apostille(seed, generator, NetworkType.TEST_NET);
const newPrivateApostille = new Apostille(seed, generator);

describe('HD account generation should be correct', () => {
it('private key should be valid', () => {
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/Private/Hashs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('private apostille hash should be correct', () => {
nem.model.apostille.hashing['MD5'],
false, {}, true,
nem.model.network.data.testnet.id);
const newPrivateApostille = new Apostille(seed, generator, NetworkType.TEST_NET);
const newPrivateApostille = new Apostille(seed, generator);
return newPrivateApostille.create(initiator, payload, [], new MD5()).then(() => {
expect(newPrivateApostille.creationHash.substring(0, 10)).toMatch(oldPrivateApostille.data.checksum);
});
Expand All @@ -46,7 +46,7 @@ describe('private apostille hash should be correct', () => {
nem.model.apostille.hashing['MD5'],
false, {}, true,
nem.model.network.data.testnet.id);
const newPrivateApostille = new Apostille(seed, generator, NetworkType.TEST_NET);
const newPrivateApostille = new Apostille(seed, generator);
return newPrivateApostille.create(initiator, payload, [], new MD5()).then(() => {
expect(newPrivateApostille.creationHash).toMatch(oldPrivateApostille.data.hash);
});
Expand All @@ -62,7 +62,7 @@ describe('private apostille hash should be correct', () => {
nem.model.apostille.hashing['SHA1'],
false, {}, true,
nem.model.network.data.testnet.id);
const newPrivateApostille = new Apostille(seed, generator, NetworkType.TEST_NET);
const newPrivateApostille = new Apostille(seed, generator);
return newPrivateApostille.create(initiator, payload, [], new SHA1()).then(() => {
expect(newPrivateApostille.creationHash.substring(0, 10)).toMatch(oldPrivateApostille.data.checksum);
});
Expand All @@ -78,7 +78,7 @@ describe('private apostille hash should be correct', () => {
nem.model.apostille.hashing['SHA1'],
false, {}, true,
nem.model.network.data.testnet.id);
const newPrivateApostille = new Apostille(seed, generator, NetworkType.TEST_NET);
const newPrivateApostille = new Apostille(seed, generator);
return newPrivateApostille.create(initiator, payload, [], new SHA1()).then(() => {
expect(newPrivateApostille.creationHash).toMatch(oldPrivateApostille.data.hash);
});
Expand All @@ -94,7 +94,7 @@ describe('private apostille hash should be correct', () => {
nem.model.apostille.hashing['SHA256'],
false, {}, true,
nem.model.network.data.testnet.id);
const newPrivateApostille = new Apostille(seed, generator, NetworkType.TEST_NET);
const newPrivateApostille = new Apostille(seed, generator);
return newPrivateApostille.create(initiator, payload, [], new SHA256()).then(() => {
expect(newPrivateApostille.creationHash.substring(0, 10)).toMatch(oldPrivateApostille.data.checksum);
});
Expand All @@ -110,7 +110,7 @@ describe('private apostille hash should be correct', () => {
nem.model.apostille.hashing['SHA256'],
false, {}, true,
nem.model.network.data.testnet.id);
const newPrivateApostille = new Apostille(seed, generator, NetworkType.TEST_NET);
const newPrivateApostille = new Apostille(seed, generator);
return newPrivateApostille.create(initiator, payload, [], new SHA256()).then(() => {
expect(newPrivateApostille.creationHash).toMatch(oldPrivateApostille.data.hash);
});
Expand All @@ -126,7 +126,7 @@ describe('private apostille hash should be correct', () => {
nem.model.apostille.hashing['SHA3-256'],
false, {}, true,
nem.model.network.data.testnet.id);
const newPrivateApostille = new Apostille(seed, generator, NetworkType.TEST_NET);
const newPrivateApostille = new Apostille(seed, generator);
return newPrivateApostille.create(initiator, payload, [], new KECCAK256()).then(() => {
expect(newPrivateApostille.creationHash.substring(0, 10)).toMatch(oldPrivateApostille.data.checksum);
});
Expand All @@ -142,7 +142,7 @@ describe('private apostille hash should be correct', () => {
nem.model.apostille.hashing['SHA3-256'],
false, {}, true,
nem.model.network.data.testnet.id);
const newPrivateApostille = new Apostille(seed, generator, NetworkType.TEST_NET);
const newPrivateApostille = new Apostille(seed, generator);
return newPrivateApostille.create(initiator, payload, [], new KECCAK256()).then(() => {
expect(newPrivateApostille.creationHash).toMatch(oldPrivateApostille.data.hash);
});
Expand All @@ -158,7 +158,7 @@ describe('private apostille hash should be correct', () => {
nem.model.apostille.hashing['SHA3-512'],
false, {}, true,
nem.model.network.data.testnet.id);
const newPrivateApostille = new Apostille(seed, generator, NetworkType.TEST_NET);
const newPrivateApostille = new Apostille(seed, generator);
return newPrivateApostille.create(initiator, payload, [], new KECCAK512()).then(() => {
expect(newPrivateApostille.creationHash.substring(0, 10)).toMatch(oldPrivateApostille.data.checksum);
});
Expand All @@ -174,7 +174,7 @@ describe('private apostille hash should be correct', () => {
nem.model.apostille.hashing['SHA3-512'],
false, {}, true,
nem.model.network.data.testnet.id);
const newPrivateApostille = new Apostille(seed, generator, NetworkType.TEST_NET);
const newPrivateApostille = new Apostille(seed, generator);
return newPrivateApostille.create(initiator, payload, [], new KECCAK512()).then(() => {
expect(newPrivateApostille.creationHash).toMatch(oldPrivateApostille.data.hash);
});
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Private/Setters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const signer = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
// creation payload
const payload = 'Apostille is awesome !';

const PrivateApostille = new Apostille(seed, generator, NetworkType.MIJIN_TEST);
const PrivateApostille = new Apostille(seed, generator);

describe('Setters should work properly', () => {

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/TransactionsStreams.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ beforeAll(() => {
const seed = '.N:@N%5SVj3Wkmr-';
const sk = '0F30BA45EF341096493CD793D17D4808DAB5EC20A6CC0EB2354DDD687A3A8CF8';
const gensignr = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille(seed, gensignr, NetworkType.MIJIN_TEST);
const privateApostille = new Apostille(seed, gensignr);
const initiator = new Initiator(gensignr, NetworkType.MIJIN_TEST);

describe('TransactionStreams should work properly', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/initiator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const tag = 'NEM is Awesome!';
const sk = 'aaaaaaaaaaeeeeeeeeeebbbbbbbbbb5555555555dddddddddd1111111111aaee';
const generator = Account.createFromPrivateKey(sk, NetworkType.MIJIN_TEST);

const PrivateApostille1 = new Apostille(tag, generator, NetworkType.MIJIN_TEST);
const PrivateApostille1 = new Apostille(tag, generator);

describe('initiator should work properly', () => {
it('multisisg without compleet boolean should throw an error', () => {
Expand Down

0 comments on commit 155cc6e

Please sign in to comment.