Merge pull request #11 from James-Sangalli/advancedOrderHandler

Advanced order handler
pull/16/head
James Sangalli 7 years ago committed by GitHub
commit 51fb02a9a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      Podfile
  2. 14
      Podfile.lock
  3. 56
      Trust.xcodeproj/project.pbxproj
  4. 8
      Trust/Info.plist
  5. 82
      Trust/Market/OrdersRequest.swift
  6. 72
      Trust/Market/SignOrders.swift
  7. 58
      TrustTests/Market/OrderRequestTest.swift
  8. 27
      TrustTests/Market/OrderSigningTests.swift

@ -20,14 +20,14 @@ target 'Trust' do
pod 'BonMot', '~> 5.0'
pod 'Lokalise'
pod 'Moya', '~> 10.0.1'
pod 'Just', :git => 'https://github.com/JustHTTP/Just.git', :branch => 'swift-4'
pod 'JavaScriptKit'
pod 'JavaScriptKit'
pod 'CryptoSwift', :git=>'https://github.com/krzyzanowskim/CryptoSwift', :branch=>'master'
pod 'Fabric'
pod 'Crashlytics'
pod 'Kingfisher', '~> 4.0'
pod 'TrustKeystore', :git=>'https://github.com/TrustWallet/trust-keystore', :branch=>'master'
pod 'Branch'
pod 'SwiftyJSON'
# pod 'web3swift', :git=>'https://github.com/BANKEX/web3swift', :branch=>'master'
pod 'SAMKeychain'

@ -17,7 +17,6 @@ PODS:
- Result (~> 3.1)
- JSONRPCKit (2.0.0):
- Result (~> 3.0)
- Just (0.6.0)
- KeychainSwift (10.0.0)
- Kingfisher (4.6.1)
- Lokalise (0.8.1)
@ -43,6 +42,7 @@ PODS:
- SipHash (1.2.0)
- StatefulViewController (3.0)
- SwiftLint (0.24.1)
- SwiftyJSON (4.0.0)
- TrezorCrypto (0.0.4)
- TrustKeystore (0.1.3):
- CryptoSwift
@ -60,7 +60,6 @@ DEPENDENCIES:
- Fabric
- JavaScriptKit
- JSONRPCKit (from `https://github.com/bricklife/JSONRPCKit.git`)
- Just (from `https://github.com/JustHTTP/Just.git`, branch `swift-4`)
- KeychainSwift
- Kingfisher (~> 4.0)
- Lokalise
@ -73,6 +72,7 @@ DEPENDENCIES:
- SeedStackViewController
- StatefulViewController
- SwiftLint
- SwiftyJSON
- TrustKeystore (from `https://github.com/TrustWallet/trust-keystore`, branch `master`)
EXTERNAL SOURCES:
@ -81,9 +81,6 @@ EXTERNAL SOURCES:
:git: https://github.com/krzyzanowskim/CryptoSwift
JSONRPCKit:
:git: https://github.com/bricklife/JSONRPCKit.git
Just:
:branch: swift-4
:git: https://github.com/JustHTTP/Just.git
QRCodeReaderViewController:
:branch: master
:git: https://github.com/yannickl/QRCodeReaderViewController.git
@ -98,9 +95,6 @@ CHECKOUT OPTIONS:
JSONRPCKit:
:commit: 50d19a4f7ec593ac5e07cffa1e11c17f1fbe347d
:git: https://github.com/bricklife/JSONRPCKit.git
Just:
:commit: 93fc04992243fb459af37963797ef5ac5224b242
:git: https://github.com/JustHTTP/Just.git
QRCodeReaderViewController:
:commit: 80bd79cbeede842949b229f81bc6328f53701c5a
:git: https://github.com/yannickl/QRCodeReaderViewController.git
@ -120,7 +114,6 @@ SPEC CHECKSUMS:
Fabric: 9cd6a848efcf1b8b07497e0b6a2e7d336353ba15
JavaScriptKit: 9ff565209e6efe21bcb9c6d6ca3e863a67a8ecf7
JSONRPCKit: 22132c575ba2dc6f2f4ae72fda4943a63efca686
Just: 72e8a6e01ec5adba1aa0f7996ff4459aa6a3d710
KeychainSwift: f9f7910449a0c0fd2cabc889121530dd2c477c33
Kingfisher: 1f9157d9c02b380cbd0b7cc890161195164eb634
Lokalise: d81f4ccce8cd2c1589bf73bb39fab7781d4f7cbc
@ -138,9 +131,10 @@ SPEC CHECKSUMS:
SipHash: c6e9e43e9c531b5bc6602545130c26194a6d31ce
StatefulViewController: 4803bf900d44de26074344998e10e041113b5931
SwiftLint: 2e4b89feed5909c42c3735bbd6745f4345c4b772
SwiftyJSON: 070dabdcb1beb81b247c65ffa3a79dbbfb3b48aa
TrezorCrypto: 7f87a2296cf13872b21565c29fd9f7882dd00803
TrustKeystore: 02614fb6b5c48fc65a0c485df7e22a44aec0c0d5
PODFILE CHECKSUM: 2c518afd93340e5087c64d66d63ee842b6a7a825
PODFILE CHECKSUM: a1da5b47a2b9279042ea399b114394f480c2e69b
COCOAPODS: 1.3.1

