Skip to content

Commit

Permalink
feat: implement signing
Browse files Browse the repository at this point in the history
  • Loading branch information
eccentricexit committed Dec 30, 2020
1 parent dcec9a7 commit f8dbf5b
Show file tree
Hide file tree
Showing 7 changed files with 13,474 additions and 1,942 deletions.
40 changes: 40 additions & 0 deletions .eslintcr.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,44 @@ module.exports = {
},
},
],
// Rule Overrides
rules: {
'prefer-const': 2,
'arrow-body-style': [2, 'as-needed'],
curly: [2, 'multi'],
'padding-line-between-statements': [
2,
{ blankLine: 'never', prev: 'import', next: 'import' },
],
'no-useless-concat': 2,
'prefer-template': 2,

// unicorn
'unicorn/no-fn-reference-in-iterator': 0, // Allows [].map(func)
'unicorn/catch-error-name': [2, { name: 'err' }],
'unicorn/prevent-abbreviations': 'off',
'unicorn/no-abusive-eslint-disable': 'off',

// import
'import/no-unresolved': 2,
'import/named': 2,
'import/default': 2,
'import/namespace': 2,
'import/no-named-as-default': 2,
'import/no-named-as-default-member': 2,
'import/no-extraneous-dependencies': 2,
'import/newline-after-import': 2,
'import/no-named-default': 2,
'import/no-useless-path-segments': 2,

'react/jsx-indent': 0,
'react/jsx-curly-brace-presence': [2, 'never'],

// React
'react/prefer-stateless-function': 2,
'react/destructuring-assignment': [2, 'always'],
// hooks
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn',
},
}
5 changes: 5 additions & 0 deletions connectors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { InjectedConnector } from '@web3-react/injected-connector'

export const injected = new InjectedConnector({
supportedChainIds: [1, 5],
})
73 changes: 73 additions & 0 deletions hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { useState, useEffect } from 'react'
import { useWeb3React } from '@web3-react/core'

import { injected } from '../connectors'

export function useEagerConnect() {
const { activate, active } = useWeb3React()

const [tried, setTried] = useState(false)

useEffect(() => {
injected.isAuthorized().then((isAuthorized: boolean) => {
if (isAuthorized) {
activate(injected, undefined, true).catch(() => {
setTried(true)
})
} else {
setTried(true)
}
})
}, []) // intentionally only running on mount (make sure it's only mounted once :))

// if the connection worked, wait until we get confirmation of that to flip the flag
useEffect(() => {
if (!tried && active) {
setTried(true)
}
}, [tried, active])

return tried
}

export function useInactiveListener(suppress: boolean = false) {
const { active, error, activate } = useWeb3React()

useEffect((): any => {
const { ethereum } = window as any
if (ethereum && ethereum.on && !active && !error && !suppress) {
const handleConnect = () => {
console.log("Handling 'connect' event")
activate(injected)
}
const handleChainChanged = (chainId: string | number) => {
console.log("Handling 'chainChanged' event with payload", chainId)
activate(injected)
}
const handleAccountsChanged = (accounts: string[]) => {
console.log("Handling 'accountsChanged' event with payload", accounts)
if (accounts.length > 0) {
activate(injected)
}
}
const handleNetworkChanged = (networkId: string | number) => {
console.log("Handling 'networkChanged' event with payload", networkId)
activate(injected)
}

ethereum.on('connect', handleConnect)
ethereum.on('chainChanged', handleChainChanged)
ethereum.on('accountsChanged', handleAccountsChanged)
ethereum.on('networkChanged', handleNetworkChanged)

return () => {
if (ethereum.removeListener) {
ethereum.removeListener('connect', handleConnect)
ethereum.removeListener('chainChanged', handleChainChanged)
ethereum.removeListener('accountsChanged', handleAccountsChanged)
ethereum.removeListener('networkChanged', handleNetworkChanged)
}
}
}
}, [active, error, suppress, activate])
}
Loading

0 comments on commit f8dbf5b

Please sign in to comment.