Esempio n. 1
0
def verify_settle(guid, transactions):
    settle_guid = decode_single(
        "uint256", w3.toBytes(hexstr=transactions[0]["data"][10:]))
    transaction_count = len(transactions)
    return transaction_count == 1 and check_int_uuid(
        settle_guid) and check_uuid(guid) and str(
            UUID(int=settle_guid, version=4)) == guid
Esempio n. 2
0
def submit_request(account, sub=0, aud=0, exp=0, nbf=0, iat=0, wait=False):
    log.out.debug("submit_request: \"Using contract at {}\"".format(
        authority_contract.address))

    # setting header
    alg = w3.toBytes(text="RS256")
    typ = w3.toBytes(text="JWT")
    aud = w3.toBytes(text=aud)

    # hashing request id
    try:
        request_id_bytes = w3.soliditySha3(['address', 'bytes32'],
                                           [account, aud])
    except web3.exceptions.InvalidAddress as e:
        log.out.warning(
            "\033[91mPlease make sure your address has a valid EIP cheksum. Test on etherscan.io and correct in ressources/user_info.json\033[0m"
        )
        exit()

    # casting those filthy bytes to an human-readable hexstr
    request_id = w3.toHex(request_id_bytes)
    request_id = request_id[0:20]
    # Proof that this works
    #log.out.debug(w3.toBytes(hexstr=request_id))
    #log.out.debug(request_id_bytes)

    # submit to blockchain
    txn_hash = authority_contract.functions.addPermissionRequest(
        request_id, alg, typ, aud).transact({'from': account})

    # wait for tx_receipt, could be disabled
    if (wait):
        timer = 50
        tx_receipt = None
        log.out.info(
            "Transaction sent, waiting for mining: max. {} seconds".format(
                timer))
        while (tx_receipt is None and timer > 0):
            tx_receipt = w3.eth.getTransactionReceipt(txn_hash)
            time.sleep(1)
            timer -= 1
        if tx_receipt is not None:
            log.out.info(
                "\033[92mTransaction with request id {} mined!\033[0m".format(
                    request_id))

    return request_id
Esempio n. 3
0
def verify_stake(amount, transactions):
    transaction_count = len(transactions)
    # We should add in address validation later
    (address,
     approved) = decode_single("(address,uint256)",
                               bytes.fromhex(transactions[0]["data"][10:]))
    stake = decode_single("uint256",
                          w3.toBytes(hexstr=transactions[1]["data"][10:]))
    return transaction_count == 2 and check_address(address) and int(
        stake) == int(amount) and int(approved) == int(amount)
Esempio n. 4
0
def verify_vote(guid, verdicts, transactions):
    (vote_guid, vote_verdicts, validBloom) = decode_single(
        "(uint256,uint256,bool)",
        w3.toBytes(hexstr=transactions[0]["data"][10:]))
    verdicts_int = sum([1 << n if b else 0 for n, b in enumerate(verdicts)])
    transaction_count = len(transactions)
    return transaction_count == 1 and check_int_uuid(vote_guid) and check_uuid(
        guid) and str(
            UUID(int=vote_guid,
                 version=4)) == guid and verdicts_int == vote_verdicts
Esempio n. 5
0
def request_token(account, request_id):
    log.out.debug("submit_request: \"Using contract at {}\"".format(
        authority_contract.address))
    log.out.debug("submit_request: \"Using request_id {}".format(request_id))

    # watch submit request
    request_id_bytes = w3.toBytes(hexstr=request_id)

    # Call contract
    alg, typ, iss, sub, aud, iat, nbf, exp, jti, auth_token = authority_contract.functions.permissionList(
        request_id_bytes).call({'from': account})

    # Sorry, web3 don't remove the padding so doing it manually
    typ = typ.split(b'\0', 1)[0]
    alg = alg.split(b'\0', 1)[0]
    aud = aud.split(b'\0', 1)[0]
    auth_token = auth_token.split(b'\0', 1)[0]

    #### Byte-String to text
    typ = w3.toText(typ)
    alg = w3.toText(alg)
    aud = w3.toText(aud)
    auth_token = w3.toText(auth_token)

    token_dict = {
        "header": {
            "typ": typ,
            "alg": alg
        },
        "payload": {
            "iss": iss,
            "sub": sub,
            "aud": aud,
            "exp": exp,
            "nbf": nbf,
            "iat": iat,
            "jti": jti,
        },
        "token": auth_token
    }

    #token_json = json.dumps(token_dict)
    log.out.debug("Token in dict-format: {}".format(token_dict))
    return token_dict
Esempio n. 6
0
def store_token(account, request_id, auth_token, exp, nbf, iat, wait=False):
    token_bytes = w3.toBytes(text=auth_token)
    exp = int(exp)
    nbf = int(nbf)
    iat = int(iat)
    txn_hash = authority_contract.functions.storeToken(
        request_id, token_bytes, exp, nbf, iat).transact({'from': account})

    if (wait):
        timer = 50
        tx_receipt = None
        log.out.info(
            "Transaction sent, waiting for mining: max. {} seconds".format(
                timer))
        while (tx_receipt is None and timer > 0):
            tx_receipt = w3.eth.getTransactionReceipt(txn_hash)
            time.sleep(1)
            timer -= 1
        if tx_receipt is not None:
            log.out.info(
                "\033[92mToken with request id {} mined!\033[0m".format(
                    request_id))
Esempio n. 7
0
 def to_32byte_hex(val):
     return web3.toHex(web3.toBytes(val).rjust(32, b'\0'))
Esempio n. 8
0
def bytes32_to_ipfs(bytes_array):
    """Convert bytes_array into IPFS hash format."""
    merge = w3.toBytes(hexstr='1220') + bytes_array
    return base58.b58encode(merge).decode("utf-8")
Esempio n. 9
0
def ipfs_to_bytes32(ipfs_hash: str) -> str:
    """bytes32 is converted back into Ipfs hash format."""
    ipfs_hash_bytes32 = _ipfs_to_bytes32(ipfs_hash)
    return w3.toBytes(hexstr=ipfs_hash_bytes32)