Skip to content

Commit f821ef7

Browse files
committed
tests: integration tests
1 parent 0c5eff3 commit f821ef7

File tree

3 files changed

+104
-16
lines changed

3 files changed

+104
-16
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { GenesisWalletHelper } from './helpers/genesis-wallet.helper';
2+
import {
3+
createTokenHelper,
4+
DEFAULT_PIN_CODE,
5+
generateWalletHelper,
6+
stopAllWallets,
7+
waitForTxReceived,
8+
} from './helpers/wallet.helper';
9+
import { HATHOR_TOKEN_CONFIG } from '../../src/constants';
10+
import SendTransaction from '../../src/new/sendTransaction';
11+
import PartialTxProposal from '../../src/wallet/partialTxProposal';
12+
13+
const fakeTokenUid = '008a19f84f2ae284f19bf3d03386c878ddd15b8b0b604a3a3539aa9d714686e1';
14+
15+
describe('partial tx proposal', () => {
16+
afterEach(async () => {
17+
await stopAllWallets();
18+
await GenesisWalletHelper.clearListeners();
19+
});
20+
21+
it('Should exchange tokens between wallets', async () => {
22+
// Create the wallet
23+
const hWallet1 = await generateWalletHelper();
24+
const hWallet2 = await generateWalletHelper();
25+
const network = hWallet1.getNetworkObject();
26+
27+
// Injecting funds and creating a new custom token
28+
await GenesisWalletHelper.injectFunds(hWallet1.getAddressAtIndex(0), 101);
29+
const { hash: token1Uid } = await createTokenHelper(
30+
hWallet1,
31+
'Token1',
32+
'TK1',
33+
100,
34+
);
35+
36+
// Injecting funds and creating a new custom token
37+
await GenesisWalletHelper.injectFunds(hWallet2.getAddressAtIndex(0), 10);
38+
const { hash: token2Uid } = await createTokenHelper(
39+
hWallet2,
40+
'Token2',
41+
'TK2',
42+
1000,
43+
);
44+
45+
/**
46+
* The exchange will be:
47+
*
48+
* Wallet1 will send 100 HTR and 100 TK1
49+
* Wallet2 will send 1000 TK2
50+
*/
51+
const proposal = new PartialTxProposal(network);
52+
// Wallet1 side
53+
proposal.addSend(hWallet1, HATHOR_TOKEN_CONFIG.uid, 100);
54+
proposal.addSend(hWallet1, token1Uid, 100);
55+
proposal.addReceive(hWallet1, token2Uid, 1000);
56+
expect(proposal.partialTx.isComplete()).toBeFalsy();
57+
// Wallet2 side
58+
proposal.addSend(hWallet2, token2Uid, 1000);
59+
proposal.addReceive(hWallet2, HATHOR_TOKEN_CONFIG.uid, 100);
60+
proposal.addReceive(hWallet2, token1Uid, 100);
61+
expect(proposal.partialTx.isComplete()).toBeTruthy();
62+
63+
const serialized = ''; // TODO
64+
expect(proposal.partialTx.serialize()).toEqual(serialized);
65+
66+
const proposal1 = PartialTxProposal.fromPartialTx(serialized, network);
67+
await proposal1.signData(DEFAULT_PIN_CODE, true);
68+
expect(proposal1.signatures.isComplete()).toBeFalsy();
69+
70+
const proposal2 = PartialTxProposal.fromPartialTx(serialized, network);
71+
await proposal2.signData(DEFAULT_PIN_CODE, true);
72+
expect(proposal2.signatures.isComplete()).toBeFalsy();
73+
74+
proposal2.signatures.addSignatures(proposal1.signatures.serialize());
75+
expect(proposal2.signatures.isComplete()).toBeTruthy();
76+
77+
const transaction = proposal2.prepareTx();
78+
const sendTransaction = new SendTransaction({ transaction, network });
79+
const tx = await sendTransaction.runFromMining();
80+
expect(tx.hash).toBeDefined();
81+
});
82+
});

__tests__/models/partial_tx.test.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ import dateFormatter from '../../src/date';
1212

1313

