Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add stacks transaction memo equality by removing trailing empty … #1630

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
Loading