Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sapphire-contracts: Add EIP-4361 SIWE parser and base contract #332

Merged
merged 2 commits into from
Jul 19, 2024

Conversation

matevz
Copy link
Member

@matevz matevz commented Jul 12, 2024

Fixes #327

This PR:

  • adds contracts/SiweParser.sol, an on-chain parser for SIWE messages
  • adds contracts/auth/A13e.sol, an abstract interface for authenticated queries (an alternative to signed queries which will get deprecated in Add Viem v2 & Wagmi v2 packages, and remove Ethers dependency #303)
  • adds contracts/auth/SiweAuth.sol, an on-chain authenticator for SIWE.
  • tests, natspec docs + example

TODO:

  • make SiweParser self-contained (merge DateTime into SiweParser?) (this doesn't actually improve anything, just adds clutter)
  • use error constants in SiweParser and SiweAuth

Before (signed queries)

contract MyContract {
  address private _owner;

  modifier onlyOwner() {
    if (msg.sender != _owner) {
      revert("not allowed");
    }
    _;
  }

  constructor() {
    _owner = msg.sender;
  }

  function getSecretMessage() external view onlyOwner returns (string memory) {
    return "Very secret message";
  }
}

After (on-chain auth)

import {SiweAuth} from  "@oasisprotocol/sapphire-contracts/contracts/auth/SiweAuth.sol"

contract MyContract is SiweAuth {
  address private _owner;

  modifier onlyOwner(bytes calldata bearer) {
    if (authMsgSender(bearer) != _owner) {
      revert("not allowed");
    }
    _;
  }

  constructor(string memory domain) SiweAuth(domain) {
    _owner = msg.sender;
  }

  function getSecretMessage(bytes calldata bearer) external view onlyOwner(bearer) returns (string memory) {
    return "Very secret message";
  }
}

@matevz matevz added the contracts Pull requests that update sapphire-contracts label Jul 12, 2024
@matevz matevz requested a review from CedarMist July 12, 2024 10:51
@matevz matevz self-assigned this Jul 12, 2024
Copy link

netlify bot commented Jul 12, 2024

Deploy Preview for oasisprotocol-sapphire-paratime canceled.

Name Link
🔨 Latest commit 679fc0e
🔍 Latest deploy log https://app.netlify.com/sites/oasisprotocol-sapphire-paratime/deploys/669a677281915f0008633571

@matevz matevz force-pushed the matevz/sapphire-contracts-auth branch 3 times, most recently from 9411e94 to b3bf4d0 Compare July 12, 2024 11:11
@matevz matevz force-pushed the matevz/sapphire-contracts-auth branch from b3bf4d0 to 726e800 Compare July 12, 2024 15:13
@matevz matevz changed the title sapphire-contracts: Add SIWE Authentication helpers sapphire-contracts: Add EIP-4361 SIWE Authentication helpers Jul 12, 2024
@matevz matevz force-pushed the matevz/sapphire-contracts-auth branch from b028bb7 to aeac833 Compare July 16, 2024 10:24
@matevz matevz changed the title sapphire-contracts: Add EIP-4361 SIWE Authentication helpers sapphire-contracts: Add EIP-4361 SIWE parser and base contract Jul 16, 2024
@matevz matevz requested a review from aefhm July 16, 2024 10:27
Copy link
Contributor

@aefhm aefhm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super clear test cases!

contracts/contracts/SiweParser.sol Show resolved Hide resolved
contracts/test/auth.ts Outdated Show resolved Hide resolved
contracts/test/auth.ts Outdated Show resolved Hide resolved
contracts/contracts/auth/SiweAuth.sol Show resolved Hide resolved
Copy link
Contributor

@aefhm aefhm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments aren't blocking.

@matevz matevz force-pushed the matevz/sapphire-contracts-auth branch from aeac833 to 2bb9f58 Compare July 19, 2024 10:38
@matevz matevz force-pushed the matevz/sapphire-contracts-auth branch 2 times, most recently from 0117269 to 97efc78 Compare July 19, 2024 12:56
@matevz matevz force-pushed the matevz/sapphire-contracts-auth branch from 97efc78 to 679fc0e Compare July 19, 2024 13:17
@matevz matevz merged commit ff1b8f9 into main Jul 19, 2024
15 checks passed
@matevz matevz deleted the matevz/sapphire-contracts-auth branch July 19, 2024 13:26
github-actions bot added a commit that referenced this pull request Jul 19, 2024
…tevz/sapphire-contracts-auth

sapphire-contracts: Add EIP-4361 SIWE parser and base contract ff1b8f9
github-actions bot added a commit that referenced this pull request Jul 19, 2024
…atevz/sapphire-contracts-auth

sapphire-contracts: Add EIP-4361 SIWE parser and base contract ff1b8f9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contracts Pull requests that update sapphire-contracts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sapphire-contracts: Add SIWE parser
2 participants