Merge 86762370d8bf6ce221c7cb51d9004c5a81f5294a into faee7c8f6a9fe85dcc4fb3c5ffacdde72668137f

This commit is contained in:
cwd.systems | 0KN 2025-02-17 13:54:28 +01:00 committed by GitHub
commit af07acf661
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,51 +5,77 @@ import org.qortal.crypto.Crypto;
import org.qortal.data.account.AccountData; import org.qortal.data.account.AccountData;
import org.qortal.repository.Repository; import org.qortal.repository.Repository;
import java.util.Objects;
public class PublicKeyAccount extends Account { public class PublicKeyAccount extends Account {
protected final byte[] publicKey; protected final Ed25519PublicKeyParameters edPublicKeyParams;
protected final Ed25519PublicKeyParameters edPublicKeyParams; protected final byte[] publicKey;
public PublicKeyAccount(Repository repository, byte[] publicKey) { /**
this(repository, new Ed25519PublicKeyParameters(publicKey, 0)); * 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, validatePublicKey(publicKey));
}
protected PublicKeyAccount(Repository repository, Ed25519PublicKeyParameters edPublicKeyParams) { protected PublicKeyAccount(Repository repository, Ed25519PublicKeyParameters edPublicKeyParams) {
super(repository, Crypto.toAddress(edPublicKeyParams.getEncoded())); super(repository, Crypto.toAddress(edPublicKeyParams.getEncoded()));
this.edPublicKeyParams = edPublicKeyParams;
this.publicKey = edPublicKeyParams.getEncoded();
}
this.edPublicKeyParams = edPublicKeyParams; /**
this.publicKey = edPublicKeyParams.getEncoded(); * 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");
}
return new Ed25519PublicKeyParameters(publicKey, 0);
}
protected PublicKeyAccount(Repository repository, byte[] publicKey, String address) { public byte[] getPublicKey() {
super(repository, address); return this.publicKey;
}
this.publicKey = publicKey; @Override
this.edPublicKeyParams = null; protected AccountData buildPublicKeyAccountData() {
} AccountData accountData = super.buildAccountData();
accountData.setPublicKey(this.publicKey);
return accountData;
}
protected PublicKeyAccount() { /**
this.publicKey = null; * Verifies the given signature for the message using this account's public key.
this.edPublicKeyParams = null; *
} * @param signature The signature to verify.
* @param message The original message.
public byte[] getPublicKey() { * @return true if the signature is valid, false otherwise.
return this.publicKey; * @throws IllegalStateException if the public key parameters are not initialized.
} */
public boolean verify(byte[] signature, byte[] message) {
@Override if (edPublicKeyParams == null) {
protected AccountData buildAccountData() { throw new IllegalStateException("Public key parameters not initialized");
AccountData accountData = super.buildAccountData(); }
accountData.setPublicKey(this.publicKey); return Crypto.verify(this.publicKey, signature, message);
return accountData; }
}
public boolean verify(byte[] signature, byte[] message) {
return Crypto.verify(this.publicKey, signature, message);
}
public static String getAddress(byte[] publicKey) {
return Crypto.toAddress(publicKey);
}
/**
* 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);
}
} }