Giter Site home page Giter Site logo

swapynetwork / electron-metamask-boilerplate Goto Github PK

View Code? Open in Web Editor NEW
96.0 9.0 72.0 203 KB

Easily create a desktop version of your dApp using Electron (Muon) + MetaMask.

JavaScript 71.90% HTML 28.10%
muon electron dapp metamask ethereum electron-builder

electron-metamask-boilerplate's Introduction

[DISCONTINUED] Electron (Muon) + Metamask Boilerplate

⚠️ This project is no longer maintained. Please beware of this before starting using it!

Easily create a desktop version of your dApp using Electron (Muon) + MetaMask.

Introduction

When it comes to decentralized apps, the more the users don’t have to rely on third parties in order to use the application, the more trust they can put in.

Serving your frontend through cloud servers is great because anyone using a browser with MetaMask can access it, but you’re still depending on third-parties to host the code. This means that if the servers are down, *e.g.*maintenance or a DDoS attack, the users won’t be able to use the application.

On the other hand, if you created an offline version of your dApp using Electron, you wouldn't be able to use MetaMask to manage the wallets... until now.

Using this boilerplate, you'll be able to wrap your dApp code within a Muon application that includes MetaMask directly from Chrome Store!

How it works

This boilerplate is built on top of Muon, which is a security-focused fork from Electron with support for Chrome extensions. The boilerplate sets up a Muon application and injects the MetaMask extension.

  1. The npm scripts download the MetaMask code from the Chrome Store to the local folder.
  2. The Muon application renders your local HTML and injects the MetaMask content scripts.
  3. Your frontend code should use chrome.ipcRenderer.send('message') to communicate with Muon's main process and trigger the MetaMask popups.
  4. MetaMask handles all the wallet management side of the application. So you don't have to deal directly with user's private keys.
  5. Electron Builder packs and generates installers for Linux, Windows and MacOS.

How to use

Fork the repo and clone it to your local machine

git clone https://github.com/YourAccount/electron-metamask-boilerplate.git

Include your dApp frontend repository as a submodule

git submodule add https://github.com/your-awesome-company/dapp.git app

It's recommended to add the submodule with https instead of ssh. This way you can easily clone and install both the parent and the children repos (check this link).


Now the folder structure should be something like:

electron-metamask-boilerplate
  -> node_modules/

  -> extensions/

    --> metamask/

  -> main.js

  -> package.json
  
  ->  [ ... ]

  -> app - (your git repository)
    --> node_modules/

    --> src/

    --> dist/

    --> package.json

    --> [ ... ]

Setting up your project

Usually the frontend code that is rendered in the browser resides within a dist folder, so we recommend that you build your project to a dist folder so you can use our configuration without having to alter too many things.

Now, edit main.js to properly read index.html from your app's folder.

In development mode Muon will read the extensions file from the parent folder at /extensions/metamask, but when bundling to production, Electron Builder will expect the same folder at app/dist/extensions/metamask (this can be changed in extensions.js).

We've set up npm scripts to download MetaMask from Chrome Store. When in development run npm run download.metamask.dev. In production npm run download.metamask.prod.

Once you're ready run npm start for a development server.

Managing MetaMask's popups

Unfortunately, the built-in communication between the MetaMask scripts to trigger the UI doesn't work on Muon. In order to make the popups work, you'll need to intercept the web3 calls and manually send a message to Muon.

Check out this function.

Building the installers

Electron Builder depends on both package.jsons to generate the installers. For more info, check the docs.

External Resources

swapy-exchange-electron-wrapper

Swapy's article on how we did the Electron + MetaMask integration

Aragon's implementation

electron-metamask-boilerplate's People

Contributors

icaroharry avatar tuliobraga avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

electron-metamask-boilerplate's Issues

Web3 is not defined

Occurs on both Windows and Ubuntu. Just attempting to test out the base package by running:

npm install
npm run download.metamask.dev
npm start

I get the error:

Web3 is not defined

in the console.

The example application doesn't work as a result of this.

install error

root@alex-All-Series:~/electron-metamask-boilerplate# sudo npm install electron --unsafe-perm=true

> [email protected] postinstall /home/alex/electron-metamask-boilerplate/node_modules/electron
> node install.js

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/7zip-bin-win):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"win32","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/7zip-bin-mac):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ [email protected]
updated 1 package and audited 1601 packages in 4.335s
found 44 vulnerabilities (8 low, 28 moderate, 8 high)
  run `npm audit fix` to fix them, or `npm audit` for details
