2021-09-17 11:36:57 -07:00
|
|
|
//! @brief Solana precompiled programs
|
|
|
|
|
|
|
|
#![cfg(feature = "full")]
|
|
|
|
|
|
|
|
use {
|
|
|
|
crate::{
|
|
|
|
decode_error::DecodeError,
|
2021-09-28 23:25:08 -07:00
|
|
|
feature_set::{
|
|
|
|
ed25519_program_enabled, prevent_calling_precompiles_as_programs, FeatureSet,
|
|
|
|
},
|
2021-09-17 11:36:57 -07:00
|
|
|
instruction::CompiledInstruction,
|
|
|
|
pubkey::Pubkey,
|
|
|
|
},
|
|
|
|
lazy_static::lazy_static,
|
|
|
|
std::sync::Arc,
|
|
|
|
thiserror::Error,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Precompile errors
|
|
|
|
#[derive(Error, Debug, Clone, PartialEq)]
|
|
|
|
pub enum PrecompileError {
|
|
|
|
#[error("public key is not valid")]
|
|
|
|
InvalidPublicKey,
|
|
|
|
#[error("id is not valid")]
|
|
|
|
InvalidRecoveryId,
|
|
|
|
#[error("signature is not valid")]
|
|
|
|
InvalidSignature,
|
|
|
|
#[error("offset not valid")]
|
|
|
|
InvalidDataOffsets,
|
|
|
|
#[error("instruction is incorrect size")]
|
|
|
|
InvalidInstructionDataSize,
|
|
|
|
}
|
|
|
|
impl<T> DecodeError<T> for PrecompileError {
|
|
|
|
fn type_of() -> &'static str {
|
|
|
|
"PrecompileError"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// All precompiled programs must implement the `Verify` function
|
|
|
|
pub type Verify = fn(&[u8], &[&[u8]], &Arc<FeatureSet>) -> std::result::Result<(), PrecompileError>;
|
|
|
|
|
|
|
|
/// Information on a precompiled program
|
2021-09-28 23:25:08 -07:00
|
|
|
pub struct Precompile {
|
2021-09-17 11:36:57 -07:00
|
|
|
/// Program id
|
|
|
|
pub program_id: Pubkey,
|
|
|
|
/// Feature to enable on, `None` indicates always enabled
|
|
|
|
pub feature: Option<Pubkey>,
|
|
|
|
/// Verification function
|
|
|
|
pub verify_fn: Verify,
|
|
|
|
}
|
|
|
|
impl Precompile {
|
|
|
|
/// Creates a new `Precompile`
|
|
|
|
pub fn new(program_id: Pubkey, feature: Option<Pubkey>, verify_fn: Verify) -> Self {
|
|
|
|
Precompile {
|
|
|
|
program_id,
|
|
|
|
feature,
|
|
|
|
verify_fn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Check if a program id is this precompiled program
|
2021-09-28 23:25:08 -07:00
|
|
|
pub fn check_id<F>(&self, program_id: &Pubkey, is_enabled: F) -> bool
|
|
|
|
where
|
|
|
|
F: Fn(&Pubkey) -> bool,
|
|
|
|
{
|
|
|
|
self.feature
|
|
|
|
.map_or(true, |ref feature_id| is_enabled(feature_id))
|
|
|
|
&& self.program_id == *program_id
|
2021-09-17 11:36:57 -07:00
|
|
|
}
|
|
|
|
/// Verify this precompiled program
|
|
|
|
pub fn verify(
|
|
|
|
&self,
|
|
|
|
data: &[u8],
|
|
|
|
instruction_datas: &[&[u8]],
|
|
|
|
feature_set: &Arc<FeatureSet>,
|
|
|
|
) -> std::result::Result<(), PrecompileError> {
|
|
|
|
(self.verify_fn)(data, instruction_datas, feature_set)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
/// The list of all precompiled programs
|
|
|
|
static ref PRECOMPILES: Vec<Precompile> = vec![
|
|
|
|
Precompile::new(
|
|
|
|
crate::secp256k1_program::id(),
|
2021-09-28 23:25:08 -07:00
|
|
|
Some(prevent_calling_precompiles_as_programs::id()),
|
2021-09-17 11:36:57 -07:00
|
|
|
crate::secp256k1_instruction::verify,
|
|
|
|
),
|
|
|
|
Precompile::new(
|
|
|
|
crate::ed25519_program::id(),
|
|
|
|
Some(ed25519_program_enabled::id()),
|
|
|
|
crate::ed25519_instruction::verify,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if a program is a precompiled program
|
2021-09-28 23:25:08 -07:00
|
|
|
pub fn is_precompile<F>(program_id: &Pubkey, is_enabled: F) -> bool
|
|
|
|
where
|
|
|
|
F: Fn(&Pubkey) -> bool,
|
|
|
|
{
|
2021-09-17 11:36:57 -07:00
|
|
|
PRECOMPILES
|
|
|
|
.iter()
|
2021-09-28 23:25:08 -07:00
|
|
|
.any(|precompile| precompile.check_id(program_id, |feature_id| is_enabled(feature_id)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_precompiles<'a>() -> &'a [Precompile] {
|
|
|
|
&PRECOMPILES
|
2021-09-17 11:36:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check that a program is precompiled and if so verify it
|
|
|
|
pub fn verify_if_precompile(
|
|
|
|
program_id: &Pubkey,
|
|
|
|
precompile_instruction: &CompiledInstruction,
|
|
|
|
all_instructions: &[CompiledInstruction],
|
|
|
|
feature_set: &Arc<FeatureSet>,
|
|
|
|
) -> Result<(), PrecompileError> {
|
|
|
|
for precompile in PRECOMPILES.iter() {
|
2021-09-28 23:25:08 -07:00
|
|
|
if precompile.check_id(program_id, |feature_id| feature_set.is_active(feature_id)) {
|
2021-09-17 11:36:57 -07:00
|
|
|
let instruction_datas: Vec<_> = all_instructions
|
|
|
|
.iter()
|
|
|
|
.map(|instruction| instruction.data.as_ref())
|
|
|
|
.collect();
|
|
|
|
return precompile.verify(
|
|
|
|
&precompile_instruction.data,
|
|
|
|
&instruction_datas,
|
|
|
|
feature_set,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|