Ejemplo n.º 1
0
 def get_spend_secret_hash(self):
     with self.mutex:
         if self.spend_secret is not None:  # payee
             return util.b2h(util.hash160(util.h2b(self.spend_secret)))
         elif self.deposit_script_hex is not None:  # payer
             script = util.h2b(self.deposit_script_hex)
             return get_deposit_spend_secret_hash(script)
         else:  # undefined
             raise Exception("Undefined state, not payee or payer.")
Ejemplo n.º 2
0
 def get_spend_secret_hash(self):
     with self.mutex:
         if self.spend_secret is not None:  # payee
             return util.b2h(util.hash160(util.h2b(self.spend_secret)))
         elif self.deposit_script_hex is not None:  # payer
             script = util.h2b(self.deposit_script_hex)
             return get_deposit_spend_secret_hash(script)
         else:  # undefined
             raise Exception("Undefined state, not payee or payer.")
Ejemplo n.º 3
0
    def set_commit(self, rawtx, script_hex):
        with self.mutex:
            self._assert_open_state()

            # TODO validate rawtx
            # TODO validate script
            # TODO validate rawtx signed by payer
            # TODO check it is for the current deposit
            # TODO check given script and rawtx match
            # TODO check given script is commit script

            script = util.h2b(script_hex)
            self._validate_commit_secret_hash(script)
            self._validate_commit_payee_pubkey(script)

            quantity = self.control.get_quantity(rawtx)
            revoke_secret_hash = get_commit_revoke_secret_hash(script)
            for revoke_secret in self.commits_requested[:]:

                # revoke secret hash must match as it would
                # otherwise break the channels reversability
                if revoke_secret_hash == util.hash160hex(revoke_secret):

                    # remove from requests
                    self.commits_requested.remove(revoke_secret)

                    # add to active
                    self._order_active()
                    self.commits_active.append({
                        "rawtx": rawtx, "script": script_hex,
                        "revoke_secret": revoke_secret
                    })
                    return self.get_transferred_amount()

            return None
Ejemplo n.º 4
0
 def close_channel(self):
     with self.mutex:
         self._assert_open_state()
         assert (len(self.commits_active) > 0)
         self._order_active()
         commit = self.commits_active[-1]
         rawtx = self.control.finalize_commit(
             self.payee_wif, commit["rawtx"],
             util.h2b(self.deposit_script_hex))
         commit["rawtx"] = rawtx  # update commit
         return util.gettxid(rawtx)
Ejemplo n.º 5
0
 def revoke(self, secret):
     with self.mutex:
         secret_hash = util.hash160hex(secret)
         for commit in self.commits_active[:]:
             script = util.h2b(commit["script"])
             if secret_hash == get_commit_revoke_secret_hash(script):
                 self.commits_active.remove(commit)  # remove from active
                 commit["revoke_secret"] = secret  # save secret
                 self.commits_revoked.append(commit)  # add to revoked
                 return copy.deepcopy(commit)
         return None
Ejemplo n.º 6
0
 def revoke(self, secret):
     with self.mutex:
         secret_hash = util.hash160hex(secret)
         for commit in self.commits_active[:]:
             script = util.h2b(commit["script"])
             if secret_hash == get_commit_revoke_secret_hash(script):
                 self.commits_active.remove(commit)  # remove from active
                 commit["revoke_secret"] = secret  # save secret
                 self.commits_revoked.append(commit)  # add to revoked
                 return copy.deepcopy(commit)
         return None
Ejemplo n.º 7
0
 def close_channel(self):
     with self.mutex:
         self._assert_open_state()
         assert(len(self.commits_active) > 0)
         self._order_active()
         commit = self.commits_active[-1]
         rawtx = self.control.finalize_commit(
             self.payee_wif, commit["rawtx"],
             util.h2b(self.deposit_script_hex)
         )
         commit["rawtx"] = rawtx  # update commit
         return util.gettxid(rawtx)
Ejemplo n.º 8
0
 def create_commit(self, quantity, revoke_secret_hash, delay_time):
     with self.mutex:
         self._validate_transfer_quantity(quantity)
         rawtx, script = self.control.create_commit(
             self.payer_wif, util.h2b(self.deposit_script_hex),
             quantity, revoke_secret_hash, delay_time
         )
         script_hex = util.b2h(script)
         self._order_active()
         self.commits_active.append({
             "rawtx": rawtx, "script": script_hex, "revoke_secret": None
         })
         return {"rawtx": rawtx, "script": script_hex}
