def binary(self): """ Returns a binary encoded versoin of the AtomicSwapCondition """ result = bytearray() result.extend(self._type) # 106 size of the atomicswap condition in binary form result.extend(binary.encode(106)) result.extend(binary.encode(UnlockHash.from_string(self._sender))) result.extend(binary.encode(UnlockHash.from_string(self._reciever))) result.extend(binary.encode(self._hashed_secret, type_='hex')) result.extend(binary.encode(self._locktime)) return result
def _generate_multisig_address(self, ulhs): """ Generates a multisig address """ mtree = Tree(hash_func=utils.hash) mtree.push(binary.encode(self._nr_of_cosigners)) # make sure that regardless of the order of the unlockhashes, we sort them so that we always # produce the same multisig address for ulh in sorted(ulhs): mtree.push(binary.encode(UnlockHash.from_string(ulh))) mtree.push(binary.encode(self._required_sig)) address_hash = mtree.root() ulh = UnlockHash(unlock_type=UNLOCK_TYPE_MULTISIG, hash=address_hash) return str(ulh)
def test_unlockhash_from_string(): """ Tests loading an unlockhash from a string """ input = '01324dcf027dd4a30a932c441f365a25e86b173defa4b8e58948253471b81b72cf57a828ea336a' assert str(UnlockHash.from_string( input)) == input, "Failed to load unlockhash from string"
def data(self): """ Retruns the binary format of the data on the condition """ result = bytearray() result.extend(binary.encode(self._min_nr_sig)) result.extend(binary.encode(len(self._unlockhashes))) for unlockhash in self._unlockhashes: result.extend(binary.encode(UnlockHash.from_string(unlockhash))) return result
def unlockhash(self): """ Calculate unlock hash of the spendable key """ if self._unlockhash is None: pub_key = Ed25519PublicKey(pub_key=self._pk) encoded_pub_key = binary.encode(pub_key) hash = utils.hash(encoded_pub_key, encoding_type='slice') self._unlockhash = UnlockHash(unlock_type=UNLOCK_TYPE_PUBKEY, hash=hash) return self._unlockhash
def binary(self): """ Returns a binary encoded versoin of the MultiSignatureCondition """ result = bytearray() result.extend(self._type) condition_binary = bytearray() condition_binary.extend(binary.encode(self._min_nr_sig)) condition_binary.extend(binary.encode(len(self._unlockhashes))) for unlockhash in self._unlockhashes: condition_binary.extend( binary.encode(UnlockHash.from_string(unlockhash))) result.extend(binary.encode(condition_binary, type_='slice')) return result
def add_coin_output(self, value, recipient, locktime=None): """ Add a new coin output to the transaction @param value: Amout of coins @param recipient: The recipient address @param locktime: If provided then a locktimecondition will be created for this output """ unlockhash = UnlockHash.from_string(recipient) condition = UnlockHashCondition(unlockhash=unlockhash) if locktime is not None: condition = LockTimeCondition(condition=condition, locktime=locktime) self._coins_outputs.append(CoinOutput(value=value, condition=condition))
def from_dict(condition_dict): """ Creates an unlock condition object from a dictionary """ if 'data' in condition_dict: if 'type' in condition_dict: if condition_dict['type'] == 1: return UnlockHashCondition( unlockhash=UnlockHash.from_string( condition_dict['data']['unlockhash'])) elif condition_dict['type'] == 1: return AtomicSwapCondition.from_dict( condition_dict['data']) elif condition_dict['type'] == 3: return LockTimeCondition.from_dict(condition_dict['data']) elif condition_dict['type'] == 4: return MultiSignatureCondition.from_dict( condition_dict['data'])
def ulh(): """ Generates a test unlockhash of with unlock type publickey """ hash = utils.hash(b'hello') return UnlockHash(unlock_type=UNLOCK_TYPE_PUBKEY, hash=hash)
def recipient(): """ Generates a recipient address """ hash = utils.hash(b'hello recipient') return str(UnlockHash(unlock_type=UNLOCK_TYPE_PUBKEY, hash=hash))