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

Small fixes. #11

Merged
merged 7 commits into from
Nov 8, 2017
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
expose swift types
  • Loading branch information
zweigraf committed Nov 7, 2017
commit 48b5513d1c571524aacefbfc4d6f5e749d55adb3
10 changes: 10 additions & 0 deletions Resources/Extensions/StringExtension.swift
Original file line number Diff line number Diff line change
@@ -84,3 +84,13 @@ extension String {
return forBytes * 2
}
}

// MARK: - Hex Helper
extension String {
/// Returns a new string, removing a '0x' prefix if present.
var withoutHexPrefix: String {
return hasPrefix("0x")
? String(dropFirst(2))
: self
}
}
4 changes: 1 addition & 3 deletions Resources/Types/Address.swift
Original file line number Diff line number Diff line change
@@ -15,9 +15,7 @@ public extension Solidity {
private let bigInt: BigUInt

init(_ address: Swift.String) throws {
let hex = address.hasPrefix("0x")
? Swift.String(address[address.index(address.startIndex, offsetBy: 2)...])
: address
let hex = address.withoutHexPrefix
guard let bigInt = BigUInt(hex, radix: 16) else {
throw BivrostError.Address.invalidAddress(hex)
}
2 changes: 1 addition & 1 deletion Resources/Types/ArrayX.swift
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
extension _DoNotUse {
// swiftlint:disable:next type_name
class _ArrayX<T: SolidityCodable & Equatable> {
private let items: [T]
let items: [T]
class var length: UInt {
fatalError("Not meant to be called directly.")
}
6 changes: 6 additions & 0 deletions Resources/Types/Bool.swift
Original file line number Diff line number Diff line change
@@ -10,14 +10,20 @@ import BigInt

public extension Solidity {
struct Bool {
private let value: Swift.Bool
private let wrapper: UInt8

init(_ value: Swift.Bool) {
self.value = value
guard let wrapper = try? UInt8(BigUInt(value ? 1 : 0)) else {
fatalError("Solidity.Bool could not be created with value of \(value). This should not happen.")
}
self.wrapper = wrapper
}

func unwrap() -> Swift.Bool {
return value
}
}
}

4 changes: 4 additions & 0 deletions Resources/Types/Bytes.swift
Original file line number Diff line number Diff line change
@@ -21,6 +21,10 @@ public extension Solidity {
self.value = value
self.length = length
}

func unwrap() -> Data {
return value
}
}
}

4 changes: 4 additions & 0 deletions Resources/Types/BytesX.swift
Original file line number Diff line number Diff line change
@@ -24,6 +24,10 @@ public extension _DoNotUse {
}
self.value = value
}

func unwrap() -> Data {
return value
}
}
}

4 changes: 4 additions & 0 deletions Resources/Types/IntX.swift
Original file line number Diff line number Diff line change
@@ -26,6 +26,10 @@ public extension _DoNotUse {
}
value = int
}

func unwrap() -> BigInt {
return value
}
}
}

7 changes: 7 additions & 0 deletions Resources/Types/String.swift
Original file line number Diff line number Diff line change
@@ -9,14 +9,21 @@
public extension Solidity {
struct String {
let wrapper: Solidity.Bytes
let value: Swift.String

init?(_ value: Swift.String) {
self.value = value

guard let data = value.data(using: .utf8),
let bytes = Solidity.Bytes(data) else {
return nil
}
self.wrapper = bytes
}

func unwrap() -> Swift.String {
return value
}
}
}

4 changes: 4 additions & 0 deletions Resources/Types/UIntX.swift
Original file line number Diff line number Diff line change
@@ -26,6 +26,10 @@ public extension _DoNotUse {
}
value = uint
}

func unwrap() -> BigUInt {
return value
}
}
}