@ -318,6 +318,7 @@
76F1D137B10D8309E513BBDD /* OrderSigningTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76F1DE8ADA3176D0277EDF20 /* OrderSigningTests.swift */; };
76F1D5AF727A83205BBCF0EC /* SignOrders.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76F1DADFD07E2941897FD2E1 /* SignOrders.swift */; };
76F1D8E2B5BCCD68EF2AF362 /* OrdersRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1DC375C203AEAE100C9756D /* OrdersRequest.swift */; };
76F1DE32A8FDB44BFEE6B8C3 /* SignOrders.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76F1DADFD07E2941897FD2E1 /* SignOrders.swift */; };
771A8471202F067D00528D28 /* NetworksViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 771A8470202F067D00528D28 /* NetworksViewController.swift */; };
771A847320322F2500528D28 /* PreferencesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 771A847220322F2500528D28 /* PreferencesViewController.swift */; };
771A847520322FD700528D28 /* PreferencesViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 771A847420322FD700528D28 /* PreferencesViewModel.swift */; };
@ -2245,7 +2246,7 @@
2912CCF41F6A830700C6CBE3 = {
CreatedOnToolsVersion = 9.0;
LastSwiftMigration = 0910;
ProvisioningStyle = Manual;
ProvisioningStyle = Automatic;
SystemCapabilities = {
com.apple.DataProtection = {
enabled = 1;
@ -2268,7 +2269,7 @@
};
2912CD151F6A830700C6CBE3 = {
CreatedOnToolsVersion = 9.0;
ProvisioningStyle = Automatic;
ProvisioningStyle = Manual;
TestTargetID = 2912CCF41F6A830700C6CBE3;
};
};
@ -2505,7 +2506,6 @@
"${BUILT_PRODUCTS_DIR}/Eureka/Eureka.framework",
"${BUILT_PRODUCTS_DIR}/JSONRPCKit/JSONRPCKit.framework",
"${BUILT_PRODUCTS_DIR}/JavaScriptKit/JavaScriptKit.framework",
"${BUILT_PRODUCTS_DIR}/Just/Just.framework",
"${BUILT_PRODUCTS_DIR}/KeychainSwift/KeychainSwift.framework",
"${BUILT_PRODUCTS_DIR}/Kingfisher/Kingfisher.framework",
"${BUILT_PRODUCTS_DIR}/MBProgressHUD/MBProgressHUD.framework",
@ -2519,6 +2519,7 @@
"${BUILT_PRODUCTS_DIR}/SeedStackViewController/StackViewController.framework",
"${BUILT_PRODUCTS_DIR}/SipHash/SipHash.framework",
"${BUILT_PRODUCTS_DIR}/StatefulViewController/StatefulViewController.framework",
"${BUILT_PRODUCTS_DIR}/SwiftyJSON/SwiftyJSON.framework",
"${BUILT_PRODUCTS_DIR}/TrezorCrypto/TrezorCrypto.framework",
"${BUILT_PRODUCTS_DIR}/TrustKeystore/TrustKeystore.framework",
"${BUILT_PRODUCTS_DIR}/secp256k1_ios/secp256k1_ios.framework",
@ -2534,7 +2535,6 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Eureka.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/JSONRPCKit.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/JavaScriptKit.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Just.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/KeychainSwift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Kingfisher.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MBProgressHUD.framework",
@ -2548,6 +2548,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/StackViewController.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SipHash.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/StatefulViewController.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftyJSON.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/TrezorCrypto.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/TrustKeystore.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/secp256k1_ios.framework",
@ -2849,7 +2850,6 @@
291ED08D1F6F5F0A00E7E93A /* KeyStoreError.swift in Sources */,
29F1C83E1FEB5C91003780D8 /* EditTokensViewController.swift in Sources */,
296AF9A31F733AB30058AF78 /* WalletCoordinator.swift in Sources */,
76F1DA1A819D5543FF698AF1,
442FCBBFCC5926B4D416E6D3 /* GetNameCoordinator.swift in Sources */,
442FCB9850FC6F2D28978199 /* GETERC20Symbol.swift in Sources */,
442FC5F70AF003F331F7C841 /* GetSymbolCoordinator.swift in Sources */,
@ -2888,7 +2888,6 @@
299573A21FA1F369006F17FD /* QRURLParserTests.swift in Sources */,
61FC5ED11FCFBDEB00CCB12A /* EtherNumberFormatterTests.swift in Sources */,
29E14FD91F7F4D4E00185568 /* FakeTransactionsStorage.swift in Sources */,
AA893ED6203C3E5400CDCED1,
29BDF1961FEE43F40023A45F /* UnconfirmedTransaction.swift in Sources */,
29E14FD71F7F490000185568 /* Transaction.swift in Sources */,
296106CC1F776FD00006164B /* WalletCoordinatorTests.swift in Sources */,
@ -2930,6 +2929,7 @@
2981F4731F8303E600CA6590 /* TransactionCoordinatorTests.swift in Sources */,
76F1D137B10D8309E513BBDD /* OrderSigningTests.swift in Sources */,
76F1D8E2B5BCCD68EF2AF362 /* OrdersRequest.swift in Sources */,
76F1DE32A8FDB44BFEE6B8C3 /* SignOrders.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -3101,18 +3101,18 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = Trust/Trust.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution";
CODE_SIGN_STYLE = Manual;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 157;
DEVELOPMENT_TEAM = 9873B38DWV;
DEVELOPMENT_TEAM = LRAW5PL536;
ENABLE_BITCODE = YES;
INFOPLIST_FILE = Trust/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
OTHER_SWIFT_FLAGS = "$(inherited) \"-D\" \"COCOAPODS\" -D DEBUG";
PRODUCT_BUNDLE_IDENTIFIER = com.sixdays.trust;
PRODUCT_BUNDLE_IDENTIFIER = com.stormbird.alphawallet;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "7edbff9c-3e3f-4ba3-b84f-19ac095b8e95";
PROVISIONING_PROFILE_SPECIFIER = "match AppStore com.sixdays.trust";
PROVISIONING_PROFILE = "";
PROVISIONING_PROFILE_SPECIFIER = "";
TARGETED_DEVICE_FAMILY = "1,2";
VALID_ARCHS = "arm64 armv7 armv7s";
VERSIONING_SYSTEM = "apple-generic";
@ -3125,17 +3125,17 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = Trust/Trust.entitlements;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution";
CODE_SIGN_STYLE = Manual;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 157;
DEVELOPMENT_TEAM = 9873B38DWV;
DEVELOPMENT_TEAM = LRAW5PL536;
ENABLE_BITCODE = YES;
INFOPLIST_FILE = Trust/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.sixdays.trust;
PRODUCT_BUNDLE_IDENTIFIER = com.stormbird.alphawallet;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "7edbff9c-3e3f-4ba3-b84f-19ac095b8e95";
PROVISIONING_PROFILE_SPECIFIER = "match AppStore com.sixdays.trust";
PROVISIONING_PROFILE = "";
PROVISIONING_PROFILE_SPECIFIER = "";
TARGETED_DEVICE_FAMILY = "1,2";
VALID_ARCHS = "arm64 armv7 armv7s";
VERSIONING_SYSTEM = "apple-generic";
@ -3150,7 +3150,7 @@
BUNDLE_LOADER = "$(TEST_HOST)";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = 9873B38DWV;
DEVELOPMENT_TEAM = LRAW5PL536;
INFOPLIST_FILE = TrustTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.sixdays.TrustTests;
@ -3167,8 +3167,9 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
BUNDLE_LOADER = "$(TEST_HOST)";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = LRAW5PL536;
INFOPLIST_FILE = TrustTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.sixdays.TrustTests;
@ -3184,12 +3185,16 @@
baseConfigurationReference = 477899BEAA4489DA423E8857 /* Pods-TrustUITests.debug.xcconfig */;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 9873B38DWV;
CODE_SIGN_IDENTITY = "iPhone Distribution: StormBird Pte LTD (LRAW5PL536)";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = LRAW5PL536;
INFOPLIST_FILE = TrustUITests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.sixdays.TrustUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "2869a1ac-b598-45b8-a7c4-7560774c8a2f";
PROVISIONING_PROFILE_SPECIFIER = new;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_TARGET_NAME = Trust;
};
@ -3200,12 +3205,15 @@
baseConfigurationReference = A3B747245A27436E621E7E41 /* Pods-TrustUITests.release.xcconfig */;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 9873B38DWV;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = LRAW5PL536;
INFOPLIST_FILE = TrustUITests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.sixdays.TrustUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "2869a1ac-b598-45b8-a7c4-7560774c8a2f";
PROVISIONING_PROFILE_SPECIFIER = new;
TARGETED_DEVICE_FAMILY = "1,2";
TEST_TARGET_NAME = Trust;
};

