typestar

keccak256 en Solidity

La huella de lo que sea, vía abi.encodePacked.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

// keccak256 over abi.encodePacked: the fingerprint of anything.
contract SeedVault {
    mapping(bytes32 => bool) public deposited;

    function depositProof(string calldata variety, uint256 lot)
        external returns (bytes32 id)
    {
        id = keccak256(abi.encodePacked(variety, lot));
        deposited[id] = true;
    }

    function verify(string calldata variety, uint256 lot)
        external view returns (bool)
    {
        return deposited[keccak256(abi.encodePacked(variety, lot))];
    }
}

Cómo funciona

  1. abi.encodePacked serializa los argumentos apretados.
  2. keccak256 hashea los bytes a un id bytes32.
  3. Recalcular el hash verifica sin guardar las entradas.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
20
Caracteres a escribir
502
Tokens
93
Ritmo de tres estrellas
55 tpm

Al ritmo de tres estrellas de 55 tokens por minuto, este intento toma unos 101 segundos.

Escribe este fragmento

Paso 2 de 3 en El entorno; paso 22 de 28 en Fundamentos del lenguaje.

← Anterior Siguiente →