-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
56 lines (45 loc) · 1.32 KB
/
User.java
File metadata and controls
56 lines (45 loc) · 1.32 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
package org.example;
import java.security.*;
import java.security.spec.ECGenParameterSpec;
public class User {
private final String username;
private final KeyPair keyPair;
private double balance;
public User(String username) {
this.username = username;
this.keyPair = generateKeyPair();
this.balance = 0;
}
public PublicKey getPublicKey() {
return keyPair.getPublic();
}
public PrivateKey getPrivateKey() {
return keyPair.getPrivate();
}
public double getBalance() {
return balance;
}
public void addToBalance(double amount) {
balance += amount;
}
private KeyPair generateKeyPair() {
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256k1");
keyGen.initialize(ecSpec, new SecureRandom());
return keyGen.generateKeyPair();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String getUsername() {
return username;
}
public KeyPair getKeyPair() {
return keyPair;
}
public void setBalance(double balance) {
this.balance = balance;
}
}