-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(evmconnector): magic link magic link connector package * add all possible options * fix lint error * WIP * perf(evmConnector): make package as small as possible * remove exposed email * add cdn build compatibility * add cdn test * use evmProvider datatype * remove unused code * change output name * proper implementation of abstract connector * fix magic build issue * fix cjs build magic-sdk * magic-sdk build issue Co-authored-by: Erno <[email protected]>
- Loading branch information
1 parent
55f54e1
commit 108a8db
Showing
22 changed files
with
634 additions
and
38 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@moralisweb3/evm-magic-link-connector': major | ||
--- | ||
|
||
This package gives the ability to connect to EVM using magic link |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
REACT_APP_SERVER_URL = "" | ||
REACT_APP_APP_ID = "" | ||
REACT_APP_API_KEY = "" | ||
REACT_APP_MORALIS_SECRET = "" | ||
REACT_APP_MORALIS_SECRET = "" | ||
REACT_APP_MAGIC_LINK_KEY = "" |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,19 @@ import Moralis from 'moralis'; | |
import { Erc20Value, EvmNative } from '@moralisweb3/core'; | ||
import WalletConnectConnector from '@moralisweb3/evm-wallet-connect-connector'; | ||
import MetamaskConnector from '@moralisweb3/evm-metamask-connector'; | ||
import MagiclinkConnector from '@moralisweb3/evm-magic-link-connector'; | ||
|
||
// Register connector connect connector | ||
Moralis.Evm.connectors.register(WalletConnectConnector); | ||
Moralis.Evm.connectors.register(MagiclinkConnector); | ||
// Moralis.Evm.connectors.remove('wallet-connect'); | ||
|
||
export const Evm = () => { | ||
return ( | ||
<div> | ||
<h2>EVM</h2> | ||
<button onClick={() => Moralis.Evm.connect('metamask', { silent: false })}>Connect via metamask</button> | ||
<button onClick={() => Moralis.Evm.connect('magic-link', { email: '[email protected]', apiKey: process.env.REACT_APP_MAGIC_LINK_KEY, chainId: 3, newSession: true })}>Connect via magiclink</button> | ||
<button onClick={() => Moralis.Evm.connect('wallet-connect', { newSession: true })}> | ||
Connect via walletconnect | ||
</button> | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
function initMoralis() { | ||
Moralis.Evm.connectors.register(WalletConnectConnector); | ||
Moralis.Evm.connectors.register(MagiclinkConnector); | ||
|
||
Moralis.start({ | ||
serverUrl: MORALIS_SERVER_URL, | ||
|
@@ -33,6 +34,10 @@ function initDemo() { | |
return Moralis.Evm.connect('wallet-connect', {}); | ||
}); | ||
|
||
appendButton('Evm.connect / Magiclink', () => { | ||
return Moralis.Evm.connect('magic-link', { email: '[email protected]', apiKey: MAGIC_LINK_KEY, chainId: 3 }); | ||
}); | ||
|
||
appendButton('Evm.transferNative', () => { | ||
return Moralis.Evm.transferNative({ | ||
to: '0x295522b61890c3672D12eFbFf4358a6411CE996F', | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<title>🔵 UMD Demo - Moralis Test CDN</title> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>🔵 UMD Demo - Moralis Test CDN</title> | ||
<script src="../../../packages/moralis/dist/index.umd.js"></script> | ||
<script src="../../../packages/evmConnectors/EvmWalletconnectConnector/dist/index.umd.js"></script> | ||
<script src="../../../packages/evmConnectors/EvmMagiclinkConnector/dist/index.umd.js"></script> | ||
<script src="./env.js"></script> | ||
<script src="./demo.js"></script> | ||
|
||
<script src="../../../packages/moralis/dist/index.umd.js"></script> | ||
<script src="../../../packages/evmConnectors/EvmWalletconnectConnector/dist/index.umd.js"></script> | ||
<script src="./env.js"></script> | ||
<script src="./demo.js"></script> | ||
<style> | ||
body { | ||
font: 14px/1.4em Arial, Verdana, Serif; | ||
} | ||
button { | ||
margin: 5px; | ||
} | ||
</style> | ||
</head> | ||
|
||
<style> | ||
body {font: 14px/1.4em Arial, Verdana, Serif;} | ||
button {margin: 5px;} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<h1>Demo UMD - Moralis CDN</h1> | ||
|
||
<div id="demo"></div> | ||
</body> | ||
<body> | ||
<h1>Demo UMD - Moralis CDN</h1> | ||
|
||
<div id="demo"></div> | ||
</body> | ||
</html> |
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
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
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
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CHANGELOG.md | ||
|
||
# Output folders | ||
lib/**/* | ||
dist | ||
coverage/ | ||
lib.esm | ||
node_modules |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
...require('../../../.prettierrc'), | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @moralisweb3/evm-magic-link-connector |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "@moralisweb3/evm-magic-link-connector", | ||
"author": "Moralis", | ||
"version": "2.0.0-alpha.1", | ||
"license": "MIT", | ||
"private": false, | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"sideEffects": false, | ||
"scripts": { | ||
"format": "prettier . '**/*.+(js|ts|json)' --write", | ||
"format:check": "prettier . '**/*.+(js|ts|json)' --check", | ||
"lint": "eslint . --ext .js,.ts,.tsx,jsx", | ||
"clean": "rm -rf lib && rm -rf lib.esm && rm -rf tsconfig.tsbuildinfo", | ||
"build": "rollup -c", | ||
"dev": "rollup -c --watch" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.7.0", | ||
"prettier": "^2.5.1", | ||
"typescript": "^4.5.5" | ||
}, | ||
"dependencies": { | ||
"@moralisweb3/core": "^2.0.0-alpha.1", | ||
"@moralisweb3/evm-connector-utils": "^2.0.0-alpha.1", | ||
"magic-sdk": "7.0.0" | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/evmConnectors/EvmMagiclinkConnector/rollup.config.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import packageJson from './package.json'; | ||
import { commonJs, esm, umd } from '../../../rollup.config'; | ||
|
||
const umdExternalMap = { | ||
'@moralisweb3/core': 'Moralis.CoreLib', | ||
'@moralisweb3/evm-connector-utils': 'Moralis.EvmConnectorUtilsLib', | ||
}; | ||
|
||
export default [commonJs(packageJson), esm(packageJson), umd('MagiclinkConnector', packageJson, umdExternalMap)]; |
90 changes: 90 additions & 0 deletions
90
packages/evmConnectors/EvmMagiclinkConnector/src/EvmMagicLinkConnector.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import core, { | ||
EvmAddress, | ||
EvmChain, | ||
EvmConnection, | ||
EvmMagicLinkConnectorOptions, | ||
EvmProvider, | ||
MoralisCore, | ||
MoralisNetworkConnectorError, | ||
NetworkConnectorErrorCode, | ||
} from '@moralisweb3/core'; | ||
import { EvmAbstractConnector } from '@moralisweb3/evm-connector-utils'; | ||
import { EthNetworkConfiguration, Magic } from 'magic-sdk'; | ||
|
||
const DEFAULT_OPTIONS = { | ||
chainId: '0x1', | ||
}; | ||
|
||
export interface EvmMagiclinkConnectorConfig { | ||
core: MoralisCore; | ||
} | ||
|
||
/** | ||
* Connector for WalletConnect v1 | ||
*/ | ||
export class EvmMagiclinkConnector extends EvmAbstractConnector<EvmProvider, EvmMagicLinkConnectorOptions> { | ||
constructor(config: EvmMagiclinkConnectorConfig) { | ||
super({ | ||
name: 'magic-link', | ||
core: config.core, | ||
}); | ||
} | ||
|
||
protected async createProvider(options: EvmMagicLinkConnectorOptions): Promise<EvmProvider> { | ||
const magic = new Magic(options.apiKey, { | ||
network: EvmChain.create(options.chainId!).name as EthNetworkConfiguration, | ||
}); | ||
|
||
// Log out of any previous sessions | ||
if (options.newSession) { | ||
await this.cleanup(magic); | ||
} | ||
|
||
await magic.auth.loginWithMagicLink({ | ||
email: options.email, | ||
}); | ||
|
||
const provider = magic.rpcProvider; | ||
|
||
if (!provider) { | ||
throw new MoralisNetworkConnectorError({ | ||
code: NetworkConnectorErrorCode.NO_PROVIDER, | ||
message: 'Failed to create provider', | ||
}); | ||
} | ||
|
||
return provider; | ||
} | ||
|
||
protected async createConnection(_options: EvmMagicLinkConnectorOptions): Promise<EvmConnection> { | ||
const options = { ...DEFAULT_OPTIONS, ..._options }; | ||
|
||
this.logger.verbose('Connecting', { providedOptions: _options, options }); | ||
|
||
const provider = await this.getProvider(options); | ||
|
||
const [accounts, chainId] = await Promise.all([ | ||
provider.request({ method: 'eth_accounts' }) as Promise<string[]>, | ||
provider.request({ method: 'eth_chainId' }) as Promise<string>, | ||
]); | ||
|
||
return { | ||
provider: provider, | ||
chain: new EvmChain(chainId), | ||
account: accounts[0] ? new EvmAddress(accounts[0]) : null, | ||
}; | ||
} | ||
|
||
async cleanup(magic: Magic): Promise<void> { | ||
if (magic.user) { | ||
try { | ||
await magic.user.logout(); | ||
} catch (error) { | ||
this.logger.verbose('Failed to logout', { error }); | ||
} | ||
} | ||
} | ||
} | ||
|
||
const evmMagicLinkConnector = new EvmMagiclinkConnector({ core }); | ||
export default evmMagicLinkConnector; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import EvmMagiclinkConnector from './EvmMagicLinkConnector'; | ||
|
||
export default EvmMagiclinkConnector; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../../tsconfig.package.json", | ||
"compilerOptions": { | ||
"outDir": "./lib/", | ||
"declarationDir": "./lib/", | ||
"rootDir": "./src" | ||
}, | ||
"include": ["src/**/*", "types/**/*"] | ||
} |
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
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
Oops, something went wrong.