def main(): """Receive seeds and add them as prioritizers to whitelist account.""" args = parse_args() env = KinEnvironment(NETWORK_NAME, args.horizon, args.passphrase) prioritizer_kps = load_accounts(args.prioritizer_seeds_file) whitelist_builder = Builder(env.name, KinClient(env).horizon, MIN_FEE, args.whitelist_seed) add_prioritizers(whitelist_builder, prioritizer_kps)
def initialize(): if len(sys.argv) < 4: print('invalid number of params :', len(clients)) sys.exit(1) global PASSPHRASE global WHITELIST_MANAGER_KEYPAIR global root_account global tx_count PASSPHRASE = sys.argv[1] WHITELIST_MANAGER_KEYPAIR = Keypair(sys.argv[2]) tx_count = int(sys.argv[3]) for arg in sys.argv[4:]: local_env = Environment('LOCAL', arg, PASSPHRASE, 'http://localhost:8001') # Creates a client clients.append(KinClient(local_env)) print('Client ', len(clients), ' created!') # Create the root account object root_account = clients[0].kin_account( derive_root_account(PASSPHRASE).secret_seed) print('Root account object created')
def init_tx_builders(env, kps, sequences): """Initialize transaction builders for each given seed.""" builders = [] for i, kp in enumerate(kps): client = KinClient(env) builder = Builder(env.name, client.horizon, MIN_FEE, kp.secret_seed) builder.sequence = sequences[i] builders.append(builder) return builders
def create_whitelist_account(): """Create whitelist account for prioritizing transactions in tests.""" print('Creating whitelist account') env = KinEnvironment('LOCAL', HORIZON_ENDPOINT, PASSPHRASE) root_client = KinClient(env) root_seed = derive_root_account_seed(PASSPHRASE) builder = Builder(env.name, root_client.horizon, 100, root_seed) builder.append_create_account_op(WHITELIST_ADDRESS, str(100e5)) builder.sign() builder.submit()
async def main(): """Create accounts and print their seeds to stdout.""" args = parse_args() env = Environment(NETWORK_NAME, (args.horizon)[0], args.passphrase) builder = Builder(NETWORK_NAME, KinClient(env).horizon, MIN_FEE, root_account_seed(args.passphrase)) builder.sequence = builder.get_sequence() kps = await create_accounts(builder, args.horizon, args.accounts, STARTING_BALANCE) for kp in kps: print(kp.secret_seed)
async def init_channel_builders(channel_seeds_file, passphrase, horizon): env = Environment(NETWORK_NAME, (horizon)[0], passphrase) channel_kps = load_accounts(channel_seeds_file) sequences = await get_sequences_multiple_endpoints(horizon, [kp.public_address for kp in channel_kps]) channel_builders = [] for i, kp in enumerate(channel_kps): b = Builder(NETWORK_NAME, KinClient(env).horizon, MIN_FEE, kp.secret_seed) b.sequence = str(sequences[i]) channel_builders.append(b) return channel_builders
def create_whitelist_account(): """Create whitelist account for prioritizing transactions in tests.""" print('Creating whitelist account') env = KinEnvironment('LOCAL', 'http://localhost:8000', 'private testnet') root_client = KinClient(env) root_seed = derive_root_account_seed('private testnet') builder = Builder(env.name, root_client.horizon, 100, root_seed) builder.append_create_account_op( 'GBR7K7S6N6C2A4I4URBXM43W7IIOK6ZZD5SAGC7LBRCUCDMICTYMK4LO', str(100e5)) builder.sign() builder.submit()
async def main(): # Create the environment local_env = Environment('LOCAL', 'http://localhost:8000', PASSPHRASE, 'http://localhost:8001') # Create a client client = KinClient(local_env) print('Client created') initial_ledger_size = get_latest_ledger(client)['max_tx_set_size'] try: # Set ledger tx size to 1 requests.get( 'http://localhost:11626/upgrades?mode=set&maxtxsize=1&upgradetime=2018-10-15T18:34:00Z' ) # Create the root account object root_account = client.kin_account( derive_root_account(PASSPHRASE).secret_seed) print('Root account object created') minimum_fee = client.get_minimum_fee() # Create an account with 0 base reserve test_account = Keypair() root_account.create_account(test_account.public_address, 0, minimum_fee) assert client.does_account_exists(test_account.public_address) print('Test account created') # Add the account to the whitelist if not client.does_account_exists( WHITELIST_MANAGER_KEYPAIR.public_address): root_account.create_account( WHITELIST_MANAGER_KEYPAIR.public_address, 10000, 100) print('Created whitelisting account') builder = Builder('LOCAL', client.horizon, fee=minimum_fee, secret=WHITELIST_MANAGER_KEYPAIR.secret_seed) builder.get_sequence() builder.append_manage_data_op(test_account.public_address, test_account._hint) builder.sign() builder.submit() print('Added account to whitelist') for _ in range(5): txs = [] first_builder = Builder('LOCAL', client.horizon, fee=minimum_fee, secret=test_account.secret_seed) first_builder.append_manage_data_op('test', 'test'.encode()) first_builder.get_sequence() first_builder.sign() txs.append(first_builder) second_builder = Builder( 'LOCAL', client.horizon, fee=minimum_fee, secret=WHITELIST_MANAGER_KEYPAIR.secret_seed) second_builder.append_payment_op(test_account.public_address, '1') second_builder.get_sequence() second_builder.sign() txs.append(second_builder) initial_ledger = get_latest_ledger(client)['sequence'] print(f'Initial ledger: {initial_ledger}') while initial_ledger == get_latest_ledger(client)['sequence']: time.sleep(0.5) first_populated_ledger = initial_ledger + 2 print(f'Sending on ledger: {first_populated_ledger}') print(f'Sending txs at {time.strftime("%d/%m/%Y %H:%M:%S")}') await send_txs(txs) print(f'Done sending txs at {time.strftime("%d/%m/%Y %H:%M:%S")}') first_ledger_txs = client.horizon.ledger_transactions( first_populated_ledger)['_embedded']['records'] # First ledger should have tx from whitelist_manager assert WHITELIST_MANAGER_KEYPAIR.public_address == first_ledger_txs[ 0]['source_account'] print('Verified tx from whitelist manager got priority') except: raise finally: # Set tx size to what it was before requests.get( f'http://localhost:11626/upgrades?mode=set&maxtxsize={initial_ledger_size}&upgradetime=2018-10-15T18:34:00Z' )
def main(): # Create the environment local_env = Environment('LOCAL', 'http://localhost:8000', PASSPHRASE, 'http://localhost:8001') # Create a client client = KinClient(local_env) print('Client created') # Create the root account object root_account = client.kin_account( derive_root_account(PASSPHRASE).secret_seed) print('Root account object created') minimum_fee = client.get_minimum_fee() # Create an account with 0 base reserve test_account = Keypair() root_account.create_account(test_account.public_address, 0, minimum_fee) assert client.does_account_exists(test_account.public_address) print('Test account created') # Send a tx that does not require spending kin builder = Builder('LOCAL', client.horizon, fee=0, secret=test_account.secret_seed) builder.get_sequence() builder.append_manage_data_op('test', 'test'.encode()) builder.sign() try: builder.submit() except KinErrors.HorizonError as e: # Tx should fail since the fee is under minimum fee assert e.extras.result_codes.transaction == KinErrors.TransactionResultCode.INSUFFICIENT_FEE print('Sending under minimum fee - Passed') builder = Builder('LOCAL', client.horizon, fee=minimum_fee, secret=test_account.secret_seed) builder.get_sequence() builder.append_manage_data_op('test', 'test'.encode()) builder.sign() try: builder.submit() except KinErrors.HorizonError as e: # Tx should fail since the account cant pay the fee assert e.extras.result_codes.transaction == KinErrors.TransactionResultCode.INSUFFICIENT_BALANCE print('Sending with no fee to pay - Passed') # Add the account to the whitelist if not client.does_account_exists( WHITELIST_MANAGER_KEYPAIR.public_address): root_account.create_account(WHITELIST_MANAGER_KEYPAIR.public_address, 10000, 100) print('Created whitelisting account') builder = Builder('LOCAL', client.horizon, fee=minimum_fee, secret=WHITELIST_MANAGER_KEYPAIR.secret_seed) builder.get_sequence() builder.append_manage_data_op(test_account.public_address, test_account._hint) builder.sign() builder.submit() print('Added account to whitelist') # test_account is now a whitelister, so the fee should be ignored builder = Builder('LOCAL', client.horizon, fee=0, secret=test_account.secret_seed) builder.get_sequence() builder.append_manage_data_op('test2', 'test2'.encode()) builder.sign() builder.submit() print('Sending prioritized tx under minimum fee - Passed') builder = Builder('LOCAL', client.horizon, fee=999999, secret=test_account.secret_seed) builder.get_sequence() builder.append_manage_data_op('test3', 'test3'.encode()) builder.sign() builder.submit() print('Sending prioritized tx with fee > balance - Passed') # Try the same if the account is not a whitelister, but the tx is whitelisted test_account2 = Keypair() root_account.create_account(test_account2.public_address, 0, minimum_fee) builder = Builder('LOCAL', client.horizon, fee=999999, secret=test_account2.secret_seed) builder.get_sequence() builder.append_manage_data_op('test', 'test'.encode()) builder.sign() # sign with the whitelister as well to prioritize the tx builder.sign(secret=test_account.secret_seed) builder.submit() print('Sending prioritized tx2 with fee > balance - Passed')
async def main(): # Create the environment local_env = Environment('LOCAL', 'http://localhost:8000', PASSPHRASE, 'http://localhost:8001') # Create a client client = KinClient(local_env) print('Client created') initial_ledger_size = get_latest_ledger(client)['max_tx_set_size'] try: # Set ledger tx size to 3 requests.get( 'http://localhost:11626/upgrades?mode=set&maxtxsize=3&upgradetime=2018-10-15T18:34:00Z' ) # Create the root account object root_account = client.kin_account( derive_root_account(PASSPHRASE).secret_seed) print('Root account object created') minimum_fee = client.get_minimum_fee() # Create an account with 0 base reserve test_account = Keypair() root_account.create_account(test_account.public_address, 0, minimum_fee) assert client.does_account_exists(test_account.public_address) print('Test account created') accounts = [Keypair() for _ in range(5)] builder = Builder('LOCAL', client.horizon, fee=minimum_fee, secret=root_account.keypair.secret_seed) for keypair in accounts: builder.append_create_account_op(keypair.public_address, '100') builder.get_sequence() builder.sign() builder.submit() print('Created 5 accounts') txs = [] for index, account in enumerate(accounts, start=1): builder = Builder('LOCAL', client.horizon, fee=minimum_fee * index, secret=account.secret_seed) builder.append_manage_data_op('test', 'test'.encode()) builder.get_sequence() builder.sign() txs.append(builder) initial_ledger = get_latest_ledger(client)['sequence'] print(f'Initial ledger: {initial_ledger}') while initial_ledger == get_latest_ledger(client)['sequence']: time.sleep(0.5) first_populated_ledger = initial_ledger + 2 second_populated_ledger = initial_ledger + 3 print(f'Sending on ledger: {first_populated_ledger}') print(f'Sending txs at {time.strftime("%d/%m/%Y %H:%M:%S")}') await send_txs(txs) print(f'Done sending txs at {time.strftime("%d/%m/%Y %H:%M:%S")}') first_ledger_txs = client.horizon.ledger_transactions( first_populated_ledger)['_embedded']['records'] second_ledger_txs = client.horizon.ledger_transactions( second_populated_ledger)['_embedded']['records'] # First ledger should have txs where fee>=300 first_txs = sum(1 for tx in first_ledger_txs if tx['fee_paid'] >= 300) assert first_txs == 3 print('Verified first ledger') # Second ledger should have txs where fee<=200 second_txs = sum(1 for tx in second_ledger_txs if tx['fee_paid'] <= 200) assert second_txs == 2 print('Verified seconds ledger') except: raise finally: # Set tx size to what it was before requests.get( f'http://localhost:11626/upgrades?mode=set&maxtxsize={initial_ledger_size}&upgradetime=2018-10-15T18:34:00Z' )
async def init_kin(): client = KinClient(kin_env) channels = get_hd_channels(simple_transfer_seed, "", 100) account = client.kin_account(seed=simple_transfer_seed, channel_secret_keys=channels, app_id=app_id) return client, account
class Blockchain(object): read_sdk = KinClient(STELLAR_ENV) minimum_fee = read_sdk.get_minimum_fee() def __init__(self, sdk: KinAccount, channel: str): self.write_sdk = sdk self.write_sdk.raw_seed = BaseKeypair.from_seed( sdk.keypair.secret_seed).raw_seed() self.root_address = self.write_sdk.keypair.public_address self.channel = channel self.channel_address = Keypair.address_from_seed(channel) def create_wallet(self, public_address: str) -> str: """create a wallet.""" try: account_exists = self.get_wallet(public_address) except Exception as e: log.info('failed checking wallet state', public_address=public_address) else: if account_exists: raise AccountExistsError log.info('creating wallet', public_address=public_address) # We use 'build_create_account' instead of 'create_account' since we have our method of managing seeds in redis builder = self.write_sdk.build_create_account(public_address, 0, self.minimum_fee) tx_id = self._sign_and_send_tx(builder) log.info('create wallet transaction', tx_id=tx_id) return tx_id def pay_to(self, public_address: str, amount: int, payment_id: str) -> str: """send kins to an address.""" log.info('sending kin to', address=public_address) # We use 'build_send_kin' instead of 'send_kin' since we have our method of managing seeds in redis builder = self.write_sdk.build_send_kin(public_address, amount, fee=self.minimum_fee, memo_text=payment_id) return self._sign_and_send_tx(builder) def submit_transaction(self, transaction): builder = root_account.get_transaction_builder(0) builder.import_from_xdr(transaction) builder.sign(self.write_sdk.keypair.secret_seed) return self.write_sdk.submit_transaction(builder) @staticmethod def get_wallet(public_address: str) -> Wallet: try: data = Blockchain.read_sdk.get_account_data(public_address) return Wallet.from_blockchain(data) except AccountNotFoundError: raise WalletNotFoundError('wallet %s not found' % public_address) @staticmethod def get_transaction_data(tx_id) -> SimplifiedTransaction: return Blockchain.read_sdk.get_transaction_data(tx_id) @staticmethod def get_payment_data(tx_id) -> Payment: return Payment.from_blockchain(Blockchain.get_transaction_data(tx_id)) @staticmethod def try_parse_payment(tx_data) -> Payment: """try to parse payment from given tx_data. return None when failed.""" try: return Payment.from_blockchain(tx_data) # except ParseError as e: # todo Ron: Why was this removed # log.exception('failed to parse payment', tx_data=tx_data) # return except Exception as e: log.exception('failed to parse payment', tx_data=tx_data) @staticmethod def get_address_records(address, cursor, limit=100) -> List[TransactionRecord]: log.debug('getting records from', address=address, cursor=cursor) reply = Blockchain.read_sdk.horizon.account_payments(address=address, params={ 'cursor': cursor, 'order': 'asc', 'limit': limit }) records = [ TransactionRecord(r, strict=False) for r in reply['_embedded']['records'] ] return records @staticmethod def get_all_records(cursor, limit=100) -> List[TransactionRecord]: log.debug('getting records from', cursor=cursor) reply = Blockchain.read_sdk.horizon.payments(params={ 'cursor': cursor, 'order': 'asc', 'limit': limit }) records = [ TransactionRecord(r, strict=False) for r in reply['_embedded']['records'] ] return records @staticmethod def get_last_cursor(): reply = Blockchain.read_sdk.horizon.payments(params={ 'cursor': 'now', 'order': 'desc', 'limit': 1 }) return reply['_embedded']['records'][0]['paging_token'] def _sign_and_send_tx(self, builder) -> str: builder.set_channel(self.channel) builder.sign(self.channel) if self.channel != self.write_sdk.keypair.secret_seed: builder.sign(self.write_sdk.keypair.secret_seed) tx_id = self.write_sdk.submit_transaction(builder) return tx_id
async def main(): # Create the environment local_env = Environment('LOCAL', 'http://localhost:8000', PASSPHRASE, 'http://localhost:8001') # Create a client client = KinClient(local_env) print('Client created') initial_ledger_size = get_latest_ledger(client)['max_tx_set_size'] try: # Set ledger tx size to 3 requests.get( 'http://localhost:11626/upgrades?mode=set&maxtxsize=3&upgradetime=2018-10-15T18:34:00Z' ) # Create the root account object root_account = client.kin_account( derive_root_account(PASSPHRASE).secret_seed) print('Root account object created') minimum_fee = client.get_minimum_fee() # Create an account with 0 base reserve test_account = Keypair() root_account.create_account(test_account.public_address, 0, minimum_fee) assert client.does_account_exists(test_account.public_address) print('Test account created') # Add the account to the whitelist if not client.does_account_exists( WHITELIST_MANAGER_KEYPAIR.public_address): root_account.create_account( WHITELIST_MANAGER_KEYPAIR.public_address, 10000, 100) print('Created whitelisting account') builder = Builder('LOCAL', client.horizon, fee=minimum_fee, secret=WHITELIST_MANAGER_KEYPAIR.secret_seed) builder.get_sequence() builder.append_manage_data_op(test_account.public_address, test_account._hint) builder.sign() builder.submit() print('Added account to whitelist') accounts = [Keypair() for _ in range(5)] builder = Builder('LOCAL', client.horizon, fee=minimum_fee, secret=root_account.keypair.secret_seed) for keypair in accounts: builder.append_create_account_op(keypair.public_address, '100') builder.get_sequence() builder.sign() builder.submit() print('Created 5 accounts') txs = [] for account in accounts: builder = Builder('LOCAL', client.horizon, fee=minimum_fee, secret=account.secret_seed) builder.append_manage_data_op('test', 'test'.encode()) builder.get_sequence() builder.sign() txs.append(builder) for tx in txs[2:]: tx.sign(test_account.secret_seed) print('Whitelisted 3 transactions') initial_ledger = get_latest_ledger(client)['sequence'] print(f'Initial ledger: {initial_ledger}') while initial_ledger == get_latest_ledger(client)['sequence']: time.sleep(0.5) first_populated_ledger = initial_ledger + 2 second_populated_ledger = initial_ledger + 3 print(f'Sending on ledger: {first_populated_ledger}') print(f'Sending txs at {time.strftime("%d/%m/%Y %H:%M:%S")}') await send_txs(txs) print(f'Done sending txs at {time.strftime("%d/%m/%Y %H:%M:%S")}') first_ledger_txs = client.horizon.ledger_transactions( first_populated_ledger)['_embedded']['records'] second_ledger_txs = client.horizon.ledger_transactions( second_populated_ledger)['_embedded']['records'] # First ledger should have 2 whitelisted txs, and 1 non-whitelisted whitelisted_amount = sum(1 for tx in first_ledger_txs if len(tx['signatures']) == 2) assert whitelisted_amount == 2 regular_amount = sum(1 for tx in first_ledger_txs if len(tx['signatures']) == 1) assert regular_amount == 1 # # Second ledger should have 1 whitelisted tx, and 1 non-whitelisted whitelisted_amount = sum(1 for tx in second_ledger_txs if len(tx['signatures']) == 2) assert whitelisted_amount == 1 regular_amount = sum(1 for tx in second_ledger_txs if len(tx['signatures']) == 1) assert regular_amount == 1 except: raise finally: # Set tx size to what it was before requests.get( f'http://localhost:11626/upgrades?mode=set&maxtxsize={initial_ledger_size}&upgradetime=2018-10-15T18:34:00Z' )
async def main(): # Create the environment local_env = Environment('LOCAL', 'http://localhost:8000', PASSPHRASE, 'http://localhost:8001') # Create a client client = KinClient(local_env) print('Client created') initial_ledger_size = get_latest_ledger(client)['max_tx_set_size'] try: # Set ledger tx size to 1 requests.get( 'http://localhost:11626/upgrades?mode=set&maxtxsize=1&upgradetime=2018-10-15T18:34:00Z' ) # Create the root account object root_account = client.kin_account( derive_root_account(PASSPHRASE).secret_seed) print('Root account object created') minimum_fee = client.get_minimum_fee() # Create an account with 0 base reserve test_account = Keypair() root_account.create_account(test_account.public_address, 0, minimum_fee) assert client.does_account_exists(test_account.public_address) print('Test account created') # Add the account to the whitelist if not client.does_account_exists( WHITELIST_MANAGER_KEYPAIR.public_address): root_account.create_account( WHITELIST_MANAGER_KEYPAIR.public_address, 10000, 100) print('Created whitelisting account') initial_ledger = get_latest_ledger(client)['sequence'] print(f'Adding account to whitelist on ledger: {initial_ledger + 2}') builder = Builder('LOCAL', client.horizon, fee=minimum_fee, secret=WHITELIST_MANAGER_KEYPAIR.secret_seed) builder.get_sequence() builder.append_manage_data_op(test_account.public_address, test_account._hint) builder.sign() builder.submit() print('Added account to whitelist') while initial_ledger + 1 == get_latest_ledger(client)['sequence']: time.sleep(0.5) print( f'Submitting tx from test account on ledger: {initial_ledger + 3}') builder = Builder('LOCAL', client.horizon, fee=0, secret=test_account.secret_seed) builder.append_manage_data_op('test', 'test'.encode()) builder.get_sequence() builder.sign() builder.submit() while initial_ledger + 2 == get_latest_ledger(client)['sequence']: time.sleep(0.5) populated_ledger_txs = client.horizon.ledger_transactions( initial_ledger + 3)['_embedded']['records'] assert len(populated_ledger_txs) == 1 assert populated_ledger_txs[0]['fee_paid'] == 0 except: raise finally: # Set tx size to what it was before requests.get( f'http://localhost:11626/upgrades?mode=set&maxtxsize={initial_ledger_size}&upgradetime=2018-10-15T18:34:00Z' )
async def init_kin(): client = KinClient(kin_env) account = client.kin_account(seed, app_id=unique_app_id) return client, account