Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 242 additions & 0 deletions e2e/infrastructure/HashLockHttp.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
/*
* Copyright 2018 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from 'chai';
import { Account } from '../../src/model/account/Account';
import { Address } from '../../src/model/account/Address';
import { PlainMessage } from '../../src/model/message/PlainMessage';
import { NetworkType } from '../../src/model/network/NetworkType';
import { AggregateTransaction } from '../../src/model/transaction/AggregateTransaction';
import { Deadline } from '../../src/model/transaction/Deadline';
import { MultisigAccountModificationTransaction } from '../../src/model/transaction/MultisigAccountModificationTransaction';
import { TransferTransaction } from '../../src/model/transaction/TransferTransaction';
import { UInt64 } from '../../src/model/UInt64';
import { IntegrationTestHelper } from './IntegrationTestHelper';
import { toArray, take } from 'rxjs/operators';
import { deepEqual } from 'assert';
import { Order, HashLockPaginationStreamer } from '../../src/infrastructure/infrastructure';
import { Mosaic } from '../../src/model/mosaic/Mosaic';
import { LockFundsTransaction } from '../../src/model/transaction/LockFundsTransaction';
import { ChronoUnit } from 'js-joda';
import { SignedTransaction } from '../../src/model/transaction/SignedTransaction';
import { UnresolvedMosaicId } from '../../src/model/mosaic/UnresolvedMosaicId';
import { HashLockRepository } from '../../src/infrastructure/HashLockRepository';

describe('HashLockHttp', () => {
const helper = new IntegrationTestHelper();
let account: Account;
let account2: Account;
let multisigAccount: Account;
let cosignAccount1: Account;
let cosignAccount2: Account;
let cosignAccount3: Account;
let hashLockRepo: HashLockRepository;
let hash: string;
let generationHash: string;
let networkType: NetworkType;

before(() => {
return helper.start().then(() => {
account = helper.account;
account2 = helper.account2;
multisigAccount = helper.multisigAccount;
cosignAccount1 = helper.cosignAccount1;
cosignAccount2 = helper.cosignAccount2;
cosignAccount3 = helper.cosignAccount3;
generationHash = helper.generationHash;
networkType = helper.networkType;
hashLockRepo = helper.repositoryFactory.createHashLockRepository();
});
});
before(() => {
return helper.listener.open();
});

after(() => {
helper.listener.close();
});

const createSignedAggregatedBondTransaction = (aggregatedTo: Account, signer: Account, recipient: Address): SignedTransaction => {
const transferTransaction = TransferTransaction.create(
Deadline.create(),
recipient,
[],
PlainMessage.create('test-message'),
networkType,
helper.maxFee,
);

const aggregateTransaction = AggregateTransaction.createBonded(
Deadline.create(2, ChronoUnit.MINUTES),
[transferTransaction.toAggregate(aggregatedTo.publicAccount)],
networkType,
[],
helper.maxFee,
);
return signer.sign(aggregateTransaction, generationHash);
};

const createHashLockTransactionAndAnnounce = (
signedAggregatedTransaction: SignedTransaction,
signer: Account,
mosaicId: UnresolvedMosaicId,
): void => {
const lockFundsTransaction = LockFundsTransaction.create(
Deadline.create(),
new Mosaic(mosaicId, UInt64.fromUint(10 * Math.pow(10, helper.networkCurrencyDivisibility))),
UInt64.fromUint(1000),
signedAggregatedTransaction,
networkType,
helper.maxFee,
);
const signedLockFundsTransaction = signer.sign(lockFundsTransaction, generationHash);
hash = signedLockFundsTransaction.hash;
helper.announce(signedLockFundsTransaction);
};

/**
* =========================
* Setup test data
* =========================
*/

describe('Setup test multisig account', () => {
it('Announce MultisigAccountModificationTransaction', () => {
const modifyMultisigAccountTransaction = MultisigAccountModificationTransaction.create(
Deadline.create(),
2,
1,
[cosignAccount1.address, cosignAccount2.address, cosignAccount3.address],
[],
networkType,
helper.maxFee,
);

const aggregateTransaction = AggregateTransaction.createComplete(
Deadline.create(),
[modifyMultisigAccountTransaction.toAggregate(multisigAccount.publicAccount)],
networkType,
[],
helper.maxFee,
);
const signedTransaction = aggregateTransaction.signTransactionWithCosignatories(
multisigAccount,
[cosignAccount1, cosignAccount2, cosignAccount3],
generationHash,
);

return helper.announce(signedTransaction);
});
});

