3 min de lectura
Solana: Signer added when using has_one
CRYPTOCURRENCY
Here is a draft article based on your request:
Using the Has_one Tag in Solana: Hidden Additional Signer Requirement
In Solana, accounts can be configured with various constraints to ensure proper authorization and operation. One such constraint is has_one
, which allows developers to specify that an account must have exactly one subaccount.
However, when using has_one
with certain rules, such as admin', it is not immediately clear what happens in practice. A closer look reveals that adding a second signer requirement via
has_onecan sometimes lead to unexpected behavior, even if only the admin is declared as such.
In this article, we will explore the use ofhas_onewith specific rules in Solana and explore when an additional signer requirement can be added even if only the admin is declared.
Has_one Syntax
When you define an account with a constraint like "has_one", the syntax looks something like this:
#[account(
...
has_one(
child_account: Address,
child_signer: Address,
),
)]
Here we see that two new fields have been added to the account structure:
- child_account
: This is the account that must exist alongside the account being defined.
- child_signer
: This specifies that only the signer of this child account can sign it.
Administrator Signature Requirement
Now let's consider what happens when we use the administrator signature requirement (has_one(admin)) with a specific rule like
admin’:
#[account(
...
has_one(
admin: Address,
),
)]
In this scenario, an additional signer requirement is added to the account, even though only the administrator account is declared as such.
Catch: Only one child account is required
When using the `has_one'' rule with a specific rule like
admin'', it is important to note that the administrator does not need to sign the sub-account. Instead, the administrator needs to sign his own account, which acts as a child. This is why the additional signer requirement is added to the parent account.
Conclusion
In summary, when usinghas_one'' with certain rules, such as
admin'', it is important to understand that only one subaccount is required, and it is signed by the signer. Adding an extra signature requirement can sometimes lead to unexpected behavior, even if only the admin is one.
As a developer, it is important to carefully evaluate the limitations of each account definition in Solana to ensure proper authorization and behavior. By understanding these nuances, you can write more robust and efficient code that leverages the power of`has_one» while minimizing unnecessary signers.