Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new commit #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,109 changes: 1,663 additions & 446 deletions client/package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"preview": "vite preview"
},
"dependencies": {
"axios": "^0.27.2",
"axios": "^1.7.2",
"ethereum-cryptography": "^2.1.3",
"ethereumjs-tx": "^2.1.2",
"ethereumjs-util": "^7.1.5",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
4 changes: 3 additions & 1 deletion client/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Wallet from "./Wallet";
import Transfer from "./Transfer";
// import Transfer from "./Transfer";
import "./App.scss";
import Transfer from "./Transffer";
import { useState } from "react";


function App() {
const [balance, setBalance] = useState(0);
const [address, setAddress] = useState("");
Expand Down
10 changes: 10 additions & 0 deletions client/src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,14 @@ input {
.transfer {
display: flex;
flex-direction: column;

.signature {
letter-spacing: 1px;
font-weight: 400;
font-size: .9em;
display: inline-flex;
margin-top: 10px;
padding: 0.75rem;
background-color: #f4f6f8;
}
}
2 changes: 2 additions & 0 deletions client/src/Transfer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ function Transfer({ address, setBalance }) {
</label>

<input type="submit" className="button" value="Transfer" />
<div className="signature">Mssg:</div>
</form>
);
}

export default Transfer;
// new
69 changes: 69 additions & 0 deletions client/src/Transffer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { useState } from "react";
import server from "./server";

function Transfer({ address, setBalance }) {
const [sendAmount, setSendAmount] = useState("");
const [recipient, setRecipient] = useState("");
// Dummy signature for demonstration purposes
const [dummySignature, setDummySignature] = useState("");

const setValue = (setter) => (evt) => setter(evt.target.value);

async function transfer(evt) {
evt.preventDefault();

try {
// Simulated transaction data
const transactionData = `${address}${recipient}${sendAmount}`;

// Generate a dummy signature (replace this with actual signature generation)
const dummySignatureValue = "0x1234567890123456789012345678901234567890"; // Replace with actual signature

const {
data: { balance },
} = await server.post(`send`, {
sender: address,
amount: parseInt(sendAmount),
recipient,
signature: dummySignatureValue, // Include the dummy signature in the request
});

setBalance(balance);
} catch (ex) {
alert(ex.response.data.message);
}
}

return (
<form className="container transfer" onSubmit={transfer}>
<h1>Send Transaction</h1>

<label>
Send Amount
<input
placeholder="1, 2, 3..."
value={sendAmount}
onChange={setValue(setSendAmount)}
></input>
</label>

<label>
Recipient
<input
placeholder="Type an address, for example: 0x2"
value={recipient}
onChange={setValue(setRecipient)}
></input>
</label>

{/* Display the dummy signature */}
<div className="signature">
Signature: <span>{dummySignature}</span>
</div>

<input type="submit" className="button" value="Transfer" />
</form>
);
}

export default Transfer;
7 changes: 4 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ app.use(cors());
app.use(express.json());

const balances = {
"0x1": 100,
"0x2": 50,
"0x3": 75,
"089b40fad730c2e6edc3": 100,
"9b7fcd1c1787f635d8a6": 50,
"cb062d401405b8ea555c": 75,
};

app.get("/balance/:address", (req, res) => {
Expand Down Expand Up @@ -42,3 +42,4 @@ function setInitialBalance(address) {
balances[address] = 0;
}
}
/// new
59 changes: 59 additions & 0 deletions server/newIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const express = require("express");
const app = express();
const cors = require("cors");
const port = 3042;
const { Transaction, utils } = require("ethereumjs-tx");

app.use(cors());
app.use(express.json());

const balances = {
"089b40fad730c2e6edc3": 100,
"9b7fcd1c1787f635d8a6": 50,
"cb062d401405b8ea555c": 75,
};

// Function to verify a signature
function verifySignature(messageHash, signature, publicKey) {
return Transaction.verifyTransaction(messageHash, signature, publicKey);
}

app.get("/balance/:address", (req, res) => {
const { address } = req.params;
const balance = balances[address] || 0;
res.send({ balance });
});

app.post("/send", (req, res) => {
const { sender, recipient, amount, signature } = req.body;

// Verify the signature
const messageHash = utils.sha3(utils.toBuffer(`${sender}${recipient}${amount}`));
const isValid = verifySignature(messageHash, signature, sender);

if (!isValid) {
res.status(403).send({ message: "Invalid signature" });
return;
}

setInitialBalance(sender);
setInitialBalance(recipient);

if (balances[sender] < amount) {
res.status(400).send({ message: "Not enough funds!" });
} else {
balances[sender] -= amount;
balances[recipient] += amount;
res.send({ balance: balances[sender] });
}
});

app.listen(port, () => {
console.log(`Listening on port ${port}`);
});

function setInitialBalance(address) {
if (!balances[address]) {
balances[address] = 0;
}
}
Loading