| 186 | } |
| 187 | |
| 188 | struct onion_payload *onion_decode(const tal_t *ctx, |
| 189 | const struct route_step *rs, |
| 190 | const struct pubkey *path_key, |
| 191 | const u64 *accepted_extra_tlvs, |
| 192 | struct amount_msat amount_in, |
| 193 | u32 cltv_expiry, |
| 194 | u64 *failtlvtype, |
| 195 | size_t *failtlvpos, |
| 196 | const char **explanation) |
| 197 | { |
| 198 | struct onion_payload *p = tal(ctx, struct onion_payload); |
| 199 | const u8 *cursor = rs->raw_payload; |
| 200 | size_t max = tal_bytelen(cursor), len; |
| 201 | |
| 202 | p->final = (rs->nextcase == ONION_END); |
| 203 | |
| 204 | /* BOLT #4: |
| 205 | * 1. type: `hop_payloads` |
| 206 | * 2. data: |
| 207 | * * [`bigsize`:`length`] |
| 208 | * * [`length*byte`:`payload`] |
| 209 | */ |
| 210 | len = fromwire_bigsize(&cursor, &max); |
| 211 | if (!cursor || len > max) { |
| 212 | *failtlvtype = 0; |
| 213 | *failtlvpos = tal_bytelen(rs->raw_payload); |
| 214 | if (explanation) |
| 215 | *explanation = tal_fmt(ctx, "Too short for initial length"); |
| 216 | return tal_free(p); |
| 217 | } |
| 218 | |
| 219 | /* We do this manually so we can accept extra types, and get |
| 220 | * error off and type. */ |
| 221 | p->tlv = tlv_payload_new(p); |
| 222 | if (!fromwire_tlv(&cursor, &max, tlvs_tlv_payload, |
| 223 | TLVS_ARRAY_SIZE_tlv_payload, |
| 224 | p->tlv, &p->tlv->fields, accepted_extra_tlvs, |
| 225 | failtlvpos, failtlvtype)) { |
| 226 | if (explanation) |
| 227 | *explanation = tal_fmt(ctx, "Unparseable TLV"); |
| 228 | return tal_free(p); |
| 229 | } |
| 230 | |
| 231 | /* BOLT #4: |
| 232 | * |
| 233 | * The reader: |
| 234 | * |
| 235 | * - If `encrypted_recipient_data` is present: |
| 236 | */ |
| 237 | if (p->tlv->encrypted_recipient_data) { |
| 238 | struct tlv_encrypted_data_tlv *enc; |
| 239 | |
| 240 | /* BOLT #4: |
| 241 | * |
| 242 | * - If `path_key` is set in the incoming `update_add_htlc`: |
| 243 | * - MUST return an error if `current_path_key` is present. |
| 244 | * - MUST use that `path_key` as `path_key` for decryption. |
| 245 | * - Otherwise: |
no test coverage detected