Skip to content

Commit 1402e91

Browse files
committed
Added serialize method for transaction
1 parent 7bbafae commit 1402e91

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';
@@ -24,7 +25,6 @@ import { Deadline } from './Deadline';
2425
import { InnerTransaction } from './InnerTransaction';
2526
import { SignedTransaction } from './SignedTransaction';
2627
import { TransactionInfo } from './TransactionInfo';
27-
import { SerializeTransactionToJSON } from '../../infrastructure/transaction/SerializeTransactionToJSON';
2828

2929
/**
3030
* An abstract transaction class that serves as the base class of all NEM transactions.
@@ -172,7 +172,19 @@ export abstract class Transaction {
172172
}
173173

174174
/**
175-
* Create JSON object
175+
* @description Serialize a transaction object
176+
* @returns {string}
177+
* @memberof Transaction
178+
*/
179+
public serialize() {
180+
const transaction = this.buildTransaction();
181+
return transaction.serializeUnsignedTransaction();
182+
}
183+
184+
/**
185+
* @description Create JSON object
186+
* @returns {Object}
187+
* @memberof Transaction
176188
*/
177189
public toJSON() {
178190
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
@@ -149,6 +149,24 @@ describe('Transaction', () => {
149149
expect(after).to.be.equal(true);
150150
});
151151
});
152+
153+
describe('Transaction serialize', () => {
154+
it('Should return serialized payload', () => {
155+
const transaction = TransferTransaction.create(
156+
Deadline.create(),
157+
Address.createFromRawAddress('SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC'),
158+
[],
159+
PlainMessage.create('test-message'),
160+
NetworkType.MIJIN_TEST,
161+
);
162+
const serialized = transaction.serialize();
163+
164+
expect(serialized.substring(
165+
240,
166+
serialized.length,
167+
)).to.be.equal('9050B9837EFAB4BBE8A4B9BB32D812F9885C00D8FC1650E1420D000000746573742D6D657373616765');
168+
});
169+
});
152170
});
153171

154172
class FakeTransaction extends Transaction {

0 commit comments

Comments
 (0)