Skip to content

Commit

Permalink
web: Support NeoLine wallet
Browse files Browse the repository at this point in the history
- Add neolineN3 API

Signed-off-by: Mikhail Petrov <[email protected]>
  • Loading branch information
mike-petrov committed Feb 28, 2024
1 parent 7a7dee9 commit 8c383d7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
49 changes: 46 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const App = () => {
const location = useLocation();
const wcSdk = useWalletConnect();
const dapi = window.OneGate ? new BaseDapi(window.OneGate) : null;
let [neolineN3, setNeolineN3] = useState(null);
const [activeNet] = useState(process.env.REACT_APP_NETWORK ? process.env.REACT_APP_NETWORK : 'mainnet');

const [ContentTypeHeader] = useState("Content-Type");
Expand Down Expand Up @@ -143,6 +144,20 @@ export const App = () => {
}, 2000);
};

const loadNeolineN3 = () => {
const neolineN3 = new window.NEOLineN3.Init();
setNeolineN3(neolineN3);

if (location.pathname.indexOf('/profile') !== -1) {
onConnectWallet(neolineN3);
}
};

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

useEffect(() => {
if (process.env.REACT_APP_WC_PROJECT_ID && process.env.REACT_APP_WC_PROJECT_ID !== '') {
if (dapi) {
Expand Down Expand Up @@ -275,14 +290,19 @@ export const App = () => {
onModal('failed', error.data.message);
} else if (error.message) {
onModal('failed', error.message);
} else if (error.description) {
onModal('failed', error.description);
} else {
onModal('failed', 'Something went wrong, try again');
}
};

const onSignMessage = async (msg = '', type, operation, params) => {
let response = '';
if (dapi) {

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));
response.data = response.signature;
} else {
Expand Down Expand Up @@ -498,8 +518,30 @@ export const App = () => {
}
};

const onConnectWallet = async () => {
if (dapi) {
const onConnectWallet = async (neolineN3Temp = neolineN3) => {
if (neolineN3Temp) {
neolineN3Temp.getPublicKey().then((account) => {
neolineN3Temp.getNetworks().then((networks) => {
setWalletData({
name: 'NeoLine',
type: 'neo3',
net: networks.defaultNetwork.toLowerCase(),
account: account,
tokens: {
container: {},
object: null,
}
});

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

if (location.pathname.indexOf('/profile') === -1) {
document.location.href = "/profile";
}
}).catch((err) => handleError(err));
}).catch((err) => handleError(err));
} else if (dapi) {
const provider = await dapi.getProvider();
const networks = await dapi.getNetworks();
const account = await dapi.getAccount();
Expand Down Expand Up @@ -1399,6 +1441,7 @@ export const App = () => {
setWalletData={setWalletData}
wcSdk={wcSdk}
dapi={dapi}
neolineN3={neolineN3}
isLoadContainers={isLoadContainers}
setLoadContainers={setLoadContainers}
onDisconnectWallet={onDisconnectWallet}
Expand Down
2 changes: 1 addition & 1 deletion src/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const Home = ({
<Heading align="center">Connect your wallet to log in</Heading>
<Button
color="primary"
onClick={onConnectWallet}
onClick={() => onConnectWallet()}
style={{ display: 'flex', margin: '0 auto 20px' }}
>
Connect wallet
Expand Down
22 changes: 16 additions & 6 deletions src/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const Profile = ({
onPopup,
wcSdk,
dapi,
neolineN3,
isLoadContainers,
setLoadContainers,
ContentTypeHeader,
Expand Down Expand Up @@ -108,9 +109,12 @@ const Profile = ({

const signers = [{
scopes: 1, // WitnessScope.CalledByEntry
account: Neon.create.account(walletData.account.address).scriptHash,
}];

if (dapi) {
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));
} else {
if (wcSdk.session.expiry * 1000 < new Date().getTime()) {
Expand Down Expand Up @@ -182,11 +186,14 @@ const Profile = ({

const signers = [{
scopes: 1, // WitnessScope.CalledByEntry
account: Neon.create.account(walletData.account.address).scriptHash,
}];

let response = '';
if (dapi) {
response = await dapi.invoke({ ...invocations[0] }).catch((err) => handleError(err));
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));
} else {
response = await wcSdk.invokeFunction({ invocations, signers }).catch((error) => {
if (error.message === 'Failed or Rejected Request') {
Expand Down Expand Up @@ -221,16 +228,19 @@ const Profile = ({

const signers = [{
scopes: 16, // WitnessScope.CustomContracts
account: Neon.create.account(walletData.account.address).scriptHash,
allowedContracts: [NeoFSContract.gasToken, NeoFSContract.scriptHash]
}];

let response = '';
if (dapi) {
response = await dapi.invoke({ ...invocations[0] }).catch((err) => handleError(err));
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));
} else {
response = await wcSdk.invokeFunction({ invocations, signers }).catch((err) => handleError(err));
}
if (!response.message) {
if (response && !response.message) {
setWithdrawQuantity(0);
onModal('success', response.txid ? response.txid : response);
}
Expand Down

0 comments on commit 8c383d7

Please sign in to comment.