Skip to main content

dlp_api/args/
delegate_with_actions.rs

1use borsh::{BorshDeserialize, BorshSerialize};
2
3use super::DelegateArgs;
4use crate::compact;
5
6#[derive(Debug, BorshSerialize, BorshDeserialize)]
7pub struct DelegateWithActionsArgs {
8    /// Standard delegation parameters.
9    pub delegate: DelegateArgs,
10
11    /// Compact post-delegation actions.
12    pub actions: PostDelegationActions,
13}
14
15///
16/// This struct is used both as instruction args and as persisted state: it is received as
17/// instruction data and then stored in the delegation-record account immediately after
18/// DelegationRecord.
19///
20/// In the basic form, PostDelegationActions is constructed from Vec<Instruction>. In that case,
21/// both inserted_signers and inserted_non_signers are zero, and the pubkey indices used by
22/// compact::AccountMeta are computed from this imagined pubkey list:
23///
24///   [signers.., non_signers..]
25///
26/// That is, the first non-signer pubkey index is signers.len().
27///
28/// In the advanced form, an existing PostDelegationActions value (usually provided by an off-chain
29/// client) is combined with Vec<Instruction> (usually constructed on-chain) to produce a merged
30/// PostDelegationActions via ClearTextWithInsertable::cleartext_with_insertable, which allows the
31/// existing actions to be inserted at a specific instruction index. In this case, both inserted_signers
32/// and inserted_non_signers may be non-zero, and the imagined pubkey list takes this form:
33///
34///   [old_signers.., old_non_signers.., new_signers.., new_non_signers..]
35///
36/// That is, the first "new signer" comes after the old keys (old_signers + old_non_signers). Also,
37/// the `signers` field is created from:
38///
39///  [old_signers.., new_signers..]
40///
41/// and `non-signers` field is created from:
42///
43///  [old_non_signers.., new_non_signers..]
44///
45///
46#[derive(Debug, BorshSerialize, BorshDeserialize)]
47pub struct PostDelegationActions {
48    pub inserted_signers: u8,
49
50    pub inserted_non_signers: u8,
51
52    pub signers: Vec<[u8; 32]>,
53
54    pub non_signers: Vec<MaybeEncryptedPubkey>,
55
56    pub instructions: Vec<MaybeEncryptedInstruction>,
57}
58
59#[derive(Clone, Debug, BorshSerialize, BorshDeserialize)]
60pub struct MaybeEncryptedInstruction {
61    pub program_id: u8,
62
63    pub accounts: Vec<MaybeEncryptedAccountMeta>,
64
65    pub data: MaybeEncryptedIxData,
66}
67
68#[derive(Clone, Debug, BorshSerialize, BorshDeserialize)]
69#[cfg_attr(test, derive(PartialEq))]
70pub enum MaybeEncryptedPubkey {
71    ClearText([u8; 32]),
72    Encrypted(EncryptedBuffer),
73}
74
75impl From<[u8; 32]> for MaybeEncryptedPubkey {
76    fn from(pubkey: [u8; 32]) -> Self {
77        Self::ClearText(pubkey)
78    }
79}
80
81impl From<Vec<u8>> for MaybeEncryptedPubkey {
82    fn from(bytes: Vec<u8>) -> Self {
83        Self::Encrypted(bytes.into())
84    }
85}
86
87#[derive(Clone, Debug, BorshSerialize, BorshDeserialize)]
88pub enum MaybeEncryptedAccountMeta {
89    ClearText(compact::AccountMeta),
90    Encrypted(EncryptedBuffer),
91}
92
93impl From<compact::AccountMeta> for MaybeEncryptedAccountMeta {
94    fn from(account_meta: compact::AccountMeta) -> Self {
95        Self::ClearText(account_meta)
96    }
97}
98
99impl From<Vec<u8>> for MaybeEncryptedAccountMeta {
100    fn from(bytes: Vec<u8>) -> Self {
101        Self::Encrypted(bytes.into())
102    }
103}
104
105#[derive(Clone, Debug, BorshSerialize, BorshDeserialize)]
106pub struct MaybeEncryptedIxData {
107    pub prefix: Vec<u8>,
108    pub suffix: EncryptedBuffer,
109}
110
111#[derive(Clone, Debug, Default, BorshSerialize, BorshDeserialize)]
112#[cfg_attr(test, derive(PartialEq))]
113pub struct EncryptedBuffer(Vec<u8>);
114
115impl EncryptedBuffer {
116    pub fn new(bytes: Vec<u8>) -> Self {
117        Self(bytes)
118    }
119
120    pub fn as_bytes(&self) -> &[u8] {
121        &self.0
122    }
123
124    pub fn into_inner(self) -> Vec<u8> {
125        self.0
126    }
127}
128
129impl From<Vec<u8>> for EncryptedBuffer {
130    fn from(bytes: Vec<u8>) -> Self {
131        Self(bytes)
132    }
133}