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

Commit c00adbe

Browse files
Add capability to freeze token Accounts (#297)
* Add freeze elements to state structs * Add can_freeze paramter to InitializeMint instruction, and plumb * Add fixed_supply parameter to InitializeMint * Convert to SetAuthority instruction * Add frozen restrictions * Add FreezeAccount instruction * Make FreezeAccount into toggle * Add parameter to make freezing explicit * Pass mint owner, freeze_authority as parameters * Rename ToggleFreeze * Make AuthorityType more explicit * Rename mint owner => mint_authority * Add Account::is_frozen method * C header updates * s/is_initialized/state for Account * Minting nit * Add helpers to clean up AuthorityType packing * Split FreezeAccount into 2 instructions * Pass new_authority as parameter not account * More C header updates * Use new COption helpers * Use boolean for toggle fn
1 parent c2da3db commit c00adbe

File tree

5 files changed

+1294
-220
lines changed

5 files changed

+1294
-220
lines changed

token/program2/inc/token2.h

Lines changed: 157 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,85 @@
2121
*/
2222
#define Token_MIN_SIGNERS 1
2323

24+
/**
25+
* Account state.
26+
*/
27+
enum Token_AccountState
28+
#ifdef __cplusplus
29+
: uint8_t
30+
#endif // __cplusplus
31+
{
32+
/**
33+
* Account is not yet initialized
34+
*/
35+
Token_AccountState_Uninitialized,
36+
/**
37+
* Account is initialized; the account owner and/or delegate may perform permitted operations
38+
* on this account
39+
*/
40+
Token_AccountState_Initialized,
41+
/**
42+
* Account has been frozen by the mint freeze authority. Neither the account owner nor
43+
* the delegate are able to perform operations on this account.
44+
*/
45+
Token_AccountState_Frozen,
46+
};
47+
#ifndef __cplusplus
48+
typedef uint8_t Token_AccountState;
49+
#endif // __cplusplus
50+
51+
/**
52+
* Specifies the authority type for SetAuthority instructions
53+
*/
54+
enum Token_AuthorityType
55+
#ifdef __cplusplus
56+
: uint8_t
57+
#endif // __cplusplus
58+
{
59+
/**
60+
* Authority to mint new tokens
61+
*/
62+
Token_AuthorityType_MintTokens,
63+
/**
64+
* Authority to freeze any account associated with the Mint
65+
*/
66+
Token_AuthorityType_FreezeAccount,
67+
/**
68+
* Holder of a given token account
69+
*/
70+
Token_AuthorityType_AccountHolder,
71+
};
72+
#ifndef __cplusplus
73+
typedef uint8_t Token_AuthorityType;
74+
#endif // __cplusplus
75+
76+
typedef uint8_t Token_Pubkey[32];
77+
78+
/**
79+
* A C representation of Rust's `std::option::Option`
80+
*/
81+
typedef enum Token_COption_Pubkey_Tag {
82+
/**
83+
* No value
84+
*/
85+
Token_COption_Pubkey_None_Pubkey,
86+
/**
87+
* Some value `T`
88+
*/
89+
Token_COption_Pubkey_Some_Pubkey,
90+
} Token_COption_Pubkey_Tag;
91+
92+
typedef struct Token_COption_Pubkey_Token_Some_Body_Pubkey {
93+
Token_Pubkey _0;
94+
} Token_COption_Pubkey_Token_Some_Body_Pubkey;
95+
96+
typedef struct Token_COption_Pubkey {
97+
Token_COption_Pubkey_Tag tag;
98+
union {
99+
Token_COption_Pubkey_Token_Some_Body_Pubkey some;
100+
};
101+
} Token_COption_Pubkey;
102+
24103
/**
25104
* Instructions supported by the token program.
26105
*/
@@ -35,11 +114,7 @@ typedef enum Token_TokenInstruction_Tag {
35114
* Accounts expected by this instruction:
36115
*
37116
* 0. `[writable]` The mint to initialize.
38-
* 1.
39-
* * If supply is non-zero: `[writable]` The account to hold all the newly minted tokens.
40-
* * If supply is zero: `[]` The owner/multisignature of the mint.
41-
* 2. `[]` (optional) The owner/multisignature of the mint if supply is non-zero, if
42-
* present then further minting is supported.
117+
* 1. If supply is non-zero: `[writable]` The account to hold all the newly minted tokens.
43118
*
44119
*/
45120
Token_TokenInstruction_InitializeMint,
@@ -127,36 +202,34 @@ typedef enum Token_TokenInstruction_Tag {
127202
*/
128203
Token_TokenInstruction_Revoke,
129204
/**
130-
* Sets a new owner of a mint or account.
205+
* Sets a new authority of a mint or account.
131206
*
132207
* Accounts expected by this instruction:
133208
*
134-
* * Single owner
135-
* 0. `[writable]` The mint or account to change the owner of.
136-
* 1. `[]` The new owner/delegate/multisignature.
137-
* 2. `[signer]` The owner of the mint or account.
209+
* * Single authority
210+
* 0. `[writable]` The mint or account to change the authority of.
211+
* 1. `[signer]` The current authority of the mint or account.
138212
*
139-
* * Multisignature owner
140-
* 0. `[writable]` The mint or account to change the owner of.
141-
* 1. `[]` The new owner/delegate/multisignature.
142-
* 2. `[]` The mint's or account's multisignature owner.
213+
* * Multisignature authority
214+
* 0. `[writable]` The mint or account to change the authority of.
215+
* 1. `[]` The mint's or account's multisignature authority.
143216
* 3. ..3+M '[signer]' M signer accounts
144217
*/
145-
Token_TokenInstruction_SetOwner,
218+
Token_TokenInstruction_SetAuthority,
146219
/**
147220
* Mints new tokens to an account. The native mint does not support minting.
148221
*
149222
* Accounts expected by this instruction:
150223
*
151-
* * Single owner
224+
* * Single authority
152225
* 0. `[writable]` The mint.
153226
* 1. `[writable]` The account to mint tokens to.
154-
* 2. `[signer]` The mint's owner.
227+
* 2. `[signer]` The mint's minting authority.
155228
*
156-
* * Multisignature owner
229+
* * Multisignature authority
157230
* 0. `[writable]` The mint.
158231
* 1. `[writable]` The account to mint tokens to.
159-
* 2. `[]` The mint's multisignature owner.
232+
* 2. `[]` The mint's multisignature mint-tokens authority.
160233
* 3. ..3+M '[signer]' M signer accounts.
161234
*/
162235
Token_TokenInstruction_MintTo,
@@ -194,6 +267,40 @@ typedef enum Token_TokenInstruction_Tag {
194267
* 3. ..3+M '[signer]' M signer accounts.
195268
*/
196269
Token_TokenInstruction_CloseAccount,
270+
/**
271+
* Freeze an Initialized account using the Mint's freeze_authority (if set).
272+
*
273+
* Accounts expected by this instruction:
274+
*
275+
* * Single owner
276+
* 0. `[writable]` The account to freeze.
277+
* 1. '[]' The token mint.
278+
* 2. `[signer]` The mint freeze authority.
279+
*
280+
* * Multisignature owner
281+
* 0. `[writable]` The account to freeze.
282+
* 1. '[]' The token mint.
283+
* 2. `[]` The mint's multisignature freeze authority.
284+
* 3. ..3+M '[signer]' M signer accounts.
285+
*/
286+
Token_TokenInstruction_FreezeAccount,
287+
/**
288+
* Thaw a Frozen account using the Mint's freeze_authority (if set).
289+
*
290+
* Accounts expected by this instruction:
291+
*
292+
* * Single owner
293+
* 0. `[writable]` The account to freeze.
294+
* 1. '[]' The token mint.
295+
* 2. `[signer]` The mint freeze authority.
296+
*
297+
* * Multisignature owner
298+
* 0. `[writable]` The account to freeze.
299+
* 1. '[]' The token mint.
300+
* 2. `[]` The mint's multisignature freeze authority.
301+
* 3. ..3+M '[signer]' M signer accounts.
302+
*/
303+
Token_TokenInstruction_ThawAccount,
197304
} Token_TokenInstruction_Tag;
198305

199306
typedef struct Token_TokenInstruction_Token_InitializeMint_Body {
@@ -205,6 +312,15 @@ typedef struct Token_TokenInstruction_Token_InitializeMint_Body {
205312
* Number of base 10 digits to the right of the decimal place.
206313
*/
207314
uint8_t decimals;
315+
/**
316+
* The authority/multisignature to mint tokens, if supply is non-zero. If present,
317+
* further minting is supported.
318+
*/
319+
Token_COption_Pubkey mint_authority;
320+
/**
321+
* The freeze authority/multisignature of the mint.
322+
*/
323+
Token_COption_Pubkey freeze_authority;
208324
} Token_TokenInstruction_Token_InitializeMint_Body;
209325

210326
typedef struct Token_TokenInstruction_Token_InitializeMultisig_Body {
@@ -228,6 +344,17 @@ typedef struct Token_TokenInstruction_Token_Approve_Body {
228344
uint64_t amount;
229345
} Token_TokenInstruction_Token_Approve_Body;
230346

347+
typedef struct Token_TokenInstruction_Token_SetAuthority_Body {
348+
/**
349+
* The type of authority to update.
350+
*/
351+
Token_AuthorityType authority_type;
352+
/**
353+
* The new authority
354+
*/
355+
Token_COption_Pubkey new_authority;
356+
} Token_TokenInstruction_Token_SetAuthority_Body;
357+
231358
typedef struct Token_TokenInstruction_Token_MintTo_Body {
232359
/**
233360
* The amount of new tokens to mint.
@@ -249,48 +376,22 @@ typedef struct Token_TokenInstruction {
249376
Token_TokenInstruction_Token_InitializeMultisig_Body initialize_multisig;
250377
Token_TokenInstruction_Token_Transfer_Body transfer;
251378
Token_TokenInstruction_Token_Approve_Body approve;
379+
Token_TokenInstruction_Token_SetAuthority_Body set_authority;
252380
Token_TokenInstruction_Token_MintTo_Body mint_to;
253381
Token_TokenInstruction_Token_Burn_Body burn;
254382
};
255383
} Token_TokenInstruction;
256384

257-
typedef uint8_t Token_Pubkey[32];
258-
259-
/**
260-
* A C representation of Rust's `std::option::Option`
261-
*/
262-
typedef enum Token_COption_Pubkey_Tag {
263-
/**
264-
* No value
265-
*/
266-
Token_COption_Pubkey_None_Pubkey,
267-
/**
268-
* Some value `T`
269-
*/
270-
Token_COption_Pubkey_Some_Pubkey,
271-
} Token_COption_Pubkey_Tag;
272-
273-
typedef struct Token_COption_Pubkey_Token_Some_Body_Pubkey {
274-
Token_Pubkey _0;
275-
} Token_COption_Pubkey_Token_Some_Body_Pubkey;
276-
277-
typedef struct Token_COption_Pubkey {
278-
Token_COption_Pubkey_Tag tag;
279-
union {
280-
Token_COption_Pubkey_Token_Some_Body_Pubkey some;
281-
};
282-
} Token_COption_Pubkey;
283-
284385
/**
285386
* Mint data.
286387
*/
287388
typedef struct Token_Mint {
288389
/**
289-
* Optional owner, used to mint new tokens. The owner may only
290-
* be provided during mint creation. If no owner is present then the mint
291-
* has a fixed supply and no further tokens may be minted.
390+
* Optional authority used to mint new tokens. The mint authority may only be provided during
391+
* mint creation. If no mint authority is present then the mint has a fixed supply and no
392+
* further tokens may be minted.
292393
*/
293-
Token_COption_Pubkey owner;
394+
Token_COption_Pubkey mint_authority;
294395
/**
295396
* Number of base 10 digits to the right of the decimal place.
296397
*/
@@ -299,6 +400,10 @@ typedef struct Token_Mint {
299400
* Is `true` if this structure has been initialized
300401
*/
301402
bool is_initialized;
403+
/**
404+
* Optional authority to freeze token accounts.
405+
*/
406+
Token_COption_Pubkey freeze_authority;
302407
} Token_Mint;
303408

304409
/**
@@ -323,9 +428,9 @@ typedef struct Token_Account {
323428
*/
324429
Token_COption_Pubkey delegate;
325430
/**
326-
* Is `true` if this structure has been initialized
431+
* The account's state
327432
*/
328-
bool is_initialized;
433+
Token_AccountState state;
329434
/**
330435
* Is this a native token
331436
*/

token/program2/src/error.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,21 @@ pub enum TokenError {
4343
/// Invalid instruction
4444
#[error("Invalid instruction")]
4545
InvalidInstruction,
46+
/// State is invalid for requested operation.
47+
#[error("State is invalid for requested operation")]
48+
InvalidState,
4649
/// Operation overflowed
4750
#[error("Operation overflowed")]
4851
Overflow,
52+
/// Account does not support specified authority type.
53+
#[error("Account does not support specified authority type")]
54+
AuthorityTypeNotSupported,
55+
/// This token mint cannot freeze accounts.
56+
#[error("This token mint cannot freeze accounts")]
57+
MintCannotFreeze,
58+
/// Account is frozen; all account operations will fail
59+
#[error("Account is frozen")]
60+
AccountFrozen,
4961
}
5062
impl From<TokenError> for ProgramError {
5163
fn from(e: TokenError) -> Self {

0 commit comments

Comments
 (0)