diff --git a/vac/raw/eth-demls.md b/vac/raw/eth-demls.md index 3053a430..83a53979 100644 --- a/vac/raw/eth-demls.md +++ b/vac/raw/eth-demls.md @@ -1038,7 +1038,7 @@ allowing the dynamic management of group membership. ### SIWE Message Structure -``` +```text STRUCTURE SIWEMessage: domain: STRING # The domain requesting the signing address: ADDRESS # The user's Ethereum address @@ -1053,7 +1053,7 @@ STRUCTURE SIWEMessage: This contract handles authentication, group management, and session management -``` +```text CLASS EthereumStyleAuthWithTimeWindow: # State variables groupMembers: MAP # Stores group membership status @@ -1062,7 +1062,7 @@ CLASS EthereumStyleAuthWithTimeWindow: sessions: MAP> # Stores session expiration times sessionHashes: MAP> # Maps session IDs to IPFS hashes ``` -``` +```text # State variables for failed attempt tracking failedAttempts: MAP lastFailedAttemptTime: MAP @@ -1076,7 +1076,7 @@ CLASS EthereumStyleAuthWithTimeWindow: CONSTANT MAX_FAILED_ATTEMPTS = 5 CONSTANT LOCKOUT_DURATION = 15 minutes ``` -``` +```text # Constructor: Initializes the contract state CONSTRUCTOR(): groupMembers = EMPTY_MAP() @@ -1086,7 +1086,7 @@ CLASS EthereumStyleAuthWithTimeWindow: allowedPurposes["Authenticate to create session"] = TRUE EMIT GroupMemberAdded(admin) ``` -``` +```text # Adds a new member to the group (admin only) FUNCTION addGroupMember(memberAddress: ADDRESS): REQUIRE(TRANSACTION_SENDER() == admin, "Only admin can perform this action") @@ -1095,7 +1095,7 @@ CLASS EthereumStyleAuthWithTimeWindow: groupMembers[memberAddress] = TRUE EMIT GroupMemberAdded(memberAddress) ``` -``` +```text # Removes a member from the group (admin only) FUNCTION removeGroupMember(memberAddress: ADDRESS): REQUIRE(TRANSACTION_SENDER() == admin, "Only admin can perform this action") @@ -1105,7 +1105,7 @@ CLASS EthereumStyleAuthWithTimeWindow: groupMembers[memberAddress] = FALSE EMIT GroupMemberRemoved(memberAddress) ``` -``` +```text # Transfers admin rights to a new address FUNCTION transferAdmin(newAdminAddress: ADDRESS): REQUIRE(TRANSACTION_SENDER() == admin, "Only current admin can transfer admin rights") @@ -1114,7 +1114,7 @@ CLASS EthereumStyleAuthWithTimeWindow: EMIT AdminTransferred(admin, newAdminAddress) admin = newAdminAddress ``` -``` +```text # Authenticates a user based on their SIWE message and signature FUNCTION authenticate( domain: STRING, @@ -1190,14 +1190,14 @@ CLASS EthereumStyleAuthWithTimeWindow: EMIT AuthenticationSuccessful(signer, purpose) RETURN TRUE ``` -``` +```text # Helper function to record failed authentication attempts FUNCTION recordFailedAttempt(signer: ADDRESS, currentTime: TIMESTAMP, reason: STRING): failedAttempts[signer] = failedAttempts[signer] + 1 lastFailedAttemptTime[signer] = currentTime EMIT AuthenticationFailed(signer, reason) ``` -``` +```text # Helper function to verify the purpose FUNCTION verifyPurpose(purpose: STRING) RETURNS (BOOLEAN): # Implement logic to verify that the purpose is valid and expected @@ -1207,7 +1207,7 @@ CLASS EthereumStyleAuthWithTimeWindow: ### Session management functions -``` +```text # Creates a new session for an authenticated user FUNCTION createSession(sessionId: BYTES32, expirationBlock: INTEGER, ipfsHash: STRING): REQUIRE(groupMembers[TRANSACTION_SENDER()] == TRUE, "Not a group member") @@ -1218,7 +1218,7 @@ CLASS EthereumStyleAuthWithTimeWindow: sessionHashes[TRANSACTION_SENDER()][sessionId] = ipfsHash EMIT SessionCreated(TRANSACTION_SENDER(), sessionId, expirationBlock) ``` -``` +```text # Retrieves the IPFS hash for a given session ID FUNCTION getSessionHash(sessionId: BYTES32) VIEW RETURNS (STRING): expirationBlock = sessions[TRANSACTION_SENDER()][sessionId] @@ -1227,7 +1227,7 @@ CLASS EthereumStyleAuthWithTimeWindow: RETURN sessionHashes[TRANSACTION_SENDER()][sessionId] ``` -``` +```text # Extends the expiration time of an existing session FUNCTION extendSession(sessionId: BYTES32, newExpirationBlock: INTEGER): REQUIRE(sessions[TRANSACTION_SENDER()][sessionId] != NULL, "Session does not exist") @@ -1235,7 +1235,7 @@ CLASS EthereumStyleAuthWithTimeWindow: sessions[TRANSACTION_SENDER()][sessionId] = newExpirationBlock EMIT SessionExtended(TRANSACTION_SENDER(), sessionId, newExpirationBlock) ``` -``` +```text # Ends a session, removing it from storage FUNCTION endSession(sessionId: BYTES32): REQUIRE(sessions[TRANSACTION_SENDER()][sessionId] != NULL, "Session does not exist") @@ -1243,7 +1243,7 @@ CLASS EthereumStyleAuthWithTimeWindow: DELETE sessionHashes[TRANSACTION_SENDER()][sessionId] EMIT SessionEnded(TRANSACTION_SENDER(), sessionId) ``` -``` +```text # Cleans up expired sessions for the calling user FUNCTION cleanUpExpiredSessions(): FOR EACH sessionId IN sessions[TRANSACTION_SENDER()]: @@ -1256,7 +1256,7 @@ CLASS EthereumStyleAuthWithTimeWindow: ### Client side functions -``` +```text # Generates a new SIWE message for authentication FUNCTION generateSIWEMessage(userAddress: ADDRESS): domain = "example.com" @@ -1268,14 +1268,14 @@ FUNCTION generateSIWEMessage(userAddress: ADDRESS): RETURN NEW SIWEMessage( domain, userAddress, uri, chainId, issuedAt, expirationTime, purpose) ``` -``` +```text # Signs a SIWE message with the user's private key FUNCTION signSIWEMessage(message: SIWEMessage, userAddress: ADDRESS): messageHash = HASH_STRUCTURED_DATA(message) signature = SIGN_MESSAGE_HASH(messageHash, userAddress) RETURN signature ``` -``` +```text # Main authentication function that calls the smart contract FUNCTION authenticate(): userAddress = GET_CURRENT_USER_ADDRESS() @@ -1299,13 +1299,13 @@ FUNCTION authenticate(): ELSE: RETURN "Authentication failed" ``` -``` +```text # Constants or configuration parameters AVERAGE_BLOCK_TIME = 12 # in seconds SESSION_DURATION_IN_SECONDS = 3600 # 1 hour SESSION_DURATION_IN_BLOCKS = ROUND(SESSION_DURATION_IN_SECONDS / AVERAGE_BLOCK_TIME) # 300 blocks ``` -``` +```text # Creates and stores a new session after successful authentication FUNCTION createAndStoreSession(userAddress: ADDRESS): sessionData = { @@ -1325,7 +1325,7 @@ FUNCTION createAndStoreSession(userAddress: ADDRESS): STORE_LOCALLY(sessionId) RETURN sessionId ``` -``` +```text # Restores a user's session using the stored session ID FUNCTION restoreUserSession(): storedSessionId = RETRIEVE_LOCALLY_STORED_SESSION_ID() @@ -1341,24 +1341,24 @@ FUNCTION restoreUserSession(): ELSE: RETURN "No stored session found" ``` -``` +```text # Encrypts session data for secure storage FUNCTION encryptSessionData(sessionData: OBJECT, userPublicKey: PUBLIC_KEY): encryptedData = ENCRYPT(JSON.stringify(sessionData), userPublicKey) RETURN encryptedData ``` -``` +```text # Decrypts session data retrieved from storage FUNCTION decryptSessionData(encryptedData: STRING, userPrivateKey: PRIVATE_KEY): decryptedData = DECRYPT(encryptedData, userPrivateKey) RETURN JSON.parse(decryptedData) ``` -``` +```text # Helper functions which hashes the SIWE message according to EIP-712 standards FUNCTION HASH_STRUCTURED_DATA(message: SIWEMessage): RETURN KECCAK256(ENCODED_STRUCTURED_DATA(message)) ``` -``` +```text # Helper functions which recovers the signer's address from a message hash and signature FUNCTION RECOVER_SIGNER(messageHash: BYTES32, signature: BYTES): RETURN ECRECOVER(messageHash, signature) @@ -1366,7 +1366,7 @@ FUNCTION RECOVER_SIGNER(messageHash: BYTES32, signature: BYTES): ### Events -``` +```text EVENT AuthenticationSuccessful(user: ADDRESS, purpose: STRING) EVENT GroupMemberAdded(member: ADDRESS) EVENT GroupMemberRemoved(member: ADDRESS)