From 4939ddb2d9d4f4b982ffeb75279414b63d16a005 Mon Sep 17 00:00:00 2001 From: Ronak Thacker Date: Thu, 25 May 2023 18:51:57 +0530 Subject: [PATCH] feat: ssi:lib version updated --- build.gradle | 3 +- .../controller/DidDocumentController.java | 3 +- .../dao/entity/Wallet.java | 8 ++-- .../service/DidDocumentService.java | 3 +- .../service/WalletService.java | 35 +++++++++++++---- .../utils/StringToDidDocumentConverter.java | 38 +++++++++++++++++++ .../DidDocumentsTest.java | 19 +++++++++- 7 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 src/main/java/org/eclipse/tractusx/managedidentitywallets/utils/StringToDidDocumentConverter.java diff --git a/build.gradle b/build.gradle index ed05f3032..cc8f8cf28 100644 --- a/build.gradle +++ b/build.gradle @@ -43,8 +43,7 @@ dependencies { implementation "org.springdoc:springdoc-openapi-starter-common:${openApiVersion}" implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:${openApiVersion}" implementation 'org.liquibase:liquibase-core' - implementation 'org.eclipse.tractusx.ssi:cx-ssi-lib:0.0.3' - implementation 'org.eclipse.tractusx.ssi:cx-ssi-agent-lib:0.0.3' + implementation 'org.eclipse.tractusx.ssi.lib:cx-ssi-lib:0.0.4' runtimeOnly 'org.postgresql:postgresql' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/DidDocumentController.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/DidDocumentController.java index 5a57860a8..b0e1ae325 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/DidDocumentController.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/controller/DidDocumentController.java @@ -24,6 +24,7 @@ import lombok.RequiredArgsConstructor; import org.eclipse.tractusx.managedidentitywallets.constant.RestURI; import org.eclipse.tractusx.managedidentitywallets.service.DidDocumentService; +import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -46,7 +47,7 @@ public class DidDocumentController { * @return the did document */ @GetMapping(path = RestURI.DID_DOCUMENTS, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity getDidDocument(@PathVariable(name = "identifier") String identifier) { + public ResponseEntity getDidDocument(@PathVariable(name = "identifier") String identifier) { return ResponseEntity.status(HttpStatus.OK).body(service.getDidDocument(identifier)); } } diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/entity/Wallet.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/entity/Wallet.java index 6e6381aed..9de9bce3f 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/entity/Wallet.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/dao/entity/Wallet.java @@ -25,6 +25,8 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.*; +import org.eclipse.tractusx.managedidentitywallets.utils.StringToDidDocumentConverter; +import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; /** * The type Wallet. @@ -54,8 +56,8 @@ public class Wallet extends BaseEntity { @Column(nullable = false) private String algorithm; - - @Column(nullable = false) - private String didDocument; + @Column(nullable = false) + @Convert(converter = StringToDidDocumentConverter.class) + private DidDocument didDocument; } diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/DidDocumentService.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/DidDocumentService.java index 8cb304a37..e1ff927e2 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/DidDocumentService.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/DidDocumentService.java @@ -26,6 +26,7 @@ import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet; import org.eclipse.tractusx.managedidentitywallets.dao.repository.WalletRepository; import org.eclipse.tractusx.managedidentitywallets.exception.DidDocumentsNotFoundProblem; +import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; import org.springframework.stereotype.Service; /** @@ -44,7 +45,7 @@ public class DidDocumentService { * @param identifier the identifier * @return the did document */ - public String getDidDocument(String identifier) { + public DidDocument getDidDocument(String identifier) { Wallet wallet = identifier.startsWith("did:") ? walletRepository.getByDid(identifier) : walletRepository.getByBpn(identifier); if (wallet == null) { throw new DidDocumentsNotFoundProblem("DidDocument not found for identifier " + identifier); diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/WalletService.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/WalletService.java index 1d1a6bc00..178e7611d 100644 --- a/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/WalletService.java +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/service/WalletService.java @@ -41,17 +41,19 @@ import org.eclipse.tractusx.managedidentitywallets.exception.DuplicateWalletProblem; import org.eclipse.tractusx.managedidentitywallets.exception.WalletNotFoundProblem; import org.eclipse.tractusx.managedidentitywallets.utils.EncryptionUtils; -import org.eclipse.tractusx.ssi.agent.lib.DidDocumentBuilder; +import org.eclipse.tractusx.ssi.lib.base.MultibaseFactory; import org.eclipse.tractusx.ssi.lib.crypt.ed25519.Ed25519KeySet; import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory; -import org.eclipse.tractusx.ssi.lib.model.did.Did; -import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; +import org.eclipse.tractusx.ssi.lib.model.MultibaseString; +import org.eclipse.tractusx.ssi.lib.model.did.*; import org.springframework.stereotype.Service; import java.io.StringWriter; +import java.net.URI; import java.net.URLDecoder; import java.nio.charset.Charset; import java.security.SecureRandom; +import java.util.ArrayList; import java.util.List; /** @@ -109,15 +111,32 @@ public Wallet createWallet(CreateWalletRequest request) { //create did json Did did = DidWebFactory.fromHostname(URLDecoder.decode(miwSettings.host() + ":" + request.getBpn(), Charset.defaultCharset())); - DidDocument didDocument = new DidDocumentBuilder() - .withDid(did) - .withEd25519PublicKey(keyPair.getPublicKey()) - .build(); + + //Extracting keys + Ed25519KeySet keySet = new Ed25519KeySet(keyPair.getPrivateKey(), keyPair.getPublicKey()); + MultibaseString publicKeyBase = MultibaseFactory.create(keySet.getPublicKey()); + + //Building Verification Methods: + List verificationMethods = new ArrayList<>(); + Ed25519VerificationKey2020Builder builder = new Ed25519VerificationKey2020Builder(); + Ed25519VerificationKey2020 key = + builder + .id(URI.create(did.toUri() + "#key-" + 1)) + .controller(did.toUri()) + .publicKeyMultiBase(publicKeyBase) + .build(); + verificationMethods.add(key); + + DidDocumentBuilder didDocumentBuilder = new DidDocumentBuilder(); + didDocumentBuilder.id(did.toUri()); + didDocumentBuilder.verificationMethods(verificationMethods); + DidDocument didDocument = didDocumentBuilder.build(); + log.debug("did document created for bpn ->{}", request.getBpn()); //Save wallet Wallet wallet = walletRepository.save(Wallet.builder() - .didDocument("did document") //TODO remove once we have solution in lib didDocument.toString or didDocument.toJson + .didDocument(didDocument) .bpn(request.getBpn()) .name(request.getName()) .did(did.toString()) diff --git a/src/main/java/org/eclipse/tractusx/managedidentitywallets/utils/StringToDidDocumentConverter.java b/src/main/java/org/eclipse/tractusx/managedidentitywallets/utils/StringToDidDocumentConverter.java new file mode 100644 index 000000000..43ef157ed --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/managedidentitywallets/utils/StringToDidDocumentConverter.java @@ -0,0 +1,38 @@ +/* + * ******************************************************************************* + * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ****************************************************************************** + */ + +package org.eclipse.tractusx.managedidentitywallets.utils; + +import jakarta.persistence.AttributeConverter; +import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; + +public class StringToDidDocumentConverter implements AttributeConverter { + + @Override + public String convertToDatabaseColumn(DidDocument didDocument) { + return didDocument.toJson(); + } + + @Override + public DidDocument convertToEntityAttribute(String string) { + return DidDocument.fromJson(string); + } +} \ No newline at end of file diff --git a/src/test/java/org/eclipse/tractusx/managedidentitywallets/DidDocumentsTest.java b/src/test/java/org/eclipse/tractusx/managedidentitywallets/DidDocumentsTest.java index 0956bcdcb..2fd6eca82 100644 --- a/src/test/java/org/eclipse/tractusx/managedidentitywallets/DidDocumentsTest.java +++ b/src/test/java/org/eclipse/tractusx/managedidentitywallets/DidDocumentsTest.java @@ -25,6 +25,7 @@ import org.eclipse.tractusx.managedidentitywallets.constant.RestURI; import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet; import org.eclipse.tractusx.managedidentitywallets.dao.repository.WalletRepository; +import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -61,10 +62,26 @@ void getDidDocumentInvalidBpn404() { @Test @Order(2) void getDidDocumentWithBpn200() { + + String didDocument = """ + { + "id": "did:web:localhost%3Abpn123124", + "verificationMethod": [ + { + "publicKeyMultibase": "z9mo3TUPvEntiBQtHYVXXy5DfxLGgaHa84ZT6Er2qWs4y", + "controller": "did:web:localhost%3Abpn123124", + "id": "did:web:localhost%3Abpn123124#key-1", + "type": "Ed25519VerificationKey2020" + } + ], + "@context": "https://www.w3.org/ns/did/v1" + } + """; + Wallet wallet = Wallet.builder() .bpn(bpn) .did(did) - .didDocument("{\"@context\":[\"https://w3id.org/did/v1\"],\"id\":\"did:web:localhost%3A8080\",\"verificationMethod\":[{\"id\":\"did:web:localhost%3A8080#key-1\",\"type\":\"Ed25519VerificationKey2020\",\"controller\":\"did:web:localhost%3A8080\",\"publicKeyMultibase\":\"zc37wdjQ4VsLEmX2AoKMHjQNxTtQBdouKbhhwEXLhB1fNJqwFDDaWkqRQ2GTWEhvubaNp8v2ox5dK6B4Efk8n2xMR4so6Ybsgck1BVs3i8WfXNsPNj4Gyz2YSS9rqLpdDv8sfMYdemQa4eq3EmrrkSLNukQrdXvGEtr9cV49CM6cYC6MSXc3S8hxuKh32AbKGURrGDnT9b5j4gj59a7Z4d66EWR1FoLHTZqh8YC96qNboYJyPSCEA6ZgXzRqeKKVwpeSxKXWSD4sa3xEHNeSpLrL4ojXNP3LyNbaHZ2nYqNaQ5pxdnUJCUqKYmiSbyRxbCrZCHSY36xab9XRjpQqQPuDtzrUCJQhpspjg5rd14EABDYzX1x54ZDtiYS9y9j2Pkora3phAyW6EBVBK2u2jo2krLeUgYSWzDTfgsWUJgtT9Pwx6aXvAf68tPQTWJtNoCSsSTBUzAZagosQnGLLTqya3Kgqg9VH1H2KuRfRQVTH29LnfuEa3e9Q2vn8sa6tS74oX2b7tKacndbnbw2CSVmbcuc9qZG8GZEc8ikK1Vvw3ukxTr\"}]}") + .didDocument(DidDocument.fromJson(didDocument)) .algorithm("ED25519") .name(bpn) .build();