chore: add constant for pubkey byte length (#27134)
This commit is contained in:
parent
981948980c
commit
a97346aec7
|
@ -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[] = [];
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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});
|
||||
|
|
Loading…
Reference in New Issue