예제 #1
0
def get_signature_vrs(raw):
    try:
        hashed_raw = sha256(raw)
        wallet = get_aquarius_wallet()

        keys_pk = keys.PrivateKey(wallet.key)

        prefix = "\x19Ethereum Signed Message:\n32"
        signable_hash = Web3.solidityKeccak(
            ["bytes", "bytes"],
            [Web3.toBytes(text=prefix),
             Web3.toBytes(hashed_raw.digest())],
        )
        signed = keys.ecdsa_sign(message_hash=signable_hash,
                                 private_key=keys_pk)

        values = {
            "hash": "0x" + hashed_raw.hexdigest(),
            "publicKey": wallet.address
        }

        values["v"] = (signed.v + 27) if signed.v <= 1 else signed.v
        values["r"] = (Web3.toHex(Web3.toBytes(signed.r).rjust(32, b"\0")), )
        values["s"] = (Web3.toHex(Web3.toBytes(signed.s).rjust(32, b"\0")), )
    except AquariusPrivateKeyException:
        values = {"hash": "", "publicKey": "", "r": "", "s": "", "v": ""}

    return values
예제 #2
0
def generate_multi_value_hash(types: List[str], values: List[str]) -> HexBytes:
    """
    Return the hash of the given list of values.
    This is equivalent to packing and hashing values in a solidity smart contract
    hence the use of `soliditySha3`.

    :param types: list of solidity types expressed as strings
    :param values: list of values matching the `types` list
    :return: bytes
    """
    assert len(types) == len(values)
    return Web3.solidityKeccak(types, values)
예제 #3
0
def get_signature_bytes(raw):
    try:
        wallet = get_aquarius_wallet()

        keys_pk = keys.PrivateKey(wallet.key)
        message_hash = Web3.solidityKeccak(
            ["bytes"],
            [Web3.toBytes(text=raw)],
        )
        prefix = "\x19Ethereum Signed Message:\n32"
        signable_hash = Web3.solidityKeccak(
            ["bytes", "bytes"],
            [Web3.toBytes(text=prefix),
             Web3.toBytes(message_hash)])
        signed = keys.ecdsa_sign(message_hash=signable_hash,
                                 private_key=keys_pk)
        v = str(Web3.toHex(Web3.toBytes(signed.v)))
        r = str(Web3.toHex(Web3.toBytes(signed.r).rjust(32, b"\0")))
        s = str(Web3.toHex(Web3.toBytes(signed.s).rjust(32, b"\0")))
        signature = "0x" + r[2:] + s[2:] + v[2:]
    except AquariusPrivateKeyException:
        signature = None

    return signature
예제 #4
0
def test_order_started(events_object, client, base_ddo_url):
    web3 = events_object._web3  # get_web3()
    block = web3.eth.block_number
    _ddo = new_ddo(test_account1, web3, f"dt.{block}")
    did = _ddo.id

    _, dt_contract, erc20_address = send_create_update_tx(
        "create", _ddo, bytes([2]), test_account1)
    events_object.process_current_blocks()
    token_contract = web3.eth.contract(
        abi=ERC20Template.abi, address=web3.toChecksumAddress(erc20_address))

    token_contract.functions.mint(
        web3.toChecksumAddress(test_account3.address),
        web3.toWei(10, "ether")).transact({"from": test_account1.address})
    # mock provider fees
    provider_wallet = get_aquarius_wallet()
    provider_fee_amount = 0
    provider_data = json.dumps({"timeout": 0}, separators=(",", ":"))
    provider_fee_address = provider_wallet.address
    provider_fee_token = "0x0000000000000000000000000000000000000000"
    message_hash = Web3.solidityKeccak(
        ["bytes", "address", "address", "uint256", "uint256"],
        [
            Web3.toHex(Web3.toBytes(text=provider_data)),
            provider_fee_address,
            provider_fee_token,
            provider_fee_amount,
            0,
        ],
    )
    pk = keys.PrivateKey(provider_wallet.key)
    prefix = "\x19Ethereum Signed Message:\n32"
    signable_hash = Web3.solidityKeccak(
        ["bytes", "bytes"],
        [Web3.toBytes(text=prefix),
         Web3.toBytes(message_hash)])
    signed = keys.ecdsa_sign(message_hash=signable_hash, private_key=pk)

    provider_fee = {
        "providerFeeAddress": web3.toChecksumAddress(provider_fee_address),
        "providerFeeToken": web3.toChecksumAddress(provider_fee_token),
        "providerFeeAmount": provider_fee_amount,
        "providerData": Web3.toHex(Web3.toBytes(text=provider_data)),
        # make it compatible with last openzepellin https://github.com/OpenZeppelin/openzeppelin-contracts/pull/1622
        "v": (signed.v + 27) if signed.v <= 1 else signed.v,
        "r": Web3.toHex(Web3.toBytes(signed.r).rjust(32, b"\0")),
        "s": Web3.toHex(Web3.toBytes(signed.s).rjust(32, b"\0")),
        "validUntil": 0,
    }
    txn = token_contract.functions.startOrder(
        web3.toChecksumAddress(test_account3.address),
        1,
        (
            web3.toChecksumAddress(provider_fee["providerFeeAddress"]),
            web3.toChecksumAddress(provider_fee["providerFeeToken"]),
            provider_fee["providerFeeAmount"],
            provider_fee["v"],
            provider_fee["r"],
            provider_fee["s"],
            provider_fee["validUntil"],
            provider_fee["providerData"],
        ),
        (
            "0x0000000000000000000000000000000000000000",
            "0x0000000000000000000000000000000000000000",
            0,
        ),
    ).transact({"from": test_account3.address})
    web3.eth.wait_for_transaction_receipt(txn)
    events_object.process_current_blocks()

    published_ddo = get_ddo(client, base_ddo_url, did)
    assert published_ddo["stats"]["orders"] == 1