mirror of
https://github.com/Qortal/qortal.git
synced 2025-04-01 17:55:54 +00:00
Update PublicKeyAccount.java
Hardened. Try this out and see if this works better.
This commit is contained in:
parent
c2bfa26376
commit
86762370d8
@ -5,32 +5,42 @@ import org.qortal.crypto.Crypto;
|
||||
import org.qortal.data.account.AccountData;
|
||||
import org.qortal.repository.Repository;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class PublicKeyAccount extends Account {
|
||||
|
||||
protected final byte[] publicKey;
|
||||
protected final Ed25519PublicKeyParameters edPublicKeyParams;
|
||||
protected final byte[] publicKey;
|
||||
|
||||
/**
|
||||
* Constructs a PublicKeyAccount from a public key byte array.
|
||||
*
|
||||
* @param repository The repository to use for database operations.
|
||||
* @param publicKey The public key as a byte array.
|
||||
* @throws IllegalArgumentException if the public key is invalid.
|
||||
*/
|
||||
public PublicKeyAccount(Repository repository, byte[] publicKey) {
|
||||
this(repository, new Ed25519PublicKeyParameters(publicKey, 0));
|
||||
this(repository, validatePublicKey(publicKey));
|
||||
}
|
||||
|
||||
protected PublicKeyAccount(Repository repository, Ed25519PublicKeyParameters edPublicKeyParams) {
|
||||
super(repository, Crypto.toAddress(edPublicKeyParams.getEncoded()));
|
||||
|
||||
this.edPublicKeyParams = edPublicKeyParams;
|
||||
this.publicKey = edPublicKeyParams.getEncoded();
|
||||
}
|
||||
|
||||
protected PublicKeyAccount(Repository repository, byte[] publicKey, String address) {
|
||||
super(repository, address);
|
||||
|
||||
this.publicKey = publicKey;
|
||||
this.edPublicKeyParams = null;
|
||||
/**
|
||||
* Validates the public key before use.
|
||||
*
|
||||
* @param publicKey The public key to validate.
|
||||
* @return Ed25519PublicKeyParameters object if valid.
|
||||
* @throws IllegalArgumentException if the public key is invalid.
|
||||
*/
|
||||
private static Ed25519PublicKeyParameters validatePublicKey(byte[] publicKey) {
|
||||
if (publicKey == null || publicKey.length != Ed25519PublicKeyParameters.KEY_SIZE) {
|
||||
throw new IllegalArgumentException("Invalid public key size");
|
||||
}
|
||||
|
||||
protected PublicKeyAccount() {
|
||||
this.publicKey = null;
|
||||
this.edPublicKeyParams = null;
|
||||
return new Ed25519PublicKeyParameters(publicKey, 0);
|
||||
}
|
||||
|
||||
public byte[] getPublicKey() {
|
||||
@ -38,18 +48,34 @@ public class PublicKeyAccount extends Account {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AccountData buildAccountData() {
|
||||
protected AccountData buildPublicKeyAccountData() {
|
||||
AccountData accountData = super.buildAccountData();
|
||||
accountData.setPublicKey(this.publicKey);
|
||||
return accountData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the given signature for the message using this account's public key.
|
||||
*
|
||||
* @param signature The signature to verify.
|
||||
* @param message The original message.
|
||||
* @return true if the signature is valid, false otherwise.
|
||||
* @throws IllegalStateException if the public key parameters are not initialized.
|
||||
*/
|
||||
public boolean verify(byte[] signature, byte[] message) {
|
||||
if (edPublicKeyParams == null) {
|
||||
throw new IllegalStateException("Public key parameters not initialized");
|
||||
}
|
||||
return Crypto.verify(this.publicKey, signature, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an address from a public key.
|
||||
*
|
||||
* @param publicKey The public key to convert to an address.
|
||||
* @return The address string.
|
||||
*/
|
||||
public static String getAddress(byte[] publicKey) {
|
||||
return Crypto.toAddress(publicKey);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user