|
44 | 44 | RedeemerTag,
|
45 | 45 | ScriptType,
|
46 | 46 | datum_hash,
|
47 |
| - script_hash, |
| 47 | + script_hash, plutus_script_hash, |
48 | 48 | )
|
49 | 49 | from pycardano.transaction import (
|
50 | 50 | Asset,
|
@@ -250,24 +250,45 @@ def add_script_input(
|
250 | 250 | self._consolidate_redeemer(redeemer)
|
251 | 251 | self._inputs_to_redeemers[utxo] = redeemer
|
252 | 252 |
|
| 253 | + input_script_hash = utxo.output.address.payment_part |
| 254 | + if not isinstance(input_script_hash, ScriptHash): |
| 255 | + raise InvalidArgumentException( |
| 256 | + f"Expect the payment part of the address to be of a script (type ScriptHash), " |
| 257 | + f"but got {type(input_script_hash)} instead." |
| 258 | + ) |
| 259 | + |
| 260 | + # collect potential scripts to fulfill the input |
| 261 | + candidate_scripts = [] |
253 | 262 | if utxo.output.script:
|
254 |
| - self._inputs_to_scripts[utxo] = utxo.output.script |
255 |
| - self.reference_inputs.add(utxo) |
256 |
| - self._reference_scripts.append(utxo.output.script) |
| 263 | + candidate_scripts.append((utxo.output.script, utxo)) |
257 | 264 | elif not script:
|
258 | 265 | for i in self.context.utxos(utxo.output.address):
|
259 | 266 | if i.output.script:
|
260 |
| - self._inputs_to_scripts[utxo] = i.output.script |
261 |
| - self.reference_inputs.add(i) |
262 |
| - self._reference_scripts.append(i.output.script) |
263 |
| - break |
| 267 | + candidate_scripts.append((i.output.script, i)) |
264 | 268 | elif isinstance(script, UTxO):
|
265 |
| - assert script.output.script is not None |
266 |
| - self._inputs_to_scripts[utxo] = script.output.script |
267 |
| - self.reference_inputs.add(script) |
268 |
| - self._reference_scripts.append(script.output.script) |
| 269 | + if script.output.script is None: |
| 270 | + raise InvalidArgumentException( |
| 271 | + f"Expect the output of the UTxO to have a script, " |
| 272 | + f"but got None instead." |
| 273 | + ) |
| 274 | + candidate_scripts.append((script.output.script, script)) |
269 | 275 | else:
|
270 |
| - self._inputs_to_scripts[utxo] = script |
| 276 | + candidate_scripts.append((script, None)) |
| 277 | + |
| 278 | + found_valid_script = False |
| 279 | + for candidate_script, candidate_utxo in candidate_scripts: |
| 280 | + if script_hash(candidate_script) != input_script_hash: |
| 281 | + continue |
| 282 | + found_valid_script = True |
| 283 | + self._inputs_to_scripts[utxo] = candidate_script |
| 284 | + if candidate_utxo is not None: |
| 285 | + self.reference_inputs.add(candidate_utxo) |
| 286 | + self._reference_scripts.append(candidate_script) |
| 287 | + if not found_valid_script: |
| 288 | + raise InvalidArgumentException( |
| 289 | + f"Cannot find a valid script to fulfill the input UTxO: {utxo}." |
| 290 | + "Supplied scripts do not match the payment part of the input address." |
| 291 | + ) |
271 | 292 |
|
272 | 293 | self.inputs.append(utxo)
|
273 | 294 | return self
|
|
0 commit comments