Register FT4 accounts
In the FT4 library, accounts function as digital identities, enabling users to transfer assets, interact with dapps, and sign transactions. Account registration allows the system to securely identify and authenticate users, supporting access control mechanisms that let users or administrators define permissions for actions an account can perform. This approach is crucial for maintaining security, preventing unauthorized access, and ensuring that resources such as assets or data remain accessible only to the rightful owner.
Account registration framework
For most dapps, the account registration framework provided by the FT4 library is the recommended approach. This framework offers a flexible, secure, and scalable system for creating new accounts, supporting various strategies tailored to different business models and security requirements. The framework simplifies the developer experience, offering pre-built solutions for common registration scenarios.
Registration strategies
The account registration framework supports several strategies:
- Open: Anyone can call the
register_account()
operation to create an account without restrictions. - Transfer strategy: Users must perform a transfer to the account address before creating an account. The transfer
strategy includes three sub-strategies:
- Open: Users can claim the entire deposit to their newly created account.
- Fee: Part of the transferred assets is collected as a fee to the chain's fee account, allowing users to claim only the remaining assets.
- Subscription: Similar to the fee strategy, but users need to periodically renew their subscription by paying the subscription fee to maintain account access.
To enable a specific strategy, import the corresponding module into the Rell file and configure it in the chromia.yml
file. The modules are named as follows:
lib.ft4.core.accounts.strategies.open
(open strategy)lib.ft4.core.accounts.strategies.transfer.open
(transfer open strategy)lib.ft4.core.accounts.strategies.transfer.fee
(transfer fee strategy)lib.ft4.core.accounts.strategies.transfer.subscription
(transfer subscription strategy)
Transfer strategy
To use any of the transfer strategies, certain moduleArgs
must first be configured in the chromia.yml
file under the
key:
blockchains:
my_dapp_chain:
module: my_dapp_module
moduleArgs:
lib.ft4.core.accounts.strategies.transfer:
Under this key, settings must be specified, including which chains are permitted to make transfers to the chain and which assets are accepted. Configuring these parameters helps prevent potential DOS attacks and spam from untrusted chains. Here is an example configuration:
blockchains:
my_dapp_chain:
module: my_dapp_module
moduleArgs:
lib.ft4.core.accounts.strategies.transfer:
rules:
- sender_blockchain: # List of blockchain rids from which we will accept transfers
- x"08B02E0E14B634031FDF2ED3FD78E7410A5849CD28"
sender: * # Anyone on the specified blockchain can send us assets
recipient: * # They can send assets to anyone on this chain
asset: # List of assets that can be sent from this chain
- name: CHR # Name of the asset (id can also be used instead)
min_amount: 5L # If transfer is of less than this value, then the transfer will be rejected
timeout_days: 30 # After this many days, the sender is allowed to recall the transfer if it hasn't been claimed
strategy: # List of transfer strategies to enable
- "fee"
- "open"
In this example, every user on a specified Chromia blockchain is allowed to send a minimum of 0.000005 CHR to any user
on the receiving blockchain. If the transfer recipient does not have an existing account, they can create one using
either the open
or fee
strategy. This configuration enables granular control over which blockchains can send assets
and create accounts.
Transfer strategy configuration values
The transfer strategy uses special values to define flexible rules:
$
(Current Chain): Represents the current blockchain's BRID. Can be used insender_blockchain
,issuing_blockchain_rid
for assets.*
(Any Value): Represents any allowed value. Can be used insender_blockchain
,sender
,recipient
, and asset fields.X
(Same Value): Requires the sender and recipient to be the same account. Can only be used insender
andrecipient
fields together.
Example configurations
Allow transfers from any chain:
sender_blockchain:
- "*"
Allow transfers only from current chain:
sender_blockchain:
- "$"
Allow transfers from specific chains:
sender_blockchain:
- x"08B02E0E14B634031FDF2ED3FD78E7410A5849CD28"
- x"6403ccac0c67f7cb6af78e5e15b3aaebb2b42370f0d12e099ed01fa5a068f9fb"
Allow self-transfers only:
sender: "X"
recipient: "X"
If only the open
strategy is enabled, additional configurations are not required. However, the open
strategy may not
provide optimal spam protection, as it could allow the creation of numerous accounts with minimal assets. For enhanced
protection, using an alternative strategy, such as the fee
strategy, is recommended.
To configure the fee
strategy, additional steps are required beyond the initial setup. First, import the strategy in
the main module.rell
file
import lib.ft4.accounts.strategies.transfer.fee;
Then, configure the strategy by specifying the valid asset(s) for payments, the fee amount, and the destination for the
fee payment. This is done by adding a configuration under the moduleArgs
key in the chromia.yml
file:
blockchains:
my_dapp_chain:
module: my_dapp_module
moduleArgs:
lib.ft4.core.accounts.strategies.transfer.fee:
asset:
- id: x"b31ba66a11a28930d948c8f959cc306184096d1ee858542e765a139b3c79b1aa" # We can specify an asset by id
amount: 2L # How much of this asset to pay
- name: test1 # we can specify an asset by name, this will refer to an asset issued by this chain
amount: 1L
- issuing_blockchain_rid: x"6403ccac0c67f7cb6af78e5e15b3aaebb2b42370f0d12e099ed01fa5a068f9fb" # We can also specify assets issued by a different chain, even if the names are the same
name: test1
amount: 3L
fee_account: x"023c72addb4fdf09af94f0c94d7fe92a386a7e70cf8a1d85916386bb2535c7b1b1" # All fees will be collected into this account
In the configuration above, users can pay the fee using one of three assets, with varying amounts depending on the asset. This flexibility can accommodate value differences between assets or encourage payment with a preferred asset. It is worth mentioning that the asset id cannot be accompanied by the name or issuing chain rid. Additionally, the account for fee collection is specified, which should typically be controlled by the dapp owner.
Configuring foreign assets (cross-chain assets)
For most mainnet dapps, you'll want to use foreign assets like CHR for account registration. Here are the different ways to specify assets:
Local asset (issued by current chain)
asset:
- name: "MyLocalAsset" # Asset issued by this chain
amount: 1L
Foreign asset by ID
asset:
- id: x"C633343E4AA3213EA92158648F11BA8DFF606C6CAC80614CFA5F45E57367F823" # Asset ID
amount: 1L # 10 CHR (6 decimals)
Foreign asset by name and issuing chain (recommended for CHR)
asset:
- name: "CHR" # Asset name
issuing_blockchain_rid: x"6403ccac0c67f7cb6af78e5e15b3aaebb2b42370f0d12e099ed01fa5a068f9fb" # Token chain RID
amount: 1L # 10 CHR (6 decimals)
Most common use case - for mainnet dapps using CHR for account registration, use the third option with the CHR token chain RID.
The amount
set for an asset must be lower than the min_amount
specified for that asset. If the amount
is set too
high, users may accidentally send an insufficient amount, preventing account creation until the transfer timeout is
reached. They would then need to recall and repeat the transfer with the correct amount.
For account registration using a transfer strategy, a transfer must first be made to the account. If the user already holds an account with enough assets, they may initiate the transfer themselves, or a friend might perform it on their behalf, functioning as an invitation system.
Rate limiting
FT4 provides built-in rate limiting to prevent spam and abuse. Rate limiting can be configured globally or per-account to control how frequently users can perform operations.
Global rate limiting configuration
Configure rate limiting in your chromia.yml
:
blockchains:
<my_blockchain_name>:
module: my_dapp_module
moduleArgs:
lib.ft4.core.accounts:
rate_limit:
active: true
max_points: 20
recovery_time: 5000 # milliseconds
points_at_account_creation: 1
Rate limiting parameters
active
: Enables or disables rate limiting globallymax_points
: Maximum points allowed before rate limiting is triggeredrecovery_time
: Time in milliseconds for points to recoverpoints_at_account_creation
: Points consumed when creating an account
Per-account rate limiting
You can implement custom rate limiting logic by extending the get_rate_limit_config_for_account
function:
@extend(get_rate_limit_config_for_account) function get_rate_limit_config_for_account(account: account): rate_limit_config? {
// Return custom config for specific accounts
// Return null to use global default
}
For more details on rate limiting implementation, see the Rell Security documentation.
To see an example of account registration using the transfer open
strategy, explore the
Transfer open strategy account registration demo.
You can also find examples of all strategies in this
repository.