Skip to content

Commit

Permalink
Merge branch 'feature/xdr-decoding'
Browse files Browse the repository at this point in the history
  • Loading branch information
Volendi committed Mar 4, 2020
2 parents a108065 + c5515ff commit 2b8b15d
Show file tree
Hide file tree
Showing 350 changed files with 9,586 additions and 1,542 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
DLCryptoKit: 491c522583e92c90c09c06bf1192ef741022af33
DLOpenSSL: 78d928228cb6d95fcaa354f4f9aba041ccf95457
TokenDWallet: c66abb4a51c2c035ea439e27cf64e8589773a63f
TokenDWallet: a740f7767f3e66566e4a507f351a2bbf5901aa22

PODFILE CHECKSUM: 2ff703f484f93022caae12590846b6530f3c4419

COCOAPODS: 1.6.1
COCOAPODS: 1.8.0
199 changes: 199 additions & 0 deletions Example/Tests/XDRDecodingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import XCTest
@testable import TokenDWallet

// swiftlint:disable identifier_name

class XDRDecodingTests: XCTestCase {

func testBasicTypes() {
// Int
XCTAssertEqual(try! Int32(xdrBase64: "AAAABQ=="), Int32(5))
XCTAssertEqual(try! UInt32(xdrBase64: "AAAABQ=="), UInt32(5))
XCTAssertEqual(try! UInt64(xdrBase64: "AAAAAAAAAAU="), UInt64(5))
XCTAssertEqual(try! Int64(xdrBase64: "AAAAAAAAAAU="), Int64(5))

// Float, Double and Quadruple is unsupported

// Bool
XCTAssertEqual(try! Bool(xdrBase64: "AAAAAQ=="), true)
}

func testEnum() {
enum TestEnum: Int32, XDREnum {
case a = 0
case b = 1
case c = -1
}

XCTAssertEqual(try! TestEnum(xdrBase64: "AAAAAA=="), TestEnum.a)
XCTAssertEqual(try! TestEnum(xdrBase64: "AAAAAQ=="), TestEnum.b)
XCTAssertEqual(try! TestEnum(xdrBase64: "/////w=="), TestEnum.c)
}

func testOptional() {
var test: Int32?
XCTAssertEqual(decodeOptionalInt32(xdrBase64: "AAAAAA=="), test)
test = 5
XCTAssertEqual(decodeOptionalInt32(xdrBase64: "AAAAAQAAAAU="), test)
}

private func decodeOptionalInt32(xdrBase64: String) -> Int32? {
var data = Data(base64Encoded: xdrBase64)!
if (try! Bool(xdrData: &data)) {
return try! Int32(xdrData: &data)
} else {
return nil
}
}

func testString() {
XCTAssertEqual(try! String(xdrBase64: "AAAABHRlc3Q="), "test")
}

func testOpaque() {
// Dinamic size
let data = Data(bytes: [1])
XCTAssertEqual(try! Data(xdrBase64: "AAAAAQE="), data)
// Fixed size
struct XDRDataFixed1: XDRDataFixed {
static var length: Int { return 1 }

var wrapped: Data

init() {
self.wrapped = Data()
}
}
XCTAssertEqual(try! XDRDataFixed1(xdrBase64: "AQ=="), try! XDRDataFixed1(data))
}

func testArray() {
// Dinamic size
let data = [Int64(1)]
XCTAssertEqual(try! decodeArrayInt64(xdrBase64: "AAAAAQAAAAAAAAAB"), data)
struct XDRArrayFixed1<WrappedElement: XDRCodable>: XDRArrayFixed {
typealias Element = WrappedElement

static var length: Int { return 1 }

var wrapped: [WrappedElement]

init() {
self.wrapped = [WrappedElement]()
}
}
// Fixed size
XCTAssertEqual(try! XDRArrayFixed1<Int64>(xdrBase64: "AAAAAAAAAAE=").wrapped, try! XDRArrayFixed1(data).wrapped)
}

private func decodeArrayInt64(xdrBase64: String) throws -> [Int64] {
var data = Data(base64Encoded: xdrBase64)!
let length = try Int32(xdrData: &data)
var result = [Int64]()
for _ in 1...length {
result.append(try Int64(xdrData: &data))
}

return result
}

func testDescriminatedUnion() {
enum TestDescriminatedUnion: XDRDiscriminatedUnion, XDRDecodable {
case a(Int64)
case b(Int32)
case c

var discriminant: Int32 {
switch self {
case .a: return 0
case .b: return 1
case .c: return 2
}
}

func toXDR() -> Data {
var xdr = Data()

xdr.append(self.discriminant.toXDR())

switch self {
case .a(let data): xdr.append(data.toXDR())
case .b(let data): xdr.append(data.toXDR())
case .c: xdr.append(Data())
}

return xdr
}

init(xdrData: inout Data) throws {
let discriminant = try Int32(xdrData: &xdrData)

switch discriminant {
case 0:
let data = try Int64(xdrData: &xdrData)
self = .a(data)
case 1:
let data = try Int32(xdrData: &xdrData)
self = .b(data)
case 2:
self = .c
default:
throw XDRErrors.unknownEnumCase
}
}
}

XCTAssertEqual(try! TestDescriminatedUnion(xdrBase64: "AAAAAAAAAAAAAAAB").toXdrBase64String(), TestDescriminatedUnion.a(1).toXdrBase64String())
XCTAssertEqual(try! TestDescriminatedUnion(xdrBase64: "AAAAAQAAAAE=").toXdrBase64String(), TestDescriminatedUnion.b(1).toXdrBase64String())
XCTAssertEqual(try! TestDescriminatedUnion(xdrBase64: "AAAAAg==").toXdrBase64String(), TestDescriminatedUnion.c.toXdrBase64String())
}

func testStruct() {
enum TestEnum: Int32, XDREnum {
case a = 0
case b = 1
case c = -1
}
struct Test: XDRCodable {
var a: Int32
var b: Int32?
var c: Bool
var d: TestEnum

func toXDR() -> Data {
var xdr = Data()

xdr.append(self.a.toXDR())
xdr.append(self.b.toXDR())
xdr.append(self.c.toXDR())
xdr.append(self.d.toXDR())

return xdr
}

init(a: Int32, b: Int32?, c: Bool, d: TestEnum) {
self.a = a
self.b = b
self.c = c
self.d = d
}

init(xdrData: inout Data) throws {
self.a = try Int32(xdrData: &xdrData)
if (try Bool(xdrData: &xdrData)) {
self.b = try Int32(xdrData: &xdrData)
} else {
self.b = nil
}
self.c = try Bool(xdrData: &xdrData)
self.d = try TestEnum(xdrData: &xdrData)
}
}

let test = Test(a: 1, b: nil, c: true, d: TestEnum.a)

XCTAssertEqual(try! Test(xdrBase64: "AAAAAQAAAAAAAAABAAAAAA==").toXdrBase64String(), test.toXdrBase64String())
}
}

