package info.frostfs.sdk.jdo; import info.frostfs.sdk.annotations.NotNull; import info.frostfs.sdk.exceptions.FrostFSException; import info.frostfs.sdk.exceptions.ValidationFrostFSException; import io.neow3j.wallet.Account; import io.neow3j.wallet.nep6.NEP6Account; import io.neow3j.wallet.nep6.NEP6Wallet; import lombok.Getter; import org.apache.commons.lang3.StringUtils; import java.io.File; import java.io.FileInputStream; import java.security.PrivateKey; import java.util.Optional; import static info.frostfs.sdk.KeyExtension.loadPrivateKey; import static info.frostfs.sdk.constants.ErrorConst.WALLET_IS_INVALID; import static info.frostfs.sdk.constants.ErrorConst.WIF_IS_INVALID; import static info.frostfs.sdk.constants.FieldConst.EMPTY_STRING; import static io.neow3j.wallet.Wallet.OBJECT_MAPPER; import static java.util.Objects.isNull; @Getter public class ECDsa { @NotNull private final byte[] publicKeyByte; @NotNull private final byte[] privateKeyByte; @NotNull private final PrivateKey privateKey; @NotNull private final Account account; public ECDsa(String wif) { if (StringUtils.isEmpty(wif)) { throw new ValidationFrostFSException(WIF_IS_INVALID); } this.account = Account.fromWIF(wif); this.privateKeyByte = account.getECKeyPair().getPrivateKey().getBytes(); this.publicKeyByte = account.getECKeyPair().getPublicKey().getEncoded(true); this.privateKey = loadPrivateKey(privateKeyByte); } public ECDsa(File walletFile, String password) { if (isNull(walletFile)) { throw new ValidationFrostFSException(WALLET_IS_INVALID); } try (var walletStream = new FileInputStream(walletFile)) { NEP6Wallet nep6Wallet = OBJECT_MAPPER.readValue(walletStream, NEP6Wallet.class); Optional defaultAccount = nep6Wallet.getAccounts().stream() .filter(NEP6Account::getDefault) .findFirst(); var account = defaultAccount.map(Account::fromNEP6Account) .orElseGet(() -> Account.fromNEP6Account(nep6Wallet.getAccounts().get(0))); account.decryptPrivateKey(isNull(password) ? EMPTY_STRING : password); this.account = account; this.privateKeyByte = account.getECKeyPair().getPrivateKey().getBytes(); this.publicKeyByte = account.getECKeyPair().getPublicKey().getEncoded(true); this.privateKey = loadPrivateKey(privateKeyByte); } catch (Exception exp) { throw new FrostFSException(exp.getMessage()); } } }