Skip to content

Commit

Permalink
feat(pluto): add registered dids dao
Browse files Browse the repository at this point in the history
Fixes ATL-2357
  • Loading branch information
goncalo-frade-iohk committed Dec 1, 2022
1 parent 82dae4e commit 130c0e5
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Domain/Sources/Models/DID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public typealias DIDMethodId = String

/// Represents a DID with ``DIDMethod`` and ``DIDMethodId``
/// As specified in [w3 standards](https://www.w3.org/TR/did-core/#dfn-did-schemes)
public struct DID {
public struct DID: Equatable {
public let schema: String
public let method: DIDMethod
public let methodId: DIDMethodId
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Combine
import CoreData
import Domain

extension CDRegisteredDIDDAO: DIDProvider {
func getAll() -> AnyPublisher<[(did: DID, keyPairIndex: Int, alias: String?)], Error> {
fetchController(context: readContext)
.map { $0.map { (DID(from: $0), Int($0.keyIndex), $0.alias) } }
.eraseToAnyPublisher()
}

func getDIDInfo(
alias: String
) -> AnyPublisher<[(did: DID, keyPairIndex: Int, alias: String?)], Error> {
fetchByKeyValuePublisher(key: "alias", value: alias, context: readContext)
.map { $0.map {
(DID(from: $0), Int($0.keyIndex), $0.alias)
}}
.eraseToAnyPublisher()
}

func getDIDInfo(
did: DID
) -> AnyPublisher<(did: DID, keyPairIndex: Int, alias: String?)?, Error> {
fetchByKeyValuePublisher(key: "did", value: did.string, context: readContext)
.map { $0.first.map {
(DID(from: $0), Int($0.keyIndex), $0.alias)
}}
.eraseToAnyPublisher()
}

func getDIDInfo(
keyPairIndex: Int
) -> AnyPublisher<(did: DID, keyPairIndex: Int, alias: String?)?, Error> {
fetchController(
predicate: NSPredicate(
format: "%K == %@", "keyIndex", NSNumber(value: keyPairIndex)
),
context: readContext
)
.map { $0.first.map {
(DID(from: $0), Int($0.keyIndex), $0.alias)
}}
.eraseToAnyPublisher()
}
}

private extension DID {
init(from: CDDID) {
self.init(
schema: from.schema,
method: from.method,
methodId: from.methodId
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Combine
import CoreData
import Domain

extension CDRegisteredDIDDAO: DIDStore {
func addDID(did: DID, keyPairIndex: Int, alias: String?) -> AnyPublisher<Void, Error> {
updateOrCreate(did.string, context: writeContext) { cdobj, _ in
cdobj.parseFrom(did: did, keyPairIndex: keyPairIndex, alias: alias)
}
.map { _ in () }
.eraseToAnyPublisher()
}

func removeDID(did: DID) -> AnyPublisher<Void, Error> {
deleteByIDsPublisher([did.string], context: writeContext)
}

func removeAll() -> AnyPublisher<Void, Error> {
deleteAllPublisher(context: writeContext)
}
}

private extension CDRegisteredDID {
func parseFrom(did: DID, keyPairIndex: Int, alias: String?) {
self.did = did.string
schema = did.schema
method = did.method
methodId = did.methodId
keyIndex = Int64(keyPairIndex)
self.alias = alias
}
}
9 changes: 9 additions & 0 deletions Pluto/Sources/PersistentStorage/DAO/CDRegisteredDIDDAO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Combine
import CoreData

struct CDRegisteredDIDDAO: CoreDataDAO {
typealias CoreDataObject = CDRegisteredDID
let readContext: NSManagedObjectContext
let writeContext: NSManagedObjectContext
let identifierKey: String? = "did"
}
233 changes: 233 additions & 0 deletions Pluto/Tests/CDRegisteredDIDDaoTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import Domain
@testable import Pluto
import XCTest

final class CDRegisteredDIDDaoTests: XCTestCase {
private var coreDataManager: CoreDataManager!

override func setUpWithError() throws {
try super.setUpWithError()
coreDataManager = CoreDataManager(setup: .init(
modelPath: .storeName("PrismPluto", ModelKit.bundle),
storeType: .memory
))
}

func testStoreSingleDID() throws {
let dao = CDRegisteredDIDDAO(
readContext: coreDataManager.mainContext,
writeContext: coreDataManager.editContext
)

let testDID = DID(method: "test", methodId: "test")
let testKeyPairIndex = 0
let testAlias = "Test"
let expectation = expectation(description: "Awaiting publisher")
let cancellable = dao.addDID(
did: testDID,
keyPairIndex: testKeyPairIndex,
alias: testAlias
).flatMap {
dao.getDIDInfo(did: testDID)
}.sink { _ in } receiveValue: {
XCTAssertEqual(testDID, $0?.did)
XCTAssertEqual(testKeyPairIndex, $0?.keyPairIndex)
XCTAssertEqual(testAlias, $0?.alias)
expectation.fulfill()
}

waitForExpectations(timeout: 5)
}

func testStoreNoDuplicatedDID() throws {
let dao = CDRegisteredDIDDAO(
readContext: coreDataManager.mainContext,
writeContext: coreDataManager.editContext
)

let testDID = DID(method: "test", methodId: "test")
let testKeyPairIndex = 0
let testAlias = "Test"
let expectation = expectation(description: "Awaiting publisher")
let cancellable = dao.addDID(
did: testDID,
keyPairIndex: testKeyPairIndex,
alias: testAlias
).flatMap {
dao.addDID(
did: testDID,
keyPairIndex: testKeyPairIndex,
alias: testAlias
)
}
.flatMap {
dao.getAll()
}.sink { _ in } receiveValue: {
XCTAssertEqual($0.count, 1)
expectation.fulfill()
}

waitForExpectations(timeout: 5)
}

func testGetAllDIDs() throws {
let dao = CDRegisteredDIDDAO(
readContext: coreDataManager.mainContext,
writeContext: coreDataManager.editContext
)

let testDID1 = DID(method: "test1", methodId: "test1")
let testKeyPairIndex1 = 0
let testAlias1 = "Test1"

let testDID2 = DID(method: "test2", methodId: "test2")
let testKeyPairIndex2 = 1
let testAlias2 = "Test2"

let expectation = expectation(description: "Awaiting publisher")
let cancellable = dao.addDID(
did: testDID1,
keyPairIndex: testKeyPairIndex1,
alias: testAlias1
).flatMap {
dao.addDID(
did: testDID2,
keyPairIndex: testKeyPairIndex2,
alias: testAlias2
)
}
.flatMap {
dao.getAll()
}.sink { _ in } receiveValue: {
XCTAssertEqual($0.count, 2)
expectation.fulfill()
}

waitForExpectations(timeout: 5)
}

func testGetDIDInfoByDID() throws {
let dao = CDRegisteredDIDDAO(
readContext: coreDataManager.mainContext,
writeContext: coreDataManager.editContext
)

let testDID1 = DID(method: "test1", methodId: "test1")
let testKeyPairIndex1 = 0
let testAlias1 = "Test1"

let testDID2 = DID(method: "test2", methodId: "test2")
let testKeyPairIndex2 = 1
let testAlias2 = "Test2"

let expectation = expectation(description: "Awaiting publisher")
let cancellable = dao.addDID(
did: testDID1,
keyPairIndex: testKeyPairIndex1,
alias: testAlias1
).flatMap {
dao.addDID(
did: testDID2,
keyPairIndex: testKeyPairIndex2,
alias: testAlias2
)
}
.flatMap {
dao.getDIDInfo(did: testDID2)
}.sink { _ in } receiveValue: {
XCTAssertEqual(testDID2, $0?.did)
XCTAssertEqual(testKeyPairIndex2, $0?.keyPairIndex)
XCTAssertEqual(testAlias2, $0?.alias)
expectation.fulfill()
}

waitForExpectations(timeout: 5)
}

func testGetDIDInfoByAlias() throws {
let dao = CDRegisteredDIDDAO(
readContext: coreDataManager.mainContext,
writeContext: coreDataManager.editContext
)

let testDID1 = DID(method: "test1", methodId: "test1")
let testKeyPairIndex1 = 0
let testAlias1 = "Test1"

let testDID2 = DID(method: "test2", methodId: "test2")
let testKeyPairIndex2 = 1
let testAlias2 = "Test2"

let testDID3 = DID(method: "test3", methodId: "test3")
let testKeyPairIndex3 = 2
let testAlias3 = "Test2"

let testAlias = "Test2"

let expectation = expectation(description: "Awaiting publisher")
let cancellable = dao.addDID(
did: testDID1,
keyPairIndex: testKeyPairIndex1,
alias: testAlias1
).flatMap {
dao.addDID(
did: testDID2,
keyPairIndex: testKeyPairIndex2,
alias: testAlias2
)
}
.flatMap {
dao.addDID(
did: testDID3,
keyPairIndex: testKeyPairIndex3,
alias: testAlias3
)
}
.flatMap {
dao.getDIDInfo(alias: testAlias)
}.sink { _ in } receiveValue: {
XCTAssertEqual($0.count, 2)
expectation.fulfill()
}

waitForExpectations(timeout: 5)
}

func testGetDIDInfoByKeyIndex() throws {
let dao = CDRegisteredDIDDAO(
readContext: coreDataManager.mainContext,
writeContext: coreDataManager.editContext
)

let testDID1 = DID(method: "test1", methodId: "test1")
let testKeyPairIndex1 = 0
let testAlias1 = "Test1"

let testDID2 = DID(method: "test2", methodId: "test2")
let testKeyPairIndex2 = 1
let testAlias2 = "Test2"

let expectation = expectation(description: "Awaiting publisher")
let cancellable = dao.addDID(
did: testDID1,
keyPairIndex: testKeyPairIndex1,
alias: testAlias1
).flatMap {
dao.addDID(
did: testDID2,
keyPairIndex: testKeyPairIndex2,
alias: testAlias2
)
}
.flatMap {
dao.getDIDInfo(keyPairIndex: testKeyPairIndex2)
}.sink { _ in } receiveValue: {
XCTAssertEqual(testDID2, $0?.did)
XCTAssertEqual(testKeyPairIndex2, $0?.keyPairIndex)
XCTAssertEqual(testAlias2, $0?.alias)
expectation.fulfill()
}

waitForExpectations(timeout: 5)
}
}
1 change: 0 additions & 1 deletion Pluto/Tests/PlutoTest.swift

This file was deleted.

0 comments on commit 130c0e5

Please sign in to comment.