From 8cdbd73b00a5962ed9a9d6c64733de2a8da29ca4 Mon Sep 17 00:00:00 2001 From: kaiadachi Date: Sat, 16 Nov 2024 21:33:15 +0700 Subject: [PATCH 1/3] clean: apply lint feature/add_sample --- test/SignProtocolExample.t.sol | 94 ++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 test/SignProtocolExample.t.sol diff --git a/test/SignProtocolExample.t.sol b/test/SignProtocolExample.t.sol new file mode 100644 index 0000000..4857ec3 --- /dev/null +++ b/test/SignProtocolExample.t.sol @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.20; + +import { Test } from "forge-std/Test.sol"; +import { WhitelistHook } from "../src/02-schema-hook/WhitelistHook.sol"; +import { SP } from "@ethsign/sign-protocol-evm/src/core/SP.sol"; +import { ISP } from "@ethsign/sign-protocol-evm/src/interfaces/ISP.sol"; +import { Schema } from "@ethsign/sign-protocol-evm/src/models/Schema.sol"; +import { DataLocation } from "@ethsign/sign-protocol-evm/src/models/DataLocation.sol"; +import { Attestation } from "@ethsign/sign-protocol-evm/src/models/Attestation.sol"; + +/** + * @title SignProtocolExample + * @notice Reference implementation for Sign Protocol integration + * @dev Demonstrates basic schema registration and attestation creation + */ +contract SignProtocolExample is Test { + ISP public sp; + WhitelistHook public whitelistHook; + uint64 public schemaId; + + address public constant EXAMPLE_ATTESTER = 0x55D22d83752a9bE59B8959f97FCf3b2CAbca5094; + + function setUp() external { + sp = new SP(); + SP(address(sp)).initialize(1, 1); + + whitelistHook = new WhitelistHook(); + whitelistHook.setWhitelist(EXAMPLE_ATTESTER, true); + + Schema memory schema = _createSchema(); + schemaId = sp.register(schema, ""); + } + + function test_attestationFlow() external { + bytes memory data = encodeData("exampleId", "exampleField", block.timestamp); + + Attestation memory attestation = _createAttestation(data); + + vm.prank(EXAMPLE_ATTESTER); + uint64 attestationId = sp.attest(attestation, "example", "", ""); + + _verifyAttestation(attestationId); + } + + function _createSchema() internal view returns (Schema memory) { + return Schema({ + registrant: address(this), + revocable: true, + dataLocation: DataLocation.ONCHAIN, + maxValidFor: 0, + hook: whitelistHook, + timestamp: 0, + data: _getSchemaData() + }); + } + + function _createAttestation(bytes memory data) internal view returns (Attestation memory) { + return Attestation({ + schemaId: schemaId, + linkedAttestationId: 0, + attestTimestamp: 0, + revokeTimestamp: 0, + data: data, + attester: EXAMPLE_ATTESTER, + validUntil: uint64(block.timestamp + 1 days), + dataLocation: DataLocation.ONCHAIN, + revoked: false, + recipients: new bytes[](0) + }); + } + + function _verifyAttestation(uint64 attestationId) internal { + Attestation memory storedAttestation = sp.getAttestation(attestationId); + (string memory id, string memory field, uint256 timestamp) = decodeData(storedAttestation.data); + + assertEq(id, "exampleId"); + assertEq(field, "exampleField"); + assertEq(timestamp, block.timestamp); + } + + function _getSchemaData() internal pure returns (string memory) { + return "{\"name\":\"ExampleSchema\",\"data\":[{\"name\":\"id\",\"type\":\"string\"}," + "{\"name\":\"field\",\"type\":\"string\"},{\"name\":\"timestamp\",\"type\":\"uint256\"}]}"; + } + + function encodeData(string memory id, string memory field, uint256 timestamp) public pure returns (bytes memory) { + return abi.encode(id, field, timestamp); + } + + function decodeData(bytes memory data) public pure returns (string memory, string memory, uint256) { + return abi.decode(data, (string, string, uint256)); + } +} From e41bdd1f16212b41b65cd071d3866d81a2f530a0 Mon Sep 17 00:00:00 2001 From: kaiadachi Date: Sat, 16 Nov 2024 21:39:55 +0700 Subject: [PATCH 2/3] clean: add NatSpec feature/add_sample --- test/SignProtocolExample.t.sol | 40 +++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/test/SignProtocolExample.t.sol b/test/SignProtocolExample.t.sol index 4857ec3..df7a378 100644 --- a/test/SignProtocolExample.t.sol +++ b/test/SignProtocolExample.t.sol @@ -15,12 +15,17 @@ import { Attestation } from "@ethsign/sign-protocol-evm/src/models/Attestation.s * @dev Demonstrates basic schema registration and attestation creation */ contract SignProtocolExample is Test { + // ============ Storage ============ ISP public sp; WhitelistHook public whitelistHook; uint64 public schemaId; address public constant EXAMPLE_ATTESTER = 0x55D22d83752a9bE59B8959f97FCf3b2CAbca5094; + /** + * @notice Set up the test environment + * @dev Deploys SP, initializes WhitelistHook, and registers schema + */ function setUp() external { sp = new SP(); SP(address(sp)).initialize(1, 1); @@ -32,6 +37,10 @@ contract SignProtocolExample is Test { schemaId = sp.register(schema, ""); } + /** + * @notice Test the basic attestation flow + * @dev Creates and verifies a simple attestation + */ function test_attestationFlow() external { bytes memory data = encodeData("exampleId", "exampleField", block.timestamp); @@ -43,6 +52,10 @@ contract SignProtocolExample is Test { _verifyAttestation(attestationId); } + /** + * @notice Creates a schema definition + * @return Schema memory The created schema + */ function _createSchema() internal view returns (Schema memory) { return Schema({ registrant: address(this), @@ -55,6 +68,11 @@ contract SignProtocolExample is Test { }); } + /** + * @notice Creates an attestation with the given data + * @param data The encoded attestation data + * @return Attestation memory The created attestation + */ function _createAttestation(bytes memory data) internal view returns (Attestation memory) { return Attestation({ schemaId: schemaId, @@ -70,7 +88,11 @@ contract SignProtocolExample is Test { }); } - function _verifyAttestation(uint64 attestationId) internal { + /** + * @notice Verifies an attestation's data + * @param attestationId The ID of the attestation to verify + */ + function _verifyAttestation(uint64 attestationId) internal view { Attestation memory storedAttestation = sp.getAttestation(attestationId); (string memory id, string memory field, uint256 timestamp) = decodeData(storedAttestation.data); @@ -79,15 +101,31 @@ contract SignProtocolExample is Test { assertEq(timestamp, block.timestamp); } + /** + * @notice Returns the schema data definition + * @return string memory The schema data in JSON format + */ function _getSchemaData() internal pure returns (string memory) { return "{\"name\":\"ExampleSchema\",\"data\":[{\"name\":\"id\",\"type\":\"string\"}," "{\"name\":\"field\",\"type\":\"string\"},{\"name\":\"timestamp\",\"type\":\"uint256\"}]}"; } + /** + * @notice Encodes the attestation data + * @param id The ID field + * @param field The example field + * @param timestamp The timestamp + * @return bytes memory The encoded data + */ function encodeData(string memory id, string memory field, uint256 timestamp) public pure returns (bytes memory) { return abi.encode(id, field, timestamp); } + /** + * @notice Decodes the attestation data + * @param data The encoded data + * @return tuple containing decoded id, field, and timestamp + */ function decodeData(bytes memory data) public pure returns (string memory, string memory, uint256) { return abi.decode(data, (string, string, uint256)); } From 0654173de453840bb79d3146cbef13f7461c4d63 Mon Sep 17 00:00:00 2001 From: kaiadachi Date: Sat, 30 Nov 2024 00:16:16 +0900 Subject: [PATCH 3/3] fix: replace bun with npm to resolve github dependencies feature/add_sample --- .github/workflows/test.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 06f84f7..43b7753 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -26,11 +26,8 @@ jobs: with: version: nightly - - name: Setup Bun - uses: oven-sh/setup-bun@v2 - - name: Install dependencies - run: bun i + run: npm install - name: Run Forge build run: |