Skip to content

Commit

Permalink
w3c did doc (#28)
Browse files Browse the repository at this point in the history
Co-authored-by: Rodolfo Miranda <[email protected]>
  • Loading branch information
rodolfomiranda and Rodolfo Miranda authored Jun 12, 2022
1 parent 21b065c commit f7aa530
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
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

0 comments on commit f7aa530

Please sign in to comment.