1414
import { UnsupportedScriptError } from '../../src/errors';
15-
import { HATHOR_TOKEN_CONFIG, DEFAULT_TX_VERSION, TOKEN_AUTHORITY_MASK } from '../../src/constants';
15+
import {
16+
HATHOR_TOKEN_CONFIG,
17+
DEFAULT_TX_VERSION,
18+
TOKEN_AUTHORITY_MASK,
19+
TOKEN_MINT_MASK,
20+
TOKEN_MELT_MASK,
21+
} from '../../src/constants';
1622
import helpers from '../../src/utils/helpers';
1723
import txApi from '../../src/api/txApi';
1824
import P2PKH from '../../src/models/p2pkh';
@@ -178,7 +184,7 @@ describe('PartialTx.isComplete', () => {
178184
new ProposalOutput(1, Buffer.from([])),
179185
new ProposalOutput(1, Buffer.from([]), { token: '2' }),
180186
// Add authority output for token 2
181-
new ProposalOutput(1, Buffer.from([]), { token: '2', authorities: 1 }), // mint
187+
new ProposalOutput(1, Buffer.from([]), { token: '2', authorities: TOKEN_MINT_MASK }),
182188
];
183189

184190
expect(partialTx.isComplete()).toBe(true);
@@ -203,8 +209,8 @@ describe('PartialTx.addInput', () => {
203209
expect(partialTx.inputs).toEqual(expected);
204210

205211
// Authority input
206-
expected.push(expect.objectContaining({ hash: 'hash3', index: 10, token: '1', authorities: 3, value: 1056, address: 'W1b3' }));
207-
partialTx.addInput('hash3', 10, 1056, 'W1b3', { token: '1', authorities: 3 });
212+
expected.push(expect.objectContaining({ hash: 'hash3', index: 10, token: '1', authorities: TOKEN_MINT_MASK | TOKEN_MELT_MASK, value: 1056, address: 'W1b3' }));
213+
partialTx.addInput('hash3', 10, 1056, 'W1b3', { token: '1', authorities: TOKEN_MINT_MASK | TOKEN_MELT_MASK });
208214
expect(partialTx.inputs).toEqual(expected);
209215
});
210216
});
@@ -223,9 +229,9 @@ describe('PartialTx.addOutput', () => {
223229
isChange: true,
224230
value: 27,
225231
script: expect.toMatchBuffer(Buffer.from([230, 148, 32])),
226-
authorities: 2,
232+
authorities: TOKEN_MELT_MASK,
227233
}));
228-
partialTx.addOutput(27, Buffer.from([230, 148, 32]), { token: '1', authorities: 2, isChange: true});
234+
partialTx.addOutput(27, Buffer.from([230, 148, 32]), { token: '1', authorities: TOKEN_MELT_MASK, isChange: true});
229235
expect(partialTx.outputs).toEqual(expected);
230236

