コード例 #1
0
ファイル: wallet_server.py プロジェクト: hal13/pyblockchain
def create_transaction():
    request_json = request.json
    required = ('sender_private_key', 'sender_blockchain_address',
                'recipient_blockchain_address', 'sender_public_key', 'value')
    if not all(k in request_json for k in required):
        return 'missing value', 400

    sender_private_key = request_json['sender_private_key']
    sender_blockchain_address = request_json['sender_blockchain_address']
    recipient_blockchain_address = request_json['recipient_blockchain_address']
    sender_public_key = request_json['sender_public_key']
    value = float(request_json['value'])

    transaction = wallet.Transaction(sender_private_key, sender_public_key,
                                     sender_blockchain_address,
                                     recipient_blockchain_address, value)

    json_data = {
        'sender_blockchain_address': sender_blockchain_address,
        'recipient_blockchain_address': recipient_blockchain_address,
        'sender_public_key': sender_public_key,
        'value': value,
        'signature': transaction.generate_signature()
    }

    response = requests.post(urllib.parse.urljoin(app.config['gw'],
                                                  'transactions'),
                             json=json_data,
                             timeout=3)

    if response.status_code == 201:
        return jsonify({'message': 'success'}), 201
    return jsonify({'message': 'fail', 'response': response}), 400
コード例 #2
0
def create_transaction():
    request_json = request.json
    required = ("sender_private_key", "sender_blockchain_address",
                "recipient_blockchain_address", "sender_public_key", "value")
    if not all(k in request_json for k in required):
        return "missing values", 400

    sender_private_key = request_json["sender_private_key"]
    sender_blockchain_address = request_json["sender_blockchain_address"]
    recipient_blockchain_address = request_json["recipient_blockchain_address"]
    sender_public_key = request_json["sender_public_key"]
    value = float(request_json["value"])

    # transactionを作る情報は集まった
    transaction = wallet.Transaction(sender_private_key, sender_public_key,
                                     sender_blockchain_address,
                                     recipient_blockchain_address, value)

    json_data = {
        "sender_blockchain_address": sender_blockchain_address,
        "recipient_blockchain_address": recipient_blockchain_address,
        "sender_public_key": sender_public_key,
        "value": value,
        "signature": transaction.generate_signature(),
    }

    # blockchain nodeにリクエストする
    response = requests.post(urllib.parse.urljoin(app.config["gw"],
                                                  "transactions"),
                             json=json_data,
                             timeout=3)

    if response.status_code == 201:
        return jsonify({"message": "success"}), 201
    return jsonify({"message": "fail", "response": response}), 400
コード例 #3
0
def create_transaction():  #TODO
    request_json = request.json
    required = (
        'sender_private_key',
        # 'sender_blockchain_address',
        'recipient_blockchain_address',
        # 'sender_public_key',
        'value',
        'transaction_message')  # 必須事項を示す 1
    if not all(k in request_json for k in required):
        return 'missing values', 400

    sender_private_key = request_json['sender_private_key']
    private_key_bytes = SigningKey.from_string(
        bytes().fromhex(sender_private_key), curve=SECP256k1)
    sender_public_key_string_bytes = bytes.fromhex(
        "04") + private_key_bytes.get_verifying_key().to_string()
    sender_public_key = sender_public_key_string_bytes.hex()
    sender_blockchain_address = wallet.Wallet.generate_blockchain_address(
        sender_public_key_string_bytes)
    recipient_blockchain_address = request_json['recipient_blockchain_address']
    value = float(request_json['value'])
    transaction_message = request_json['transaction_message']

    recipient_blockchain_address_bytes = recipient_blockchain_address.encode()
    network_bitcoin_public_key_bytes = base58.b58decode(
        recipient_blockchain_address_bytes)[:21]
    checksum = base58.b58decode(recipient_blockchain_address_bytes)[21:]
    if checksum != wallet.Wallet.create_checksum(
            network_bitcoin_public_key_bytes):
        return 'missing recipient_blockchain_address', 400

    transaction = wallet.Transaction(sender_private_key, sender_public_key,
                                     sender_blockchain_address,
                                     recipient_blockchain_address, value,
                                     transaction_message)

    json_data = {
        'sender_blockchain_address': sender_blockchain_address,
        'recipient_blockchain_address': recipient_blockchain_address,
        'sender_public_key': sender_public_key,
        'value': value,
        'transaction_message': transaction_message,
        'signature': transaction.generate_signature(),
    }

    response = requests.post(urllib.parse.urljoin(app.config['gw'],
                                                  'transactions'),
                             json=json_data,
                             timeout=3)

    if response.status_code == 201:
        return jsonify({'message': 'success'}), 201
    return jsonify({'message': 'fail', 'response': response}), 400