def send(message, address=config["address"]): nem_amount = 0 hex_message = hexlify(message.encode("utf-8")).decode() fee = currentFeeFactor * calculate_minimum(nem_amount / 1000000) msg_fee = calculate_message(hex_message) total_fee = math.floor((msg_fee + fee) * 1000000) transfer = {} transfer["timeStamp"] = int(time.time()) - 1427587585 transfer["deadline"] = int(time.time()) - 1427587585 + 3600 * 2 transfer["amount"] = nem_amount transfer["fee"] = total_fee transfer["recipient"] = address transfer["type"] = 257 transfer["message"] = {"payload": hex_message, "type": 1} transfer["version"] = nem_network( config["network"] ) # mainnet = 1744830464 + 1 ; testnet = -1744830464 + 1 transfer["signer"] = config["publickey"] tx_hex = TransactionBuilder().encode(transfer) sign_raw = sign(msg=unhexlify(tx_hex.encode()), sk=config["privatekey"], pk=config["publickey"]) sign_hex = hexlify(sign_raw).decode() transaction = json.dumps({"data": tx_hex, "signature": sign_hex}) r = send_request("/transaction/announce", "POST", transaction) res = {"tx": r["transactionHash"]["data"]} if r["code"] != 1: res["error"] = r["message"] return res
def multisig_sending(): # Multisig sending main_net = False nem = NemConnect(main_net=main_net) nem.start() print("start") cosigner_sk = '' cosigner_pk = '' multisig_pk = '' recipient_ck = '' mosaics = {'nem:xem': 123} tx_dict = nem.multisig_mosaics_transfer(cosigner_pk, multisig_pk, recipient_ck, mosaics) print("tx:", tx_dict) tb = TransactionBuilder() tx_hex = tb.encode(tx_dict) print("tx hex:", tx_hex) sign_raw = sign(msg=unhexlify(tx_hex.encode()), sk=cosigner_sk, pk=cosigner_pk) sign_hex = hexlify(sign_raw).decode() print("sign", sign_hex) r = nem.transaction_announce(tx_hex, sign_hex) print(r)
def message2signature(raw, address): # sign by address with closing(create_db(V.DB_ACCOUNT_PATH)) as db: cur = db.cursor() uuid, sk, pk = read_address2keypair(address, cur) if sk is None: raise BlockChainError('Not found address {}'.format(address)) return pk, sign(msg=raw, sk=sk, pk=pk)
def generate_sign(self, sk): assert self.version is not None, 'mint version error.' d = { 'version': self.version, 'coin_id': self.coin_id, 'name': self.name, 'unit': self.unit, 'digit': self.digit, 'amount': self.amount, 'additional_issue': self.additional_issue, 'owner': self.owner } binary = bjson.dumps(d, compress=False) self.sign = sign(msg=binary, sk=sk, pk=self.owner)
def test(): PUB = '80d2ae0d784d28db38b5b85fd77e190981cea6f4328235ec173a90c2853c0761' PRI = '6a858fb93e0202fa62f894e591478caa23b06f90471e7976c30fb95efda4b312' MSG = "how silent! the cicada's voice soaks into the rocks. " \ "Up here, a stillness the sound of the cicadas seeps into the crags.".encode() address = get_address(pk=PUB, main_net=True) print(address) sign_raw = sign(msg=MSG, sk=PRI, pk=PUB) print(sign_raw) # raised ValueError if verification filed verify(msg=MSG, sign=sign_raw, pk=PUB) PUB2 = '28e8469422106f406051a24f2ea6402bac6f1977cf7e02eb3bf8c11d4070157a' PRI2 = '3c60f29c84b63c76ca8e3f1068ad328285ae8d5af2a95aa99ceb83d327dfb97e' print("pub2 address", get_address(pk=PUB2, main_net=True)) enc = encrypt(sk=PRI, pk=PUB2, msg=MSG) print(enc) dec = decrypt(sk=PRI2, pk=PUB, enc=enc) print(dec)
import os from time import time from nem_ed25519.signature import sign, verify import cProfile sk = '78f8932df54d22319a16dc4940c269205ae0946f98d38ef30aea488a47426153' pk = '77041bfb4b6afebc31aaab7b02d68e577fe069524b3c661c804b42ef381f717b' ck = 'NBOGOGSENUPBFMAPTGHVI4UIAQPVSNKJLWUVHBED' COUNT = 300 start = time() pr = cProfile.Profile() sign_list = list() pr.enable() for i in range(COUNT): msg = os.urandom(i + 1) signature = sign(msg, sk, pk) sign_list.append((msg, signature)) # pr.disable() for msg, signature in sign_list: verify(msg, signature, pk) pr.disable() print(round((time() - start) * 1000 / COUNT, 3), 'mS/sign&verify') pr.print_stats() # before 3.85 mS/sign&verify
def send(self, from_id, to_address, mosaics, msg=b'', only_check=True, balance_check=True, encrypted=False, db=None): if db is None: db = self.db assert self.sk is not None, 'You need sk if you use \"send\"' to_address = to_address.replace('-', '') if to_address == self.ck: raise AccountError("You send to and receive to same address.") elif not is_address(to_address): raise AccountError('Not correct address format.') for mosaic in mosaics: self._check_expire_mosaic(mosaic, db) if encrypted: to_pk = self.nem.get_account_info(ck=to_address)['account']['publicKey'] if to_pk is None: raise AccountError('You send encrypt msg to Account that have never send before.') # Generally cannot convert CK to PK. msg = encrypt(self.sk, to_pk, msg) msg_type = 2 else: msg_type = 1 fee = self.nem.estimate_levy_fee(mosaics) fee = DictMath.add(fee, self.nem.estimate_msg_fee(msg)) fee = DictMath.add(fee, self.nem.estimate_send_fee(mosaics)) tx_dict = self.nem.mosaic_transfer(self.pk, to_address, mosaics, msg, msg_type) tb = TransactionBuilder() tx_hex = tb.encode(tx_dict) sign_raw = sign(msg=unhexlify(tx_hex.encode()), sk=self.sk, pk=self.pk) sign_hex = hexlify(sign_raw).decode() if only_check: balance = self.balance(from_id) need_amount = DictMath.add(fee, mosaics) send_ok = DictMath.all_plus_amount(DictMath.sub(balance, need_amount)) # only_check=False return sending info, NOT send return fee, send_ok, tx_dict, tx_hex, sign_hex else: with self.transaction: self.refresh(db=db) with db as conn: balance = self.balance(from_id) need_amount = DictMath.add(fee, mosaics) if balance_check and not DictMath.all_plus_amount(DictMath.sub(balance, need_amount)): need = {m: a for m, a in DictMath.sub(balance, need_amount).items() if a < 0} raise AccountError('Not enough balance on ID:%d, %s' % (from_id, need)) tx_hash = self.nem.transaction_announce(tx_hex, sign_hex) outgoing_many = list() for mosaic in need_amount: # height, time is None amount = need_amount[mosaic] value = self.get_value(mosaic, amount, db=db) price = self.get_price(mosaic, db=db) outgoing_many.append(( unhexlify(tx_hash.encode()), None, from_id, mosaic, amount, value, price, None )) conn.executemany(""" INSERT INTO `outgoing_table` VALUES (?, ?, ?, ?, ?, ?, ?, ?) """, outgoing_many) conn.commit() threading.Thread(target=self._send, name='Wait', args=(tx_hash,), daemon=False).start() return tx_hash
def message2signature(raw, address): # sign by address with closing(create_db(V.DB_ACCOUNT_PATH)) as db: cur = db.cursor() uuid, sk, pk = read_address2keypair(address, cur) return pk, sign(msg=raw, sk=sk, pk=pk)