Skip to content

Commit

Permalink
fix: splice bytes in-place when reading buffers (#2315)
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher authored Mar 13, 2024
1 parent b645b4f commit 650707b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
12 changes: 4 additions & 8 deletions packages/library-legacy/src/message/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions packages/library-legacy/src/transaction/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand Down
6 changes: 2 additions & 4 deletions packages/library-legacy/src/validator-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ export class ValidatorInfo {

const configKeys: Array<ConfigKey> = [];
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});
}

Expand Down

0 comments on commit 650707b

Please sign in to comment.