-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<ADDRESS, BOOLEAN> # Stores group membership status | ||
|
@@ -1062,7 +1062,7 @@ CLASS EthereumStyleAuthWithTimeWindow: | |
sessions: MAP<ADDRESS, MAP<BYTES32, INTEGER>> # Stores session expiration times | ||
sessionHashes: MAP<ADDRESS, MAP<BYTES32, STRING>> # Maps session IDs to IPFS hashes | ||
``` | ||
Check failure on line 1064 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
Check failure on line 1064 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
|
||
``` | ||
```text | ||
Check failure on line 1065 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
Check failure on line 1065 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
|
||
# State variables for failed attempt tracking | ||
failedAttempts: MAP<ADDRESS, INTEGER> | ||
lastFailedAttemptTime: MAP<ADDRESS, TIMESTAMP> | ||
|
@@ -1076,7 +1076,7 @@ CLASS EthereumStyleAuthWithTimeWindow: | |
CONSTANT MAX_FAILED_ATTEMPTS = 5 | ||
CONSTANT LOCKOUT_DURATION = 15 minutes | ||
``` | ||
Check failure on line 1078 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
Check failure on line 1078 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
|
||
``` | ||
```text | ||
Check failure on line 1079 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
Check failure on line 1079 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
|
||
# Constructor: Initializes the contract state | ||
CONSTRUCTOR(): | ||
groupMembers = EMPTY_MAP() | ||
|
@@ -1086,7 +1086,7 @@ CLASS EthereumStyleAuthWithTimeWindow: | |
allowedPurposes["Authenticate to create session"] = TRUE | ||
EMIT GroupMemberAdded(admin) | ||
``` | ||
Check failure on line 1088 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
Check failure on line 1088 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
|
||
``` | ||
```text | ||
Check failure on line 1089 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
Check failure on line 1089 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
|
||
# 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) | ||
``` | ||
Check failure on line 1097 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
Check failure on line 1097 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
|
||
``` | ||
```text | ||
Check failure on line 1098 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
Check failure on line 1098 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
|
||
# 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) | ||
``` | ||
Check failure on line 1107 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
Check failure on line 1107 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
|
||
``` | ||
```text | ||
Check failure on line 1108 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
Check failure on line 1108 in vac/raw/eth-demls.md GitHub Actions / lintFenced code blocks should be surrounded by blank lines
|
||
# 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,23 +1227,23 @@ 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") | ||
REQUIRE(newExpirationBlock > CURRENT_BLOCK_NUMBER(), "New expiration block must be in the future") | ||
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") | ||
DELETE sessions[TRANSACTION_SENDER()][sessionId] | ||
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,32 +1341,32 @@ 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) | ||
``` | ||
|
||
### Events | ||
|
||
``` | ||
```text | ||
EVENT AuthenticationSuccessful(user: ADDRESS, purpose: STRING) | ||
EVENT GroupMemberAdded(member: ADDRESS) | ||
EVENT GroupMemberRemoved(member: ADDRESS) | ||
|