Move some private key methods to Crypto class

This commit is contained in:
catbref 2022-05-14 16:50:52 +01:00
parent c5e5316f2e
commit 51930d3ccf
6 changed files with 24 additions and 16 deletions

View File

@ -11,15 +11,15 @@ public class PrivateKeyAccount extends PublicKeyAccount {
private final Ed25519PrivateKeyParameters edPrivateKeyParams; private final Ed25519PrivateKeyParameters edPrivateKeyParams;
/** /**
* Create PrivateKeyAccount using byte[32] seed. * Create PrivateKeyAccount using byte[32] private key.
* *
* @param seed * @param privateKey
* byte[32] used to create private/public key pair * byte[32] used to create private/public key pair
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if passed invalid seed * if passed invalid privateKey
*/ */
public PrivateKeyAccount(Repository repository, byte[] seed) { public PrivateKeyAccount(Repository repository, byte[] privateKey) {
this(repository, new Ed25519PrivateKeyParameters(seed, 0)); this(repository, new Ed25519PrivateKeyParameters(privateKey, 0));
} }
private PrivateKeyAccount(Repository repository, Ed25519PrivateKeyParameters edPrivateKeyParams) { private PrivateKeyAccount(Repository repository, Ed25519PrivateKeyParameters edPrivateKeyParams) {
@ -37,10 +37,6 @@ public class PrivateKeyAccount extends PublicKeyAccount {
return this.privateKey; return this.privateKey;
} }
public static byte[] toPublicKey(byte[] seed) {
return new Ed25519PrivateKeyParameters(seed, 0).generatePublicKey().getEncoded();
}
public byte[] sign(byte[] message) { public byte[] sign(byte[] message) {
return Crypto.sign(this.edPrivateKeyParams, message); return Crypto.sign(this.edPrivateKeyParams, message);
} }

View File

@ -292,7 +292,7 @@ public class TradeBot implements Listener {
} }
public static byte[] deriveTradeNativePublicKey(byte[] privateKey) { public static byte[] deriveTradeNativePublicKey(byte[] privateKey) {
return PrivateKeyAccount.toPublicKey(privateKey); return Crypto.toPublicKey(privateKey);
} }
public static byte[] deriveTradeForeignPublicKey(byte[] privateKey) { public static byte[] deriveTradeForeignPublicKey(byte[] privateKey) {

View File

@ -253,6 +253,10 @@ public abstract class Crypto {
return false; return false;
} }
public static byte[] toPublicKey(byte[] privateKey) {
return new Ed25519PrivateKeyParameters(privateKey, 0).generatePublicKey().getEncoded();
}
public static boolean verify(byte[] publicKey, byte[] signature, byte[] message) { public static boolean verify(byte[] publicKey, byte[] signature, byte[] message) {
try { try {
return Ed25519.verify(signature, 0, publicKey, 0, message, 0, message.length); return Ed25519.verify(signature, 0, publicKey, 0, message, 0, message.length);
@ -264,7 +268,15 @@ public abstract class Crypto {
public static byte[] sign(Ed25519PrivateKeyParameters edPrivateKeyParams, byte[] message) { public static byte[] sign(Ed25519PrivateKeyParameters edPrivateKeyParams, byte[] message) {
byte[] signature = new byte[SIGNATURE_LENGTH]; byte[] signature = new byte[SIGNATURE_LENGTH];
edPrivateKeyParams.sign(Ed25519.Algorithm.Ed25519, edPrivateKeyParams.generatePublicKey(), null, message, 0, message.length, signature, 0); edPrivateKeyParams.sign(Ed25519.Algorithm.Ed25519,null, message, 0, message.length, signature, 0);
return signature;
}
public static byte[] sign(byte[] privateKey, byte[] message) {
byte[] signature = new byte[SIGNATURE_LENGTH];
new Ed25519PrivateKeyParameters(privateKey, 0).sign(Ed25519.Algorithm.Ed25519,null, message, 0, message.length, signature, 0);
return signature; return signature;
} }
@ -281,5 +293,4 @@ public abstract class Crypto {
return sharedSecret; return sharedSecret;
} }
} }

View File

@ -23,7 +23,6 @@ import java.util.stream.Stream;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.qortal.account.PrivateKeyAccount;
import org.qortal.crypto.Crypto; import org.qortal.crypto.Crypto;
import org.qortal.globalization.Translator; import org.qortal.globalization.Translator;
import org.qortal.gui.SysTray; import org.qortal.gui.SysTray;
@ -1003,7 +1002,7 @@ public class HSQLDBRepository implements Repository {
if (privateKey == null) if (privateKey == null)
return null; return null;
return PrivateKeyAccount.toPublicKey(privateKey); return Crypto.toPublicKey(privateKey);
} }
public static String ed25519PublicKeyToAddress(byte[] publicKey) { public static String ed25519PublicKeyToAddress(byte[] publicKey) {

View File

@ -6,6 +6,7 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jsse.provider.BouncyCastleJsseProvider; import org.bouncycastle.jsse.provider.BouncyCastleJsseProvider;
import org.qortal.account.PrivateKeyAccount; import org.qortal.account.PrivateKeyAccount;
import org.qortal.account.PublicKeyAccount; import org.qortal.account.PublicKeyAccount;
import org.qortal.crypto.Crypto;
import org.qortal.utils.Base58; import org.qortal.utils.Base58;
public class RewardShareKeys { public class RewardShareKeys {
@ -28,7 +29,7 @@ public class RewardShareKeys {
PublicKeyAccount recipientAccount = new PublicKeyAccount(null, args.length > 1 ? Base58.decode(args[1]) : minterAccount.getPublicKey()); PublicKeyAccount recipientAccount = new PublicKeyAccount(null, args.length > 1 ? Base58.decode(args[1]) : minterAccount.getPublicKey());
byte[] rewardSharePrivateKey = minterAccount.getRewardSharePrivateKey(recipientAccount.getPublicKey()); byte[] rewardSharePrivateKey = minterAccount.getRewardSharePrivateKey(recipientAccount.getPublicKey());
byte[] rewardSharePublicKey = PrivateKeyAccount.toPublicKey(rewardSharePrivateKey); byte[] rewardSharePublicKey = Crypto.toPublicKey(rewardSharePrivateKey);
System.out.println(String.format("Minter account: %s", minterAccount.getAddress())); System.out.println(String.format("Minter account: %s", minterAccount.getAddress()));
System.out.println(String.format("Minter's public key: %s", Base58.encode(minterAccount.getPublicKey()))); System.out.println(String.format("Minter's public key: %s", Base58.encode(minterAccount.getPublicKey())));

View File

@ -6,6 +6,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.qortal.account.PrivateKeyAccount; import org.qortal.account.PrivateKeyAccount;
import org.qortal.crypto.Crypto;
import org.qortal.data.transaction.BaseTransactionData; import org.qortal.data.transaction.BaseTransactionData;
import org.qortal.data.transaction.PaymentTransactionData; import org.qortal.data.transaction.PaymentTransactionData;
import org.qortal.data.transaction.RewardShareTransactionData; import org.qortal.data.transaction.RewardShareTransactionData;
@ -45,7 +46,7 @@ public class AccountUtils {
long timestamp = repository.getTransactionRepository().fromSignature(reference).getTimestamp() + 1; long timestamp = repository.getTransactionRepository().fromSignature(reference).getTimestamp() + 1;
byte[] rewardSharePrivateKey = mintingAccount.getRewardSharePrivateKey(recipientAccount.getPublicKey()); byte[] rewardSharePrivateKey = mintingAccount.getRewardSharePrivateKey(recipientAccount.getPublicKey());
byte[] rewardSharePublicKey = PrivateKeyAccount.toPublicKey(rewardSharePrivateKey); byte[] rewardSharePublicKey = Crypto.toPublicKey(rewardSharePrivateKey);
BaseTransactionData baseTransactionData = new BaseTransactionData(timestamp, txGroupId, reference, mintingAccount.getPublicKey(), fee, null); BaseTransactionData baseTransactionData = new BaseTransactionData(timestamp, txGroupId, reference, mintingAccount.getPublicKey(), fee, null);
TransactionData transactionData = new RewardShareTransactionData(baseTransactionData, recipientAccount.getAddress(), rewardSharePublicKey, sharePercent); TransactionData transactionData = new RewardShareTransactionData(baseTransactionData, recipientAccount.getAddress(), rewardSharePublicKey, sharePercent);