Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve DID as W3C compliance DID Document #28

Merged
merged 1 commit into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
61 changes: 61 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,61 @@ 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