1
0

verifyInput.ts 1.2 KB

12345678910111213141516171819202122232425262728
  1. /**
  2. * """ Check an InputComponent against electrumx service. This can be a bit slow
  3. since it gets all utxos on that address.
  4. Returns normally if the check passed. Raises ValidationError if the input is not
  5. consistent with blockchain (according to server), and raises other exceptions if
  6. the server times out or gives an unexpected kind of response.
  7. """
  8. */
  9. export default (network, inpcomp) => {
  10. address = Address.from_pubkey(inpcomp.pubkey)
  11. prevhash = inpcomp.prev_txid[::-1].hex()
  12. prevn = inpcomp.prev_index
  13. sh = address.to_scripthash_hex()
  14. u = network.synchronous_get(('blockchain.scripthash.listunspent', [sh]), timeout=5)
  15. for item in u:
  16. if prevhash == item['tx_hash'] and prevn == item['tx_pos']:
  17. break
  18. else:
  19. raise ValidationError('missing or spent or scriptpubkey mismatch')
  20. check(item['height'] > 0, 'not confirmed')
  21. check(item['value'] == inpcomp.amount, 'amount mismatch')
  22. # Not checked: is it a coinbase? is it matured?
  23. # A feasible strategy to identify unmatured coinbase is to cache the results
  24. # of blockchain.transaction.id_from_pos(height, 0) from the last 100 blocks.
  25. }