Skip to content

Commit a8d6748

Browse files
authored
Replace CredentialsService with SignerService (#143)
1 parent b71ab75 commit a8d6748

File tree

8 files changed

+109
-63
lines changed

8 files changed

+109
-63
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44

55
## [[NEXT]](https://github.com/iExecBlockchainComputing/iexec-blockchain-adapter-api/releases/tag/vNEXT) 2024
66

7+
### New Features
8+
9+
- Replace `CredentialsService` with `SignerService`. (#143)
10+
711
### Quality
812

913
- Use `Instant` instead of `DateTimeUtils`. (#138)

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version=8.4.0
22
iexecCommonVersion=8.4.0
3-
iexecCommonsPocoVersion=3.2.0
3+
iexecCommonsPocoVersion=4.0.0-NEXT-SNAPSHOT
44

55
nexusUser
66
nexusPassword

src/itest/java/com/iexec/blockchain/IntegrationTests.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 IEXEC BLOCKCHAIN TECH
2+
* Copyright 2021-2024 IEXEC BLOCKCHAIN TECH
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import com.iexec.blockchain.api.BlockchainAdapterApiClientBuilder;
2121
import com.iexec.blockchain.broker.BrokerService;
2222
import com.iexec.blockchain.tool.ChainConfig;
23-
import com.iexec.blockchain.tool.CredentialsService;
2423
import com.iexec.blockchain.tool.IexecHubService;
2524
import com.iexec.common.chain.adapter.args.TaskFinalizeArgs;
2625
import com.iexec.common.sdk.broker.BrokerOrder;
@@ -54,7 +53,6 @@
5453
import org.testcontainers.junit.jupiter.Container;
5554
import org.testcontainers.junit.jupiter.Testcontainers;
5655
import org.web3j.crypto.Hash;
57-
import org.web3j.crypto.Sign;
5856
import org.web3j.protocol.core.methods.response.TransactionReceipt;
5957

6058
import java.io.File;
@@ -111,16 +109,16 @@ static void registerProperties(DynamicPropertyRegistry registry) {
111109
private int randomServerPort;
112110

113111
private final IexecHubService iexecHubService;
114-
private final CredentialsService credentialsService;
112+
private final SignerService signerService;
115113
private final BrokerService brokerService;
116114
private final ChainConfig chainConfig;
117115
private BlockchainAdapterApiClient appClient;
118116

119117
@Autowired
120-
IntegrationTests(IexecHubService iexecHubService, CredentialsService credentialsService,
118+
IntegrationTests(IexecHubService iexecHubService, SignerService signerService,
121119
BrokerService brokerService, ChainConfig chainConfig) {
122120
this.iexecHubService = iexecHubService;
123-
this.credentialsService = credentialsService;
121+
this.signerService = signerService;
124122
this.brokerService = brokerService;
125123
this.chainConfig = chainConfig;
126124
}
@@ -138,7 +136,7 @@ private static String getServiceUrl(String serviceHost, int servicePort) {
138136
}
139137

140138
@Test
141-
public void shouldBeFinalized() throws Exception {
139+
void shouldBeFinalized() throws Exception {
142140
TransactionReceipt receipt;
143141
String dealId = triggerDeal(1);
144142

@@ -176,7 +174,7 @@ public void shouldBeFinalized() throws Exception {
176174
}
177175

178176
@Test
179-
public void shouldBurstTransactionsWithAverageOfOneTxPerBlock() {
177+
void shouldBurstTransactionsWithAverageOfOneTxPerBlock() {
180178
int taskVolume = 10;//small volume ensures reasonable execution time on CI/CD
181179
String dealId = triggerDeal(taskVolume);
182180
List<CompletableFuture<Void>> txCompletionWatchers = new ArrayList<>();
@@ -226,14 +224,14 @@ private String triggerDeal(int taskVolume) {
226224
log.info("Created datasetAddress: {}", datasetAddress);
227225

228226
OrderSigner orderSigner = new OrderSigner(
229-
chainConfig.getId(), chainConfig.getHubAddress(), credentialsService.getCredentials().getEcKeyPair());
227+
chainConfig.getId(), chainConfig.getHubAddress(), signerService.getCredentials().getEcKeyPair());
230228
AppOrder signedAppOrder = orderSigner.signAppOrder(buildAppOrder(appAddress, taskVolume));
231229
WorkerpoolOrder signedWorkerpoolOrder = orderSigner.signWorkerpoolOrder(buildWorkerpoolOrder(workerpool, taskVolume));
232230
DatasetOrder signedDatasetOrder = orderSigner.signDatasetOrder(buildDatasetOrder(datasetAddress, taskVolume));
233231
RequestOrder signedRequestOrder = orderSigner.signRequestOrder(buildRequestOrder(signedAppOrder,
234232
signedWorkerpoolOrder,
235233
signedDatasetOrder,
236-
credentialsService.getCredentials().getAddress(),
234+
signerService.getAddress(),
237235
DealParams.builder()
238236
.iexecArgs("abc")
239237
.iexecResultStorageProvider("ipfs")
@@ -377,20 +375,19 @@ private void waitBeforeFinalizing(String chainTaskId) {
377375

378376
public WorkerpoolAuthorization mockAuthorization(String chainTaskId,
379377
String enclaveChallenge) {
380-
String workerWallet = credentialsService.getCredentials().getAddress();
381-
String hash = HashUtils.concatenateAndHash(
378+
final String workerWallet = signerService.getAddress();
379+
final String hash = HashUtils.concatenateAndHash(
382380
workerWallet,
383381
chainTaskId,
384382
enclaveChallenge);
385383

386-
Sign.SignatureData sign = Sign.signPrefixedMessage(BytesUtils.stringToBytes(hash),
387-
credentialsService.getCredentials().getEcKeyPair());
384+
final Signature signature = signerService.signMessageHash(hash);
388385

389386
return WorkerpoolAuthorization.builder()
390387
.workerWallet(workerWallet)
391388
.chainTaskId(chainTaskId)
392389
.enclaveChallenge(enclaveChallenge)
393-
.signature(new Signature(sign))
390+
.signature(signature)
394391
.build();
395392
}
396393

src/main/java/com/iexec/blockchain/tool/CredentialsService.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/main/java/com/iexec/blockchain/tool/IexecHubService.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
package com.iexec.blockchain.tool;
1818

1919
import com.iexec.common.worker.result.ResultUtils;
20-
import com.iexec.commons.poco.chain.ChainDeal;
21-
import com.iexec.commons.poco.chain.ChainTask;
22-
import com.iexec.commons.poco.chain.ChainTaskStatus;
23-
import com.iexec.commons.poco.chain.IexecHubAbstractService;
20+
import com.iexec.commons.poco.chain.*;
2421
import com.iexec.commons.poco.utils.BytesUtils;
2522
import org.apache.commons.lang3.StringUtils;
2623
import org.springframework.stereotype.Service;
@@ -38,11 +35,11 @@
3835
@Service
3936
public class IexecHubService extends IexecHubAbstractService {
4037

41-
public IexecHubService(CredentialsService credentialsService,
38+
public IexecHubService(SignerService signerService,
4239
Web3jService web3jService,
4340
ChainConfig chainConfig) {
4441
super(
45-
credentialsService.getCredentials(),
42+
signerService.getCredentials(),
4643
web3jService,
4744
chainConfig.getHubAddress()
4845
);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.blockchain.tool;
18+
19+
import com.iexec.commons.poco.chain.SignerService;
20+
import lombok.Value;
21+
import org.springframework.boot.context.properties.ConfigurationProperties;
22+
import org.springframework.boot.context.properties.ConstructorBinding;
23+
import org.springframework.context.annotation.Bean;
24+
25+
@Value
26+
@ConstructorBinding
27+
@ConfigurationProperties(prefix = "wallet")
28+
public class WalletConfiguration {
29+
String path;
30+
String password;
31+
32+
@Bean
33+
SignerService signerService(Web3jService web3jService, ChainConfig chainConfig) throws Exception {
34+
return new SignerService(web3jService.getWeb3j(), chainConfig.getId(), password, path);
35+
}
36+
}

src/test/java/com/iexec/blockchain/tool/IexecHubServiceTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.iexec.blockchain.tool;
1818

19+
import com.iexec.commons.poco.chain.SignerService;
1920
import com.iexec.commons.poco.contract.generated.IexecHubContract;
2021
import lombok.SneakyThrows;
2122
import org.junit.jupiter.api.BeforeEach;
@@ -39,7 +40,7 @@ class IexecHubServiceTests {
3940
.hubAddress("0xC129e7917b7c7DeDfAa5Fff1FB18d5D7050fE8ca")
4041
.build();
4142
@Mock
42-
private CredentialsService credentialsService;
43+
private SignerService signerService;
4344
@Mock
4445
private IexecHubContract iexecHubContract;
4546
@Mock
@@ -52,8 +53,8 @@ class IexecHubServiceTests {
5253
void init() {
5354
MockitoAnnotations.openMocks(this);
5455
Credentials credentials = createEthereumCredentials();
55-
when(credentialsService.getCredentials()).thenReturn(credentials);
56-
iexecHubService = new IexecHubService(credentialsService, web3jService, chainConfig);
56+
when(signerService.getCredentials()).thenReturn(credentials);
57+
iexecHubService = new IexecHubService(signerService, web3jService, chainConfig);
5758
ReflectionTestUtils.setField(iexecHubService, "iexecHubContract", iexecHubContract);
5859
}
5960

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.blockchain.tool;
18+
19+
import com.iexec.commons.poco.chain.SignerService;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.io.TempDir;
22+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
23+
import org.web3j.crypto.WalletUtils;
24+
25+
import java.io.File;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
class WalletConfigurationTest {
30+
private final ApplicationContextRunner runner = new ApplicationContextRunner();
31+
@TempDir
32+
private File tempWalletDir;
33+
34+
@Test
35+
void shouldCreateBeans() throws Exception {
36+
final String tempWalletName = WalletUtils.generateFullNewWalletFile("changeit", tempWalletDir);
37+
final String tempWalletPath = tempWalletDir.getAbsolutePath() + File.separator + tempWalletName;
38+
runner.withBean(ChainConfig.class, 65535, "http://localhost:8545", 5, "0xC129e7917b7c7DeDfAa5Fff1FB18d5D7050fE8ca", true, 1.0f, 0L, 2)
39+
.withBean(IexecHubService.class)
40+
.withBean(WalletConfiguration.class, tempWalletPath, "changeit")
41+
.withBean(Web3jService.class)
42+
.run(context -> assertThat(context)
43+
.hasSingleBean(ChainConfig.class)
44+
.hasSingleBean(IexecHubService.class)
45+
.hasSingleBean(SignerService.class)
46+
.hasSingleBean(WalletConfiguration.class)
47+
.hasSingleBean(Web3jService.class));
48+
}
49+
}

0 commit comments

Comments
 (0)