onion_error.go 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  1. package lnwire
  2. import (
  3. "bufio"
  4. "bytes"
  5. "crypto/sha256"
  6. "encoding/binary"
  7. "fmt"
  8. "io"
  9. "github.com/davecgh/go-spew/spew"
  10. "github.com/go-errors/errors"
  11. "github.com/lightningnetwork/lnd/fn"
  12. "github.com/lightningnetwork/lnd/tlv"
  13. )
  14. // FailureMessage represents the onion failure object identified by its unique
  15. // failure code.
  16. type FailureMessage interface {
  17. // Code returns a failure code describing the exact nature of the
  18. // error.
  19. Code() FailCode
  20. // Error returns a human readable string describing the error. With
  21. // this method, the FailureMessage interface meets the built-in error
  22. // interface.
  23. Error() string
  24. }
  25. // FailureMessageLength is the size of the failure message plus the size of
  26. // padding. The FailureMessage message should always be EXACTLY this size.
  27. const FailureMessageLength = 256
  28. const (
  29. // FlagBadOnion error flag describes an unparsable, encrypted by
  30. // previous node.
  31. FlagBadOnion FailCode = 0x8000
  32. // FlagPerm error flag indicates a permanent failure.
  33. FlagPerm FailCode = 0x4000
  34. // FlagNode error flag indicates a node failure.
  35. FlagNode FailCode = 0x2000
  36. // FlagUpdate error flag indicates a new channel update is enclosed
  37. // within the error.
  38. FlagUpdate FailCode = 0x1000
  39. )
  40. // FailCode specifies the precise reason that an upstream HTLC was canceled.
  41. // Each UpdateFailHTLC message carries a FailCode which is to be passed
  42. // backwards, encrypted at each step back to the source of the HTLC within the
  43. // route.
  44. type FailCode uint16
  45. // The currently defined onion failure types within this current version of the
  46. // Lightning protocol.
  47. const (
  48. CodeNone FailCode = 0
  49. CodeInvalidRealm = FlagBadOnion | 1
  50. CodeTemporaryNodeFailure = FlagNode | 2
  51. CodePermanentNodeFailure = FlagPerm | FlagNode | 2
  52. CodeRequiredNodeFeatureMissing = FlagPerm | FlagNode | 3
  53. CodeInvalidOnionVersion = FlagBadOnion | FlagPerm | 4
  54. CodeInvalidOnionHmac = FlagBadOnion | FlagPerm | 5
  55. CodeInvalidOnionKey = FlagBadOnion | FlagPerm | 6
  56. CodeTemporaryChannelFailure = FlagUpdate | 7
  57. CodePermanentChannelFailure = FlagPerm | 8
  58. CodeRequiredChannelFeatureMissing = FlagPerm | 9
  59. CodeUnknownNextPeer = FlagPerm | 10
  60. CodeAmountBelowMinimum = FlagUpdate | 11
  61. CodeFeeInsufficient = FlagUpdate | 12
  62. CodeIncorrectCltvExpiry = FlagUpdate | 13
  63. CodeExpiryTooSoon = FlagUpdate | 14
  64. CodeChannelDisabled = FlagUpdate | 20
  65. CodeIncorrectOrUnknownPaymentDetails = FlagPerm | 15
  66. CodeIncorrectPaymentAmount = FlagPerm | 16
  67. CodeFinalExpiryTooSoon FailCode = 17
  68. CodeFinalIncorrectCltvExpiry FailCode = 18
  69. CodeFinalIncorrectHtlcAmount FailCode = 19
  70. CodeExpiryTooFar FailCode = 21
  71. CodeInvalidOnionPayload = FlagPerm | 22
  72. CodeMPPTimeout FailCode = 23
  73. CodeInvalidBlinding = FlagBadOnion | FlagPerm | 24 //nolint:lll
  74. )
  75. // String returns the string representation of the failure code.
  76. func (c FailCode) String() string {
  77. switch c {
  78. case CodeInvalidRealm:
  79. return "InvalidRealm"
  80. case CodeTemporaryNodeFailure:
  81. return "TemporaryNodeFailure"
  82. case CodePermanentNodeFailure:
  83. return "PermanentNodeFailure"
  84. case CodeRequiredNodeFeatureMissing:
  85. return "RequiredNodeFeatureMissing"
  86. case CodeInvalidOnionVersion:
  87. return "InvalidOnionVersion"
  88. case CodeInvalidOnionHmac:
  89. return "InvalidOnionHmac"
  90. case CodeInvalidOnionKey:
  91. return "InvalidOnionKey"
  92. case CodeTemporaryChannelFailure:
  93. return "TemporaryChannelFailure"
  94. case CodePermanentChannelFailure:
  95. return "PermanentChannelFailure"
  96. case CodeRequiredChannelFeatureMissing:
  97. return "RequiredChannelFeatureMissing"
  98. case CodeUnknownNextPeer:
  99. return "UnknownNextPeer"
  100. case CodeAmountBelowMinimum:
  101. return "AmountBelowMinimum"
  102. case CodeFeeInsufficient:
  103. return "FeeInsufficient"
  104. case CodeIncorrectCltvExpiry:
  105. return "IncorrectCltvExpiry"
  106. case CodeIncorrectPaymentAmount:
  107. return "IncorrectPaymentAmount"
  108. case CodeExpiryTooSoon:
  109. return "ExpiryTooSoon"
  110. case CodeChannelDisabled:
  111. return "ChannelDisabled"
  112. case CodeIncorrectOrUnknownPaymentDetails:
  113. return "IncorrectOrUnknownPaymentDetails"
  114. case CodeFinalExpiryTooSoon:
  115. return "FinalExpiryTooSoon"
  116. case CodeFinalIncorrectCltvExpiry:
  117. return "FinalIncorrectCltvExpiry"
  118. case CodeFinalIncorrectHtlcAmount:
  119. return "FinalIncorrectHtlcAmount"
  120. case CodeExpiryTooFar:
  121. return "ExpiryTooFar"
  122. case CodeInvalidOnionPayload:
  123. return "InvalidOnionPayload"
  124. case CodeMPPTimeout:
  125. return "MPPTimeout"
  126. case CodeInvalidBlinding:
  127. return "InvalidBlinding"
  128. default:
  129. return "<unknown>"
  130. }
  131. }
  132. // FailInvalidRealm is returned if the realm byte is unknown.
  133. //
  134. // NOTE: May be returned by any node in the payment route.
  135. type FailInvalidRealm struct{}
  136. // Returns a human readable string describing the target FailureMessage.
  137. //
  138. // NOTE: Implements the error interface.
  139. func (f *FailInvalidRealm) Error() string {
  140. return f.Code().String()
  141. }
  142. // Code returns the failure unique code.
  143. //
  144. // NOTE: Part of the FailureMessage interface.
  145. func (f *FailInvalidRealm) Code() FailCode {
  146. return CodeInvalidRealm
  147. }
  148. // FailTemporaryNodeFailure is returned if an otherwise unspecified transient
  149. // error occurs for the entire node.
  150. //
  151. // NOTE: May be returned by any node in the payment route.
  152. type FailTemporaryNodeFailure struct{}
  153. // Code returns the failure unique code.
  154. // NOTE: Part of the FailureMessage interface.
  155. func (f *FailTemporaryNodeFailure) Code() FailCode {
  156. return CodeTemporaryNodeFailure
  157. }
  158. // Returns a human readable string describing the target FailureMessage.
  159. //
  160. // NOTE: Implements the error interface.
  161. func (f *FailTemporaryNodeFailure) Error() string {
  162. return f.Code().String()
  163. }
  164. // FailPermanentNodeFailure is returned if an otherwise unspecified permanent
  165. // error occurs for the entire node.
  166. //
  167. // NOTE: May be returned by any node in the payment route.
  168. type FailPermanentNodeFailure struct{}
  169. // Code returns the failure unique code.
  170. //
  171. // NOTE: Part of the FailureMessage interface.
  172. func (f *FailPermanentNodeFailure) Code() FailCode {
  173. return CodePermanentNodeFailure
  174. }
  175. // Returns a human readable string describing the target FailureMessage.
  176. //
  177. // NOTE: Implements the error interface.
  178. func (f *FailPermanentNodeFailure) Error() string {
  179. return f.Code().String()
  180. }
  181. // FailRequiredNodeFeatureMissing is returned if a node has requirement
  182. // advertised in its node_announcement features which were not present in the
  183. // onion.
  184. //
  185. // NOTE: May be returned by any node in the payment route.
  186. type FailRequiredNodeFeatureMissing struct{}
  187. // Code returns the failure unique code.
  188. //
  189. // NOTE: Part of the FailureMessage interface.
  190. func (f *FailRequiredNodeFeatureMissing) Code() FailCode {
  191. return CodeRequiredNodeFeatureMissing
  192. }
  193. // Returns a human readable string describing the target FailureMessage.
  194. //
  195. // NOTE: Implements the error interface.
  196. func (f *FailRequiredNodeFeatureMissing) Error() string {
  197. return f.Code().String()
  198. }
  199. // FailPermanentChannelFailure is return if an otherwise unspecified permanent
  200. // error occurs for the outgoing channel (eg. channel (recently).
  201. //
  202. // NOTE: May be returned by any node in the payment route.
  203. type FailPermanentChannelFailure struct{}
  204. // Code returns the failure unique code.
  205. //
  206. // NOTE: Part of the FailureMessage interface.
  207. func (f *FailPermanentChannelFailure) Code() FailCode {
  208. return CodePermanentChannelFailure
  209. }
  210. // Returns a human readable string describing the target FailureMessage.
  211. //
  212. // NOTE: Implements the error interface.
  213. func (f *FailPermanentChannelFailure) Error() string {
  214. return f.Code().String()
  215. }
  216. // FailRequiredChannelFeatureMissing is returned if the outgoing channel has a
  217. // requirement advertised in its channel announcement features which were not
  218. // present in the onion.
  219. //
  220. // NOTE: May only be returned by intermediate nodes.
  221. type FailRequiredChannelFeatureMissing struct{}
  222. // Code returns the failure unique code.
  223. //
  224. // NOTE: Part of the FailureMessage interface.
  225. func (f *FailRequiredChannelFeatureMissing) Code() FailCode {
  226. return CodeRequiredChannelFeatureMissing
  227. }
  228. // Returns a human readable string describing the target FailureMessage.
  229. //
  230. // NOTE: Implements the error interface.
  231. func (f *FailRequiredChannelFeatureMissing) Error() string {
  232. return f.Code().String()
  233. }
  234. // FailUnknownNextPeer is returned if the next peer specified by the onion is
  235. // not known.
  236. //
  237. // NOTE: May only be returned by intermediate nodes.
  238. type FailUnknownNextPeer struct{}
  239. // Code returns the failure unique code.
  240. //
  241. // NOTE: Part of the FailureMessage interface.
  242. func (f *FailUnknownNextPeer) Code() FailCode {
  243. return CodeUnknownNextPeer
  244. }
  245. // Returns a human readable string describing the target FailureMessage.
  246. //
  247. // NOTE: Implements the error interface.
  248. func (f *FailUnknownNextPeer) Error() string {
  249. return f.Code().String()
  250. }
  251. // FailIncorrectPaymentAmount is returned if the amount paid is less than the
  252. // amount expected, the final node MUST fail the HTLC. If the amount paid is
  253. // more than twice the amount expected, the final node SHOULD fail the HTLC.
  254. // This allows the sender to reduce information leakage by altering the amount,
  255. // without allowing accidental gross overpayment.
  256. //
  257. // NOTE: May only be returned by the final node in the path.
  258. type FailIncorrectPaymentAmount struct{}
  259. // Code returns the failure unique code.
  260. //
  261. // NOTE: Part of the FailureMessage interface.
  262. func (f *FailIncorrectPaymentAmount) Code() FailCode {
  263. return CodeIncorrectPaymentAmount
  264. }
  265. // Returns a human readable string describing the target FailureMessage.
  266. //
  267. // NOTE: Implements the error interface.
  268. func (f *FailIncorrectPaymentAmount) Error() string {
  269. return f.Code().String()
  270. }
  271. // FailIncorrectDetails is returned for two reasons:
  272. //
  273. // 1) if the payment hash has already been paid, the final node MAY treat the
  274. // payment hash as unknown, or may succeed in accepting the HTLC. If the
  275. // payment hash is unknown, the final node MUST fail the HTLC.
  276. //
  277. // 2) if the amount paid is less than the amount expected, the final node MUST
  278. // fail the HTLC. If the amount paid is more than twice the amount expected,
  279. // the final node SHOULD fail the HTLC. This allows the sender to reduce
  280. // information leakage by altering the amount, without allowing accidental
  281. // gross overpayment.
  282. //
  283. // NOTE: May only be returned by the final node in the path.
  284. type FailIncorrectDetails struct {
  285. // amount is the value of the extended HTLC.
  286. amount MilliSatoshi
  287. // height is the block height when the htlc was received.
  288. height uint32
  289. // extraOpaqueData contains additional failure message tlv data.
  290. extraOpaqueData ExtraOpaqueData
  291. }
  292. // NewFailIncorrectDetails makes a new instance of the FailIncorrectDetails
  293. // error bound to the specified HTLC amount and acceptance height.
  294. func NewFailIncorrectDetails(amt MilliSatoshi,
  295. height uint32) *FailIncorrectDetails {
  296. return &FailIncorrectDetails{
  297. amount: amt,
  298. height: height,
  299. extraOpaqueData: []byte{},
  300. }
  301. }
  302. // Amount is the value of the extended HTLC.
  303. func (f *FailIncorrectDetails) Amount() MilliSatoshi {
  304. return f.amount
  305. }
  306. // Height is the block height when the htlc was received.
  307. func (f *FailIncorrectDetails) Height() uint32 {
  308. return f.height
  309. }
  310. // ExtraOpaqueData returns additional failure message tlv data.
  311. func (f *FailIncorrectDetails) ExtraOpaqueData() ExtraOpaqueData {
  312. return f.extraOpaqueData
  313. }
  314. // Code returns the failure unique code.
  315. //
  316. // NOTE: Part of the FailureMessage interface.
  317. func (f *FailIncorrectDetails) Code() FailCode {
  318. return CodeIncorrectOrUnknownPaymentDetails
  319. }
  320. // Returns a human readable string describing the target FailureMessage.
  321. //
  322. // NOTE: Implements the error interface.
  323. func (f *FailIncorrectDetails) Error() string {
  324. return fmt.Sprintf(
  325. "%v(amt=%v, height=%v)", CodeIncorrectOrUnknownPaymentDetails,
  326. f.amount, f.height,
  327. )
  328. }
  329. // Decode decodes the failure from bytes stream.
  330. //
  331. // NOTE: Part of the Serializable interface.
  332. func (f *FailIncorrectDetails) Decode(r io.Reader, pver uint32) error {
  333. err := ReadElement(r, &f.amount)
  334. switch {
  335. // This is an optional tack on that was added later in the protocol. As
  336. // a result, older nodes may not include this value. We'll account for
  337. // this by checking for io.EOF here which means that no bytes were read
  338. // at all.
  339. case err == io.EOF:
  340. return nil
  341. case err != nil:
  342. return err
  343. }
  344. // At a later stage, the height field was also tacked on. We need to
  345. // check for io.EOF here as well.
  346. err = ReadElement(r, &f.height)
  347. switch {
  348. case err == io.EOF:
  349. return nil
  350. case err != nil:
  351. return err
  352. }
  353. return f.extraOpaqueData.Decode(r)
  354. }
  355. // Encode writes the failure in bytes stream.
  356. //
  357. // NOTE: Part of the Serializable interface.
  358. func (f *FailIncorrectDetails) Encode(w *bytes.Buffer, pver uint32) error {
  359. if err := WriteMilliSatoshi(w, f.amount); err != nil {
  360. return err
  361. }
  362. if err := WriteUint32(w, f.height); err != nil {
  363. return err
  364. }
  365. return f.extraOpaqueData.Encode(w)
  366. }
  367. // FailFinalExpiryTooSoon is returned if the cltv_expiry is too low, the final
  368. // node MUST fail the HTLC.
  369. //
  370. // NOTE: May only be returned by the final node in the path.
  371. type FailFinalExpiryTooSoon struct{}
  372. // Code returns the failure unique code.
  373. //
  374. // NOTE: Part of the FailureMessage interface.
  375. func (f *FailFinalExpiryTooSoon) Code() FailCode {
  376. return CodeFinalExpiryTooSoon
  377. }
  378. // Returns a human readable string describing the target FailureMessage.
  379. //
  380. // NOTE: Implements the error interface.
  381. func (f *FailFinalExpiryTooSoon) Error() string {
  382. return f.Code().String()
  383. }
  384. // NewFinalExpiryTooSoon creates new instance of the FailFinalExpiryTooSoon.
  385. func NewFinalExpiryTooSoon() *FailFinalExpiryTooSoon {
  386. return &FailFinalExpiryTooSoon{}
  387. }
  388. // FailInvalidOnionVersion is returned if the onion version byte is unknown.
  389. //
  390. // NOTE: May be returned only by intermediate nodes.
  391. type FailInvalidOnionVersion struct {
  392. // OnionSHA256 hash of the onion blob which haven't been proceeded.
  393. OnionSHA256 [sha256.Size]byte
  394. }
  395. // Returns a human readable string describing the target FailureMessage.
  396. //
  397. // NOTE: Implements the error interface.
  398. func (f *FailInvalidOnionVersion) Error() string {
  399. return fmt.Sprintf("InvalidOnionVersion(onion_sha=%x)", f.OnionSHA256[:])
  400. }
  401. // NewInvalidOnionVersion creates new instance of the FailInvalidOnionVersion.
  402. func NewInvalidOnionVersion(onion []byte) *FailInvalidOnionVersion {
  403. return &FailInvalidOnionVersion{OnionSHA256: sha256.Sum256(onion)}
  404. }
  405. // Code returns the failure unique code.
  406. //
  407. // NOTE: Part of the FailureMessage interface.
  408. func (f *FailInvalidOnionVersion) Code() FailCode {
  409. return CodeInvalidOnionVersion
  410. }
  411. // Decode decodes the failure from bytes stream.
  412. //
  413. // NOTE: Part of the Serializable interface.
  414. func (f *FailInvalidOnionVersion) Decode(r io.Reader, pver uint32) error {
  415. return ReadElement(r, f.OnionSHA256[:])
  416. }
  417. // Encode writes the failure in bytes stream.
  418. //
  419. // NOTE: Part of the Serializable interface.
  420. func (f *FailInvalidOnionVersion) Encode(w *bytes.Buffer, pver uint32) error {
  421. return WriteBytes(w, f.OnionSHA256[:])
  422. }
  423. // FailInvalidOnionHmac is return if the onion HMAC is incorrect.
  424. //
  425. // NOTE: May only be returned by intermediate nodes.
  426. type FailInvalidOnionHmac struct {
  427. // OnionSHA256 hash of the onion blob which haven't been proceeded.
  428. OnionSHA256 [sha256.Size]byte
  429. }
  430. // NewInvalidOnionHmac creates new instance of the FailInvalidOnionHmac.
  431. func NewInvalidOnionHmac(onion []byte) *FailInvalidOnionHmac {
  432. return &FailInvalidOnionHmac{OnionSHA256: sha256.Sum256(onion)}
  433. }
  434. // Code returns the failure unique code.
  435. //
  436. // NOTE: Part of the FailureMessage interface.
  437. func (f *FailInvalidOnionHmac) Code() FailCode {
  438. return CodeInvalidOnionHmac
  439. }
  440. // Decode decodes the failure from bytes stream.
  441. //
  442. // NOTE: Part of the Serializable interface.
  443. func (f *FailInvalidOnionHmac) Decode(r io.Reader, pver uint32) error {
  444. return ReadElement(r, f.OnionSHA256[:])
  445. }
  446. // Encode writes the failure in bytes stream.
  447. //
  448. // NOTE: Part of the Serializable interface.
  449. func (f *FailInvalidOnionHmac) Encode(w *bytes.Buffer, pver uint32) error {
  450. return WriteBytes(w, f.OnionSHA256[:])
  451. }
  452. // Returns a human readable string describing the target FailureMessage.
  453. //
  454. // NOTE: Implements the error interface.
  455. func (f *FailInvalidOnionHmac) Error() string {
  456. return fmt.Sprintf("InvalidOnionHMAC(onion_sha=%x)", f.OnionSHA256[:])
  457. }
  458. // FailInvalidOnionKey is return if the ephemeral key in the onion is
  459. // unparsable.
  460. //
  461. // NOTE: May only be returned by intermediate nodes.
  462. type FailInvalidOnionKey struct {
  463. // OnionSHA256 hash of the onion blob which haven't been proceeded.
  464. OnionSHA256 [sha256.Size]byte
  465. }
  466. // NewInvalidOnionKey creates new instance of the FailInvalidOnionKey.
  467. func NewInvalidOnionKey(onion []byte) *FailInvalidOnionKey {
  468. return &FailInvalidOnionKey{OnionSHA256: sha256.Sum256(onion)}
  469. }
  470. // Code returns the failure unique code.
  471. //
  472. // NOTE: Part of the FailureMessage interface.
  473. func (f *FailInvalidOnionKey) Code() FailCode {
  474. return CodeInvalidOnionKey
  475. }
  476. // Decode decodes the failure from bytes stream.
  477. //
  478. // NOTE: Part of the Serializable interface.
  479. func (f *FailInvalidOnionKey) Decode(r io.Reader, pver uint32) error {
  480. return ReadElement(r, f.OnionSHA256[:])
  481. }
  482. // Encode writes the failure in bytes stream.
  483. //
  484. // NOTE: Part of the Serializable interface.
  485. func (f *FailInvalidOnionKey) Encode(w *bytes.Buffer, pver uint32) error {
  486. return WriteBytes(w, f.OnionSHA256[:])
  487. }
  488. // Returns a human readable string describing the target FailureMessage.
  489. //
  490. // NOTE: Implements the error interface.
  491. func (f *FailInvalidOnionKey) Error() string {
  492. return fmt.Sprintf("InvalidOnionKey(onion_sha=%x)", f.OnionSHA256[:])
  493. }
  494. // parseChannelUpdateCompatibilityMode will attempt to parse a channel updated
  495. // encoded into an onion error payload in two ways. First, we'll try the
  496. // compatibility oriented version wherein we'll _skip_ the length prefixing on
  497. // the channel update message. Older versions of c-lighting do this so we'll
  498. // attempt to parse these messages in order to retain compatibility. If we're
  499. // unable to pull out a fully valid version, then we'll fall back to the
  500. // regular parsing mechanism which includes the length prefix an NO type byte.
  501. func parseChannelUpdateCompatibilityMode(reader io.Reader, length uint16,
  502. chanUpdate *ChannelUpdate, pver uint32) error {
  503. // Instantiate a LimitReader because there may be additional data
  504. // present after the channel update. Without limiting the stream, the
  505. // additional data would be interpreted as channel update tlv data.
  506. limitReader := io.LimitReader(reader, int64(length))
  507. r := bufio.NewReader(limitReader)
  508. // We'll peek out two bytes from the buffer without advancing the
  509. // buffer so we can decide how to parse the remainder of it.
  510. maybeTypeBytes, err := r.Peek(2)
  511. if err != nil {
  512. return err
  513. }
  514. // Some nodes well prefix an additional set of bytes in front of their
  515. // channel updates. These bytes will _almost_ always be 258 or the type
  516. // of the ChannelUpdate message.
  517. typeInt := binary.BigEndian.Uint16(maybeTypeBytes)
  518. if typeInt == MsgChannelUpdate {
  519. // At this point it's likely the case that this is a channel
  520. // update message with its type prefixed, so we'll snip off the
  521. // first two bytes and parse it as normal.
  522. var throwAwayTypeBytes [2]byte
  523. _, err := r.Read(throwAwayTypeBytes[:])
  524. if err != nil {
  525. return err
  526. }
  527. }
  528. // At this pint, we've either decided to keep the entire thing, or snip
  529. // off the first two bytes. In either case, we can just read it as
  530. // normal.
  531. return chanUpdate.Decode(r, pver)
  532. }
  533. // FailTemporaryChannelFailure is if an otherwise unspecified transient error
  534. // occurs for the outgoing channel (eg. channel capacity reached, too many
  535. // in-flight htlcs)
  536. //
  537. // NOTE: May only be returned by intermediate nodes.
  538. type FailTemporaryChannelFailure struct {
  539. // Update is used to update information about state of the channel
  540. // which caused the failure.
  541. //
  542. // NOTE: This field is optional.
  543. Update *ChannelUpdate
  544. }
  545. // NewTemporaryChannelFailure creates new instance of the FailTemporaryChannelFailure.
  546. func NewTemporaryChannelFailure(update *ChannelUpdate) *FailTemporaryChannelFailure {
  547. return &FailTemporaryChannelFailure{Update: update}
  548. }
  549. // Code returns the failure unique code.
  550. //
  551. // NOTE: Part of the FailureMessage interface.
  552. func (f *FailTemporaryChannelFailure) Code() FailCode {
  553. return CodeTemporaryChannelFailure
  554. }
  555. // Returns a human readable string describing the target FailureMessage.
  556. //
  557. // NOTE: Implements the error interface.
  558. func (f *FailTemporaryChannelFailure) Error() string {
  559. if f.Update == nil {
  560. return f.Code().String()
  561. }
  562. return fmt.Sprintf("TemporaryChannelFailure(update=%v)",
  563. spew.Sdump(f.Update))
  564. }
  565. // Decode decodes the failure from bytes stream.
  566. //
  567. // NOTE: Part of the Serializable interface.
  568. func (f *FailTemporaryChannelFailure) Decode(r io.Reader, pver uint32) error {
  569. var length uint16
  570. err := ReadElement(r, &length)
  571. if err != nil {
  572. return err
  573. }
  574. if length != 0 {
  575. f.Update = &ChannelUpdate{}
  576. return parseChannelUpdateCompatibilityMode(
  577. r, length, f.Update, pver,
  578. )
  579. }
  580. return nil
  581. }
  582. // Encode writes the failure in bytes stream.
  583. //
  584. // NOTE: Part of the Serializable interface.
  585. func (f *FailTemporaryChannelFailure) Encode(w *bytes.Buffer,
  586. pver uint32) error {
  587. if f.Update != nil {
  588. return writeOnionErrorChanUpdate(w, f.Update, pver)
  589. }
  590. // Write zero length to indicate no channel_update is present.
  591. return WriteUint16(w, 0)
  592. }
  593. // FailAmountBelowMinimum is returned if the HTLC does not reach the current
  594. // minimum amount, we tell them the amount of the incoming HTLC and the current
  595. // channel setting for the outgoing channel.
  596. //
  597. // NOTE: May only be returned by the intermediate nodes in the path.
  598. type FailAmountBelowMinimum struct {
  599. // HtlcMsat is the wrong amount of the incoming HTLC.
  600. HtlcMsat MilliSatoshi
  601. // Update is used to update information about state of the channel
  602. // which caused the failure.
  603. Update ChannelUpdate
  604. }
  605. // NewAmountBelowMinimum creates new instance of the FailAmountBelowMinimum.
  606. func NewAmountBelowMinimum(htlcMsat MilliSatoshi,
  607. update ChannelUpdate) *FailAmountBelowMinimum {
  608. return &FailAmountBelowMinimum{
  609. HtlcMsat: htlcMsat,
  610. Update: update,
  611. }
  612. }
  613. // Code returns the failure unique code.
  614. //
  615. // NOTE: Part of the FailureMessage interface.
  616. func (f *FailAmountBelowMinimum) Code() FailCode {
  617. return CodeAmountBelowMinimum
  618. }
  619. // Returns a human readable string describing the target FailureMessage.
  620. //
  621. // NOTE: Implements the error interface.
  622. func (f *FailAmountBelowMinimum) Error() string {
  623. return fmt.Sprintf("AmountBelowMinimum(amt=%v, update=%v", f.HtlcMsat,
  624. spew.Sdump(f.Update))
  625. }
  626. // Decode decodes the failure from bytes stream.
  627. //
  628. // NOTE: Part of the Serializable interface.
  629. func (f *FailAmountBelowMinimum) Decode(r io.Reader, pver uint32) error {
  630. if err := ReadElement(r, &f.HtlcMsat); err != nil {
  631. return err
  632. }
  633. var length uint16
  634. if err := ReadElement(r, &length); err != nil {
  635. return err
  636. }
  637. f.Update = ChannelUpdate{}
  638. return parseChannelUpdateCompatibilityMode(
  639. r, length, &f.Update, pver,
  640. )
  641. }
  642. // Encode writes the failure in bytes stream.
  643. //
  644. // NOTE: Part of the Serializable interface.
  645. func (f *FailAmountBelowMinimum) Encode(w *bytes.Buffer, pver uint32) error {
  646. if err := WriteMilliSatoshi(w, f.HtlcMsat); err != nil {
  647. return err
  648. }
  649. return writeOnionErrorChanUpdate(w, &f.Update, pver)
  650. }
  651. // FailFeeInsufficient is returned if the HTLC does not pay sufficient fee, we
  652. // tell them the amount of the incoming HTLC and the current channel setting
  653. // for the outgoing channel.
  654. //
  655. // NOTE: May only be returned by intermediate nodes.
  656. type FailFeeInsufficient struct {
  657. // HtlcMsat is the wrong amount of the incoming HTLC.
  658. HtlcMsat MilliSatoshi
  659. // Update is used to update information about state of the channel
  660. // which caused the failure.
  661. Update ChannelUpdate
  662. }
  663. // NewFeeInsufficient creates new instance of the FailFeeInsufficient.
  664. func NewFeeInsufficient(htlcMsat MilliSatoshi,
  665. update ChannelUpdate) *FailFeeInsufficient {
  666. return &FailFeeInsufficient{
  667. HtlcMsat: htlcMsat,
  668. Update: update,
  669. }
  670. }
  671. // Code returns the failure unique code.
  672. //
  673. // NOTE: Part of the FailureMessage interface.
  674. func (f *FailFeeInsufficient) Code() FailCode {
  675. return CodeFeeInsufficient
  676. }
  677. // Returns a human readable string describing the target FailureMessage.
  678. //
  679. // NOTE: Implements the error interface.
  680. func (f *FailFeeInsufficient) Error() string {
  681. return fmt.Sprintf("FeeInsufficient(htlc_amt==%v, update=%v", f.HtlcMsat,
  682. spew.Sdump(f.Update))
  683. }
  684. // Decode decodes the failure from bytes stream.
  685. //
  686. // NOTE: Part of the Serializable interface.
  687. func (f *FailFeeInsufficient) Decode(r io.Reader, pver uint32) error {
  688. if err := ReadElement(r, &f.HtlcMsat); err != nil {
  689. return err
  690. }
  691. var length uint16
  692. if err := ReadElement(r, &length); err != nil {
  693. return err
  694. }
  695. f.Update = ChannelUpdate{}
  696. return parseChannelUpdateCompatibilityMode(
  697. r, length, &f.Update, pver,
  698. )
  699. }
  700. // Encode writes the failure in bytes stream.
  701. //
  702. // NOTE: Part of the Serializable interface.
  703. func (f *FailFeeInsufficient) Encode(w *bytes.Buffer, pver uint32) error {
  704. if err := WriteMilliSatoshi(w, f.HtlcMsat); err != nil {
  705. return err
  706. }
  707. return writeOnionErrorChanUpdate(w, &f.Update, pver)
  708. }
  709. // FailIncorrectCltvExpiry is returned if outgoing cltv value does not match
  710. // the update add htlc's cltv expiry minus cltv expiry delta for the outgoing
  711. // channel, we tell them the cltv expiry and the current channel setting for
  712. // the outgoing channel.
  713. //
  714. // NOTE: May only be returned by intermediate nodes.
  715. type FailIncorrectCltvExpiry struct {
  716. // CltvExpiry is the wrong absolute timeout in blocks, after which
  717. // outgoing HTLC expires.
  718. CltvExpiry uint32
  719. // Update is used to update information about state of the channel
  720. // which caused the failure.
  721. Update ChannelUpdate
  722. }
  723. // NewIncorrectCltvExpiry creates new instance of the FailIncorrectCltvExpiry.
  724. func NewIncorrectCltvExpiry(cltvExpiry uint32,
  725. update ChannelUpdate) *FailIncorrectCltvExpiry {
  726. return &FailIncorrectCltvExpiry{
  727. CltvExpiry: cltvExpiry,
  728. Update: update,
  729. }
  730. }
  731. // Code returns the failure unique code.
  732. //
  733. // NOTE: Part of the FailureMessage interface.
  734. func (f *FailIncorrectCltvExpiry) Code() FailCode {
  735. return CodeIncorrectCltvExpiry
  736. }
  737. func (f *FailIncorrectCltvExpiry) Error() string {
  738. return fmt.Sprintf("IncorrectCltvExpiry(expiry=%v, update=%v",
  739. f.CltvExpiry, spew.Sdump(f.Update))
  740. }
  741. // Decode decodes the failure from bytes stream.
  742. //
  743. // NOTE: Part of the Serializable interface.
  744. func (f *FailIncorrectCltvExpiry) Decode(r io.Reader, pver uint32) error {
  745. if err := ReadElement(r, &f.CltvExpiry); err != nil {
  746. return err
  747. }
  748. var length uint16
  749. if err := ReadElement(r, &length); err != nil {
  750. return err
  751. }
  752. f.Update = ChannelUpdate{}
  753. return parseChannelUpdateCompatibilityMode(
  754. r, length, &f.Update, pver,
  755. )
  756. }
  757. // Encode writes the failure in bytes stream.
  758. //
  759. // NOTE: Part of the Serializable interface.
  760. func (f *FailIncorrectCltvExpiry) Encode(w *bytes.Buffer, pver uint32) error {
  761. if err := WriteUint32(w, f.CltvExpiry); err != nil {
  762. return err
  763. }
  764. return writeOnionErrorChanUpdate(w, &f.Update, pver)
  765. }
  766. // FailExpiryTooSoon is returned if the ctlv-expiry is too near, we tell them
  767. // the current channel setting for the outgoing channel.
  768. //
  769. // NOTE: May only be returned by intermediate nodes.
  770. type FailExpiryTooSoon struct {
  771. // Update is used to update information about state of the channel
  772. // which caused the failure.
  773. Update ChannelUpdate
  774. }
  775. // NewExpiryTooSoon creates new instance of the FailExpiryTooSoon.
  776. func NewExpiryTooSoon(update ChannelUpdate) *FailExpiryTooSoon {
  777. return &FailExpiryTooSoon{
  778. Update: update,
  779. }
  780. }
  781. // Code returns the failure unique code.
  782. //
  783. // NOTE: Part of the FailureMessage interface.
  784. func (f *FailExpiryTooSoon) Code() FailCode {
  785. return CodeExpiryTooSoon
  786. }
  787. // Returns a human readable string describing the target FailureMessage.
  788. //
  789. // NOTE: Implements the error interface.
  790. func (f *FailExpiryTooSoon) Error() string {
  791. return fmt.Sprintf("ExpiryTooSoon(update=%v", spew.Sdump(f.Update))
  792. }
  793. // Decode decodes the failure from l stream.
  794. //
  795. // NOTE: Part of the Serializable interface.
  796. func (f *FailExpiryTooSoon) Decode(r io.Reader, pver uint32) error {
  797. var length uint16
  798. if err := ReadElement(r, &length); err != nil {
  799. return err
  800. }
  801. f.Update = ChannelUpdate{}
  802. return parseChannelUpdateCompatibilityMode(
  803. r, length, &f.Update, pver,
  804. )
  805. }
  806. // Encode writes the failure in bytes stream.
  807. //
  808. // NOTE: Part of the Serializable interface.
  809. func (f *FailExpiryTooSoon) Encode(w *bytes.Buffer, pver uint32) error {
  810. return writeOnionErrorChanUpdate(w, &f.Update, pver)
  811. }
  812. // FailChannelDisabled is returned if the channel is disabled, we tell them the
  813. // current channel setting for the outgoing channel.
  814. //
  815. // NOTE: May only be returned by intermediate nodes.
  816. type FailChannelDisabled struct {
  817. // Flags least-significant bit must be set to 0 if the creating node
  818. // corresponds to the first node in the previously sent channel
  819. // announcement and 1 otherwise.
  820. Flags uint16
  821. // Update is used to update information about state of the channel
  822. // which caused the failure.
  823. Update ChannelUpdate
  824. }
  825. // NewChannelDisabled creates new instance of the FailChannelDisabled.
  826. func NewChannelDisabled(flags uint16, update ChannelUpdate) *FailChannelDisabled {
  827. return &FailChannelDisabled{
  828. Flags: flags,
  829. Update: update,
  830. }
  831. }
  832. // Code returns the failure unique code.
  833. //
  834. // NOTE: Part of the FailureMessage interface.
  835. func (f *FailChannelDisabled) Code() FailCode {
  836. return CodeChannelDisabled
  837. }
  838. // Returns a human readable string describing the target FailureMessage.
  839. //
  840. // NOTE: Implements the error interface.
  841. func (f *FailChannelDisabled) Error() string {
  842. return fmt.Sprintf("ChannelDisabled(flags=%v, update=%v", f.Flags,
  843. spew.Sdump(f.Update))
  844. }
  845. // Decode decodes the failure from bytes stream.
  846. //
  847. // NOTE: Part of the Serializable interface.
  848. func (f *FailChannelDisabled) Decode(r io.Reader, pver uint32) error {
  849. if err := ReadElement(r, &f.Flags); err != nil {
  850. return err
  851. }
  852. var length uint16
  853. if err := ReadElement(r, &length); err != nil {
  854. return err
  855. }
  856. f.Update = ChannelUpdate{}
  857. return parseChannelUpdateCompatibilityMode(
  858. r, length, &f.Update, pver,
  859. )
  860. }
  861. // Encode writes the failure in bytes stream.
  862. //
  863. // NOTE: Part of the Serializable interface.
  864. func (f *FailChannelDisabled) Encode(w *bytes.Buffer, pver uint32) error {
  865. if err := WriteUint16(w, f.Flags); err != nil {
  866. return err
  867. }
  868. return writeOnionErrorChanUpdate(w, &f.Update, pver)
  869. }
  870. // FailFinalIncorrectCltvExpiry is returned if the outgoing_cltv_value does not
  871. // match the ctlv_expiry of the HTLC at the final hop.
  872. //
  873. // NOTE: might be returned by final node only.
  874. type FailFinalIncorrectCltvExpiry struct {
  875. // CltvExpiry is the wrong absolute timeout in blocks, after which
  876. // outgoing HTLC expires.
  877. CltvExpiry uint32
  878. }
  879. // Returns a human readable string describing the target FailureMessage.
  880. //
  881. // NOTE: Implements the error interface.
  882. func (f *FailFinalIncorrectCltvExpiry) Error() string {
  883. return fmt.Sprintf("FinalIncorrectCltvExpiry(expiry=%v)", f.CltvExpiry)
  884. }
  885. // NewFinalIncorrectCltvExpiry creates new instance of the
  886. // FailFinalIncorrectCltvExpiry.
  887. func NewFinalIncorrectCltvExpiry(cltvExpiry uint32) *FailFinalIncorrectCltvExpiry {
  888. return &FailFinalIncorrectCltvExpiry{
  889. CltvExpiry: cltvExpiry,
  890. }
  891. }
  892. // Code returns the failure unique code.
  893. //
  894. // NOTE: Part of the FailureMessage interface.
  895. func (f *FailFinalIncorrectCltvExpiry) Code() FailCode {
  896. return CodeFinalIncorrectCltvExpiry
  897. }
  898. // Decode decodes the failure from bytes stream.
  899. //
  900. // NOTE: Part of the Serializable interface.
  901. func (f *FailFinalIncorrectCltvExpiry) Decode(r io.Reader, pver uint32) error {
  902. return ReadElement(r, &f.CltvExpiry)
  903. }
  904. // Encode writes the failure in bytes stream.
  905. //
  906. // NOTE: Part of the Serializable interface.
  907. func (f *FailFinalIncorrectCltvExpiry) Encode(w *bytes.Buffer,
  908. pver uint32) error {
  909. return WriteUint32(w, f.CltvExpiry)
  910. }
  911. // FailFinalIncorrectHtlcAmount is returned if the amt_to_forward is higher
  912. // than incoming_htlc_amt of the HTLC at the final hop.
  913. //
  914. // NOTE: May only be returned by the final node.
  915. type FailFinalIncorrectHtlcAmount struct {
  916. // IncomingHTLCAmount is the wrong forwarded htlc amount.
  917. IncomingHTLCAmount MilliSatoshi
  918. }
  919. // Returns a human readable string describing the target FailureMessage.
  920. //
  921. // NOTE: Implements the error interface.
  922. func (f *FailFinalIncorrectHtlcAmount) Error() string {
  923. return fmt.Sprintf("FinalIncorrectHtlcAmount(amt=%v)",
  924. f.IncomingHTLCAmount)
  925. }
  926. // NewFinalIncorrectHtlcAmount creates new instance of the
  927. // FailFinalIncorrectHtlcAmount.
  928. func NewFinalIncorrectHtlcAmount(amount MilliSatoshi) *FailFinalIncorrectHtlcAmount {
  929. return &FailFinalIncorrectHtlcAmount{
  930. IncomingHTLCAmount: amount,
  931. }
  932. }
  933. // Code returns the failure unique code.
  934. //
  935. // NOTE: Part of the FailureMessage interface.
  936. func (f *FailFinalIncorrectHtlcAmount) Code() FailCode {
  937. return CodeFinalIncorrectHtlcAmount
  938. }
  939. // Decode decodes the failure from bytes stream.
  940. //
  941. // NOTE: Part of the Serializable interface.
  942. func (f *FailFinalIncorrectHtlcAmount) Decode(r io.Reader, pver uint32) error {
  943. return ReadElement(r, &f.IncomingHTLCAmount)
  944. }
  945. // Encode writes the failure in bytes stream.
  946. //
  947. // NOTE: Part of the Serializable interface.
  948. func (f *FailFinalIncorrectHtlcAmount) Encode(w *bytes.Buffer,
  949. pver uint32) error {
  950. return WriteMilliSatoshi(w, f.IncomingHTLCAmount)
  951. }
  952. // FailExpiryTooFar is returned if the CLTV expiry in the HTLC is too far in the
  953. // future.
  954. //
  955. // NOTE: May be returned by any node in the payment route.
  956. type FailExpiryTooFar struct{}
  957. // Code returns the failure unique code.
  958. //
  959. // NOTE: Part of the FailureMessage interface.
  960. func (f *FailExpiryTooFar) Code() FailCode {
  961. return CodeExpiryTooFar
  962. }
  963. // Returns a human readable string describing the target FailureMessage.
  964. //
  965. // NOTE: Implements the error interface.
  966. func (f *FailExpiryTooFar) Error() string {
  967. return f.Code().String()
  968. }
  969. // InvalidOnionPayload is returned if the hop could not process the TLV payload
  970. // enclosed in the onion.
  971. type InvalidOnionPayload struct {
  972. // Type is the TLV type that caused the specific failure.
  973. Type uint64
  974. // Offset is the byte offset within the payload where the failure
  975. // occurred.
  976. Offset uint16
  977. }
  978. // NewInvalidOnionPayload initializes a new InvalidOnionPayload failure.
  979. func NewInvalidOnionPayload(typ uint64, offset uint16) *InvalidOnionPayload {
  980. return &InvalidOnionPayload{
  981. Type: typ,
  982. Offset: offset,
  983. }
  984. }
  985. // Code returns the failure unique code.
  986. //
  987. // NOTE: Part of the FailureMessage interface.
  988. func (f *InvalidOnionPayload) Code() FailCode {
  989. return CodeInvalidOnionPayload
  990. }
  991. // Returns a human readable string describing the target FailureMessage.
  992. //
  993. // NOTE: Implements the error interface.
  994. func (f *InvalidOnionPayload) Error() string {
  995. return fmt.Sprintf("%v(type=%v, offset=%d)",
  996. f.Code(), f.Type, f.Offset)
  997. }
  998. // Decode decodes the failure from bytes stream.
  999. //
  1000. // NOTE: Part of the Serializable interface.
  1001. func (f *InvalidOnionPayload) Decode(r io.Reader, pver uint32) error {
  1002. var buf [8]byte
  1003. typ, err := tlv.ReadVarInt(r, &buf)
  1004. if err != nil {
  1005. return err
  1006. }
  1007. f.Type = typ
  1008. return ReadElements(r, &f.Offset)
  1009. }
  1010. // Encode writes the failure in bytes stream.
  1011. //
  1012. // NOTE: Part of the Serializable interface.
  1013. func (f *InvalidOnionPayload) Encode(w *bytes.Buffer, pver uint32) error {
  1014. var buf [8]byte
  1015. if err := tlv.WriteVarInt(w, f.Type, &buf); err != nil {
  1016. return err
  1017. }
  1018. return WriteUint16(w, f.Offset)
  1019. }
  1020. // FailMPPTimeout is returned if the complete amount for a multi part payment
  1021. // was not received within a reasonable time.
  1022. //
  1023. // NOTE: May only be returned by the final node in the path.
  1024. type FailMPPTimeout struct{}
  1025. // Code returns the failure unique code.
  1026. //
  1027. // NOTE: Part of the FailureMessage interface.
  1028. func (f *FailMPPTimeout) Code() FailCode {
  1029. return CodeMPPTimeout
  1030. }
  1031. // Returns a human readable string describing the target FailureMessage.
  1032. //
  1033. // NOTE: Implements the error interface.
  1034. func (f *FailMPPTimeout) Error() string {
  1035. return f.Code().String()
  1036. }
  1037. // FailInvalidBlinding is returned if there has been a route blinding related
  1038. // error.
  1039. type FailInvalidBlinding struct {
  1040. OnionSHA256 [sha256.Size]byte
  1041. }
  1042. // Code returns the failure unique code.
  1043. //
  1044. // NOTE: Part of the FailureMessage interface.
  1045. func (f *FailInvalidBlinding) Code() FailCode {
  1046. return CodeInvalidBlinding
  1047. }
  1048. // Returns a human readable string describing the target FailureMessage.
  1049. //
  1050. // NOTE: Implements the error interface.
  1051. func (f *FailInvalidBlinding) Error() string {
  1052. return f.Code().String()
  1053. }
  1054. // Decode decodes the failure from bytes stream.
  1055. //
  1056. // NOTE: Part of the Serializable interface.
  1057. func (f *FailInvalidBlinding) Decode(r io.Reader, _ uint32) error {
  1058. return ReadElement(r, f.OnionSHA256[:])
  1059. }
  1060. // Encode writes the failure in bytes stream.
  1061. //
  1062. // NOTE: Part of the Serializable interface.
  1063. func (f *FailInvalidBlinding) Encode(w *bytes.Buffer, _ uint32) error {
  1064. return WriteBytes(w, f.OnionSHA256[:])
  1065. }
  1066. // NewInvalidBlinding creates new instance of FailInvalidBlinding.
  1067. func NewInvalidBlinding(
  1068. onion fn.Option[[OnionPacketSize]byte]) *FailInvalidBlinding {
  1069. // The spec allows empty onion hashes for invalid blinding, so we only
  1070. // include our onion hash if it's provided.
  1071. if onion.IsNone() {
  1072. return &FailInvalidBlinding{}
  1073. }
  1074. shaSum := fn.MapOptionZ(onion, func(o [OnionPacketSize]byte) [32]byte {
  1075. return sha256.Sum256(o[:])
  1076. })
  1077. return &FailInvalidBlinding{OnionSHA256: shaSum}
  1078. }
  1079. // DecodeFailure decodes, validates, and parses the lnwire onion failure, for
  1080. // the provided protocol version.
  1081. func DecodeFailure(r io.Reader, pver uint32) (FailureMessage, error) {
  1082. // First, we'll parse out the encapsulated failure message itself. This
  1083. // is a 2 byte length followed by the payload itself.
  1084. var failureLength uint16
  1085. if err := ReadElement(r, &failureLength); err != nil {
  1086. return nil, fmt.Errorf("unable to read failure len: %w", err)
  1087. }
  1088. failureData := make([]byte, failureLength)
  1089. if _, err := io.ReadFull(r, failureData); err != nil {
  1090. return nil, fmt.Errorf("unable to full read payload of "+
  1091. "%v: %w", failureLength, err)
  1092. }
  1093. // Read the padding.
  1094. var padLength uint16
  1095. if err := ReadElement(r, &padLength); err != nil {
  1096. return nil, fmt.Errorf("unable to read pad len: %w", err)
  1097. }
  1098. if _, err := io.CopyN(io.Discard, r, int64(padLength)); err != nil {
  1099. return nil, fmt.Errorf("unable to read padding %w", err)
  1100. }
  1101. // Verify that we are at the end of the stream now.
  1102. scratch := make([]byte, 1)
  1103. _, err := r.Read(scratch)
  1104. if err != io.EOF {
  1105. return nil, fmt.Errorf("unexpected failure bytes")
  1106. }
  1107. // Check the total length. Convert to 32 bits to prevent overflow.
  1108. totalLength := uint32(padLength) + uint32(failureLength)
  1109. if totalLength < FailureMessageLength {
  1110. return nil, fmt.Errorf("failure message too short: "+
  1111. "msg=%v, pad=%v, total=%v",
  1112. failureLength, padLength, totalLength)
  1113. }
  1114. // Decode the failure message.
  1115. dataReader := bytes.NewReader(failureData)
  1116. return DecodeFailureMessage(dataReader, pver)
  1117. }
  1118. // DecodeFailureMessage decodes just the failure message, ignoring any padding
  1119. // that may be present at the end.
  1120. func DecodeFailureMessage(r io.Reader, pver uint32) (FailureMessage, error) {
  1121. // Once we have the failure data, we can obtain the failure code from
  1122. // the first two bytes of the buffer.
  1123. var codeBytes [2]byte
  1124. if _, err := io.ReadFull(r, codeBytes[:]); err != nil {
  1125. return nil, fmt.Errorf("unable to read failure code: %w", err)
  1126. }
  1127. failCode := FailCode(binary.BigEndian.Uint16(codeBytes[:]))
  1128. // Create the empty failure by given code and populate the failure with
  1129. // additional data if needed.
  1130. failure, err := makeEmptyOnionError(failCode)
  1131. if err != nil {
  1132. return nil, fmt.Errorf("unable to make empty error: %w", err)
  1133. }
  1134. // Finally, if this failure has a payload, then we'll read that now as
  1135. // well.
  1136. switch f := failure.(type) {
  1137. case Serializable:
  1138. if err := f.Decode(r, pver); err != nil {
  1139. return nil, fmt.Errorf("unable to decode error "+
  1140. "update (type=%T): %v", failure, err)
  1141. }
  1142. }
  1143. return failure, nil
  1144. }
  1145. // EncodeFailure encodes, including the necessary onion failure header
  1146. // information.
  1147. func EncodeFailure(w *bytes.Buffer, failure FailureMessage, pver uint32) error {
  1148. var failureMessageBuffer bytes.Buffer
  1149. err := EncodeFailureMessage(&failureMessageBuffer, failure, pver)
  1150. if err != nil {
  1151. return err
  1152. }
  1153. // The combined size of this message must be below the max allowed
  1154. // failure message length.
  1155. failureMessage := failureMessageBuffer.Bytes()
  1156. if len(failureMessage) > FailureMessageLength {
  1157. return fmt.Errorf("failure message exceed max "+
  1158. "available size: %v", len(failureMessage))
  1159. }
  1160. // Finally, we'll add some padding in order to ensure that all failure
  1161. // messages are fixed size.
  1162. pad := make([]byte, FailureMessageLength-len(failureMessage))
  1163. if err := WriteUint16(w, uint16(len(failureMessage))); err != nil {
  1164. return err
  1165. }
  1166. if err := WriteBytes(w, failureMessage); err != nil {
  1167. return err
  1168. }
  1169. if err := WriteUint16(w, uint16(len(pad))); err != nil {
  1170. return err
  1171. }
  1172. return WriteBytes(w, pad)
  1173. }
  1174. // EncodeFailureMessage encodes just the failure message without adding a length
  1175. // and padding the message for the onion protocol.
  1176. func EncodeFailureMessage(w *bytes.Buffer,
  1177. failure FailureMessage, pver uint32) error {
  1178. // First, we'll write out the error code itself into the failure
  1179. // buffer.
  1180. var codeBytes [2]byte
  1181. code := uint16(failure.Code())
  1182. binary.BigEndian.PutUint16(codeBytes[:], code)
  1183. _, err := w.Write(codeBytes[:])
  1184. if err != nil {
  1185. return err
  1186. }
  1187. // Next, some message have an additional message payload, if this is
  1188. // one of those types, then we'll also encode the error payload as
  1189. // well.
  1190. switch failure := failure.(type) {
  1191. case Serializable:
  1192. if err := failure.Encode(w, pver); err != nil {
  1193. return err
  1194. }
  1195. }
  1196. return nil
  1197. }
  1198. // makeEmptyOnionError creates a new empty onion error of the proper concrete
  1199. // type based on the passed failure code.
  1200. func makeEmptyOnionError(code FailCode) (FailureMessage, error) {
  1201. switch code {
  1202. case CodeInvalidRealm:
  1203. return &FailInvalidRealm{}, nil
  1204. case CodeTemporaryNodeFailure:
  1205. return &FailTemporaryNodeFailure{}, nil
  1206. case CodePermanentNodeFailure:
  1207. return &FailPermanentNodeFailure{}, nil
  1208. case CodeRequiredNodeFeatureMissing:
  1209. return &FailRequiredNodeFeatureMissing{}, nil
  1210. case CodePermanentChannelFailure:
  1211. return &FailPermanentChannelFailure{}, nil
  1212. case CodeRequiredChannelFeatureMissing:
  1213. return &FailRequiredChannelFeatureMissing{}, nil
  1214. case CodeUnknownNextPeer:
  1215. return &FailUnknownNextPeer{}, nil
  1216. case CodeIncorrectOrUnknownPaymentDetails:
  1217. return &FailIncorrectDetails{}, nil
  1218. case CodeIncorrectPaymentAmount:
  1219. return &FailIncorrectPaymentAmount{}, nil
  1220. case CodeFinalExpiryTooSoon:
  1221. return &FailFinalExpiryTooSoon{}, nil
  1222. case CodeInvalidOnionVersion:
  1223. return &FailInvalidOnionVersion{}, nil
  1224. case CodeInvalidOnionHmac:
  1225. return &FailInvalidOnionHmac{}, nil
  1226. case CodeInvalidOnionKey:
  1227. return &FailInvalidOnionKey{}, nil
  1228. case CodeTemporaryChannelFailure:
  1229. return &FailTemporaryChannelFailure{}, nil
  1230. case CodeAmountBelowMinimum:
  1231. return &FailAmountBelowMinimum{}, nil
  1232. case CodeFeeInsufficient:
  1233. return &FailFeeInsufficient{}, nil
  1234. case CodeIncorrectCltvExpiry:
  1235. return &FailIncorrectCltvExpiry{}, nil
  1236. case CodeExpiryTooSoon:
  1237. return &FailExpiryTooSoon{}, nil
  1238. case CodeChannelDisabled:
  1239. return &FailChannelDisabled{}, nil
  1240. case CodeFinalIncorrectCltvExpiry:
  1241. return &FailFinalIncorrectCltvExpiry{}, nil
  1242. case CodeFinalIncorrectHtlcAmount:
  1243. return &FailFinalIncorrectHtlcAmount{}, nil
  1244. case CodeExpiryTooFar:
  1245. return &FailExpiryTooFar{}, nil
  1246. case CodeInvalidOnionPayload:
  1247. return &InvalidOnionPayload{}, nil
  1248. case CodeMPPTimeout:
  1249. return &FailMPPTimeout{}, nil
  1250. case CodeInvalidBlinding:
  1251. return &FailInvalidBlinding{}, nil
  1252. default:
  1253. return nil, errors.Errorf("unknown error code: %v", code)
  1254. }
  1255. }
  1256. // writeOnionErrorChanUpdate writes out a ChannelUpdate using the onion error
  1257. // format. The format is that we first write out the true serialized length of
  1258. // the channel update, followed by the serialized channel update itself.
  1259. func writeOnionErrorChanUpdate(w *bytes.Buffer, chanUpdate *ChannelUpdate,
  1260. pver uint32) error {
  1261. // First, we encode the channel update in a temporary buffer in order
  1262. // to get the exact serialized size.
  1263. var b bytes.Buffer
  1264. updateLen, err := WriteMessage(&b, chanUpdate, pver)
  1265. if err != nil {
  1266. return err
  1267. }
  1268. // Now that we know the size, we can write the length out in the main
  1269. // writer.
  1270. if err := WriteUint16(w, uint16(updateLen)); err != nil {
  1271. return err
  1272. }
  1273. // With the length written, we'll then write out the serialized channel
  1274. // update.
  1275. if _, err := w.Write(b.Bytes()); err != nil {
  1276. return err
  1277. }
  1278. return nil
  1279. }