Skip to content

Instantly share code, notes, and snippets.

View duartefdias's full-sized avatar

Duarte Dias duartefdias

View GitHub Profile
<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>
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;
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];
function checkIfWalletConnected() {
if (window.ethereum.request({ method: 'eth_accounts' }).then(function (accounts) {
if (accounts.length > 0) {
connected = true;
buyerAddress = accounts[0];
} else {
connected = false;
}
})
) {
// 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}`;
export const state = () => ({
metamaskConnected: false,
ethereum: null,
accounts: []
})
export const mutations = {
setMetamaskConnected(state, value) {
state.metamaskConnected = value
},
// 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) => {
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
// 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
});
});
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 => {