From ddeeb790a8471d67b1cb02e556be2f8862c1f0da Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Sun, 14 Aug 2022 17:19:06 +0100 Subject: [PATCH] chore: add constant for pubkey byte length (#27134) --- web3.js/src/message/legacy.ts | 12 +++++------- web3.js/src/publickey.ts | 9 +++++++-- web3.js/src/validator-info.ts | 8 +++----- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/web3.js/src/message/legacy.ts b/web3.js/src/message/legacy.ts index 9d5ff3f3f0fbe3..ff6ea9a0005233 100644 --- a/web3.js/src/message/legacy.ts +++ b/web3.js/src/message/legacy.ts @@ -2,7 +2,7 @@ import bs58 from 'bs58'; import {Buffer} from 'buffer'; import * as BufferLayout from '@solana/buffer-layout'; -import {PublicKey} from '../publickey'; +import {PublicKey, PUBLIC_KEY_LENGTH} from '../publickey'; import type {Blockhash} from '../blockhash'; import * as Layout from '../layout'; import {PACKET_DATA_SIZE} from '../transaction/constants'; @@ -24,8 +24,6 @@ export type MessageArgs = { instructions: CompiledInstruction[]; }; -const PUBKEY_LENGTH = 32; - /** * List of instructions to be processed atomically */ @@ -199,13 +197,13 @@ export class Message { const accountCount = shortvec.decodeLength(byteArray); let accountKeys = []; for (let i = 0; i < accountCount; i++) { - const account = byteArray.slice(0, PUBKEY_LENGTH); - byteArray = byteArray.slice(PUBKEY_LENGTH); + const account = byteArray.slice(0, PUBLIC_KEY_LENGTH); + byteArray = byteArray.slice(PUBLIC_KEY_LENGTH); accountKeys.push(bs58.encode(Buffer.from(account))); } - const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH); - byteArray = byteArray.slice(PUBKEY_LENGTH); + const recentBlockhash = byteArray.slice(0, PUBLIC_KEY_LENGTH); + byteArray = byteArray.slice(PUBLIC_KEY_LENGTH); const instructionCount = shortvec.decodeLength(byteArray); let instructions: CompiledInstruction[] = []; diff --git a/web3.js/src/publickey.ts b/web3.js/src/publickey.ts index 877d2103db283d..b50ebd1832a3b0 100644 --- a/web3.js/src/publickey.ts +++ b/web3.js/src/publickey.ts @@ -12,6 +12,11 @@ import {toBuffer} from './utils/to-buffer'; */ export const MAX_SEED_LENGTH = 32; +/** + * Size of public key in bytes + */ +export const PUBLIC_KEY_LENGTH = 32; + /** * Value to be converted into public key */ @@ -54,7 +59,7 @@ export class PublicKey extends Struct { if (typeof value === 'string') { // assume base 58 encoding by default const decoded = bs58.decode(value); - if (decoded.length != 32) { + if (decoded.length != PUBLIC_KEY_LENGTH) { throw new Error(`Invalid public key input`); } this._bn = new BN(decoded); @@ -103,7 +108,7 @@ export class PublicKey extends Struct { */ toBuffer(): Buffer { const b = this._bn.toArrayLike(Buffer); - if (b.length === 32) { + if (b.length === PUBLIC_KEY_LENGTH) { return b; } diff --git a/web3.js/src/validator-info.ts b/web3.js/src/validator-info.ts index efaa4ee3216511..7a1e0a868d5712 100644 --- a/web3.js/src/validator-info.ts +++ b/web3.js/src/validator-info.ts @@ -8,7 +8,7 @@ import { import * as Layout from './layout'; import * as shortvec from './utils/shortvec-encoding'; -import {PublicKey} from './publickey'; +import {PublicKey, PUBLIC_KEY_LENGTH} from './publickey'; export const VALIDATOR_INFO_KEY = new PublicKey( 'Va1idator1nfo111111111111111111111111111111', @@ -77,16 +77,14 @@ export class ValidatorInfo { static fromConfigData( buffer: Buffer | Uint8Array | Array, ): ValidatorInfo | null { - const PUBKEY_LENGTH = 32; - let byteArray = [...buffer]; const configKeyCount = shortvec.decodeLength(byteArray); if (configKeyCount !== 2) return null; const configKeys: Array = []; for (let i = 0; i < 2; i++) { - const publicKey = new PublicKey(byteArray.slice(0, PUBKEY_LENGTH)); - byteArray = byteArray.slice(PUBKEY_LENGTH); + 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); configKeys.push({publicKey, isSigner});