|
| 1 | +/* |
| 2 | + * Copyright 2024 IEXEC BLOCKCHAIN TECH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.iexec.commons.poco.chain; |
| 18 | + |
| 19 | +import com.iexec.commons.poco.encoding.AssetDataEncoder; |
| 20 | +import lombok.extern.slf4j.Slf4j; |
| 21 | +import org.web3j.utils.Numeric; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.math.BigInteger; |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +@Slf4j |
| 28 | +public abstract class AbstractAssetDeploymentService { |
| 29 | + private static final String ASSET_REGISTRY_ADDRESS_INITIALIZATION_NEEDED = |
| 30 | + "Registry address has not been initialized, please call initRegistryAddress"; |
| 31 | + protected final SignerService signerService; |
| 32 | + |
| 33 | + private final String assetRegistrySelector; |
| 34 | + private String assetRegistryAddress; |
| 35 | + |
| 36 | + protected AbstractAssetDeploymentService(SignerService signerService, String assetRegistrySelector) { |
| 37 | + this.signerService = signerService; |
| 38 | + this.assetRegistrySelector = assetRegistrySelector; |
| 39 | + } |
| 40 | + |
| 41 | + public void initRegistryAddress(String iexecHubAddress) throws IOException { |
| 42 | + if (this.assetRegistryAddress != null) { |
| 43 | + return; |
| 44 | + } |
| 45 | + this.assetRegistryAddress = toEthereumAddress(signerService.sendCall(iexecHubAddress, assetRegistrySelector)); |
| 46 | + } |
| 47 | + |
| 48 | + public String callCreateAsset(String assetTxData) throws IOException { |
| 49 | + Objects.requireNonNull(assetRegistryAddress, ASSET_REGISTRY_ADDRESS_INITIALIZATION_NEEDED); |
| 50 | + return toEthereumAddress(signerService.sendCall(assetRegistryAddress, assetTxData)); |
| 51 | + } |
| 52 | + |
| 53 | + public boolean isAssetDeployed(String address) throws IOException { |
| 54 | + Objects.requireNonNull(assetRegistryAddress, ASSET_REGISTRY_ADDRESS_INITIALIZATION_NEEDED); |
| 55 | + final String isRegisteredTxData = AssetDataEncoder.encodeIsRegistered(address); |
| 56 | + return Numeric.toBigInt(signerService.sendCall(assetRegistryAddress, isRegisteredTxData)).equals(BigInteger.ONE); |
| 57 | + } |
| 58 | + |
| 59 | + public String submitAssetTxData(BigInteger nonce, BigInteger gasPrice, String assetTxData) throws IOException { |
| 60 | + Objects.requireNonNull(assetRegistryAddress, ASSET_REGISTRY_ADDRESS_INITIALIZATION_NEEDED); |
| 61 | + return signerService.signAndSendTransaction(nonce, gasPrice, assetRegistryAddress, assetTxData); |
| 62 | + } |
| 63 | + |
| 64 | + private String toEthereumAddress(String hexaString) { |
| 65 | + return Numeric.toHexStringWithPrefixZeroPadded( |
| 66 | + Numeric.toBigInt(hexaString), 40); |
| 67 | + } |
| 68 | +} |
0 commit comments