Skip to content

Commit

Permalink
Restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
bisgardo committed Feb 26, 2024
1 parent 1b6be13 commit 7a78e6a
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 185 deletions.
161 changes: 4 additions & 157 deletions Sources/ConcordiumSwiftSdk/Wallet.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ConcordiumWalletCrypto
import CryptoKit
import Foundation

Expand All @@ -16,7 +15,7 @@ public class Wallet {
self.generator = generator
}

public func createAccount(credential: AccountCredential) throws -> AccountAddress {
public func createAccount(credential: AccountCredentialCoordinates) throws -> AccountAddress {
let account = try generator.generateAccount(credentials: [credential])
let address = account.address
if try accounts.lookup(address) != nil {
Expand Down Expand Up @@ -47,46 +46,6 @@ public class Wallet {
}
}

public struct Identity {
public var providerIndex: UInt32
public var index: UInt32

public init(providerIndex: UInt32, index: UInt32) {
self.providerIndex = providerIndex
self.index = index
}
}

public struct AccountCredential {
public var identity: Identity
public var counter: UInt8

public init(identity: Identity, counter: UInt8) {
self.identity = identity
self.counter = counter
}
}

public struct Issuer {
public var index: UInt64
public var subindex: UInt64

public init(index: UInt64, subindex: UInt64) {
self.index = index
self.subindex = subindex
}
}

public struct VerifiableCredential {
public var issuer: Issuer
public var index: UInt32

public init(issuer: Issuer, index: UInt32) {
self.issuer = issuer
self.index = index
}
}

public enum AccountGenerationError: Error {
case noCredentials
}
Expand All @@ -100,7 +59,7 @@ public class DeterministicAccountGenerator {
self.commitmentKey = commitmentKey
}

public func generateAccount(credentials: [AccountCredential]) throws -> WalletAccount {
public func generateAccount(credentials: [AccountCredentialCoordinates]) throws -> WalletAccount {
guard let firstCred = credentials.first else {
throw AccountGenerationError.noCredentials
}
Expand All @@ -110,13 +69,13 @@ public class DeterministicAccountGenerator {
)
}

public func generateAccountAddress(firstCredential: AccountCredential) throws -> AccountAddress {
public func generateAccountAddress(firstCredential: AccountCredentialCoordinates) throws -> AccountAddress {
let id = try seed.id(of: firstCredential, commitmentKey: commitmentKey)
let hash = try SHA256.hash(data: Data(hex: id))
return AccountAddress(Data(hash))
}

public func generateKeys(credentials: [AccountCredential]) throws -> AccountKeysCurve25519 {
public func generateKeys(credentials: [AccountCredentialCoordinates]) throws -> AccountKeysCurve25519 {
try AccountKeysCurve25519(
Dictionary(
uniqueKeysWithValues: credentials.enumerated().map { idx, cred in
Expand All @@ -129,115 +88,3 @@ public class DeterministicAccountGenerator {
)
}
}

public enum Network: String {
case mainnet = "Mainnet"
case testnet = "Testnet"
}

/// Class for deterministically deriving cryptographic values related to credentials from a seed.
public class WalletSeed {
private let hex: String
private let network: Network

public init(hex: String, network: Network) {
self.hex = hex
self.network = network
}

public func signingKey(of credential: AccountCredential) throws -> String {
try ConcordiumWalletCrypto.getAccountSigningKey(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: credential.identity.providerIndex,
identityIndex: credential.identity.index,
credentialCounter: UInt32(credential.counter)
)
}

public func publicKey(of credential: AccountCredential) throws -> String {
try ConcordiumWalletCrypto.getAccountPublicKey(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: credential.identity.providerIndex,
identityIndex: credential.identity.index,
credentialCounter: UInt32(credential.counter)
)
}

public func id(of credential: AccountCredential, commitmentKey: String) throws -> String {
try ConcordiumWalletCrypto.getCredentialId(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: credential.identity.providerIndex,
identityIndex: credential.identity.index,
credentialCounter: credential.counter,
commitmentKey: commitmentKey
)
}

public func prfKey(of identity: Identity) throws -> String {
try ConcordiumWalletCrypto.getPrfKey(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: identity.providerIndex,
identityIndex: identity.index
)
}

public func credSec(of identity: Identity) throws -> String {
try ConcordiumWalletCrypto.getIdCredSec(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: identity.providerIndex,
identityIndex: identity.index
)
}

public func signatureBlindingRandomness(of identity: Identity) throws -> String {
try ConcordiumWalletCrypto.getSignatureBlindingRandomness(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: identity.providerIndex,
identityIndex: identity.index
)
}

public func attributeCommitmentRandomness(of credential: AccountCredential, attribute: UInt8) throws -> String {
try ConcordiumWalletCrypto.getAttributeCommitmentRandomness(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: credential.identity.providerIndex,
identityIndex: credential.identity.index,
credentialCounter: UInt32(credential.counter),
attribute: attribute
)
}

public func signingKey(of verifiableCredential: VerifiableCredential) throws -> String {
try ConcordiumWalletCrypto.getVerifiableCredentialSigningKey(
seedHex: hex,
network: network.rawValue,
issuerIndex: verifiableCredential.issuer.index,
issuerSubindex: verifiableCredential.issuer.subindex,
verifiableCredentialIndex: verifiableCredential.index
)
}

public func publicKey(of verifiableCredential: VerifiableCredential) throws -> String {
try ConcordiumWalletCrypto.getVerifiableCredentialPublicKey(
seedHex: hex,
network: network.rawValue,
issuerIndex: verifiableCredential.issuer.index,
issuerSubindex: verifiableCredential.issuer.subindex,
verifiableCredentialIndex: verifiableCredential.index
)
}

public func verifiableCredentialBackupEncryptionKey() throws -> String {
try ConcordiumWalletCrypto.getVerifiableCredentialBackupEncryptionKey(
seedHex: hex,
network: network.rawValue
)
}
}
154 changes: 154 additions & 0 deletions Sources/ConcordiumSwiftSdk/WalletSeed.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import ConcordiumWalletCrypto
import Foundation

/// Class for deterministically deriving cryptographic values related to credentials from a seed.
public class WalletSeed {
private let hex: String
private let network: Network

public init(hex: String, network: Network) {
self.hex = hex
self.network = network
}

public func signingKey(of credential: AccountCredentialCoordinates) throws -> String {
try ConcordiumWalletCrypto.getAccountSigningKey(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: credential.identity.providerIndex,
identityIndex: credential.identity.index,
credentialCounter: UInt32(credential.counter)
)
}

public func publicKey(of credential: AccountCredentialCoordinates) throws -> String {
try ConcordiumWalletCrypto.getAccountPublicKey(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: credential.identity.providerIndex,
identityIndex: credential.identity.index,
credentialCounter: UInt32(credential.counter)
)
}

public func id(of credential: AccountCredentialCoordinates, commitmentKey: String) throws -> String {
try ConcordiumWalletCrypto.getCredentialId(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: credential.identity.providerIndex,
identityIndex: credential.identity.index,
credentialCounter: credential.counter,
commitmentKey: commitmentKey
)
}

public func prfKey(of identity: IdentityCoordinates) throws -> String {
try ConcordiumWalletCrypto.getPrfKey(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: identity.providerIndex,
identityIndex: identity.index
)
}

public func credSec(of identity: IdentityCoordinates) throws -> String {
try ConcordiumWalletCrypto.getIdCredSec(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: identity.providerIndex,
identityIndex: identity.index
)
}

public func signatureBlindingRandomness(of identity: IdentityCoordinates) throws -> String {
try ConcordiumWalletCrypto.getSignatureBlindingRandomness(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: identity.providerIndex,
identityIndex: identity.index
)
}

public func attributeCommitmentRandomness(of credential: AccountCredentialCoordinates, attribute: UInt8) throws -> String {
try ConcordiumWalletCrypto.getAttributeCommitmentRandomness(
seedHex: hex,
network: network.rawValue,
identityProviderIndex: credential.identity.providerIndex,
identityIndex: credential.identity.index,
credentialCounter: UInt32(credential.counter),
attribute: attribute
)
}

public func signingKey(of verifiableCredential: VerifiableCredentialCoordinates) throws -> String {
try ConcordiumWalletCrypto.getVerifiableCredentialSigningKey(
seedHex: hex,
network: network.rawValue,
issuerIndex: verifiableCredential.issuer.index,
issuerSubindex: verifiableCredential.issuer.subindex,
verifiableCredentialIndex: verifiableCredential.index
)
}

public func publicKey(of verifiableCredential: VerifiableCredentialCoordinates) throws -> String {
try ConcordiumWalletCrypto.getVerifiableCredentialPublicKey(
seedHex: hex,
network: network.rawValue,
issuerIndex: verifiableCredential.issuer.index,
issuerSubindex: verifiableCredential.issuer.subindex,
verifiableCredentialIndex: verifiableCredential.index
)
}

public func verifiableCredentialBackupEncryptionKey() throws -> String {
try ConcordiumWalletCrypto.getVerifiableCredentialBackupEncryptionKey(
seedHex: hex,
network: network.rawValue
)
}
}

public enum Network: String {
case mainnet = "Mainnet"
case testnet = "Testnet"
}

public struct IdentityCoordinates {
public var providerIndex: UInt32
public var index: UInt32

public init(providerIndex: UInt32, index: UInt32) {
self.providerIndex = providerIndex
self.index = index
}
}

public struct AccountCredentialCoordinates {
public var identity: IdentityCoordinates
public var counter: UInt8

public init(identity: IdentityCoordinates, counter: UInt8) {
self.identity = identity
self.counter = counter
}
}

public struct IssuerCoordinates {
public var index: UInt64
public var subindex: UInt64

public init(index: UInt64, subindex: UInt64) {
self.index = index
self.subindex = subindex
}
}

public struct VerifiableCredentialCoordinates {
public var issuer: IssuerCoordinates
public var index: UInt32

public init(issuer: IssuerCoordinates, index: UInt32) {
self.issuer = issuer
self.index = index
}
}
Loading

0 comments on commit 7a78e6a

Please sign in to comment.