Skip to content

Commit

Permalink
3 update to atala 1.3.2 (#4)
Browse files Browse the repository at this point in the history
* build: update code to use Atala PRISM 1.3.2 SDK

Atala PRISM 1.3.2 SDK has introduced breaking changes. PrismKeyType no longer exists, so key types are expressed with MasterKeyUsage, IssuingKeyUsage, and RevocationKeyUsage objects.

wal-library continues to work in the same way.
  • Loading branch information
Essbante authored Mar 25, 2022
1 parent c8891fc commit 834daf2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
22 changes: 11 additions & 11 deletions 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.0"
version = "1.0.1-SNAPSHOT"

repositories {
mavenCentral()
Expand All @@ -23,24 +23,24 @@ repositories {

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.6.10")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0-native-mt")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
implementation("org.litote.kmongo:kmongo:4.4.0")
implementation("org.litote.kmongo:kmongo:4.5.0")

// needed for cryptography primitives implementation
implementation("io.iohk.atala:prism-crypto:1.3.0")
implementation("io.iohk.atala:prism-identity:1.3.0")
implementation("io.iohk.atala:prism-credentials:1.3.0")
implementation("io.iohk.atala:prism-api:1.3.0")
implementation("io.iohk.atala:prism-crypto:v1.3.2")
implementation("io.iohk.atala:prism-identity:v1.3.2")
implementation("io.iohk.atala:prism-credentials:v1.3.2")
implementation("io.iohk.atala:prism-api:v1.3.2")

// Fixes a build issue
implementation("com.soywiz.korlibs.krypto:krypto-jvm:2.0.6")

// QR Code
implementation("org.boofcv:boofcv-core:0.39.1")
implementation("org.boofcv:boofcv-swing:0.39.1")
implementation("org.boofcv:boofcv-kotlin:0.39.1")
implementation("org.boofcv:boofcv-WebcamCapture:0.39.1")
implementation("org.boofcv:boofcv-core:0.40.1")
implementation("org.boofcv:boofcv-swing:0.40.1")
implementation("org.boofcv:boofcv-kotlin:0.40.1")
implementation("org.boofcv:boofcv-WebcamCapture:0.40.1")

// DIDComm
implementation("org.didcommx:didcomm:0.3.0")
Expand Down
57 changes: 35 additions & 22 deletions src/main/kotlin/com/rootsid/wal/library/DLT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ 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.GetOperationInfoRequest
import io.iohk.atala.prism.protos.GrpcClient
import io.iohk.atala.prism.protos.GrpcOptions
import io.iohk.atala.prism.protos.NodeServiceCoroutine
import io.iohk.atala.prism.protos.*
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
Expand Down Expand Up @@ -75,11 +72,17 @@ private fun waitUntilConfirmed(nodePublicApi: NodePublicApi, operationId: AtalaO
* @param keyId Id of the key to derive
* @return Key pair
*/
@OptIn(PrismSdkInternal::class)
private fun deriveKeyPair(keyPairs: MutableList<KeyPair>, seed: ByteArray, keyId: String): ECKeyPair {
val keyPathList = keyPairs.filter { it.keyId == keyId }
if (keyPathList.isNotEmpty()) {
val keyPath = keyPathList[0]
return KeyGenerator.deriveKeyFromFullPath(seed, keyPath.didIdx, keyPath.keyType, keyPath.keyIdx)
return KeyGenerator.deriveKeyFromFullPath(
seed,
keyPath.didIdx,
PublicKeyUsage.fromProto(KeyUsage.fromValue(keyPath.keyTypeValue)),
keyPath.keyIdx
)
} else {
throw NoSuchElementException("Key ID '$keyId' not found.")
}
Expand Down Expand Up @@ -116,18 +119,20 @@ fun newWallet(name: String, mnemonic: String, passphrase: String): Wallet {
* @param issuer If true issuing and holder keys are included, otherwise only a master key pair is added
* @return updated wallet
*/
@OptIn(PrismSdkInternal::class)
fun newDid(wallet: Wallet, didAlias: String, issuer: Boolean): Wallet {
// To keep DID index sequential
val didIdx = wallet.dids.size
val keyPairs = mutableListOf<KeyPair>()
val seed = KeyDerivation.binarySeed(MnemonicCode(wallet.mnemonic), wallet.passphrase)
val masterKeyPair = KeyGenerator.deriveKeyFromFullPath(
seed, didIdx, PrismKeyType.MASTER_KEY, 0
seed, didIdx, MasterKeyUsage, 0
)
val masterKeyPairData = KeyPair(
PrismDid.DEFAULT_MASTER_KEY_ID,
KeyUsage.MASTER_KEY.value,
didIdx,
PrismKeyType.MASTER_KEY,
MasterKeyUsage.derivationIndex(),
0,
masterKeyPair.privateKey.getHexEncoded(),
masterKeyPair.publicKey.getHexEncoded()
Expand All @@ -136,22 +141,25 @@ fun newDid(wallet: Wallet, didAlias: String, issuer: Boolean): Wallet {

val unpublishedDid = if (issuer) {
val issuingKeyPair = KeyGenerator.deriveKeyFromFullPath(
seed, didIdx, PrismKeyType.ISSUING_KEY, 0
seed, didIdx, IssuingKeyUsage, 0
)
val revocationKeyPair = KeyGenerator.deriveKeyFromFullPath(
seed, didIdx, PrismKeyType.REVOCATION_KEY, 0
seed, didIdx, RevocationKeyUsage, 0
)
val issuingKeyPairData = KeyPair(
PrismDid.DEFAULT_ISSUING_KEY_ID,
KeyUsage.ISSUING_KEY.value,
didIdx,
PrismKeyType.ISSUING_KEY,
IssuingKeyUsage.derivationIndex(),
0,
issuingKeyPair.privateKey.getHexEncoded(),
issuingKeyPair.publicKey.getHexEncoded()
)
val revocationKeyPairData = KeyPair(
PrismDid.DEFAULT_REVOCATION_KEY_ID,
didIdx, PrismKeyType.REVOCATION_KEY,
KeyUsage.REVOCATION_KEY.value,
didIdx,
RevocationKeyUsage.derivationIndex(),
0,
revocationKeyPair.privateKey.getHexEncoded(),
revocationKeyPair.publicKey.getHexEncoded()
Expand Down Expand Up @@ -249,26 +257,33 @@ fun publishDid(wallet: Wallet, didAlias: String): Wallet {
* @param wallet Wallet containing the DID
* @param didAlias Alias of DID where the key will be added
* @param keyId Key identifier for the new key
* @param keyType Type of key (master, issuing or revocation)
* @param keyTypeValue Type of key (master, issuing or revocation)
* @return updated wallet
*/
fun addKey(wallet: Wallet, didAlias: String, keyId: String, keyType: Int): Wallet {
@OptIn(PrismSdkInternal::class)
fun addKey(wallet: Wallet, didAlias: String, keyId: String, keyTypeValue: Int): Wallet {
val didList = wallet.dids.filter { it.alias == didAlias }
if (didList.isNotEmpty()) {
val did = didList[0]
val keyIdx = did.keyPairs.filter { it.keyType == keyType }.size
val keyIdx = did.keyPairs.filter { it.keyTypeValue == keyTypeValue }.size
val nodeAuthApi = NodeAuthApiImpl(GrpcConfig.options)

// Key pairs to get private keys
val seed = KeyDerivation.binarySeed(MnemonicCode(wallet.mnemonic), wallet.passphrase)
// TODO: masterKey index 0 may be revoked, do something to indicate the currently valid masterKey
val masterKeyPair = KeyGenerator.deriveKeyFromFullPath(seed, did.didIdx, PrismKeyType.MASTER_KEY, 0)
val newKeyPair = KeyGenerator.deriveKeyFromFullPath(seed, did.didIdx, keyType, keyIdx)

val masterKeyPair = KeyGenerator.deriveKeyFromFullPath(seed, did.didIdx, MasterKeyUsage, 0)
val newKeyPair = KeyGenerator.deriveKeyFromFullPath(
seed,
did.didIdx,
PublicKeyUsage.fromProto(KeyUsage.fromValue(keyTypeValue)),
keyIdx
)
val keyUsage = KeyUsage.fromValue(keyTypeValue)
val newKeyPairData = KeyPair(
keyId,
keyTypeValue,
did.didIdx,
keyType,
PublicKeyUsage.fromProto(keyUsage).derivationIndex(),
keyIdx,
newKeyPair.privateKey.getHexEncoded(),
newKeyPair.publicKey.getHexEncoded()
Expand All @@ -278,9 +293,7 @@ fun addKey(wallet: Wallet, didAlias: String, keyId: String, keyType: Int): Walle
mapOf(PrismDid.DEFAULT_MASTER_KEY_ID to masterKeyPair.privateKey)
)
val newKeyInfo = PrismKeyInformation(
keyId,
keyType,
newKeyPair.publicKey
DidPublicKey(keyId, PublicKeyUsage.fromProto(keyUsage), newKeyPair.publicKey)
)
val updateDidInfo = nodePayloadGenerator.updateDid(
previousHash = Sha256Digest.fromHex(did.operationHash),
Expand Down Expand Up @@ -331,7 +344,7 @@ fun revokeKey(wallet: Wallet, didAlias: String, keyId: String): Wallet {
// Key pairs to get private keys
val seed = KeyDerivation.binarySeed(MnemonicCode(wallet.mnemonic), wallet.passphrase)
// TODO: masterKey index 0 may be revoked, do something to indicate the currently valid masterKey
val masterKeyPair = KeyGenerator.deriveKeyFromFullPath(seed, did.didIdx, PrismKeyType.MASTER_KEY, 0)
val masterKeyPair = KeyGenerator.deriveKeyFromFullPath(seed, did.didIdx, MasterKeyUsage, 0)

val nodePayloadGenerator = NodePayloadGenerator(
PrismDid.fromString(did.uriLongForm) as LongFormPrismDid,
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/com/rootsid/wal/library/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ data class DID(
*
* @property keyId
* @property didIdx
* @property keyType
* @property keyTypeValue
* @property keyIdx
* @property privateKey
* @property publicKey
Expand All @@ -66,8 +66,9 @@ data class DID(
@Serializable
data class KeyPair(
val keyId: String,
val keyTypeValue: Int,
val didIdx: Int,
val keyType: Int,
val keyDerivation: Int,
val keyIdx: Int,
val privateKey: String,
val publicKey: String,
Expand Down

0 comments on commit 834daf2

Please sign in to comment.