Skip to content

Commit

Permalink
New registration specs
Browse files Browse the repository at this point in the history
  • Loading branch information
flypaper0 committed Dec 6, 2023
1 parent b57fe00 commit 5f5cf67
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 65 deletions.
4 changes: 2 additions & 2 deletions Example/Shared/ImportAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ enum ImportAccount: Codable {
}
}

func onSign(message: String) -> SigningResult {
func onSign(message: String) -> CacaoSignature {
let privateKey = Data(hex: privateKey)
let signer = MessageSignerFactory(signerFactory: DefaultSignerFactory()).create()
let signature = try! signer.sign(message: message, privateKey: privateKey, type: .eip191)
return .signed(signature)
return signature
}

static func new() -> ImportAccount {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ final class ConfigurationService {

Task {
do {
try await Notify.instance.register(account: importAccount.account, domain: "com.walletconnect", onSign: importAccount.onSign)
let params = try await Notify.instance.prepareRegistration(account: importAccount.account, domain: "com.walletconnect")
let signature = importAccount.onSign(message: params.message)
try await Notify.instance.register(params: params, signature: signature)
} catch {
DispatchQueue.main.async {
let logMessage = LogMessage(message: "Push Server registration failed with: \(error.localizedDescription)")
Expand Down
16 changes: 13 additions & 3 deletions Sources/WalletConnectIdentity/IdentityClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ public final class IdentityClient {
self.logger = logger
}

public func register(account: Account, domain: String, statement: String, resources: [String], onSign: SigningCallback) async throws -> String {
let pubKey = try await identityService.registerIdentity(account: account, domain: domain, statement: statement, resources: resources, onSign: onSign)
public func prepareRegistration(account: Account,
domain: String,
statement: String,
resources: [String]) async throws -> IdentityRegistrationParams
{
let registration = try await identityService.prepareRegistration(account: account, domain: domain, statement: statement, resources: resources)
logger.debug("Did prepare registration for \(account)")
return registration
}

public func register(params: IdentityRegistrationParams, signature: CacaoSignature) async throws {
let account = try params.account
try await identityService.registerIdentity(params: params, signature: signature)
logger.debug("Did register an account: \(account)")
return pubKey
}

public func goPublic(account: Account) async throws -> AgreementPublicKey {
Expand Down
5 changes: 0 additions & 5 deletions Sources/WalletConnectIdentity/IdentityError.swift

This file was deleted.

11 changes: 11 additions & 0 deletions Sources/WalletConnectIdentity/IdentityRegistrationParams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

public struct IdentityRegistrationParams {
public let message: String
public let payload: CacaoPayload
public let privateIdentityKey: SigningPrivateKey

public var account: Account {
get throws { try Account(DIDPKHString: payload.iss) }
}
}
72 changes: 31 additions & 41 deletions Sources/WalletConnectIdentity/IdentityService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,45 @@ actor IdentityService {
self.messageFormatter = messageFormatter
}

func registerIdentity(account: Account,
func prepareRegistration(account: Account,
domain: String,
statement: String,
resources: [String],
onSign: SigningCallback
) async throws -> String {
resources: [String]) throws -> IdentityRegistrationParams {

let identityKey = SigningPrivateKey()

let payload = CacaoPayload(
iss: account.did,
domain: domain,
aud: identityKey.publicKey.did,
version: getVersion(),
nonce: getNonce(),
iat: iatProvader.iat,
nbf: nil, exp: nil,
statement: statement,
requestId: nil,
resources: resources
)

let message = try messageFormatter.formatMessage(from: payload)

return IdentityRegistrationParams(message: message, payload: payload, privateIdentityKey: identityKey)
}

// TODO: Verifications
func registerIdentity(params: IdentityRegistrationParams, signature: CacaoSignature) async throws {
let account = try params.account

if let identityKey = try? storage.getIdentityKey(for: account) {
return identityKey.publicKey.hexRepresentation
return
}

let identityKey = SigningPrivateKey()
let audience = identityKey.publicKey.did
let cacao = try await makeCacao(account: account, domain: domain, statement: statement, resources: resources, audience: audience, onSign: onSign)
let cacaoHeader = CacaoHeader(t: "eip4361")
let cacao = Cacao(h: cacaoHeader, p: params.payload, s: signature)

try await networkService.registerIdentity(cacao: cacao)
try storage.saveIdentityKey(params.privateIdentityKey, for: account)

return try storage.saveIdentityKey(identityKey, for: account).publicKey.hexRepresentation
}

func registerInvite(account: Account) async throws -> AgreementPublicKey {
Expand Down Expand Up @@ -89,38 +111,6 @@ actor IdentityService {

private extension IdentityService {

func makeCacao(account: Account,
domain: String,
statement: String,
resources: [String],
audience: String,
onSign: SigningCallback
) async throws -> Cacao {

let cacaoHeader = CacaoHeader(t: "eip4361")
let cacaoPayload = CacaoPayload(
iss: account.did,
domain: domain,
aud: audience,
version: getVersion(),
nonce: getNonce(),
iat: iatProvader.iat,
nbf: nil, exp: nil,
statement: statement,
requestId: nil,
resources: resources
)

let result = await onSign(try messageFormatter.formatMessage(from: cacaoPayload))

switch result {
case .signed(let cacaoSignature):
return Cacao(h: cacaoHeader, p: cacaoPayload, s: cacaoSignature)
case .rejected:
throw IdentityError.signatureRejected
}
}

func makeIDAuth<Claims: IDAuthClaims>(account: Account, issuer: DIDKey, claims: Claims.Type) throws -> String {
let identityKey = try storage.getIdentityKey(for: account)

Expand Down
10 changes: 7 additions & 3 deletions Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,13 @@ public class NotifyClient {
self.subscriptionWatcher = subscriptionWatcher
}

public func register(account: Account, domain: String, isLimited: Bool = false, onSign: @escaping SigningCallback) async throws {
try await identityService.register(account: account, domain: domain, isLimited: isLimited, onSign: onSign)
notifyAccountProvider.setAccount(account)
public func prepareRegistration(account: Account, domain: String, allApps: Bool = false) async throws -> IdentityRegistrationParams {
return try await identityService.prepareRegistration(account: account, domain: domain, allApps: allApps)
}

public func register(params: IdentityRegistrationParams, signature: CacaoSignature) async throws {
try await identityService.register(params: params, signature: signature)
notifyAccountProvider.setAccount(try params.account)
try await subscriptionWatcher.start()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

// TODO: Remove
final class NotifyIdentityService {

private let keyserverURL: URL
Expand All @@ -12,13 +13,17 @@ final class NotifyIdentityService {
self.logger = logger
}

public func register(account: Account, domain: String, isLimited: Bool, onSign: @escaping SigningCallback) async throws {
let statement = makeStatement(isLimited: isLimited)
_ = try await identityClient.register(account: account,
public func prepareRegistration(account: Account, domain: String, allApps: Bool) async throws -> IdentityRegistrationParams {
return try await identityClient.prepareRegistration(
account: account,
domain: domain,
statement: statement,
resources: [keyserverURL.absoluteString],
onSign: onSign)
statement: makeStatement(allApps: allApps),
resources: [keyserverURL.absoluteString]
)
}

public func register(params: IdentityRegistrationParams, signature: CacaoSignature) async throws {
try await identityClient.register(params: params, signature: signature)
}

public func unregister(account: Account) async throws {
Expand All @@ -32,11 +37,11 @@ final class NotifyIdentityService {

private extension NotifyIdentityService {

func makeStatement(isLimited: Bool) -> String {
switch isLimited {
case true:
return "I further authorize this app to send me notifications. Read more at https://walletconnect.com/notifications"
func makeStatement(allApps: Bool) -> String {
switch allApps {
case false:
return "I further authorize this app to send me notifications. Read more at https://walletconnect.com/notifications"
case true:
return "I further authorize this app to view and manage my notifications for ALL apps. Read more at https://walletconnect.com/notifications"
}
}
Expand Down

0 comments on commit 5f5cf67

Please sign in to comment.