Commit 5f3d57ca authored by Konstantin Munichev's avatar Konstantin Munichev
Browse files

[dlt] reconfiguration support

parent 2ca6ae4e
Loading
Loading
Loading
Loading
+8 −6
Original line number Diff line number Diff line
@@ -23,9 +23,6 @@ fun connect(): Gateway {

@OptIn(ExperimentalSerializationApi::class)
suspend fun main(args: Array<String>) {
//    enrollAdmin()
//    registerUser()
//
//    // connect to the network and invoke the smart contract
//    connect().use { gateway ->
//        // get the network and contract
@@ -36,9 +33,14 @@ suspend fun main(args: Array<String>) {
//        val result = contract.evaluateTransaction("GetAllAssets")
//        println("Evaluate Transaction: GetAllAssets, result: " + String(result))
//    }
//
    val cfg = DltConfig.newBuilder().setWallet("wallet123").setConnectionFile("file").setUser("uuser").setChannel("ch1")
        .setContract("basic").build()

    // TODO: default configuration file
    val cfg = DltConfig.newBuilder().setWallet("wallet").setConnectionFile("config/connection-org1.json")
        .setUser("appUser")
        .setChannel("dlt")
        .setContract("basic").setCaCertFile("config/ca.org1.example.com-cert.pem").setCaUrl("https://s2:7054")
        .setCaAdmin("admin").setCaAdminSecret("adminpw").setMsp("Org1MSP").setAffiliation("org1.department1")
        .build()
    val cfgBytes = cfg.toByteArray()

    val client = HttpClient(CIO)
+9 −23
Original line number Diff line number Diff line
@@ -7,37 +7,23 @@
package fabric

import org.hyperledger.fabric.gateway.Identities
import org.hyperledger.fabric.gateway.Wallets
import org.hyperledger.fabric.sdk.security.CryptoSuiteFactory
import org.hyperledger.fabric.gateway.Wallet
import org.hyperledger.fabric_ca.sdk.EnrollmentRequest
import org.hyperledger.fabric_ca.sdk.HFCAClient
import java.nio.file.Paths
import java.util.*

fun enrollAdmin() {
    // Create a CA client for interacting with the CA.
    val props = Properties()
    props["pemFile"] = "config/ca.org1.example.com-cert.pem"
    props["allowAllHostNames"] = "true"
    val caClient = HFCAClient.createNewInstance("https://s2:7054", props)
    val cryptoSuite = CryptoSuiteFactory.getDefault().cryptoSuite
    caClient.cryptoSuite = cryptoSuite

    // Create a wallet for managing identities
    val wallet = Wallets.newFileSystemWallet(Paths.get("wallet"))

fun enrollAdmin(config: proto.Config.DltConfig, caClient: HFCAClient, wallet: Wallet) {
    // Check to see if we've already enrolled the admin user.
    if (wallet.get("admin") != null) {
        println("An identity for the admin user \"admin\" already exists in the wallet")
    if (wallet.get(config.caAdmin) != null) {
        println("An identity for the admin user ${config.caAdmin} already exists in the wallet")
        return
    }

    // Enroll the admin user, and import the new identity into the wallet.
    val enrollmentRequestTLS = EnrollmentRequest()
    enrollmentRequestTLS.addHost("s2")
    enrollmentRequestTLS.addHost(config.caUrl)
    enrollmentRequestTLS.profile = "tls"
    val enrollment = caClient.enroll("admin", "adminpw", enrollmentRequestTLS)
    val user = Identities.newX509Identity("Org1MSP", enrollment)
    wallet.put("admin", user)
    println("Successfully enrolled user \"admin\" and imported it into the wallet")
    val enrollment = caClient.enroll(config.caAdmin, config.caAdminSecret, enrollmentRequestTLS)
    val user = Identities.newX509Identity(config.msp, enrollment)
    wallet.put(config.caAdmin, user)
    println("Successfully enrolled user ${config.caAdmin} and imported it into the wallet")
}
+31 −0
Original line number Diff line number Diff line
package fabric

import org.hyperledger.fabric.gateway.Wallet
import org.hyperledger.fabric.gateway.Wallets
import org.hyperledger.fabric.sdk.security.CryptoSuiteFactory
import org.hyperledger.fabric_ca.sdk.HFCAClient
import proto.Config
import java.nio.file.Paths
import java.util.*

class FabricConnector(val config: Config.DltConfig) {
    private val caClient: HFCAClient
    private val wallet: Wallet
    init {
        // Create a CA client for interacting with the CA.
        val props = Properties()
        props["pemFile"] = config.caCertFile
        props["allowAllHostNames"] = "true"
        caClient = HFCAClient.createNewInstance(config.caUrl, props)
        val cryptoSuite = CryptoSuiteFactory.getDefault().cryptoSuite
        caClient.cryptoSuite = cryptoSuite

        // Create a wallet for managing identities
        wallet = Wallets.newFileSystemWallet(Paths.get(config.wallet))
    }

    fun connect() {
        enrollAdmin(config, caClient, wallet)
        registerUser(config, caClient, wallet)
    }
}
 No newline at end of file
+15 −29
Original line number Diff line number Diff line
@@ -4,38 +4,24 @@ SPDX-License-Identifier: Apache-2.0
package fabric

import org.hyperledger.fabric.gateway.Identities
import org.hyperledger.fabric.gateway.Wallets
import org.hyperledger.fabric.gateway.Wallet
import org.hyperledger.fabric.gateway.X509Identity
import org.hyperledger.fabric.sdk.Enrollment
import org.hyperledger.fabric.sdk.User
import org.hyperledger.fabric.sdk.security.CryptoSuiteFactory
import org.hyperledger.fabric_ca.sdk.HFCAClient
import org.hyperledger.fabric_ca.sdk.RegistrationRequest
import java.nio.file.Paths
import java.security.PrivateKey
import java.util.*

fun registerUser() {
    // Create a CA client for interacting with the CA.
    val props = Properties()
    props["pemFile"] = "config/ca.org1.example.com-cert.pem"
    props["allowAllHostNames"] = "true"
    val caClient = HFCAClient.createNewInstance("https://s2:7054", props)
    val cryptoSuite = CryptoSuiteFactory.getDefault().cryptoSuite
    caClient.cryptoSuite = cryptoSuite

    // Create a wallet for managing identities
    val wallet = Wallets.newFileSystemWallet(Paths.get("wallet"))

fun registerUser(config: proto.Config.DltConfig, caClient: HFCAClient, wallet: Wallet) {
    // Check to see if we've already enrolled the user.
    if (wallet["appUser"] != null) {
        println("An identity for the user \"appUser\" already exists in the wallet")
    if (wallet[config.user] != null) {
        println("An identity for the user ${config.user} already exists in the wallet")
        return
    }
    val adminIdentity = wallet["admin"] as X509Identity
    val adminIdentity = wallet[config.caAdmin] as X509Identity
    val admin = object : User {
        override fun getName(): String {
            return "admin"
            return config.caAdmin
        }

        override fun getRoles(): Set<String>? {
@@ -47,7 +33,7 @@ fun registerUser() {
        }

        override fun getAffiliation(): String {
            return "org1.department1"
            return config.affiliation
        }

        override fun getEnrollment(): Enrollment {
@@ -63,17 +49,17 @@ fun registerUser() {
        }

        override fun getMspId(): String {
            return "Org1MSP"
            return config.msp
        }
    }

    // Register the user, enroll the user, and import the new identity into the wallet.
    val registrationRequest = RegistrationRequest("appUser")
    registrationRequest.affiliation = "org1.department1"
    registrationRequest.enrollmentID = "appUser"
    val registrationRequest = RegistrationRequest(config.user)
    registrationRequest.affiliation = config.affiliation
    registrationRequest.enrollmentID = config.user
    val enrollmentSecret = caClient.register(registrationRequest, admin)
    val enrollment = caClient.enroll("appUser", enrollmentSecret)
    val user = Identities.newX509Identity("Org1MSP", enrollment)
    wallet.put("appUser", user)
    println("Successfully enrolled user \"appUser\" and imported it into the wallet")
    val enrollment = caClient.enroll(config.user, enrollmentSecret)
    val user = Identities.newX509Identity(config.msp, enrollment)
    wallet.put(config.user, user)
    println("Successfully enrolled user ${config.user} and imported it into the wallet")
}
+3 −1
Original line number Diff line number Diff line
package http

import fabric.FabricConnector
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.request.*
@@ -17,10 +18,11 @@ fun main() {
        routing {
            post("/dlt/configure") {
                withContext(Dispatchers.IO) {
                    println("incoming connection")
                    val data = call.receiveStream()
                    val config = parseFrom(data)
                    println(config)
                    val connector = FabricConnector(config)
                    connector.connect()
                    val encodedBack = config.toByteArray()
                    call.respond(encodedBack)
                }
Loading