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

ecdsa-node-project #35

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
115 changes: 115 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"axios": "^0.27.2",
"ethereum-cryptography": "^2.1.3",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
9 changes: 8 additions & 1 deletion client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useState } from "react";
function App() {
const [balance, setBalance] = useState(0);
const [address, setAddress] = useState("");
const [privateKey, setPrivateKey] = useState("");

return (
<div className="app">
Expand All @@ -14,8 +15,14 @@ function App() {
setBalance={setBalance}
address={address}
setAddress={setAddress}
privateKey={privateKey}
setPrivateKey={setPrivateKey}
/>
<Transfer
setBalance={setBalance}
address={address}
privateKey={privateKey}
/>
<Transfer setBalance={setBalance} address={address} />
</div>
);
}
Expand Down
41 changes: 33 additions & 8 deletions client/src/Transfer.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
import { useState } from "react";
import React, { useState } from "react";
import { keccak_256 } from "@noble/hashes/sha3";
import { secp256k1 } from '@noble/curves/secp256k1';
import { bytesToHex, utf8ToBytes } from "@noble/hashes/utils";
import server from "./server";

function Transfer({ address, setBalance }) {
function Transfer({ address, setBalance, privateKey }) {

const [sendAmount, setSendAmount] = useState("");
const [recipient, setRecipient] = useState("");

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

async function transfer(evt) {
const hashMessage = (message) => {
const bytes = utf8ToBytes(message);
const hash = keccak_256(bytes);
return hash;
};

const signMessage = (message, privateKey) => {
const signed = secp256k1.sign(hashMessage(message), privateKey);
return signed;
};

const jsonReplacer = (key, value) => typeof value === "bigint" ? value.toString() : value;

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

const senderSignature = signMessage("transfer", privateKey);

try {
const {
data: { balance },
} = await server.post(`send`, {
sender: address,
amount: parseInt(sendAmount),
recipient,
signature: JSON.stringify(senderSignature, jsonReplacer)
});

setBalance(balance);
} catch (ex) {
alert(ex.response.data.message);
Expand All @@ -33,22 +54,26 @@ function Transfer({ address, setBalance }) {
<input
placeholder="1, 2, 3..."
value={sendAmount}
onChange={setValue(setSendAmount)}
onChange={setValue(setSendAmount)}
></input>
</label>

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

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


export default Transfer;
46 changes: 32 additions & 14 deletions client/src/Wallet.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
import server from "./server";
import { secp256k1 } from "@noble/curves/secp256k1";
import { bytesToHex } from "@noble/hashes/utils";

function Wallet({ address, setAddress, balance, setBalance }) {

function Wallet({ address, setAddress, balance, setBalance, privateKey, setPrivateKey }) {

async function onChange(evt) {
const address = evt.target.value;

const privateKey = evt.target.value;
setPrivateKey(privateKey);

const address = bytesToHex(secp256k1.getPublicKey(privateKey));
setAddress(address);
if (address) {
const {
data: { balance },
} = await server.get(`balance/${address}`);
setBalance(balance);
} else {
setBalance(0);
}

if (address) {
const {
data: { balance },
} = await server.get(`balance/${address}`);
setBalance(balance);
} else {
setBalance(0);
}
}

return (
<div className="container wallet">
<h1>Your Wallet</h1>

<label>
Wallet Address
<input placeholder="Type an address, for example: 0x1" value={address} onChange={onChange}></input>
Private Key
<input
placeholder="Type in a private key..."
value={privateKey}
onChange={onChange}
></input>
</label>

<div>
Public Key: {address ?
address.slice(0, 10) + "..." + address.slice(-10) :
"Invalid address"}
</div>

<div className="balance">Balance: {balance}</div>
</div>
);
}

export default Wallet;
export default Wallet;
36 changes: 22 additions & 14 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
const express = require("express");
const app = express();
const cors = require("cors");
const { verify } = require("./verification");
const port = 3042;

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

const balances = {
"0x1": 100,
"0x2": 50,
"0x3": 75,
/*public key:*/ "03c781cf2662714a11a3a9f98654d09abf35b59bb29952cf35f55088ce27166dba": 100, // Private Key: 3ae0ffa9b45dcf48baba0c45eb4f4dc3b6d7f4ed8e8d38548409c33d851d3734
/*public key:*/ "0226afe000841b07c5f988abdb85f069d969697f7e576f9ba3217f75086d9b906d": 50 , // Private Key: 328fa7d1cb0ff6fd9e3e86a217f467d17e42aa3125b6c76ca18c0ca8ea07b5d3
/*public key:*/ "020e8767c081c4c37b88a3bee1d0a726f88a5530ea8f8a63221b8e34e7066b0e01": 75 , // Private Key: 214f36fd9ccb8c1cff2665d4f808a0d2227efbb6a7c3c3ac935bbad8f2ba75d0
};

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

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


setInitialBalance(sender);
setInitialBalance(recipient);
if (!verify(signature, "transfer", sender)) {
return res.status(400).send({ message: "You are not the account owner!" });
}

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

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

});


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