@@ -15,6 +15,18 @@ 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+ loadRef ( ) : CommentSlice ;
28+ }
29+
1830export class Transaction extends BaseTransaction {
1931 public recipient : Recipient ;
2032 public bounceable : boolean ;
@@ -235,7 +247,14 @@ export class Transaction extends BaseTransaction {
235247 explainTransaction ( ) : TransactionExplanation {
236248 const displayOrder = [ 'id' , 'outputs' , 'outputAmount' , 'changeOutputs' , 'changeAmount' , 'fee' , 'withdrawAmount' ] ;
237249
238- const outputs : TransactionRecipient [ ] = [ this . recipient ] ;
250+ // Only a plain send's body comment is a user memo; the other transaction types carry
251+ // protocol payloads (e.g. withdraw opcodes) in `message`, which must not be exposed as memo.
252+ const outputs : TransactionRecipient [ ] = [
253+ {
254+ ...this . recipient ,
255+ ...( this . type === TransactionType . Send && this . message ? { memo : this . message } : { } ) ,
256+ } ,
257+ ] ;
239258 const outputAmount = this . recipient . amount ;
240259 const withdrawAmount = this . withdrawAmount ;
241260 return {
@@ -295,6 +314,29 @@ export class Transaction extends BaseTransaction {
295314 } ;
296315 }
297316
317+ /**
318+ * Decode a comment cell chain per the TON snake format (TL-B SnakeData: raw bytes
319+ * per cell, refs chain to continuations with no length prefix), mirroring the
320+ * standard reader (@ton/core readBuffer) exactly: byte-aligned cells, at most one
321+ * ref per cell, whole-cell text concatenated across the chain. Binding the memo to
322+ * the full text means a prebuild cannot carry the declared memoId in its first
323+ * cell while chain readers credit a longer, different comment, nor smuggle an
324+ * undeclared comment past verification as no comment. Violations fail closed
325+ * (throw) exactly where the standard reader throws.
326+ */
327+ private loadComment ( slice : CommentSlice ) : string {
328+ const freeBits = slice . getFreeBits ( ) ;
329+ const remainingRefs = slice . refs . length - slice . refCursor ;
330+ if ( freeBits % 8 !== 0 || ( remainingRefs !== 0 && remainingRefs !== 1 ) ) {
331+ throw new Error ( 'invalid snake-format comment' ) ;
332+ }
333+ let text = new TextDecoder ( ) . decode ( slice . loadBits ( freeBits ) ) ;
334+ if ( remainingRefs === 1 ) {
335+ text += this . loadComment ( slice . loadRef ( ) ) ;
336+ }
337+ return text ;
338+ }
339+
298340 private parseTransactionBody ( slice : any ) : any {
299341 const signature = Buffer . from ( slice . loadBits ( 512 ) ) . toString ( 'hex' ) ;
300342 // signing message
@@ -354,8 +396,7 @@ export class Transaction extends BaseTransaction {
354396 if ( order . getFreeBits ( ) > 32 ) {
355397 const opcode = order . loadUint ( 32 ) . toNumber ( ) ;
356398 if ( opcode === 0 ) {
357- const payloadBytes = order . loadBits ( order . getFreeBits ( ) ) ;
358- payload = new TextDecoder ( ) . decode ( payloadBytes ) ;
399+ payload = this . loadComment ( order ) ;
359400 if ( payload === 'Deposit' ) {
360401 this . transactionType = TransactionType . TonWhalesVestingDeposit ;
361402 } else if ( payload === 'Withdraw' ) {
@@ -382,18 +423,32 @@ export class Transaction extends BaseTransaction {
382423 const forwarderAddress = order . loadAddress ( ) ;
383424 if ( ! forwarderAddress ) throw new Error ( 'invalid forwarderAddress' ) ;
384425
385- order . loadBit ( ) ; // skip bit
426+ const hasCustomPayload = order . loadBit ( ) ;
427+ if ( hasCustomPayload ) {
428+ // consume the custom_payload ref (TEP-74): BitGo builders never write one,
429+ // but a prebuild that sets it shifts the ref cursor so the forward payload
430+ // would otherwise be decoded from the wrong cell
431+ order . loadRef ( ) ;
432+ }
386433
387434 const forwardTonAmount = order . loadCoins ( ) ;
388435 if ( ! forwardTonAmount . gt ( new BN ( 0 ) ) ) throw new Error ( 'invalid forwardTonAmount' ) ;
389436
390437 let message = '' ;
391438 if ( order . loadBit ( ) ) {
392439 order = order . loadRef ( ) ;
440+ // the ref may be an empty cell — TokenTransferBuilder always writes a forward
441+ // payload ref, empty when the transfer carries no comment
442+ if ( order . getFreeBits ( ) > 0 ) {
443+ const messageOpcode = order . loadUint ( 32 ) . toNumber ( ) ;
444+ if ( messageOpcode !== 0 ) throw new Error ( 'invalid message opcode' ) ;
445+ message = this . loadComment ( order ) ;
446+ }
447+ } else if ( order . getFreeBits ( ) > 32 ) {
448+ // Either bit 0: the forward payload is inline in the remaining bits
393449 const messageOpcode = order . loadUint ( 32 ) . toNumber ( ) ;
394450 if ( messageOpcode !== 0 ) throw new Error ( 'invalid message opcode' ) ;
395- const messageBytes = order . loadBits ( order . getFreeBits ( ) ) ;
396- message = new TextDecoder ( ) . decode ( messageBytes ) ;
451+ message = this . loadComment ( order ) ;
397452 }
398453
399454 payload = {
0 commit comments