@@ -16,6 +16,7 @@ import {TransactionInstruction} from '../transaction';
1616import { AddressLookupTableAccount } from '../programs' ;
1717import { CompiledKeys } from './compiled-keys' ;
1818import { AccountKeysFromLookups , MessageAccountKeys } from './account-keys' ;
19+ import { guardedShift , guardedSplice } from '../utils/guarded-array-utils' ;
1920
2021/**
2122 * Message constructor arguments
@@ -426,7 +427,7 @@ export class MessageV0 {
426427 static deserialize ( serializedMessage : Uint8Array ) : MessageV0 {
427428 let byteArray = [ ...serializedMessage ] ;
428429
429- const prefix = byteArray . shift ( ) as number ;
430+ const prefix = guardedShift ( byteArray ) ;
430431 const maskedPrefix = prefix & VERSION_PREFIX_MASK ;
431432 assert (
432433 prefix !== maskedPrefix ,
@@ -440,29 +441,35 @@ export class MessageV0 {
440441 ) ;
441442
442443 const header : MessageHeader = {
443- numRequiredSignatures : byteArray . shift ( ) as number ,
444- numReadonlySignedAccounts : byteArray . shift ( ) as number ,
445- numReadonlyUnsignedAccounts : byteArray . shift ( ) as number ,
444+ numRequiredSignatures : guardedShift ( byteArray ) ,
445+ numReadonlySignedAccounts : guardedShift ( byteArray ) ,
446+ numReadonlyUnsignedAccounts : guardedShift ( byteArray ) ,
446447 } ;
447448
448449 const staticAccountKeys = [ ] ;
449450 const staticAccountKeysLength = shortvec . decodeLength ( byteArray ) ;
450451 for ( let i = 0 ; i < staticAccountKeysLength ; i ++ ) {
451452 staticAccountKeys . push (
452- new PublicKey ( byteArray . splice ( 0 , PUBLIC_KEY_LENGTH ) ) ,
453+ new PublicKey ( guardedSplice ( byteArray , 0 , PUBLIC_KEY_LENGTH ) ) ,
453454 ) ;
454455 }
455456
456- const recentBlockhash = bs58 . encode ( byteArray . splice ( 0 , PUBLIC_KEY_LENGTH ) ) ;
457+ const recentBlockhash = bs58 . encode (
458+ guardedSplice ( byteArray , 0 , PUBLIC_KEY_LENGTH ) ,
459+ ) ;
457460
458461 const instructionCount = shortvec . decodeLength ( byteArray ) ;
459462 const compiledInstructions : MessageCompiledInstruction [ ] = [ ] ;
460463 for ( let i = 0 ; i < instructionCount ; i ++ ) {
461- const programIdIndex = byteArray . shift ( ) as number ;
464+ const programIdIndex = guardedShift ( byteArray ) ;
462465 const accountKeyIndexesLength = shortvec . decodeLength ( byteArray ) ;
463- const accountKeyIndexes = byteArray . splice ( 0 , accountKeyIndexesLength ) ;
466+ const accountKeyIndexes = guardedSplice (
467+ byteArray ,
468+ 0 ,
469+ accountKeyIndexesLength ,
470+ ) ;
464471 const dataLength = shortvec . decodeLength ( byteArray ) ;
465- const data = new Uint8Array ( byteArray . splice ( 0 , dataLength ) ) ;
472+ const data = new Uint8Array ( guardedSplice ( byteArray , 0 , dataLength ) ) ;
466473 compiledInstructions . push ( {
467474 programIdIndex,
468475 accountKeyIndexes,
@@ -473,11 +480,21 @@ export class MessageV0 {
473480 const addressTableLookupsCount = shortvec . decodeLength ( byteArray ) ;
474481 const addressTableLookups : MessageAddressTableLookup [ ] = [ ] ;
475482 for ( let i = 0 ; i < addressTableLookupsCount ; i ++ ) {
476- const accountKey = new PublicKey ( byteArray . splice ( 0 , PUBLIC_KEY_LENGTH ) ) ;
483+ const accountKey = new PublicKey (
484+ guardedSplice ( byteArray , 0 , PUBLIC_KEY_LENGTH ) ,
485+ ) ;
477486 const writableIndexesLength = shortvec . decodeLength ( byteArray ) ;
478- const writableIndexes = byteArray . splice ( 0 , writableIndexesLength ) ;
487+ const writableIndexes = guardedSplice (
488+ byteArray ,
489+ 0 ,
490+ writableIndexesLength ,
491+ ) ;
479492 const readonlyIndexesLength = shortvec . decodeLength ( byteArray ) ;
480- const readonlyIndexes = byteArray . splice ( 0 , readonlyIndexesLength ) ;
493+ const readonlyIndexes = guardedSplice (
494+ byteArray ,
495+ 0 ,
496+ readonlyIndexesLength ,
497+ ) ;
481498 addressTableLookups . push ( {
482499 accountKey,
483500 writableIndexes,
0 commit comments