|
@@ -2,23 +2,40 @@ package com.platform2easy.genesis.domain.service;
|
|
|
|
|
|
|
|
import com.platform2easy.genesis.domain.model.AppUser;
|
|
import com.platform2easy.genesis.domain.model.AppUser;
|
|
|
import com.platform2easy.genesis.domain.model.Company;
|
|
import com.platform2easy.genesis.domain.model.Company;
|
|
|
|
|
+import com.platform2easy.genesis.domain.model.Wallet;
|
|
|
import com.platform2easy.genesis.domain.repository.AppUserRepository;
|
|
import com.platform2easy.genesis.domain.repository.AppUserRepository;
|
|
|
import com.platform2easy.genesis.domain.repository.CompanyRepository;
|
|
import com.platform2easy.genesis.domain.repository.CompanyRepository;
|
|
|
|
|
+import com.platform2easy.genesis.domain.service.WalletService;
|
|
|
import com.platform2easy.genesis.web.dto.CompanyWithUserDTO;
|
|
import com.platform2easy.genesis.web.dto.CompanyWithUserDTO;
|
|
|
-import lombok.AllArgsConstructor;
|
|
|
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
|
+import com.platform2easy.genesis.lib.BashExecutor;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
+import java.nio.file.Paths;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
@Service
|
|
@Service
|
|
|
-@AllArgsConstructor
|
|
|
|
|
|
|
+@RequiredArgsConstructor
|
|
|
public class CompanyService {
|
|
public class CompanyService {
|
|
|
|
|
|
|
|
private final CompanyRepository repository;
|
|
private final CompanyRepository repository;
|
|
|
private final AppUserRepository userRepository;
|
|
private final AppUserRepository userRepository;
|
|
|
private final PasswordEncoder passwordEncoder;
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
|
+ private final WalletService walletService;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${easycli.path:}")
|
|
|
|
|
+ private String easycliPath;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${chain.polygon.id:137}")
|
|
|
|
|
+ private Long defaultChainId;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${ROOT_DIR:}")
|
|
|
|
|
+ private String rootDir;
|
|
|
|
|
|
|
|
@Transactional
|
|
@Transactional
|
|
|
public Company salvar(Company company) {
|
|
public Company salvar(Company company) {
|
|
@@ -63,6 +80,43 @@ public class CompanyService {
|
|
|
user.setUserFlag("a");
|
|
user.setUserFlag("a");
|
|
|
userRepository.save(user);
|
|
userRepository.save(user);
|
|
|
|
|
|
|
|
|
|
+ String root = (rootDir == null || rootDir.isBlank()) ? System.getenv("ROOT_DIR") : rootDir;
|
|
|
|
|
+ String cliFullPath = (root != null && !root.isBlank())
|
|
|
|
|
+ ? Paths.get(root).resolve("bin").resolve("easycli").toString()
|
|
|
|
|
+ : ((easycliPath == null || easycliPath.isBlank())
|
|
|
|
|
+ ? Paths.get("").toAbsolutePath().resolve("bin").resolve("easycli").toString()
|
|
|
|
|
+ : easycliPath);
|
|
|
|
|
+ String command = cliFullPath + " polygon create-new-address";
|
|
|
|
|
+ BashExecutor.Result result = BashExecutor.run(command, 15000);
|
|
|
|
|
+ if (result.isTimedOut() || result.getExitCode() != 0) {
|
|
|
|
|
+ throw new RuntimeException("Falha ao gerar nova address via CLI: " + result.getOutput());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, String> kv = new HashMap<>();
|
|
|
|
|
+ for (String line : result.getOutput().split("\\R")) {
|
|
|
|
|
+ int i = line.indexOf('=');
|
|
|
|
|
+ if (i > 0) {
|
|
|
|
|
+ String k = line.substring(0, i).trim();
|
|
|
|
|
+ String v = line.substring(i + 1).trim();
|
|
|
|
|
+ kv.put(k, v);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ String privateKey = kv.get("privateKey");
|
|
|
|
|
+ String publicKey = kv.get("publicKey");
|
|
|
|
|
+ String address = kv.get("address");
|
|
|
|
|
+ if (privateKey == null || publicKey == null || address == null) {
|
|
|
|
|
+ throw new RuntimeException("Saída da CLI inválida: " + result.getOutput());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Wallet wallet = new Wallet();
|
|
|
|
|
+ wallet.setCompanyId(company.getId());
|
|
|
|
|
+ wallet.setAddress(address);
|
|
|
|
|
+ wallet.setPublicKey(publicKey);
|
|
|
|
|
+ wallet.setPrivateKey(privateKey);
|
|
|
|
|
+ wallet.setFlag("a");
|
|
|
|
|
+ wallet.setChainId(defaultChainId);
|
|
|
|
|
+ walletService.salvar(wallet);
|
|
|
|
|
+
|
|
|
return company;
|
|
return company;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|