diff --git a/src/main/java/org/qortal/api/resource/ArbitraryResource.java b/src/main/java/org/qortal/api/resource/ArbitraryResource.java index 46bd9c5b..3ddc217c 100644 --- a/src/main/java/org/qortal/api/resource/ArbitraryResource.java +++ b/src/main/java/org/qortal/api/resource/ArbitraryResource.java @@ -261,35 +261,42 @@ public class ArbitraryResource { throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.NON_PRODUCTION); } - if (creatorPublicKeyBase58 == null || path == null) { - throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA); - } - byte[] creatorPublicKey = Base58.decode(creatorPublicKeyBase58); - - String name = null; - byte[] secret = null; - Method method = Method.PUT; - Service service = Service.ARBITRARY_DATA; - Compression compression = Compression.NONE; - - ArbitraryDataWriter arbitraryDataWriter = new ArbitraryDataWriter(Paths.get(path), name, service, method, compression); - try { - arbitraryDataWriter.save(); - } catch (IOException | DataException | InterruptedException e) { - LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage()); - throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE); - } catch (RuntimeException e) { - LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage()); - throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA); - } - - ArbitraryDataFile arbitraryDataFile = arbitraryDataWriter.getArbitraryDataFile(); - if (arbitraryDataFile == null) { - throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA); - } - + ArbitraryDataFile arbitraryDataFile = null; try (final Repository repository = RepositoryManager.getRepository()) { + if (creatorPublicKeyBase58 == null || path == null) { + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA); + } + byte[] creatorPublicKey = Base58.decode(creatorPublicKeyBase58); + final String creatorAddress = Crypto.toAddress(creatorPublicKey); + final byte[] lastReference = repository.getAccountRepository().getLastReference(creatorAddress); + if (lastReference == null) { + LOGGER.info(String.format("Qortal account %s has no last reference", creatorAddress)); + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA); + } + + String name = null; + byte[] secret = null; + Method method = Method.PUT; + Service service = Service.ARBITRARY_DATA; + Compression compression = Compression.NONE; + + ArbitraryDataWriter arbitraryDataWriter = new ArbitraryDataWriter(Paths.get(path), name, service, method, compression); + try { + arbitraryDataWriter.save(); + } catch (IOException | DataException | InterruptedException e) { + LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage()); + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE); + } catch (RuntimeException e) { + LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage()); + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA); + } + + arbitraryDataFile = arbitraryDataWriter.getArbitraryDataFile(); + if (arbitraryDataFile == null) { + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA); + } + ArbitraryDataFile.ValidationResult validationResult = arbitraryDataFile.isValid(); if (validationResult != ArbitraryDataFile.ValidationResult.OK) { LOGGER.error("Invalid file: {}", validationResult); @@ -310,9 +317,6 @@ public class ArbitraryResource { throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA); } - final String creatorAddress = Crypto.toAddress(creatorPublicKey); - final byte[] lastReference = repository.getAccountRepository().getLastReference(creatorAddress); - final BaseTransactionData baseTransactionData = new BaseTransactionData(NTP.getTime(), Group.NO_GROUP, lastReference, creatorPublicKey, BlockChain.getInstance().getUnitFee(), null); final int size = (int) arbitraryDataFile.size(); diff --git a/src/main/java/org/qortal/api/resource/WebsiteResource.java b/src/main/java/org/qortal/api/resource/WebsiteResource.java index 588a8cd2..9aafe4da 100644 --- a/src/main/java/org/qortal/api/resource/WebsiteResource.java +++ b/src/main/java/org/qortal/api/resource/WebsiteResource.java @@ -96,42 +96,46 @@ public class WebsiteResource { throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.NON_PRODUCTION); } - if (creatorPublicKeyBase58 == null || path == null) { - throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA); - } - byte[] creatorPublicKey = Base58.decode(creatorPublicKeyBase58); - - String name = "CalDescentTest1"; // TODO: dynamic - ArbitraryTransactionData.Method method = ArbitraryTransactionData.Method.PATCH; - ArbitraryTransactionData.Service service = ArbitraryTransactionData.Service.WEBSITE; - ArbitraryTransactionData.Compression compression = ArbitraryTransactionData.Compression.ZIP; - - ArbitraryDataWriter arbitraryDataWriter = new ArbitraryDataWriter(Paths.get(path), name, service, method, compression); - try { - arbitraryDataWriter.save(); - } catch (IOException | DataException | InterruptedException e) { - LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage()); - throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE); - } catch (RuntimeException e) { - LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage()); - throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA); - } - - ArbitraryDataFile arbitraryDataFile = arbitraryDataWriter.getArbitraryDataFile(); - if (arbitraryDataFile == null) { - throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA); - } - - String digest58 = arbitraryDataFile.digest58(); - if (digest58 == null) { - LOGGER.error("Unable to calculate digest"); - throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA); - } - + ArbitraryDataFile arbitraryDataFile = null; try (final Repository repository = RepositoryManager.getRepository()) { + if (creatorPublicKeyBase58 == null || path == null) { + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA); + } + byte[] creatorPublicKey = Base58.decode(creatorPublicKeyBase58); final String creatorAddress = Crypto.toAddress(creatorPublicKey); final byte[] lastReference = repository.getAccountRepository().getLastReference(creatorAddress); + if (lastReference == null) { + LOGGER.info(String.format("Qortal account %s has no last reference", creatorAddress)); + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA); + } + + String name = "CalDescentTest1"; // TODO: dynamic + ArbitraryTransactionData.Method method = ArbitraryTransactionData.Method.PUT; // TODO: dynamic + ArbitraryTransactionData.Service service = ArbitraryTransactionData.Service.WEBSITE; + ArbitraryTransactionData.Compression compression = ArbitraryTransactionData.Compression.ZIP; + + ArbitraryDataWriter arbitraryDataWriter = new ArbitraryDataWriter(Paths.get(path), name, service, method, compression); + try { + arbitraryDataWriter.save(); + } catch (IOException | DataException | InterruptedException e) { + LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage()); + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE); + } catch (RuntimeException e) { + LOGGER.info("Unable to create arbitrary data file: {}", e.getMessage()); + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA); + } + + arbitraryDataFile = arbitraryDataWriter.getArbitraryDataFile(); + if (arbitraryDataFile == null) { + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA); + } + + String digest58 = arbitraryDataFile.digest58(); + if (digest58 == null) { + LOGGER.error("Unable to calculate digest"); + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA); + } final BaseTransactionData baseTransactionData = new BaseTransactionData(NTP.getTime(), Group.NO_GROUP, lastReference, creatorPublicKey, BlockChain.getInstance().getUnitFee(), null);