Ilan
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # rLogin - the new authentication libraries for blockchain based apps In this series of three articles, we are going to build a decentralized application with a back-end. We will use rLogin suite, developed by [RIF Identity](https://developers.rsk.co/rif/identity/) team, which will allow us to build a secure, reliable and scalable application from scratch. ![](https://i.imgur.com/6RqIUvm.png) The first article is an introduction and explains **how to connect to the user's digital wallet**. It also shows how to perform different operations and interactions with the wallet: send transactions, digitally sign messages, and so on. This will be a pure front-end app at first. Users will be able to join the application with any RSK or Ethereum compatible wallet, even using a mobile phone wallet. This will be powered by [rLogin](https://github.com/rsksmart/rLogin) library. In the second article, we will build a back-end for this app, using a [Express DID Auth](https://github.com/rsksmart/express-did-auth) library, which allows us to **authenticate users using their wallet address as a unique identifier**. The library enables a secure and simple authentication model where the user needs to digitally sign a message with their wallet. This optimizes the user experience avoiding the need to remember passwords, or receive any emails to validate identity. Finally, in the third article, we will work with something a little bit more complex: the [RIF Data Vault](https://github.com/rsksmart/rif-data-vault). This service allows users to have **a user-centric cloud storage for free, where saved files are encrypted on the client side**. We make use of the user's wallet to encrypt files, empowering users with real privacy for their information. This means that even the Data Vault server cannot decrypt your data. The service allows us, for example, to store Verifiable Credentials that can be requested at the time as part of authentication. rLogin and DID Auth integrate this service and make use of this feature, enabling developers to do things that were never possible in the crypto world. ## How to connect to the user's digital wallet In this first article, as mentioned before, we are going to connect to the user's wallet from a React.js application. To summarize, we are going to: 1. Create a front-end app 2. Add a _login button_ that requests user's account and wallet access 3. Describe some operations that can be done with user's wallet Some tiny prerequisites: - React.js for the front-end - A compatible wallet - you can try with any of [these supported providers](https://github.com/rsksmart/rlogin#features) -- we recommend trying this with a browser wallet and a mobile wallet, to have both experiences **Let's start!** The app we are going to build is published in this repo: https://github.com/rsksmart/rlogin-sample ### Create the front-end ``` npx create-react-app rlogin-sample cd rlogin-sample ``` We have a React.js front-end already created! If you want you can first clean-up a little bit of the UI removing React logo and such. ### Install rLogin Let's install [rLogin](https://github.com/rsksmart/rLogin). With this library, we are going to display a pop-up that lets users pick any compatible wallet of choice. ``` yarn add @rsksmart/rlogin @walletconnect/web3-provider ``` With a quick setup, we can enable users to connect to our app using any browser wallet or even any mobile wallet. Connect to RSK Testnet with your wallet (or pick another set of supported networks in the setup). I recommend trying first with [Nifty browser wallet](https://chrome.google.com/webstore/detail/nifty-wallet/jbdaocneiiinmjbjlgalhcelgbejmnid) that has an easy setup for RSK (just switch network in the top left corner of the wallet), and [Defiant mobile wallet](https://defiantapp.tech/) that support RSK by default. ```javascript import RLogin, { RLoginButton } from '@rsksmart/rlogin' import WalletConnectProvider from '@walletconnect/web3-provider' import { useState } from 'react'; import './App.css'; // construct rLogin pop-up in DOM const rLogin = new RLogin({ cachedProvider: false, // change to true to cache user's wallet choice providerOptions: { // read more about providers setup in https://github.com/web3Modal/web3modal/ walletconnect: { package: WalletConnectProvider, // setup wallet connect for mobile wallet support options: { rpc: { 31: 'https://public-node.testnet.rsk.co' // use RSK public nodes to connect to the testnet } } } }, supportedChains: [31] // enable rsk testnet network }) function App() { const [provider, setProvider] = useState(null) const [account, setAccount] = useState(null) // display pop up const connect = () => rLogin.connect() .then(({ provider }) => { // the provider is used to operate with user's wallet setProvider(provider) // request user's account provider.request({ method: 'eth_accounts' }).then(([account]) => setAccount(account)) }) return ( <div className="App"> <RLoginButton onClick={connect}>Connect wallet</RLoginButton> <p>wallet address: {account}</p> </div> ); } export default App; ``` We now have a simple button that, when clicked, displays a pop-up. This pop-up will let user pick their wallet of choice. Once the user accepts, their wallet address will be displayed in the front. Easy!! ![](https://i.imgur.com/jkVPhdk.png) ### Operate user's wallet We are now connected to our user's wallet and we want to do three things: 1. Get user's balance 2. Send blockchain transactions 3. Digitally sign messages The last two operations will prompt a notification in the user's wallet asking for permission to do so. Let's go for it! #### Get user's balance This is one of the most common queries we can do to the blockchain. To do so we are going to execute another RPC request. I also added some code for setting the tx hash to React state. ```javascript const [balance, setBalance] = useState('') const getBalance = () => provider.request({ method: 'eth_getBalance', params: [account] }).then(setBalance) ``` ```jsx <button onClick={getBalance} disabled={!account}>get balance</button> <p>balance: {balance}</p> ``` Get some funds in the [RSK Testnet faucet](https://faucet.rsk.co/) and try it out. #### Sending transactions As an example, we are just going to send a basic transaction with no data. It is simple: ```javascript const [txHash, setTxHash] = useState('') const faucetAddress = '0x88250f772101179a4ecfaa4b92a983676a3ce445' // giving back some funds const sendTransaction = () => provider.request({ method: 'eth_sendTransaction', params: [{ from: account, to: faucetAddress, value: '100000' }] }).then(setTxHash) ``` ```jsx <button onClick={sendTransaction} disabled={!account}>send transaction</button> <p>txHash: {txHash}</p> ``` As mentioned before, this will notify the user asking for approval in their wallet. Once the user accepts, the method will return the transaction hash. This is the unique identifier of the transaction and can be searched in the [RSK Testnet explorer](https://explorer.testnet.rsk.co). #### Digitally sign messages This is pretty similar! We just need to request another RPC request, `personal_sign`: ```javascript const [signature, setSignature] = useState('') const message = 'Welcome to RIF Identity suite!!!' const personalSign = () => provider.request({ method: 'personal_sign', params: [message, account] }).then(setSignature) ``` ```jsx <button onClick={personalSign} disabled={!account}>sign message</button> <p>signature: {signature}</p> ``` ![](https://i.imgur.com/rXk5AG4.png) #### Performing complex transactions Complex transactions, like smart contract operations, can be performed with any Web3 client, such as [`web3.js`](https://github.com/ChainSafe/web3.js), [`ethers.js`](https://github.com/ethers-io/ethers.js/) or [`ethjs`](https://github.com/ethjs/ethjs). For example, you can instantiate a `web3.js` object with: ```javascript import Web3 from 'web3' const web3 = new Web3(provider) ``` #### Other operations Sending transactions and signing messages are just the two most common operations, but with rLogin provider, you are able to perform any [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) operation: - Send transactions - Operate with smart contracts - Sign messages ([EIP-191](https://eips.ethereum.org/EIPS/eip-191)) - Sign typed data ([EIP-712](https://eips.ethereum.org/EIPS/eip-712)) - Request user's accounts - Request user's connected network - Listen to network or accounts changes - Listen to user disconnection ## The rLogin library rLogin is a front-end library that was built with one main purpose: to reduce the technical gap for new crypto apps users. Most of our work was put in building a simple and intuitive front-end, that in future versions will include tutorials for each wallet provider. We support RSK Mainnet and RSK Testnet, and also Ethereum and its testnets. The objective of rLogin is to maintain a set of quality assurance tests, making the library a reliable front-end tool to be used along different kind of applications. rLogin adds features to [`web3modal`](https://github.com/web3Modal/web3modal/), the Ethereum login tool. For setting up rLogin, you can pick any providers that are supported by `web3modal`, and, in addition, the library also allows setting up a challenge-response authentication model -- this will be explained in the following chapter. **Thanks!! See you in the next edition. We are going to set up a back-end for our dApp!** ## Useful links * RIF Identity docs: https://developers.rsk.co/rif/identity/ * rLogin repo: https://github.com/rsksmart/rlogin * rLogin docs: https://developers.rsk.co/rif/identity/rlogin/ * rLogin sample apps: https://github.com/rsksmart/rlogin-sample-apps * `web3modal`: https://github.com/web3Modal/web3modal/ * EIP-1193 - Ethereum Provider JavaScript API: https://eips.ethereum.org/EIPS/eip-1193 * RIF landing: https://rifos.org Ilan Olkies Github: [@ilanolkies](https://github.com/ilanolkies) Twitter: [@ilanolkies](https://twitter.com/ilanolkies)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully