def sign_create_deposit(deposit_params, private_key): """ Function to sign the deposit parameters required by the Switcheo API. Execution of this function is as follows:: sign_create_deposit(deposit_params=signable_params, private_key=eth_private_key) The expected return result for this function is as follows:: { 'blockchain': 'eth', 'asset_id': 'ETH', 'amount': '10000000000000000', 'timestamp': 1542089346249, 'contract_hash': '0x607af5164d95bd293dbe2b994c7d8aef6bec03bf', 'address': '0x32c46323b51c977814e05ef5e258ee4da0e4c3c3', 'signature': 'd4b8491d6514bff28b9f2caa440f51a93f31d....' } :param deposit_params: Parameters needed to deposit to the Switcheo API and signed in this function. :type deposit_params: dict :param private_key: The Ethereum private key to sign the deposit parameters. :type private_key: str :return: Dictionary of signed message to send to the Switcheo API. """ hash_message = defunct_hash_message(text=stringify_message(deposit_params)) hex_message = binascii.hexlify(hash_message).decode() signed_message = binascii.hexlify(Account.signHash(hex_message, private_key=private_key)['signature']).decode() create_params = deposit_params.copy() create_params['address'] = to_normalized_address(Account.privateKeyToAccount(private_key=private_key).address) create_params['signature'] = signed_message return create_params
def sign_create_cancellation(cancellation_params, private_key): """ Function to sign the parameters required to create a cancellation request from the Switcheo Exchange. Execution of this function is as follows:: sign_create_cancellation(cancellation_params=signable_params, private_key=eth_private_key) The expected return result for this function is as follows:: { 'order_id': '3125550a-04f9-4475-808b-42b5f89d6693', 'timestamp': 1542088842108, 'address': '0x32c46323b51c977814e05ef5e258ee4da0e4c3c3', 'signature': 'dac70ca711bcfbeefbdead2158ef8b15fab1a1....' } :param cancellation_params: Dictionary with Order ID and timestamp to sign for creating the cancellation. :type cancellation_params: dict :param private_key: The Ethereum private key to sign the deposit parameters. :type private_key: str :return: Dictionary of signed message to send to the Switcheo API. """ hash_message = defunct_hash_message(text=stringify_message(cancellation_params)) hex_message = binascii.hexlify(hash_message).decode() signed_message = binascii.hexlify(Account.signHash(hex_message, private_key=private_key)['signature']).decode() create_params = cancellation_params.copy() create_params['address'] = to_normalized_address(Account.privateKeyToAccount(private_key=private_key).address) create_params['signature'] = signed_message return create_params
def sign_create_withdrawal(withdrawal_params, private_key): """ Function to create a withdrawal from the Switcheo Smart Contract. Execution of this function is as follows:: sign_create_withdrawal(withdrawal_params=signable_params, private_key=eth_private_key) The expected return result for this function is as follows:: { 'blockchain': 'eth', 'asset_id': 'ETH', 'amount': '10000000000000000', 'timestamp': 1542090476102, 'contract_hash': '0x607af5164d95bd293dbe2b994c7d8aef6bec03bf', 'address': '0x32c46323b51c977814e05ef5e258ee4da0e4c3c3', 'signature': '375ddce62e5b3676d5e94ebb9f9a8af5963b....' } :param withdrawal_params: The parameters to be signed and create a withdraw from Switcheo. :type withdrawal_params: dict :param private_key: The Ethereum private key to sign the deposit parameters. :type private_key: str :return: Dictionary of the signed transaction to initiate the withdrawal of ETH via the Switcheo API. """ hash_message = defunct_hash_message(text=stringify_message(withdrawal_params)) hex_message = binascii.hexlify(hash_message).decode() signed_message = binascii.hexlify(Account.signHash(hex_message, private_key=private_key)['signature']).decode() create_params = withdrawal_params.copy() create_params['address'] = to_normalized_address(Account.privateKeyToAccount(private_key=private_key).address) create_params['signature'] = signed_message return create_params
def test_stringify_message(self): json_msg = { "name": "John Smith", "age": 27, "siblings": ["Jane", "Joe"] } stringify_msg = '{"age":27,"name":"John Smith","siblings":["Jane","Joe"]}' self.assertEqual(stringify_message(json_msg), stringify_msg)
def on_all(self, data): self.lock.acquire() self.order_book[data["room"]["pair"]] = data self.lock.release() digest_hash = data["digest"] book = data["book"] book_digest_hash = sha1_hash_digest(stringify_message(book)) if digest_hash != book_digest_hash: self.emit(event="leave", data=data["room"], namespace='/v2/books') self.emit(event="join", data=data["room"], namespace='/v2/books')
def on_all(self, data): self.lock.acquire() self.trade_events[data["room"]["pair"]] = data self.lock.release() digest_hash = data["digest"] trades = data["trades"] trade_digest_hash = sha1_hash_digest(stringify_message(trades)) if digest_hash != trade_digest_hash: self.emit(event="leave", data=data["room"], namespace='/v2/trades') self.emit(event="join", data=data["room"], namespace='/v2/trades')
def on_updates(self, data): update_digest = data["digest"] update_pair = data["room"]["pair"] update_events = data["events"] update_limit = data["limit"] self.lock.acquire() self.trade_events[update_pair]["trades"] = update_events + \ self.trade_events[update_pair]["trades"] trade_slice = update_limit - 1 self.trade_events[update_pair]["trades"] = self.trade_events[ update_pair]["trades"][0:trade_slice] trades = self.trade_events[update_pair]["trades"] self.lock.release() trade_digest_hash = sha1_hash_digest(stringify_message(trades)) if update_digest != trade_digest_hash: self.emit(event="leave", data=data["room"], namespace='/v2/trades') self.emit(event="join", data=data["room"], namespace='/v2/trades')
def sign_create_order(order_params, private_key): """ Function to sign the create order parameters and send to the Switcheo API. Execution of this function is as follows:: sign_create_order(order_params=signable_params, private_key=eth_private_key) The expected return result for this function is as follows:: { 'blockchain': 'eth', 'pair': 'JRC_ETH', 'side': 'buy', 'price': '0.00000003', 'want_amount': '3350000000000000000000000', 'use_native_tokens': False, 'order_type': 'limit', 'timestamp': 1542089785915, 'contract_hash': '0x607af5164d95bd293dbe2b994c7d8aef6bec03bf', 'signature': '536306a2f2aee499ffd6584027029ee585293b3686....', 'address': '0x32c46323b51c977814e05ef5e258ee4da0e4c3c3' } :param order_params: Parameters to create an order to be submitted to the Switcheo Order Book. :type order_params: dict :param private_key: The Ethereum private key to sign the deposit parameters. :type private_key: str :return: Dictionary of signed message to send to the Switcheo API. """ hash_message = defunct_hash_message(text=stringify_message(order_params)) hex_message = binascii.hexlify(hash_message).decode() create_params = order_params.copy() signed_message = binascii.hexlify( Account.signHash(hex_message, private_key=private_key)['signature']).decode() create_params['signature'] = signed_message create_params['address'] = to_normalized_address( Account.privateKeyToAccount(private_key=private_key).address) return create_params
def encode_message(message): message_hex = binascii.hexlify( stringify_message(message).encode('utf-8')).decode() message_hex_length = hex(int(len(message_hex) / 2))[2:] return '010001f0' + message_hex_length + message_hex + '0000'
def on_updates(self, data): update_digest = data["digest"] update_pair = data["room"]["pair"] update_events = data["events"] buy_event = False sell_event = False if "symbol" in self.order_book[update_pair]["book"]: del self.order_book[update_pair]["book"]["symbol"] self.lock.acquire() for event in update_events: price_match = False event_iteration = 0 if event["side"] == "buy": event_side = "buys" buy_event = True elif event["side"] == "sell": event_side = "sells" sell_event = True event_price = event["price"] event_change = event["delta"] for side in self.order_book[update_pair]["book"][event_side]: if side["price"] == event_price: price_match = True updated_amount = int(side["amount"]) + int(event_change) if updated_amount == 0: self.order_book[update_pair]["book"][ event_side].remove(side) else: updated_book = {} updated_book["amount"] = str(updated_amount) updated_book["price"] = str(event_price) self.order_book[update_pair]["book"][event_side][ event_iteration] = updated_book break event_iteration += 1 if not price_match: new_book = {} new_book["amount"] = event_change new_book["price"] = event_price self.order_book[update_pair]["book"][event_side].append( new_book) if buy_event and sell_event: self.order_book[update_pair]["book"]["buys"] = sorted( self.order_book[update_pair]["book"]["buys"], key=itemgetter("price"), reverse=True) self.order_book[update_pair]["book"]["sells"] = sorted( self.order_book[update_pair]["book"]["sells"], key=itemgetter("price"), reverse=True) elif buy_event: self.order_book[update_pair]["book"]["buys"] = sorted( self.order_book[update_pair]["book"]["buys"], key=itemgetter("price"), reverse=True) elif sell_event: self.order_book[update_pair]["book"]["sells"] = sorted( self.order_book[update_pair]["book"]["sells"], key=itemgetter("price"), reverse=True) book = self.order_book[update_pair]["book"] self.lock.release() book_digest_hash = sha1_hash_digest(stringify_message(book)) if update_digest != book_digest_hash: self.emit(event="leave", data=data["room"], namespace='/v2/books') self.emit(event="join", data=data["room"], namespace='/v2/books')