Skip to content

Commit

Permalink
Add SwiftLint & Spring Cleaning (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
zweigraf authored Oct 24, 2017
1 parent 6b4ae07 commit 357dec2
Show file tree
Hide file tree
Showing 42 changed files with 462 additions and 777 deletions.
42 changes: 42 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
included:
- Source
- Tests
- Resources

excluded:
# basically only excluded because of file_length requirement
- "Resources/Generated Types"

disabled_rules:
- file_header

opt_in_rules: # some rules are only opt-in
- attributes
- closure_spacing
- contains_over_first_not_nil
- empty_count
- explicit_init
- extension_access_modifier
- fatal_error_message
- file_header
- force_unwrapping
- implicit_return
- implicitly_unwrapped_optional
- literal_expression_end_indentation
- multiline_arguments
- multiline_parameters
- number_separator
- object_literal
- operator_usage_whitespace
- overridden_super_call
- prohibited_super_call
- redundant_nil_coalescing
- sorted_imports
- trailing_closure
- unneeded_parentheses_in_closure_argument
- vertical_parameter_alignment_on_call

# Make certain rules give out an error
force_cast: error
force_try: error
force_unwrapping: error
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
osx_image: xcode9
language: objective-c
before_install:
- brew tap yonaskolb/mint https://github.com/yonaskolb/mint.git
- brew install mint
script:
- mint run realm/swiftlint@0.23.1
- swift build
- swift test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Bivrost
# Bivrost [![Build Status](https://travis-ci.org/gnosis/bivrost-swift.svg?branch=master)](https://travis-ci.org/gnosis/bivrost-swift)

🔥 🌈 Bridge between Solidity Contracts and Swift

Expand Down
16 changes: 8 additions & 8 deletions Resources/BivrostError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//
//

import Foundation
import BigInt
import Foundation

enum BivrostError: Error {
enum Decoder: Error {
Expand All @@ -20,7 +20,7 @@ enum BivrostError: Error {
case invalidBytesLength(hex: String)
case invalidStringEncoding(data: Data)
case invalidArrayLength(hex: String)

// Creating Objects
case couldNotCreateString(source: BaseDecoder.PartitionData)
case couldNotCreateBytes(source: BaseDecoder.PartitionData)
Expand All @@ -30,28 +30,28 @@ enum BivrostError: Error {
case couldNotCreateAddress(source: BaseDecoder.PartitionData)
case couldNotCreateVariableArray(source: BaseDecoder.PartitionData)
}

enum ArrayX: Error {
case itemCountMismatch(expected: UInt, actual: UInt)
}

enum BytesX: Error {
case byteCountMismatch(max: UInt, actual: UInt)
}

enum Address: Error {
case invalidAddress(String)
case invalidBigUInt(BigUInt)
}

enum Function: Error {
case invalidFunctionSelector(String)
}

enum UIntX: Error {
case bitWidthMismatch(max: UInt, actual: UInt)
}

enum IntX: Error {
case bitWidthMismatch(max: UInt, actual: UInt)
}
Expand Down
52 changes: 28 additions & 24 deletions Resources/Coding/BaseDecoder.swift
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
//
// BaseDecoder.swift
// BivrostKit
// BivrostHelper
//
// Created by Luis Reisewitz on 10.10.17.
// Copyright © 2017 Gnosis. All rights reserved.
//
import Foundation

import BigInt
import Foundation

struct BaseDecoder {
fileprivate static func partitionData(inHex string: SolidityCodable.EncodeFormat) -> [SolidityCodable.EncodeFormat] {
return string.splitSolidityLines()
fileprivate static func partitionData(
inHex string: SolidityCodable.EncodeFormat) -> [SolidityCodable.EncodeFormat] {
return string.splitSolidityLines()
}

static func partition(_ data: String) -> BaseDecoder.PartitionData {
return BaseDecoder.PartitionData(data: data)
}

static func decodeUInt(data: SolidityCodable.EncodeFormat) throws -> BigUInt {
guard let bigUInt = BigUInt(data, radix: 16) else {
throw BivrostError.Decoder.invalidUInt(hex: data)
}
return bigUInt
}

static func decodeBool(data: SolidityCodable.EncodeFormat) throws -> Bool {
guard let bigUInt = BigUInt(data, radix: 16) else {
throw BivrostError.Decoder.invalidUInt(hex: data)
Expand All @@ -37,16 +39,16 @@ struct BaseDecoder {
throw BivrostError.Decoder.invalidBool(hex: data)
}
}

static func decodeInt(data: SolidityCodable.EncodeFormat) throws -> BigInt {
guard let bigInt = BigInt(twosComplementHex: data) else {
throw BivrostError.Decoder.invalidInt(hex: data)
}
return bigInt
}

static func decodeBytesX(data: SolidityCodable.EncodeFormat, length: UInt) throws -> Data {
let hexStringSize = String._hexStringSize(forBytes: length)
let hexStringSize = String.hexStringSize(forBytes: length)
let endIndex = data.index(data.startIndex, offsetBy: Int(hexStringSize))
let hexPartition = String(data[data.startIndex..<endIndex])
guard let byteData = Data(fromHexEncodedString: hexPartition),
Expand All @@ -55,31 +57,32 @@ struct BaseDecoder {
}
return byteData
}

static func decodeBytes(source: PartitionData) throws -> Data {
let sizePart = source.consume()
guard let size = Int(sizePart, radix: 16) else {
throw BivrostError.Decoder.invalidBytesLength(hex: sizePart)
}

var byteHolder = Data()
while byteHolder.count < size {
if let data = Data(fromHexEncodedString: source.consume()){
if let data = Data(fromHexEncodedString: source.consume()) {
byteHolder.append(data)
}
}
return byteHolder.prefix(upTo: size)
}

static func decodeString(source: PartitionData) throws -> String {
let data = try decodeBytes(source: source)
guard let string = String(data: data, encoding: .utf8) else {
throw BivrostError.Decoder.invalidStringEncoding(data: data)
}
return string
}

static func decodeArray<T>(data: SolidityCodable.EncodeFormat, decoder: (SolidityCodable.EncodeFormat) throws -> T) throws -> [T] {

static func decodeArray<T>(data: SolidityCodable.EncodeFormat,
decoder: (SolidityCodable.EncodeFormat) throws -> T) throws -> [T] {
let lines = partitionData(inHex: data)
let sizePart = lines[0]
guard let size = Int(sizePart, radix: 16),
Expand All @@ -91,8 +94,10 @@ struct BaseDecoder {
}
return try (1..<lines.count).map { try decoder(lines[$0]) }
}

static func decodeArray<T: SolidityCodable>(source: PartitionData, capacity: UInt, decoder: (PartitionData) throws -> T) throws -> [T] {

static func decodeArray<T: SolidityCodable>(source: PartitionData,
capacity: UInt,
decoder: (PartitionData) throws -> T) rethrows -> [T] {
guard capacity != 0 else {
return []
}
Expand All @@ -113,23 +118,22 @@ extension BaseDecoder {
init(_ lines: [SolidityCodable.EncodeFormat]) {
self.lines = lines
}

convenience init(data: String) {
let lines = BaseDecoder.partitionData(inHex: data)
self.init(lines)
}

var index: Int = 0

func consume() -> String {
let returnValue = lines[index]
index = index + 1
index += 1
return returnValue
}

func reset() {
index = 0
}
}
}

28 changes: 14 additions & 14 deletions Resources/Coding/BaseEncoder.swift
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
//
// BaseEncoder.swift
// BivrostKit
// BivrostHelper
//
// Created by Luis Reisewitz on 10.10.17.
// Copyright © 2017 Gnosis. All rights reserved.
//

import Foundation
import BigInt
import Foundation

struct BaseEncoder {
private static let solidityLocationSizeInBytes = 32

static func encode(_ arguments: [SolidityCodable]) -> SolidityCodable.EncodeFormat {
var parts = [(data: SolidityCodable.EncodeFormat, dynamic: Bool)]()
var sizeOfStaticBlockInBytes = 0

arguments.forEach {
let encoded = $0.encode()
if type(of: $0).isDynamic {
parts.append((data: encoded, dynamic: true))
// Add length of location entry to static block
sizeOfStaticBlockInBytes = sizeOfStaticBlockInBytes + solidityLocationSizeInBytes
sizeOfStaticBlockInBytes += solidityLocationSizeInBytes
} else {
parts.append((data: encoded, dynamic: false))
// Add byte size (hexString / 2)
sizeOfStaticBlockInBytes = sizeOfStaticBlockInBytes + encoded._hexStringByteSize
sizeOfStaticBlockInBytes += encoded.hexStringByteSize
}
}
var staticPart = ""
var dynamicPart = ""
parts.forEach { pair in
if pair.dynamic {
let location = sizeOfStaticBlockInBytes + dynamicPart._hexStringByteSize
let location = sizeOfStaticBlockInBytes + dynamicPart.hexStringByteSize
guard let locationUint = try? Solidity.UInt256(BigUInt(location)) else {
fatalError("BaseEncoder calculated invalid location for dynamic part. This should not happen.")
}
staticPart = staticPart + locationUint.encode()
dynamicPart = dynamicPart + pair.data
staticPart += locationUint.encode()
dynamicPart += pair.data
} else {
staticPart = staticPart + pair.data
staticPart += pair.data
}
}

return staticPart + dynamicPart
}

static func encode(arguments: SolidityCodable...) -> SolidityCodable.EncodeFormat {
return encode(arguments)
}

static func encode(arguments: Void) -> SolidityCodable.EncodeFormat {
return ""
}

static func encodeUnPadded(uint: BigUInt, bitWidth: UInt) -> SolidityCodable.EncodeFormat {
guard uint.bitWidth <= bitWidth else {
fatalError("\(#function) called with UInt \(uint) that is too big for bit width \(bitWidth).")
Expand Down
6 changes: 3 additions & 3 deletions Resources/Extensions/BigIntExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// Copyright © 2017 Gnosis. All rights reserved.
//

import Foundation
import BigInt
import Foundation

extension BigInt {
/// Returns the binary data for the two's complement.
Expand All @@ -19,7 +19,7 @@ extension BigInt {
}
return ((~magnitude) + 1).serialize()
}

init?(twosComplementHex: String) {
let lowercase = twosComplementHex.lowercased()
// Check if we have a negative number, else just return
Expand All @@ -34,7 +34,7 @@ extension BigInt {
self.init(lowercase, radix: 16)
return
}

guard let uint = BigUInt(lowercase, radix: 16) else {
return nil
}
Expand Down
Loading

0 comments on commit 357dec2

Please sign in to comment.