Skip to content

Commit

Permalink
Batch 11: ToBytes on EntropyNBytes (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX authored Apr 25, 2024
1 parent cdff3ce commit 6a4e34c
Show file tree
Hide file tree
Showing 25 changed files with 267 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,53 +1,6 @@
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// File.swift
//
//
// Created by Alexander Cyon on 2024-04-25.
//

import Foundation
import SargonUniFFI

extension Entropy16Bytes {

public init(bytes: some DataProtocol) throws {
self = try newEntropy16BytesFromBytes(bytes: Data(bytes))
}

public var data: Data {
entropy16BytesToBytes(bytes: self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// File.swift
//
//
// Created by Alexander Cyon on 2024-04-25.
//

import Foundation
import SargonUniFFI

extension Entropy20Bytes {

public init(bytes: some DataProtocol) throws {
self = try newEntropy20BytesFromBytes(bytes: Data(bytes))
}

public var data: Data {
entropy20BytesToBytes(bytes: self)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation
import SargonUniFFI

extension Entropy24Bytes {
public init(bytes: some DataProtocol) throws {
self = try newEntropy24BytesFromBytes(bytes: Data(bytes))
}

public var data: Data {
entropy24BytesToBytes(bytes: self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation
import SargonUniFFI

extension Entropy28Bytes {
public init(bytes: some DataProtocol) throws {
self = try newEntropy28BytesFromBytes(bytes: Data(bytes))
}

public var data: Data {
entropy28BytesToBytes(bytes: self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation
import SargonUniFFI

extension Entropy32Bytes {
public init(bytes: some DataProtocol) throws {
self = try newEntropy32BytesFromBytes(bytes: Data(bytes))
}

public var data: Data {
entropy32BytesToBytes(bytes: self)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import Foundation
import SargonUniFFI

extension Profile {
public init(json bytes: some DataProtocol) throws {

public init(jsonData bytes: some DataProtocol) throws {
self = try newProfileFromJsonBytes(jsonBytes: Data(bytes))
}

Expand All @@ -16,6 +17,9 @@ extension Profile {
public func profileSnapshot() -> Data {
profileToJsonBytes(profile: self)
}
public func jsonData() -> Data {
profileSnapshot()
}

public func encrypt(password: String) -> Data {
profileEncryptWithPassword(profile: self, encryptionPassword: password)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import SargonUniFFI

#if DEBUG
extension Entropy16Bytes {
public static let sample: Self = newEntropy16BytesSample()
public static let sampleOther: Self = newEntropy16BytesSampleOther()
}
#endif // DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import SargonUniFFI

#if DEBUG
extension Entropy20Bytes {
public static let sample: Self = newEntropy20BytesSample()
public static let sampleOther: Self = newEntropy20BytesSampleOther()
}
#endif // DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import SargonUniFFI

#if DEBUG
extension Entropy24Bytes {
public static let sample: Self = newEntropy24BytesSample()
public static let sampleOther: Self = newEntropy24BytesSampleOther()
}
#endif // DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import SargonUniFFI

#if DEBUG
extension Entropy28Bytes {
public static let sample: Self = newEntropy28BytesSample()
public static let sampleOther: Self = newEntropy28BytesSampleOther()
}
#endif // DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import SargonUniFFI

#if DEBUG
extension Entropy32Bytes {
public static let sample: Self = newEntropy32BytesSample()
public static let sampleOther: Self = newEntropy32BytesSampleOther()
}
#endif // DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import SargonUniFFI
import Foundation

public typealias BIP39Entropy = Bip39Entropy
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation
import SargonUniFFI

extension Entropy16Bytes: ExactlyNBytesProtocol {
public static let length = 16
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation
import SargonUniFFI

extension Entropy20Bytes: ExactlyNBytesProtocol {
public static let length = 20
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation
import SargonUniFFI

extension Entropy24Bytes: ExactlyNBytesProtocol {
public static let length = 24
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import Foundation
import SargonUniFFI

extension Entropy28Bytes: ExactlyNBytesProtocol {
public static let length = 28
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation
import SargonUniFFI

extension Entropy32Bytes: ExactlyNBytesProtocol {
public static let length = 32
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SargonUniFFI

extension Profile: SargonModel {}

extension Profile: SargonObjectCodable {}
extension Profile {

public init(
Expand Down
8 changes: 8 additions & 0 deletions apple/Sources/Sargon/Protocols/BinaryProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public protocol BinaryProtocol: BaseBinaryProtocol, ToDataProtocol, CustomString

extension BinaryProtocol {

public var count: Int {
data.count
}

public var hex: String {
data.hex
}

public init(hex: String) throws {
try self.init(bytes: Data(hex: hex))
}
Expand Down
11 changes: 10 additions & 1 deletion apple/Sources/Sargon/Protocols/ExactlyNBytesProtocol.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import SargonUniFFI
import Foundation

public protocol ExactlyNBytesProtocol: BinaryProtocol {
static var length: Int { get }
static var length: Int { get }
}

extension ExactlyNBytesProtocol {
public static func generate() -> Self {
try! Self.init(bytes: Data.random(byteCount: Self.length))
}
}
11 changes: 11 additions & 0 deletions apple/Tests/TestCases/Prelude/Bytes/EntropyOfNBytesTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import CustomDump
import Foundation
import Sargon
import SargonUniFFI
import XCTest

final class Entropy16BytesTests: ExactlyNBytesTest<Entropy16Bytes> {}
final class Entropy20BytesTests: ExactlyNBytesTest<Entropy20Bytes> {}
final class Entropy24BytesTests: ExactlyNBytesTest<Entropy24Bytes> {}
final class Entropy28BytesTests: ExactlyNBytesTest<Entropy28Bytes> {}
final class Entropy32BytesTests: ExactlyNBytesTest<Entropy32Bytes> {}
18 changes: 11 additions & 7 deletions apple/Tests/TestCases/Profile/ProfileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ final class ProfileTests: Test<Profile> {
func test_id_is_header_id() {
XCTAssertNoDifference(SUT.sample.id, SUT.sample.header.id)
}

func test_serialize_deserialize() throws {
let sut = SUT.sample
XCTAssertNoDifference(sut, try Profile(json: sut.profileSnapshot()))
}

func test_codable_roundtrip() throws {
try SUT.sampleValues.forEach(doTestCodableRoundtrip)
}

func test_encryption_roundtrip() throws {
let password = "ultra secret"
let sut = SUT.sample
let encrypted = sut.encrypt(password: password)
let decrypted = try Profile(encrypted: encrypted, decryptionPassword: password)
let encrypted = sut.encrypt(
password: password
)
let decrypted = try Profile(
encrypted: encrypted,
decryptionPassword: password
)
XCTAssertNoDifference(decrypted, sut)
}

Expand Down
15 changes: 15 additions & 0 deletions apple/Tests/Utils/BinaryProtocolTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class PublicKeyTest<SUT_: PublicKeyProtocol>: BinaryProtocolTest<SUT_> {
class SignatureTest<SUT_: SignatureProtocol>: BinaryProtocolTest<SUT_> {}

class ExactlyNBytesTest<SUT_: ExactlyNBytesProtocol>: BinaryProtocolTest<SUT_> {

func test_length() throws {
SUT.sampleValues.forEach {
XCTAssertEqual($0.data.count, SUT.length)
Expand All @@ -38,4 +39,18 @@ class ExactlyNBytesTest<SUT_: ExactlyNBytesProtocol>: BinaryProtocolTest<SUT_> {
XCTAssertNotEqual($0.hash().data, $0.hash().data.hash().data)
}
}

func test_generate_is_indeed_random() {
let n = 10
func doTest(_ sut: SUT) {
let randomCollections: [SUT] = (0..<n).map { _ in
let generated = SUT.generate()
XCTAssertEqual(generated.count, SUT.length)
XCTAssertNotEqual(sut, generated)
return generated
}
XCTAssertEqual(Set(randomCollections).count, n)
}
SUT.sampleValues.forEach(doTest)
}
}
Loading

0 comments on commit 6a4e34c

Please sign in to comment.