Example #1
0
def listing_contract(web3, wait_for_transaction, wait_for_block,
                     purchase_lib_contract, listing_registry_contract,
                     eth_test_seller):
    contract_name = 'Listing'
    with open("./contracts/{}.json".format(contract_name)) as f:
        contract_interface = json.loads(f.read())
    wait_for_block(web3)

    CONTRACT_META = {"abi": contract_interface['abi']}

    contract = web3.eth.contract(**CONTRACT_META)
    ipfs_hash = "QmZtQDL4UjQWryQLjsS5JUsbdbn2B27Tmvz2gvLkw7wmmb"
    deploy_txn_hash = \
        listing_registry_contract.functions.create(
            base58_to_hex(ipfs_hash),
            3, 25).transact({'from': eth_test_seller,
                             'gas': 1000000})
    deploy_receipt = wait_for_transaction(web3, deploy_txn_hash)
    assert deploy_receipt["gasUsed"] > 0
    # we better have created one of these
    listings_length = listing_registry_contract.functions.listingsLength(
    ).call()
    assert listings_length > 0
    # get the last listing created
    contract_address = listing_registry_contract.functions.getListing(
        listings_length - 1).call()[0]
    return contract(address=contract_address)
    def verify_twitter(oauth_verifier, eth_address):
        ipfs_helper = IPFSHelper()
        # Verify authenticity of user
        if 'request_token' not in session:
            raise TwitterVerificationError('Session not found.')

        oauth = OAuth1(settings.TWITTER_CONSUMER_KEY,
                       settings.TWITTER_CONSUMER_SECRET,
                       session['request_token']['oauth_token'],
                       session['request_token']['oauth_token_secret'],
                       verifier=oauth_verifier)

        response = requests.post(url=twitter_access_token_url, auth=oauth)

        try:
            response.raise_for_status()
        except requests.exceptions.HTTPError as exc:
            logger.exception(exc)
            raise TwitterVerificationError(
                'The verifier you provided is invalid.')

        query_string = urllib.parse.parse_qs(response.content)
        screen_name = query_string[b'screen_name'][0].decode('utf-8')

        ipfs_hash = ipfs_helper.add_json({
            'schemaId':
            'https://schema.originprotocol.com/twitter-attestation_1.0.0.json',
            'screen_name': screen_name
        })

        signature = attestations.generate_signature(signing_key, eth_address,
                                                    TOPICS['twitter'],
                                                    base58_to_hex(ipfs_hash))

        attestation = Attestation(method=AttestationTypes.TWITTER,
                                  eth_address=eth_address,
                                  value=screen_name,
                                  signature=signature,
                                  remote_ip_address=request.remote_addr)
        db.session.add(attestation)
        db.session.commit()

        return VerificationServiceResponse({
            'signature': signature,
            'claim_type': TOPICS['twitter'],
            'data': ipfs_hash
        })
    def verify_airbnb(eth_address, airbnbUserId):
        ipfs_helper = IPFSHelper()
        validate_airbnb_user_id(airbnbUserId)

        code = get_airbnb_verification_code(eth_address, airbnbUserId)

        try:
            # TODO: determine if this user agent is acceptable.
            # We need to set an user agent otherwise Airbnb returns 403
            response = urlopen(
                Request(
                    url='https://www.airbnb.com/users/show/' + airbnbUserId,
                    headers={'User-Agent': 'Origin Protocol client-0.1.0'}))
        except HTTPError as e:
            if e.code == 404:
                raise AirbnbVerificationError('Airbnb user id: ' +
                                              airbnbUserId + ' not found.')
            else:
                raise AirbnbVerificationError(
                    "Can not fetch user's Airbnb profile.")
        except URLError as e:
            raise AirbnbVerificationError(
                "Can not fetch user's Airbnb profile.")

        if code not in response.read().decode('utf-8'):
            raise AirbnbVerificationError(
                "Origin verification code: " + code +
                " has not been found in user's Airbnb profile.")

        ipfs_hash = ipfs_helper.add_json({
            'schemaId':
            'https://schema.originprotocol.com/airbnb-attestation_1.0.0.json',
            'airbnb_user_id': airbnbUserId
        })
        """ - IPFS hash is a base58 encoded string
            - We store IPFS hashes in solidity claims in bytes32 binary format to minimise
              gas cost.
            - bytes32 is not string serialisable so it can not be transmitted in that form
              from bridge to the DApp
            - bridge needs to transform ipfs hash to bytes32 format (that is how
              it is going to be stored in the contract) before signing the claim, and then
              send IPFS hash to the DApp in base58 string encoding.
            - this way claim has a correct signature if IPFS hash has bytes32 hex encoding
            - the DApp takes signature and other claim info and transforms the base58 encoded
              IPFS hash to base32 hex before submitting the claim to web3.
        """
        signature = attestations.generate_signature(signing_key, eth_address,
                                                    TOPICS['airbnb'],
                                                    base58_to_hex(ipfs_hash))

        attestation = Attestation(method=AttestationTypes.AIRBNB,
                                  eth_address=eth_address,
                                  value=airbnbUserId,
                                  signature=signature,
                                  remote_ip_address=request.remote_addr)
        db.session.add(attestation)
        db.session.commit()

        return VerificationServiceResponse({
            'signature': signature,
            'claim_type': TOPICS['airbnb'],
            'data': ipfs_hash
        })