defuse_core/intents/
account.rs

1use std::borrow::Cow;
2
3use defuse_crypto::PublicKey;
4use near_sdk::{AccountIdRef, CryptoHash, near};
5
6use crate::{
7    Result,
8    accounts::{AccountEvent, PublicKeyEvent},
9    engine::{Engine, Inspector, State},
10};
11
12use super::ExecutableIntent;
13
14#[near(serializers = [borsh, json])]
15#[derive(Debug, Clone)]
16/// Given an account id, the user can add public keys. The added public keys can sign
17/// intents on behalf of these accounts, even to add new ones.
18/// Warning: Implicit account ids, by default, have their corresponding public keys added.
19/// Meaning: For a leaked private key, whose implicit account id had been used in intents,
20/// the user must manually rotate the underlying public key within intents, too.
21pub struct AddPublicKey {
22    pub public_key: PublicKey,
23}
24
25impl ExecutableIntent for AddPublicKey {
26    #[inline]
27    fn execute_intent<S, I>(
28        self,
29        signer_id: &AccountIdRef,
30        engine: &mut Engine<S, I>,
31        _intent_hash: CryptoHash,
32    ) -> Result<()>
33    where
34        S: State,
35        I: Inspector,
36    {
37        engine
38            .state
39            .add_public_key(signer_id.to_owned(), self.public_key)?;
40
41        engine
42            .inspector
43            .on_event(crate::events::DefuseEvent::PublicKeyAdded(
44                AccountEvent::new(
45                    Cow::Borrowed(signer_id),
46                    PublicKeyEvent {
47                        public_key: Cow::Borrowed(&self.public_key),
48                    },
49                ),
50            ));
51        Ok(())
52    }
53}
54
55#[near(serializers = [borsh, json])]
56#[derive(Debug, Clone)]
57/// Remove the public key associated with a given account. See `AddPublicKey`.
58pub struct RemovePublicKey {
59    pub public_key: PublicKey,
60}
61
62impl ExecutableIntent for RemovePublicKey {
63    #[inline]
64    fn execute_intent<S, I>(
65        self,
66        signer_id: &AccountIdRef,
67        engine: &mut Engine<S, I>,
68        _intent_hash: CryptoHash,
69    ) -> crate::Result<()>
70    where
71        S: State,
72        I: Inspector,
73    {
74        engine
75            .state
76            .remove_public_key(signer_id.to_owned(), self.public_key)?;
77        engine
78            .inspector
79            .on_event(crate::events::DefuseEvent::PublicKeyRemoved(
80                AccountEvent::new(
81                    Cow::Borrowed(signer_id),
82                    PublicKeyEvent {
83                        public_key: Cow::Borrowed(&self.public_key),
84                    },
85                ),
86            ));
87        Ok(())
88    }
89}
90
91#[near(serializers = [borsh, json])]
92#[derive(Debug, Clone)]
93pub struct SetAuthByPredecessorId {
94    pub enabled: bool,
95}
96
97impl ExecutableIntent for SetAuthByPredecessorId {
98    fn execute_intent<S, I>(
99        self,
100        signer_id: &AccountIdRef,
101        engine: &mut Engine<S, I>,
102        _intent_hash: CryptoHash,
103    ) -> Result<()>
104    where
105        S: State,
106        I: Inspector,
107    {
108        engine
109            .state
110            .set_auth_by_predecessor_id(signer_id.to_owned(), self.enabled)?;
111
112        engine
113            .inspector
114            .on_event(AccountEvent::new(signer_id, self).into());
115
116        Ok(())
117    }
118}