Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind latest swift, add scaffold for unit test, update swift example app #99

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bindings/swift/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import PackageDescription

let package = Package(
name: "Web5",
platforms: [
.macOS(.v10_15)
],
products: [
.library(
name: "Web5",
Expand All @@ -25,5 +28,9 @@ let package = Package(
dependencies: [.target(name: "Web5CoreRS")],
path: "Sources/UniFFI"
),
.testTarget(
name: "Web5Tests",
dependencies: ["Web5"]
),
]
)
4 changes: 3 additions & 1 deletion bindings/swift/Sources/Web5/web5.swift
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// The Swift Programming Language
// https://docs.swift.org/swift-book
// https://docs.swift.org/swift-book

@_exported import UniFFI
39 changes: 39 additions & 0 deletions bindings/swift/Tests/LocalKeyManagerTests.swift
Original file line number Diff line number Diff line change
@@ -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))
}
}
3 changes: 3 additions & 0 deletions examples/SwiftExample/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import PackageDescription

let package = Package(
name: "SwiftExample",
platforms: [
.macOS(.v10_15)
],
dependencies: [
.package(name: "Web5", path: "../../bindings/swift")
],
Expand Down
55 changes: 3 additions & 52 deletions examples/SwiftExample/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -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)