231237
expected.push(expect.objectContaining({
@@ -464,7 +470,7 @@ describe('PartialTx.validate', () => {
464470
// TokenData of inputs[1] is wrong
465471
partialTx.inputs = [
466472
new ProposalInput(txId1, 0, 27, addr1),
467-
new ProposalInput(txId2, 4, 13, addr2, { token: testTokenConfig.uid, authorities: 2 }),
473+
new ProposalInput(txId2, 4, 13, addr2, { token: testTokenConfig.uid, authorities: TOKEN_MELT_MASK }),
468474
];
469475

470476
await expect(partialTx.validate()).resolves.toEqual(false);

__tests__/wallet/partialTxProposal.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,11 @@ test('addInput', async () => {
295295
20,
296296
70,
297297
ADDR2,
298-
{ token: FAKE_UID, authorities: 1, markAsSelected: false },
298+
{ token: FAKE_UID, authorities: TOKEN_MINT_MASK, markAsSelected: false },
299299
);
300300
expect(spyReset).toHaveBeenCalledTimes(1);
301301
expect(hwallet.markUtxoSelected).not.toHaveBeenCalled();
302-
expect(spyInput).toBeCalledWith(FAKE_TXID, 20, 70, ADDR2, { token: FAKE_UID, authorities: 1 });
302+
expect(spyInput).toBeCalledWith(FAKE_TXID, 20, 70, ADDR2, { token: FAKE_UID, authorities: TOKEN_MINT_MASK });
303303

304304
// Remove mocks
305305
spyReset.mockRestore();
@@ -381,12 +381,12 @@ test('calculateBalance', async () => {
381381
new ProposalInput(FAKE_TXID, 2, 7, ADDR3),
382382
new ProposalInput(FAKE_TXID, 3, 3, ADDR1),
383383
// Authority
384-
new ProposalInput(FAKE_TXID, 4, TOKEN_MELT_MASK, ADDR2, { token: fakeUid3, authorities: 2 }),
385-
new ProposalInput(FAKE_TXID, 5, TOKEN_MELT_MASK, ADDR3, { token: fakeUid3, authorities: 2 }),
384+
new ProposalInput(FAKE_TXID, 4, TOKEN_MELT_MASK, ADDR2, { token: fakeUid3, authorities: TOKEN_MELT_MASK }),
385+
new ProposalInput(FAKE_TXID, 5, TOKEN_MELT_MASK, ADDR3, { token: fakeUid3, authorities: TOKEN_MELT_MASK }),
386386
// Not from the wallet
387387
new ProposalInput(FAKE_TXID, 6, 4, ADDR_OTHER, { token: fakeUid3 }),
388388
new ProposalInput(FAKE_TXID, 7, 999, ADDR_OTHER, { token: fakeUid4 }),
389-
new ProposalInput(FAKE_TXID, 8, TOKEN_MINT_MASK, ADDR_OTHER, { token: fakeUid3, authorities: 1 }),
389+
new ProposalInput(FAKE_TXID, 8, TOKEN_MINT_MASK, ADDR_OTHER, { token: fakeUid3, authorities: TOKEN_MINT_MASK }),
390390
], [
391391
new ProposalOutput(5, scriptFromAddressP2PKH(ADDR1), { token: FAKE_UID }),
392392
new ProposalOutput(8, scriptFromAddressP2PKH(ADDR2), { token: fakeUid2 }),
@@ -397,13 +397,13 @@ test('calculateBalance', async () => {
397397
new ProposalOutput(3, scriptFromAddressP2PKH(ADDR3, timelock), { token: FAKE_UID }),
398398
new ProposalOutput(4, scriptFromAddressP2PKH(ADDR1, timelock), { token: fakeUid2 }),
399399
// Authority
400-
new ProposalOutput(TOKEN_MINT_MASK, scriptFromAddressP2PKH(ADDR2), { token: FAKE_UID, authorities: 1 }),
400+
new ProposalOutput(TOKEN_MINT_MASK, scriptFromAddressP2PKH(ADDR2), { token: FAKE_UID, authorities: TOKEN_MINT_MASK }),
401401
// Authority locked
402-
new ProposalOutput(TOKEN_MELT_MASK, scriptFromAddressP2PKH(ADDR3, timelock), { token: fakeUid2, authorities: 2 }),
403-
new ProposalOutput(TOKEN_MINT_MASK, scriptFromAddressP2PKH(ADDR1, timelock), { token: fakeUid3, authorities: 1 }),
402+
new ProposalOutput(TOKEN_MELT_MASK, scriptFromAddressP2PKH(ADDR3, timelock), { token: fakeUid2, authorities: TOKEN_MELT_MASK }),
403+
new ProposalOutput(TOKEN_MINT_MASK, scriptFromAddressP2PKH(ADDR1, timelock), { token: fakeUid3, authorities: TOKEN_MINT_MASK }),
404404
// Not from the wallet
405405
new ProposalOutput(10, scriptFromAddressP2PKH(ADDR_OTHER)),
406-
new ProposalOutput(TOKEN_MELT_MASK, scriptFromAddressP2PKH(ADDR_OTHER), { token: fakeUid2, authorities: 2 }),
406+
new ProposalOutput(TOKEN_MELT_MASK, scriptFromAddressP2PKH(ADDR_OTHER), { token: fakeUid2, authorities: TOKEN_MELT_MASK }),
407407
]);
408408
/**
409409
* Summary of the test fixture

0 commit comments

Comments
 (0)