Skip to content

Commit

Permalink
#228: Upgrade bitski to 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
generalpiston committed Jun 16, 2019
1 parent 379c14c commit 3993ad5
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 144 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { View } from 'react-native';
import Entypo from 'react-native-vector-icons/Entypo';
import { connect as connectNavigation } from '../../../../navigation';
import * as AssetService from '../../../../services/AssetService';
import { WalletService } from '../../../../services/WalletService';
Expand Down
4 changes: 3 additions & 1 deletion clients/0x.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ export default class ZeroExClient {

@time
async getContractWrappers() {
const options = this.ethereumClient.options || {};
return new ContractWrappers(this.ethereumClient.getCurrentProvider(), {
networkId: await this.ethereumClient.getNetworkId()
networkId: await this.ethereumClient.getNetworkId(),
gasPrice: options.gasPrice
});
}

Expand Down
110 changes: 67 additions & 43 deletions ios/BitskiManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,30 @@ import Foundation
import Bitski
import Web3

struct BitskiManagerError: Error {
let message: String

init(message: String) {
self.message = message
}
}

@objc(BitskiManager)
class BitskiManager: NSObject {
private var network: Bitski.Network? = nil;

@objc(initialize:clientID:redirectURL:callback:) func initialize(network: String, clientID: String, redirectURL: String, callback: @escaping RCTResponseSenderBlock) -> Void {
self.network = Bitski.Network.init(rawValue: network)
switch(network) {
case "mainnet":
self.network = Bitski.Network.mainnet
break;
case "kovan":
self.network = Bitski.Network.kovan
break;
default:
self.network = Bitski.Network.mainnet
break;
}

Bitski.shared = Bitski(clientID: clientID, redirectURL: URL(string: redirectURL)!)

Expand Down Expand Up @@ -52,62 +70,68 @@ class BitskiManager: NSObject {
callback(["Bitski not instantiated", NSNull()])
return;
}

guard let waited = try? Bitski.shared?.getWeb3(network: self.network!).eth.accounts().wait(),
let addresses = waited,
addresses.count > 0 else {
callback(["Not logged in.", NSNull()])
return;
}
let address = addresses[0]
let data = EthereumData(bytes: Bytes.init(hexString: message as! String)!)
let response = try? bitski.getWeb3(network: self.network!).eth.sign(address: address, data: data).wait()

callback([NSNull(), response?.hex()])
let web3 = bitski.getWeb3(network: self.network!)
let data = EthereumData(bytes: hexToPaddedData(message).bytes)

firstly {
web3.eth.accounts().firstValue
}.then { account in
web3.eth.sign(address: account, data: data)
}.done { signature in
callback([NSNull(), signature.hex()])
}.catch { error in
callback([error.localizedDescription, NSNull()])
}
}

@objc(signTransaction:callback:) func signTransaction(tx: NSDictionary, callback: @escaping RCTResponseSenderBlock) -> Void {
guard let bitski = Bitski.shared else {
callback(["Bitski not instantiated", NSNull()])
@objc(sendTransaction:callback:) func sendTransaction(tx: NSDictionary, callback: @escaping RCTResponseSenderBlock) -> Void {
if (tx.object(forKey: "gasPrice") == nil) {
tx.setValue("0x1", forKey: "gasPrice")
}

guard let to: EthereumAddress = EthereumAddress(hexString: tx["to"] as! String) else {
callback(["`to` address is malformed.", NSNull()])
return;
}

let nonce: EthereumQuantity? = EthereumQuantity(quantity: hexToPaddedBigUInt(tx["nonce"] as! String))
let to: EthereumAddress = EthereumAddress(hexString: tx["to"] as! String)!
var gas: EthereumQuantity? = nil
var gasPrice: EthereumQuantity? = nil
var value: EthereumQuantity? = nil
var from: EthereumAddress? = nil
var data: EthereumData = EthereumData(bytes: Bytes.init())
let gas: EthereumQuantity = EthereumQuantity(quantity: hexToPaddedBigUInt(tx["gas"] as! String))
let gasPrice: EthereumQuantity = EthereumQuantity(quantity: hexToPaddedBigUInt(tx["gasPrice"] as! String))

if (tx.object(forKey: "data") != nil) {
if let bytes = Bytes.init(hexString: tx["data"] as! String) {
data = EthereumData(bytes: bytes)
}
var value: EthereumQuantity? = nil
if tx.object(forKey: "value") != nil {
print("value")
value = EthereumQuantity(quantity: hexToPaddedBigUInt(tx["value"] as! String))
}

if (tx.object(forKey: "from") != nil) {
from = EthereumAddress(hexString: tx["from"] as! String)
var data: EthereumData = EthereumData(bytes: [])
if tx.object(forKey: "data") != nil {
data = EthereumData(bytes: hexToPaddedData(tx["data"] as! String).bytes)
}

if (tx.object(forKey: "gas") != nil) {
gas = EthereumQuantity(quantity: hexToPaddedBigUInt(tx["gas"] as! String))

guard let bitski = Bitski.shared else {
callback(["Bitski not instantiated", NSNull()])
return;
}

if (tx.object(forKey: "gasPrice") != nil) {
gasPrice = EthereumQuantity(quantity: hexToPaddedBigUInt(tx["gasPrice"] as! String))
}
let web3 = bitski.getWeb3(network: self.network!)

if (tx.object(forKey: "value") != nil) {
value = EthereumQuantity(quantity: hexToPaddedBigUInt(tx["value"] as! String))
} else {
value = EthereumQuantity(quantity: hexToPaddedBigUInt("0x0"))
firstly {
web3.eth.accounts().firstValue
}.done { account in
firstly {
web3.eth.getTransactionCount(address: account, block: .latest)
}.then { nonce -> Promise<EthereumData> in
let transaction = EthereumTransaction(nonce: nonce, gasPrice: gasPrice, gas: gas, from: account, to: to, value: value, data: data)
return web3.eth.sendTransaction(transaction: transaction)
}.done { transactionHash in
callback([NSNull(), transactionHash.hex()])
}.catch { error in
callback([error.localizedDescription, NSNull()])
}
}.catch { error in
callback([error.localizedDescription, NSNull()])
}

let tx = EthereumTransaction(nonce: nonce, gasPrice: gasPrice, gas: gas, from: from, to: to, value: value, data: data)
let response = try? bitski.getWeb3(network: self.network!).eth.sendTransaction(transaction: tx).wait()

callback([NSNull(), response?.hex()])
}

@objc(login:) func login(callback: @escaping RCTResponseSenderBlock) -> Void {
Expand Down
2 changes: 1 addition & 1 deletion ios/BitskiManagerBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ @interface RCT_EXTERN_MODULE(BitskiManager, NSObject)
RCT_EXTERN_METHOD(initialize:(NSString *)network clientID:(NSString *)clientID redirectURL:(NSString *)redirectURL callback:(RCTResponseSenderBlock)callback)
RCT_EXTERN_METHOD(isWalletAvailable:(RCTResponseSenderBlock)callback)
RCT_EXTERN_METHOD(loadWalletAddress:(RCTResponseSenderBlock)callback)
RCT_EXTERN_METHOD(signTransaction:(NSDictionary *)tx callback:(RCTResponseSenderBlock)callback)
RCT_EXTERN_METHOD(sendTransaction:(NSDictionary *)tx callback:(RCTResponseSenderBlock)callback)
RCT_EXTERN_METHOD(signMessage:(NSString *)message callback:(RCTResponseSenderBlock)callback)
RCT_EXTERN_METHOD(login:(RCTResponseSenderBlock)callback)
RCT_EXTERN_METHOD(logout:(RCTResponseSenderBlock)callback)
Expand Down
2 changes: 1 addition & 1 deletion ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ target 'mobidex' do

pod 'web3swift', '~> 2.1.3'
pod 'AppAuth', '>= 0.94'
pod 'Bitski', '~> 0.4.2'
pod 'Bitski', '~> 0.5.0'
end
8 changes: 4 additions & 4 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PODS:
- BigInt (3.1.0):
- SipHash (~> 1.2)
- BigInt.swift (1.0.0)
- Bitski (0.4.2):
- Bitski (0.5.0):
- AppAuth (~> 0.93)
- BigInt.swift (~> 1.0)
- PromiseKit/CorePromise (~> 6.0)
Expand Down Expand Up @@ -59,7 +59,7 @@ PODS:

DEPENDENCIES:
- AppAuth (>= 0.94)
- Bitski (~> 0.4.2)
- Bitski (~> 0.5.0)
- web3swift (~> 2.1.3)

SPEC REPOS:
Expand All @@ -84,7 +84,7 @@ SPEC CHECKSUMS:
AppAuth: 137f6bb6fc9dfbaf2cf9f6fcff1d336ff0163bc6
BigInt: 76b5dfdfa3e2e478d4ffdf161aeede5502e2742f
BigInt.swift: 1e0ddf08d82166e72dfb0fa8d40301d273081afc
Bitski: a978042793ded140519a18c117d73133be2aa521
Bitski: bf093dfe26419fb29490d1af731ba72956441b09
CryptoSwift: 769f58a9e89f64e8796c2e59ce5f002dc81a2438
EthereumABI: f040f5429e5a4366d028c88b88d9441e137593af
EthereumAddress: f476e1320dca3a0024431e713ede7a09c7eb7796
Expand All @@ -97,6 +97,6 @@ SPEC CHECKSUMS:
Web3: e9be7ef88585e252df50abc28538d6e4691c786e
web3swift: 23cc365dd3b28e9b990813965af4bd031e108f64

PODFILE CHECKSUM: 14267bd72080fbda397f3a9deadbed4cc0cb974e
PODFILE CHECKSUM: 837a81f625cc905e114dc6710e5ee2f18e15feef

COCOAPODS: 1.5.3
34 changes: 0 additions & 34 deletions lib/stores/bitski.js

This file was deleted.

24 changes: 14 additions & 10 deletions lib/wallets/bitski.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export class BitskiWallet {
constructor(settings) {
this.endpoint = settings.ethereumNodeEndpoint;
this.ready = false;
// this.available = Boolean(BitskiManager);
this.available = false;
this.available = Boolean(BitskiManager);
// this.available = false;
this.settings = settings;
this.address = null;
}
Expand All @@ -24,27 +24,31 @@ export class BitskiWallet {
getAccounts: cb => {
cb(null, addresses);
},
signTransaction: (txParams, cb) => {
console.debug(`signTransaction: ${JSON.stringify(txParams, null, 2)}`);
BitskiManager.signTransaction(txParams, (err, signedTx) => {
processTransaction: (txParams, cb) => {
console.debug(
`processTransaction: ${JSON.stringify(txParams, null, 2)}`
);
BitskiManager.sendTransaction(txParams, (err, txHash) => {
console.warn('processTransaction: ', err, txHash);
if (err) {
cb(err);
} else if (!signedTx) {
cb(new Error('Transaction was not signed.'));
} else if (!txHash) {
cb(new Error('Transaction did not go through.'));
} else {
cb(null, `0x${signedTx.serialize().toString('hex')}`);
cb(null, txHash);
}
});
},
signMessage: (params, cb) => {
console.debug('signMessage', params);
BitskiManager.signMessage(params, (err, signature) => {
BitskiManager.signMessage(params.data, (err, signature) => {
console.warn('signMessage: ', err, signature);
if (err) {
cb(err);
} else if (!signature) {
cb(new Error('Message was not signed.'));
} else {
cb(null, `0x${signature.serialize().toString('hex')}`);
cb(null, signature);
}
});
}
Expand Down
45 changes: 0 additions & 45 deletions thunks/bitski.js

This file was deleted.

1 change: 0 additions & 1 deletion thunks/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './0x';
export * from './bitski';
export * from './boot';
export * from './inf0x';
export * from './mobidex';
Expand Down
7 changes: 6 additions & 1 deletion thunks/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../navigation';
import * as AssetService from '../services/AssetService';
import { cancelOrder } from './orders';
import { refreshGasPrice } from './settings';
import { setUnlimitedProxyAllowance, setNoProxyAllowance } from './wallet';

/* eslint-disable */
Expand Down Expand Up @@ -86,7 +87,11 @@ export function approve(parentComponentId, assetData) {
return async dispatch => {
const asset = AssetService.findAssetByData(assetData);
const actionOptions = {
action: () => dispatch(setUnlimitedProxyAllowance(asset.address)),
action: () =>
dispatch(async dispatch => {
await dispatch(refreshGasPrice());
await dispatch(setUnlimitedProxyAllowance(asset.address));
}),
icon: <FontAwesome name="unlock" size={100} />,
label: <MajorText>Unlocking...</MajorText>
};
Expand Down
10 changes: 8 additions & 2 deletions thunks/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,16 @@ export function checkAndUnwrapEther(
}

export function setUnlimitedProxyAllowance(address) {
return async () => {
return async (dispatch, getState) => {
const {
settings: { gasPrice, gasLimit }
} = getState();

const web3 = WalletService.instance.web3;

const ethereumClient = new EthereumClient(web3);
const ethereumClient = new EthereumClient(web3, {
gasPrice
});
const tokenClient = new TokenClient(ethereumClient, address);
const txhash = await tokenClient.setUnlimitedProxyAllowance();
const activeTransaction = {
Expand Down

0 comments on commit 3993ad5

Please sign in to comment.