Global Props

This commit is contained in:
Prrorr2 2023-08-11 19:40:34 +02:00
parent c6528bae79
commit abbab28dfd
10 changed files with 88 additions and 115 deletions

View File

@ -1,26 +1,29 @@
/*eslint-disable */ /*eslint-disable */
import Login from "./Screens/Login.jsx"; import Login from "./Screens/Login.jsx";
import { useState, useEffect } from "react"; import { useState, useEffect, createContext } from "react";
import MainApp from "./Screens/MainApp.jsx"; import MainApp from "./Screens/MainApp.jsx";
import sjcl from "sjcl"; import sjcl from "sjcl";
export const Appcontext = createContext();
export default function App() { export default function App() {
const [keys, setKeys] = useState({ public: "", private: "" }); const [keys, setKeys] = useState({ public: "", private: "" });
useEffect(() => {
if (keys.private !== "") { if (keys.private !== "") {
console.log("heykj");
console.log(keys.private);
window.localStorage.setItem("private", keys.private); window.localStorage.setItem("private", keys.private);
} else { } else {
if (window.localStorage.getItem("private") !== null) { if (window.localStorage.getItem("private") !== null) {
console.log(window.localStorage.getItem("private"));
setKeys({ setKeys({
private: window.localStorage.getItem("private"), private: window.localStorage.getItem("private"),
}); });
} }
} }
console.log(keys); }, [keys.private]);
return keys.private.length < 10 ? ( return keys.private.length < 10 ? (
<Login setKeys={setKeys} /> <Appcontext.Provider value={{ setKeys, keys }}>
<Login />
</Appcontext.Provider>
) : ( ) : (
<MainApp keys={keys} setKeys={setKeys} /> <Appcontext.Provider value={{ setKeys, keys }}>
<MainApp />
</Appcontext.Provider>
); );
} }

View File

