Ejemplo n.º 1
0
 def request_commit(self, quantity):
     with self.mutex:
         self._validate_transfer_quantity(quantity)
         secret = util.b2h(os.urandom(32))  # secure random number
         secret_hash = util.hash160hex(secret)
         self.commits_requested.append(secret)
         return quantity, secret_hash
Ejemplo n.º 2
0
 def _validate_commit_secret_hash(self, script):
     given_spend_secret_hash = get_commit_spend_secret_hash(script)
     own_spend_secret_hash = util.hash160hex(self.spend_secret)
     if given_spend_secret_hash != own_spend_secret_hash:
         msg = "Incorrect spend secret hash: {0} != {1}"
         raise ValueError(
             msg.format(given_spend_secret_hash, own_spend_secret_hash))
Ejemplo n.º 3
0
 def request_commit(self, quantity):
     with self.mutex:
         self._validate_transfer_quantity(quantity)
         secret = util.b2h(os.urandom(32))  # secure random number
         secret_hash = util.hash160hex(secret)
         self.commits_requested.append(secret)
         return quantity, secret_hash
Ejemplo n.º 4
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.º 5
0
 def _validate_commit_secret_hash(self, script):
     given_spend_secret_hash = get_commit_spend_secret_hash(script)
     own_spend_secret_hash = util.hash160hex(self.spend_secret)
     if given_spend_secret_hash != own_spend_secret_hash:
         msg = "Incorrect spend secret hash: {0} != {1}"
         raise ValueError(msg.format(
             given_spend_secret_hash, own_spend_secret_hash
         ))
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 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.º 8
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