@@ -15,6 +15,19 @@ import {
1515 SINGLE_NOMINATOR_WITHDRAW_ALL_COMMENT ,
1616} from './constants' ;
1717
18+ /**
19+ * The subset of the tonweb Slice API the comment decoders rely on. tonweb ships no
20+ * type definitions for Slice, so this structural type documents the access pattern.
21+ */
22+ interface CommentSlice {
23+ refs : unknown [ ] ;
24+ refCursor : number ;
25+ getFreeBits ( ) : number ;
26+ loadBits ( bits : number ) : Uint8Array ;
27+ loadUint ( bits : number ) : { toNumber ( ) : number } ;
28+ loadRef ( ) : CommentSlice ;
29+ }
30+
1831export class Transaction extends BaseTransaction {
1932 public recipient : Recipient ;
2033 public bounceable : boolean ;
@@ -235,7 +248,14 @@ export class Transaction extends BaseTransaction {
235248 explainTransaction ( ) : TransactionExplanation {
236249 const displayOrder = [ 'id' , 'outputs' , 'outputAmount' , 'changeOutputs' , 'changeAmount' , 'fee' , 'withdrawAmount' ] ;
237250
238- const outputs : TransactionRecipient [ ] = [ this . recipient ] ;
251+ // Only a plain send's body comment is a user memo; the other transaction types carry
252+ // protocol payloads (e.g. withdraw opcodes) in `message`, which must not be exposed as memo.
253+ const outputs : TransactionRecipient [ ] = [
254+ {
255+ ...this . recipient ,
256+ ...( this . type === TransactionType . Send && this . message ? { memo : this . message } : { } ) ,
257+ } ,
258+ ] ;
239259 const outputAmount = this . recipient . amount ;
240260 const withdrawAmount = this . withdrawAmount ;
241261 return {
@@ -295,6 +315,30 @@ export class Transaction extends BaseTransaction {
295315 } ;
296316 }
297317
318+ /**
319+ * Decode a comment cell per the TON comment standard: the current cell holds the
320+ * text, and each ref is a snake-format continuation cell (8-bit byte count, then
321+ * that many bytes). Standard chain readers concatenate the whole chain, so the
322+ * memo must be the full text — otherwise a prebuild could carry the declared
323+ * memoId in its first cell while chain readers credit a longer, different
324+ * comment, or smuggle an undeclared comment past verification as no comment.
325+ * A malformed continuation fails closed (throws) rather than exposing partial
326+ * text.
327+ */
328+ private loadComment ( slice : CommentSlice ) : string {
329+ const decoder = new TextDecoder ( ) ;
330+ let text = decoder . decode ( slice . loadBits ( slice . getFreeBits ( ) ) ) ;
331+ while ( slice . refs . length - slice . refCursor > 0 ) {
332+ slice = slice . loadRef ( ) ;
333+ const byteCount = slice . loadUint ( 8 ) . toNumber ( ) ;
334+ if ( byteCount * 8 > slice . getFreeBits ( ) ) {
335+ throw new Error ( 'invalid snake-format comment' ) ;
336+ }
337+ text += decoder . decode ( slice . loadBits ( byteCount * 8 ) ) ;
338+ }
339+ return text ;
340+ }
341+
298342 private parseTransactionBody ( slice : any ) : any {
299343 const signature = Buffer . from ( slice . loadBits ( 512 ) ) . toString ( 'hex' ) ;
300344 // signing message
@@ -354,8 +398,7 @@ export class Transaction extends BaseTransaction {
354398 if ( order . getFreeBits ( ) > 32 ) {
355399 const opcode = order . loadUint ( 32 ) . toNumber ( ) ;
356400 if ( opcode === 0 ) {
357- const payloadBytes = order . loadBits ( order . getFreeBits ( ) ) ;
358- payload = new TextDecoder ( ) . decode ( payloadBytes ) ;
401+ payload = this . loadComment ( order ) ;
359402 if ( payload === 'Deposit' ) {
360403 this . transactionType = TransactionType . TonWhalesVestingDeposit ;
361404 } else if ( payload === 'Withdraw' ) {
@@ -382,18 +425,32 @@ export class Transaction extends BaseTransaction {
382425 const forwarderAddress = order . loadAddress ( ) ;
383426 if ( ! forwarderAddress ) throw new Error ( 'invalid forwarderAddress' ) ;
384427
385- order . loadBit ( ) ; // skip bit
428+ const hasCustomPayload = order . loadBit ( ) ;
429+ if ( hasCustomPayload ) {
430+ // consume the custom_payload ref (TEP-74): BitGo builders never write one,
431+ // but a prebuild that sets it shifts the ref cursor so the forward payload
432+ // would otherwise be decoded from the wrong cell
433+ order . loadRef ( ) ;
434+ }
386435
387436 const forwardTonAmount = order . loadCoins ( ) ;
388437 if ( ! forwardTonAmount . gt ( new BN ( 0 ) ) ) throw new Error ( 'invalid forwardTonAmount' ) ;
389438
390439 let message = '' ;
391440 if ( order . loadBit ( ) ) {
392441 order = order . loadRef ( ) ;
442+ // the ref may be an empty cell — TokenTransferBuilder always writes a forward
443+ // payload ref, empty when the transfer carries no comment
444+ if ( order . getFreeBits ( ) > 0 ) {
445+ const messageOpcode = order . loadUint ( 32 ) . toNumber ( ) ;
446+ if ( messageOpcode !== 0 ) throw new Error ( 'invalid message opcode' ) ;
447+ message = this . loadComment ( order ) ;
448+ }
449+ } else if ( order . getFreeBits ( ) > 32 ) {
450+ // Either bit 0: the forward payload is inline in the remaining bits
393451 const messageOpcode = order . loadUint ( 32 ) . toNumber ( ) ;
394452 if ( messageOpcode !== 0 ) throw new Error ( 'invalid message opcode' ) ;
395- const messageBytes = order . loadBits ( order . getFreeBits ( ) ) ;
396- message = new TextDecoder ( ) . decode ( messageBytes ) ;
453+ message = this . loadComment ( order ) ;
397454 }
398455
399456 payload = {
0 commit comments