def _sign(self, data: bytes) -> bytes: """ Use eth_sign compatible hasher to sign matrix data """ return signing.sign( data, self._raiden_service.private_key, hasher=eth_sign_sha3, )
def sign(self, private_key): """ Sign message using `private_key`. """ packed = self.packed() field = packed.fields_spec[-1] assert field.name == 'signature', 'signature is not the last field' message_data = packed.data[:-field.size_bytes] signature, public_key = signing.sign(message_data, private_key) packed.signature = signature self.sender = signing.address_from_key(public_key) self.signature = packed.signature
def sign(self, private_key, node_address): """ Sign message using `private_key`. """ packed = self.packed() field = type(packed).fields_spec[-1] assert field.name == 'signature', 'signature is not the last field' # this slice must be from the end of the buffer message_data = packed.data[:-field.size_bytes] signature = signing.sign(message_data, private_key) packed.signature = signature self.sender = node_address self.signature = signature
def sign(self, private_key): """ Sign message using `private_key`. """ packed = self.packed() field = packed.fields_spec[-1] assert field.name == 'signature', 'signature is not the last field' message_data = packed.data[:-field. size_bytes] # XXX: this slice must be from the end of the buffer signature, public_key = signing.sign(message_data, private_key) packed.data[-field.size_bytes:] = signature self.sender = publickey_to_address(public_key) self.signature = signature
def sign(self, private_key, node_address): """ Sign message using `private_key`. """ packed = self.packed() field = packed.fields_spec[-1] assert field.name == 'signature', 'signature is not the last field' # this slice must be from the end of the buffer message_data = packed.data[:-field.size_bytes] signature = signing.sign(message_data, private_key) packed.data[-field.size_bytes:] = signature self.sender = node_address self.signature = signature
def sign(self, private_key, node_address): packed = self.packed() klass = type(packed) field = klass.fields_spec[-1] assert field.name == 'signature', 'signature is not the last field' data = packed.data data_to_sign = pack_signing_data( klass.get_bytes_from(data, 'nonce'), klass.get_bytes_from(data, 'transferred_amount'), klass.get_bytes_from(data, 'channel'), klass.get_bytes_from(data, 'locksroot'), self.message_hash, ) signature = signing.sign(data_to_sign, private_key) packed.signature = signature self.sender = node_address self.signature = signature
def sign(self, private_key, node_address): packed = self.packed() klass = type(packed) field = klass.fields_spec[-1] assert field.name == 'signature', 'signature is not the last field' data = packed.data nonce = klass.get_bytes_from(data, 'nonce') transferred_amount = klass.get_bytes_from(data, 'transferred_amount') locksroot = klass.get_bytes_from(data, 'locksroot') channel_address = klass.get_bytes_from(data, 'channel') message_hash = self.message_hash data_to_sign = nonce + transferred_amount + locksroot + channel_address + message_hash signature = signing.sign(data_to_sign, private_key) packed.signature = signature self.sender = node_address self.signature = signature
def sign(self, private_key): """ Sign message using `private_key`. """ message_data = self._data_to_sign() self.signature = signing.sign(message_data, private_key)
def find_max_pending_transfers(gas_limit): """Measure gas consumption of TokenNetwork.unlock() depending on number of pending transfers and find the maximum number of pending transfers so gas_limit is not exceeded.""" tester = ContractTester(generate_keys=2) tester.deploy_contract('SecretRegistry') tester.deploy_contract( 'HumanStandardToken', _initialAmount=100000, _decimalUnits=3, _tokenName='SomeToken', _tokenSymbol='SMT', ) tester.deploy_contract( 'TokenNetwork', _token_address=tester.contract_address('HumanStandardToken'), _secret_registry=tester.contract_address('SecretRegistry'), _chain_id=1, _settlement_timeout_min=100, _settlement_timeout_max=200, ) tester.call_transaction( 'HumanStandardToken', 'transfer', _to=tester.accounts[1], _value=10000, ) receipt = tester.call_transaction( 'TokenNetwork', 'openChannel', participant1=tester.accounts[0], participant2=tester.accounts[1], settle_timeout=150, ) channel_identifier = receipt['logs'][0]['topics'][1] tester.call_transaction( 'HumanStandardToken', 'approve', sender=tester.accounts[0], _spender=tester.contract_address('TokenNetwork'), _value=10000, ) tester.call_transaction( 'HumanStandardToken', 'approve', sender=tester.accounts[1], _spender=tester.contract_address('TokenNetwork'), _value=5000, ) tester.call_transaction( 'TokenNetwork', 'setTotalDeposit', participant=tester.accounts[0], total_deposit=5000, partner=tester.accounts[1], ) tester.call_transaction( 'TokenNetwork', 'setTotalDeposit', participant=tester.accounts[1], total_deposit=2000, partner=tester.accounts[0], ) print( "Measuring unlock()'s gas cost for different Merkle tree widths, can take a while..." ) before_closing = tester.tester.take_snapshot() enough = 0 too_much = 1024 nonce = 10 additional_hash = urandom(32) token_network_identifier = tester.contract_address('TokenNetwork') while enough + 1 < too_much: tree_size = (enough + too_much) // 2 tester.tester.revert_to_snapshot(before_closing) pending_transfers_tree = get_pending_transfers_tree( tester.web3, unlockable_amounts=[1] * tree_size, ) balance_hash = hash_balance_data(3000, 2000, pending_transfers_tree.merkle_root) data_to_sign = pack_signing_data( nonce, balance_hash, additional_hash, channel_identifier, token_network_identifier, 1, ) signature = sign(data_to_sign, tester.private_keys[1]) tester.call_transaction( 'TokenNetwork', 'closeChannel', partner=tester.accounts[1], balance_hash=balance_hash, nonce=nonce, additional_hash=additional_hash, signature=signature, ) tester.tester.mine_blocks(160) # close settlement window tester.call_transaction( 'TokenNetwork', 'settleChannel', participant1=tester.accounts[0], participant1_transferred_amount=0, participant1_locked_amount=0, participant1_locksroot=b'\x00' * 32, participant2=tester.accounts[1], participant2_transferred_amount=3000, participant2_locked_amount=2000, participant2_locksroot=pending_transfers_tree.merkle_root, ) receipt = tester.call_transaction( 'TokenNetwork', 'unlock', participant=tester.accounts[0], partner=tester.accounts[1], merkle_tree_leaves=pending_transfers_tree.packed_transfers, ) gas_used = receipt['gasUsed'] if gas_used <= gas_limit: enough = tree_size print( f'{tree_size} pending transfers work ({gas_used} gas needed to unlock)' ) else: too_much = tree_size print( f'{tree_size} pending transfers are too much ({gas_used} gas needed to unlock)' )