Global contract identifiers and universal state initialization
Global contract workflows keep shared contract code separate from the accounts that use it. This page explains how a workflow identifies that code and how it creates an account with the selected code and its first storage state. It also covers universal accounts, which extend this model with deterministic 0u identities and initial access keys. See Global contracts for the wider workflow and Contract state and initialization for the account-state model.
Global contract identity
Global contract identity tells the runtime which shared code an account must use. A workflow can identify that code by its content hash or through the account that provides it, so each user account does not need its own copy of the code.
Code-hash identity
GlobalContractId::CodeHash identifies shared code by its exact CryptoHash. This form suits a workflow that already knows the required code version. Its JSON form uses a hash field with a Base58 value.
References: near-global-contracts/…/global_contract_identifier.rs• L32-48 near-global-contracts/…/global_contract_identifier.rs• L108-123
Provider-account identity
GlobalContractId::AccountId identifies shared code through the account that provides it. This lets a workflow keep a provider account as its public code reference while the runtime resolves the shared code linked to that account. Its JSON form uses an account_id field.
References: near-global-contracts/…/global_contract_identifier.rs• L34-55 near-global-contracts/…/global_contract_identifier.rs• L125-135 near-sdk/…/promise.rs• L569-583
Contract code identity
The runtime distinguishes local contract code from shared global code. The SDK exposes the current code mode and returns a global identifier only when the current account uses shared code.
Code mode
AccountContract represents no code, local code, and the two global-code identity forms. current_global_contract_id maps only the global forms to GlobalContractId. It returns None for accounts that use local code or have no code.
References: near-global-contracts/…/global_contract_identifier.rs• L5-12 near-sdk/…/env.rs• L198-241 near-sys/…/lib.rs• L12-18
Deterministic state initialization
Deterministic state initialization combines a global code selector with the first storage entries for a new account. The resulting description serves two related purposes: it defines the account's planned starting state and supplies the inputs needed to create that state.
State-init descriptor
StateInitV1 holds a GlobalContractId and an ordered map of raw storage keys to raw values. StateInit wraps this descriptor as a versioned type, which leaves room for later protocol forms. The code constructor starts with empty data, while with_data_entry adds an initial entry.
References: near-global-contracts/…/state_init.rs• L7-24 near-global-contracts/…/state_init.rs• L77-116 near-sdk/…/state_init.rs• L1-13
Deterministic account identity
The complete state-init descriptor determines the account identity for this workflow. With the borsh feature enabled, derive_account_id hashes the serialized descriptor with Keccak-256 and formats the result as an account ID that starts with 0s. A change to the selected code or to any initial key or value therefore selects a different account.
The on-chain and off-chain hash paths are intended to produce the same result. A service can use that result to compute the target account before it submits the transaction.
References: near-global-contracts/…/lib.rs• L1-8 near-global-contracts/…/state_init.rs• L26-74 near-global-contracts/README.md• L42-55
State-init action
Promise::state_init turns the descriptor and a deposit into an action for the target account. The SDK selects the matching runtime action from the identifier: a code hash uses the hash form, and a provider account uses the account form. It then attaches the initial entries to that action.
The low-level environment interface exposes the same flow for code that needs direct control. It creates the state-init action, keeps its action index, and adds the key-value entries to that action. The new account can then use shared code without a separate code deployment step.
References: near-sdk/…/promise.rs• L198-224 near-sdk/…/promise.rs• L585-589 near-sdk/…/env.rs• L1293-1400 near-sys/…/lib.rs• L105-132
Universal state initialization
Universal state initialization creates a 0u account from one complete description. That description can select global contract code, seed storage, and add full-access keys, so it can create either a key-only account or an account that starts with shared code.
Universal state-init descriptor
UniversalStateInitV1 carries three kinds of initial state: optional global contract code, storage data, and full-access key handles. UniversalStateInit wraps the versioned form, so later protocol versions can change the payload without changing the meaning of version 1. An empty code field creates a key-only account; a code field creates a contract account.
The descriptor uses sorted collections for its data and keys. This gives the same logical description one canonical serialized form. RawStateInit holds the exact serialized bytes carried by the action and committed to by the account ID. The SDK exposes these types through near_sdk::universal_state_init.
References: near-global-contracts/…/universal_state_init.rs• L10-27 near-global-contracts/…/universal_state_init.rs• L244-331 near-sdk/…/universal_state_init.rs• L7-18
Universal account identity
The complete raw state-init description determines the account identity. The account ID starts with 0u and is derived from the SHA3-256 hash of the exact state-init bytes, encoded with Crockford base32. A change to the code, data, or access keys therefore selects a different account.
Typed derivation and the SDK environment helper produce the same ID from the canonical description. A raw-byte helper can preserve a state-init version that the SDK does not yet know. Raw bytes must remain unchanged because decoding and re-encoding a non-canonical value can produce a different account ID.
Known state-init versions can be checked for valid decoding and canonical ordering before an address is shown for funding. This local check does not prove that initialization will succeed on a network. Limits, code availability, and funding still apply when the action runs. Universal accounts require protocol version 88 or later, shipped in nearcore 2.14.
References: near-global-contracts/…/lib.rs• L6-8 near-global-contracts/…/universal_state_init.rs• L10-23 near-global-contracts/…/universal_state_init.rs• L47-95 near-global-contracts/…/universal_state_init.rs• L244-271 near-sdk/…/universal_state_init.rs• L7-35
Universal state-init action
Promise::universal_state_init creates the universal account described by the state-init value and funds it with a deposit. The promise must target the ID derived from the same bytes, because the runtime rejects a receipt whose receiver does not match the description.
The lower-level environment action accepts the serialized state-init bytes and sends them to the runtime. This supports direct control and lets callers pass through a newer state-init version. The selected global code, initial storage, and access keys then become part of the new account's starting state.
References: near-sdk/…/promise.rs• L602-635 near-sdk/…/env.rs• L2621-2683 near-sys/…/lib.rs• L245-258
Global-contract workflow
The workflow uses one state-init description to connect code selection, account identity, and starting state. The code reference identifies the shared behavior, while the full descriptor determines the account that will receive that behavior and state.
This diagram shows how a code reference and initial data produce a predictable account ID and a matching initialization action. A universal workflow can also include access keys and produce a 0u account.
flowchart LR
A[Code hash or provider account] --> B[State-init descriptor]
D[Initial key-value data] --> B
K[Initial access keys] --> U[Universal state-init descriptor]
A --> U
D --> U
B --> C[Deterministic 0s account ID]
B --> E[Deterministic state-init action]
U --> V[Universal 0u account ID]
U --> W[Universal state-init action]
E --> F[Account with shared code and initial state]
W --> X[Account with shared code, state, and keys]
References: near-global-contracts/…/state_init.rs• L22-24 near-global-contracts/…/universal_state_init.rs• L71-92 near-sdk/…/promise.rs• L602-635
Change checklist
When these files or sections are changed, remember to consider these:
- A new
StateInitorUniversalStateInitversion must remain compatible with account-ID derivation and with promise dispatch to the matching runtime action. - A change to
GlobalContractIdmust keep its JSON and Borsh forms aligned with nearcore conversions. - A change to either derivation input, hash method, or account-ID format must preserve parity between on-chain and off-chain derivation.
- A change to universal state-init fields must preserve canonical ordering and the exact nearcore byte layout.
- A change to raw universal state-init handling must preserve exact bytes when contracts or services forward an initialization request.
- A change to state-init action arguments must pass through the
near-syshost declarations, the environment layer, and the promise layer. - A change to global-code reporting must preserve the mapping between runtime code modes and
current_global_contract_id.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The state-init descriptor changes after an account ID is computed. | The derived ID names a different account from the one planned. | Derive and submit from the same complete state-init value. |
| A provider account does not identify available global code. | The runtime cannot resolve the selected code. | Use a known global-code provider or select the code by hash. |
| Code treats a local contract as a global contract. | A global-code check uses an invalid reference. | Check for Some from current_global_contract_id before use. |
| Initial data is attached to the wrong state-init action. | The new account receives incomplete or incorrect initial state. | Keep the action index returned by state-init creation with its data entries. |
| A caller sends the wrong serialized identifier shape. | The identifier cannot cross the JSON boundary. | Use { "hash": ... } for code hashes and { "account_id": ... } for provider accounts. |
| A universal action targets an account ID that does not match its state-init bytes. | The runtime rejects the receipt. | Derive the target from the exact state-init bytes sent in the action. |
| A caller re-encodes non-canonical universal state-init bytes before deriving the ID. | The derived ID differs from the account ID committed to the original bytes. | Validate known versions, then preserve the original bytes when forwarding. |
| The host does not support universal accounts. | Universal account derivation or creation is unavailable. | Use a host with protocol version 88 or later. |
| A caller shows an address from unvalidated raw bytes. | Funding can target an invalid or unintended account. | Validate canonical bytes before presenting the address for funding. |
Key files
| File | Why to read it |
|---|---|
| near-global-contracts/…/lib.rs | States the account-ID models, hash rules, and intended off-chain use of the types. |
| near-global-contracts/…/global_contract_identifier.rs | Defines the two forms of global code identity and their external representations. |
| near-global-contracts/…/state_init.rs | Defines the versioned deterministic state-init value, initial data, and 0s account-ID rule. |
| near-global-contracts/…/universal_state_init.rs | Defines raw and typed universal state-init values, canonical identity rules, initial keys, and 0u account-ID derivation. |
| near-sdk/…/promise.rs | Shows how high-level promise APIs create deterministic and universal state-init actions. |
| near-sdk/…/env.rs | Shows account-ID derivation and the contract-facing low-level state-init actions. |
| near-sdk/…/state_init.rs | Provides the near-sdk entry point for deterministic state-init types. |
| near-sdk/…/universal_state_init.rs | Provides the near-sdk entry point for universal state-init types and their raw-byte forwarding contract. |
| near-sys/…/lib.rs | Defines the host interface for universal account derivation and initialization actions. |