Skip to content

Commit 897e5e7

Browse files
fix: raw message encoding in TransferTransaction (#853)
1 parent 894a017 commit 897e5e7

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/model/transaction/TransferTransaction.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,15 @@ export class TransferTransaction extends Transaction {
202202
if (!this.message || !this.message.payload) {
203203
return Uint8Array.of();
204204
}
205-
const messgeHex =
206-
this.message.type === MessageType.PersistentHarvestingDelegationMessage
207-
? this.message.payload
208-
: Convert.utf8ToHex(this.message.payload);
209-
const payloadBuffer = Convert.hexToUint8(messgeHex);
205+
const isPersistentHarvestingDelegationMessage = this.message.type === MessageType.PersistentHarvestingDelegationMessage;
206+
const isRawMessage = this.message.type === MessageType.RawMessage;
207+
208+
const messageHex =
209+
isPersistentHarvestingDelegationMessage || isRawMessage ? this.message.payload : Convert.utf8ToHex(this.message.payload);
210+
const payloadBuffer = Convert.hexToUint8(messageHex);
210211
const typeBuffer = GeneratorUtils.uintToBuffer(this.message.type, 1);
211-
return this.message.type === MessageType.PersistentHarvestingDelegationMessage || !this.message.payload
212+
213+
return isPersistentHarvestingDelegationMessage || isRawMessage || !this.message.payload
212214
? payloadBuffer
213215
: GeneratorUtils.concatTypedArrays(typeBuffer, payloadBuffer);
214216
}

test/model/transaction/TransferTransaction.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { TransactionMapping } from '../../../src/core/utils';
2121
import { CreateTransactionFromPayload } from '../../../src/infrastructure/transaction';
2222
import { PersistentHarvestingDelegationMessage, UInt64 } from '../../../src/model';
2323
import { Account, Address } from '../../../src/model/account';
24-
import { EmptyMessage, MessageMarker, MessageType, PlainMessage } from '../../../src/model/message';
24+
import { EmptyMessage, MessageMarker, MessageType, PlainMessage, RawMessage } from '../../../src/model/message';
2525
import { Mosaic, MosaicId } from '../../../src/model/mosaic';
2626
import { NamespaceId } from '../../../src/model/namespace';
2727
import { NetworkType } from '../../../src/model/network';
@@ -133,6 +133,29 @@ describe('TransferTransaction', () => {
133133
);
134134
});
135135

136+
it('should createComplete an TransferTransaction object with raw message', () => {
137+
// Arrange:
138+
const messageBytes = new Uint8Array([3, 2, 1, 123, 0, 255]);
139+
const messageHex = '0302017B00FF';
140+
141+
// Act:
142+
const transferTransaction = TransferTransaction.create(
143+
Deadline.create(epochAdjustment),
144+
testAddress,
145+
[],
146+
RawMessage.create(messageBytes),
147+
TestNetworkType,
148+
);
149+
const transactionPayload = transferTransaction.signWith(account, generationHash).payload;
150+
const recreatedTransferTransaction = TransferTransaction.createFromPayload(transactionPayload) as TransferTransaction;
151+
152+
// Assert:
153+
expect(transferTransaction.message.type).to.be.equal(MessageType.RawMessage);
154+
expect(transferTransaction.message.payload).to.be.equal(messageHex);
155+
expect(recreatedTransferTransaction.message.type).to.be.equal(MessageType.RawMessage);
156+
expect(recreatedTransferTransaction.message.payload).to.be.equal(messageHex);
157+
});
158+
136159
it('should createComplete an TransferTransaction object and sign it with mosaics', () => {
137160
const transferTransaction = TransferTransaction.create(
138161
Deadline.create(epochAdjustment),

0 commit comments

Comments
 (0)