@ -4,6 +4,8 @@
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>AlphaWallet</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
@ -15,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.1.145</string>
<string>0.1</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
@ -99,8 +101,6 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>com.apple.security.get-task-allow</key>
<true/>
<key>branch_key</key>
<dict>
<key>live</key>
@ -108,5 +108,7 @@
<key>test</key>
<string>key_test_odFMiPaHXUB4zNECuK28kdboCqgkAF3K</string>
</dict>
<key>com.apple.security.get-task-allow</key>
<true/>
</dict>
</plist>

@ -1,9 +1,9 @@
//
// Created by James Sangalli on 15/2/18.
//
import Foundation
import Just
import Alamofire
import SwiftyJSON
//"orders": [
// {
@ -17,40 +17,64 @@ import Just
//]
public class OrdersRequest {
public let baseURL = "https://482kdh4npg.execute-api.ap-southeast-1.amazonaws.com/dev/"
public let contractAddress = "0x007bee82bdd9e866b2bd114780a47f2261c684e3" //this is wrong as it is the deployer address, will be corrected later
public func getOrders(callback: @escaping (_ result : Any) -> ()) {
Just.get(baseURL + "contract/" + contractAddress) {
r in
if r.ok
{
callback(r)
}
else
{
callback(r.error)
}
public func getOrders(callback: @escaping (_ result : Any) -> Void) {
Alamofire.request(baseURL + "/contract/" + contractAddress, method: .get).responseJSON {
response in
callback(response)
}
}
// public func giveOrderToServer(signedOrders : [SignedOrder], publicKeyHex : String,
// callback: @escaping (_ result: Any) -> ()) {
// Just.put(baseURL + "public-key/" , data : ["" : publicKeyHex]) {
// r in
// if r.ok
// {
// //success of placing orders
// callback(r)
// }
// else
// {
// callback(r.error)
// }
// }
// }
//only have to give first order to server then pad the signatures
public func putOrderToServer(signedOrders : [SignedOrder], publicKey: String,
callback: @escaping (_ result: Any) -> Void)
{
//TODO get encoding for count and start
let query : String = baseURL + "public-key/" + publicKey + "?start=" +
signedOrders[0].order.start.description + ";count=" + signedOrders[0].order.count.description
var data: [UInt8] = signedOrders[0].message
}
for i in 0...signedOrders.count - 1 {
for j in 0...64 {
data.append(signedOrders[i].signature.hexa2Bytes[j])
}
}
var hexData : String = bytesToHexa(data)
let parameters : Parameters = [
"data": hexData
]
let headers: HTTPHeaders = [
"Content-Type": "application/vnd.awallet-signed-orders-v0"
]
Alamofire.request(query, method: .put, parameters: parameters,
encoding: JSONEncoding.default, headers: headers).responseJSON {
response in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)") // response serialization result
if let json = response.result.value {
//print("JSON: \(json)") // serialized json response
let parsedJSON = JSON(parseJSON: json as! String)
callback(parsedJSON["orders"]["accepted"])
}
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)") // original server data as UTF8 string
}
}
}
public func bytesToHexa(_ bytes: [UInt8]) -> String {
return bytes.map{ String(format: "%02X", $0) }.joined()
}
}

