Skip to content

Commit

Permalink
Remove UserDefaults for the default encryption key (#292)
Browse files Browse the repository at this point in the history
* remove the user of user defaults from the sdk

* fix up the tests
  • Loading branch information
nplasterer authored Mar 21, 2024
1 parent f4a59a4 commit cc245fc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
11 changes: 1 addition & 10 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,7 @@ public final class Client {

var encryptionKey = options?.mlsEncryptionKey
if (encryptionKey == nil) {
let preferences = UserDefaults.standard
let key = "xmtp-key"
if preferences.data(forKey: key) == nil {
let data = Data(try Crypto.secureRandomBytes(count: 32))
preferences.set(data, forKey: key)
preferences.synchronize()
encryptionKey = data
} else {
encryptionKey = preferences.data(forKey: key)
}
throw ClientError.creationError("No encryption key passed for the database. Please store and provide a secure encryption key.")
}

let v3Client = try await LibXMTP.createClient(
Expand Down
31 changes: 16 additions & 15 deletions Tests/XMTPTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ class ClientTests: XCTestCase {
}

func testPassingSavedKeysWithNoSignerWithMLSErrors() async throws {
try TestConfig.skipIfNotRunningLocalNodeTests()

let key = try Crypto.secureRandomBytes(count: 32)
let bo = try PrivateKey.generate()

do {
let client = try await Client.create(
account: bo,
options: .init(
api: .init(env: .local, isSecure: false),
mlsAlpha: true
mlsAlpha: true,
mlsEncryptionKey: key
)
)
} catch {
Expand All @@ -39,14 +39,14 @@ class ClientTests: XCTestCase {
}

func testPassingSavedKeysWithMLS() async throws {
try TestConfig.skipIfNotRunningLocalNodeTests()

let key = try Crypto.secureRandomBytes(count: 32)
let bo = try PrivateKey.generate()
let client = try await Client.create(
account: bo,
options: .init(
api: .init(env: .local, isSecure: false),
mlsAlpha: true
mlsAlpha: true,
mlsEncryptionKey: key
)
)

Expand All @@ -56,16 +56,15 @@ class ClientTests: XCTestCase {
options: .init(
api: .init(env: .local, isSecure: false),
// Should not need to pass the signer again
mlsAlpha: true
mlsAlpha: true,
mlsEncryptionKey: key
)
)

XCTAssertEqual(client.address, otherClient.address)
}

func testPassingMLSEncryptionKey() async throws {
try TestConfig.skipIfNotRunningLocalNodeTests()

let bo = try PrivateKey.generate()
let key = try Crypto.secureRandomBytes(count: 32)

Expand Down Expand Up @@ -94,9 +93,7 @@ class ClientTests: XCTestCase {
}
}

func testPassingMLSEncryptionKeyAndDatabasePath() async throws {
try TestConfig.skipIfNotRunningLocalNodeTests()

func testPassingMLSEncryptionKeyAndDatabasePath() async throws {
let bo = try PrivateKey.generate()
let key = try Crypto.secureRandomBytes(count: 32)
let documentsURL = try
Expand Down Expand Up @@ -158,21 +155,24 @@ class ClientTests: XCTestCase {
}

func testCanDeleteDatabase() async throws {
let key = try Crypto.secureRandomBytes(count: 32)
let bo = try PrivateKey.generate()
let alix = try PrivateKey.generate()
var boClient = try await Client.create(
account: bo,
options: .init(
api: .init(env: .local, isSecure: false),
mlsAlpha: true
mlsAlpha: true,
mlsEncryptionKey: key
)
)

let alixClient = try await Client.create(
account: alix,
options: .init(
api: .init(env: .local, isSecure: false),
mlsAlpha: true
mlsAlpha: true,
mlsEncryptionKey: key
)
)

Expand All @@ -188,7 +188,8 @@ class ClientTests: XCTestCase {
account: bo,
options: .init(
api: .init(env: .local, isSecure: false),
mlsAlpha: true
mlsAlpha: true,
mlsEncryptionKey: key
)
)

Expand Down
10 changes: 7 additions & 3 deletions Tests/XMTPTests/GroupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ class GroupTests: XCTestCase {
}

func localFixtures() async throws -> LocalFixtures {
let key = try Crypto.secureRandomBytes(count: 32)
let alice = try PrivateKey.generate()
let aliceClient = try await Client.create(
account: alice,
options: .init(
api: .init(env: .local, isSecure: false),
codecs: [GroupMembershipChangedCodec()],
mlsAlpha: true
mlsAlpha: true,
mlsEncryptionKey: key
)
)
let bob = try PrivateKey.generate()
Expand All @@ -60,7 +62,8 @@ class GroupTests: XCTestCase {
options: .init(
api: .init(env: .local, isSecure: false),
codecs: [GroupMembershipChangedCodec()],
mlsAlpha: true
mlsAlpha: true,
mlsEncryptionKey: key
)
)
let fred = try PrivateKey.generate()
Expand All @@ -69,7 +72,8 @@ class GroupTests: XCTestCase {
options: .init(
api: .init(env: .local, isSecure: false),
codecs: [GroupMembershipChangedCodec()],
mlsAlpha: true
mlsAlpha: true,
mlsEncryptionKey: key
)
)

Expand Down

0 comments on commit cc245fc

Please sign in to comment.