From 650707b551f8b583346e0af990635645a1dc0b85 Mon Sep 17 00:00:00 2001 From: Steven Luscher Date: Wed, 13 Mar 2024 14:35:57 -0700 Subject: [PATCH] fix: splice bytes in-place when reading buffers (#2315) --- packages/library-legacy/src/message/legacy.ts | 12 ++++-------- packages/library-legacy/src/transaction/legacy.ts | 3 +-- packages/library-legacy/src/validator-info.ts | 6 ++---- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/packages/library-legacy/src/message/legacy.ts b/packages/library-legacy/src/message/legacy.ts index 98b9500354fc..587c6df5d15b 100644 --- a/packages/library-legacy/src/message/legacy.ts +++ b/packages/library-legacy/src/message/legacy.ts @@ -284,25 +284,21 @@ export class Message { const accountCount = shortvec.decodeLength(byteArray); let accountKeys = []; for (let i = 0; i < accountCount; i++) { - const account = byteArray.slice(0, PUBLIC_KEY_LENGTH); - byteArray = byteArray.slice(PUBLIC_KEY_LENGTH); + const account = byteArray.splice(0, PUBLIC_KEY_LENGTH); accountKeys.push(new PublicKey(Buffer.from(account))); } - const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH); - byteArray = byteArray.slice(PUBLIC_KEY_LENGTH); + const recentBlockhash = byteArray.splice(0, PUBLIC_KEY_LENGTH); const instructionCount = shortvec.decodeLength(byteArray); let instructions: CompiledInstruction[] = []; for (let i = 0; i < instructionCount; i++) { const programIdIndex = byteArray.shift()!; const accountCount = shortvec.decodeLength(byteArray); - const accounts = byteArray.slice(0, accountCount); - byteArray = byteArray.slice(accountCount); + const accounts = byteArray.splice(0, accountCount); const dataLength = shortvec.decodeLength(byteArray); - const dataSlice = byteArray.slice(0, dataLength); + const dataSlice = byteArray.splice(0, dataLength); const data = bs58.encode(Buffer.from(dataSlice)); - byteArray = byteArray.slice(dataLength); instructions.push({ programIdIndex, accounts, diff --git a/packages/library-legacy/src/transaction/legacy.ts b/packages/library-legacy/src/transaction/legacy.ts index 8013c1e74255..ee11644cd8c3 100644 --- a/packages/library-legacy/src/transaction/legacy.ts +++ b/packages/library-legacy/src/transaction/legacy.ts @@ -904,8 +904,7 @@ export class Transaction { const signatureCount = shortvec.decodeLength(byteArray); let signatures = []; for (let i = 0; i < signatureCount; i++) { - const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES); - byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES); + const signature = byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES); signatures.push(bs58.encode(Buffer.from(signature))); } diff --git a/packages/library-legacy/src/validator-info.ts b/packages/library-legacy/src/validator-info.ts index 7a1e0a868d57..2412b0a75e42 100644 --- a/packages/library-legacy/src/validator-info.ts +++ b/packages/library-legacy/src/validator-info.ts @@ -83,10 +83,8 @@ export class ValidatorInfo { const configKeys: Array = []; for (let i = 0; i < 2; i++) { - const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH)); - byteArray = byteArray.slice(PUBLIC_KEY_LENGTH); - const isSigner = byteArray.slice(0, 1)[0] === 1; - byteArray = byteArray.slice(1); + const publicKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH)); + const isSigner = byteArray.splice(0, 1)[0] === 1; configKeys.push({publicKey, isSigner}); }