Skip to content

Commit

Permalink
update to v 1.0.2 (#29)
Browse files Browse the repository at this point in the history
* update java version to 11

* feat: add a function to get DID document Json from a DID URI

* Update DLT.kt (#23)

* Main (#25)

* Update CODE_OF_CONDUCT.md

* Update LICENSE

* Create CONTRIBUTING.md

* Main (#26)

* Update CODE_OF_CONDUCT.md

* Update LICENSE

* Create CONTRIBUTING.md

* w3c did doc (#28)

Co-authored-by: Rodolfo Miranda <[email protected]>

* apply ktlint

* update version

Co-authored-by: Lance <[email protected]>
Co-authored-by: Rodolfo <[email protected]>
Co-authored-by: Rodolfo Miranda <[email protected]>
  • Loading branch information
4 people authored Jun 14, 2022
1 parent 452c95e commit d056392
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.idea/
build/
gradle/
bin/

*.jar
*.war
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "com.rootsid.wal"
version = "1.0.1-SNAPSHOT"
version = "1.0.2"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
Expand Down
64 changes: 64 additions & 0 deletions src/main/kotlin/com/rootsid/wal/library/DLT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@ import io.iohk.atala.prism.api.node.NodePublicApi
import io.iohk.atala.prism.api.node.PrismDidState
import io.iohk.atala.prism.common.PrismSdkInternal
import io.iohk.atala.prism.credentials.json.JsonBasedCredential
import io.iohk.atala.prism.crypto.EC
import io.iohk.atala.prism.crypto.MerkleInclusionProof
import io.iohk.atala.prism.crypto.Sha256Digest
import io.iohk.atala.prism.crypto.derivation.KeyDerivation
import io.iohk.atala.prism.crypto.derivation.MnemonicCode
import io.iohk.atala.prism.crypto.keys.ECKeyPair
import io.iohk.atala.prism.identity.*
import io.iohk.atala.prism.protos.*
import io.ipfs.multibase.Base58
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import pbandk.ByteArr
import pbandk.json.encodeToJsonString

Expand Down Expand Up @@ -252,6 +258,64 @@ fun getDidDocument(did: String): PrismDidState {
return runBlocking { nodeAuthApi.getDidDocument(prismDid) }
}

/**
* Get did document
*
* @param did a prism did
* @return W3C compliant DID document
*/
@OptIn(PrismSdkInternal::class)
fun getDidDocumentW3C(did: String): JsonObject {
fun byteArrayOfInts(ints: List<String>) = ByteArray(ints.size) { pos -> ints[pos].toInt().toByte() }
fun String.decodeHex(): ByteArray {
check(length % 2 == 0) { "Must have an even length" }
return chunked(2)
.map { it.toInt(16).toByte() }
.toByteArray()
}

val nodeAuthApi = NodeAuthApiImpl(GrpcConfig.options())
val prismDid = try {
PrismDid.fromString(did)
} catch (e: Exception) {
throw Exception("not a Prism DID: $did")
}
val prismDoc = runBlocking { nodeAuthApi.getDidDocument(prismDid) }

var didDocW3C = mutableMapOf<String, JsonElement>(
"@context" to JsonArray(listOf(JsonPrimitive("https://www.w3.org/ns/did/v1"))),
"id" to JsonPrimitive(did),
"assertionMethod" to JsonArray(listOf(JsonPrimitive(did + "#master0"))),
)
var verificationMethods: MutableList<JsonObject> = ArrayList()
// TODO parsing a string is not the best way to access the object. Need to figure out
// how to access OneOf.CompressedEcKeyData directly
for (pubk in prismDoc.didData.publicKeys) {
val keyId = pubk.didPublicKey.toProto().id
val dataStr = pubk.didPublicKey.toProto().keyData.toString()
.replace("OneOf.CompressedEcKeyData(CompressedECKeyData(curve=secp256k1, data=[", "")
.replace("], unknownFields={}))", "")
.replace(" ", "")
val dataArr = dataStr.split(",")
val dataCompress = byteArrayOfInts(dataArr)
val dataHexa = EC.toPublicKeyFromCompressed(dataCompress).getHexEncoded()
verificationMethods.add(
JsonObject(
mapOf(
"@context" to JsonArray(listOf(JsonPrimitive("https://w3id.org/security/v1"))),
"id" to JsonPrimitive(did + "#" + keyId),
"type" to JsonPrimitive("EcdsaSecp256k1VerificationKey2019"),
"controller" to JsonPrimitive(did),
"publicKeyBase58" to JsonPrimitive(Base58.encode(dataHexa.drop(2).decodeHex()))
)
)
)
}
didDocW3C["verificationMethod"] = JsonArray(verificationMethods)

return JsonObject(didDocW3C)
}

/**
* Publish did
*
Expand Down

0 comments on commit d056392

Please sign in to comment.