Skip to content

Commit

Permalink
feat: ssi:lib version updated
Browse files Browse the repository at this point in the history
  • Loading branch information
thackerronak committed May 25, 2023
1 parent aed48ee commit 4939ddb
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 16 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,7 +47,7 @@ public class DidDocumentController {
* @return the did document
*/
@GetMapping(path = RestURI.DID_DOCUMENTS, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> getDidDocument(@PathVariable(name = "identifier") String identifier) {
public ResponseEntity<DidDocument> getDidDocument(@PathVariable(name = "identifier") String identifier) {
return ResponseEntity.status(HttpStatus.OK).body(service.getDidDocument(identifier));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<VerificationMethod> 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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DidDocument, String> {

@Override
public String convertToDatabaseColumn(DidDocument didDocument) {
return didDocument.toJson();
}

@Override
public DidDocument convertToEntityAttribute(String string) {
return DidDocument.fromJson(string);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 4939ddb

Please sign in to comment.