Skip to content

Commit b0d2505

Browse files
committed
Added #204
- Fixed signTransactionGivenSignatures bug - Added e2e test for signTransactionGivenSignatures - Improved unit test for signTransactionGivenSignatures - Fixed typo
1 parent 4d22d6f commit b0d2505

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

e2e/infrastructure/TransactionHttp.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {ChronoUnit} from 'js-joda';
1919
import {keccak_256, sha3_256} from 'js-sha3';
2020
import {Crypto} from '../../src/core/crypto';
2121
import { Convert as convert } from '../../src/core/format';
22+
import { TransactionMapping } from '../../src/core/utils/TransactionMapping';
2223
import {AccountHttp} from '../../src/infrastructure/AccountHttp';
2324
import { NamespaceHttp } from '../../src/infrastructure/infrastructure';
2425
import {Listener} from '../../src/infrastructure/Listener';
@@ -44,6 +45,7 @@ import { AccountRestrictionTransaction } from '../../src/model/transaction/Accou
4445
import { AddressAliasTransaction } from '../../src/model/transaction/AddressAliasTransaction';
4546
import {AggregateTransaction} from '../../src/model/transaction/AggregateTransaction';
4647
import {CosignatureSignedTransaction} from '../../src/model/transaction/CosignatureSignedTransaction';
48+
import { CosignatureTransaction } from '../../src/model/transaction/CosignatureTransaction';
4749
import {Deadline} from '../../src/model/transaction/Deadline';
4850
import { HashLockTransaction } from '../../src/model/transaction/HashLockTransaction';
4951
import {HashType} from '../../src/model/transaction/HashType';
@@ -1641,6 +1643,67 @@ describe('TransactionHttp', () => {
16411643
});
16421644
});
16431645

