Skip to content

Commit

Permalink
chore: add constant for pubkey byte length (#27134)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry authored Aug 14, 2022
1 parent 9819489 commit a97346a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
12 changes: 5 additions & 7 deletions web3.js/src/message/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -24,8 +24,6 @@ export type MessageArgs = {
instructions: CompiledInstruction[];
};

const PUBKEY_LENGTH = 32;

/**
* List of instructions to be processed atomically
*/
Expand Down Expand Up @@ -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[] = [];
Expand Down
9 changes: 7 additions & 2 deletions web3.js/src/publickey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 3 additions & 5 deletions web3.js/src/validator-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -77,16 +77,14 @@ export class ValidatorInfo {
static fromConfigData(
buffer: Buffer | Uint8Array | Array<number>,
): ValidatorInfo | null {
const PUBKEY_LENGTH = 32;

let byteArray = [...buffer];
const configKeyCount = shortvec.decodeLength(byteArray);
if (configKeyCount !== 2) return null;

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

0 comments on commit a97346a

Please sign in to comment.