diff --git a/bindings/swift/Package.swift b/bindings/swift/Package.swift index 00034de6..bdd7c884 100644 --- a/bindings/swift/Package.swift +++ b/bindings/swift/Package.swift @@ -5,6 +5,9 @@ import PackageDescription let package = Package( name: "Web5", + platforms: [ + .macOS(.v10_15) + ], products: [ .library( name: "Web5", @@ -25,5 +28,9 @@ let package = Package( dependencies: [.target(name: "Web5CoreRS")], path: "Sources/UniFFI" ), + .testTarget( + name: "Web5Tests", + dependencies: ["Web5"] + ), ] ) \ No newline at end of file diff --git a/bindings/swift/Sources/Web5/web5.swift b/bindings/swift/Sources/Web5/web5.swift index 35d3a822..05583825 100644 --- a/bindings/swift/Sources/Web5/web5.swift +++ b/bindings/swift/Sources/Web5/web5.swift @@ -1,2 +1,4 @@ // The Swift Programming Language -// https://docs.swift.org/swift-book \ No newline at end of file +// https://docs.swift.org/swift-book + +@_exported import UniFFI \ No newline at end of file diff --git a/bindings/swift/Tests/LocalKeyManagerTests.swift b/bindings/swift/Tests/LocalKeyManagerTests.swift new file mode 100644 index 00000000..ed5ba01a --- /dev/null +++ b/bindings/swift/Tests/LocalKeyManagerTests.swift @@ -0,0 +1,39 @@ +import XCTest +@testable import Web5 + +final class LocalKeyManagerTests: XCTestCase { + func testCanGenerateEd25519Key() throws { + let keyManager = LocalKeyManager.newInMemory() + let keyAlias = try keyManager.generatePrivateKey(curve: Curve.ed25519, keyAlias: nil) + XCTAssertNotEqual(keyAlias.count, 0) + } + + func testCanGenerateSecp256k1Key() throws { + let keyManager = LocalKeyManager.newInMemory() + let keyAlias = try keyManager.generatePrivateKey(curve: Curve.secp256k1, keyAlias: nil) + XCTAssertNotEqual(keyAlias.count, 0) + } + + func testCanSignAndVerify() throws { + let keyManager = LocalKeyManager.newInMemory() + let keyAlias = try keyManager.generatePrivateKey(curve: Curve.ed25519, keyAlias: nil) + XCTAssertNotEqual(keyAlias.count, 0) + + let payload = "hello world".data(using: .utf8)!.map { $0 } + let signature = try keyManager.sign(keyAlias: keyAlias, payload: payload) + XCTAssertNotEqual(signature.count, 0) + + let publicKey = try keyManager.getPublicKey(keyAlias: keyAlias) + XCTAssertNoThrow(try publicKey.verify(payload: payload, signature: Array(signature))) + } + + func testCanExportAndImportKeys() throws { + let keyManager = LocalKeyManager.newInMemory() + _ = try keyManager.generatePrivateKey(curve: Curve.secp256k1, keyAlias: nil) + + let exportedPrivateKeys = try keyManager.exportPrivateKeys() + XCTAssertEqual(exportedPrivateKeys.count, 1) + + XCTAssertNoThrow(try keyManager.importPrivateKeys(privateKeys: exportedPrivateKeys)) + } +} \ No newline at end of file diff --git a/examples/SwiftExample/Package.swift b/examples/SwiftExample/Package.swift index 55e4e237..ab473007 100644 --- a/examples/SwiftExample/Package.swift +++ b/examples/SwiftExample/Package.swift @@ -5,6 +5,9 @@ import PackageDescription let package = Package( name: "SwiftExample", + platforms: [ + .macOS(.v10_15) + ], dependencies: [ .package(name: "Web5", path: "../../bindings/swift") ], diff --git a/examples/SwiftExample/Sources/main.swift b/examples/SwiftExample/Sources/main.swift index 61ea4702..5d136ecd 100644 --- a/examples/SwiftExample/Sources/main.swift +++ b/examples/SwiftExample/Sources/main.swift @@ -1,55 +1,6 @@ import Foundation - import UniFFI +import UniFFI print("hello world") - -let jwk = UniFFI.Jwk(alg: "ES256K", kty: "EC", crv: "secp256k1", d: "P3hRuve79GaggsVdQG_w-JpdM6dHXG33-1nwZ8Jw07g", x: "vA8umEbOhhQjFfk1-byvVxtJNRtwQSEE0UMVmxSN9K4", y: "A1qGUBx-wpznzVI0DLu8kEhDZ77ou533NKSCw90R33Q") -print(jwk) - -let thumbprint = try UniFFI.computeThumbprint(jwk: jwk) -print("Thumbprint: \(thumbprint)") - -let ed25519Jwk = try UniFFI.ed25519Generate() -let ed25519Thumbprint = try UniFFI.computeThumbprint(jwk: ed25519Jwk) -print("Thumbprint (Ed25519): \(ed25519Thumbprint)") - -let payload = Data("hello world".utf8) -let signature = try UniFFI.ed25519Sign(privateJwk: ed25519Jwk, payload: payload) -print("Signature: \(signature.base64EncodedString())") - -try UniFFI.ed25519Verify(publicJwk: ed25519Jwk, payload: payload, signature: signature) -print("verify() passed as expected") - -do { - try UniFFI.ed25519Verify(publicJwk: ed25519Jwk, payload: payload, signature: Data("invalid sig".utf8)) -} catch { - print("verify() failed as expected") -} - -let keyManager = UniFFI.LocalJwkManager() -let keyAlias = try keyManager.generatePrivateKey(curve: Curve.ed25519, keyAlias: nil) -print("key alias \(keyAlias)") -let publicKey = try keyManager.getPublicKey(keyAlias: keyAlias) -print("public key \(publicKey)") -let signature2 = try keyManager.sign(keyAlias: keyAlias, payload: payload) -try UniFFI.ed25519Verify(publicJwk: publicKey, payload: payload, signature: signature2) -print("signed & verified \(signature2.base64EncodedString())") -let privateKeys = try keyManager.exportPrivateKeys() -print("private keys \(privateKeys)") -try keyManager.importPrivateKeys(privateKeys: privateKeys) -print("imported private keys") - -let identifier = try UniFFI.identifierParse(didUri: "did:example:123456789abcdefghi;foo=bar;baz=qux?foo=bar&baz=qux#keys-1") -print(identifier) - -let verificationMethod = UniFFI.VerificationMethod(id: "did:jwk:eyJrdHkiOiJPS1AiLCJjcnYiOiJFZDI1NTE5IiwieCI6ImRKZ3VIWDF0QTZORlRnWlU0ZkUzZkNTTEVnSlI2NU9EOC1uM1JmYjVaMlkifQ#0", type: "JsonWebKey", controller: "did:jwk:eyJrdHkiOiJPS1AiLCJjcnYiOiJFZDI1NTE5IiwieCI6ImRKZ3VIWDF0QTZORlRnWlU0ZkUzZkNTTEVnSlI2NU9EOC1uM1JmYjVaMlkifQ", publicKeyJwk: jwk) -let document = UniFFI.Document(id: "did:jwk:eyJrdHkiOiJPS1AiLCJjcnYiOiJFZDI1NTE5IiwieCI6ImRKZ3VIWDF0QTZORlRnWlU0ZkUzZkNTTEVnSlI2NU9EOC1uM1JmYjVaMlkifQ", context: ["https://www.w3.org/ns/did/v1"], controller: nil, alsoKnownAs: nil, verificationMethod: [verificationMethod], authentication: [verificationMethod.id], assertionMethod: [verificationMethod.id], keyAgreement: [verificationMethod.id], capabilityInvocation: [verificationMethod.id], capabilityDelegation: [verificationMethod.id], service: nil) -print(document) - -let keySelector1 = UniFFI.KeySelector.keyId(keyId: verificationMethod.id) -let verificationMethod2 = try UniFFI.getVerificationMethod(document: document, keySelector: keySelector1) -print(verificationMethod2) - -let keySelector2 = UniFFI.KeySelector.methodType(methodType: UniFFI.VerificationMethodType.authentication) -let verificationMethod3 = try UniFFI.getVerificationMethod(document: document, keySelector: keySelector2) -print(verificationMethod3) +let keyManager = UniFFI.LocalKeyManager.newInMemory() +print(keyManager) \ No newline at end of file