Skip to content

Commit 650707b

Browse files
authored
fix: splice bytes in-place when reading buffers (#2315)
1 parent b645b4f commit 650707b

File tree

3 files changed

+7
-14
lines changed

3 files changed

+7
-14
lines changed

packages/library-legacy/src/message/legacy.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,25 +284,21 @@ export class Message {
284284
const accountCount = shortvec.decodeLength(byteArray);
285285
let accountKeys = [];
286286
for (let i = 0; i < accountCount; i++) {
287-
const account = byteArray.slice(0, PUBLIC_KEY_LENGTH);
288-
byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
287+
const account = byteArray.splice(0, PUBLIC_KEY_LENGTH);
289288
accountKeys.push(new PublicKey(Buffer.from(account)));
290289
}
291290

292-
const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH);
293-
byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
291+
const recentBlockhash = byteArray.splice(0, PUBLIC_KEY_LENGTH);
294292

295293
const instructionCount = shortvec.decodeLength(byteArray);
296294
let instructions: CompiledInstruction[] = [];
297295
for (let i = 0; i < instructionCount; i++) {
298296
const programIdIndex = byteArray.shift()!;
299297
const accountCount = shortvec.decodeLength(byteArray);
300-
const accounts = byteArray.slice(0, accountCount);
301-
byteArray = byteArray.slice(accountCount);
298+
const accounts = byteArray.splice(0, accountCount);
302299
const dataLength = shortvec.decodeLength(byteArray);
303-
const dataSlice = byteArray.slice(0, dataLength);
300+
const dataSlice = byteArray.splice(0, dataLength);
304301
const data = bs58.encode(Buffer.from(dataSlice));
305-
byteArray = byteArray.slice(dataLength);
306302
instructions.push({
307303
programIdIndex,
308304
accounts,

packages/library-legacy/src/transaction/legacy.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,8 +904,7 @@ export class Transaction {
904904
const signatureCount = shortvec.decodeLength(byteArray);
905905
let signatures = [];
906906
for (let i = 0; i < signatureCount; i++) {
907-
const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
908-
byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
907+
const signature = byteArray.splice(0, SIGNATURE_LENGTH_IN_BYTES);
909908
signatures.push(bs58.encode(Buffer.from(signature)));
910909
}
911910

packages/library-legacy/src/validator-info.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,8 @@ export class ValidatorInfo {
8383

8484
const configKeys: Array<ConfigKey> = [];
8585
for (let i = 0; i < 2; i++) {
86-
const publicKey = new PublicKey(byteArray.slice(0, PUBLIC_KEY_LENGTH));
87-
byteArray = byteArray.slice(PUBLIC_KEY_LENGTH);
88-
const isSigner = byteArray.slice(0, 1)[0] === 1;
89-
byteArray = byteArray.slice(1);
86+
const publicKey = new PublicKey(byteArray.splice(0, PUBLIC_KEY_LENGTH));
87+
const isSigner = byteArray.splice(0, 1)[0] === 1;
9088
configKeys.push({publicKey, isSigner});
9189
}
9290

0 commit comments

Comments
 (0)