예제 #1
0
def getwalletfrompkey(request, pkey):
    data = {}
    print(type(pkey))
    wallet = generate_wallet_from_pkey(pkey)
    data["public_key"] = pkey
    data["wallet"] = wallet
    return HttpResponse(json.dumps(data), content_type="application/json")
예제 #2
0
def landing(request):
    try:
        pubkey = request.session['pubkey']
        prikey = request.session['prikey']
        print(pubkey)
        print(pubkey.encode('utf-8'))
        print(type(pubkey.encode('utf-8')))
        wallet_id = generate_wallet_from_pkey(
            pubkey
        )  #hashlib.sha256(pubkey.encode('utf-8')).hexdigest() #SHA256.new(pubkey).hexdigest()
        balance = getbalance(pubkey)
        if balance is None:
            balance = 0
        return render(request, "ok.html", locals())
    except KeyError:
        return render(request, "index.html", locals())
예제 #3
0
def getbalance(pubkey):
    wallet_id = generate_wallet_from_pkey(pubkey)
    outgoing = transaction.objects.filter(sender=pubkey).aggregate(
        Sum('amount'))['amount__sum']
    income = transaction.objects.filter(receiver=wallet_id).aggregate(
        Sum('amount'))['amount__sum']
    # print(outgoing)
    # print(income)

    if income and outgoing:
        # print("user have both")
        return (income - outgoing)
    elif outgoing is None:
        # print("user dont have  outgoing")
        return income
    elif income is None:
        return 0
    else:
        return 0
예제 #4
0
def sendcloudcoin(request):
    allify = {}
    data = {}
    if request.method == 'POST':
        senderprivatekey = request.POST.get('sprikey')
        receiverwallet = request.POST.get('receiverwallet').strip()
        amount = request.POST.get('amount').strip()
        sender = generate_pubkey_from_prikey(senderprivatekey)
        #print(checkreward())
        if not receiverwallet:
            allify['response'] = "fail"
            allify['explain'] = "Please fill the receiver box"
            return HttpResponse(json.dumps(allify),
                                content_type="application/json")
        try:
            amount = int(request.POST.get('amount').strip())
        except ValueError:
            allify['response'] = "fail"
            allify['explain'] = "Please fill the balance box"
            return HttpResponse(json.dumps(allify),
                                content_type="application/json")
        if int(amount) <= 0:
            allify['response'] = "fail"
            allify['explain'] = "insufficient balance"
            return HttpResponse(json.dumps(allify),
                                content_type="application/json")
        balance = getbalance(sender)
        if balance is None:
            balance = 0
        if int(amount) > int(balance):
            allify['response'] = "fail"
            allify['explain'] = "insufficient balance"
            return HttpResponse(json.dumps(allify),
                                content_type="application/json")
        else:
            utc = arrow.utcnow()
            local = utc.to('GMT')
            first_timestamp = local.timestamp
            data['sender'] = str(sender)  #1
            data['receiver'] = str(receiverwallet)  #2
            data['previous_hash'] = str(
                transaction.objects.all().last().blockhash)  #3
            data['amount'] = str(amount)  #4
            data['timestamp'] = str(first_timestamp)  #5
            perfect = miner(first_timestamp, sender, receiverwallet, amount)
            data["nonce"] = str(perfect)
            data = collections.OrderedDict(sorted(data.items()))
            datashash = hashlib.sha256(
                json.dumps(data).encode('utf-8')).hexdigest()
            #print(checkreward())
            try:
                sk = SigningKey.from_string(bytes.fromhex(senderprivatekey),
                                            curve=SECP256k1)
                vk = sk.get_verifying_key()  #public_key
                print(vk.to_string().hex())
            except UnicodeDecodeError:
                data["response"] = "Check your wallet details"
                return HttpResponse(json.dumps(data),
                                    content_type="application/json")
            print("digital sign ishere", datashash.encode('utf-8'))
            digitalSignature = sk.sign(datashash.encode('utf-8'))
            digitalSignature = json.dumps(digitalSignature.hex())

            wllt = generate_wallet_from_pkey(sender)
            newtrans = transaction(
                sender=sender,
                senderwallet=wllt,
                receiver=receiverwallet,
                prevblockhash=transaction.objects.all().last().blockhash,
                blockhash=datashash,
                amount=amount,
                nonce=perfect,
                first_timestamp=first_timestamp,
                P2PKH=digitalSignature,
                verification=True)
            newtrans.save()
            ip = ni.ifaddresses('eth0')[ni.AF_INET][0]['addr']
            geturl = "http://{}/api/v1/gettransaction/{}/".format(
                ip, newtrans.id)
            test = {
                "server": False,
                "sender": sender,
                "receiver": receiverwallet,
                "prevblockhash": transaction.objects.all().last().blockhash,
                "blockhash": datashash,
                "amount": amount,
                "nonce": perfect,
                "timestamp": first_timestamp,
                "P2PKH": digitalSignature,
                "verification": True,
                "block": transaction.objects.all().last().id + 1,
                "message": "new_transaction",
                "url": geturl
            }

            payload = json.dumps(test)

            ws = websocket.WebSocket()
            wsip = "ws://{}:9000".format(ip)
            ws.connect(wsip)
            ws.send(payload)

            allify['response'] = "ok"
            allify['explain'] = "You currency transfered successfuly"

            allify['datashash'] = datashash
            allify['datastring'] = json.dumps(allify)
            return HttpResponse(json.dumps(allify),
                                content_type="application/json")
