Skip to content

Commit 1e28de9

Browse files
committed
Added serialize method for transaction
1 parent 0a008bc commit 1e28de9

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

src/model/transaction/Transaction.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { VerifiableTransaction } from 'nem2-library';
18+
import { SerializeTransactionToJSON } from '../../infrastructure/transaction/SerializeTransactionToJSON';
1819
import { Account } from '../account/Account';
1920
import { PublicAccount } from '../account/PublicAccount';
2021
import { NetworkType } from '../blockchain/NetworkType';
@@ -25,7 +26,6 @@ import { InnerTransaction } from './InnerTransaction';
2526
import { SignedTransaction } from './SignedTransaction';
2627
import { TransactionInfo } from './TransactionInfo';
2728
import { TransactionType } from './TransactionType';
28-
import { SerializeTransactionToJSON } from '../../infrastructure/transaction/SerializeTransactionToJSON';
2929

3030
/**
3131
* An abstract transaction class that serves as the base class of all NEM transactions.
@@ -176,7 +176,19 @@ export abstract class Transaction {
176176
}
177177

178178
/**
179-
* Create JSON object
179+
* @description Serialize a transaction object
180+
* @returns {string}
181+
* @memberof Transaction
182+
*/
183+
public serialize() {
184+
const transaction = this.buildTransaction();
185+
return transaction.serializeUnsignedTransaction();
186+
}
187+
188+
/**
189+
* @description Create JSON object
190+
* @returns {Object}
191+
* @memberof Transaction
180192
*/
181193
public toJSON() {
182194
const commonTransactionObject = {

src/utility/TransactionMapping.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,4 @@ export class TransactionMapping {
3737
public static createFromPayload(dataBytes: string): Transaction {
3838
return CreateTransactionFromPayload(dataBytes);
3939
}
40-
41-
/**
42-
* Serialize a transaction.
43-
* @returns {string} Serialized transaction in binary.
44-
*/
45-
public static serialize(): string {
46-
throw new Error();
47-
}
48-
49-
/**
50-
* Create transaction class from Json.
51-
* @returns {object} Serialized transaction in json object.
52-
*/
53-
public static serializeJson(): object {
54-
throw new Error();
55-
}
5640
}

test/model/transaction/Transaction.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,24 @@ describe('Transaction', () => {
180180
}).to.throw(Error, 'Inner transaction cannot be an aggregated transaction.');
181181
});
182182
});
183+
184+
describe('Transaction serialize', () => {
185+
it('Should return serialized payload', () => {
186+
const transaction = TransferTransaction.create(
187+
Deadline.create(),
188+
Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'),
189+
[],
190+
PlainMessage.create('test-message'),
191+
NetworkType.MIJIN_TEST,
192+
);
193+
const serialized = transaction.serialize();
194+
195+
expect(serialized.substring(
196+
240,
197+
serialized.length,
198+
)).to.be.equal('9050B9837EFAB4BBE8A4B9BB32D812F9885C00D8FC1650E1420D000000746573742D6D657373616765');
199+
});
200+
});
183201
});
184202

185203
class FakeTransaction extends Transaction {

0 commit comments

Comments
 (0)