Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

olympia address #36

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
extension LegacyOlympiaAccountAddress {
public init(validatingAddress bech32String: String) throws {
self = try newLegacyOlympiaAccountAddressFromString(string: bech32String)
}

public init(publicKey: Secp256k1PublicKey) {
self = newLegacyOlympiaAccountAddressFromPublicKey(publicKey: publicKey)

Check warning on line 7 in apple/Sources/Sargon/Extensions/Methods/Address/LegacyOlympiaAccountAddress+Wrap+Functions.swift

View check run for this annotation

Codecov / codecov/patch

apple/Sources/Sargon/Extensions/Methods/Address/LegacyOlympiaAccountAddress+Wrap+Functions.swift#L6-L7

Added lines #L6 - L7 were not covered by tests
}

/// The bech32 encoded string for this address.
public var address: String {
legacyOlympiaAccountAddressToString(address: self)
}

public var networkID: NetworkId {
// We do not allow creation of Non-Mainnet Olympia Addresses.
.mainnet
}

public func toBabylonAddress() -> AccountAddress {
legacyOlympiaAccountAddressToBabylonAccountAddress(address: self)
}

public func isLegacyOfBabylonAddress(_ babylon: AccountAddress) -> Bool {
legacyOlympiaAccountAddressIsLegacyOfBabylon(legacyOlympiaAddress: self, babylonAccountAddress: babylon)
}
}

