Skip to content

Commit

Permalink
Merge pull request #166 from nspcc-dev/feature/143-support_o3_wallet
Browse files Browse the repository at this point in the history
Add support for O3 wallet
  • Loading branch information
roman-khimov authored Mar 5, 2024
2 parents e1e7913 + f61fc24 commit 61bdc5d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ $ make docker/start

- [Neon wallet for Windows, Mac OS, Linux, Android and iOS](https://neon.coz.io/)
- [NeoLine wallet for chrome extension](https://neoline.io/)
- [O3 wallet](https://o3.network/#/wallet)
- [OneGate wallet (Mobile)](https://onegate.space/)
- [Aero web wallet](https://melanke.github.io/aero-beta/) (testnet only, see #133)

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@cityofzion/wallet-connect-sdk-react": "^3.2.0",
"@neongd/neo-dapi": "^1.2.2",
"bulma": "^0.9.4",
"neo3-dapi": "^1.0.1",
"react": "^17.0.2",
"react-bulma-components": "^4.1.0",
"react-copy-to-clipboard": "^5.1.0",
Expand Down
44 changes: 39 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Route,
Link,
useLocation,
useNavigate,
} from "react-router-dom";
import {
Navbar,
Expand All @@ -22,11 +23,13 @@ import api from './api';
import { useWalletConnect } from "@cityofzion/wallet-connect-sdk-react";
import { CopyToClipboardBlock } from './CopyToClipboardBlock';
import { BaseDapi } from '@neongd/neo-dapi';
import neo3Dapi from "neo3-dapi";
import 'bulma/css/bulma.min.css';
import './App.css';

export const App = () => {
const location = useLocation();
const navigate = useNavigate();
const wcSdk = useWalletConnect();
const dapi = window.OneGate ? new BaseDapi(window.OneGate) : null;
let [neolineN3, setNeolineN3] = useState(null);
Expand Down Expand Up @@ -155,7 +158,7 @@ export const App = () => {

useEffect(() => {
window.addEventListener("NEOLine.N3.EVENT.READY", loadNeolineN3);
return () => window.removeListener("NEOLine.N3.EVENT.READY", loadNeolineN3);
return () => window.removeEventListener("NEOLine.N3.EVENT.READY", loadNeolineN3);
}, []); // eslint-disable-line react-hooks/exhaustive-deps

useEffect(() => {
Expand Down Expand Up @@ -184,7 +187,7 @@ export const App = () => {
if (location.pathname.indexOf('/profile') === -1) {
document.location.href = "/profile";
}
} else if (walletData) {
} else if (walletData || (!walletData && location.pathname !== '/')) {
onDisconnectWallet();
if (location.pathname !== '/') {
document.location.href = "/";
Expand Down Expand Up @@ -290,6 +293,8 @@ export const App = () => {
onModal('failed', error.data.message);
} else if (error.message) {
onModal('failed', error.message);
} else if (error.description && error.description.msg) {
onModal('failed', error.description.msg);
} else if (error.description) {
onModal('failed', error.description);
} else {
Expand All @@ -300,7 +305,9 @@ export const App = () => {
const onSignMessage = async (msg = '', type, operation, params) => {
let response = '';

if (neolineN3) {
if (walletData.name === 'o3-desktop') {
response = await neo3Dapi.signMessage({ message: msg }).catch((err) => handleError(err));
} else if (neolineN3) {
response = await neolineN3.signMessage({ message: msg }).catch((err) => handleError(err));
} else if (dapi) {
response = await dapi.signMessage({ message: msg }).catch((err) => handleError(err));
Expand Down Expand Up @@ -519,7 +526,32 @@ export const App = () => {
};

const onConnectWallet = async (neolineN3Temp = neolineN3) => {
if (neolineN3Temp) {
let account;
try {
account = await neo3Dapi.getPublicKey();
} catch(err) {}
if (account) {
const provider = await neo3Dapi.getProvider();
const networks = await neo3Dapi.getNetworks();

setWalletData({
name: provider.name,
type: 'neo3',
net: networks.defaultNetwork.toLowerCase(),
account: account,
tokens: {
container: {},
object: null,
}
});

onPopup('success', 'Wallet connected');
onModal();

if (location.pathname.indexOf('/profile') === -1) {
navigate('/profile');
}
} else if (neolineN3Temp) {
neolineN3Temp.getPublicKey().then((account) => {
neolineN3Temp.getNetworks().then((networks) => {
setWalletData({
Expand Down Expand Up @@ -575,7 +607,9 @@ export const App = () => {
}

const onDisconnectWallet = async () => {
if (!dapi) {
if (walletData.name === 'o3-desktop') {
await neo3Dapi.disconnect();
} else if (!dapi) {
await wcSdk.disconnect();
}
document.location.href = "/";
Expand Down
2 changes: 1 addition & 1 deletion src/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const Home = ({
Connect wallet
</Button>
<Heading align="center" size={6} weight="normal">
Built with <a href="https://walletconnect.com/" target="_blank" rel="noopener noreferrer">WalletConnect</a>, tested with <a href="https://neonwallet.com/" target="_blank" rel="noopener noreferrer">NEON wallet</a>, <a href="https://neoline.io/" target="_blank" rel="noopener noreferrer">NeoLine extension</a>, <a href="https://onegate.space/" target="_blank" rel="noopener noreferrer">OneGate wallet (Mobile)</a>.
Built with <a href="https://walletconnect.com/" target="_blank" rel="noopener noreferrer">WalletConnect</a>, tested with <a href="https://neonwallet.com/" target="_blank" rel="noopener noreferrer">NEON wallet</a>, <a href="https://neoline.io/" target="_blank" rel="noopener noreferrer">NeoLine extension</a>, <a href="https://o3.network/#/wallet" target="_blank" rel="noopener noreferrer">O3 wallet</a>, <a href="https://onegate.space/" target="_blank" rel="noopener noreferrer">OneGate wallet (Mobile)</a>.
</Heading>
</>
)}
Expand Down
13 changes: 10 additions & 3 deletions src/Profile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useEffect, useState } from 'react';
import Neon from "@cityofzion/neon-js";
import neo3Dapi from "neo3-dapi";
import {
Container,
Section,
Expand Down Expand Up @@ -112,7 +113,9 @@ const Profile = ({
account: Neon.create.account(walletData.account.address).scriptHash,
}];

if (neolineN3) {
if (walletData.name === 'o3-desktop') {
response = await neo3Dapi.invokeRead({ ...invocations[0], signers }).catch((err) => handleError(err));
} else if (neolineN3) {
response = await neolineN3.invokeRead({ ...invocations[0], signers }).catch((err) => handleError(err));
} else if (dapi) {
response = await dapi.invokeRead({ ...invocations[0] }).catch((err) => handleError(err));
Expand Down Expand Up @@ -190,7 +193,9 @@ const Profile = ({
}];

let response = '';
if (neolineN3) {
if (walletData.name === 'o3-desktop') {
response = await neo3Dapi.invoke({ ...invocations[0], signers }).catch((err) => handleError(err));
} else if (neolineN3) {
response = await neolineN3.invoke({ ...invocations[0], signers }).catch((err) => handleError(err));
} else if (dapi) {
response = await dapi.invoke({ ...invocations[0], signers }).catch((err) => handleError(err));
Expand Down Expand Up @@ -233,7 +238,9 @@ const Profile = ({
}];

let response = '';
if (neolineN3) {
if (walletData.name === 'o3-desktop') {
response = await neo3Dapi.invoke({ ...invocations[0], signers }).catch((err) => handleError(err));
} else if (neolineN3) {
response = await neolineN3.invoke({ ...invocations[0], signers }).catch((err) => handleError(err));
} else if (dapi) {
response = await dapi.invoke({ ...invocations[0], signers }).catch((err) => handleError(err));
Expand Down

0 comments on commit 61bdc5d

Please sign in to comment.