-
Notifications
You must be signed in to change notification settings - Fork 702
Expand file tree
/
Copy pathTransfer.jsx
More file actions
126 lines (108 loc) · 3.55 KB
/
Transfer.jsx
File metadata and controls
126 lines (108 loc) · 3.55 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { getRandomBytesSync } from "ethereum-cryptography/random.js";
import { keccak256 } from "ethereum-cryptography/keccak.js";
import { utf8ToBytes, toHex } from "ethereum-cryptography/utils.js"
import { secp256k1 } from "ethereum-cryptography/secp256k1";
import { useState } from "react";
import server from "./server";
function Transfer({ address, setBalance }) {
const [signature, setSignature] = useState("");
const [sendAmount, setSendAmount] = useState("");
const [recipient, setRecipient] = useState("");
const [isValid, setValid] = useState("");
const [txMessage, setTxMessage] = useState("");
const [txHash, setTxHash] = useState("");
const transferButton = document.getElementById("transferButton");
const setValue = (setter) => (evt) => setter(evt.target.value);
async function transfer(evt) {
evt.preventDefault();
try {
const {
data: { balance },
} = await server.post(`send`, {
sender: address,
amount: parseInt(sendAmount),
recipient,
signature,
txHash,
});
setBalance(balance);
} catch (ex) {
alert(ex.response.data.message);
}
}
function CreateTransaction(evt) {
evt.preventDefault();
transferButton.disabled = true;
const salt = toHex(getRandomBytesSync(32));
const txMessage = recipient + ':' + sendAmount + ':' + salt;
const txHash = toHex(keccak256(utf8ToBytes(txMessage)));
setTxMessage(txMessage);
setTxHash(txHash);
}
function verification(evt) {
const signature = evt.target.value;
setSignature(signature);
if (signature) {
try {
const sig = secp256k1.Signature.fromCompact(signature);
for (let i = 0; i < 4; i++) {
let publicKey = sig.addRecoveryBit(i).recoverPublicKey(txHash).toRawBytes();
let publicKeyHash = keccak256(publicKey.slice(1));
let recoveredAddress = toHex(publicKeyHash.slice(publicKeyHash.length - 20));
if (recoveredAddress === address) {
setValid('Approved');
transferButton.disabled = false;
break;
}
}
} catch {
setValid('Signature is not valid');
transferButton.disabled = true;
}
} else {
setValid("Undefined");
transferButton.disabled = true;
}
}
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>
<input type="button" className="button" value="Create Transaction" onClick={CreateTransaction} />
<div className="transaction">
<div className="up">Transaction: </div>
<div className="low">{txMessage}</div>
</div>
<div className="transaction">
<div className="up">Transaction hash: </div>
<div className="low">{txHash}</div>
</div>
<label>
Verify
<input
placeholder="hex signature"
value={signature}
onChange={verification}
></input>
</label>
<div className="Verification">Verification status: {isValid}</div>
<input type="submit" disabled id="transferButton" className="button" value="Transfer" />
</form>
);
}
export default Transfer;