-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement DID Exchange Protocol (#28)
Signed-off-by: conanoc <[email protected]>
- Loading branch information
Showing
30 changed files
with
852 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...amework/src/androidTest/java/org/hyperledger/ariesframework/connection/DidExchangeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.hyperledger.ariesframework.connection | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.test.runTest | ||
import org.hyperledger.ariesframework.TestHelper | ||
import org.hyperledger.ariesframework.agent.Agent | ||
import org.hyperledger.ariesframework.agent.SubjectOutboundTransport | ||
import org.hyperledger.ariesframework.connection.models.ConnectionState | ||
import org.hyperledger.ariesframework.oob.models.CreateOutOfBandInvitationConfig | ||
import org.hyperledger.ariesframework.oob.models.HandshakeProtocol | ||
import org.junit.After | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class DidExchangeTest { | ||
lateinit var faberAgent: Agent | ||
lateinit var aliceAgent: Agent | ||
|
||
@Before | ||
fun setUp() = runTest { | ||
val faberConfig = TestHelper.getBaseConfig("faber") | ||
val aliceConfig = TestHelper.getBaseConfig("alice") | ||
|
||
val context = InstrumentationRegistry.getInstrumentation().targetContext | ||
faberAgent = Agent(context, faberConfig) | ||
aliceAgent = Agent(context, aliceConfig) | ||
|
||
faberAgent.setOutboundTransport(SubjectOutboundTransport(aliceAgent)) | ||
aliceAgent.setOutboundTransport(SubjectOutboundTransport(faberAgent)) | ||
|
||
faberAgent.initialize() | ||
aliceAgent.initialize() | ||
} | ||
|
||
@After | ||
fun tearDown() = runTest { | ||
faberAgent.reset() | ||
aliceAgent.reset() | ||
} | ||
|
||
@Test | ||
fun testOobConnection() = runBlocking { | ||
val outOfBandRecord = faberAgent.oob.createInvitation(CreateOutOfBandInvitationConfig()) | ||
val invitation = outOfBandRecord.outOfBandInvitation | ||
|
||
aliceAgent.agentConfig.preferredHandshakeProtocol = HandshakeProtocol.DidExchange11 | ||
val (_, connection) = aliceAgent.oob.receiveInvitation(invitation) | ||
val aliceFaberConnection = connection | ||
?: throw Exception("Connection is nil after receiving oob invitation") | ||
assertEquals(aliceFaberConnection.state, ConnectionState.Complete) | ||
|
||
val faberAliceConnection = faberAgent.connectionService.findByInvitationKey(invitation.invitationKey()!!) | ||
?: throw Exception("Cannot find connection by invitation key") | ||
assertEquals(faberAliceConnection.state, ConnectionState.Complete) | ||
|
||
assertEquals(TestHelper.isConnectedWith(faberAliceConnection, aliceFaberConnection), true) | ||
assertEquals(TestHelper.isConnectedWith(aliceFaberConnection, faberAliceConnection), true) | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...ramework/src/androidTest/java/org/hyperledger/ariesframework/connection/JwsServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.hyperledger.ariesframework.connection | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.jsonPrimitive | ||
import org.hyperledger.ariesframework.TestHelper | ||
import org.hyperledger.ariesframework.agent.Agent | ||
import org.hyperledger.ariesframework.agent.decorators.JwsFlattenedFormat | ||
import org.hyperledger.ariesframework.decodeBase64url | ||
import org.junit.After | ||
import org.junit.Assert | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class JwsServiceTest { | ||
lateinit var agent: Agent | ||
val seed = "00000000000000000000000000000My2" | ||
val verkey = "kqa2HyagzfMAq42H5f9u3UMwnSBPQx2QfrSyXbUPxMn" | ||
val payload = "hello".toByteArray() | ||
|
||
@Before | ||
fun setUp() = runTest { | ||
val context = InstrumentationRegistry.getInstrumentation().targetContext | ||
val config = TestHelper.getBaseConfig() | ||
agent = Agent(context, config) | ||
agent.initialize() | ||
|
||
val didInfo = agent.wallet.createDid(seed) | ||
Assert.assertEquals(didInfo.verkey, verkey) | ||
} | ||
|
||
@After | ||
fun tearDown() = runTest { | ||
agent.reset() | ||
} | ||
|
||
@Test | ||
fun testCreateAndVerify() = runTest { | ||
val jws = agent.jwsService.createJws(payload, verkey) | ||
Assert.assertEquals( | ||
"did:key:z6MkfD6ccYE22Y9pHKtixeczk92MmMi2oJCP6gmNooZVKB9A", | ||
jws.header?.get("kid"), | ||
) | ||
val protectedJson = jws.protected.decodeBase64url().decodeToString() | ||
val protected = Json.decodeFromString<JsonObject>(protectedJson) | ||
Assert.assertEquals("EdDSA", protected["alg"]?.jsonPrimitive?.content) | ||
Assert.assertNotNull(protected["jwk"]) | ||
|
||
val (valid, signer) = agent.jwsService.verifyJws(jws, payload) | ||
Assert.assertTrue(valid) | ||
Assert.assertEquals(signer, verkey) | ||
} | ||
|
||
@Test | ||
fun testFlattenedJws() = runTest { | ||
val jws = agent.jwsService.createJws(payload, verkey) | ||
val list = JwsFlattenedFormat(arrayListOf(jws)) | ||
|
||
val (valid, signer) = agent.jwsService.verifyJws(list, payload) | ||
Assert.assertTrue(valid) | ||
Assert.assertEquals(signer, verkey) | ||
} | ||
|
||
@Test | ||
fun testVerifyFail() = runTest { | ||
val wrongPayload = "world".toByteArray() | ||
val jws = agent.jwsService.createJws(payload, verkey) | ||
val (valid, signer) = agent.jwsService.verifyJws(jws, wrongPayload) | ||
Assert.assertFalse(valid) | ||
Assert.assertEquals(signer, verkey) | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...work/src/androidTest/java/org/hyperledger/ariesframework/connection/PeerDIDServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.hyperledger.ariesframework.connection | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import kotlinx.coroutines.test.runTest | ||
import org.hyperledger.ariesframework.TestHelper | ||
import org.hyperledger.ariesframework.agent.Agent | ||
import org.hyperledger.ariesframework.connection.models.didauth.DidComm | ||
import org.junit.After | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class PeerDIDServiceTest { | ||
lateinit var agent: Agent | ||
val verkey = "3uhKmLCRYfe5YWDsgBC4VNTKk3RbnFCzgjVH3zmSKHWa" | ||
|
||
@Before | ||
fun setUp() = runTest { | ||
val context = InstrumentationRegistry.getInstrumentation().targetContext | ||
val config = TestHelper.getBaseConfig() | ||
agent = Agent(context, config) | ||
agent.initialize() | ||
} | ||
|
||
@After | ||
fun tearDown() = runTest { | ||
agent.reset() | ||
} | ||
|
||
@Test | ||
fun testPeerDIDwithLegacyService() = runTest { | ||
val peerDID = agent.peerDIDService.createPeerDID(verkey) | ||
parsePeerDID(peerDID) | ||
} | ||
|
||
@Test | ||
fun testPeerDIDwithDidCommV2Service() = runTest { | ||
val peerDID = agent.peerDIDService.createPeerDID(verkey, useLegacyService = false) | ||
parsePeerDID(peerDID) | ||
} | ||
|
||
suspend fun parsePeerDID(peerDID: String) { | ||
assertTrue(peerDID.startsWith("did:peer:2")) | ||
|
||
val didDoc = agent.peerDIDService.parsePeerDID(peerDID) | ||
assertEquals(didDoc.id, peerDID) | ||
assertEquals(didDoc.publicKey.size, 1) | ||
assertEquals(didDoc.service.size, 1) | ||
assertEquals(didDoc.authentication.size, 1) | ||
assertEquals(didDoc.publicKey[0].value, verkey) | ||
|
||
val service = didDoc.service.first() | ||
assertTrue(service is DidComm) | ||
val didCommService = service as DidComm | ||
assertEquals(didCommService.recipientKeys.size, 1) | ||
assertEquals(didCommService.recipientKeys[0], verkey) | ||
assertEquals(didCommService.routingKeys?.size, 0) | ||
assertEquals(didCommService.serviceEndpoint, agent.agentConfig.endpoints[0]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.