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

Public Key Cryptography #25

Open
wants to merge 3 commits 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
625 changes: 363 additions & 262 deletions client/package-lock.json

Large diffs are not rendered by default.

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.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
4 changes: 4 additions & 0 deletions client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import { useState } from "react";
function App() {
const [balance, setBalance] = useState(0);
const [address, setAddress] = useState("");
const [privateKey, setPrivateKey] = useState("");


return (
<div className="app">
<Wallet
balance={balance}
privateKey={privateKey}
setPrivateKey={setPrivateKey}
setBalance={setBalance}
address={address}
setAddress={setAddress}
Expand Down
16 changes: 12 additions & 4 deletions client/src/Wallet.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import server from "./server";
import * as secp from 'ethereum-cryptography/secp256k1';
import { toHex } from 'ethereum-cryptography/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 = toHex(secp.secp256k1.getPublicKey(privateKey));
setAddress(address);
if (address) {
const {
Expand All @@ -19,10 +23,14 @@ function Wallet({ address, setAddress, balance, setBalance }) {
<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>
Address: {address}
</div>

<div className="balance">Balance: {balance}</div>
</div>
);
Expand Down
6 changes: 1 addition & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ This project is an example of using a client and server to facilitate transfers

However, something that we would like to incoporate is Public Key Cryptography. By using Elliptic Curve Digital Signatures we can make it so the server only allows transfers that have been signed for by the person who owns the associated address.

### Video instructions
For an overview of this project as well as getting started instructions, check out the following video:

https://www.loom.com/share/0d3c74890b8e44a5918c4cacb3f646c4

### Client

The client folder contains a [react app](https://reactjs.org/) using [vite](https://vitejs.dev/). To get started, follow these steps:
Expand All @@ -29,3 +24,4 @@ The server folder contains a node.js server using [express](https://expressjs.co
The application should connect to the default server port (3042) automatically!

_Hint_ - Use [nodemon](https://www.npmjs.com/package/nodemon) instead of `node` to automatically restart the server on any changes.

6 changes: 3 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,
"030a516fbcb67b1fe1cbd06f89bf1972d46efe12583d6bb759d8ac62f017b37b8b": 100,
"034633f85e62385ae67846e10a2f676054e1708b600fc71f7207a38fff22be4fd0": 50,
"02e47ac1ff6eee8067bb631666a796a97a175db1beae3975f8929e5f4405fb5369": 75,
};

app.get("/balance/:address", (req, res) => {
Expand Down
115 changes: 115 additions & 0 deletions server/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 server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"ethereum-cryptography": "^2.1.2",
"express": "^4.18.1"
}
}
17 changes: 17 additions & 0 deletions server/scripts/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const secp = require('ethereum-cryptography/secp256k1')
const {toHex} = require('ethereum-cryptography/utils')

const privateKey = secp.secp256k1.utils.randomPrivateKey()
console.log("Private Key: ",toHex(privateKey))
const publicKey = secp.secp256k1.getPublicKey(privateKey)
console.log("Public Key: ",toHex(publicKey))


/* Private Key: c8fb3df36317cc162e09a9f47b7efad25d669fc08ccda014b222ba600c884dff
Public Key: 030a516fbcb67b1fe1cbd06f89bf1972d46efe12583d6bb759d8ac62f017b37b8b
PS C:\Users\Admin\Documents\week1\week1\server> node scripts/generate.js
Private Key: 4d81bbe416bb3d134b9f453553fac9d1a1fd5373014abbea87d43f505c9e12d8
Public Key: 034633f85e62385ae67846e10a2f676054e1708b600fc71f7207a38fff22be4fd0
PS C:\Users\Admin\Documents\week1\week1\server> node scripts/generate.js
Private Key: 9794de0cfa67c300376bc1c006caab68caeb67ddf6750e08b3fc25b707731c7c
Public Key: 02e47ac1ff6eee8067bb631666a796a97a175db1beae3975f8929e5f4405fb5369 */