# 4.4 区块链类 (Blockchain Types)

Sway is fundamentally a blockchain language, and it offers a selection of types tailored for the blockchain use case.

Sway从根本上说是一种区块链语言，它为区块链的使用情况提供了一系列的类。

These are provided via the standard library ([`lib-std`](https://github.com/FuelLabs/sway/tree/master/sway-lib-std)) which both add a degree of type-safety, as well as make the intention of the developer more clear.

这些类通过标准库（[`lib-std`](https://github.com/FuelLabs/sway/tree/master/sway-lib-std)）提供，既增加了一定程度的类的安全性，又使开发者的意图更加明确。

### `Address`类 (`Address` Type)

The `Address` type is a type-safe wrapper around the primitive `b256` type. Unlike the EVM, an address **never** refers to a deployed smart contract (see the `ContractId` type below). An `Address` can be either the hash of a public key (effectively an [externally owned account](https://ethereum.org/en/whitepaper/#ethereum-accounts) if you're coming from the EVM) or the hash of a [predicate](https://fuellabs.github.io/sway/v0.38.0/book/sway-program-types/predicates.html). Addresses own UTXOs.

`Address` 类是对原始的 `b256`类的一个类安全封装。与EVM不同的是，地址永远不会指向已部署的智能合约（见下面的`ContractId`类型）。一个`Address`可以是一个公钥的哈希值（如果你是从EVM来的，那么实际上是一个[外部拥有的账户](https://ethereum.org/en/whitepaper/#ethereum-accounts)）或者是一个[谓词](https://fuellabs.github.io/sway/v0.38.0/book/sway-program-types/predicates.html)的哈希值。地址拥有UTXO。

An `Address` is implemented as follows.

一个`Address`的实施如下：

```
pub struct Address {
    value: b256,
}

```

Casting between the `b256` and `Address` types must be done explicitly:

`b256` 和`Address`类型之间的转换必须明确进行：

```
let my_number: b256 = 0x000000000000000000000000000000000000000000000000000000000000002A;
let my_address: Address = Address::from(my_number);
let forty_two: b256 = my_address.into();

```

### `ContractId`类 (`ContractId` Type)

The `ContractId` type is a type-safe wrapper around the primitive `b256` type. A contract's ID is a unique, deterministic identifier analogous to a contract's address in the EVM. Contracts cannot own UTXOs but can own assets.

&#x20;`ContractId` 类是对原始的 `b256`类的一个类安全封装。一个合约的ID是一个唯一的、确定的标识符，类似于EVM中的合约地址。合约不能拥有UTXO，但可以拥有资产。

A `ContractId` is implemented as follows.

一个 `ContractId` 的实施如下：

```
pub struct ContractId {
    value: b256,
}

```

Casting between the `b256` and `ContractId` types must be done explicitly:

`b256` 和 `ContractId`类型之间的转换必须明确进行：

```
let my_number: b256 = 0x000000000000000000000000000000000000000000000000000000000000002A;
let my_contract_id: ContractId = ContractId::from(my_number);
let forty_two: b256 = my_contract_id.into();

```

### `Identity` 类 (`Identity` Type)

The `Identity` type is an enum that allows for the handling of both `Address` and `ContractId` types. This is useful in cases where either type is accepted, e.g. receiving funds from an identified sender, but not caring if the sender is an address or a contract.

`Identity`类是一个穷举，允许处理 `Address` 和 `ContractId` 类。这在任何一种类都被接受的情况下是有用的，例如，从一个确定的发件人接收资金，但不关心发件人是一个地址还是一个合同。

An `Identity` is implemented as follows.

一个  `Identity` 的实施如下：

```
pub enum Identity {
    Address: Address,
    ContractId: ContractId,
}

```

Casting to an `Identity` must be done explicitly:

铸造到一个 `Identity`必须明确进行：

```
        let raw_address: b256 = 0xddec0e7e6a9a4a4e3e57d08d080d71a299c628a46bc609aab4627695679421ca;
        let my_identity: Identity = Identity::Address(Address::from(raw_address));

```

A `match` statement can be used to return to an `Address` or `ContractId` as well as handle cases in which their execution differs.

`match` 语句可用于返回到`Address` 或 `ContractId` ，以及处理它们执行不同的情况。

```
        let my_contract_id: ContractId = match my_identity {
            Identity::ContractId(identity) => identity,
            _ => revert(0),
        };

```

```
        match my_identity {
            Identity::Address(address) => transfer_to_address(amount, token_id, address),
            Identity::ContractId(contract_id) => force_transfer_to_contract(amount, token_id, contract_id),
        };

```

A common use case for `Identity` is for access control. The use of `Identity` uniquely allows both `ContractId` and `Address` to have access control inclusively.

`Identity`的一个常见用例是用于访问控制。使用`Identity`可以唯一地允许`ContractId`和`Address`, 同时拥有访问控制。

```
        let sender = msg_sender().unwrap();
        require(sender == storage.owner.read(), MyError::UnauthorizedUser(sender));

```