root@alex-All-Series:~/electron-metamask-boilerplate# sudo node main
/home/alex/electron-metamask-boilerplate/main.js:64
app.on('ready', createWindow);
    ^

TypeError: Cannot read property 'on' of undefined
    at Object.<anonymous> (/home/alex/electron-metamask-boilerplate/main.js:64:5)
    at Module._compile (internal/modules/cjs/loader.js:707:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
    at Module.load (internal/modules/cjs/loader.js:605:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
    at Function.Module._load (internal/modules/cjs/loader.js:536:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:760:12)
    at startup (internal/bootstrap/node.js:303:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:872:3)
root@alex-All-Series:~/electron-metamask-boilerplate# sudo node setup
/bin/sh: 1: electron: not found
process exited with code 127
root@alex-All-Series:~/electron-metamask-boilerplate# 

making build

Uncaught Exception:
TypeError: Cannot read property 'load' of undefined
at /tmp/.mount_firstB5axfdY/resources/app.asar/extensions.js:21:38
at ELECTRON_ASAR.js:500:9
at FSReqWrap.wrapper [as oncomplete] (fs.js:664:17)

Uncaught error with MetaMask manifest.json on launch

Looks like the path to the manifest.json file is malformed, I get the following error on launch:

Uncaught Exception:
Error: ENOENT: no such file or directory, open '/Users/will/Documents/electron-metamask-boilerplate/extensions/metamask//manifest.json'

Note metamask//manifest.json. My system info is:

  • Node 8.9.4
  • NPM 5.7.1
  • OSX 10.13.2

npm run build fails

npm run build

fails with the following error with no changes to the project:

Error: Exit code: 1. Command failed: /home/alex/.cache/electron-builder/app-builder/app-builder-v0.6.0-x64/app-builder icon --format set --root /home/alex/Work/electron-metamask-boilerplate/app/dist/assets/icons --root /home/alex/Work/electron-metamask-boilerplate --input your-app/dist/assets/icons --input /home/alex/Work/electron-metamask-boilerplate/your-app/dist/assets/icons/swapy-exchange.icns --input /home/alex/Work/electron-metamask-boilerplate/node_modules/electron-builder-lib/templates/linux/electron-icons

download metamask script not working

I followed the instructions in the readme, and when i run npm run download.metamask.dev I get the error "cannot find module '..' " - the file structure that I get upon forking and cloning this repo does not match the expected file structure given in the readme. It has no extensions directory, but does include an extensions.js file, which seems to interact with metamask, but is not working.

When running the dev server (since no metamask extension is downloaded), I get the following error:

Error: ENOENT: no such file or directory, open '//extensions/metamask//manifest.json'

presumably because i don't have the metamask extension downloaded correctly.

Any help would be much appreciated! Maybe i'm running the scripts wrong?

Web3 Not Defined in Window.load

web3 is defined in the your-app submodule
After adding my submodule which is a reactjs -bootstrap project
it attempts to obtain a handle on web3 to no avail.
Adding the web3 script to index.html does not help.
The following is printed to my console:

Non-Ethereum browser detected. You should consider trying MetaMask!

        window.addEventListener('load', async () => {
            // Modern dapp browsers...
            if (window.ethereum) {
                window.web3 = new Web3(ethereum);
                try {
                    // Request account access if needed
                    this.setState({pendingEnable: true})
                    ethereum.enable().then((res) => {
                        console.log(`Connect Result : ${res}`)
                        this.setState({web3Obj: window.web3, pendingEnable: false, acceptedEnable: true, loading: false})
                    });
                } catch (error) {
                    // User denied account access...
                    this.setState({web3Obj: null, pendingEnable: false, acceptedEnable: false, loading: false})
                }
            }
            // Legacy dapp browsers...
            else if (typeof window.web3 !== 'undefined') {
                window.web3 = new Web3(web3.currentProvider);
                this.setState({web3Obj: window.web3, pendingEnable: false, loading: false})
            }
            // Non-dapp browsers...
            else {
                console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
                this.setState({web3Obj: null, pendingEnable: false, loading: false})

            }
        });
    }```

"require not defined" in index.js

Hey,

I'm getting "require not defined" in index.js if i try to require a node module.
I only added require, rest is untouched and working.
Is this by design from Muon or a bug?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.