def do_batch(dat): """ Make a batch of transfers :param dat: :return: """ import config snap_info = pickle.loads(Redisdb.get("snapshot_info")) try: Bitshares = config.BitShares(node=config.WSS_NODE, wif=dat['form']['key']) oaccount = Bitshares.wallet.getAccounts()[0] except Exception as err: print(err.__repr__()) Redisdb.rpush("messages", "*|launch_error|key not found") print(0) return False for job in dat['job']: try: wif = dat['key'] pub = format(account.PrivateKey(wif).pubkey, config.PREFIX) to_account_id = job[0].decode('utf8') amount = 10 asset_id = dat['asset']['id'] message = "abcdefgABCDEFG0123456789" nonce = "5862723643998573708" fee = objects.Asset(amount=0, asset_id="1.3.0") amount = objects.Asset(amount=int(amount), asset_id=asset_id) encrypted_memo = memo.encode_memo( account.PrivateKey(wif), account.PublicKey(pub, prefix=config.PREFIX), nonce, message) memoStruct = { "from": pub, "to": pub, "nonce": nonce, "message": encrypted_memo, } memoObj = objects.Memo(**memoStruct) tmp = operations.Transfer( **{ "fee": fee, "from": dat['from'], "to": to_account_id, "amount": amount, "memo": memoObj, "prefix": config.PREFIX }) print() #test = Bitshares.transfer(job[0].decode('utf8'), float(job[1]), dat['asset'], "", dat['from']) except Exception as err: print(err.__repr__()) print(0) Redisdb.decr("batch_jobs", int(1)) Redisdb.rpush( "messages", datetime.datetime.now().isoformat() + " Jobs queue: {}".format(int(Redisdb.get("batch_jobs"))))
def encrypt(self, memo): """ Encrypt a memo :param str memo: clear text memo message :returns: encrypted memo :rtype: str """ if not memo: return None nonce = str(random.getrandbits(64)) memo_wif = self.bitshares.wallet.getPrivateKeyForPublicKey( self.from_account["options"]["memo_key"]) if not memo_wif: raise MissingKeyError("Memo key for %s missing!" % self.from_account["name"]) enc = BtsMemo.encode_memo( PrivateKey(memo_wif), PublicKey(self.to_account["options"]["memo_key"], prefix=self.bitshares.prefix), nonce, memo) return { "message": enc, "nonce": nonce, "from": self.from_account["options"]["memo_key"], "to": self.to_account["options"]["memo_key"] }
async def _encrypt_memo(self, to, memo): ''' 加密备注信息 ''' if not memo: return None # 生成nonce nonce = str(random.getrandbits(64)) if SysConfig().memo_key == None: raise MissingKeyError('Memo key {0} missing!'.format(SysConfig().account)) # 加密备注信息 prefix = self.client.chain_params['prefix'] to_account = await self.client.get_account_by_name(to) enc = BtsMemo.encode_memo( PrivateKey(SysConfig().memo_key, prefix=prefix), PublicKey( prefix=prefix, pk=to_account['options']['memo_key'] ), nonce, memo ) # 返回结构信息 memo_data = { 'nonce': nonce, 'message': enc, 'to': to_account['options']['memo_key'], 'from': self.account['options']['memo_key'] } return to_account['id'], memo_data
def test_encrypt(self): for memo in test_cases: enc = encode_memo( PrivateKey(memo["wif"]), PublicKey(memo["to"], prefix="GPH"), memo["nonce"], memo["plain"], ) self.assertEqual(memo["message"], enc)
def test_Transfer(self): pub = format(account.PrivateKey(wif).pubkey, prefix) from_account_id = "1.2.0" to_account_id = "1.2.1" amount = 1000000 asset_id = "1.3.4" message = "abcdefgABCDEFG0123456789" nonce = "5862723643998573708" fee = objects.Asset(amount=0, asset_id="1.3.0") amount = objects.Asset(amount=int(amount), asset_id=asset_id) encrypted_memo = memo.encode_memo( account.PrivateKey(wif), account.PublicKey(pub, prefix=prefix), nonce, message) memoStruct = { "from": pub, "to": pub, "nonce": nonce, "message": encrypted_memo, "chain": prefix } memoObj = objects.Memo(**memoStruct) op = operations.Transfer( **{ "fee": fee, "from": from_account_id, "to": to_account_id, "amount": amount, "memo": memoObj }) ops = [Operation(op)] tx = Signed_Transaction(ref_block_num=ref_block_num, ref_block_prefix=ref_block_prefix, expiration=expiration, operations=ops) tx = tx.sign([wif], chain=prefix) tx.verify([PrivateKey(wif).pubkey], "BTS") txWire = hexlify(bytes(tx)).decode("ascii") compare = ("f68585abf4dce7c804570100000000000000000000000140420" "f0000000000040102c0ded2bc1f1305fb0faac5e6c03ee3a192" "4234985427b6167ca569d13df435cf02c0ded2bc1f1305fb0fa" "ac5e6c03ee3a1924234985427b6167ca569d13df435cf8c94d1" "9817945c5120fa5b6e83079a878e499e2e52a76a7739e9de409" "86a8e3bd8a68ce316cee50b210000011f39e3fa7071b795491e" "3b6851d61e7c959be92cc7deb5d8491cf1c3c8c99a1eb44553c" "348fb8f5001a78b18233ac66727e32fc776d48e92d9639d64f6" "8e641948") self.assertEqual(compare[:-130], txWire[:-130])
def encodeMemo(self, from_pubkey, to_pubkey, clear_text): from_pubkey = self.guessKey(from_pubkey) privkey = self.privkeys[str(from_pubkey)] if not isinstance(from_pubkey, (PublicKey, GPHPublicKey)): from_pubkey = PublicKey(from_pubkey, prefix=from_pubkey[0:3]) if not isinstance(to_pubkey, (PublicKey, GPHPublicKey)): to_pubkey = PublicKey(to_pubkey, prefix=to_pubkey[0:3]) import random nonce = str(random.getrandbits(64)) cipher = BTSMemo.encode_memo(privkey, to_pubkey, nonce, clear_text) return { "to": str(to_pubkey), "from": str(privkey.pubkey), "nonce": nonce, "message": cipher }
def test_transfer(self): pub = format(account.PrivateKey(wif).pubkey, prefix) from_account_id = "1.2.0" to_account_id = "1.2.1" amount = 1000000 asset_id = "1.3.4" message = "abcdefgABCDEFG0123456789" nonce = "5862723643998573708" fee = objects.Asset(amount=0, asset_id="1.3.0") amount = objects.Asset(amount=int(amount), asset_id=asset_id) self.op = operations.Transfer( **{ "fee": fee, "from": from_account_id, "to": to_account_id, "amount": amount, "memo": { "from": pub, "to": pub, "nonce": nonce, "message": memo.encode_memo( account.PrivateKey(wif), account.PublicKey(pub, prefix=prefix), nonce, message, ), }, "prefix": prefix, }) self.cm = ("f68585abf4dce7c804570100000000000000000000000140420" "f0000000000040102c0ded2bc1f1305fb0faac5e6c03ee3a192" "4234985427b6167ca569d13df435cf02c0ded2bc1f1305fb0fa" "ac5e6c03ee3a1924234985427b6167ca569d13df435cf8c94d1" "9817945c5120fa5b6e83079a878e499e2e52a76a7739e9de409" "86a8e3bd8a68ce316cee50b210000011f39e3fa7071b795491e" "3b6851d61e7c959be92cc7deb5d8491cf1c3c8c99a1eb44553c" "348fb8f5001a78b18233ac66727e32fc776d48e92d9639d64f6" "8e641948") self.doit()
def test_asset_issue(self): message = "abcdefgABCDEFG0123456789" nonce = "5862723643998573708" pub = format(account.PrivateKey(wif).pubkey, prefix) encrypted_memo = memo.encode_memo( account.PrivateKey(wif), account.PublicKey(pub, prefix=prefix), nonce, message, ) self.op = operations.Asset_issue( **{ "fee": { "amount": 0, "asset_id": "1.3.0" }, "issuer": "1.2.0", "asset_to_issue": { "amount": 0, "asset_id": "1.3.0" }, "memo": { "from": pub, "to": pub, "nonce": nonce, "message": encrypted_memo, }, "issue_to_account": "1.2.0", "extensions": [], "prefix": prefix, }) self.cm = ("f68585abf4dce7c80457010e000000000000000000000000000" "00000000000000102c0ded2bc1f1305fb0faac5e6c03ee3a192" "4234985427b6167ca569d13df435cf02c0ded2bc1f1305fb0fa" "ac5e6c03ee3a1924234985427b6167ca569d13df435cf8c94d1" "9817945c5120fa5b6e83079a878e499e2e52a76a7739e9de409" "86a8e3bd8a68ce316cee50b210000012055139900ea2ae7db9d" "4ef0d5d4015d2d993d0590ad32662bda94daba74a5e13411aef" "4de6f847e9e4300e5c8c36aa8e5f9032d25fd8ca01abd58c7e9" "528677e4") self.doit()