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
def sign_message(self, account: str, message: bytes, content_type: str = None, validator_address: str = None, **kwargs) -> HexBytes: """ See https://github.com/ethereum/go-ethereum/blob/a32a2b933ad6793a2fe4172cd46c5c5906da259a/signer/core/signed_data.go#L185 """ if isinstance(message, bytes): message = Web3.toHex(message) if not content_type: content_type = self.DEFAULT_CONTENT_TYPE elif content_type not in self.SIGN_DATA_CONTENT_TYPES: raise ValueError(f'{content_type} is not a valid content type. ' f'Valid types are {self.SIGN_DATA_CONTENT_TYPES}') if content_type == self.SIGN_DATA_FOR_VALIDATOR: if not validator_address or validator_address == NULL_ADDRESS: raise ValueError( 'When using the intended validator type, a validator address is required.' ) data = {'address': validator_address, 'message': message} elif content_type == self.SIGN_DATA_FOR_ECRECOVER: data = message else: raise NotImplementedError signed_data = self.__ipc_request("account_signData", content_type, account, data) return HexBytes(signed_data)
def convert_to_string(data: bytes) -> HexStr: """ :param data: :return: """ return Web3.toHex(data)
def ec_revoverable_message(msg, private_key): signed_message = sign_message(msg, private_key) ec_recover_args = (msghash, v, r, s) = ( Web3.toHex(signed_message.messageHash), signed_message.v, to_32byte_hex(signed_message.r), to_32byte_hex(signed_message.s), ) return ec_recover_args
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
def to_32byte_hex(val): return Web3.toHex(Web3.toBytes(val).rjust(32, b'\0'))
def int_to_hex(value: int) -> HexStr: hexString = str(Web3.toHex(value)) if len(hexString) < 66: leftPadding = '0' * (66 - len(hexString)) hexString = f'0x{leftPadding}{hexString[2:]}' return hexString
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