예제 #1
0
 def __init__(self, sk, i, tokenNetwork, addrs, deposit, channel_identifier, secret_registry_contract, lock):
     self.sk = sk
     self.i = i
     self.token_network = tokenNetwork
     self.addrs = addrs
     self.deposit = deposit
     self.moving_average_capacity = list(deposit)
     self.channel_identifier = channel_identifier
     self.secret_registry_contract = secret_registry_contract
     self.status = "OK"
     self.nonce = 0
     self.transferred_amount = [0,0]
     self.locked_amount = [0,0]
     self.locksroot = [pack_data(['uint256'],[0]),pack_data(['uint256'],[0])]
     self.leaves = [{},{}]
     self.BP = [0,0]
     self.chain_id = 337
     self.RTT = RTT()
     self.pending_payment = [{},{}]
     self.reserve_payment = [{}, {}]
     self.wait_confirm = [{},{}]
     self.lock = lock
     self.payment_history = [[],[]]
     self.weight = 0.6
     self.contract_boundary = [0,0]
예제 #2
0
def test_pack_data():
    assert pack_data(("Test", "string"),
                     (49, "uint32")) == b"Test\x00\x00\x001"

    with pytest.raises(ValueError):
        pack_data((13, "uint256"), ("address"))

    with pytest.raises(TypeError):
        pack_data((256, "uint256"), ("This is not a uint256", "uint256"))
예제 #3
0
    def locked_BP(self, BP):
        self.lock.acquire()
        try:
            self.BP[1 - self.i] = BP
            self.locked_amount[1-self.i] += BP.message_data.amount + BP.message_data.selected_incentive
            self.locksroot[1-self.i] = BP.message_data.locksroot
        finally:
            self.lock.release()
        cr = BP.message_data.payment_identifier
        locked_amount = BP.message_data.amount + BP.message_data.selected_incentive
        expiration = BP.message_data.expiration
        secrethash = BP.message_data.secrethash
        selected_incentive = BP.message_data.selected_incentive
        selected_delay = BP.message_data.selected_delay
        aux_incentive = BP.message_data.aux_incentive
        aux_delay = BP.message_data.aux_delay
        startTime = BP.message_data.start_time
        target = BP.message_data.target

        leaf = pack_data(
            ['uint256', 'uint256', 'bytes32', 'uint256', 'uint256', 'uint256', 'uint256', 'uint256', 'address'],
            [locked_amount, expiration, secrethash, selected_incentive,selected_delay, aux_incentive, aux_delay, startTime, target])
        self.leaves[1-self.i][cr] = leaf

        # return self.transferred_amount[1-self.i], self.locked_amount[1-self.i], self.locksroot[1-self.i]
        return locked_amount, selected_delay, startTime
예제 #4
0
def pack_balance_proof(
    nonce: typing.Nonce,
    balance_hash: typing.BalanceHash,
    additional_hash: typing.AdditionalHash,
    channel_identifier: typing.ChannelID,
    token_network_identifier: typing.TokenNetworkID,
    chain_id: typing.ChainID,
    msg_type: MessageTypeId = MessageTypeId.BALANCE_PROOF,
) -> bytes:
    return pack_data([
        'address',
        'uint256',
        'uint256',
        'uint256',
        'bytes32',
        'uint256',
        'bytes32',
    ], [
        token_network_identifier,
        chain_id,
        msg_type,
        channel_identifier,
        balance_hash,
        nonce,
        additional_hash,
    ])
예제 #5
0
def pack_balance_proof(
        nonce: typing.Nonce,
        balance_hash: typing.BalanceHash,
        additional_hash: typing.AdditionalHash,
        channel_identifier: typing.ChannelID,
        token_network_identifier: typing.TokenNetworkID,
        chain_id: typing.ChainID,
        msg_type: MessageTypeId = MessageTypeId.BALANCE_PROOF,
) -> bytes:
    """Packs balance proof data to be signed

    Packs the given arguments in a byte array in the same configuration the
    contracts expect the signed data to have.
    """
    return pack_data([
        'address',
        'uint256',
        'uint256',
        'uint256',
        'bytes32',
        'uint256',
        'bytes32',
    ], [
        token_network_identifier,
        chain_id,
        msg_type,
        channel_identifier,
        balance_hash,
        nonce,
        additional_hash,
    ])
예제 #6
0
 def _data_to_sign(self) -> bytes:
     return pack_data(
         (self.cmdid.value, "uint8"),
         (b"\x00" * 3, "bytes"),  # padding
         (self.message_identifier, "uint64"),
         (self.secret, "bytes32"),
     )
