Skip to content

Commit 2012207

Browse files
authored
Fixed #365 (#636)
- Added optional param in CosignatureTransaction.signWith
1 parent 5684000 commit 2012207

File tree

2 files changed

+122
-56
lines changed

2 files changed

+122
-56
lines changed

src/model/transaction/CosignatureTransaction.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { Transaction } from './Transaction';
2626
*/
2727
export class CosignatureTransaction {
2828
/**
29-
* @param transactionToCosign
29+
* @param transactionToCosign Aggregate transaction
3030
*/
3131
constructor(
3232
/**
@@ -66,13 +66,14 @@ export class CosignatureTransaction {
6666
/**
6767
* Serialize and sign transaction creating a new SignedTransaction
6868
* @param account
69+
* @param transactionHash Transaction hash (optional)
6970
* @returns {CosignatureSignedTransaction}
7071
*/
71-
public signWith(account: Account): CosignatureSignedTransaction {
72-
if (!this.transactionToCosign.transactionInfo!.hash) {
72+
public signWith(account: Account, transactionHash?: string): CosignatureSignedTransaction {
73+
if ((!this.transactionToCosign.transactionInfo || !this.transactionToCosign.transactionInfo!.hash) && !transactionHash) {
7374
throw new Error('Transaction to cosign should be announced first');
7475
}
75-
const hash = this.transactionToCosign.transactionInfo!.hash;
76+
const hash = !transactionHash ? this.transactionToCosign.transactionInfo!.hash : transactionHash;
7677
const hashBytes = Convert.hexToUint8(hash ? hash : '');
7778
const keyPairEncoded = KeyPair.createKeyPairFromPrivateKeyString(account.privateKey);
7879
const signature = KeyPair.sign(keyPairEncoded, new Uint8Array(hashBytes));

test/model/transaction/CosignatureTransaction.spec.ts

Lines changed: 117 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -32,63 +32,63 @@ describe('CosignatureTransaction', () => {
3232
account = TestingAccount;
3333
});
3434

35-
it('should createComplete an TransferTransaction object and sign it', () => {
36-
const aggregateTransferTransactionDTO = {
37-
meta: {
38-
hash: '671653C94E2254F2A23EFEDB15D67C38332AED1FBD24B063C0A8E675582B6A96',
39-
height: '18160',
40-
id: '5A0069D83F17CF0001777E55',
41-
index: 0,
42-
merkleComponentHash: '81E5E7AE49998802DABC816EC10158D3A7879702FF29084C2C992CD1289877A7',
43-
},
44-
transaction: {
45-
cosignatures: [
46-
{
47-
version: '0',
48-
signature:
49-
'5780C8DF9D46BA2BCF029DCC5D3BF55FE1CB5BE7ABCF30387C4637DD' +
50-
'EDFC2152703CA0AD95F21BB9B942F3CC52FCFC2064C7B84CF60D1A9E69195F1943156C07',
51-
signerPublicKey: 'A5F82EC8EBB341427B6785C8111906CD0DF18838FB11B51CE0E18B5E79DFF630',
35+
const aggregateTransferTransactionDTO = {
36+
meta: {
37+
hash: '671653C94E2254F2A23EFEDB15D67C38332AED1FBD24B063C0A8E675582B6A96',
38+
height: '18160',
39+
id: '5A0069D83F17CF0001777E55',
40+
index: 0,
41+
merkleComponentHash: '81E5E7AE49998802DABC816EC10158D3A7879702FF29084C2C992CD1289877A7',
42+
},
43+
transaction: {
44+
cosignatures: [
45+
{
46+
version: '0',
47+
signature:
48+
'5780C8DF9D46BA2BCF029DCC5D3BF55FE1CB5BE7ABCF30387C4637DD' +
49+
'EDFC2152703CA0AD95F21BB9B942F3CC52FCFC2064C7B84CF60D1A9E69195F1943156C07',
50+
signerPublicKey: 'A5F82EC8EBB341427B6785C8111906CD0DF18838FB11B51CE0E18B5E79DFF630',
51+
},
52+
],
53+
deadline: '1000',
54+
maxFee: '0',
55+
signature:
56+
'939673209A13FF82397578D22CC96EB8516A6760C894D9B7535E3A1E0680' +
57+
'07B9255CFA9A914C97142A7AE18533E381C846B69D2AE0D60D1DC8A55AD120E2B606',
58+
signerPublicKey: '7681ED5023141D9CDCF184E5A7B60B7D466739918ED5DA30F7E71EA7B86EFF2D',
59+
transactions: [
60+
{
61+
meta: {
62+
aggregateHash: '3D28C804EDD07D5A728E5C5FFEC01AB07AFA5766AE6997B38526D36015A4D006',
63+
aggregateId: '5A0069D83F17CF0001777E55',
64+
height: '18160',
65+
id: '5A0069D83F17CF0001777E56',
66+
index: 0,
5267
},
53-
],
54-
deadline: '1000',
55-
maxFee: '0',
56-
signature:
57-
'939673209A13FF82397578D22CC96EB8516A6760C894D9B7535E3A1E0680' +
58-
'07B9255CFA9A914C97142A7AE18533E381C846B69D2AE0D60D1DC8A55AD120E2B606',
59-
signerPublicKey: '7681ED5023141D9CDCF184E5A7B60B7D466739918ED5DA30F7E71EA7B86EFF2D',
60-
transactions: [
61-
{
62-
meta: {
63-
aggregateHash: '3D28C804EDD07D5A728E5C5FFEC01AB07AFA5766AE6997B38526D36015A4D006',
64-
aggregateId: '5A0069D83F17CF0001777E55',
65-
height: '18160',
66-
id: '5A0069D83F17CF0001777E56',
67-
index: 0,
68+
transaction: {
69+
message: {
70+
payload: '746573742D6D657373616765',
71+
type: 0,
6872
},
69-
transaction: {
70-
message: {
71-
payload: '746573742D6D657373616765',
72-
type: 0,
73+
mosaics: [
74+
{
75+
amount: '100',
76+
id: '85BBEA6CC462B244',
7377
},
74-
mosaics: [
75-
{
76-
amount: '100',
77-
id: '85BBEA6CC462B244',
78-
},
79-
],
80-
recipientAddress: '6823BB7C3C089D996585466380EDBDC19D4959184893E38C',
81-
signerPublicKey: 'B4F12E7C9F6946091E2CB8B6D3A12B50D17CCBBF646386EA27CE2946A7423DCF',
82-
type: 16724,
83-
version: 36865,
84-
},
78+
],
79+
recipientAddress: '6823BB7C3C089D996585466380EDBDC19D4959184893E38C',
80+
signerPublicKey: 'B4F12E7C9F6946091E2CB8B6D3A12B50D17CCBBF646386EA27CE2946A7423DCF',
81+
type: 16724,
82+
version: 36865,
8583
},
86-
],
87-
type: 16705,
88-
version: 36865,
89-
},
90-
};
84+
},
85+
],
86+
type: 16705,
87+
version: 36865,
88+
},
89+
};
9190

91+
it('should createComplete an TransferTransaction object and sign it', () => {
9292
const aggregateTransferTransaction = CreateTransactionFromDTO(aggregateTransferTransactionDTO);
9393

9494
const cosignatureTransaction = CosignatureTransaction.create(aggregateTransferTransaction as AggregateTransaction);
@@ -119,4 +119,69 @@ describe('CosignatureTransaction', () => {
119119
expect(signedTx.signerPublicKey).to.be.equal(account.publicKey);
120120
expect(signedTx.version.toString()).to.be.equal('0');
121121
});
122+
123+
it('should sign a transaction with provided transactionHash', () => {
124+
const tx = TransferTransaction.create(
125+
Deadline.create(),
126+
account.address,
127+
[],
128+
PlainMessage.create('a to b'),
129+
NetworkType.MIJIN_TEST,
130+
);
131+
132+
const aggregate = AggregateTransaction.createComplete(
133+
Deadline.create(),
134+
[tx.toAggregate(account.publicAccount)],
135+
NetworkType.MIJIN_TEST,
136+
[],
137+
);
138+
const txHash = '9801508C58666C746F471538E43002B85B1CD542F9874B2861183919BA8787B6';
139+
140+
const cosignTx = new CosignatureTransaction(aggregate);
141+
142+
const signedTx = cosignTx.signWith(account, txHash);
143+
144+
expect(signedTx.parentHash).to.be.equal('9801508C58666C746F471538E43002B85B1CD542F9874B2861183919BA8787B6');
145+
expect(signedTx.signerPublicKey).to.be.equal('9801508C58666C746F471538E43002B85B1CD542F9874B2861183919BA8787B6');
146+
expect(signedTx.signerPublicKey).to.be.equal(account.publicKey);
147+
expect(signedTx.version.toString()).to.be.equal('0');
148+
});
149+
150+
it('should sign a transaction to throw', () => {
151+
const tx = TransferTransaction.create(
152+
Deadline.create(),
153+
account.address,
154+
[],
155+
PlainMessage.create('a to b'),
156+
NetworkType.MIJIN_TEST,
157+
);
158+
159+
const aggregate = AggregateTransaction.createComplete(
160+
Deadline.create(),
161+
[tx.toAggregate(account.publicAccount)],
162+
NetworkType.MIJIN_TEST,
163+
[],
164+
);
165+
const cosignTx = new CosignatureTransaction(aggregate);
166+
expect(() => {
167+
cosignTx.signWith(account);
168+
}).to.throw(Error, 'Transaction to cosign should be announced first');
169+
});
170+
171+
it('should sign a transaction to throw no hash in transaction info', () => {
172+
Object.assign(aggregateTransferTransactionDTO, {
173+
meta: {
174+
hash: undefined,
175+
height: '18160',
176+
id: '5A0069D83F17CF0001777E55',
177+
index: 0,
178+
merkleComponentHash: '81E5E7AE49998802DABC816EC10158D3A7879702FF29084C2C992CD1289877A7',
179+
},
180+
});
181+
const aggregate = CreateTransactionFromDTO(aggregateTransferTransactionDTO) as AggregateTransaction;
182+
const cosignTx = new CosignatureTransaction(aggregate);
183+
expect(() => {
184+
cosignTx.signWith(account);
185+
}).to.throw(Error, 'Transaction to cosign should be announced first');
186+
});
122187
});

0 commit comments

Comments
 (0)