chore: add constant for pubkey byte length (#27134)

This commit is contained in:
Justin Starry 2022-08-14 17:19:06 +01:00 committed by GitHub
parent 981948980c
commit a97346aec7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 14 deletions

View File

@ -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[] = [];

View File

@ -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;
}

View File

@ -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<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});