This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div class="m-4 max-w-lg rounded overflow-hidden shadow-lg bg-white"> | |
<div class="p-4 max-w-xl" v-if="!connected" :key="updateModal"> | |
<h2 class="text-xl pb-9">Select one of the bellow options to perform a cryptocurrency transation via the ethereum test network.</h2> | |
<div class="flex space-x-4"> | |
<button @click="checkWalletConnected" class="py-2 px-8 rounded button-custom flex"><img src="@/assets/logos/metamask-logo.png" class="w-6 mr-3" alt=""> Metamask</button> | |
<button class="py-2 px-8 rounded button-custom disabled-custom flex"><img src="@/assets/logos/coinbase-logo.svg" class="w-6 mr-3" alt=""> Coinbase wallet</button> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makePaymentRequest(buyerAddress, sellerAddress, itemPriceInWei) { | |
// Start wallet payment process | |
window.ethereum.request({ method: 'eth_sendTransaction', params: [{ from: buyerAddress, to: sellerAddress, value: itemPriceInWei }] }) | |
.then(response => { | |
console.log(response); | |
return true; | |
}) | |
.catch(error => { | |
console.log(error); | |
return false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function connectWallet() { | |
if (window.ethereum) { | |
console.log('MetaMask is installed'); | |
window.web3 = new Web3(window.ethereum); | |
window.ethereum.send('eth_requestAccounts').then(function() { | |
// Get account address | |
window.ethereum.request({ method: 'eth_accounts' }) | |
.then(function(accounts) { | |
if (accounts.length > 0) { | |
buyerAddress = accounts[0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkIfWalletConnected() { | |
if (window.ethereum.request({ method: 'eth_accounts' }).then(function (accounts) { | |
if (accounts.length > 0) { | |
connected = true; | |
buyerAddress = accounts[0]; | |
} else { | |
connected = false; | |
} | |
}) | |
) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Process signed message | |
router.post('/:user/signature', (req, res) => { | |
// Get user from db | |
db.get('SELECT * FROM users WHERE address = ?', [req.params.user], (err, row) => { | |
if (err) { | |
console.error(err.message); | |
return res.status(500).send(err.message); | |
} | |
if (row) { | |
const msg = `Nonce: ${row.nonce}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const state = () => ({ | |
metamaskConnected: false, | |
ethereum: null, | |
accounts: [] | |
}) | |
export const mutations = { | |
setMetamaskConnected(state, value) { | |
state.metamaskConnected = value | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get user nonce | |
router.get('/:wallet_address/nonce', (req, res) => { | |
// Check if user exists | |
// ... search in database for user and returns its current nonce | |
}); | |
// Process signed message | |
router.post('/:user/signature', (req, res) => { | |
// Get user from db | |
User.findOne({wallet_address: req.params.user}, (err, user) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async connectToMetamask() { | |
try { | |
// Connect to metamask and get user accounts | |
const accounts = await this.$store.getters['metamask/ethereum'].request({ method: 'eth_requestAccounts' }); | |
// Update vuex store | |
this.$store.commit('metamask/setMetamaskConnected', true) | |
this.$store.commit('metamask/setAccounts', accounts) | |
// Check if user is registered, if not, register them |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Test authenticated route | |
// Use this route to test if the front-end is able to access this route | |
// Front-end needs to pass the token in the request header (header name: "Authorization") | |
router.get('/authenticated/test', passport.authenticate('jwt', { session: false }), (req, res) => { | |
console.log('Authentication successful'); | |
res.json({ | |
message: 'Successfully authenticated', | |
user: req.user | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const JwtStrategy = require('passport-jwt').Strategy; | |
const ExtractJwt = require('passport-jwt').ExtractJwt; | |
const mongoose = require('mongoose'); | |
import User from '../models/users'; | |
const opts = {}; | |
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken(); | |
opts.secretOrKey = process.env.JWT_SECRET; | |
module.exports = passport => { |
NewerOlder