1646+
describe('SignTransactionGivenSignatures', () => {
1647+
let listener: Listener;
1648+
before (() => {
1649+
listener = new Listener(config.apiUrl);
1650+
return listener.open();
1651+
});
1652+
after(() => {
1653+
return listener.close();
1654+
});
1655+
it('Announce cosign signatures given', (done) => {
1656+
1657+
/**
1658+
* @see https://github.com/nemtech/nem2-sdk-typescript-javascript/issues/112
1659+
*/
1660+
// AliceAccount: account
1661+
// BobAccount: account
1662+
1663+
const sendAmount = NetworkCurrencyMosaic.createRelative(1000);
1664+
const backAmount = NetworkCurrencyMosaic.createRelative(1);
1665+
1666+
const aliceTransferTransaction = TransferTransaction.create(Deadline.create(), account2.address, [sendAmount],
1667+
PlainMessage.create('payout'), NetworkType.MIJIN_TEST);
1668+
const bobTransferTransaction = TransferTransaction.create(Deadline.create(), account.address, [backAmount],
1669+
PlainMessage.create('payout'), NetworkType.MIJIN_TEST);
1670+
1671+
// 01. Alice creates the aggregated tx and sign it. Then payload send to Bob
1672+
const aggregateTransaction = AggregateTransaction.createComplete(
1673+
Deadline.create(),
1674+
[
1675+
aliceTransferTransaction.toAggregate(account.publicAccount),
1676+
bobTransferTransaction.toAggregate(account2.publicAccount),
1677+
],
1678+
NetworkType.MIJIN_TEST,
1679+
[],
1680+
);
1681+
1682+
const aliceSignedTransaction = aggregateTransaction.signWith(account, generationHash);
1683+
1684+
// 02 Bob cosigns the tx and sends it back to Alice
1685+
const signedTxBob = CosignatureTransaction.signTransactionPayload(account2, aliceSignedTransaction.payload, generationHash);
1686+
1687+
// 03. Alice collects the cosignatures, recreate, sign, and announces the transaction
1688+
const cosignatureSignedTransactions = [
1689+
new CosignatureSignedTransaction(signedTxBob.parentHash, signedTxBob.signature, signedTxBob.signer),
1690+
];
1691+
const recreatedTx = TransactionMapping.createFromPayload(aliceSignedTransaction.payload) as AggregateTransaction;
1692+
1693+
const signedTransaction = recreatedTx.signTransactionGivenSignatures(account, cosignatureSignedTransactions, generationHash);
1694+
1695+
listener.confirmed(account.address).subscribe(() => {
1696+
done();
1697+
});
1698+
listener.status(account.address).subscribe((error) => {
1699+
console.log('Error:', error);
1700+
assert(false);
1701+
done();
1702+
});
1703+
transactionHttp.announce(signedTransaction);
1704+
});
1705+
});
1706+
16441707
describe('transactions', () => {
16451708
it('should call transactions successfully', (done) => {
16461709
accountHttp.transactions(account.publicAccount).subscribe((transactions) => {

src/model/transaction/CosignatureTransaction.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
*/
1616

1717
import { SignSchema } from '../../core/crypto';
18+
import { Convert } from '../../core/format/Convert';
1819
import {CosignatureTransaction as CosignaturetransactionLibrary} from '../../infrastructure/builders/CosignatureTransaction';
20+
import { VerifiableTransaction } from '../../infrastructure/builders/VerifiableTransaction';
1921
import {Account} from '../account/Account';
2022
import {AggregateTransaction} from './AggregateTransaction';
2123
import {CosignatureSignedTransaction} from './CosignatureSignedTransaction';
22-
import { VerifiableTransaction } from '../../infrastructure/builders/VerifiableTransaction';
2324

2425
/**
2526
* Cosignature transaction is used to sign an aggregate transactions with missing cosignatures.
@@ -52,14 +53,14 @@ export class CosignatureTransaction {
5253
* Creating a new CosignatureSignedTransaction
5354
* @param account - The signing account
5455
* @param payload - off transaction payload (aggregated transaction is unannounced)
55-
* @param gernationHash - Network generation hash
56+
* @param generationHash - Network generation hash
5657
* @returns {CosignatureSignedTransaction}
5758
*/
58-
public static signTransactionPayload(account: Account, payload: string, gernationHash: string): CosignatureSignedTransaction {
59+
public static signTransactionPayload(account: Account, payload: string, generationHash: string): CosignatureSignedTransaction {
5960
/**
6061
* For aggregated complete transaction, cosignatories are gathered off chain announced.
6162
*/
62-
const transactionHash = VerifiableTransaction.createTransactionHash(payload, gernationHash);
63+
const transactionHash = VerifiableTransaction.createTransactionHash(payload, Array.from(Convert.hexToUint8(generationHash)));
6364
const aggregateSignatureTransaction = new CosignaturetransactionLibrary(transactionHash);
6465
const signedTransactionRaw = aggregateSignatureTransaction.signCosignatoriesTransaction(account);
6566
return new CosignatureSignedTransaction(signedTransactionRaw.parentHash,
@@ -81,4 +82,4 @@ export class CosignatureTransaction {
8182
signedTransactionRaw.signature,
8283
signedTransactionRaw.signer);
8384
}
84-
}
85+
}

test/model/transaction/AggregateTransaction.spec.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ describe('AggregateTransaction', () => {
388388

389389
it('Should create signed transaction with cosignatories - Aggregated Complete', () => {
390390
/**
391-
* https://github.com/nemtech/nem2-sdk-typescript-javascript/issues/112
391+
* @see https://github.com/nemtech/nem2-sdk-typescript-javascript/issues/112
392392
*/
393393
const accountAlice = TestingAccount;
394394
const accountBob = CosignatoryAccount;
@@ -410,22 +410,24 @@ describe('AggregateTransaction', () => {
410410
PlainMessage.create('c to a'),
411411
NetworkType.MIJIN_TEST);
412412

413-
// 01. Alice creates the aggregated tx and serialize it, Then payload send to Bob & Carol
414-
const aggregateTransactionPayload = AggregateTransaction.createComplete(
413+
// 01. Alice creates the aggregated tx and sign it, Then payload send to Bob & Carol
414+
const aggregateTransaction = AggregateTransaction.createComplete(
415415
Deadline.create(),
416416
[
417417
AtoBTx.toAggregate(accountAlice.publicAccount),
418418
BtoATx.toAggregate(accountBob.publicAccount),
419419
CtoATx.toAggregate(accountCarol.publicAccount)],
420420
NetworkType.MIJIN_TEST,
421421
[],
422-
).serialize();
422+
);
423+
424+
const aliceSignedTransaction = aggregateTransaction.signWith(accountAlice, generationHash);
423425

424426
// 02.1 Bob cosigns the tx and sends it back to Alice
425-
const signedTxBob = CosignatureTransaction.signTransactionPayload(accountBob, aggregateTransactionPayload, generationHash);
427+
const signedTxBob = CosignatureTransaction.signTransactionPayload(accountBob, aliceSignedTransaction.payload, generationHash);
426428

427429
// 02.2 Carol cosigns the tx and sends it back to Alice
428-
const signedTxCarol = CosignatureTransaction.signTransactionPayload(accountCarol, aggregateTransactionPayload, generationHash);
430+
const signedTxCarol = CosignatureTransaction.signTransactionPayload(accountCarol, aliceSignedTransaction.payload, generationHash);
429431

430432
// 03. Alice collects the cosignatures, recreate, sign, and announces the transaction
431433

@@ -435,14 +437,20 @@ describe('AggregateTransaction', () => {
435437
new CosignatureSignedTransaction(signedTxCarol.parentHash, signedTxCarol.signature, signedTxCarol.signer),
436438
];
437439

438-
const recreatedTx = TransactionMapping.createFromPayload(aggregateTransactionPayload) as AggregateTransaction;
440+
const recreatedTx = TransactionMapping.createFromPayload(aliceSignedTransaction.payload) as AggregateTransaction;
439441

440442
const signedTransaction = recreatedTx.signTransactionGivenSignatures(accountAlice, cosignatureSignedTransactions, generationHash);
441443

442444
expect(signedTransaction.type).to.be.equal(TransactionType.AGGREGATE_COMPLETE);
443445
expect(signedTransaction.signer).to.be.equal(accountAlice.publicKey);
444446
expect(signedTransaction.payload.indexOf(accountBob.publicKey) > -1).to.be.true;
445447
expect(signedTransaction.payload.indexOf(accountCarol.publicKey) > -1).to.be.true;
448+
449+
// To make sure that the new cosign method returns the same payload & hash as standard cosigning
450+
const standardCosignedTransaction = aggregateTransaction
451+
.signTransactionWithCosignatories(accountAlice, [accountBob, accountCarol], generationHash);
452+
expect(standardCosignedTransaction.payload).to.be.equal(signedTransaction.payload);
453+
expect(standardCosignedTransaction.hash).to.be.equal(signedTransaction.hash);
446454
});
447455

448456
describe('size', () => {

0 commit comments

Comments
 (0)