Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 14 additions & 3 deletions modules/sdk-coin-polyx/src/lib/tokenTransferBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,25 @@ export class TokenTransferBuilder extends PolyxBaseBuilder<TxMethod, Transaction

/**
* Sets the memo for the transaction.
* Pads the memo on the left with zeros to ensure it is 32 characters long.
* Encodes the memo as UTF-8 bytes right-padded with zero bytes to 32 bytes,
* matching the Polymesh Memo type ([u8; 32]).
*
* @param {string} memo - The memo for the transaction.
* @returns {this} The current instance of the builder.
*/
memo(memo: string): this {
const paddedMemo = memo.padStart(32, '0');
this._memo = paddedMemo;
// fromImplementation passes the decoded on-chain hex (0x + 64 hex chars) — pass through unchanged
if (/^0x[0-9a-fA-F]{64}$/.test(memo)) {
this._memo = memo;
return this;
}
const memoBytes = Buffer.from(memo, 'utf8');
if (memoBytes.length > 32) {
throw new Error('Memo must be 32 bytes or fewer when UTF-8 encoded');
}
const paddedBuffer = Buffer.alloc(32, 0);
memoBytes.copy(paddedBuffer, 0);
this._memo = '0x' + paddedBuffer.toString('hex');
return this;
}

Expand Down
17 changes: 14 additions & 3 deletions modules/sdk-coin-polyx/src/lib/transferBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,25 @@ export class TransferBuilder extends PolyxBaseBuilder<TxMethod, Transaction> {

/**
* The memo to attach to the transfer transaction.
* Pads the memo on the left with zeros to ensure it is 32 characters long.
* Encodes the memo as UTF-8 bytes right-padded with zero bytes to 32 bytes,
* matching the Polymesh Memo type ([u8; 32]).
*
* @param {string} memo The memo string to include.
* @returns {TransferBuilder} This transfer builder.
*/
memo(memo: string): this {
const paddedMemo = memo.padStart(32, '0');
this._memo = paddedMemo;
// fromImplementation passes the decoded on-chain hex (0x + 64 hex chars) — pass through unchanged
if (/^0x[0-9a-fA-F]{64}$/.test(memo)) {
this._memo = memo;
return this;
}
const memoBytes = Buffer.from(memo, 'utf8');
if (memoBytes.length > 32) {
throw new Error('Memo must be 32 bytes or fewer when UTF-8 encoded');
}
const paddedBuffer = Buffer.alloc(32, 0);
memoBytes.copy(paddedBuffer, 0);
this._memo = '0x' + paddedBuffer.toString('hex');
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Polyx token transfer Builder - Testnet', () => {
should.deepEqual(txJson.fromDID, senderDID);
should.deepEqual(txJson.sender, sender.address);
should.deepEqual(txJson.assetId, assetId);
should.deepEqual(txJson.memo, '0x3030303030303030303030303030303030303030303030303030303030303030');
should.deepEqual(txJson.memo, '0x3000000000000000000000000000000000000000000000000000000000000000');
should.deepEqual(txJson.blockNumber, 3933);
should.deepEqual(txJson.referenceBlock, '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d');
should.deepEqual(txJson.genesisHash, genesisHash);
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('Polyx token transfer Builder - Testnet', () => {
should.deepEqual(txJson.fromDID, senderDID);
should.deepEqual(txJson.sender, sender.address);
should.deepEqual(txJson.assetId, assetId);
should.deepEqual(txJson.memo, '0x3030303030303030303030303030303030303030303030303030303030303030');
should.deepEqual(txJson.memo, '0x3000000000000000000000000000000000000000000000000000000000000000');
should.deepEqual(txJson.blockNumber, 3933);
should.deepEqual(txJson.referenceBlock, '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d');
should.deepEqual(txJson.genesisHash, genesisHash);
Expand Down
Loading