def encrypt_wif(self, wif): """ Encrypt a wif key """ assert not self.locked() return format( bip38.encrypt(PrivateKey(wif), self.masterpassword), "encwif")
def test_create_account(self): s = { "fee": { "amount": 1467634, "asset_id": "1.3.0" }, "registrar": "1.2.33", "referrer": "1.2.27", "referrer_percent": 3, "name": "foobar-f124", "owner": { "weight_threshold": 1, "account_auths": [], 'key_auths': [['BTS6pbVDAjRFiw6fkiKYCrkz7PFeL7XNAfefrsREwg8MKpJ9VYV9x', 1], ['BTS6zLNtyFVToBsBZDsgMhgjpwysYVbsQD6YhP3kRkQhANUB4w7Qp', 1]], "address_auths": [] }, "active": { "weight_threshold": 1, "account_auths": [], 'key_auths': [['BTS6pbVDAjRFiw6fkiKYCrkz7PFeL7XNAfefrsREwg8MKpJ9VYV9x', 1], ['BTS6zLNtyFVToBsBZDsgMhgjpwysYVbsQD6YhP3kRkQhANUB4w7Qp', 1], ['BTS8CemMDjdUWSV5wKotEimhK6c4dY7p2PdzC2qM1HpAP8aLtZfE7', 1]], "address_auths": [] }, "options": { "memo_key": "BTS5TPTziKkLexhVKsQKtSpo4bAv5RnB8oXcG4sMHEwCcTf3r7dqE", "voting_account": "1.2.5", "num_witness": 0, "num_committee": 0, "votes": [], "extensions": [] }, "extensions": {} } op = operations.Account_create(**s) 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 = ("f68585abf4dce7c804570105f26416000000000000211b03000b666f" "6f6261722d6631323401000000000202fe8cc11cc8251de6977636b5" "5c1ab8a9d12b0b26154ac78e56e7c4257d8bcf6901000314aa202c91" "58990b3ec51a1aa49b2ab5d300c97b391df3beb34bb74f3c62699e01" "000001000000000303b453f46013fdbccb90b09ba169c388c34d8445" "4a3b9fbec68d5a7819a734fca0010002fe8cc11cc8251de6977636b5" "5c1ab8a9d12b0b26154ac78e56e7c4257d8bcf6901000314aa202c91" "58990b3ec51a1aa49b2ab5d300c97b391df3beb34bb74f3c62699e01" "0000024ab336b4b14ba6d881675d1c782912783c43dbbe31693aa710" "ac1896bd7c3d61050000000000000000011f61ad276120bc3f189296" "2bfff7db5e8ce04d5adec9309c80529e3a978a4fa1073225a6d56929" "e34c9d2a563e67a8f4a227e4fadb4a3bb6ec91bfdf4e57b80efd") self.assertEqual(compare[:-130], txWire[:-130])
def randomwif(prefix, num): t = PrettyTable(["wif", "pubkey"]) for n in range(0, num): wif = PrivateKey() t.add_row([str(wif), format(wif.pubkey, prefix)]) click.echo(str(t))
async def process_operations(self, op_id): """ 处理操作 """ op_info = await self.node_api.get_objects([op_id]) for operation in op_info[::-1]: if operation["op"][0] != 0: return # 操作基本信息 trx = {} op = operation['op'][1] trx['trx_id'] = operation['id'] # 获取区块信息 trx['block_num'] = operation['block_num'] block_info = await self.node_api.get_block(trx['block_num']) trx['timestamp'] = block_info['timestamp'] # 获取转账金额 asset = await self.get_asset_info(op['amount']['asset_id']) trx['asset'] = asset['symbol'] trx['asset_id'] = op['amount']['asset_id'] trx['amount'] = float(op['amount']['amount']) / float(10**int( asset['precision'])) # 获取转账手续费 trx['fee'] = {} fee = await self.get_asset_info(op['fee']['asset_id']) trx['fee']['asset'] = fee['symbol'] trx['fee']['asset_id'] = op['fee']['asset_id'] trx['fee']['amount'] = float(op['fee']['amount']) / float(10**int( fee['precision'])) # 获取涉案账户 trx['to_id'] = op['to'] trx['from_id'] = op['from'] trx['to'] = (await self.node_api.get_objects([op['to']]))[0]['name'] trx['from'] = (await self.node_api.get_objects([op['from']]))[0]['name'] # 解码备注信息 if 'memo' in op: memo = op['memo'] trx['nonce'] = memo['nonce'] try: privkey = PrivateKey(self.wifkey) prefix = self.node_api.chain_params['prefix'] if trx['to_id'] == self.account['id']: pubkey = PublicKey(memo['from'], prefix=prefix) else: pubkey = PublicKey(memo['to'], prefix=prefix) trx['memo'] = BtsMemo.decode_memo(privkey, pubkey, memo['nonce'], memo['message']) except Exception: trx['memo'] = None else: trx['memo'] = None trx['nonce'] = None # 触发转账事件 if trx['from_id'] == self.account['id']: await self.on_sent(trx) elif trx['to_id'] == self.account['id']: await self.on_receive(trx)
def generate_webwalletlike_password(): from bitsharesbase.account import PrivateKey random_private_key_asWif = repr(PrivateKey(wif=None)) return ("P" + random_private_key_asWif) [0:45]
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_shared_secret(self): for s in test_shared_secrets: priv = PrivateKey(s[0]) pub = PublicKey(s[1], prefix="GPH") shared_secret = get_shared_secret(priv, pub) self.assertEqual(s[2], shared_secret)
async def _process_transfer_operations(self, client, operation): ''' 处理转账操作 ''' # 筛选操作类型 if operation['op'][0] != self.TRANSFER_OPERATION: return # 操作基本信息 trx = {} op = operation['op'][1] # 获取区块信息 trx['heigth'] = operation['block_num'] block_info = await client.get_block(trx['heigth']) trx['timestamp'] = block_info['timestamp'] # 获取交易ID trx_in_block = operation['trx_in_block'] transaction = block_info['transactions'][trx_in_block] trx['txid'] = await self._get_transaction_id(transaction) # 获取转账金额 asset = await self._get_asset_info(client, op['amount']['asset_id']) trx['asset'] = asset['symbol'] trx['asset_id'] = op['amount']['asset_id'] trx['amount'] = str( float(op['amount']['amount']) / float(10**int(asset['precision']))) # 获取转账手续费 trx['fee'] = {} fee = await self._get_asset_info(client, op['fee']['asset_id']) trx['fee']['asset'] = fee['symbol'] trx['fee']['asset_id'] = op['fee']['asset_id'] trx['fee']['amount'] = str( float(op['fee']['amount']) / float(10**int(fee['precision']))) # 获取涉案账户 trx['to_id'] = op['to'] trx['from_id'] = op['from'] trx['to'] = (await client.get_objects([op['to']]))[0]['name'] trx['from'] = (await client.get_objects([op['from']]))[0]['name'] # 解码备注信息 if 'memo' in op: memo = op['memo'] trx['nonce'] = memo['nonce'] try: privkey = PrivateKey(SysConfig().memo_key) prefix = client.chain_params['prefix'] if trx['to_id'] == self._account['id']: pubkey = PublicKey(memo['from'], prefix=prefix) else: pubkey = PublicKey(memo['to'], prefix=prefix) trx['memo'] = BtsMemo.decode_memo(privkey, pubkey, memo['nonce'], memo['message']) except Exception as e: logging.warn('Failed to decode memo, %s, %s', operation['id'], str(e)) trx['memo'] = None else: trx['memo'] = None trx['nonce'] = None return trx
# trying to access a non-existent field -> probably looking at something we don't want print('invalid transaction for bit20 publication: {}'.format( json.dumps(trx, indent=4))) return False for f in account_history_b20: if not is_valid_bit20_publication(f): print('Hijacking attempt of the bit20 feed? trx: {}'.format( json.dumps(f, indent=4))) continue memo = f['op'][1]['memo'] privkey = PrivateKey(wifkey_announce) pubkey = PublicKey(Account(f['op'][1]['from'])['options']['memo_key']) memomsg = BtsMemo.decode_memo(privkey, pubkey, memo["nonce"], memo["message"]) #print(memomsg) if memomsg.startswith('COMPOSITION'): # last_updated = re.search('\((.*)\)', memomsg) # if last_updated: # last_updated = pendulum.from_format(last_updated.group(1), '%Y/%m/%d') # #print(last_updated) bit20 = json.loads(memomsg.split(')', maxsplit=1)[1]) break else: print(
def getAccountFromPrivateKey(self, wif): """ Obtain account name from private key """ pub = format(PrivateKey(wif).pubkey, self.prefix) return self.getAccountFromPublicKey(pub)
def test_decrypt_bugged_padding(self): for memo in not_enough_padding: dec = decode_memo(PrivateKey(memo["wif"]), PublicKey(memo["to"], prefix="GPH"), memo["nonce"], memo["message"]) self.assertEqual(memo["plain"], dec)
import click import requests from pprint import pprint from bitsharesbase.account import PrivateKey p = PrivateKey("5J539ijxkg4tw7zP95spwek5YqoD39zWp2kKMA6PBqpmukHL6yP") data = { "account": { "name": "test-faucet-21", "owner_key": str(p.pubkey), "active_key": str(p.pubkey), "memo_key": str(p.pubkey), "real_name": "Foobar Hans", "email": "*****@*****.**" } } @click.command() @click.option("--endpoint", default="http://*****:*****@click.argument("name", default="test-faucet-21") def main(endpoint, name): data["account"]["name"] = name pprint(data) ret = requests.post(endpoint + "/api/v1/accounts", json=data)