Skip to content

Commit

Permalink
Batch 10: BIP39Entropy UF exp and more (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX authored Apr 24, 2024
1 parent 5498570 commit fcf1fc4
Show file tree
Hide file tree
Showing 57 changed files with 1,025 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,81 @@
import SargonUniFFI
import Foundation

public typealias BIP39Entropy = Bip39Entropy

extension Entropy16Bytes {
public init(bytes: some DataProtocol) throws {
self = try newEntropy16BytesFromBytes(bytes: Data(bytes))
}
public static func generate() -> Self {
try! Self.init(bytes: Data.random(byteCount: 16))
}
}

extension Entropy20Bytes {
public init(bytes: some DataProtocol) throws {
self = try newEntropy20BytesFromBytes(bytes: Data(bytes))
}
public static func generate() -> Self {
try! Self.init(bytes: Data.random(byteCount: 20))
}
}

extension Entropy24Bytes {
public init(bytes: some DataProtocol) throws {
self = try newEntropy24BytesFromBytes(bytes: Data(bytes))
}
public static func generate() -> Self {
try! Self.init(bytes: Data.random(byteCount: 24))
}
}

extension Entropy28Bytes {
public init(bytes: some DataProtocol) throws {
self = try newEntropy28BytesFromBytes(bytes: Data(bytes))
}
public static func generate() -> Self {
try! Self.init(bytes: Data.random(byteCount: 28))
}
}

extension Entropy32Bytes {
public init(bytes: some DataProtocol) throws {
self = try newEntropy32BytesFromBytes(bytes: Data(bytes))
}
public static func generate() -> Self {
try! Self.init(bytes: Data.random(byteCount: 32))
}
}

extension Mnemonic {
public var phrase: String {
mnemonicPhrase(from: self)
}

public init(phrase: String) throws {
self = try newMnemonicFromPhrase(phrase: phrase)
public init(wordCount: BIP39WordCount, language: BIP39Language) {
let entropy: BIP39Entropy = switch wordCount {
case .twentyFour:
BIP39Entropy.entropyOf32Bytes(.generate())
case .twentyOne:
BIP39Entropy.entropyOf28Bytes(.generate())
case .eighteen:
BIP39Entropy.entropyOf24Bytes(.generate())
case .fifteen:
BIP39Entropy.entropyOf20Bytes(.generate())
case .twelve:
BIP39Entropy.entropyOf16Bytes(.generate())
}

self = newMnemonicGenerateWithEntropy(entropy: entropy, language: language)
}

public init(phrase: String, language: BIP39Language) throws {
self = try newMnemonicFromPhraseLanguage(phrase: phrase, language: language)
public init(phrase: String, language: BIP39Language? = nil) throws {
if let language {
self = try newMnemonicFromPhraseLanguage(phrase: phrase, language: language)
} else {
self = try newMnemonicFromPhrase(phrase: phrase)
}
}

public init(words: some Collection<BIP39Word>) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import Foundation
import SargonUniFFI

extension Exactly32Bytes {

public func jsonStringLiteral() -> String {
exactly32BytesToJsonString(exactly32Bytes: self)
}

public init(
jsonStringLiteral: String
) throws {
self = try newExactly32BytesFromJsonString(
jsonString: jsonStringLiteral
)
}

public init(bytes: some DataProtocol) throws {
self = try newExactly32Bytes(bytes: BagOfBytes(bytes))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ extension AppearanceID: CaseIterable {
public static var allCases: [Self] {
appearanceIdsAll()
}

public static func fromNumberOfAccounts(_ numberOfAccounts: Int) -> Self {
newAppearanceIdFromNumberOfAccountsOnNetwork(count: UInt64(numberOfAccounts))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// File.swift
//
//
// Created by Alexander Cyon on 2024-04-23.
//

import Foundation
import SargonUniFFI

extension FactorSourceCryptoParameters {
public static let babylon: Self = newFactorSourceCryptoParametersPresetBabylonOnly()

public static let olympia: Self = newFactorSourceCryptoParametersPresetOlympiaOnly()

public static let babylonOlympiaCompatible: Self = newFactorSourceCryptoParametersPresetBabylonOlympiaCompatible()

public var supportsOlympia: Bool {
factorSourceCryptoParametersSupportsOlympia(parameters: self)
}

public var supportsBabylon: Bool {
factorSourceCryptoParametersSupportsBabylon(parameters: self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import Foundation
import SargonUniFFI

extension FactorSourceIDFromHash {
public func toString() -> String {
factorSourceIdFromHashToString(factorSourceId: self)

public init(kind: FactorSourceKind, mnemonicWithPassphrase: MnemonicWithPassphrase) {
self = newFactorSourceIdFromHashFromMnemonicWithPassphrase(
factorSourceKind: kind,
mnemonicWithPassphrase: mnemonicWithPassphrase
)
}

public init(jsonData: some DataProtocol) throws {
Expand All @@ -20,4 +24,8 @@ extension FactorSourceIDFromHash {
public func jsonData() -> Data {
factorSourceIDFromHashToJsonBytes(factorSourceIDFromHash: self)
}

public func toString() -> String {
factorSourceIdFromHashToString(factorSourceId: self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// File.swift
//
//
// Created by Alexander Cyon on 2024-04-24.
//

import Foundation
import SargonUniFFI

#if DEBUG
extension BIP39Word {
public static let sample: Self = newBip39WordSample()
public static let sampleOther: Self = newBip39WordSampleOther()
}
#endif // DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// File.swift
//
//
// Created by Alexander Cyon on 2024-04-23.
//

import Foundation
import SargonUniFFI

#if DEBUG
extension FactorSourceCryptoParameters {
public static let sample: Self = newFactorSourceCryptoParametersSample()
public static let sampleOther: Self = newFactorSourceCryptoParametersSampleOther()
}
#endif // DEBUG
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import SargonUniFFI

public typealias BIP39Word = Bip39Word
extension BIP39Word: SargonModel {}
extension BIP39Word: Identifiable {

public typealias ID = U11

public var id: ID {
index
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ import SargonUniFFI
extension Exactly32Bytes: ExactlyNBytesProtocol {
public static let length = 32
}

extension Exactly32Bytes: SargonStringCodable {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ extension DeviceFactorSource: Identifiable {
}

extension DeviceFactorSource: FactorSourceProtocol {
public static let kind: FactorSourceKind = .device

public static func extract(from someFactorSource: some BaseFactorSourceProtocol) -> Self? {
guard case let .device(factorSource) = someFactorSource.asGeneral else { return nil }
return factorSource
}

public var asGeneral: FactorSource {
.device(value: self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension FactorSource: Identifiable {
}
}

extension FactorSource: FactorSourceProtocol {
extension FactorSource: BaseFactorSourceProtocol {
public var factorSourceID: FactorSourceID {
id
}
Expand All @@ -31,7 +31,44 @@ extension FactorSource: FactorSourceProtocol {
}
}

public var common: FactorSourceCommon {
get {
switch self {
case let .device(value): value.common
case let .ledger(value): value.common
}
}
set {
switch self {
case var .device(source):
source.common = newValue
self = .device(value: source)
case var .ledger(source):
source.common = newValue
self = .ledger(value: source)
}
}
}

public var asGeneral: FactorSource { self }


public func extract<F>(_ type: F.Type = F.self) -> F? where F: FactorSourceProtocol {
F.extract(from: self)
}

public func extract<F>(as _: F.Type = F.self) throws -> F where F: FactorSourceProtocol {
guard let extracted = extract(F.self) else {
throw IncorrectFactorSourceType(
expectedKind: F.kind,
actualKind: factorSourceKind
)
}
return extracted
}

public struct IncorrectFactorSourceType: Swift.Error {
public let expectedKind: FactorSourceKind
public let actualKind: FactorSourceKind
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,27 @@ extension FactorSourceID: CustomStringConvertible {
toString()
}
}

extension FactorSourceID: FactorSourceIDProtocol {
public var asGeneral: FactorSourceID {
self
}
}

extension FactorSourceID {

public func extract<F>(_ type: F.Type = F.self) -> F? where F: FactorSourceIDSpecificProtocol {
F.extract(from: self)
}

public func extract<F>(as _: F.Type = F.self) throws -> F where F: FactorSourceIDSpecificProtocol {
guard let extracted = extract(F.self) else {
throw IncorrectFactorSourceIDType()
}
return extracted
}


}

public struct IncorrectFactorSourceIDType: Swift.Error {}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ public typealias FactorSourceIDFromAddress = FactorSourceIdFromAddress
extension FactorSourceIDFromAddress: SargonModel {}
extension FactorSourceIDFromAddress: SargonObjectCodable {}

extension FactorSourceIDFromAddress {
extension FactorSourceIDFromAddress: FactorSourceIDSpecificProtocol {
public var asGeneral: FactorSourceID {
.address(value: self)
}
}


extension FactorSourceIDFromAddress: CustomStringConvertible {
public var description: String {
toString()
public static func extract(from someFactorSourceID: some FactorSourceIDProtocol) -> Self? {
guard case let .address(id) = someFactorSourceID.asGeneral else { return nil }
return id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public typealias FactorSourceIDFromHash = FactorSourceIdFromHash
extension FactorSourceIDFromHash: SargonModel {}
extension FactorSourceIDFromHash: SargonObjectCodable {}

extension FactorSourceIDFromHash {
extension FactorSourceIDFromHash: FactorSourceIDSpecificProtocol {
public var asGeneral: FactorSourceID {
.hash(value: self)
}
}

extension FactorSourceIDFromHash: CustomStringConvertible {
public var description: String {
toString()
public static func extract(from someFactorSourceID: some FactorSourceIDProtocol) -> Self? {
guard case let .hash(id) = someFactorSourceID.asGeneral else { return nil }
return id
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// File.swift
//
//
// Created by Alexander Cyon on 2024-04-24.
//

import Foundation

public protocol FactorSourceIDProtocol: SargonModel & CustomStringConvertible {
var asGeneral: FactorSourceID { get }
func toString() -> String
}

extension FactorSourceIDProtocol {
public var description: String {
toString()
}
}

public protocol FactorSourceIDSpecificProtocol: FactorSourceIDProtocol & Codable {
static func extract(from someFactorSourceID: some FactorSourceIDProtocol) -> Self?
}
Loading

0 comments on commit fcf1fc4

Please sign in to comment.