describe('Create a hash lock', () => {
it('Announce HashLockTransaction', () => {
const signedAggregatedTx = createSignedAggregatedBondTransaction(multisigAccount, account, account2.address);
return createHashLockTransactionAndAnnounce(signedAggregatedTx, account, helper.networkCurrencyNamespaceId);
});
});

/**
* =========================
* Tests
* =========================
*/

describe('getHashLock', () => {
it('should return hash lock info given hash', async () => {
const info = await hashLockRepo.getHashLock(hash).toPromise();
expect(info.ownerAddress.plain()).to.be.equal(account.address.plain());
expect(info.amount.toString()).to.be.equal('1000');
});
});

describe('searchHashLock', () => {
it('should return hash lock page info', async () => {
const info = await hashLockRepo.search({ address: account.address }).toPromise();
expect(info.data.length).to.be.greaterThan(0);
});
});

describe('searchHashLock with streamer', () => {
it('should return hash lock page info', async () => {
const streamer = new HashLockPaginationStreamer(hashLockRepo);
const infoStreamer = await streamer
.search({ address: account.address, pageSize: 20, order: Order.Asc })
.pipe(take(20), toArray())
.toPromise();
const info = await hashLockRepo.search({ address: account.address, pageSize: 20, order: Order.Asc }).toPromise();
expect(infoStreamer.length).to.be.greaterThan(0);
deepEqual(infoStreamer[0], info.data[0]);
});
});

/**
* =========================
* House Keeping
* =========================
*/

describe('Restore test multisig Accounts', () => {
it('Announce MultisigAccountModificationTransaction', () => {
const removeCosigner1 = MultisigAccountModificationTransaction.create(
Deadline.create(),
-1,
0,
[],
[cosignAccount1.address],
networkType,
helper.maxFee,
);
const removeCosigner2 = MultisigAccountModificationTransaction.create(
Deadline.create(),
0,
0,
[],
[cosignAccount2.address],
networkType,
helper.maxFee,
);

const removeCosigner3 = MultisigAccountModificationTransaction.create(
Deadline.create(),
-1,
-1,
[],
[cosignAccount3.address],
networkType,
helper.maxFee,
);

const aggregateTransaction = AggregateTransaction.createComplete(
Deadline.create(),
[
removeCosigner1.toAggregate(multisigAccount.publicAccount),
removeCosigner2.toAggregate(multisigAccount.publicAccount),
removeCosigner3.toAggregate(multisigAccount.publicAccount),
],
networkType,
[],
helper.maxFee,
);
const signedTransaction = aggregateTransaction.signTransactionWithCosignatories(
cosignAccount1,
[cosignAccount2, cosignAccount3],
generationHash,
);
return helper.announce(signedTransaction);
});
});
});
121 changes: 121 additions & 0 deletions e2e/infrastructure/SecretLockHttp.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright 2018 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from 'chai';
import { Account } from '../../src/model/account/Account';
import { NetworkType } from '../../src/model/network/NetworkType';
import { Deadline } from '../../src/model/transaction/Deadline';
import { UInt64 } from '../../src/model/UInt64';
import { IntegrationTestHelper } from './IntegrationTestHelper';
import { toArray, take } from 'rxjs/operators';
import { deepEqual } from 'assert';
import { Order, SecretLockPaginationStreamer } from '../../src/infrastructure/infrastructure';
import { SecretLockRepository } from '../../src/infrastructure/SecretLockRepository';
import { SecretLockTransaction } from '../../src/model/transaction/SecretLockTransaction';
import { LockHashAlgorithm } from '../../src/model/lock/LockHashAlgorithm';
import { sha3_256 } from 'js-sha3';
import { Crypto } from '../../src/core/crypto';