@ -1,7 +1,8 @@
/*eslint-disable */ /*eslint-disable */
import { useEffect, useState } from "react"; import { useContext, useEffect, useState } from "react";
import getAccount from "../Scripts/Testnet"; import getAccount from "../Scripts/Testnet";
import sjcl from "sjcl"; import sjcl from "sjcl";
import { Appcontext } from "../App";
function desprorr2(encripted) { function desprorr2(encripted) {
var parsedMessage = JSON.parse(encripted); var parsedMessage = JSON.parse(encripted);
var encryptedMessageWithoutParameters = JSON.stringify(parsedMessage); var encryptedMessageWithoutParameters = JSON.stringify(parsedMessage);
@ -12,10 +13,10 @@ function desprorr2(encripted) {
var decryptedMessage = sjcl.decrypt("arstotzka", messageWithParameters); var decryptedMessage = sjcl.decrypt("arstotzka", messageWithParameters);
return decryptedMessage; return decryptedMessage;
} }
export default function Header({ keys, setKeys }) { export default function Header() {
const props = useContext(Appcontext);
const [balance, setBalance] = useState([null, null]); const [balance, setBalance] = useState([null, null]);
const desprivate = desprorr2(keys.private); const desprivate = desprorr2(props.keys.private);
console.log(desprivate);
if (balance[0] === null) { if (balance[0] === null) {
getAccount(desprivate).then((account) => { getAccount(desprivate).then((account) => {
console.log(account.balances); console.log(account.balances);
@ -33,7 +34,7 @@ export default function Header({ keys, setKeys }) {
}); });
} }
function handleLogout() { function handleLogout() {
setKeys({ props.setKeys({
public: "r", public: "r",
private: "r", private: "r",
}); });

View File

@ -3,6 +3,8 @@ import { useEffect, useRef, useState } from "react";
import { send } from "../Scripts/Testnet"; import { send } from "../Scripts/Testnet";
import sjcl from "sjcl"; import sjcl from "sjcl";
import { getFee } from "../Scripts/Testnet"; import { getFee } from "../Scripts/Testnet";
import { useContext } from "react";
import { Appcontext } from "../App";
function desprorr2(encripted) { function desprorr2(encripted) {
var parsedMessage = JSON.parse(encripted); var parsedMessage = JSON.parse(encripted);
var encryptedMessageWithoutParameters = JSON.stringify(parsedMessage); var encryptedMessageWithoutParameters = JSON.stringify(parsedMessage);
@ -13,29 +15,20 @@ function desprorr2(encripted) {
var decryptedMessage = sjcl.decrypt("arstotzka", messageWithParameters); var decryptedMessage = sjcl.decrypt("arstotzka", messageWithParameters);
return decryptedMessage; return decryptedMessage;
} }
export default function Pay({ keys }) { export default function Pay() {
const [comision, setComision] = useState("0"); const props = useContext(Appcontext);
const [fee, setFee] = useState("0"); const [fee, setFee] = useState("0");
useEffect(() => { useEffect(() => {
setFee(((parseFloat(getFee()) * 0.00001) / 100).toString()); setFee(((parseFloat(getFee()) * 0.00001) / 100).toString());
}, []); }, []);
function calculatecomision() {
let amount = document.getElementById("amount").value;
if (amount < 1) {
setComision("0.001");
} else {
setComision((parseFloat(amount) / 500).toString());
}
}
function handleSend() { function handleSend() {
console.log(keys);
const amount = document.getElementById("amount").value; const amount = document.getElementById("amount").value;
const to = document.getElementById("to").value; const to = document.getElementById("to").value;
const memo = document.getElementById("memo").value; const memo = document.getElementById("memo").value;
console.log(amount); console.log(amount);
console.log(to); console.log(to);
console.log(desprorr2(keys.private)); console.log(desprorr2(props.keys.private));
send(amount, desprorr2(keys.private), to, memo); send(amount, desprorr2(props.keys.private), to, memo);
} }
return ( return (
<div className="h-3/6 w-2/6 min-h-[25rem] bg-secondary self-center ml-[10rem] rounded-2xl flex flex-col gap-5 justify-center"> <div className="h-3/6 w-2/6 min-h-[25rem] bg-secondary self-center ml-[10rem] rounded-2xl flex flex-col gap-5 justify-center">
@ -45,7 +38,7 @@ export default function Pay({ keys }) {
<input <input
name="send2" name="send2"
id="to" id="to"
className="h-[2rem] w-3/6 self-center border-4 border-tertiary rounded-md" className="h-[2rem] w-3/6 self-center border-4 border-five rounded-md"
placeholder="Example: GDY2CP7XLBIY65YX7KS72X7JCZJVJ3AEPEEFTW3TYPLGDRWEZKAC7NR3" placeholder="Example: GDY2CP7XLBIY65YX7KS72X7JCZJVJ3AEPEEFTW3TYPLGDRWEZKAC7NR3"
type="text" type="text"
/> />
@ -56,13 +49,9 @@ export default function Pay({ keys }) {
<input <input
id="amount" id="amount"
name="amount" name="amount"
className="self-center w-full border-4 border-tertiary rounded-md" className="self-center w-full border-4 border-five rounded-md"
type="number" type="number"
onChange={calculatecomision}
/> />
<p className="text-alert self-center text-sm">
Comision BLC: {comision}
</p>
<p className="text-alert self-center text-sm">Comision XLM: {fee}</p> <p className="text-alert self-center text-sm">Comision XLM: {fee}</p>
</div> </div>
<label htmlFor="send2" className="font-bold text-3xl self-center"> <label htmlFor="send2" className="font-bold text-3xl self-center">
@ -71,7 +60,7 @@ export default function Pay({ keys }) {
<input <input
name="memo" name="memo"
id="memo" id="memo"
className="h-[2rem] w-3/6 self-center border-4 border-tertiary rounded-md" className="h-[2rem] w-3/6 self-center border-4 border-five rounded-md"
placeholder="Example: Buy a great coffee" placeholder="Example: Buy a great coffee"
type="text" type="text"
/> />

View File

@ -1,9 +1,10 @@
/*eslint-disable */ /*eslint-disable */
import { useState, useEffect, useRef } from "react"; import { useState, useEffect, useRef, useContext } from "react";
import { getAddress } from "../Scripts/Testnet"; import { getAddress } from "../Scripts/Testnet";
import sjcl from "sjcl"; import sjcl from "sjcl";
import StellarSdk from "stellar-sdk"; import StellarSdk from "stellar-sdk";
import normalTag from "./tags"; import { NormalTag, SpanTag, TitleTag } from "./Transactions/Tags";
import { Appcontext } from "../App";
const server = new StellarSdk.Server("https://horizon-testnet.stellar.org"); const server = new StellarSdk.Server("https://horizon-testnet.stellar.org");
function desprorr2(encripted) { function desprorr2(encripted) {
var parsedMessage = JSON.parse(encripted); var parsedMessage = JSON.parse(encripted);
@ -16,10 +17,11 @@ function desprorr2(encripted) {
return decryptedMessage; return decryptedMessage;
} }
export default function Transactions({ keys }) { export default function Transactions() {
const props = useContext(Appcontext);
const [operations, setOperations] = useState([]); const [operations, setOperations] = useState([]);
const publickey = useRef(""); const publickey = useRef("");
const desprivate = desprorr2(keys.private); const desprivate = desprorr2(props.keys.private);
useEffect(() => { useEffect(() => {
let cursor = []; let cursor = [];
let newOperations = []; let newOperations = [];
@ -56,42 +58,30 @@ export default function Transactions({ keys }) {
<div className=" w-[32%] h-5/6 bg-secondary shadow-xl rounded-xl flex flex-col gap-5 overflow-y-scroll overflow-x-clip mt-10"> <div className=" w-[32%] h-5/6 bg-secondary shadow-xl rounded-xl flex flex-col gap-5 overflow-y-scroll overflow-x-clip mt-10">
{operations.map((operation) => ( {operations.map((operation) => (
<div className="bg-five flex flex-col gap-2 rounded-lg w-[95%] self-center mt-5"> <div className="bg-five flex flex-col gap-2 rounded-lg w-[95%] self-center mt-5">
<h1 className="pl-5 text-2xl text-secondary font-bold"> <TitleTag>{operation.type}:</TitleTag>
{operation.type}: <NormalTag>{operation.amount}</NormalTag>
</h1>
<h1 className="font-semibold text-primary bg-primary2 self-center pl-5 w-5/6 text-center p-2 rounded-md">
{operation.amount}
</h1>
{operation.funder ? ( {operation.funder ? (
<h1 className="font-semibold text-primary bg-primary2 self-center text-center pl-5 w-5/6 break-all p-2 rounded-md"> <NormalTag>By: {operation.funder}</NormalTag>
By: {operation.funder}
</h1>
) : null} ) : null}
{operation.asset_code ? ( {operation.asset_code ? (
<h1 className="font-semibold text-primary bg-primary2 self-center text-center pl-5 w-5/6 p-2 rounded-md"> <NormalTag>Token: {operation.asset_code}</NormalTag>
Token: {operation.asset_code}
</h1>
) : null} ) : null}
{operation.from ? ( {operation.from ? (
operation.from === publickey.current ? ( operation.from === publickey.current ? (
<h1 className="font-semibold text-primary bg-primary2 self-center text-center pl-5 w-5/6 p-2 rounded-md"> <NormalTag>
from: <span className="text-secondary text-xl">Me</span> from: <SpanTag>Me</SpanTag>
</h1> </NormalTag>
) : ( ) : (
<h1 className="font-semibold text-primary bg-primary2 self-center text-center pl-5 w-5/6 break-all p-2 rounded-md"> <NormalTag>from: {operation.from}</NormalTag>
from: {operation.from}
</h1>
) )
) : null} ) : null}
{operation.to ? ( {operation.to ? (
operation.to === publickey.current ? ( operation.to === publickey.current ? (
<h1 className="font-semibold text-primary bg-primary2 self-center text-center pl-5 w-5/6 break-all p-2 rounded-md"> <NormalTag>
to: <span className="text-secondary text-xl">Me</span> to: <SpanTag>Me</SpanTag>
</h1> </NormalTag>
) : ( ) : (
<h1 className="font-semibold text-primary bg-primary2 self-center text-center pl-5 w-5/6 break-all p-2 rounded-md"> <NormalTag>to: {operation.to}</NormalTag>
to: {operation.to}
</h1>
) )
) : null} ) : null}
</div> </div>

View File

@ -0,0 +1,17 @@
/*eslint-disable */
export function NormalTag(props) {
return (
<h1 className="font-semibold text-primary bg-primary2 self-center text-center break-all pl-5 w-5/6 p-2 rounded-md">
{props.children}
</h1>
);
}
export function TitleTag(props) {
return (
<h1 className="pl-5 text-2xl text-secondary font-bold">{props.children}</h1>
);
}
export function SpanTag(props) {
return <span className="text-secondary text-xl">{props.children}</span>;
}

View File

@ -1,10 +1,11 @@
/*eslint-disable */ /*eslint-disable */
import { useEffect, useRef } from "react"; import { useContext, useEffect, useRef } from "react";
import getAccount, { getAddress } from "../Scripts/Testnet"; import getAccount, { getAddress } from "../Scripts/Testnet";
import StellarSdk from "stellar-sdk"; import StellarSdk from "stellar-sdk";
import sjcl from "sjcl"; import sjcl from "sjcl";
import Swal from "sweetalert2"; import Swal from "sweetalert2";
import "@sweetalert2/theme-dark/dark.css"; import "@sweetalert2/theme-dark/dark.css";
import { Appcontext } from "../App";
const server = new StellarSdk.Server("https://horizon-testnet.stellar.org"); const server = new StellarSdk.Server("https://horizon-testnet.stellar.org");
function desprorr2(encripted) { function desprorr2(encripted) {
var parsedMessage = JSON.parse(encripted); var parsedMessage = JSON.parse(encripted);
@ -16,9 +17,10 @@ function desprorr2(encripted) {
var decryptedMessage = sjcl.decrypt("arstotzka", messageWithParameters); var decryptedMessage = sjcl.decrypt("arstotzka", messageWithParameters);
return decryptedMessage; return decryptedMessage;
} }
export default function Recived({ keys }) { export default function Recived() {
const publickey = useRef(getAddress(desprorr2(keys.private))); const props = useContext(Appcontext);
const desprivate = desprorr2(keys.private); const publickey = useRef(getAddress(desprorr2(props.keys.private)));
const desprivate = desprorr2(props.keys.private);
const activado = useRef(false); const activado = useRef(false);
const cambiosdetectados = useRef(0); const cambiosdetectados = useRef(0);
const operations = useRef([{ transaction_hash: 0 }]); const operations = useRef([{ transaction_hash: 0 }]);
@ -42,7 +44,6 @@ export default function Recived({ keys }) {
payments.records.length > 0 payments.records.length > 0
? payments.records[payments.records.length - 1].paging_token ? payments.records[payments.records.length - 1].paging_token
: null; : null;
console.log(payments);
const timestamp = "2023-08-10T08:15:07Z"; const timestamp = "2023-08-10T08:15:07Z";
const date = new Date(timestamp); const date = new Date(timestamp);
const dayoftransaction = date.getUTCDate(); const dayoftransaction = date.getUTCDate();
@ -62,20 +63,16 @@ export default function Recived({ keys }) {
icon: "success", icon: "success",
confirmButtonText: "OK", confirmButtonText: "OK",
}); });
console.log(payments.records[0].transaction_hash);
} }
if ( if (
operations.current[0].transaction_hash !== operations.current[0].transaction_hash !==
payments.records[0].transaction_hash && payments.records[0].transaction_hash &&
payments.records[0] !== undefined payments.records[0] !== undefined
) { ) {
console.log(cambiosdetectados.current);
cambiosdetectados.current += 1; cambiosdetectados.current += 1;
} }
operations.current = payments.records; operations.current = payments.records;
operationsday.current = dayoftransaction; operationsday.current = dayoftransaction;
console.log(operationsday.current);
console.log(dayoftransaction);
// Si hay más transacciones, hacer otra solicitud // Si hay más transacciones, hacer otra solicitud
}) })
.catch((error) => { .catch((error) => {

View File

@ -1,8 +0,0 @@
/*eslint-disable */
export default function normalTag({ content }) {
return (
<h1 className="font-semibold text-five bg-primary2 self-center pl-5 w-5/6">
{content}
</h1>
);
}

View File

@ -1,10 +1,12 @@
/*eslint-disable */ /*eslint-disable */
import { useEffect, useState } from "react";
import sjcl from "sjcl"; import sjcl from "sjcl";
export default function Login({ setKeys }) { import { Appcontext } from "../App";
import { useContext } from "react";
export default function Login() {
const props = useContext(Appcontext);
function handleSubmit() { function handleSubmit() {
const privatekey = document.getElementById("privatekey").value; const privatekey = document.getElementById("privatekey").value;
setKeys({ props.setKeys({
private: sjcl.encrypt("arstotzka", privatekey), private: sjcl.encrypt("arstotzka", privatekey),
}); });
} }

View File

@ -3,14 +3,17 @@ import Header from "../Components/Header";
import Pay from "../Components/Pay"; import Pay from "../Components/Pay";
import Transactions from "../Components/Transactions"; import Transactions from "../Components/Transactions";
import Recived from "../Components/recived"; import Recived from "../Components/recived";
export default function MainApp({ keys, setKeys }) { import { Appcontext } from "../App";
import { useContext } from "react";
export default function MainApp() {
const props = useContext(Appcontext);
return ( return (
<div className={"bg-five w-full h-full"}> <div className={"bg-five w-full h-full"}>
<Header keys={keys} setKeys={setKeys} /> <Header keys={props.keys} setkeys={props.setKeys} />
<div className="flex flex-row w-full h-5/6 justify-center"> <div className="flex flex-row w-full h-5/6 justify-center">
<Transactions keys={keys} /> <Transactions keys={props.keys} />
<Pay keys={keys} /> <Pay keys={props.keys} />
<Recived keys={keys} /> <Recived keys={props.keys} />
</div> </div>
</div> </div>
); );

View File

@ -1,8 +1,3 @@
/*Public Key
GDY2CP7XLBIY65YX7KS72X7JCZJVJ3AEPEEFTW3TYPLGDRWEZKAC7NR3
Secret Key
SAM4KRGZ4YTAQV3XK3FRKVHZV77AA37KSYZLKZ4OZSX3SBWR6C6NYS4M
*/
import StellarSdk from 'stellar-sdk' import StellarSdk from 'stellar-sdk'
import Swal from "sweetalert2"; import Swal from "sweetalert2";
import '@sweetalert2/theme-dark/dark.css' import '@sweetalert2/theme-dark/dark.css'
@ -23,14 +18,6 @@ export function send(amount, privatekey, destination, memo){
if(memo === undefined || memo === null){ if(memo === undefined || memo === null){
memo = "."; memo = ".";
} }
let comision = '0.001';
if(parseFloat(amount) < 1){
comision = '0.001';
} else{
comision = parseFloat(amount) / 500;
comision = comision. toString();
}
console.log(comision)
const keypair = StellarSdk.Keypair.fromSecret(privatekey) const keypair = StellarSdk.Keypair.fromSecret(privatekey)
server.loadAccount(keypair.publicKey()).then(account => { server.loadAccount(keypair.publicKey()).then(account => {
console.log(account) console.log(account)
@ -45,14 +32,6 @@ const transaction = new StellarSdk.TransactionBuilder(account, {fee, networkPass
// the decimal. They are represented in JS Stellar SDK in string format // the decimal. They are represented in JS Stellar SDK in string format
// to avoid errors from the use of the JavaScript Number data structure. // to avoid errors from the use of the JavaScript Number data structure.
amount: amount, amount: amount,
})).addOperation(StellarSdk.Operation.payment({
destination: "GDY2CP7XLBIY65YX7KS72X7JCZJVJ3AEPEEFTW3TYPLGDRWEZKAC7NR3",
// The term native asset refers to lumens
asset: new StellarSdk.Asset("BLC", "GBPKUSD5ZGMEXCS5YW7WAOC2QJSJLEX6LHGUTJIMDYYPFLPOT6GYFNNX"),
// Specify 350.1234567 lumens. Lumens are divisible to seven digits past
// the decimal. They are represented in JS Stellar SDK in string format
// to avoid errors from the use of the JavaScript Number data structure.
amount: comision,
})) }))
// Make this transaction valid for the next 30 seconds only // Make this transaction valid for the next 30 seconds only
.setTimeout(30) .setTimeout(30)
@ -72,7 +51,7 @@ try {
console.log('An error has occured:'); console.log('An error has occured:');
console.log(e); console.log(e);
}Swal.fire({ }Swal.fire({
title: "Payment sent successfully Comision: " + comision+ " BLC", title: "Payment sent successfully",
icon: "success", icon: "success",
confirmButtonText: "OK", confirmButtonText: "OK",
});} });}