Ejemplo n.º 9
0
 def create_commit(self, quantity, revoke_secret_hash, delay_time):
     with self.mutex:
         self._validate_transfer_quantity(quantity)
         rawtx, script = self.control.create_commit(
             self.payer_wif, util.h2b(self.deposit_script_hex), quantity,
             revoke_secret_hash, delay_time)
         script_hex = util.b2h(script)
         self._order_active()
         self.commits_active.append({
             "rawtx": rawtx,
             "script": script_hex,
             "revoke_secret": None
         })
         return {"rawtx": rawtx, "script": script_hex}
Ejemplo n.º 10
0
    def set_deposit(self, rawtx, script_hex):
        with self.mutex:
            self._assert_unopen_state()

            # TODO validate rawtx
            # TODO validate script
            # TODO check given script and rawtx match
            # TODO check given script is deposit script

            script = util.h2b(script_hex)
            self._validate_deposit_spend_secret_hash(script)
            self._validate_deposit_payee_pubkey(script)
            self.deposit_rawtx = rawtx
            self.deposit_script_hex = script_hex
Ejemplo n.º 11
0
    def set_deposit(self, rawtx, script_hex):
        with self.mutex:
            self._assert_unopen_state()

            # TODO validate rawtx
            # TODO validate script
            # TODO check given script and rawtx match
            # TODO check given script is deposit script

            script = util.h2b(script_hex)
            self._validate_deposit_spend_secret_hash(script)
            self._validate_deposit_payee_pubkey(script)
            self.deposit_rawtx = rawtx
            self.deposit_script_hex = script_hex
Ejemplo n.º 12
0
    def set_commit(self, rawtx, script_hex):
        with self.mutex:
            self._assert_open_state()

            # TODO validate rawtx
            # TODO validate script
            # TODO validate rawtx signed by payer
            # TODO check it is for the current deposit
            # TODO check given script and rawtx match
            # TODO check given script is commit script

            script = util.h2b(script_hex)
            self._validate_commit_secret_hash(script)
            self._validate_commit_payee_pubkey(script)

            quantity = self.control.get_quantity(rawtx)
            revoke_secret_hash = get_commit_revoke_secret_hash(script)
            for revoke_secret in self.commits_requested[:]:

                # revoke secret hash must match as it would
                # otherwise break the channels reversability
                if revoke_secret_hash == util.hash160hex(revoke_secret):

                    # remove from requests
                    self.commits_requested.remove(revoke_secret)

                    # add to active
                    self._order_active()
                    self.commits_active.append({
                        "rawtx": rawtx,
                        "script": script_hex,
                        "revoke_secret": revoke_secret
                    })
                    return self.get_transferred_amount()

            return None
Ejemplo n.º 13
0
 def timeout_recover(self):
     with self.mutex:
         script = util.h2b(self.deposit_script_hex)
         self.timeout_rawtx = self.control.timeout_recover(
             self.payer_wif, script
         )
Ejemplo n.º 14
0
 def is_deposit_expired(self):
     with self.mutex:
         script = util.h2b(self.deposit_script_hex)
         t = get_deposit_expire_time(script)
         return self.get_deposit_confirms() >= t
Ejemplo n.º 15
0
 def is_deposit_expired(self):
     with self.mutex:
         script = util.h2b(self.deposit_script_hex)
         t = get_deposit_expire_time(script)
         return self.get_deposit_confirms() >= t
Ejemplo n.º 16
0
 def timeout_recover(self):
     with self.mutex:
         script = util.h2b(self.deposit_script_hex)
         self.timeout_rawtx = self.control.timeout_recover(
             self.payer_wif, script)
Ejemplo n.º 17
0
 def change_recover(self):
     with self.mutex:
         script = util.h2b(self.deposit_script_hex)
         self.change_rawtx = self.control.change_recover(
             self.payer_wif, script, self.spend_secret)
Ejemplo n.º 18
0
 def change_recover(self):
     with self.mutex:
         script = util.h2b(self.deposit_script_hex)
         self.change_rawtx = self.control.change_recover(
             self.payer_wif, script, self.spend_secret
         )