From 3a4ed9daf900988c8a85f7a9b28e457b4957967a Mon Sep 17 00:00:00 2001 From: kandrusyak Date: Tue, 28 Jun 2022 16:38:05 +0300 Subject: [PATCH] auth --- src/App.jsx | 2 +- src/api/node.js | 3 ++ src/components/DropdownProfile.jsx | 2 +- src/pages/Onboarding1.jsx | 2 +- src/pages/Onboarding2.jsx | 2 +- src/pages/Onboarding3.jsx | 6 +-- src/pages/Onboarding4.jsx | 2 +- src/shared/fetchWrapper.js | 4 +- src/store/registrationStore.js | 41 --------------- src/store/store.js | 81 ++++++++++++++++++++++++++++++ 10 files changed, 94 insertions(+), 51 deletions(-) create mode 100644 src/api/node.js delete mode 100644 src/store/registrationStore.js create mode 100644 src/store/store.js diff --git a/src/App.jsx b/src/App.jsx index c6271ac..8c034ab 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -2,7 +2,7 @@ import React, { useEffect } from 'react'; import { Routes, Route, - useLocation + useLocation, } from 'react-router-dom'; import './css/style.scss'; diff --git a/src/api/node.js b/src/api/node.js new file mode 100644 index 0000000..8cd90f5 --- /dev/null +++ b/src/api/node.js @@ -0,0 +1,3 @@ +import {fetchWrapper} from '../shared/fetchWrapper'; + +export const getNodeInfo = (headers) => fetchWrapper.get('node/info/', headers) \ No newline at end of file diff --git a/src/components/DropdownProfile.jsx b/src/components/DropdownProfile.jsx index 0deb542..e0da30e 100644 --- a/src/components/DropdownProfile.jsx +++ b/src/components/DropdownProfile.jsx @@ -2,7 +2,7 @@ import React, { useState, useRef, useEffect } from 'react'; import {observer} from "mobx-react-lite"; import { Link } from 'react-router-dom'; import Transition from '../utils/Transition'; -import {registrationStore} from "../store/registrationStore"; +import {registrationStore} from "../store/store"; import {generateSvgAvatar} from "../images/GenerateOnboardingSvg/GenerateSvg"; import UserAvatar from '../images/user-avatar-32.png'; diff --git a/src/pages/Onboarding1.jsx b/src/pages/Onboarding1.jsx index 2cefed5..2ec84de 100644 --- a/src/pages/Onboarding1.jsx +++ b/src/pages/Onboarding1.jsx @@ -6,7 +6,7 @@ import OnboardingDecoration from '../images/auth-decoration.png'; import Logo from "../images/logo.png"; import {observer} from "mobx-react-lite"; import {passphrase} from "@liskhq/lisk-client"; -import {registrationStore} from "../store/registrationStore"; +import {registrationStore} from "../store/store"; const Onboarding1 = observer(()=>{ let phrase = passphrase.Mnemonic.generateMnemonic() diff --git a/src/pages/Onboarding2.jsx b/src/pages/Onboarding2.jsx index 9460854..51d1aad 100644 --- a/src/pages/Onboarding2.jsx +++ b/src/pages/Onboarding2.jsx @@ -1,7 +1,7 @@ import React, {useEffect, useState} from 'react'; import { Link } from 'react-router-dom'; import { observer } from "mobx-react-lite"; -import { registrationStore } from "../store/registrationStore"; +import { registrationStore } from "../store/store"; import OnboardingImage from '../images/onboarding-image.jpg'; import OnboardingDecoration from '../images/auth-decoration.png'; diff --git a/src/pages/Onboarding3.jsx b/src/pages/Onboarding3.jsx index 4889fa8..a82e649 100644 --- a/src/pages/Onboarding3.jsx +++ b/src/pages/Onboarding3.jsx @@ -2,15 +2,15 @@ import React, { useState} from 'react'; import { Link } from 'react-router-dom'; import {generateSvgAvatar} from "../images/GenerateOnboardingSvg/GenerateSvg"; import Logo from "../images/logo.png"; -import {registrationStore} from "../store/registrationStore"; +import {registrationStore} from "../store/store"; import { passphrase } from "@liskhq/lisk-client"; function Onboarding3() { - const [passPhrase, setPassPhrase] = useState(registrationStore.passPhraseStore.replace(/\s+/g, ' ').split(' ').map((item, index) => ({str:item, id:index }))) + const [passPhrase, setPassPhrase] = useState(registrationStore.passPhrase.replace(/\s+/g, ' ').split(' ').map((item, index) => ({str:item, id:index }))) function generatePassPhrase() { let phrase = passphrase.Mnemonic.generateMnemonic() registrationStore.savePassPhrase(phrase) - let newPassPhrase = registrationStore.passPhraseStore.split(' ').map((item, index) => ({str:item, id:index })) + let newPassPhrase = registrationStore.passPhrase.split(' ').map((item, index) => ({str:item, id:index })) setPassPhrase(newPassPhrase) } diff --git a/src/pages/Onboarding4.jsx b/src/pages/Onboarding4.jsx index 8eb2a46..96dd347 100644 --- a/src/pages/Onboarding4.jsx +++ b/src/pages/Onboarding4.jsx @@ -3,7 +3,7 @@ import { Link } from 'react-router-dom'; import {generateSvgAvatar} from "../images/GenerateOnboardingSvg/GenerateSvg"; import Logo from "../images/logo.png"; import {observer} from "mobx-react-lite"; -import { registrationStore } from "../store/registrationStore"; +import { registrationStore } from "../store/store"; const Onboarding4 = observer(()=>{ diff --git a/src/shared/fetchWrapper.js b/src/shared/fetchWrapper.js index 7046bb4..bd3a462 100644 --- a/src/shared/fetchWrapper.js +++ b/src/shared/fetchWrapper.js @@ -32,7 +32,7 @@ function request(method, url, headers = {}, token, body) { requestOptions = { method: method, headers: { - authorization: `IDNTTY ${token}`, + Authorization: `IDNTTY ${token}`, ...headers }, signal: controller.signal, @@ -42,7 +42,7 @@ function request(method, url, headers = {}, token, body) { requestOptions = { method: method, headers: { - authorization: `IDNTTY ${token}`, + Authorization: `IDNTTY ${token}`, Accept: 'application/json', 'Content-Type': 'application/json', ...headers diff --git a/src/store/registrationStore.js b/src/store/registrationStore.js deleted file mode 100644 index c647d32..0000000 --- a/src/store/registrationStore.js +++ /dev/null @@ -1,41 +0,0 @@ -import { reaction, makeAutoObservable } from "mobx"; -import {cryptography} from "@liskhq/lisk-client"; - -class RegistrationStore { - _accountData = {} - - _passPhraseStore='' - - _pubKey = '' - - constructor() { - makeAutoObservable(this); - reaction(()=>this.passPhraseStore, ()=>this.savePubKey()) - }; - - savePassPhrase(phrase) { - this._passPhraseStore = phrase - } - - saveDataRegistration(data) { - this._accountData = data; - }; - savePubKey() { - this._pubKey = cryptography.bufferToHex(cryptography.getAddressAndPublicKeyFromPassphrase(this.passPhraseStore).publicKey) - } - - get passPhraseStore() { - return this._passPhraseStore - } - - get accountData() { - return this._accountData - }; - - get pubKey() { - return this._pubKey - } - -}; - -export const registrationStore = new RegistrationStore(); diff --git a/src/store/store.js b/src/store/store.js new file mode 100644 index 0000000..5281627 --- /dev/null +++ b/src/store/store.js @@ -0,0 +1,81 @@ +import { reaction, makeAutoObservable } from "mobx"; +import {cryptography} from "@liskhq/lisk-client"; +import {getNodeInfo} from '../api/node'; +import {fetchWrapper} from '../shared/fetchWrapper'; + +class Store { + _accountData = {} + + _passPhrase='rocket north inform swift improve fringe sweet crew client canyon bean autumn' + + _nodeInfo = {} + + constructor() { + makeAutoObservable(this); + + this.fetchNodeInfo() + + reaction(() => this.tokenKey, () => fetchWrapper.getAuth('http://3.125.47.101/api/data/account', { + networkIdentifier: this.nodeInfo.networkIdentifier, + lastBlockID: this.nodeInfo.lastBlockID + })) + }; + + savePassPhrase(phrase) { + this._passPhrase = phrase + } + + saveDataRegistration(data) { + this._accountData = data; + }; + + fetchNodeInfo() { + getNodeInfo() + .then((info) => this.fetchNodeInfoSuccess(info)) + .catch((err) => this.fetchNodeInfoFailed(err)) + } + + fetchNodeInfoSuccess(info) { + this._nodeInfo = info.data; + console.log(info) + } + + fetchNodeInfoFailed(err) { + console.log(err) + } + + get passPhrase() { + return 'rocket north inform swift improve fringe sweet crew client canyon bean autumn' + } + + get accountData() { + return this._accountData + }; + + get nodeInfo() { + return this._nodeInfo; + } + + get addressAndPubKey() { + return cryptography.getAddressAndPublicKeyFromPassphrase(this.passPhrase); + } + + get pubKey() { + return this.addressAndPubKey.publicKey.toString('hex'); + } + + get address() { + return cryptography.bufferToHex(this.addressAndPubKey.address) + } + + get tokenKey() { + const stringToSign = this.nodeInfo.networkIdentifier.concat(this.nodeInfo.lastBlockID); + if(!stringToSign) + return ''; + const sign = cryptography.signDataWithPassphrase(Buffer.from(stringToSign, 'hex'), this.passPhrase).toString('hex') + return this.pubKey + ':' +sign + } + +}; + +export const registrationStore = new Store();