Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Fixes for external node work and more flexibility for BIP32 childs derivation #44

Merged
merged 3 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion web3swift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "web3swift"
s.version = "0.4.0"
s.version = "0.4.1"
s.summary = "Web3 implementation in vanilla Swift for iOS ans macOS"

s.description = <<-DESC
Expand Down
58 changes: 44 additions & 14 deletions web3swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@
81FB21F22078142D007F9A83 /* Web3+Deprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81FB21F02078142D007F9A83 /* Web3+Deprecated.swift */; };
81FB21F4207814F7007F9A83 /* Web3+EventOperations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81FB21F3207814F7007F9A83 /* Web3+EventOperations.swift */; };
81FB21F5207814F7007F9A83 /* Web3+EventOperations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81FB21F3207814F7007F9A83 /* Web3+EventOperations.swift */; };
81FB21FE207BB297007F9A83 /* EIP67Code.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81FB21F8207BA78B007F9A83 /* EIP67Code.swift */; };
81FB21FF207BB297007F9A83 /* EIP67Code.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81FB21F8207BA78B007F9A83 /* EIP67Code.swift */; };
81FB2201207BC58C007F9A83 /* CoreImage.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 81FB2200207BC58B007F9A83 /* CoreImage.framework */; };
81FB2203207BC6DB007F9A83 /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 81FB2202207BC6DB007F9A83 /* ImageIO.framework */; };
A4733E6E22E6216A90CCEA33 /* Pods_web3swift_macOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FC4A49B538D672F4DA112A11 /* Pods_web3swift_macOS.framework */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -269,6 +273,9 @@
81FB21ED2077D98A007F9A83 /* SynchronizedQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SynchronizedQueue.swift; sourceTree = "<group>"; };
81FB21F02078142D007F9A83 /* Web3+Deprecated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Web3+Deprecated.swift"; sourceTree = "<group>"; };
81FB21F3207814F7007F9A83 /* Web3+EventOperations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Web3+EventOperations.swift"; sourceTree = "<group>"; };
81FB21F8207BA78B007F9A83 /* EIP67Code.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EIP67Code.swift; sourceTree = "<group>"; };
81FB2200207BC58B007F9A83 /* CoreImage.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreImage.framework; path = System/Library/Frameworks/CoreImage.framework; sourceTree = SDKROOT; };
81FB2202207BC6DB007F9A83 /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = System/Library/Frameworks/ImageIO.framework; sourceTree = SDKROOT; };
A9ADDE40292A17C21B8D5516 /* Pods-web3swift-iOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-web3swift-iOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-web3swift-iOS/Pods-web3swift-iOS.release.xcconfig"; sourceTree = "<group>"; };
B48CA58D134401D3C4E8CCC5 /* Pods_Web3Swift_osx.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Web3Swift_osx.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B5AFAFC5440E52BE57C7BA13 /* Pods_web3swiftTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_web3swiftTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand All @@ -284,6 +291,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
81FB2203207BC6DB007F9A83 /* ImageIO.framework in Frameworks */,
81FB2201207BC58C007F9A83 /* CoreImage.framework in Frameworks */,
81C0FD082044AB2400D82FAF /* Pods_web3swift_iOS.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -352,8 +361,9 @@
8113D2B51FD7E1590074282C /* KeystoreManager */,
8113D2C11FD7E1590074282C /* Transaction */,
8113D2AE1FD7E1590074282C /* Utils */,
1CD91AFF1FD76910007BFB45 /* web3swift.h */,
81FB21F6207BA6DC007F9A83 /* Convenience */,
1CD91B001FD76910007BFB45 /* Info.plist */,
1CD91AFF1FD76910007BFB45 /* web3swift.h */,
);
path = web3swift;
sourceTree = "<group>";
Expand All @@ -372,6 +382,8 @@
78D101CC419F27D142F6E9AE /* Frameworks */ = {
isa = PBXGroup;
children = (
81FB2202207BC6DB007F9A83 /* ImageIO.framework */,
81FB2200207BC58B007F9A83 /* CoreImage.framework */,
81C0FD0B2044AB4600D82FAF /* Pods_web3swift_iOS_Tests.framework */,
81C0FD092044AB2400D82FAF /* Pods_web3swift_iOS.framework */,
419481432036338A0065A83B /* Pods_web3swift_osx.framework */,
Expand Down Expand Up @@ -429,18 +441,7 @@
8113D2AF1FD7E1590074282C /* Classes */ = {
isa = PBXGroup;
children = (
81FB21ED2077D98A007F9A83 /* SynchronizedQueue.swift */,
81C5DA272072E18200424CD6 /* NativeTypesEncoding+Extensions.swift */,
8113D2B01FD7E1590074282C /* CryptoExtensions.swift */,
8113D2B21FD7E1590074282C /* LibSecp256k1Extension.swift */,
8113D2B31FD7E1590074282C /* DictionaryLiteralJSONSerializer.swift */,
8113DE7B1FD8514400CD8DF1 /* NSRegularExpressionExtension.swift */,
817EBB1E2006265400E02EAA /* Base58.swift */,
817EBB22200649E000E02EAA /* RIPEMD160+StackOveflow.swift */,
8123E1C8200CBAF800B6D3AB /* Data+Extension.swift */,
8123E1C6200CBAC200B6D3AB /* Dictionary+Extension.swift */,
8123E1CA200CBB2200B6D3AB /* Array+Extension.swift */,
8113D2FF1FD7F30F0074282C /* String+Extension.swift */,
81FB21F8207BA78B007F9A83 /* EIP67Code.swift */,
);
path = Classes;
sourceTree = "<group>";
Expand Down Expand Up @@ -606,6 +607,33 @@
path = "web3swift-macOS_Tests";
sourceTree = "<group>";
};
81FB21F6207BA6DC007F9A83 /* Convenience */ = {
isa = PBXGroup;
children = (
81FB21F7207BA6EB007F9A83 /* Classes */,
);
path = Convenience;
sourceTree = "<group>";
};
81FB21F7207BA6EB007F9A83 /* Classes */ = {
isa = PBXGroup;
children = (
81FB21ED2077D98A007F9A83 /* SynchronizedQueue.swift */,
81C5DA272072E18200424CD6 /* NativeTypesEncoding+Extensions.swift */,
8113D2B01FD7E1590074282C /* CryptoExtensions.swift */,
8113D2B21FD7E1590074282C /* LibSecp256k1Extension.swift */,
8113D2B31FD7E1590074282C /* DictionaryLiteralJSONSerializer.swift */,
8113DE7B1FD8514400CD8DF1 /* NSRegularExpressionExtension.swift */,
817EBB1E2006265400E02EAA /* Base58.swift */,
817EBB22200649E000E02EAA /* RIPEMD160+StackOveflow.swift */,
8123E1C8200CBAF800B6D3AB /* Data+Extension.swift */,
8123E1C6200CBAC200B6D3AB /* Dictionary+Extension.swift */,
8123E1CA200CBB2200B6D3AB /* Array+Extension.swift */,
8113D2FF1FD7F30F0074282C /* String+Extension.swift */,
);
path = Classes;
sourceTree = "<group>";
};
868F9682FE77A7BADF675F92 /* Pods */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1011,6 +1039,7 @@
buildActionMask = 2147483647;
files = (
81C146F71FF274B200AA943E /* Web3+Structures.swift in Sources */,
8104E2281FE82BDC00F952CB /* Web3+Utils.swift in Sources */,
818ABD5D1FE95FC9002657BB /* Web3+Contract.swift in Sources */,
8113D2CA1FD7E1590074282C /* KeystoreManager.swift in Sources */,
817EBB1F2006265400E02EAA /* Base58.swift in Sources */,
Expand All @@ -1034,6 +1063,7 @@
81C5DA11207254F600424CD6 /* ABIv2.swift in Sources */,
8103BBC62077954200499769 /* Web3+TransactionOperations.swift in Sources */,
817EBB102004FE2800E02EAA /* EthereumAddress.swift in Sources */,
81FB21FE207BB297007F9A83 /* EIP67Code.swift in Sources */,
81C5DA0E207254D000424CD6 /* ABIv2Elements.swift in Sources */,
8113D2CC1FD7E1590074282C /* ABIDecoder.swift in Sources */,
8125F0632047CE6900A0F2FE /* TypesEncoder.swift in Sources */,
Expand All @@ -1046,7 +1076,6 @@
81FB21F12078142D007F9A83 /* Web3+Deprecated.swift in Sources */,
810B0F9C1FEC520500CF0DA2 /* Web3+Methods.swift in Sources */,
8113DE7C1FD8514400CD8DF1 /* NSRegularExpressionExtension.swift in Sources */,
8104E2281FE82BDC00F952CB /* Web3+Utils.swift in Sources */,
81C5DA2E2074EBF500424CD6 /* ContractABIv2.swift in Sources */,
810B0F9E1FEC5B9C00CF0DA2 /* Web3+Eth.swift in Sources */,
818D810E1FDC1A2000663CE3 /* RLP.swift in Sources */,
Expand Down Expand Up @@ -1116,6 +1145,7 @@
81C5DA12207254F600424CD6 /* ABIv2.swift in Sources */,
8103BBC72077954200499769 /* Web3+TransactionOperations.swift in Sources */,
4194812D203630530065A83B /* EthereumAddress.swift in Sources */,
81FB21FF207BB297007F9A83 /* EIP67Code.swift in Sources */,
81C5DA0F207254D000424CD6 /* ABIv2Elements.swift in Sources */,
8125F0642047CE6900A0F2FE /* TypesEncoder.swift in Sources */,
818D16D0204D42940084D2A4 /* Web3+EventParser.swift in Sources */,
Expand Down
6 changes: 5 additions & 1 deletion web3swift/Concurrency/Classes/Web3+EthOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ final class SendTransactionOperation: Web3Operation {
sendRawTxOp.next = completion
return
}
guard let request = EthereumTransaction.createRawTransaction(transaction: transaction) else {return processError(Web3Error.transactionSerializationError)}
guard let request = EthereumTransaction.createRequest(method: JSONRPCmethod.sendTransaction, transaction: transaction, onBlock: nil, options: mergedOptions) else
{
return processError(Web3Error.transactionSerializationError)
}
// guard let request = EthereumTransaction.createRawTransaction(transaction: transaction) else {return processError(Web3Error.transactionSerializationError)}
let dataOp = DataFetchOperation(self.web3, queue: self.expectedQueue)
dataOp.inputData = request as AnyObject
let parsingOp = DataConversionOperation(self.web3, queue: self.expectedQueue)
Expand Down
18 changes: 18 additions & 0 deletions web3swift/Convenience/Classes/Array+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Array+Extension.swift
// web3swift
//
// Created by Alexander Vlasov on 15.01.2018.
// Copyright © 2018 Bankex Foundation. All rights reserved.
//

import Foundation

extension Array {
public func split(intoChunksOf chunkSize: Int) -> [[Element]] {
return stride(from: 0, to: self.count, by: chunkSize).map {
let endIndex = ($0.advanced(by: chunkSize) > self.count) ? self.count - $0 : chunkSize
return Array(self[$0..<$0.advanced(by: endIndex)])
}
}
}
170 changes: 170 additions & 0 deletions web3swift/Convenience/Classes/Base58.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
//
// Base58.swift
// web3swift
//
// Created by Alexander Vlasov on 10.01.2018.
// Copyright © 2018 Bankex Foundation. All rights reserved.
//

import Foundation

struct Base58 {
static let base58Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

// Encode
static func base58FromBytes(_ bytes: [UInt8]) -> String {
var bytes = bytes
var zerosCount = 0
var length = 0

for b in bytes {
if b != 0 { break }
zerosCount += 1
}

bytes.removeFirst(zerosCount)

let size = bytes.count * 138 / 100 + 1

var base58: [UInt8] = Array(repeating: 0, count: size)
for b in bytes {
var carry = Int(b)
var i = 0

for j in 0...base58.count-1 where carry != 0 || i < length {
carry += 256 * Int(base58[base58.count - j - 1])
base58[base58.count - j - 1] = UInt8(carry % 58)
carry /= 58
i += 1
}

assert(carry == 0)

length = i
}

// skip leading zeros
var zerosToRemove = 0
var str = ""
for b in base58 {
if b != 0 { break }
zerosToRemove += 1
}
base58.removeFirst(zerosToRemove)

while 0 < zerosCount {
str = "\(str)1"
zerosCount -= 1
}

for b in base58 {
str = "\(str)\(base58Alphabet[String.Index(encodedOffset: Int(b))])"
}

return str
}

// Decode
static func bytesFromBase58(_ base58: String) -> [UInt8] {
// remove leading and trailing whitespaces
let string = base58.trimmingCharacters(in: CharacterSet.whitespaces)

guard !string.isEmpty else { return [] }

var zerosCount = 0
var length = 0
for c in string {
if c != "1" { break }
zerosCount += 1
}

let size = string.lengthOfBytes(using: String.Encoding.utf8) * 733 / 1000 + 1 - zerosCount
var base58: [UInt8] = Array(repeating: 0, count: size)
for c in string where c != " " {
// search for base58 character
guard let base58Index = base58Alphabet.index(of: c) else { return [] }

var carry = base58Index.encodedOffset
var i = 0
for j in 0...base58.count where carry != 0 || i < length {
carry += 58 * Int(base58[base58.count - j - 1])
base58[base58.count - j - 1] = UInt8(carry % 256)
carry /= 256
i += 1
}

assert(carry == 0)
length = i
}

// skip leading zeros
var zerosToRemove = 0

for b in base58 {
if b != 0 { break }
zerosToRemove += 1
}
base58.removeFirst(zerosToRemove)

var result: [UInt8] = Array(repeating: 0, count: zerosCount)
for b in base58 {
result.append(b)
}
return result
}
}



extension Array where Element == UInt8 {
public var base58EncodedString: String {
guard !self.isEmpty else { return "" }
return Base58.base58FromBytes(self)
}

public var base58CheckEncodedString: String {
var bytes = self
let checksum = [UInt8](bytes.sha256().sha256()[0..<4])

bytes.append(contentsOf: checksum)

return Base58.base58FromBytes(bytes)
}
}

extension String {
public var base58EncodedString: String {
return [UInt8](utf8).base58EncodedString
}

public var base58DecodedData: Data? {
let bytes = Base58.bytesFromBase58(self)
return Data(bytes)
}

public var base58CheckDecodedData: Data? {
guard let bytes = self.base58CheckDecodedBytes else { return nil }
return Data(bytes)
}

public var base58CheckDecodedBytes: [UInt8]? {
var bytes = Base58.bytesFromBase58(self)
guard 4 <= bytes.count else { return nil }

let checksum = [UInt8](bytes[bytes.count-4..<bytes.count])
bytes = [UInt8](bytes[0..<bytes.count-4])

let calculatedChecksum = [UInt8](bytes.sha256().sha256()[0...3])
if checksum != calculatedChecksum { return nil }

return bytes
}

// public var littleEndianHexToUInt: UInt {
// let data = Data.fromHex(self)!
// let revensed =
// return UInt(sel)
// return UInt(self.dataWithHexString().bytes.reversed().fullHexString,radix: 16)!
// }

}
49 changes: 49 additions & 0 deletions web3swift/Convenience/Classes/CryptoExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// CryptoExtensions.swift
// web3swift
//
// Created by Alexander Vlasov on 04.12.2017.
// Copyright © 2017 Alexander Vlasov. All rights reserved.
//

import Foundation
import libsodium

func toByteArray<T>(_ value: T) -> [UInt8] {
var value = value
return withUnsafeBytes(of: &value) { Array($0) }
}

func fromByteArray<T>(_ value: [UInt8], _: T.Type) -> T {
return value.withUnsafeBytes {
$0.baseAddress!.load(as: T.self)
}
}

public func scrypt (password: String, salt: Data, length: Int, N: Int, R: Int, P: Int) -> Data? {
let BytesMin = Int(crypto_generichash_bytes_min())
let BytesMax = Int(crypto_generichash_bytes_max())
if length < BytesMin || length > BytesMax {
return nil
}

var output = Data(count: length)
guard let passwordData = password.data(using: .utf8) else {return nil}
let passwordLen = passwordData.count
let saltLen = salt.count
let result = output.withUnsafeMutableBytes { (outputPtr:UnsafeMutablePointer<UInt8>) -> Int32 in
salt.withUnsafeBytes { (saltPointer:UnsafePointer<UInt8>) -> Int32 in
passwordData.withUnsafeBytes{ (passwordPointer:UnsafePointer<UInt8>) -> Int32 in
let res = crypto_pwhash_scryptsalsa208sha256_ll(passwordPointer, passwordLen,
saltPointer, saltLen,
UInt64(N), UInt32(R), UInt32(P),
outputPtr, length)
return res
}
}
}
if result != 0 {
return nil
}
return output
}
Loading