Skip to content

Commit

Permalink
fix: add stacks transaction memo equality by removing trailing empty …
Browse files Browse the repository at this point in the history
…unicode characters
  • Loading branch information
janniks committed Feb 19, 2024
1 parent 922e037 commit 85fbfb0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/transactions/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ export function serializeMemoString(memoString: MemoString): Uint8Array {
}

export function deserializeMemoString(bytesReader: BytesReader): MemoString {
const content = bytesToUtf8(bytesReader.readBytes(MEMO_MAX_LENGTH_BYTES));
let content = bytesToUtf8(bytesReader.readBytes(MEMO_MAX_LENGTH_BYTES));
content = content.replace(/\u0000*$/, ''); // remove all trailing unicode empty characters
return { type: StacksMessageType.MemoString, content };
}

Expand Down
38 changes: 38 additions & 0 deletions packages/transactions/tests/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2187,3 +2187,41 @@ test('Post-conditions with amount larger than 8 bytes throw an error', () => {
serializePostCondition(fungiblePc);
}).toThrowError('The post-condition amount may not be larger than 8 bytes');
});

test('StacksTransaction serialize/deserialize equality with an empty memo', async () => {
const options = {
recipient: 'SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159',
amount: 12345n,
fee: 100n,
nonce: 0n,
memo: '', // empty memo
network: new StacksMainnet(),
anchorMode: AnchorMode.Any,
senderKey: 'edf9aee84d9b7abc145504dde6726c64f369d37ee34ded868fabd876c26570bc01',
};
const tx = await makeSTXTokenTransfer(options);

const txHex = tx.serialize();
const txDecoded = deserializeTransaction(txHex);

expect(txDecoded).toEqual(tx);
});

test('StacksTransaction serialize/deserialize equality with a memo', async () => {
const options = {
recipient: 'SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159',
amount: 12345n,
fee: 100n,
nonce: 0n,
memo: 'Hey there',
network: new StacksMainnet(),
anchorMode: AnchorMode.Any,
senderKey: 'edf9aee84d9b7abc145504dde6726c64f369d37ee34ded868fabd876c26570bc01',
};
const tx = await makeSTXTokenTransfer(options);

const txHex = tx.serialize();
const txDecoded = deserializeTransaction(txHex);

expect(txDecoded).toEqual(tx);
});

0 comments on commit 85fbfb0

Please sign in to comment.