Skip to content

Commit

Permalink
CAP-36 New P2P links (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
danvleju-rdx authored May 11, 2024
1 parent 08ad89d commit 3d7d351
Show file tree
Hide file tree
Showing 107 changed files with 1,413 additions and 301 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sargon"
version = "0.7.10"
version = "0.7.12"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ extension Ed25519PublicKey {
// Rust: `new_ed25519_public_key_from_bytes`
self = try newEd25519PublicKeyFromBytes(bytes: Data(bytes))
}

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

public func jsonStringLiteral() -> String {
ed25519PublicKeyToJsonString(ed25519PublicKey: self)
}
}

// MARK: Func -> Method / Computed Prop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ extension Ed25519Signature {
public init(exactly exactly64Bytes: Exactly64Bytes) {
self = newEd25519SignatureFromExactly64Bytes(bytes: exactly64Bytes)
}

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

public func jsonStringLiteral() -> String {
ed25519SignatureToJsonString(ed25519Signature: self)
}

public func toString() -> String {
ed25519SignatureToString(signature: self)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import Foundation
import SargonUniFFI

extension DisplayName {
public init(validating name: String) throws {
self = try newDisplayName(name: name)
}

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

public func jsonStringLiteral() -> String {
displayNameToJsonString(displayName: self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ extension Profile {
public func encrypt(password: String) -> Data {
profileEncryptWithPassword(profile: self, encryptionPassword: password)
}

public static func checkIfProfileJsonContainsLegacyP2PLinks(contents: some DataProtocol) -> Bool {
checkIfProfileJsonContainsLegacyP2pLinks(json: Data(contents))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation
import SargonUniFFI

extension LinkConnectionQrData {
public init(jsonData: some DataProtocol) throws {
self = try newLinkConnectionQRDataFromJsonBytes(jsonBytes: Data(jsonData))
}

public func jsonData() -> Data {
linkConnectionQRDataToJsonBytes(linkConnectionQRData: self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation
import SargonUniFFI

extension P2pLink {
public init(jsonData: some DataProtocol) throws {
self = try newP2PLinkFromJsonBytes(jsonBytes: Data(jsonData))
}

public func jsonData() -> Data {
p2PLinkToJsonBytes(p2PLink: self)
}

public var id: ID {
p2pLinkId(link: self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation
import SargonUniFFI

extension RadixConnectPassword {
public init(jsonStringLiteral: String) throws {
self = try newRadixConnectPasswordFromJsonString(jsonString: jsonStringLiteral)
}

public func jsonStringLiteral() -> String {
radixConnectPasswordToJsonString(radixConnectPassword: self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Foundation
import SargonUniFFI

extension RadixConnectPurpose {

public init(string: String) {
self = newRadixConnectPurposeFromString(string: string)
}

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

public func jsonStringLiteral() -> String {
radixConnectPurposeToJsonString(radixConnectPurpose: self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation
import SargonUniFFI

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

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

#if DEBUG
extension RadixConnectPurpose {
public static let sample: Self = newRadixConnectPurposeSample()
public static let sampleOther: Self = newRadixConnectPurposeSampleOther()
}
#endif // DEBUG
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import SargonUniFFI

extension Ed25519PublicKey: SargonStringCodable {}

extension Ed25519PublicKey: PublicKeyProtocol {
public var asGeneral: PublicKey {
PublicKey.ed25519(self)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Foundation
import SargonUniFFI

extension Ed25519Signature: SargonStringCodable {}

extension Ed25519Signature: SignatureProtocol {
public var data: Data {
bytes.data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation
import SargonUniFFI

extension DisplayName: SargonModel {}
extension DisplayName: SargonStringCodable {}

extension DisplayName: CustomStringConvertible {
public var description: String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation
import SargonUniFFI

public typealias LinkConnectionQRData = LinkConnectionQrData

extension LinkConnectionQrData: SargonModel {}
extension LinkConnectionQrData: SargonObjectCodable {}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import SargonUniFFI
public typealias P2PLink = P2pLink

extension P2PLink: SargonModel {}
extension P2PLink: SargonObjectCodable {}

extension P2PLink: Identifiable {
public typealias ID = Hash
public var id: ID {
p2pLinkId(link: self)
}
public typealias ID = PublicKeyHash
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation
import SargonUniFFI

extension RadixConnectPassword: SargonModel {}
extension RadixConnectPassword: SargonStringCodable {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation
import SargonUniFFI

extension RadixConnectPurpose: SargonModel {}
extension RadixConnectPurpose: SargonStringCodable {}

extension RadixConnectPurpose {

public init(rawValue: String) {
self.init(string: rawValue)
}
}

9 changes: 3 additions & 6 deletions apple/Tests/TestCases/Crypto/BIP39/BIP39WordCountTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@ final class BIP39WordCountTests: Test<BIP39WordCount> {
}

func test_id_is_raw_value() {
func doTest(_ sut: SUT) {
eachSample { sut in
XCTAssertEqual(sut.id, sut.rawValue)
}
SUT.sampleValues.forEach(doTest)
}

func test_init_raw_value() {
func doTest(_ sut: SUT) {
eachSample { sut in
XCTAssertEqual(SUT.init(rawValue: sut.rawValue), sut)
}
SUT.sampleValues.forEach(doTest)
}

func test_init_wordCount() {
func doTest(_ sut: SUT) {
eachSample { sut in
XCTAssertEqual(SUT.init(wordCount: Int(sut.rawValue)), sut)
}
SUT.sampleValues.forEach(doTest)
}

func test_comparable_less_than() {
Expand Down
6 changes: 2 additions & 4 deletions apple/Tests/TestCases/Crypto/BIP39/MnemonicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ import XCTest

final class MnemonicTests: Test<Mnemonic> {
func test_phrase_roundtrip() throws {
func doTest(_ sut: SUT) throws {
try eachSample { sut in
let string = sut.description
try XCTAssertEqual(SUT(phrase: string), sut)
try XCTAssertEqual(SUT(phrase: string, language: .english), sut)
try XCTAssertEqual(SUT(phrase: string, language: nil), sut) // ok to skip language
}
try SUT.sampleValues.forEach(doTest)
}

func test_words_roundtrip() throws {
func doTest(_ sut: SUT) throws {
try eachSample { sut in
let words = sut.words
try XCTAssertEqual(SUT(words: words), sut)
}
try SUT.sampleValues.forEach(doTest)
}

func test_new_from_generated_entropy() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,11 @@ final class MnemonicWithPassphraseTests: Test<MnemonicWithPassphrase> {
let signature = sut.sign(hash: msg, path: path)
XCTAssertTrue(publicKey.isValidSignature(signature, for: msg))
}

/// Cyon: We might be able remove this function once we have converted to `swift-testing` which has much more
/// powerful discovery than XCTest, and maybe `eachSampleCodableRoundtripTest` will be picked up as
/// a test directly.
func testJSONRoundtripAllSamples() throws {
try eachSampleCodableRoundtripTest()
}
}
3 changes: 1 addition & 2 deletions apple/Tests/TestCases/Crypto/BIP44LikePathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ final class BIP44LikePathTests: HDPathProtocolTest<BIP44LikePath> {
}

func test_index_roundtrip() {
func doTest(_ sut: SUT) {
eachSample { sut in
let index = sut.addressIndex
XCTAssertEqual(SUT(index: index), sut)
}
SUT.sampleValues.forEach(doTest)
}

func test_index() throws {
Expand Down
3 changes: 1 addition & 2 deletions apple/Tests/TestCases/Crypto/DerivationPathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ final class DerivationPathTests: HDPathProtocolTest<DerivationPath> {


func test_get_hd_path() {
func doTest(_ sut: SUT) {
eachSample { sut in
XCTAssertEqual(sut.path.components.count, sut.toString().matches(of: "/").count)
}
SUT.sampleValues.forEach(doTest)
}

func test_cap26_account_hd_path() {
Expand Down
35 changes: 35 additions & 0 deletions apple/Tests/TestCases/Crypto/Ed25519PublicKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,39 @@ final class Ed25519PublicKeyTests: PublicKeyTest<Ed25519PublicKey> {
func test_cryptokit_interop() throws {
XCTAssertNoThrow(try SUT(hex: Curve25519.Signing.PrivateKey().publicKey.rawRepresentation.hex))
}

func test_codable() throws {
let raw = "\"ec172b93ad5e563bf4932c70e1245034c35467ef2efd4d64ebf819683467e2bf\"".data(using: .utf8)!

// test decoding
let sut = try JSONDecoder().decode(SUT.self, from: raw)
XCTAssertEqual(sut, SUT.sample)

// test encoding
let encoded = try JSONEncoder().encode(sut)
try XCTAssertEqual(JSONDecoder().decode(SUT.self, from: encoded), sut)
}

func test_wrapped_in_obj() throws {
struct Wrapper: Codable, Equatable {
let myString: String
let publicKey: Ed25519PublicKey
}
let json = """
{
"myString": "Foo",
"publicKey": "ec172b93ad5e563bf4932c70e1245034c35467ef2efd4d64ebf819683467e2bf"
}
""".data(using: .utf8)!

let decoded = try JSONDecoder().decode(Wrapper.self, from: json)
XCTAssertEqual(decoded, try Wrapper.init(myString: "Foo", publicKey: .init(hex: "ec172b93ad5e563bf4932c70e1245034c35467ef2efd4d64ebf819683467e2bf")))
}

/// Cyon: We might be able remove this function once we have converted to `swift-testing` which has much more
/// powerful discovery than XCTest, and maybe `eachSampleCodableRoundtripTest` will be picked up as
/// a test directly.
func testJSONRoundtripAllSamples() throws {
try eachSampleCodableRoundtripTest()
}
}
Loading

0 comments on commit 3d7d351

Please sign in to comment.