extension AccountAddress {
public func wasMigratedFromLegacyOlympia(address legacy: LegacyOlympiaAccountAddress) -> Bool {
legacy.isLegacyOfBabylonAddress(self)

Check warning on line 31 in apple/Sources/Sargon/Extensions/Methods/Address/LegacyOlympiaAccountAddress+Wrap+Functions.swift

View check run for this annotation

Codecov / codecov/patch

apple/Sources/Sargon/Extensions/Methods/Address/LegacyOlympiaAccountAddress+Wrap+Functions.swift#L30-L31

Added lines #L30 - L31 were not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extension NonFungibleLocalID {
public func toString() -> String {
nonFungibleLocalIdAsStr(id: self)
}

public init(integer value: UInt64) {
self = newNonFungibleLocalIdInt(value: value)
}

/// Tries to decode an String as NonFungibleLocalID.string
public init(string: String) throws {
self = try newNonFungibleLocalIdString(string: string)
}

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

public init(ruid ruidBytes: some DataProtocol) throws {
self = try newNonFungibleLocalIdRuid(bytes: Data(ruidBytes))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#if DEBUG
extension LegacyOlympiaAccountAddress {
public static let sample: Self = newLegacyOlympiaAccountAddressSample()
public static let sampleOther: Self = newLegacyOlympiaAccountAddressSampleOther()
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extension NonFungibleLocalID {
public static let sample = Self(integer: 1337)
public static let sampleOther = try! Self(string: "FOO")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extension LegacyOlympiaAccountAddress: @unchecked Sendable {}
extension LegacyOlympiaAccountAddress: BaseAddressProtocol {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public typealias NonFungibleLocalID = NonFungibleLocalId

extension NonFungibleLocalID: @unchecked Sendable {}
extension NonFungibleLocalID: SargonModel {}

extension NonFungibleLocalID: CustomStringConvertible {
public var description: String {
toString()
}
}

extension NonFungibleLocalID: ExpressibleByIntegerLiteral {
public init(integerLiteral value: UInt64) {
self.init(integer: value)
}
}

#if DEBUG
extension NonFungibleLocalID: ExpressibleByStringLiteral {
/// Tries to decode an String as NonFungibleLocalID.string
/// Crashes for invalid strings.
public init(stringLiteral value: StringLiteralType) {
try! self.init(string: value)
}
}

extension NonFungibleLocalID: ExpressibleByArrayLiteral {
/// Tries to create a `LocalID.bytes`
public init(arrayLiteral bytes: UInt8...) {
try! self.init(bytes: Data(bytes))
}
}
#endif // DEBUG
28 changes: 24 additions & 4 deletions apple/Sources/Sargon/Protocols/AddressProtocol.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
public protocol AddressProtocol: SargonModel, CustomStringConvertible, CaseIterable where Self.AllCases == [Self] {
#if DEBUG
public protocol BaseBaseAddressProtocol: SargonModel, ExpressibleByStringLiteral {}
#else
public protocol BaseBaseAddressProtocol: SargonModel {}
#endif // DEBUG

public protocol BaseAddressProtocol: BaseBaseAddressProtocol, CustomStringConvertible, CaseIterable where Self.AllCases == [Self] {
init(validatingAddress bech32String: String) throws
var networkID: NetworkID { get }
var address: String { get }
}

extension BaseAddressProtocol {
public var description: String {
address
}
}

#if DEBUG
extension BaseAddressProtocol {
public init(stringLiteral value: String) {
self = try! Self(validatingAddress: value)
}
}
#endif // DEBUG

public protocol AddressProtocol: BaseAddressProtocol {

#if DEBUG
func embed() -> Address
Expand Down Expand Up @@ -36,9 +59,6 @@ extension AddressProtocol where Self: CaseIterable, AllCases == [Self] {


extension AddressProtocol {
public var description: String {
address
}

/// Returns the`ResourceAddress` of `XRD` on the same network
/// as this address.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
final class LegacyOlympiaAccountAddressTests: BaseAddressTest<LegacyOlympiaAccountAddress> {
func test_isLegacyOfBabylonAddress() {
let babylon: AccountAddress = "account_rdx168e8u653alt59xm8ple6khu6cgce9cfx9mlza6wxf7qs3wwdh0pwrf"
XCTAssert(SUT.sample.isLegacyOfBabylonAddress(babylon))
}
func test_to_babylon_address() {
XCTAssertEqual(SUT.sample.toBabylonAddress(), "account_rdx168e8u653alt59xm8ple6khu6cgce9cfx9mlza6wxf7qs3wwdh0pwrf")
}
}


89 changes: 89 additions & 0 deletions apple/Tests/TestCases/Prelude/NonFungibleLocalIDTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
final class NonFungibleLocalIDTests: Test<NonFungibleLocalID> {

// MARK: Integer
func test_integer_valid() {
XCTAssertEqual(
SUT(integer: 42).description,
"#42#"
)
XCTAssertEqual(
SUT(integer: 12345678),
12345678 as SUT // ExpressibleByIntegerliteral
)
}

// MARK: String
func test_string_valid_short() {
XCTAssertEqual(
try SUT(string: "x").description,
"<x>"
)
XCTAssertEqual(
try SUT(string: "x"),
"x" as SUT // ExpressibleByStringLiteral
)
}

func test_string_valid_max_length() {
let s = String(repeating: "z", count: 64)
XCTAssertEqual(
try SUT(string: s).description,
"<\(s)>"
)
}

func test_string_invalid_too_long() {
XCTAssertThrowsError(try SUT(string: "much2longmuch2longmuch2longmuch2longmuch2longmuch2longmuch2longmuch2long"))
}

func test_string_invalid_forbidden_chars() {
XCTAssertThrowsError(try SUT(string: "#$^"))
}

// MARK: Bytes
func test_bytes_valid_short() {
XCTAssertEqual(
try SUT(bytes: [0xde, 0xad, 0xbe, 0xef]).description,
"[deadbeef]"
)
XCTAssertEqual(
try SUT(bytes: [0xde, 0xad, 0xbe, 0xef]),
[0xde, 0xad, 0xbe, 0xef] as SUT // ExpressibleByArrayLiteral
)
}

func test_bytes_valid_max_len() {
XCTAssertEqual(
try SUT(bytes: Data(repeating: 0xab, count: 64)).description,
"[abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab]"
)
}

func test_bytes_invalid_empty() {
XCTAssertThrowsError(try SUT(bytes: Data([])))
}

func test_bytes_invalid_too_long() {
XCTAssertThrowsError(try SUT(bytes: Data(repeating: 0xff, count: 128)))
}

// MARK: RUID
func test_ruid() {
XCTAssertEqual(
try SUT(ruid: Data.sampleAced).description,
"{acedacedacedaced-acedacedacedaced-acedacedacedaced-acedacedacedaced}"
)
}

func test_ruid_invalid_empty() {
XCTAssertThrowsError(try SUT(ruid: Data([])))
}

func test_ruid_invalid_too_short() {
XCTAssertThrowsError(try SUT(ruid: Data(repeating: 0xff, count: 16)))
}

func test_ruid_invalid_too_long() {
XCTAssertThrowsError(try SUT(ruid: Data(repeating: 0xff, count: 128)))
}
}
56 changes: 35 additions & 21 deletions apple/Tests/Utils/AddressProtocolTests.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
class AddressTest<SUT_: AddressProtocol>: Test<SUT_> {
class BaseAddressTest<SUT_: BaseAddressProtocol>: Test<SUT_> {

func test_network_id_of_mainnet_sample() {
XCTAssertNoDifference(SUT.sampleMainnet.networkID, .mainnet)
}

func test_network_id_of_mainnet_sampleOther() {
XCTAssertNoDifference(SUT.sampleMainnetOther.networkID, .mainnet)
func test_network_id_of_sample() {
XCTAssertNoDifference(SUT.sample.networkID, .mainnet)
}

func test_network_id_of_stokenet_sample() {
XCTAssertNoDifference(SUT.sampleStokenet.networkID, .stokenet)
}

func test_network_id_of_stokenet_sampleOther() {
XCTAssertNoDifference(SUT.sampleStokenetOther.networkID, .stokenet)
func test_network_id_of_sampleOther() {
XCTAssertNoDifference(SUT.sampleOther.networkID, .mainnet)
}

func test_all_address_different() {
XCTAssertGreaterThanOrEqual(Set(SUT.allCases).count, 4)
XCTAssertEqual(Set(SUT.allCases).count, SUT.allCases.count)
}

func test_xrd_on_same_network_as_address() {
XCTAssertEqual(SUT.sampleMainnet.xrd, ResourceAddress.sampleMainnetXRD)
XCTAssertEqual(SUT.sampleMainnetOther.xrd, ResourceAddress.sampleMainnetXRD)
XCTAssertEqual(SUT.sampleStokenet.xrd, ResourceAddress.sampleStokenetXRD)
XCTAssertEqual(SUT.sampleStokenetOther.xrd, ResourceAddress.sampleStokenetXRD)
}


func test_bech32_roundtrip() throws {
func doTest(_ address: SUT) throws {
try XCTAssertNoDifference(
Expand All @@ -49,6 +35,34 @@ class AddressTest<SUT_: AddressProtocol>: Test<SUT_> {
SUT.allCases.forEach(doTest)
}


}

class AddressTest<SUT_: AddressProtocol>: BaseAddressTest<SUT_> {

func test_network_id_of_mainnet_sample() {
XCTAssertNoDifference(SUT.sampleMainnet.networkID, .mainnet)
}

func test_network_id_of_mainnet_sampleOther() {
XCTAssertNoDifference(SUT.sampleMainnetOther.networkID, .mainnet)
}

func test_network_id_of_stokenet_sample() {
XCTAssertNoDifference(SUT.sampleStokenet.networkID, .stokenet)
}

func test_network_id_of_stokenet_sampleOther() {
XCTAssertNoDifference(SUT.sampleStokenetOther.networkID, .stokenet)
}

func test_xrd_on_same_network_as_address() {
XCTAssertEqual(SUT.sampleMainnet.xrd, ResourceAddress.sampleMainnetXRD)
XCTAssertEqual(SUT.sampleMainnetOther.xrd, ResourceAddress.sampleMainnetXRD)
XCTAssertEqual(SUT.sampleStokenet.xrd, ResourceAddress.sampleStokenetXRD)
XCTAssertEqual(SUT.sampleStokenetOther.xrd, ResourceAddress.sampleStokenetXRD)
}

func test_embed() {
func doTest(_ address: SUT) {
XCTAssertNoDifference(
Expand Down
3 changes: 3 additions & 0 deletions src/core/error/common_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ pub enum CommonError {

#[error("Failed to convert to Decimal192 from f32 due to overflow, value: {bad_value}")]
DecimalOverflow { bad_value: String } = 10106,

#[error("Invalid Olympia address, not mainnet: {bad_value}")]
InvalidAddressNotOlympiaMainnet { bad_value: String } = 10107,
}

/*
Expand Down
Loading
Loading