validateBlame.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * """ Validate a BlameProof. Can:
  3. - return string indicating why the accused (src) is guilty
  4. - raise ValidationError, if the accuser (dest) was blatantly wrong.
  5. - return input component for further investigation, if everything internal checked out.
  6. """
  7. */
  8. export default (blame, encproof, src_commit_blob, dest_commit_blob, all_components, bad_components, component_feerate) => {
  9. dest_commit = pb.InitialCommitment()
  10. dest_commit.ParseFromString(dest_commit_blob)
  11. dest_pubkey = dest_commit.communication_key
  12. src_commit = pb.InitialCommitment()
  13. src_commit.ParseFromString(src_commit_blob)
  14. decrypter = blame.WhichOneof('decrypter')
  15. if decrypter == 'privkey':
  16. privkey = blame.privkey
  17. check(len(privkey) == 32, 'bad blame privkey')
  18. pubU, pubC = pubkeys_from_privkey(privkey)
  19. check(dest_commit.communication_key == pubC, 'bad blame privkey')
  20. try:
  21. encrypt.decrypt(encproof, privkey)
  22. except encrypt.DecryptionFailed:
  23. # good! the blame was telling us about decryption failure and they were right.
  24. return 'undecryptable'
  25. raise ValidationError('blame gave privkey but decryption worked')
  26. elif decrypter != 'session_key':
  27. raise ValidationError('unknown blame decrypter')
  28. key = blame.session_key
  29. check(len(key) == 32, 'bad blame session key')
  30. try:
  31. proofblob = encrypt.decrypt_with_symmkey(encproof, key)
  32. except encrypt.DecryptionFailed:
  33. raise ValidationError('bad blame session key')
  34. try:
  35. inpcomp = validate_proof_internal(proofblob, src_commit, all_components, bad_components, component_feerate)
  36. except ValidationError as e:
  37. # good! the blame told us something was wrong, and it was right
  38. return e.args[0]
  39. # OK so the proof was good and internally consistent, that means the only
  40. # reason they should be sending us a blame is if it's an inconsistency with
  41. # blockchain.
  42. if not blame.need_lookup_blockchain:
  43. raise ValidationError('blame indicated internal inconsistency, none found!')
  44. if inpcomp is None:
  45. raise ValidationError('blame indicated blockchain error on a non-input component')
  46. return inpcomp
  47. }