-
Notifications
You must be signed in to change notification settings - Fork 701
Expand file tree
/
Copy pathTransfer.jsx
More file actions
79 lines (63 loc) · 1.9 KB
/
Transfer.jsx
File metadata and controls
79 lines (63 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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, privateKey }) {
const [sendAmount, setSendAmount] = useState("");
const [recipient, setRecipient] = useState("");
const setValue = (setter) => (evt) => setter(evt.target.value);
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);
}
}
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 in a public key..."
value={recipient}
onChange={setValue(setRecipient)}
></input>
</label>
<input
type="submit"
className="button"
value="Transfer" />
</form>
);
}
export default Transfer;