当前位置: 首页>>代码示例>>Python>>正文


Python Transaction.hash方法代码示例

本文整理汇总了Python中transaction.Transaction.hash方法的典型用法代码示例。如果您正苦于以下问题:Python Transaction.hash方法的具体用法?Python Transaction.hash怎么用?Python Transaction.hash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在transaction.Transaction的用法示例。


在下文中一共展示了Transaction.hash方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from transaction import Transaction [as 别名]
# 或者: from transaction.Transaction import hash [as 别名]
def main():
    message = None

    # Parse arguments first
    i = 1
    while i < len(sys.argv):
        v = sys.argv[1]
        if v == '-f':
            assert message is None, "you can only specify -f once"
            i += 1
            data = open(sys.argv[i], 'rb').read()
            mime_type, encoding = mimetypes.guess_type(sys.argv[i])

            if mime_type is not None:
                filename = os.path.basename(sys.argv[i])
                message = '\n'.join([
                    'Content-type: {}{}'.format(mime_type, '; charset={}'.format(encoding) if encoding is not None else ''),
                    'Content-length: {}'.format(len(data)),
                    'Content-disposition: attachment; filename={}'.format(filename),
                ])
                message = message.encode('utf8') + b'\n\n' + data

        i += 1

    # Get coins for input
    print('*** Step 1. We need Bitcoins in order to send a message. Give me a Bitcoin private key (it starts with a 5...) to use as an input for the message transaction.')
    bitcoin_private_key = input('...Enter Bitcoin private key: ')

    # Decode private key, show bitcoin address associated with key
    private_key = base58.decode_to_bytes(bitcoin_private_key)[-36:-4]
    public_key = addressgen.get_public_key(private_key)
    bitcoin_input_address = addressgen.generate_address(public_key, version=0)
    print('...The Bitcoin address associated with that private key is: {}'.format(bitcoin_input_address))

    # Lookup the unspent outputs associated with the given input...
    print('...Looking up unspent outputs on blockchain.info...')
    unspent_outputs = filter_unspent_outputs(lookup_unspent_outputs([bitcoin_input_address])[bitcoin_input_address])

    # Show the inputs to the user, and ask him which he'd like to use as input.
    print('\n*** Step 2. You need to select an input:')
    for k, u in enumerate(unspent_outputs):
        print('...{}: txid={} n={} value={} confirmations={}'.format(k+1, u['tx_hash'], u['tx_output_n'], Bitcoin.format_money(u['value']), u['confirmations']))
    selected_inputs = [int(x.strip())-1 for x in input('Enter inputs (if more than one, separated by commas): ').split(',') if len(x) > 0]
    if not all(x >= 0 and x < len(unspent_outputs) for x in selected_inputs):
        raise Exception("Invalid input provided")
    total_input_amount = sum(unspent_outputs[k]['value'] for k in selected_inputs)
    print('...{} BTC selected from {} input{}'.format(Bitcoin.format_money(total_input_amount), len(selected_inputs), 's' if len(selected_inputs) != 1 else ''))

    # Ask the user for the change address, defaulting to the same input address
    print('\n*** Step 3. Provide a change address (this will not be used if a change address isn\'t necessary)')
    bitcoin_change_address = input('...Enter change address (leave blank to use the input address as the change address): ').strip()
    if len(bitcoin_change_address) == 0:
        bitcoin_change_address = bitcoin_input_address
        print('...Change address: {}'.format(bitcoin_change_address))

    # Select an encryption method
    while True:
        print('\n*** Step 4a. Select an encryption method:')
        print('...1. None (public message)')
        print('...2. RC4')
        print('...3. AES-128')
        print('...4. AES-256 (best)')
        print('...5. RSA (public-key)')
        try:
            i = int(input('? '))
            if i == 1:
                encryption_key = b'\x00'
                encryption_algorithm = ENCRYPT_NONE
            elif i == 2:
                required_key_length_message = "RC4 allows for variable-length keys, but longer is better"
                encryption_algorithm = ENCRYPT_RC4
            elif i == 3:
                required_key_length_message = "AES-128 requires a key length of 16 bytes"
                encryption_algorithm = ENCRYPT_AES128
            elif i == 4:
                required_key_length_message = "AES-256 requires a key length of 32 bytes"
                encryption_algorithm = ENCRYPT_AES256
            elif i == 5:
                required_key_length_message = "An RSA public-key is required"
                encryption_algorithm = ENCRYPT_RSA
            else:
                continue
            break
        except ValueError:
            continue

    if encryption_algorithm in (ENCRYPT_AES128, ENCRYPT_AES256, ENCRYPT_RC4):
        print('\n*** Step 4b. Provide an encryption key. The encryption key will be hashed and used as a Bitcoin address to designate the recipient.')
        encryption_key = input('...Enter an encryption key ({}): '.format(required_key_length_message)).encode('utf8')
        if encryption_algorithm == ENCRYPT_AES128 and len(encryption_key) != 16:
            print('...ERROR: key must have a length of 16 bytes.')
            return
        elif encryption_algorithm == ENCRYPT_AES256 and len(encryption_key) != 32:
            print('...ERROR: key must have a length of 32 bytes.')
            return
        elif encryption_algorithm == ENCRYPT_RC4 and len(encryption_key) == 0:
            print('...ERROR: key must not be empty')
            return
    elif encryption_algorithm == ENCRYPT_RSA:
        encryption_key = get_random_bytes(32)
#.........这里部分代码省略.........
开发者ID:Sphere2013,项目名称:Bitmsg,代码行数:103,代码来源:buildmessage.py


注:本文中的transaction.Transaction.hash方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。