// swiftlint:enable identifier_name
36 changes: 32 additions & 4 deletions Example/Tests/XDREncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class XDREncodingTests: XCTestCase {
case c = -1
}

XCTAssertEqual(TestEnum.a.rawValue.toXDR().base64, "AAAAAA==")
XCTAssertEqual(TestEnum.b.rawValue.toXDR().base64, "AAAAAQ==")
XCTAssertEqual(TestEnum.c.rawValue.toXDR().base64, "/////w==")
XCTAssertEqual(TestEnum.a.toXDR().base64, "AAAAAA==")
XCTAssertEqual(TestEnum.b.toXDR().base64, "AAAAAQ==")
XCTAssertEqual(TestEnum.c.toXDR().base64, "/////w==")
}

func testOptional() {
Expand Down Expand Up @@ -62,8 +62,19 @@ class XDREncodingTests: XCTestCase {
// Dinamic size
let data = [Int64(1)]
XCTAssertEqual(data.toXDR().base64, "AAAAAQAAAAAAAAAB")
struct XDRArrayFixed1<WrappedElement: XDRCodable>: XDRArrayFixed {
typealias Element = WrappedElement

static var length: Int { return 1 }

var wrapped: [WrappedElement]

init() {
self.wrapped = [WrappedElement]()
}
}
// Fixed size
XCTAssertEqual(XDRArrayFixed(data).toXDR().base64, "AAAAAAAAAAE=")
XCTAssertEqual(try! XDRArrayFixed1(data).toXDR().base64, "AAAAAAAAAAE=")
}

func testDescriminatedUnion() {
Expand Down Expand Up @@ -93,6 +104,23 @@ class XDREncodingTests: XCTestCase {

return xdr
}

init(xdrData: inout Data) throws {
let discriminant = try Int32(xdrData: &xdrData)

switch discriminant {
case 0:
let data = try Int64(xdrData: &xdrData)
self = .a(data)
case 1:
let data = try Int32(xdrData: &xdrData)
self = .b(data)
case 2:
self = .c
default:
throw XDRErrors.unknownEnumCase
}
}
}

XCTAssertEqual(TestDescriminatedUnion.a(1).toXDR().base64, "AAAAAAAAAAAAAAAB")
Expand Down
4 changes: 4 additions & 0 deletions Example/Tests/XDRTypesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class XDRTypesTests: XCTestCase {
let op = Operation(sourceAccount: nil, body: Operation.OperationBody.manageBalance(manageBalanceOp))

XCTAssertEqual(op.toXDR().base64, "AAAAAAAAAAkAAAAAAAAAAHteR0/xHy/UY52BNKpS2XhbRQ9sVCsVfrSwiwW7X7B9AAAAA09MRwAAAAAA")
// Decoding
XCTAssertEqual(try! Operation(xdrBase64: "AAAAAAAAAAkAAAAAAAAAAHteR0/xHy/UY52BNKpS2XhbRQ9sVCsVfrSwiwW7X7B9AAAAA09MRwAAAAAA").toXdrBase64String(), "AAAAAAAAAAkAAAAAAAAAAHteR0/xHy/UY52BNKpS2XhbRQ9sVCsVfrSwiwW7X7B9AAAAA09MRwAAAAAA")
}

func testDeleteBalanceOp() {
Expand All @@ -37,6 +39,8 @@ class XDRTypesTests: XCTestCase {
let op = Operation(sourceAccount: nil, body: Operation.OperationBody.manageBalance(manageBalanceOp))

XCTAssertEqual(op.toXDR().base64, "AAAAAAAAAAkAAAABAAAAAHteR0/xHy/UY52BNKpS2XhbRQ9sVCsVfrSwiwW7X7B9AAAAA09MRwAAAAAA")
// Decoding
XCTAssertEqual(try! Operation(xdrBase64: "AAAAAAAAAAkAAAABAAAAAHteR0/xHy/UY52BNKpS2XhbRQ9sVCsVfrSwiwW7X7B9AAAAA09MRwAAAAAA").toXdrBase64String(), "AAAAAAAAAAkAAAABAAAAAHteR0/xHy/UY52BNKpS2XhbRQ9sVCsVfrSwiwW7X7B9AAAAA09MRwAAAAAA")
}
}

Expand Down
4 changes: 4 additions & 0 deletions Example/TokenDWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
7D8C0B06FEDBFDD4F6A860AA /* libPods-TokenDWallet_Tests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 46DC2F103B0418D6E33CA14A /* libPods-TokenDWallet_Tests.a */; };
7FABEA6F20FF4EE30005E751 /* ECDSASignDecoratedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FABEA6E20FF4EE30005E751 /* ECDSASignDecoratedTests.swift */; };
7FABEA7120FF54E60005E751 /* TransactionModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FABEA7020FF54E60005E751 /* TransactionModelTests.swift */; };
C47C788D240D75900068C152 /* XDRDecodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C47C788C240D75900068C152 /* XDRDecodingTests.swift */; };
C4C0EF6B2094B57700DAC6C4 /* XDREncodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4C0EF6A2094B57700DAC6C4 /* XDREncodingTests.swift */; };
C4C0EF7F2097053200DAC6C4 /* Base32CheckTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4C0EF7D209704D300DAC6C4 /* Base32CheckTests.swift */; };
C4C0EFAC20971CDC00DAC6C4 /* XDRTypesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4C0EFAA20971CB400DAC6C4 /* XDRTypesTests.swift */; };
Expand Down Expand Up @@ -47,6 +48,7 @@
7FABEA6E20FF4EE30005E751 /* ECDSASignDecoratedTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ECDSASignDecoratedTests.swift; sourceTree = "<group>"; };
7FABEA7020FF54E60005E751 /* TransactionModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionModelTests.swift; sourceTree = "<group>"; };
9A7D0DDACE2712C49B8FB4B1 /* Pods-TokenDWallet_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TokenDWallet_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-TokenDWallet_Tests/Pods-TokenDWallet_Tests.release.xcconfig"; sourceTree = "<group>"; };
C47C788C240D75900068C152 /* XDRDecodingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XDRDecodingTests.swift; sourceTree = "<group>"; };
C4C0EF6A2094B57700DAC6C4 /* XDREncodingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XDREncodingTests.swift; sourceTree = "<group>"; };
C4C0EF7D209704D300DAC6C4 /* Base32CheckTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base32CheckTests.swift; sourceTree = "<group>"; };
C4C0EFAA20971CB400DAC6C4 /* XDRTypesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XDRTypesTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -121,6 +123,7 @@
7FABEA6E20FF4EE30005E751 /* ECDSASignDecoratedTests.swift */,
7FABEA7020FF54E60005E751 /* TransactionModelTests.swift */,
C4C0EF6A2094B57700DAC6C4 /* XDREncodingTests.swift */,
C47C788C240D75900068C152 /* XDRDecodingTests.swift */,
C4C0EFAA20971CB400DAC6C4 /* XDRTypesTests.swift */,
607FACE91AFB9204008FA782 /* Supporting Files */,
);
Expand Down Expand Up @@ -364,6 +367,7 @@
buildActionMask = 2147483647;
files = (
7FABEA7120FF54E60005E751 /* TransactionModelTests.swift in Sources */,
C47C788D240D75900068C152 /* XDRDecodingTests.swift in Sources */,
C4C0EF7F2097053200DAC6C4 /* Base32CheckTests.swift in Sources */,
C4C0EFAC20971CDC00DAC6C4 /* XDRTypesTests.swift in Sources */,
C4C0EF6B2094B57700DAC6C4 /* XDREncodingTests.swift in Sources */,
Expand Down
17 changes: 16 additions & 1 deletion Sources/TokenDWallet/Xdr/AMLAlertDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Foundation
// };

// ===========================================================================
public struct AMLAlertDetails: XDREncodable {
public struct AMLAlertDetails: XDRCodable {
public var comment: String
public var ext: AMLAlertDetailsExt

Expand All @@ -40,6 +40,11 @@ public struct AMLAlertDetails: XDREncodable {
return xdr
}

public init(xdrData: inout Data) throws {
self.comment = try String(xdrData: &xdrData)
self.ext = try AMLAlertDetailsExt(xdrData: &xdrData)
}

public enum AMLAlertDetailsExt: XDRDiscriminatedUnion {
case emptyVersion()

Expand All @@ -61,5 +66,15 @@ public struct AMLAlertDetails: XDREncodable {
return xdr
}

public init(xdrData: inout Data) throws {
let discriminant = try Int32(xdrData: &xdrData)

switch discriminant {
case LedgerVersion.emptyVersion.rawValue: self = .emptyVersion()
default:
throw XDRErrors.unknownEnumCase
}
}

}
}
19 changes: 18 additions & 1 deletion Sources/TokenDWallet/Xdr/AMLAlertRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Foundation
// };

// ===========================================================================
public struct AMLAlertRequest: XDREncodable {
public struct AMLAlertRequest: XDRCodable {
public var balanceID: BalanceID
public var amount: Uint64
public var creatorDetails: Longstring
Expand Down Expand Up @@ -55,6 +55,13 @@ public struct AMLAlertRequest: XDREncodable {
return xdr
}

public init(xdrData: inout Data) throws {
self.balanceID = try BalanceID(xdrData: &xdrData)
self.amount = try Uint64(xdrData: &xdrData)
self.creatorDetails = try Longstring(xdrData: &xdrData)
self.ext = try AMLAlertRequestExt(xdrData: &xdrData)
}

public enum AMLAlertRequestExt: XDRDiscriminatedUnion {
case emptyVersion()

Expand All @@ -76,5 +83,15 @@ public struct AMLAlertRequest: XDREncodable {
return xdr
}

public init(xdrData: inout Data) throws {
let discriminant = try Int32(xdrData: &xdrData)

switch discriminant {
case LedgerVersion.emptyVersion.rawValue: self = .emptyVersion()
default:
throw XDRErrors.unknownEnumCase
}
}

}
}
Loading

0 comments on commit 2b8b15d

Please sign in to comment.