예제 #7
0
 def _data_to_sign(self) -> bytes:
     return pack_data(
         (self.cmdid.value, "uint8"),
         (b"\x00" * 3, "bytes"),  # padding
         (self.nonce, "uint64"),
         (self.current_protocol_version, "uint8"),
     )
예제 #8
0
def pack_balance_proof(
    nonce: Nonce,
    balance_hash: BalanceHash,
    additional_hash: AdditionalHash,
    canonical_identifier: CanonicalIdentifier,
    msg_type: MessageTypeId = MessageTypeId.BALANCE_PROOF,
) -> bytes:
    """Packs balance proof data to be signed

    Packs the given arguments in a byte array in the same configuration the
    contracts expect the signed data to have.
    """
    return pack_data([
        'address',
        'uint256',
        'uint256',
        'uint256',
        'bytes32',
        'uint256',
        'bytes32',
    ], [
        canonical_identifier.token_network_address,
        canonical_identifier.chain_identifier,
        msg_type,
        canonical_identifier.channel_identifier,
        balance_hash,
        nonce,
        additional_hash,
    ])
예제 #9
0
 def message_hash(self) -> bytes:
     return sha3(
         pack_data(
             (self.cmdid.value, "uint8"),
             (self.message_identifier, "uint64"),
             (self.recipient, "address"),
             (self.secrethash, "bytes32"),
         ))
예제 #10
0
 def message_hash(self) -> bytes:
     return sha3(
         pack_data(
             (self.cmdid.value, "uint8"),
             (self.message_identifier, "uint64"),
             (self.payment_identifier, "uint64"),
             (self.secret, "bytes32"),
         ))
예제 #11
0
    def create_BP(self, w3, cr, initiator, target, secrethash, amount, expiration, s_contract, a_contract, start_time):
        # self.lock.acquire()
        # try:
        #     self.locked_amount[self.i] += amount + s_contract[0]
        # finally:
        #     self.lock.release()
        # print("locked_amount ", self.locked_amount[self.i])

        # uint = 32bytes, address = 20bytes, bytes32 = 32bytes
        leaf = pack_data(['uint256', 'uint256', 'bytes32', 'uint256', 'uint256', 'uint256', 'uint256', 'uint256', 'address'],
                         [amount+s_contract[0], expiration, secrethash, s_contract[0],
                          s_contract[1], a_contract[0][0], a_contract[1][1], start_time, target])
        self.leaves[self.i][cr] = leaf
        temp = []
        for i in list(self.leaves[self.i].values()) :
            temp.append(sha3(i))
        layer = compute_layers(temp)
        tree = MerkleTreeState(layer)
        locksroot = "0x" + merkleroot(tree).hex()
        # print("locksroot ", locksroot)

        self.locksroot[self.i] = locksroot
        self.nonce +=1

        message_data = LockedTransfer_structure(self.nonce, self.chain_id, cr, expiration, self.token_network.address, self.channel_identifier,
                                                 self.addrs[1-self.i], target, initiator, locksroot, secrethash, self.transferred_amount[self.i],
                                                self.locked_amount[self.i], amount, s_contract[0], s_contract[1], a_contract[0][0], a_contract[1][1], start_time)
        packed_message_data = message_data.pack()
        additional_hash = '0x' + sha3(packed_message_data).hex()
        # print("additional_hash ", additional_hash)

        packed_balance = pack_data(['uint256', 'uint256', 'bytes32'], [self.transferred_amount[self.i], self.locked_amount[self.i], locksroot])
        balance_hash = '0x' + sha3(packed_balance).hex()
        # print("balance_hash ", balance_hash)

        packed_balance_proof = pack_data(['uint256', 'bytes32', 'uint256', 'bytes32'],
                                         [self.channel_identifier, balance_hash, self.nonce, additional_hash])

        hashBP = '0x' + sha3(packed_balance_proof).hex()
        signature = w3.eth.account.signHash(message_hash=hashBP, private_key=self.sk)
        # print("signature", signature)
        BP = balanceProof(message_data, additional_hash, balance_hash, signature['signature'].hex())
        self.BP[self.i] = BP

        return BP
예제 #12
0
 def serialize_bin(self, msg_type: MessageTypeId = MessageTypeId.BALANCE_PROOF):
     return pack_data(
         (self.token_network_address, "address"),
         (self.chain_id, "uint256"),
         (msg_type.value, "uint256"),
         (self.channel_identifier, "uint256"),
         (decode_hex(self.balance_hash), "bytes32"),
         (self.nonce, "uint256"),
         (decode_hex(self.additional_hash), "bytes32"),
     )
