Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit fd479ff

Browse files
authored
TLV: Add const as_slice() function for easier matching (#4547)
1 parent b4072ec commit fd479ff

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

libraries/type-length-value/src/discriminator.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ impl Discriminator {
2828
pub const fn new(value: [u8; Self::LENGTH]) -> Self {
2929
Self(value)
3030
}
31+
/// Get the array as a const slice
32+
pub const fn as_slice(&self) -> &[u8] {
33+
self.0.as_slice()
34+
}
3135
}
3236
impl AsRef<[u8]> for Discriminator {
3337
fn as_ref(&self) -> &[u8] {

token/transfer-hook-interface/src/instruction.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,18 @@ pub enum TransferHookInstruction {
4848
/// by this type.
4949
pub struct ExecuteInstruction;
5050
impl TlvDiscriminator for ExecuteInstruction {
51-
/// Please use this discriminator in your program when matching
52-
const TLV_DISCRIMINATOR: Discriminator = Discriminator::new(EXECUTE_DISCRIMINATOR);
51+
/// First 8 bytes of `hash::hashv(&["spl-transfer-hook-interface:execute"])`
52+
const TLV_DISCRIMINATOR: Discriminator =
53+
Discriminator::new([105, 37, 101, 197, 75, 251, 102, 26]);
5354
}
54-
/// First 8 bytes of `hash::hashv(&["spl-transfer-hook-interface:execute"])`
55-
const EXECUTE_DISCRIMINATOR: [u8; Discriminator::LENGTH] = [105, 37, 101, 197, 75, 251, 102, 26];
56-
// annoying, but needed to perform a match on the value
57-
const EXECUTE_DISCRIMINATOR_SLICE: &[u8] = &EXECUTE_DISCRIMINATOR;
55+
5856
/// First 8 bytes of `hash::hashv(&["spl-transfer-hook-interface:initialize-extra-account-metas"])`
5957
const INITIALIZE_EXTRA_ACCOUNT_METAS_DISCRIMINATOR: &[u8] = &[43, 34, 13, 49, 167, 88, 235, 235];
6058

6159
impl TransferHookInstruction {
6260
/// Unpacks a byte buffer into a [TransferHookInstruction](enum.TransferHookInstruction.html).
6361
pub fn unpack(input: &[u8]) -> Result<Self, ProgramError> {
62+
const EXECUTE_DISCRIMINATOR_SLICE: &[u8] = ExecuteInstruction::TLV_DISCRIMINATOR.as_slice();
6463
if input.len() < Discriminator::LENGTH {
6564
return Err(ProgramError::InvalidInstructionData);
6665
}
@@ -84,7 +83,7 @@ impl TransferHookInstruction {
8483
let mut buf = vec![];
8584
match self {
8685
Self::Execute { amount } => {
87-
buf.extend_from_slice(EXECUTE_DISCRIMINATOR_SLICE);
86+
buf.extend_from_slice(ExecuteInstruction::TLV_DISCRIMINATOR.as_slice());
8887
buf.extend_from_slice(&amount.to_le_bytes());
8988
}
9089
Self::InitializeExtraAccountMetas => {

0 commit comments

Comments
 (0)