From b67bc1fba49ba86df4fa34ff45995a29130d4661 Mon Sep 17 00:00:00 2001 From: gimyboya Date: Wed, 11 Jul 2018 13:19:18 +0800 Subject: [PATCH] perf(Apostille): removed networkType from Apostille class Apostille class will fall back to the network type of the generator account by default making the constructor lighter BREAKING CHANGE: Apostille Class constructor don't accept network type anymore #3 --- src/Apostille.ts | 15 +++--- tests/unit/Private/Functionalities.spec.ts | 61 ++++++++-------------- tests/unit/Private/Getters.spec.ts | 4 +- tests/unit/Private/HDAccount.spec.ts | 2 +- tests/unit/Private/Hashs.spec.ts | 20 +++---- tests/unit/Private/Setters.spec.ts | 2 +- tests/unit/TransactionsStreams.spec.ts | 2 +- tests/unit/initiator.spec.ts | 2 +- 8 files changed, 47 insertions(+), 61 deletions(-) diff --git a/src/Apostille.ts b/src/Apostille.ts index 057110f..52cd687 100644 --- a/src/Apostille.ts +++ b/src/Apostille.ts @@ -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 @@ -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 { diff --git a/tests/unit/Private/Functionalities.spec.ts b/tests/unit/Private/Functionalities.spec.ts index c0e0e1a..dd33b70 100644 --- a/tests/unit/Private/Functionalities.spec.ts +++ b/tests/unit/Private/Functionalities.spec.ts @@ -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); @@ -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(() => { @@ -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(() => { @@ -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(() => { @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -162,7 +145,7 @@ 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(); @@ -170,7 +153,7 @@ describe('isCreated function should work properly', () => { }); 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); @@ -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(); @@ -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) { @@ -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) => { @@ -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; @@ -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(); }); diff --git a/tests/unit/Private/Getters.spec.ts b/tests/unit/Private/Getters.spec.ts index b503fb1..56773c5 100644 --- a/tests/unit/Private/Getters.spec.ts +++ b/tests/unit/Private/Getters.spec.ts @@ -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', () => { diff --git a/tests/unit/Private/HDAccount.spec.ts b/tests/unit/Private/HDAccount.spec.ts index 7835639..fe9ca0a 100644 --- a/tests/unit/Private/HDAccount.spec.ts +++ b/tests/unit/Private/HDAccount.spec.ts @@ -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', () => { diff --git a/tests/unit/Private/Hashs.spec.ts b/tests/unit/Private/Hashs.spec.ts index 5da0310..4d0256d 100644 --- a/tests/unit/Private/Hashs.spec.ts +++ b/tests/unit/Private/Hashs.spec.ts @@ -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); }); @@ -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); }); @@ -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); }); @@ -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); }); @@ -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); }); @@ -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); }); @@ -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); }); @@ -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); }); @@ -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); }); @@ -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); }); diff --git a/tests/unit/Private/Setters.spec.ts b/tests/unit/Private/Setters.spec.ts index 69f9dec..3b56d6c 100644 --- a/tests/unit/Private/Setters.spec.ts +++ b/tests/unit/Private/Setters.spec.ts @@ -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', () => { diff --git a/tests/unit/TransactionsStreams.spec.ts b/tests/unit/TransactionsStreams.spec.ts index d3561d8..55fa141 100644 --- a/tests/unit/TransactionsStreams.spec.ts +++ b/tests/unit/TransactionsStreams.spec.ts @@ -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', () => { diff --git a/tests/unit/initiator.spec.ts b/tests/unit/initiator.spec.ts index c5e3724..bc5387d 100644 --- a/tests/unit/initiator.spec.ts +++ b/tests/unit/initiator.spec.ts @@ -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', () => {