예제 #13
0
 def _data_to_sign(self) -> bytes:
     return pack_data(
         (self.token_network_address, "address"),
         (self.chain_id, "uint256"),
         (self.message_type, "uint256"),
         (self.channel_identifier, "uint256"),
         (self.participant, "address"),
         (self.total_withdraw, "uint256"),
         (self.expiration, "uint256"),
     )
예제 #14
0
 def _data_to_sign(self) -> bytes:
     return pack_data(
         (self.cmdid.value, "uint8"),
         (b"\x00" * 3, "bytes"),  # padding
         (self.message_identifier, "uint64"),
         (self.payment_identifier, "uint64"),
         (self.secrethash, "bytes32"),
         (self.amount, "uint256"),
         (self.expiration, "uint256"),
     )
예제 #15
0
def pack_reward_proof(canonical_identifier: CanonicalIdentifier,
                      reward_amount: TokenAmount, nonce: Nonce) -> bytes:
    channel_identifier = canonical_identifier.channel_identifier
    token_network_address = canonical_identifier.token_network_address
    chain_id = canonical_identifier.chain_identifier
    return pack_data(
        ["uint256", "uint256", "address", "uint256", "uint256"],
        [
            channel_identifier, reward_amount, token_network_address, chain_id,
            nonce
        ],
    )
예제 #16
0
 def packed_reward_proof_data(self) -> bytes:
     """Return reward proof data serialized to binary"""
     return pack_data(
         ['uint256', 'uint256', 'address', 'uint256', 'uint256'],
         [
             self.channel_identifier,
             self.reward_amount,
             self.token_network_address,
             self.chain_id,
             self.nonce,
         ],
     )
예제 #17
0
 def _data_to_sign(self) -> bytes:
     return pack_data(
         (self.canonical_identifier.chain_identifier, "uint256"),
         (self.canonical_identifier.token_network_address, "address"),
         (self.canonical_identifier.channel_identifier, "uint256"),
         (self.updating_participant, "address"),
         (self.other_participant, "address"),
         (self.updating_nonce, "uint64"),
         (self.other_nonce, "uint64"),
         (self.updating_capacity, "uint256"),
         (self.other_capacity, "uint256"),
         (self.reveal_timeout, "uint256"),
     )
예제 #18
0
 def serialize_bin(self, msg_type: MessageTypeId = MessageTypeId.BALANCE_PROOF) -> bytes:
     return pack_data(
         ['address', 'uint256', 'uint256', 'uint256', 'bytes32', 'uint256', 'bytes32'],
         [
             self.token_network_address,
             self.chain_id,
             msg_type.value,
             self.channel_identifier,
             decode_hex(self.balance_hash),
             self.nonce,
             decode_hex(self.additional_hash),
         ],
     )
예제 #19
0
 def packed_balance_proof_data(
         self,
         message_type: MessageTypeId = MessageTypeId.BALANCE_PROOF
 ) -> bytes:
     return pack_data(
         (self.token_network_address, "address"),
         (self.chain_id, "uint256"),
         (message_type.value, "uint256"),
         (self.channel_identifier, "uint256"),
         (decode_hex(self.balance_hash), "bytes32"),
         (self.nonce, "uint256"),
         (decode_hex(self.additional_hash), "bytes32"),
     )
예제 #20
0
 def _packed_data(self) -> bytes:
     return pack_data(
         (self.cmdid.value, "uint8"),
         (self.message_identifier, "uint64"),
         (self.payment_identifier, "uint64"),
         (self.lock.expiration, "uint256"),
         (self.token, "address"),
         (self.recipient, "address"),
         (self.target, "address"),
         (self.initiator, "address"),
         (self.lock.secrethash, "bytes32"),
         (self.lock.amount, "uint256"),
     )
예제 #21
0
 def _data_to_sign(self) -> bytes:
     return pack_data(
         (self.cmdid.value, "uint8"),
         (b"\x00" * 3, "bytes"),  # padding
         (self.nonce, "uint256"),
         (self.message_identifier, "uint64"),
         (self.token_network_address, "address"),
         (self.chain_id, "uint256"),
         (self.message_type, "uint256"),
         (self.channel_identifier, "uint256"),
         (self.participant, "address"),
         (self.total_withdraw, "uint256"),
         (self.expiration, "uint256"),
     )