@ -1,24 +1,22 @@
import BigInt
import TrustKeystore
//TODO not sure why errors keep coming without this
import Trust
public struct Order {
var price: [UInt8]
var ticketIndices: [UInt16]
var expiryBuffer: [UInt8]
var price: BigUInt
var indices: [UInt16]
var expiry: BigUInt
var contractAddress: String
//for mapping to server
var start : BigUInt
var count : Int
}
public struct SignedOrder {
var order : Order
var message : Data
var signature : String
}
extension String {
var hexa2Bytes: [UInt8] {
let hexa = Array(characters)
return stride(from: 0, to: count, by: 2).flatMap { UInt8(String(hexa[$0..<$0.advanced(by: 2)]), radix: 16) }
}
var order: Order
var message: [UInt8]
var signature: String
}
extension BinaryInteger {
@ -28,6 +26,13 @@ extension BinaryInteger {
}
}
extension String {
var hexa2Bytes: [UInt8] {
let hexa = Array(characters)
return stride(from: 0, to: count, by: 2).flatMap { UInt8(String(hexa[$0..<$0.advanced(by: 2)]), radix: 16) }
}
}
extension Data {
var array: [UInt8] { return Array(self) }
}
@ -37,25 +42,24 @@ public class SignOrders {
private let keyStore = try! EtherKeystore()
//takes a list of orders and returns a list of signature objects
func signOrders(orders : Array<Order>, account : Account) -> Array<SignedOrder> {
func signOrders(orders : [Order], account : Account) -> Array<SignedOrder> {
var signedOrders : Array<SignedOrder> = Array<SignedOrder>()
var signedOrders = [SignedOrder]()
for i in 0...orders.count - 1 {
let message : [UInt8] = encodeMessageForTrade(price: orders[i].price,
expiryBuffer: orders[i].expiryBuffer, tickets: orders[i].ticketIndices,
contractAddress : orders[i].contractAddress)
let messageData = Data(bytes: message)
let message : [UInt8] =
encodeMessageForTrade(price: orders[i].price, expiryBuffer: orders[i].expiry,
tickets: orders[i].indices, contractAddress: orders[i].contractAddress)
let signature = try! keyStore.signMessageData(messageData, for: account)
let signedOrder : SignedOrder = try! SignedOrder(order : orders[i], message: messageData,
let signature = try! keyStore.signMessageData(Data(bytes: message), for: account)
let signedOrder : SignedOrder = try! SignedOrder(order : orders[i], message: message,
signature : signature.description)
signedOrders.append(signedOrder)
}
return signedOrders
}
func encodeMessageForTrade(price : [UInt8], expiryBuffer : [UInt8],
func encodeMessageForTrade(price : BigUInt, expiryBuffer : BigUInt,
tickets : [UInt16], contractAddress : String) -> [UInt8]
{
//ticket count * 2 because it is 16 bits not 8
@ -63,10 +67,11 @@ public class SignOrders {
var buffer = [UInt8]()
buffer.reserveCapacity(arrayLength)
var priceInWei : [UInt8] = price
var expiry : [UInt8] = expiryBuffer
//TODO check up on this
var priceInWei = Array(price.serialize())
var expiry = Array(expiryBuffer.serialize())
for _ in 0...31 - price.count {
for _ in 0...31 - priceInWei.count {
//pad with zeros
priceInWei.insert(0, at: 0)
}
@ -74,7 +79,7 @@ public class SignOrders {
buffer.append(priceInWei[i])
}
for i in 0...31 - expiryBuffer.count {
for _ in 0...31 - expiry.count {
expiry.insert(0, at: 0)
}
@ -97,9 +102,8 @@ public class SignOrders {
return buffer
}
func uInt16ArrayToUInt8(arrayOfUInt16: [UInt16]) -> [UInt8]
{
var arrayOfUint8 : [UInt8] = [UInt8]()
func uInt16ArrayToUInt8(arrayOfUInt16: [UInt16]) -> [UInt8] {
var arrayOfUint8 = [UInt8]()
for i in 0...arrayOfUInt16.count - 1 {
var UInt8ArrayPair = arrayOfUInt16[i].bigEndian.data.array
arrayOfUint8.append(UInt8ArrayPair[0])
@ -108,14 +112,4 @@ public class SignOrders {
return arrayOfUint8
}
func bufferToString(buffer : [UInt8]) -> String
{
var bufferString : String = "";
for i in 0...buffer.count - 1 {
bufferString += String(buffer[i])
}
return bufferString
}
}

@ -2,16 +2,28 @@ import Foundation
import XCTest
@testable import Trust
import TrustKeystore
import BigInt
class OrderRequestTest : XCTestCase {
var expectations = [XCTestExpectation]()
func testHttpCallToQueue() {
let expectation = self.expectation(description: "wait til callback")
expectations.append(expectation)
OrdersRequest.init().getOrders(callback: {
callback in print(callback)
callback in
print(callback)
expectation.fulfill()
})
wait(for: expectations, timeout: 10)
}
func testPostingOrderToQueue() {
func testPuttingOrderToQueue() {
let expectation = self.expectation(description: "wait til callback")
expectations.append(expectation)
var testOrdersList : Array<Order> = Array<Order>()
let keyStore = FakeEtherKeystore()
let account = keyStore.createAccount(password: "haha")
@ -20,31 +32,33 @@ class OrderRequestTest : XCTestCase {
var indices = [UInt16]()
indices.append(1)
indices.append(2)
var price = [UInt8]()
price.append(1) //price is 1 wei
var expiry = [UInt8]()
expiry.append(0)
let testOrder1 = Order(price : price, ticketIndices: indices,
expiryBuffer: expiry, contractAddress : "d9864b424447B758CdE90f8655Ff7cA4673956bf")
let testOrder1 = Order(price: BigUInt("100000")!, indices: indices,
expiry: BigUInt("0")!, contractAddress: "007bee82bdd9e866b2bd114780a47f2261c684e3",
start: BigUInt("500000210121213")!, count: 3)
testOrdersList.append(testOrder1)
let signOrders = SignOrders()
let signedOrders : Array<SignedOrder> = signOrders.signOrders(orders: testOrdersList, account: account)
//TODO fix signature issues
var signedOrders : Array<SignedOrder> = signOrders.signOrders(orders: testOrdersList, account: account)
signedOrders[0].signature = "0x1cae08113567db5303fb1ed1b157fbc8c7247aa" +
"9689ee76902d731c9806ab9853d8fcded6145fc7ebe5c32e41e247b315" +
"b2b23f41dcb3acd17d01a9f6140669f1c"
let privateKey = keyStore.exportPrivateKey(account: account)
let publicKey = "qTIttEQTN2OhfJJimQInXPYwz9EohLtg2MFMrnCtTmSpMi20RBM3Y6F8kmKZAidc9jDP0SiEu2DYwUyucK1OZK"
//try! Secp256k1.shared.getPublicKeyFromPrivateKey(from: privateKey.dematerialize()).hexString
//TODO get public key or change server to take address
// OrdersRequest.init().giveOrderToServer(signedOrders: signedOrders, publicKeyHex: publicKey, callback: {
// callback in
// print(callback)
// })
let publicKey = try! Secp256k1.shared.pubKeyFromPrivateKey(from:
privateKey.dematerialize()).hexString
OrdersRequest.init().putOrderToServer(signedOrders: signedOrders, publicKey: publicKey, callback: {
callback in
print(callback)
expectation.fulfill()
})
wait(for: expectations, timeout: 10)
}
}

@ -5,7 +5,9 @@ import RealmSwift
import BigInt
class OrderSigningTests : XCTestCase {
var contractAddress = "d9864b424447B758CdE90f8655Ff7cA4673956bf"
func testSigningOrders() {
var testOrdersList : Array<Order> = Array<Order>()
@ -16,14 +18,9 @@ class OrderSigningTests : XCTestCase {
indices.append(1)
indices.append(2)
var price = [UInt8]()
price.append(1) //price is 1 wei
var expiry = [UInt8]()
expiry.append(0)
let testOrder1 = Order(price : price, ticketIndices: indices,
expiryBuffer: expiry, contractAddress : "d9864b424447B758CdE90f8655Ff7cA4673956bf")
let testOrder1 = Order(price: BigUInt("0")!, indices: indices,
expiry: BigUInt("0")!, contractAddress: contractAddress,
start: BigUInt("91239231313")!, count: 3)
testOrdersList.append(testOrder1)
let signOrders = SignOrders()
@ -31,16 +28,16 @@ class OrderSigningTests : XCTestCase {
print(account.address)
var signedOrders : Array<SignedOrder> = signOrders.signOrders(orders: testOrdersList, account: account)
signedOrders[0].signature = "jrzcgpsnV7IPGE3nZQeHQk5vyZdy5c8rHk0R/iG7wpiK9NT730I//DN5Dg5fHs+s4ZFgOGQnk7cXLQROBs9NvgE="
var signature = try! keyStore.signMessageData(signedOrders.first?.message, for: account).dematerialize().hexString
var signature = try! keyStore.signMessageData(Data(bytes: signedOrders[0].message), for: account).dematerialize().hexString
print("v: " + Int(signature.substring(from: 128), radix: 16)!.description)
print("r: 0x" + signature.substring(to: 64))
print("s: 0x" + signature.substring(from: 64))
// for i in 0...2016 {
// try! keyStore.signMessageData((signedOrders.first?.message)!, for: account)
// }
//
//TODO optimize by keeping unlocked during signing
//for i in 0...2015 {
// try! keyStore.signMessageData(Data(bytes: signedOrders[0].message), for: account)
//}
print(signedOrders.description)
}
}

Loading…
Cancel
Save