Skip to content

Commit

Permalink
feat: ATL-5574 Prime Anoncred Lib
Browse files Browse the repository at this point in the history
Signed-off-by: Bassam Riman <[email protected]>
  • Loading branch information
CryptoKnightIOG committed Aug 25, 2023
1 parent ebf0328 commit 81c8390
Show file tree
Hide file tree
Showing 8 changed files with 416 additions and 137 deletions.
14 changes: 8 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ lazy val D = new {
val zioCatsInterop: ModuleID = "dev.zio" %% "zio-interop-cats" % V.zioCatsInterop
val zioMetricsConnectorMicrometer: ModuleID = "dev.zio" %% "zio-metrics-connectors-micrometer" % V.zioMetricsConnector
val tapirPrometheusMetrics: ModuleID = "com.softwaremill.sttp.tapir" %% "tapir-prometheus-metrics" % V.tapir
val micrometer: ModuleID = "io.micrometer" % "micrometer-registry-prometheus" % V.micrometer
val micrometerPrometheusRegistry = "io.micrometer" % "micrometer-core" % V.micrometer

val micrometer: ModuleID = "io.micrometer" % "micrometer-registry-prometheus" % V.micrometer
val micrometerPrometheusRegistry = "io.micrometer" % "micrometer-core" % V.micrometer

val zioConfig: ModuleID = "dev.zio" %% "zio-config" % V.zioConfig
val zioConfigMagnolia: ModuleID = "dev.zio" %% "zio-config-magnolia" % V.zioConfig
Expand All @@ -119,7 +118,8 @@ lazy val D = new {
val jwk: ModuleID = "com.nimbusds" % "nimbus-jose-jwt" % "10.0.0-preview"

val typesafeConfig: ModuleID = "com.typesafe" % "config" % V.typesafeConfig
val scalaPbRuntime: ModuleID = "com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf"
val scalaPbRuntime: ModuleID =
"com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf"
val scalaPbGrpc: ModuleID = "com.thesamet.scalapb" %% "scalapb-runtime-grpc" % scalapb.compiler.Version.scalapbVersion
// TODO we are adding test stuff to the main dependencies
val testcontainersPostgres: ModuleID = "com.dimafeng" %% "testcontainers-scala-postgresql" % V.testContainersScala
Expand Down Expand Up @@ -660,8 +660,9 @@ lazy val polluxCore = project
)
.dependsOn(shared)
.dependsOn(irisClient)
.dependsOn(prismAgentWalletAPI)
.dependsOn(polluxVcJWT)
.dependsOn(protocolIssueCredential, protocolPresentProof, resolver, agentDidcommx, eventNotification)
.dependsOn(protocolIssueCredential, protocolPresentProof, resolver, agentDidcommx, eventNotification, polluxAnoncreds)

lazy val polluxDoobie = project
.in(file("pollux/lib/sql-doobie"))
Expand All @@ -684,9 +685,10 @@ lazy val polluxAnoncreds = project
.enablePlugins(JavaAppPackaging)
.settings(
name := "pollux-anoncreds",
Compile / unmanagedJars += baseDirectory.value / "UniffiPOC-1.0-SNAPSHOT.jar",
Compile / unmanagedJars += baseDirectory.value / "anoncreds-java-1.0-SNAPSHOT.jar",
Compile / unmanagedResourceDirectories ++= Seq(
// export LD_LIBRARY_PATH=.../anoncreds-rs/uniffi/target/x86_64-unknown-linux-gnu/release:$LD_LIBRARY_PATH,
baseDirectory.value / "native-lib" / "NATIVE" / "darwin-aarch64",
baseDirectory.value / "native-lib" / "NATIVE" / "linux" / "amd64"
),
)
Expand Down
21 changes: 19 additions & 2 deletions pollux/lib/anoncreds/README_anoncreds.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ Go into the folder `uniffi` and generate the file `anoncreds.kt`

Assuming uniffi_bindgen is install. Install with `cargo install uniffi_bindgen --version $(cargo pkgid uniffi | cut -f 2 -d '@')`

Replace the `anoncreds.kt` file in the UniffiPOC project (`UniffiPOC/src/main/uniffi/anoncreds/anoncreds.kt`)
(Note you can also the run the script `build-release-linux.sh` in tere)

Generate the Jar with `./gradlew jar` in the UniffiPOC project
Run the command `~/.cargo/bin/uniffi-bindgen generate src/anoncreds.udl --language kotlin -o ./wrappers/kotlin/anoncreds`

Replace the `anoncreds.kt` file in the `output-frameworks/anoncreds-java` project (`output-frameworks/anoncreds-java/src/main/uniffi/anoncreds/anoncreds.kt`)

Generate the Jar with `./gradlew jar` in the `output-frameworks/anoncreds-java` project

## Build the NATIVE lib

Expand All @@ -34,3 +38,16 @@ Go into the folder `uniffi` and build the Native lib with:
- `cargo build --release --target x86_64-unknown-linux-gnu` (For Linux with x86_64)
- `cargo build --release --target x86_64-apple-darwin` (For Mac with x86_64)
- `cargo build --release --target aarch64-apple-darwin` (For Mac with arm)

## Copy files

Then copy the files
- from `target/x86_64-unknown-linux-gnu/release/libanoncreds_uniffi.so` to `pollux/lib/anoncreds/native-lib/NATIVE/linux/amd64/libuniffi_anoncreds.so`
- from `uniffi/output-frameworks/anoncreds-java/build/libs/anoncreds-java-1.0-SNAPSHOT.jar` to `pollux/lib/anoncreds//anoncreds-java-1.0-SNAPSHOT.jar`

```shell
rm -f pollux/lib/anoncreds/native-lib/NATIVE/linux/amd64/libuniffi_anoncreds.so
rm -f pollux/lib/anoncreds//anoncreds-java-1.0-SNAPSHOT.jar
cp ../anoncreds-rs/uniffi/target/x86_64-unknown-linux-gnu/release/libanoncreds_uniffi.so pollux/lib/anoncreds/native-lib/NATIVE/linux/amd64/libuniffi_anoncreds.so
cp ../anoncreds-rs/uniffi/output-frameworks/anoncreds-java/build/libs/anoncreds-java-1.0-SNAPSHOT.jar pollux/lib/anoncreds//anoncreds-java-1.0-SNAPSHOT.jar
```
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package io.iohk.atala.pollux.anoncreds

import uniffi.anoncreds

import scala.jdk.CollectionConverters.*
import scala.language.implicitConversions

/** @see
* https://hyperledger.github.io/anoncreds-spec/
*/
object AnoncredLib {

val SCHEMA_ID = "mock:uri2"
val CRED_DEF_ID = "mock:uri2"
val ISSUER_DID = "mock:issuer_id/path&q=bar"

// issuer or any
def createSchema(
name: String, // SCHEMA_ID
version: String, // SCHEMA_Version
attr_names: AttributeNames,
issuer_id: IssuerId, // ISSUER_DID
): SchemaDef = anoncreds.Schema.apply(name, version, attr_names.toSeq.asJava, issuer_id)

// issuer
def createCredDefinition(
issuer_id: String,
schema: SchemaDef,
tag: String,
supportRevocation: Boolean,
signature_type: anoncreds.SignatureType.CL.type = anoncreds.SignatureType.CL
) = {
val credentialDefinition: anoncreds.IssuerCreateCredentialDefinitionReturn =
anoncreds
.Issuer()
.createCredentialDefinition(
schema.name,
schema: anoncreds.Schema,
issuer_id,
tag,
signature_type,
anoncreds.CredentialDefinitionConfig(supportRevocation)
)

CreateCredentialDefinition(
credentialDefinition.getCredentialDefinition(),
credentialDefinition.getCredentialDefinitionPrivate(),
credentialDefinition.getCredentialKeyCorrectnessProof()
)
}

// issuer
def createOffer(
credentialDefinition: CreateCredentialDefinition,
credentialDefinitionId: String
): CredentialOffer =
anoncreds
.Issuer()
.createCredentialOffer(
credentialDefinition.cd.schemaId, // string schema_id,
credentialDefinitionId, // string cred_def_id,
credentialDefinition.proofKey // CredentialKeyCorrectnessProof correctness_proof
)

// holder
def createCredentialRequest(
linkSecret: LinkSecretWithId,
credentialDefinition: CredentialDefinition,
credentialOffer: CredentialOffer,
entropy: String = {
val tmp = scala.util.Random()
tmp.setSeed(java.security.SecureRandom.getInstanceStrong().nextLong())
tmp.nextString(80)
}
): CreateCrendentialRequest = {
val credentialRequest =
anoncreds
.Prover()
.createCredentialRequest(
entropy, // string? entropy,
null, // string? prover_did,
credentialDefinition, // CredentialDefinition cred_def,
linkSecret.secret, // LinkSecret link_secret,
linkSecret.id, // string link_secret_id,
credentialOffer, // CredentialOffer credential_offer
)

CreateCrendentialRequest(credentialRequest.getRequest(), credentialRequest.getMetadata())
}

// issuer
def createCredential(
credentialDefinition: CredentialDefinition,
credentialDefinitionPrivate: CredentialDefinitionPrivate,
credentialOffer: CredentialOffer,
credentialRequest: CredentialRequest,
attributeValues: Seq[(String, String)]
// java.util.List[AttributeValues] : java.util.List[AttributeValues]
// revocationRegistryId : String
// revocationStatusList : RevocationStatusList
// credentialRevocationConfig : CredentialRevocationConfig
): Credential = {

anoncreds
.Issuer()
.createCredential(
credentialDefinition, // CredentialDefinition cred_def,
credentialDefinitionPrivate, // CredentialDefinitionPrivate cred_def_private,
credentialOffer, // CredentialOffer cred_offer,
credentialRequest, // CredentialRequest cred_request,
attributeValues
.map(e => anoncreds.AttributeValues(e._1, e._2))
.asJava, // sequence<AttributeValues> cred_values,
null, // RevocationRegistryId? rev_reg_id,
null, // RevocationStatusList? rev_status_list,
null, // CredentialRevocationConfig? revocation_config
)
}
}
Loading

0 comments on commit 81c8390

Please sign in to comment.