Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jan 22, 2020
1 parent 8e05d50 commit a49e285
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "server.js",
"scripts": {
"start": "node .",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "standard --fix server.js public/js/main.js"
},
"author": "",
"license": "MIT",
Expand Down
32 changes: 19 additions & 13 deletions public/js/main.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// elements

const loginButton = document.getElementById('login')
const logoutButton = document.getElementById('logout')
const verifyButton = document.getElementById('verify')
const output = document.getElementById('output')

// provider

const authereum = new Authereum('kovan')
const provider = authereum.getProvider()
const web3 = new Web3(provider)

// events

loginButton.addEventListener('click', async (event) => {
event.preventDefault()

Expand All @@ -27,33 +33,33 @@ verifyButton.addEventListener('click', async (event) => {
log(res)
})

;(() => {
loginCheck()
})();
loginCheck()

// helpers

async function login() {
async function login () {
if (!await authereum.isAuthenticated()) {
await authereum.login()
}

await loginCheck()
}

async function loginCheck() {
async function loginCheck () {
const loggedIn = await authereum.isAuthenticated()
log({loggedIn})
log({ loggedIn })

loginButton.style.display = loggedIn ? 'none' : 'inline-block'
logoutButton.style.display = loggedIn ? 'inline-block' : 'none'
verifyButton.style.display = loggedIn ? 'inline-block' : 'none'
verifyButton.style.display = loggedIn ? 'inline-block' : 'none'
}

async function logout() {
async function logout () {
await authereum.logout()
await loginCheck()
}

async function signChallenge() {
async function signChallenge () {
const res = await fetch('/challenge')
const json = await res.json()

Expand All @@ -74,24 +80,24 @@ async function signChallenge() {
localStorage.setItem('token', token)
}

async function verifyToken() {
async function verifyToken () {
const token = localStorage.getItem('token')
if (!token) {
return {error: 'token not set'}
return { error: 'token not set' }
}

const res = await fetch('/verify', {
headers: {
'content-type': 'application/json',
'Authorization': `Bearer ${token}`
Authorization: `Bearer ${token}`
}
})

const json = await res.json()
return json
}

function log(content) {
function log (content) {
if (typeof content === 'object') {
content = JSON.stringify(content, null, 2)
}
Expand Down
35 changes: 17 additions & 18 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ app.use(jwt({
const challenge = 'sign this string'

app.get('/challenge', (req, res) => {
res.json({challenge})
res.json({ challenge })
})

app.get('/verify', async (req, res) => {
const { address, signature } = req.auth

const data = '0x'+Buffer.from(challenge).toString('hex')
const data = '0x' + Buffer.from(challenge).toString('hex')
const verified = await verifySignature(address, data, signature)
res.json({verified})
res.json({ verified })
})

const port = process.env.PORT || 8000
Expand All @@ -34,30 +34,30 @@ app.listen(port, () => {
console.log(`Listening on port ${port}`)
})

async function verifySignature(account, data, signature) {
async function verifySignature (account, data, signature) {
const eip1271Abi = [
{
"constant": true,
"inputs": [
constant: true,
inputs: [
{
"name": "_messageHash",
"type": "bytes"
name: '_messageHash',
type: 'bytes'
},
{
"name": "_signature",
"type": "bytes"
name: '_signature',
type: 'bytes'
}
],
"name": "isValidSignature",
"outputs": [
name: 'isValidSignature',
outputs: [
{
"name": "magicValue",
"type": "bytes4"
name: 'magicValue',
type: 'bytes4'
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
payable: false,
stateMutability: 'view',
type: 'function'
}
]

Expand All @@ -69,4 +69,3 @@ async function verifySignature(account, data, signature) {

return verified
}

0 comments on commit a49e285

Please sign in to comment.