Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit d6ee572
Merge: a50375e 5bda589
Author: SwayStar123 <[email protected]>
Date:   Mon Jan 27 15:51:16 2025 +0530

    Merge pull request #166 from FuelLabs/bitzoic-v0.66.6

    Update to use forc `v0.66.6` and fuel core `v0.40.0`

commit 5bda589
Author: bitzoic <[email protected]>
Date:   Fri Jan 24 12:14:43 2025 +0100

    Remove sway-lib-core import

commit 10d90f9
Author: bitzoic <[email protected]>
Date:   Fri Jan 24 12:10:20 2025 +0100

    Run formatter

commit 2def8e8
Author: bitzoic <[email protected]>
Date:   Thu Jan 23 10:29:48 2025 +0100

    Update CHANGELOG

commit 420fc98
Author: bitzoic <[email protected]>
Date:   Thu Jan 23 10:25:49 2025 +0100

    Update to use forc v0.66.6

commit a50375e
Author: Antony Burrows <[email protected]>
Date:   Wed Jan 22 22:19:03 2025 +1300

    feat: add SRC-16 Typed Structured Data (#161)

    * feat: add SRC-16 typed structured data standard

    * Add backwards compatibility for EIP712.

    * Add encoding for native Fuel types, add Fixed array encoding, adjust SRC16Domain fuel_example contract, Adjust types for domains. Update docs.

    * fix code block in Domain Separator Compatibility decion in doc.

    * address spell check, add notes on salt and verifying contract, add note on chian_id padding in docs.

    * Add spell check and formatting fixes to src-16 docs.

    * adjust typeOf in src-16 docs.

    * add custom spell check words.

commit ce56cde
Author: Cameron Carstens <[email protected]>
Date:   Mon Jan 20 14:36:19 2025 +0100

    Add SRC-15 to README (#165)

commit 3f6543f
Author: Cameron Carstens <[email protected]>
Date:   Sat Dec 14 08:51:30 2024 +0000

    Update master from v0.6.1 to v0.6.2 (#163)

    * Update from v0.6.1 to v0.6.2

    * Update CHANGELOG

    * Add PR #162

commit e7d3617
Author: Cameron Carstens <[email protected]>
Date:   Fri Dec 13 11:03:50 2024 +0000

    SRC-15; Offchain Metadata Standard (#159)

    * Create SRC-15 specifications

    * Create SRC-15 standard

    * Create SRC-15 examples

    * Add SRC-15 examples to CI

    * Update CHANGELOG

    * Run formatter

    * Fix markdown

    * Update custom words spellcheck

    * Remove sender and add nonce to SRC15 event

    * Update standards and examples with nonce

    * Resolve review comments

    * Add comment on restricting who may emit

    * Remove nonce from SRC-15 log

commit ccf84fd
Author: Call Delegation <[email protected]>
Date:   Thu Dec 12 07:27:52 2024 -0500

    docs: Add CI link check (#162)

    * add

    * r

    * r

commit fefbd93
Author: Cameron Carstens <[email protected]>
Date:   Mon Nov 25 23:07:09 2024 +0700

    Fix typo in SRC-7 inline docs (#160)

    * Fix typo in SRC-7 inline docs

    * Update CHANGELOG

commit c53ef03
Author: Cameron Carstens <[email protected]>
Date:   Mon Oct 28 19:16:59 2024 +0700

    Rename SRC-7 to Onchain Native Asset Metadata Standard (#157)

    * Update SRC-7 to Onchain Native Asset Metadata Standard

    * Add onchain to spellcheck

    * Update CHANGELOG

commit a3f87a4
Author: Cameron Carstens <[email protected]>
Date:   Mon Oct 7 13:15:10 2024 +0545

    Update abi names in standards docs (#154)

    * Update abi names in standards docs

    * Update CHANGELOG

commit 408d81a
Merge: 04a5248 55541de
Author: SwayStar123 <[email protected]>
Date:   Tue Sep 17 11:45:29 2024 +0530

    Merge pull request #153 from FuelLabs/swaystar123/fix-set_src20_data-bug

    Actually write to storage in src20 multi asset example

commit 04a5248
Merge: ee95fad c8b5618
Author: SwayStar123 <[email protected]>
Date:   Tue Sep 17 10:51:52 2024 +0530

    Merge pull request #152 from FuelLabs/add-src6-inline-doc-examples

    Add inline docs examples to SRC-6 abi

commit ee95fad
Merge: 29986f1 2a822da
Author: SwayStar123 <[email protected]>
Date:   Tue Sep 17 10:51:28 2024 +0530

    Merge pull request #151 from FuelLabs/update-src-6-to-latest-src-20-specs

    Add required logging of SRC-20 to SRC-6 examples

commit 55541de
Author: SwayStar123 <[email protected]>
Date:   Mon Sep 16 11:51:57 2024 +0530

    add write to storage annotation

commit 607066a
Author: SwayStar123 <[email protected]>
Date:   Mon Sep 16 11:50:20 2024 +0530

    add changelog

commit 17809b0
Author: SwayStar123 <[email protected]>
Date:   Mon Sep 16 11:47:47 2024 +0530

    add sotrage writes to set_src20_data

commit c8b5618
Author: SwayStar123 <[email protected]>
Date:   Wed Sep 11 18:01:19 2024 +0530

    move changelog

commit 2a822da
Author: SwayStar123 <[email protected]>
Date:   Wed Sep 11 17:56:13 2024 +0530

    add write permission

commit 2ebb457
Author: SwayStar123 <[email protected]>
Date:   Wed Sep 11 17:54:19 2024 +0530

    get(asset) for maps

commit 6948958
Author: SwayStar123 <[email protected]>
Date:   Wed Sep 11 17:51:55 2024 +0530

    fmt

commit 731e5a2
Author: SwayStar123 <[email protected]>
Date:   Wed Sep 11 17:51:18 2024 +0530

    resolve comments

commit 1718d5a
Author: SwayStar123 <[email protected]>
Date:   Tue Sep 10 11:40:38 2024 +0530

    ujpdate changelog pr number

commit d467526
Author: SwayStar123 <[email protected]>
Date:   Tue Sep 10 11:40:22 2024 +0530

    init

commit d9ad426
Author: SwayStar123 <[email protected]>
Date:   Tue Sep 10 11:29:18 2024 +0530

    remove get from sasbv

commit 3263c4a
Author: SwayStar123 <[email protected]>
Date:   Tue Sep 10 11:27:20 2024 +0530

    unwrap -> read

commit 90d748c
Author: SwayStar123 <[email protected]>
Date:   Tue Sep 10 11:24:43 2024 +0530

    fmt 2

commit 6dc8a28
Author: SwayStar123 <[email protected]>
Date:   Tue Sep 10 11:23:24 2024 +0530

    fix some get calls

commit 6ab90c3
Author: SwayStar123 <[email protected]>
Date:   Tue Sep 10 11:20:03 2024 +0530

    fix import

commit 64dbef4
Author: SwayStar123 <[email protected]>
Date:   Tue Sep 10 11:17:28 2024 +0530

    remove unused param from SetSRC20Data impl

commit 18d4842
Author: SwayStar123 <[email protected]>
Date:   Tue Sep 10 11:12:18 2024 +0530

    init

commit 29986f1
Author: Cameron Carstens <[email protected]>
Date:   Mon Sep 9 14:37:47 2024 +0800

    Add helper functions to struct types and `Eq` implementations for all types (#149)

    * Add helper functions to struct types and Eq implementations

    * Add checks to enum types

    * Update CHANGELOG

commit ab52d73
Author: Cameron Carstens <[email protected]>
Date:   Wed Sep 4 10:15:04 2024 +0800

    Prepare for Sway-Standards `v0.6.1` release (#147)

    * Prepare for Sway-Standards v0.6.1 release

    * Update CHANGELOG

commit 5b501cf
Author: Cameron Carstens <[email protected]>
Date:   Mon Sep 2 11:13:31 2024 +0800

    Fix Sway Standards inline docs (#142)

    * Update sway standards inline docs

    * Update CHANGELOG

    * Fix typo

commit 7ce70c9
Author: Cameron Carstens <[email protected]>
Date:   Fri Aug 30 21:47:38 2024 +0800

    Remove metadata functions (#148)

commit 955061b
Author: Cameron Carstens <[email protected]>
Date:   Fri Aug 30 14:01:49 2024 +0800

    Add helper functions to SRC7 standard's `Metadata` (#144)

    * Add helper functions to SRC7 standard

    * Update CHANGELOG

commit d264041
Author: K1-R1 <[email protected]>
Date:   Fri Aug 30 06:58:25 2024 +0100

    docs: update src14 storage recommendation and general typos (#136)

    * chore: update to forc 0.63.1

    * chore: updated changelog

    * docs: update src14 and general typos

    * Apply PR review comments

    * Fix markdown

    * Update CHANGELOG

    * Improve CHANGELOG comment

    ---------

    Co-authored-by: bitzoic <[email protected]>

commit a477a91
Author: K1-R1 <[email protected]>
Date:   Fri Aug 30 06:41:26 2024 +0100

    chore: update to forc 0.63.3 (#135)

    * chore: update to forc 0.63.1

    * chore: updated changelog

    * Update to forc v0.63.3

    ---------

    Co-authored-by: bitzoic <[email protected]>

commit 0cbef85
Author: Cameron Carstens <[email protected]>
Date:   Tue Aug 27 13:38:25 2024 +0800

    Resolve warnings in examples (#137)

    * Resolve warnings

    * Update CHANGELOG

    * Run formatter

    * Remove cancel in progress from CI

    * Revert remove cancel in progress from CI

    * fix max_depositable and max_withdrawable examples

    ---------

    Co-authored-by: SwayStar123 <[email protected]>

commit bab74e4
Merge: 69217c2 94bc033
Author: SwayStar123 <[email protected]>
Date:   Tue Aug 27 08:19:21 2024 +0530

    Merge pull request #139 from FuelLabs/bitzoic-release-v0.6.0

    Prepare for v0.6.0 release

commit 94bc033
Author: bitzoic <[email protected]>
Date:   Mon Aug 26 21:29:41 2024 +0800

    Update CHANGELOG

commit 81c9e65
Author: bitzoic <[email protected]>
Date:   Mon Aug 26 21:28:23 2024 +0800

    Prepare for v0.6.0 release

commit 69217c2
Author: Cameron Carstens <[email protected]>
Date:   Mon Aug 26 21:03:38 2024 +0800

    Add event logging to SRC-20 and SRC-7 standards (#130)

    * Add event logging to SRC-20 and SRC-7 specification

    * Add event logging structs to SRC-20 and SRC-7

    * Update CHANGELOG

    * Add inline docs to SRC-20 events

    * Fix CI

    * Require that logs of metadata are emitted even with contants

    * Update standards with additional log and ordering

    * Update examples to follow new specs

    * Add custom word to spell checker

    * Run formatter

    * Fix markdown formatting

    * Resolve warnings in examples

    * Build CI with release

    * Store srv7 metadata to storage

    * Remove cancel in progress from CI

    * Update name for TotalSupplyEvent

    * Split examples into seperate workspace projects

commit 89e9882
Author: Cameron Carstens <[email protected]>
Date:   Fri Aug 23 18:08:00 2024 +0800

    Make the `SubId` an `Option` in SRC-3's `mint()` function  (#131)

    * Update specifications to change SRC-3 mint sub_id to an Option

    * Update SRC-3 standard for option in mint

    * Update examples

    * Udpate CHANGELOG

    * Run formatter

    * Fix spelling

commit 2f45270
Author: Cameron Carstens <[email protected]>
Date:   Tue Aug 6 10:51:32 2024 +0700

    Prepare master for v0.5.2 release (#126)

    * Prepare for v0.5.2 release

    * Update CHANGELOG

    * Update CHANGELOG formatting

commit d736ca6
Merge: ade6077 94b3978
Author: K1-R1 <[email protected]>
Date:   Thu Aug 1 19:13:53 2024 +0100

    Merge pull request #124 from FuelLabs/sophie/fix-warnings

commit 94b3978
Author: Sophie <[email protected]>
Date:   Wed Jul 31 11:33:31 2024 -0700

    Update changelog

commit 6f2b07c
Author: Sophie <[email protected]>
Date:   Thu Jul 25 10:34:28 2024 -0700

    remove for examples

commit e7e8dfa
Author: Sophie <[email protected]>
Date:   Thu Jul 25 10:28:50 2024 -0700

    chore: fix compiler warnings

commit ade6077
Author: IGI-111 <[email protected]>
Date:   Mon Jul 22 07:53:53 2024 +0200

    Use new namespace syntax for storage (#120)

commit 3acac2b
Author: Cameron Carstens <[email protected]>
Date:   Tue Jul 9 22:24:50 2024 +0800

    SRC-6 example contract does not update managed assets (#122)

    * Update SRC-6 example with decrementation of managed assets

    * Update CHANGELOG

    * Fix link on CHANGELOG.md

    * Update CHANGELOG to resolve markdown error with duplicate headers

    * Write to storage

commit b0e99a6
Merge: 9c85a02 babc929
Author: SwayStar123 <[email protected]>
Date:   Mon Jul 8 19:56:47 2024 +0530

    Merge pull request #121 from FuelLabs/fixsrc6eg

    Fix vault standard examples

commit babc929
Author: SwayStar123 <[email protected]>
Date:   Mon Jul 8 16:37:19 2024 +0530

    add  v0.5.1

commit b70a650
Author: SwayStar123 <[email protected]>
Date:   Mon Jul 8 16:36:04 2024 +0530

    remove date

commit cb4dbab
Author: SwayStar123 <[email protected]>
Date:   Mon Jul 8 16:32:05 2024 +0530

    add "unreleased" to changelog heads

commit 7edb337
Author: SwayStar123 <[email protected]>
Date:   Mon Jul 8 16:27:51 2024 +0530

    move changelog to new empty changelog

commit 050977c
Author: SwayStar123 <[email protected]>
Date:   Tue Jul 2 14:38:33 2024 +0000

    add changelog

commit f0b5631
Author: SwayStar123 <[email protected]>
Date:   Tue Jul 2 14:32:46 2024 +0000

    fix vault reads

commit 9c85a02
Author: Cameron Carstens <[email protected]>
Date:   Thu Jun 27 19:34:29 2024 +0800

     Hotfix: Update Cargo.toml to v0.5.1 (#115)

    * Hotfix: Update Cargo.toml to v0.5.1

    * Udpate CHANGELOG
  • Loading branch information
bitzoic committed Jan 27, 2025
1 parent ba788b2 commit 66deb13
Show file tree
Hide file tree
Showing 21 changed files with 1,619 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ env:
CARGO_TERM_COLOR: always
REGISTRY: ghcr.io
RUST_VERSION: 1.80.1
FORC_VERSION: 0.63.3
CORE_VERSION: 0.34.0
FORC_VERSION: 0.66.6
CORE_VERSION: 0.40.0
PATH_TO_SCRIPTS: .github/scripts

jobs:
Expand Down
27 changes: 14 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
## [Version 0.6.3]

### Added
### New Standards v0.6.3

- Something new here 1
- Something new here 2
- [#161](https://github.com/FuelLabs/sway-standards/pull/161) Defines the SRC-16; Typed Structured Data Standard.

### Changed
### Added v0.6.3

- Something changed here 1
- Something changed here 2
- [#165](https://github.com/FuelLabs/sway-standards/pull/165) Adds the SRC-15 standard to the README.

### Fixed
### Changed v0.6.3

- Some fix here 1
- Some fix here 2
- [#166](https://github.com/FuelLabs/sway-standards/pull/166) Updates standards, examples, and CI to forc `v0.66.6` and fuel-core `v0.40.0`.
- [#167](https://github.com/FuelLabs/sway-standards/pull/167) Prepares for the v0.6.3 release.

### Breaking
### Fixed v0.6.3

- Some breaking change here 1
- Some breaking change here 2
- None

### Breaking v0.6.3

- None

## [Version 0.6.2]

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<a href="https://github.com/FuelLabs/sway-standards/actions/workflows/ci.yaml" alt="CI">
<img src="https://github.com/FuelLabs/sway-standards/actions/workflows/ci.yaml/badge.svg" />
</a>
<a href="https://crates.io/crates/forc/0.63.3" alt="forc">
<img src="https://img.shields.io/badge/forc-v0.63.3-orange" />
<a href="https://crates.io/crates/forc/0.66.6" alt="forc">
<img src="https://img.shields.io/badge/forc-v0.66.6-orange" />
</a>
<a href="./LICENSE" alt="forc">
<img src="https://img.shields.io/github/license/FuelLabs/sway-standards" />
Expand Down Expand Up @@ -41,6 +41,7 @@ If you don't find what you're looking for, feel free to create an issue and prop
- [SRC-9; Metadata Keys Standard](https://docs.fuel.network/docs/sway-standards/src-9-metadata-keys/) is used to store standardized metadata keys for [Native Assets](https://docs.fuel.network/docs/sway/blockchain-development/native_assets) in combination with the SRC-7 standard.
- [SRC-6; Vault Standard](https://docs.fuel.network/docs/sway-standards/src-6-vault/) defines the implementation of a standard API for asset vaults developed in Sway.
- [SRC-13; Soulbound Address](https://docs.fuel.network/docs/sway-standards/src-13-soulbound-address/) provides a predicate interface to lock [Native Assets](https://docs.fuel.network/docs/sway/blockchain-development/native_assets) as soulbound.
- [SRC-15; Offchain Asset Metadata Standard](https://docs.fuel.network/docs/sway-standards/src-15-offchain-asset-metadata/) enables arbitrary metadata that is not required by other contracts onchain, in a stateless manner for [Native Assets](https://docs.fuel.network/docs/sway/blockchain-development/native_assets).

### Access Control

Expand Down Expand Up @@ -162,7 +163,7 @@ Example of a minimal SRC-14 implementation with no access control.
Example of a SRC-14 implementation that also implements [SRC-5](https://docs.fuel.network/docs/sway-standards/src-5-ownership/).

> **Note**
> All standards currently use `forc v0.63.3`.
> All standards currently use `forc v0.66.6`.
<!-- TODO:
## Contributing
Expand Down
4 changes: 4 additions & 0 deletions docs/spell-check-custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,7 @@ onchain
Offchain
offchain
MetadataEvent
injective
encodings
endian
DeFi
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
- [SRC-13: Soulbound Address](./src-13-soulbound-address.md)
- [SRC-14: Simple Upgradeable Contract](./src-14-simple-upgradeable-proxies.md)
- [SRC-15: Offchain Asset Metadata](./src-15-offchain-asset-metadata.md)
- [SRC-16: Typed Structured Data](./src-16-typed-structured-data.md)
- [SRC-20: Native Asset](./src-20-native-asset.md)
6 changes: 5 additions & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Standards in this repository may be in various stages of development. Use of dra
If you don't find what you're looking for, feel free to create an issue and propose a new standard!

> **Note**
> All standards currently use `forc v0.63.3`.
> All standards currently use `forc v0.66.6`.
## Using a standard

Expand Down Expand Up @@ -59,6 +59,10 @@ use standards::src20::SRC20;
- [SRC-8; Bridged Asset](./src-8-bridged-asset.md) defines the metadata required for an asset bridged to the Fuel Network.
- [SRC-10; Native Bridge Standard](./src-10-native-bridge.md) defines the standard API for the Native Bridge between the Fuel Chain and the canonical base chain.

### Encoding and hashing

- [SRC-16; Typed Structured Data](./src-16-typed-structured-data.md) defines standard encoding and hashing of typed structured data.

### Documentation

- [SRC-2; Inline Documentation](./src-2-inline-documentation.md) defines how to document your Sway files.
271 changes: 271 additions & 0 deletions docs/src/src-16-typed-structured-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
# SRC-16: Typed Structured Data

The following standard sets out to standardize encoding and hashing of typed structured data. This enables secure off-chain message signing with human-readable data structures.

## Motivation

As the Fuel ecosystem expands, there's an increasing need for applications to handle complex, human-readable data structures rather than raw bytes. When users sign messages or transactions, they should be able to clearly understand what they're signing, whether it's a simple asset transfer, or a complex DeFi interaction. Without a standard method for hashing structured data, developers risk implementing their own solutions, which could lead to confusion or compromise security. This standard provides a secure and consistent way to handle encoding and hashing of structured data, ensuring both safety and usability within ecosystem.

This standard aims to:

* Provide a secure, standardized method for hashing structured data
* Enable clear presentation of structured data for user verification during signing
* Support complex data types that mirror Sway structs
* Enable domain separation to prevent cross-protocol replay attacks
* Define a consistent encoding scheme for structured data types
* Remain stateless, not requiring any storage attributes to enable use across all Fuel program types.

## Prior Art

This standard uses ideas from [Ethereum's EIP-712 standard](https://eips.ethereum.org/EIPS/eip-712), adapting its concepts for the Fuel ecosystem. EIP-712 has proven successful in enabling secure structured data signing for applications like the various browser based wallets and signers that are utilized throughout various DeFi protocols.

## Specification

### Definition of Typed Structured Data 𝕊

The set of structured data 𝕊 consists of all instances of struct types that can be composed from the following types:

Atomic Types:

```sway
u8 to u256
bool
b256 (hash)
```

Dynamic Types:

```sway
Bytes // Variable-length byte sequences
String // Variable-length strings
```

Reference Types:

Arrays (both fixed size and dynamic)
Structs (reference to other struct types)

Example struct definition:

```sway
struct Mail {
from: Address,
to: Address,
contents: String,
}
```

### Domain Separator Encoding

The domain separator provides context for the signing operation, preventing cross-protocol replay attacks. It is computed as hash_struct(domain) where domain is defined as:

```sway
pub struct SRC16Domain {
name: String, // The protocol name (e.g., "MyProtocol")
version: String, // The protocol version (e.g., "1")
chain_id: u64, // The Fuel chain ID
verifying_contract: ContractId, // The contract id that will verify the signature
}
```

The `chain_id` field is a u64 that must be encoded by left-padding with zeros and packed in big-endian order to fill a 32-byte value.

The domain separator encoding follows this scheme:

* Add SRC16_DOMAIN_TYPE_HASH
* Add Keccak256 hash of name string
* Add Keccak256 hash of version string
* Add chain ID as 32-byte big-endian
* Add verifying contract id as 32 bytes

## Type Encoding

Each struct type is encoded as name ‖ "(" ‖ member₁ ‖ "," ‖ member₂ ‖ "," ‖ … ‖ memberₙ ")" where each member is written as type ‖ " " ‖ name.

Example:

```sway
Mail(address from,address to,string contents)
```

## Data Encoding

### Definition of hash_struct

The hash_struct function is defined as:

hash_struct(s : 𝕊) = keccak256(type_hash ‖ encode_data(s))
where:

* type_hash = keccak256(encode_type(type of s))
* ‖ represents byte concatenation
* encode_type and encode_data are defined below

### Definition of encode_data

The encoding of a struct instance is enc(value₁) ‖ enc(value₂) ‖ … ‖ enc(valueₙ), the concatenation of the encoded member values in the order they appear in the type. Each encoded member value is exactly 32 bytes long.

The values are encoded as follows:

Atomic Values:

* Boolean false and true are encoded as u64 values 0 and 1, padded to 32 bytes
* `Address`, `ContractId`, `Identity`, and `b256` are encoded directly as 32 bytes
* Unsigned Integer values (u8 to u256) are encoded as big-endian bytes, padded to 32 bytes

Dynamic Types:

* `Bytes` and `String` are encoded as their Keccak256 hash

Reference Types:

* Arrays (both fixed and dynamic) are encoded as the Keccak256 hash of their concatenated encodings
* Struct values are encoded recursively as hash_struct(value)

The implementation of `TypedDataHash` for `𝕊` SHALL utilize the `DataEncoder` for encoding each element of the struct based on its type.

## Final Message Encoding

The encoding of structured data follows this pattern:

encode(domain_separator : 𝔹²⁵⁶, message : 𝕊) = "\x19\x01" ‖ `domain_separator``hash_struct`(message)

where:

* \x19\x01 is a constant prefix
* ‖ represents byte concatenation
* `domain_separator` is the 32-byte hash of the domain parameters
* `hash_struct`(message) is the 32-byte hash of the structured data

## Example implementation

```sway
const MAIL_TYPE_HASH: b256 = 0x536e54c54e6699204b424f41f6dea846ee38ac369afec3e7c141d2c92c65e67f;
impl TypedDataHash for Mail {
fn type_hash() -> b256 {
MAIL_TYPE_HASH
}
fn struct_hash(self) -> b256 {
let mut encoded = Bytes::new();
encoded.append(
MAIL_TYPE_HASH.to_be_bytes()
);
encoded.append(
DataEncoder::encode_address(self.from).to_be_bytes()
);
encoded.append(
DataEncoder::encode_address(self.to).to_be_bytes()
);
encoded.append(
DataEncoder::encode_string(self.contents).to_be_bytes()
);
keccak256(encoded)
}
}
```

## Rationale

* Domain separators provides protocol-specific context to prevent signature replay across different protocols and chains.
* Type hashes ensure type safety and prevent collisions between different data structures
* The encoding scheme is designed to be deterministic and injective
* The standard maintains compatibility with existing Sway types and practices

## Backwards Compatibility

This standard is compatible with existing Sway data structures and can be implemented alongside other Fuel standards. It does not conflict with existing signature verification methods.

### Type System Compatibility Notes

When implementing SRC16 in relation to EIP712, the following type mappings and considerations apply:

#### String Encoding

* Both standards use the same String type and encoding
* SRC16 specifically uses String type only (not Sway's `str` or `str[]`)
* String values are encoded identically in both standards using keccak256 hash

#### Fixed Bytes

* EIP712's `bytes32` maps directly to Sway's `b256`
* Encoded using `encode_b256` in the `DataEncoder`
* Both standards handle 32-byte values identically
* Smaller fixed byte arrays (`bytes1` to `bytes31`) are not supported in SRC16

#### Address Types

* EIP712 uses 20-byte Ethereum addresses
* When encoding an EIP712 address, SRC16:
* Takes only rightmost 20 bytes from a 32-byte Fuel Address
* Pads with zeros on the left for EIP712 compatibility
* Example: Fuel `Address` of 32 bytes becomes rightmost 20 bytes in EIP712 encoding

#### ContractId Handling

* `ContractId` is unique to Fuel/SRC16 (no equivalent in EIP712)
* When encoding for EIP712 compatibility:
* Uses rightmost 20 bytes of `ContractId`
* Particularly important in domain separators where EIP712 expects a 20-byte address

#### Domain Separator Compatibility

```sway
// SRC16 Domain (Fuel native)
pub struct SRC16Domain {
name: String, // Same as EIP712
version: String, // Same as EIP712
chain_id: u64, // Fuel chain ID
verifying_contract: ContractId, // Full 32-byte ContractId
}
// EIP712 Domain (Ethereum compatible)
pub struct EIP712Domain {
name: String,
version: String,
chain_id: u256,
verifying_contract: b256, // Only rightmost 20 bytes used
}
```

Note on `verifying_contract` field; When implementing EIP712 compatibility within SRC16, the `verifying_contract` address in the `EIP712Domain` must be constructed by taking only the rightmost 20 bytes from either a Fuel `ContractId`. This ensures proper compatibility with Ethereum's 20-byte addressing scheme in the domain separator.

```sway
// Example ContractId conversion:
// Fuel ContractId (32 bytes):
// 0x000000000000000000000000a2233d3bf2aa3f0cbbe824eb04afc1acc84c364c
// └─────────────── 20 bytes ───────────────┘
//
// EIP712 Address (20 bytes):
// 0xa2233d3bf2aa3f0cbbe824eb04afc1acc84c364c
// └─────────────── 20 bytes ───────────────┘
```

Note on EIP712 Domain Separator `salt`; Within EIP712 the field `salt` is an optional field to be used at the discretion of the protocol designer. Within SRC16 the `EIP712Domain` does not use the `salt` field. The other fields in `EIP712Domain` are mandatory within SRC16.

## Security Considerations

### Replay Attacks

Implementations must ensure signatures cannot be replayed across:

Different chains (prevented by chain_id)
Different protocols (prevented by domain separator)
Different contracts (prevented by verifying_contract)

### Type Safety

Implementations must validate all type information and enforce strict encoding rules to prevent type confusion attacks.

## Example Implementation

Example of the SRC16 implementation where a contract utilizes the encoding scheme to produce a typed structured data hash of the Mail type.

```sway
{{#include ../examples/src16-typed-data/fuel_example/src/main.sw}}
{{#include ../examples/src16-typed-data/ethereum_example/src/main.sw}}
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ contract;

use standards::src11::{SecurityInformation, SRC11};

use std::{string::String, vec::Vec,};
use std::{string::String, vec::Vec};

/// The name of the project
const NAME: str[7] = __to_str_array("Example");
Expand Down
Loading

0 comments on commit 66deb13

Please sign in to comment.