示例#1
0
文件: blocks.py 项目: maxide/Bank-1
def send_signed_block(*, block, ip_address, port, protocol, url_path):
    """
    Sign block and send to recipient
    """

    signing_key = get_signing_key()
    node_identifier = get_verify_key(signing_key=signing_key)
    node_identifier = encode_verify_key(verify_key=node_identifier)
    message = sort_and_encode(block)

    signed_block = {
        'block': block,
        'node_identifier': node_identifier,
        'signature': generate_signature(message=message,
                                        signing_key=signing_key)
    }
    node_address = format_address(ip_address=ip_address,
                                  port=port,
                                  protocol=protocol)
    url = f'{node_address}{url_path}'

    try:
        post(url=url, body=signed_block)
    except Exception as e:
        request_new_primary_validator()
        logger.exception(e)
示例#2
0
def send_signed_block(*, block, ip_address, port, protocol, url_path):
    """
    Sign block and send to recipient
    """

    network_signing_key = get_environment_variable('NETWORK_SIGNING_KEY')
    signing_key = SigningKey(network_signing_key, encoder=HexEncoder)
    node_identifier = get_verify_key(signing_key=signing_key)
    node_identifier = encode_verify_key(verify_key=node_identifier)
    message = sort_and_encode(block)

    signed_block = {
        'block': block,
        'node_identifier': node_identifier,
        'signature': generate_signature(message=message,
                                        signing_key=signing_key)
    }

    node_address = format_address(ip_address=ip_address,
                                  port=port,
                                  protocol=protocol)
    url = f'{node_address}{url_path}'

    try:
        post(url=url, body=signed_block)
    except Exception as e:
        logger.exception(e)
示例#3
0
def generate_signed_request(*, data, nid_signing_key):
    """Generate and return signed request"""
    node_identifier = get_verify_key(signing_key=nid_signing_key)
    signature = generate_signature(
        message=sort_and_encode(data),
        signing_key=nid_signing_key
    )
    return {
        'message': data,
        'node_identifier': encode_verify_key(verify_key=node_identifier),
        'signature': signature
    }
示例#4
0
def signed_block(block_data, bank_signing_key):
    yield {
        'block':
        block_data,
        'node_identifier':
        encode_verify_key(verify_key=get_verify_key(
            signing_key=bank_signing_key, ), ),
        'signature':
        generate_signature(
            message=sort_and_encode(block_data),
            signing_key=bank_signing_key,
        ),
    }
示例#5
0
def generate_block(*, account_number, balance_lock, signing_key, transactions):
    """Generate block"""
    message = {
        'balance_key': balance_lock,
        'txs': sorted(transactions, key=itemgetter('recipient'))
    }

    signature = generate_signature(message=sort_and_encode(message),
                                   signing_key=signing_key)

    block = {
        'account_number': encode_verify_key(verify_key=account_number),
        'message': message,
        'signature': signature
    }

    return block
示例#6
0
def run(send_to_pv=False):
    """
    Create block used for:
    - POST /bank_blocks
    - Bank > PV
    """

    treasury_signing_key = read_signing_key_file(
        os.path.join(SIGNING_KEY_DIR, 'treasury'))
    account_number = get_verify_key(signing_key=treasury_signing_key)

    balance_lock = get_account_balance_lock(
        account_number=TREASURY_ACCOUNT_NUMBER, live_pv=True)
    transactions = [{
        'amount': BANK_TX_FEE,
        'recipient': BANK_ACCOUNT_NUMBER,
    }, {
        'amount': PV_TX_FEE,
        'recipient': PV_ACCOUNT_NUMBER,
    }, {
        'amount': 1.0,
        'recipient': BUCKY_ACCOUNT_NUMBER,
    }]
    block = generate_block(account_number=account_number,
                           balance_lock=balance_lock,
                           signing_key=treasury_signing_key,
                           transactions=transactions)

    bank_nid_sk = read_signing_key_file(
        os.path.join(SIGNING_KEY_DIR, 'bank_nid'))
    bank_nid = get_verify_key(signing_key=bank_nid_sk)
    bank_nid = encode_verify_key(verify_key=bank_nid)
    message = sort_and_encode(block)

    signed_block = {
        'block': block,
        'node_identifier': bank_nid,
        'signature': generate_signature(message=message,
                                        signing_key=bank_nid_sk)
    }

    write_json(os.path.join(BLOCKS_DIR, 'bank-blocks-request.json'),
               signed_block)

    if send_to_pv:
        send_request_to_pv(signed_block)