예제 #22
0
 def _data_to_sign(self) -> bytes:
     return pack_data(
         (self.canonical_identifier.chain_identifier, "uint256"),
         (self.canonical_identifier.token_network_address, "address"),
         (self.canonical_identifier.channel_identifier, "uint256"),
         (self.updating_participant, "address"),
         (self.fee_schedule.cap_fees, "bool"),
         (self.fee_schedule.flat, "uint256"),
         (self.fee_schedule.proportional, "uint256"),
         (rlp.encode(self.fee_schedule.imbalance_penalty or 0), "bytes"),
         (
             marshmallow.fields.NaiveDateTime()._serialize(self.timestamp, "timestamp", self),
             "string",
         ),
     )
예제 #23
0
 def make_params(timestamp: str):
     params = {
         "sender": to_checksum_address(sender),
         "receiver":
         to_checksum_address(api_sut.pathfinding_service.address),
         "timestamp": timestamp,
     }
     local_signer = LocalSigner(private_key=decode_hex(privkey))
     params["signature"] = encode_hex(
         local_signer.sign(
             pack_data(
                 (params["sender"], "address"),
                 (params["receiver"], "address"),
                 (params["timestamp"], "string"),
             )))
     return params
예제 #24
0
 def make_params(timestamp: datetime):
     params = {
         "sender": sender,
         "receiver": api_sut.pathfinding_service.address,
         "timestamp": timestamp.isoformat(),
     }
     local_signer = LocalSigner(private_key=decode_hex(privkey))
     params["signature"] = encode_hex(
         local_signer.sign(
             pack_data(
                 ["address", "address", "string"],
                 [params["sender"], params["receiver"], params["timestamp"]],
             )
         )
     )
     return params
예제 #25
0
 def make_params(timestamp: datetime):
     params = {
         'sender': sender,
         'receiver': api_sut.pathfinding_service.address,
         'timestamp': timestamp.isoformat(),
     }
     local_signer = LocalSigner(private_key=decode_hex(privkey))
     params['signature'] = encode_hex(
         local_signer.sign(
             pack_data(
                 ['address', 'address', 'string'],
                 [params['sender'], params['receiver'], params['timestamp']],
             )
         )
     )
     return params
예제 #26
0
파일: message.py 프로젝트: cnrlab341/payGo
 def pack(self):
     return pack_data([
         'uint64',
         'bytes32',
         'uint256',
         'bytes32',
         'bytes32',
         'uint256',
         'uint256',
     ], [
         self.nonce,
         self.payment_identifier,
         self.channel_identifier,
         self.secret,
         self.locksroot,
         self.transferred_amount,
         self.locked_amount,
     ])
예제 #27
0
파일: message.py 프로젝트: cnrlab341/payGo
 def pack(self):
     return pack_data([
         'uint8',
         'uint64',
         'uint256',
         'bytes32',
         'uint256',
         'address',
         'uint256',
         'address',
         'address',
         'address',
         'bytes32',
         'bytes32',
         'uint256',
         'uint256',
         'uint256',
         'uint256',
         'uint256',
         'uint256',
         'uint256',
         'uint256',
     ], [
         self.cmdid,
         self.nonce,
         self.chain_id,
         self.payment_identifier,
         self.expiration,
         self.token_network_address,
         self.channel_identifier,
         self.recipient,
         self.target,
         self.initiator,
         self.locksroot,
         self.secrethash,
         self.transferred_amount,
         self.locked_amount,
         self.amount,
         self.start_time,
         self.selected_incentive,
         self.selected_delay,
         self.aux_incentive,
         self.aux_delay,
     ])
예제 #28
0
def pack_reward_proof(
        channel_identifier: ChannelID,
        reward_amount: TokenAmount,
        token_network_address: TokenNetworkID,
        chain_id: ChainID,
        nonce: Nonce,
) -> bytes:
    return pack_data([
        'uint256',
        'uint256',
        'address',
        'uint256',
        'uint256',
    ], [
        channel_identifier,
        reward_amount,
        token_network_address,
        chain_id,
        nonce,
    ])
예제 #29
0
 def packed_balance_proof_data(
     self,
     message_type: MessageTypeId = MessageTypeId.BALANCE_PROOF,
 ) -> bytes:
     return pack_data([
         'address',
         'uint256',
         'uint256',
         'uint256',
         'bytes32',
         'uint256',
         'bytes32',
     ], [
         self.token_network_address,
         self.chain_id,
         message_type.value,
         self.channel_identifier,
         decode_hex(self.balance_hash),
         self.nonce,
         decode_hex(self.additional_hash),
     ])
예제 #30
0
    def get_partner_leaves(self):
        leaves = list(self.leaves[1-self.i].values())
        types = ["bytes" for _ in leaves]

        return pack_data(types, leaves)