describe('SecretLockHttp', () => {
const helper = new IntegrationTestHelper();
let account: Account;
let account2: Account;
let SecretLockRepo: SecretLockRepository;
let generationHash: string;
let networkType: NetworkType;
let secret: string;

before(() => {
return helper.start().then(() => {
account = helper.account;
account2 = helper.account2;
networkType = helper.networkType;
SecretLockRepo = helper.repositoryFactory.createSecretLockRepository();
secret = sha3_256.create().update(Crypto.randomBytes(20)).hex();
});
});
before(() => {
return helper.listener.open();
});

after(() => {
helper.listener.close();
});

/**
* =========================
* Setup test data
* =========================
*/

describe('Create a hash lock', () => {
it('Announce SecretLockTransaction', () => {
const secretLockTransaction = SecretLockTransaction.create(
Deadline.create(),
helper.createNetworkCurrency(10, false),
UInt64.fromUint(100),
LockHashAlgorithm.Op_Sha3_256,
secret,
account2.address,
networkType,
helper.maxFee,
);
const signedTransaction = secretLockTransaction.signWith(account, generationHash);
return helper.announce(signedTransaction).then((transaction: SecretLockTransaction) => {
expect(transaction.mosaic, 'Mosaic').not.to.be.undefined;
expect(transaction.duration, 'Duration').not.to.be.undefined;
expect(transaction.hashAlgorithm, 'HashAlgorithm').not.to.be.undefined;
expect(transaction.secret, 'Secret').not.to.be.undefined;
expect(transaction.recipientAddress, 'RecipientAddress').not.to.be.undefined;
});
});
});

/**
* =========================
* Tests
* =========================
*/

describe('getSecretLock', () => {
it('should return hash lock info given hash', async () => {
const info = await SecretLockRepo.getSecretLock(secret).toPromise();
expect(info.ownerAddress.plain()).to.be.equal(account.address.plain());
expect(info.recipientAddress.plain()).to.be.equal(account2.address.plain());
expect(info.amount.toString()).to.be.equal('100');
});
});

describe('searchSecretLock', () => {
it('should return hash lock page info', async () => {
const info = await SecretLockRepo.search({ address: account.address }).toPromise();
expect(info.data.length).to.be.greaterThan(0);
});
});

describe('searchSecretLock with streamer', () => {
it('should return hash lock page info', async () => {
const streamer = new SecretLockPaginationStreamer(SecretLockRepo);
const infoStreamer = await streamer
.search({ address: account.address, pageSize: 20, order: Order.Asc })
.pipe(take(20), toArray())
.toPromise();
const info = await SecretLockRepo.search({ address: account.address, pageSize: 20, order: Order.Asc }).toPromise();
expect(infoStreamer.length).to.be.greaterThan(0);
deepEqual(infoStreamer[0], info.data[0]);
});
});
});
2 changes: 1 addition & 1 deletion e2e/infrastructure/TransactionHttp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { CosignatureSignedTransaction } from '../../src/model/transaction/Cosign
import { CosignatureTransaction } from '../../src/model/transaction/CosignatureTransaction';
import { Deadline } from '../../src/model/transaction/Deadline';
import { HashLockTransaction } from '../../src/model/transaction/HashLockTransaction';
import { LockHashAlgorithm } from '../../src/model/transaction/LockHashAlgorithm';
import { LockHashAlgorithm } from '../../src/model/lock/LockHashAlgorithm';
import { LinkAction } from '../../src/model/transaction/LinkAction';
import { LockFundsTransaction } from '../../src/model/transaction/LockFundsTransaction';
import { MosaicAddressRestrictionTransaction } from '../../src/model/transaction/MosaicAddressRestrictionTransaction';
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"ripemd160": "^2.0.2",
"rxjs": "^6.5.3",
"rxjs-compat": "^6.5.3",
"symbol-openapi-typescript-fetch-client": "0.9.7-SNAPSHOT.202009041234",
"symbol-openapi-typescript-fetch-client": "0.9.7-SNAPSHOT.202009100936",
"tweetnacl": "^1.0.3",
"utf8": "^3.0.0",
"ws": "^7.2.3"
Expand Down
Loading