예제 #5
0
    def onMessage(self, payload, isBinary):
        data = {}
        print("onmessage")
        allify = {}
        if isBinary:
            print("Binary message received: {0} bytes".format(len(payload)))
        else:

            payloaded = json.loads(payload.decode('utf-8'))
            print(payloaded["host"])
            if str(payloaded["host"]) == str(ip):
                print("bu zaten sensin")
            else:

                payloaded = json.loads(payload.decode('utf-8'))
                if 'sender' in payloaded:

                    data['sender'] = str(payloaded["sender"])  #1
                    data['receiver'] = str(payloaded["receiver"])  #2
                    data['previous_hash'] = str(
                        transaction.objects.all().last().blockhash)  #3
                    data['amount'] = str(payloaded["amount"])  #4
                    data['timestamp'] = str(payloaded["timestamp"])  #5
                    data["nonce"] = str(payloaded["nonce"])
                    data = collections.OrderedDict(sorted(data.items()))
                    datashash = hashlib.sha256(
                        json.dumps(data).encode('utf-8')).hexdigest()
                    sig = json.loads(payloaded["P2PKH"])

                    print("datahashhere", datashash.encode('utf-8'))
                    print("sigbyte is here", sig)
                    print("sende weas here", payloaded["sender"])
                    wllt = generate_wallet_from_pkey(payloaded["sender"])
                    if (sig == "reward"):
                        newtrans = transaction(
                            sender=payloaded["sender"],
                            senderwallet=wllt,
                            receiver=payloaded["receiver"],
                            prevblockhash=transaction.objects.all().last(
                            ).blockhash,
                            blockhash=payloaded["blockhash"],
                            amount=Decimal(payloaded["amount"]),
                            nonce=payloaded["nonce"],
                            first_timestamp=payloaded["timestamp"],
                            P2PKH=payloaded["P2PKH"],
                            verification=True).save()
                    else:
                        try:
                            sigbyte = bytes.fromhex(sig)
                            vk = VerifyingKey.from_string(bytes.fromhex(
                                payloaded["sender"]),
                                                          curve=SECP256k1)
                            tt = vk.verify(sigbyte,
                                           datashash.encode('utf-8'))  # True
                        except BadSignatureError:
                            print("unbelieveable")
                            data["response"] = "unbelieveable"
                            newtrans = transaction(
                                sender=payloaded["sender"],
                                senderwallet=wllt,
                                receiver=payloaded["receiver"],
                                prevblockhash=transaction.objects.all().last(
                                ).blockhash,
                                blockhash=payloaded["blockhash"],
                                amount=Decimal(payloaded["amount"]),
                                nonce=payloaded["nonce"],
                                first_timestamp=payloaded["timestamp"],
                                P2PKH=payloaded["P2PKH"],
                                verification=False).save()
                            print("badsignature")

                        newtrans = transaction(
                            sender=payloaded["sender"],
                            senderwallet=wllt,
                            receiver=payloaded["receiver"],
                            prevblockhash=transaction.objects.all().last(
                            ).blockhash,
                            blockhash=payloaded["blockhash"],
                            amount=Decimal(payloaded["amount"]),
                            nonce=payloaded["nonce"],
                            first_timestamp=payloaded["timestamp"],
                            P2PKH=payloaded["P2PKH"],
                            verification=True).save()

                else:
                    print("other message")
                BroadcastServerFactory.broadcast(payloaded)