Skip to content
LearnBitcoin

ResearchBitcoin Dev Archive lightning-dev list

BOLT 12: Offers

15 messages, Nov 5 to 14, 2019. Started by Rusty Russell.

Rusty Russell (7), Yaacov Akiba Slama (5), ZmnSCPxj, Ross Dyson, Jorge Timón

Permalink First message in the Wayback Machine

Rusty Russell permalink original

Hi all,

        This took longer than I'd indicated; for that I'm sorry.
However, this should give us all something to chew on.  I've started
with a draft "BOLT 12" (it might be BOLT 13 by the time it's finalized
though!).
I've also appended indications where we touch other BOLTs:
1. BOLT 7 gains a message/reply system, encoded like htlc onions and
   failure messages.
2. BOLT 11 gains a `q` field for quantity; this avoids changing the
   description when the user requests an invoice for more than one of something
   (since changing the description between offer and invoice requires user
   interaction: it's the *invoice* which you are committing to).

There's definite handwaving in here; let's see if you can find it!

Cheers, Rusty.

# BOLT #12: Offer Protocols for Lightning Payments

An higher-level, QR-code-ready protocol for dealing with invoices over Lightning. There are two simple flows supported: in one, a user gets an offer (`lno...`) and requests an invoice over the lightning network, obtaining one (or an error) in reply. In the other, a user gets an invoice request (`lni...`), and sends the invoice over the lightning network, retreiving an empty reply.

# Table of Contents

  * [Offers](#offers)
    * [Encoding](#encoding)
    * [TLV Fields](#tlv-fields)
  * [Invrequests](#invrequests)
    * [Encoding](#encoding)
    * [TLV Fields](#tlv-fields)

# Offers

Offers supply a reader with enough information to request one or more invoices via the lightning network itself.

## Encoding

The human-readable part of a Lightning offer is `lno`. The data part consists of three parts:

1. 0 or more [TLV](01-messaging.md#type-length-value-format) encoded fields.
2. A 32-byte nodeid[1]
3. 64-byte signature of SHA256(hrp-as-utf8 | tlv | nodeid).

## TLV Fields

The TLV fields define how to get the invoice, and what it's for. Each offer has a unique `offer_idenfitier` so the offering node can distinguish different invoice requests.

Offers can request recurring payments of various kinds, and specify what base currency they are calculated in (the actual amount will be in the invoice).

`additional_data` is a bitfield which indicates what information the invoice requester should (odd) or must (even) supply: 1. Bits `0/1`: include `delivery_address` 2. Bits `2/3`: include `delivery_telephone_number` 3. Bits `4/5`: include `voucher_code` 4. Bits `6/7`: include `refund_proof`

`refund_for` indicates an offer for a (whole or part) refund for a previous invoice, as indicated by the `payment_hash`.

1. tlvs: `offer`
2. types:
    1. type: 1 (`paths`)
    2. data:
        * [`u16`:`num_paths`]
        * [`num_paths*path`:`path`]
    1. type: 2 (`description`)
    2. data:
        * [`...*byte`:`description`]
    1. type: 3 (`expiry`)
    2. data:
        * [`tu64`:`seconds_from_epoch`]
    1. type: 4 (`offer_identifier`)
    2. data:
        * [`...*byte`:`id`]
    1. type: 5 (`amount`)
    2. data:
        * [`4*byte`:`currency`]
        * [`tu64`:`amount`]
    1. type: 6 (`additional_data`)
    2. data:
	    * [`...*byte`:`rbits`]
    1. type: 7 (`recurrance`)
    2. data:
	    * [`byte`:`time_unit`]
		* [`u32`:`period`]
		* [`tu32`:`number`]
    1. type: 8 (`recurrance_base`)
    2. data:
		* [`u32`:`basetime`]
		* [`tu32`:`paywindow`]
    1. type: 9 (`quantity`)
    2. data:
		* [`tu64`:`max`]
    1. type: 10 (`refund_for`)
    2. data:
        * [`32*byte`:`payment_hash`]
1. subtype: `path`
2. data:
   * [`u16`:`num_hops`]
   * [`num_hops*hop`:`hops`]
1. subtype: `hop`
2. data:
   * [`pubkey`:`nodeid`]
   * [`short_channel_id`:`short_channel_id`]
   * [`u16`:`flen`]
   * [`flen*byte`:`features`]

## Requirements For Offers And Invrequests

A writer of an offer or an invrequest:
  - if it is connected only by private channels:
    - MUST include `paths` containing a path to the node.
  - otherwise:
    - MAY include `paths` containing a path to the node.
  - MUST describe the item(s) being offered or purpose of invoice in `description`.
  - MUST include `expiry` if the offer/invrequest will not be valid after some time.
  - if it includes `expiry`:
    - MUST set `seconds_from_epoch` to the expiry time in seconds since 1970 UTC.

## Requirements For Offers

A writer of an offer:
  - MUST use a unique `offer_idenfitier` for each offer.
  - MAY include `recurrence` to indicate offer should trigger time-spaced
    invoices.
  - MUST include `amount` if it includes `recurrence`.
  - if it includes `amount`:
    - MUST specify `currency` as the ISO 4712 or BIP-0173, padded with zero bytes if required
	- MUST specify `amount` to the amount expected for the invoice, as the ISO 4712 currency unit multiplied by exponent, OR the BIP-0173 minimum unit (eg. `satoshis`).
  - if it requires specific fields in the invoice:
    - MUST set the corresponding even bits in the `additional_data` field
A reader of an offer:
  - SHOULD gain user consent for recurring payments.
  - SHOULD allow user to view and cancel recurring payments.
  - SHOULD gain user consent to send `delivery_` fields.
  - if it uses `amount` to provide the user with a cost estimate:
	- MUST warn user if amount of actual invoice differs significantly
		from that expectation.
  - FIXME: more!

## Recurrance

Some offers are *periodic*, such as a subscription service or monthly dues, in that payment is expected to be repeated. There are many different flavors of repetition, consider:

* Payments due on the first of every month, for 6 months.
* Payments due on every Monday, 1pm Pacific Standard Time.
* Payments due once a year:
   * which must be made on January 1st, or
   * which are only valid if started January 1st 2020, or
   * which if paid after January 1st you (over) pay the full rate first year, or
   * which if paid after January 1st are paid pro-rata for the first year, or
   * which repeat from whenever you made the first payment
Thus, each payment has:
1. A `time_unit` defining 0 (seconds), 1 (days), 2 (months), 3 (years).
2. A `period`, defining how often (in `time_unit`) it has to be paid.
3. An optional `number` of total payments to be paid.
4. An optional `basetime`, defining when the first payment applies
   in seconds since 1970-01-01 UTC.
5. An optional `paywindow`, defining how many seconds into the period
   a payment will be accepted: 0xFFFFFFFF being a special value meaning
   "any time during the period, but you will have to pay proportionally
   to the remaining time in the period".

Note that the `expiry` field covers the case where an offer is no longer valid after January 1st 2020.

## Default Offer

The "default offer" of a node is a nominal offer used to send unsolicited payments. It is generally not actually sent, but can be used by any other node as if it has been. It has the following fields:

* `offer_idenfitier`: zero-length
* `d`: any
* `n`: the node id of the recipient.

## Invoice Request Encoding

Once it has an offer, the node can request an actual invoice using the `invoice_req` message inside `directed`'s `onion_routing_packet`. It would expect an `invoice_or_error_tlv` inside the `directed_reply` message.

This includes a `tag` it can use to identify replies, the `offer_idenfitier` from the offer, a `key` it can use to prove it was the requester of this invoice, a `recurrence` number if this is a payment in a recurring series, and other codes as required.

The `refund_proof` refers to a previous invoice paid by the sender for the specific case of a `refund_for` offer. It provides proof of payment (the `payment_preimage` and also a signature of the `payment_hash` from the `key` which requested the being-refunded invoice (which does *not* have to be the same as this `key`!).

1. tlvs: `invoice_request_tlv`
2. types:
   1. type: 1 (`tag`)
   2. data:
      * [`...*byte`:`tag`]
   1. type: 2 (`offer_identifier`)
   2. data:
      * [`...*byte`:`id`]
   1. type: 3 (`key`)
   2. data:
     * [`32`:`key`]
   1. type: 4 (`recurrence`)
   2. data:
     * [`tu64`:`number`]
   1. type: 5 (`quantity`)
   2. data:
     * [`tu64`:`n`]
   1. type: 6 (`delivery_address_name`)
   2. data:
	 * [`...*byte`:`name`]
   1. type: 7 (`delivery_address1`)
   2. data:
	 * [`...*byte`:`address1`]
   1. type: 8 (`delivery_address2`)
   2. data:
	 * [`...*byte`:`address2`]
   1. type: 9 (`delivery_city`)
   2. data:
	 * [`...*byte`:`city`]
   1. type: 10 (`delivery_state_province_or_region`)
   2. data:
	 * [`...*byte`:`state_province_or_region`]
   1. type: 11 (`delivery_zip_or_postal_code`)
   2. data:
	 * [`...*byte`:`zip_or_postal_code`]
   1. type: 12 (`delivery_country`)
   2. data:
	 * [`2*byte`:`country_code`]
   1. type: 13 (`delivery_telephone_number`)
   2. data:
	 * [`...*byte`:`tel`]
   1. type: 14 (`voucher_code`)
   2. data:
	 * [`...*byte`:`code`]
   1. type: 15 (`refund_proof`)
   2. data:
      * [`32*byte`:`payment_preimage`]
      * [`signature`:`signature`]

## Requirements

FIXME: many more
Sender MUST use ISO 3166 alpha-2 code for `delivery_country`.
Sender MUST set offer_identifier to match offer.
Sender MUST include `key`
   - SHOULD use a transient unpredictable key
   - MUST reuse key for successive recurring invoices.
Sender MUST set `recurrence` for recurring invoices.

Receiver MUST check `offer_identifier` Receiver MUST check `delivery_` fields. Receiver MUST check `recurrence`. Receiver MUST check `amount`.

1. tlvs: `invoice_or_error_tlv`
2. types:
   1. type: 1 (`tag`)
   2. data:
     * [`...*byte`:`tag`]
   1. type: 3 (`omitted`)
   2. data:
	 * [`...*u64`:`omitted_fields`]
   1. type: 4 (`invoice`)
   2. data:
     * [`...*byte`:`invoice`]
   1. type: 5 (`message`)
     * [`...*byte`:`message`]
   1. type: 6 (`replacement`)
   2. data:
     * [`signature`:`signature`]
     * [`...*byte`:`offer`]
Sender:
- MUST copy `tag` from sender.
- MUST omit fields it does not use, and place number in order in `omitted_fields`.
- if it includes `invoice`:
  - MUST not include `message`
  - MUST not include `replacement`
  - MUST [merkle fields it used](#merkle-calculation) and place that in invoice `s` field.
- otherwise, if it includes `replacement`:
  - MAY include `message`
- otherwise:
  - MUST include `message` describing the error.
Receiver:
- MUST check that `tag` matches req.
- if `replacement`:
  - MUST fail if `signature` does not sign `offer` with same key as original.
  - MUST only fetch once (no double-redirects!)
- if description or amount significantly changes, must re-ask user.
  - SHOULD note if description simply has something appended (eg "+ postage").
- within invoice:
    - MUST check that `s` matches merkle of fields, minus `omitted`.
	- MUST check that no vital fields are in `omitted`.
	- MUST check that `d` matches `description`
    - MUST check that `q` DNE if `quantity` DNE, otherwise is equal.
    - Must check valid signature, etc.

## Merkle Calculation

1. For each `invoice_req_tlv` field in ascending `tlv` type order:
   1. If the field was omitted, it is added to `omitted_fields`.
   2. Otherwise, the immediate parent merkle is:
      SHA256(SHA256(`tag` | `be64-n`) | SHA256(`tlv-value`))
      Where `be64-n` is a 64-bit big-endian counter starting at 0 and
      incrementing for each leaf.
   3. Order these nodes in increasing SHA256(`tag` | `be64-n`) order.
2. Create additional leaves until `be64-n` is the next power of 2:
      SHA256(`tag` | `be64-n`)
3. Combine adjacent leaves using SHA256(leaf1 | leaf2) until none remain.

By creating adjacent leaves using the `tag` field and a counter, and sorting the leaves, the only significant information revealed by a merkle proof on a node is the depth of tree (which implies the total number of TLV fields).

# InvRequests

There are times when it makes sense to request an invoice over another medium, such as HTTP or a QR code.

## Encoding

The human-readable part of a Lightning invrequest is `lni`. The data part consists of three parts:

1. 0 or more [TLV](01-messaging.md#type-length-value-format) encoded fields.
2. A 32-byte nodeid[1]
3. 64-byte signature of SHA256(hrp-as-utf8 | tlv | nodeid).

## TLV Fields

1. tlvs: `invreq`
2. types:
    1. type: 1 (`paths`)
    2. data:
        * [`u16`:`num_paths`]
        * [`num_paths*path`:`path`]
    1. type: 2 (`description`)
    2. data:
        * [`...*byte`:`description`]
    1. type: 3 (`expiry`)
    2. data:
        * [`tu64`:`seconds_from_epoch`]
    1. type: 4 (`amount`)
    2. data:
        * [`tu64`:`millisatoshis`]

The fields `paths`, `description`, and `expiry` fields are the same as those for offers; the optional `amount` field describes the amount an invoice will be accepted for.

Upon parsing and accepting an `invreq`, the node sends an `invoice_or_error_tlv` within an onion. The reply is empty.

## Requirements

The requirements for `paths`, `description` and `expiry` are [described above](#requirements-for-offers-and-invrequests).

FIXME: More.

[1] Assuming we go for Schnorr sigs and 32-byte pubkeys. ---- Addendum: BOLT #7: P2P Node and Channel Discovery and Directed Messages ... # Directed Messages

Directed messages allow peers to use existing connections to query for invoices (see [BOLT 12](12-offer-encoding.md)). Like gossip messages, they are not associated with a particular local channel.

The `id` is a unique, transient identifier between the peers, used to identify match messages and replies.

## The `directed` and `directed_reply` Messages

1. type: 384 (`directed`) (`option_directed_messages`)
2. data:
    * [`chain_hash`:`chain_hash`]
    * [`u64`:`id`]
    * [`1366*byte`:`onion_routing_packet`]
1. type: 384 (`directed_reply`) (`option_directed_messages`)
2. data:
    * [`chain_hash`:`chain_hash`]
    * [`u64`:`id`]
    * [`u16`:`len`]
    * [`len*byte`:`reply`]

## Requirements

FIXME: similar to update_add_htlc and update_fail_htlc. FIXME: define reasonable timeout after which you can forget if not replied?

ZmnSCPxj in reply to Rusty Russell permalink original

Good morning Rusty, and list,

Thank you Rusty. Below is a quick and dirty review; I doubt I found whatever handwaves you are using.

First, please confirm my understanding of the message flow. Suppose I have a donation offer on my website and Rusty wants to donate to me. Then:

         ZmnSCPxj                      Rusty
            |                            |
            +---------- `lno` ---------->+ (via non-Lightning communication channel e.g. https)
            |                            |
            +<---- `invoice_request` ----+ (via a normal Rusty->ZmnSCPxj payment)
            |                            |
            +---- `invoice_or_error` --->| (by failing the above payment and embedding in the failure blob)
            |                            |
            +<------- `sendpay` ---------+ (via a normal Rusty->ZmnSCPxj payment)

Is it approximately correct?

Quoted text (11 lines)
>
> BOLT #12: Offer Protocols for Lightning Payments
>
> =================================================
>
> An higher-level, QR-code-ready protocol for dealing with invoices over
> Lightning. There are two simple flows supported: in one, a user gets
> an offer (`lno...`) and requests an invoice over the lightning
> network, obtaining one (or an error) in reply. In the other, a user
> gets an invoice request (`lni...`), and sends the invoice over the
> lightning network, retreiving an empty reply.

Here are completely pointless counterproposals for the offer and invoice-request HRPs:

* Offers:
  * `lnpayme`
  * `lnbuyit`
  * `lnforsale`
* Invoice Requests:
  * `lnpaying`
  * `lnbuying`
  * `lnshutupandtakemymoney`

`lno` and `lni` feel wrong to me. Their juxtaposition implies `lno` == output and `lni` == input to me, due to the use of `o` and `i`, though `lno` is where you get money in exchange for product and `lni` is the request-for-service.

Quoted text (11 lines)
>
> Table of Contents
>
> ==================
>
> -   Offers
>     -   Encoding
>     -   TLV Fields
> -   Invrequests
>     -   Encoding
>     -   TLV Fields

Definite handwave, does not match the structure of the document *at all*.

Quoted text (43 lines)
> Encoding
>
> ---------
>
> The human-readable part of a Lightning offer is`lno`. The data part
> consists of three parts:
>
> 1.  0 or more TLV encoded fields.
> 2.  A 32-byte nodeid[1]
> 3.  64-byte signature of SHA256(hrp-as-utf8 | tlv | nodeid).
>
> TLV Fields
>
> -----------
>
> The TLV fields define how to get the invoice, and what it's for.
> Each offer has a unique `offer_idenfitier` so the offering node can
> distinguish different invoice requests.
>
> Offers can request recurring payments of various kinds, and specify
> what base currency they are calculated in (the actual amount will be
> in the invoice).
>
> `additional_data` is a bitfield which indicates what information the
> invoice requester should (odd) or must (even) supply:
>
> 1.  Bits `0/1`: include `delivery_address`
> 2.  Bits `2/3`: include `delivery_telephone_number`
> 3.  Bits `4/5`: include `voucher_code`
> 4.  Bits `6/7`: include `refund_proof`
>
>     `refund_for` indicates an offer for a (whole or part) refund for a
>     previous invoice, as indicated by the `payment_hash`.
>
> 5.  tlvs: `offer`
> 6.  types:
>     1.  type: 1 (`paths`)
>     2.  data:
>         -   [`u16`:`num_paths`]
>         -   [`num_paths*path`:`path`]
>     3.  type: 2 (`description`)
>     4.  data:
>         -   [`...*byte`:`description`]

UTF-8? Null-terminated?

Quoted text (67 lines)
>     5.  type: 3 (`expiry`)
>     6.  data:
>         -   [`tu64`:`seconds_from_epoch`]
>     7.  type: 4 (`offer_identifier`)
>     8.  data:
>         -   [`...*byte`:`id`]
>     9.  type: 5 (`amount`)
>     10.  data:
>         -   [`4*byte`:`currency`]
>         -   [`tu64`:`amount`]
>     11.  type: 6 (`additional_data`)
>     12.  data:
>         -   [`...*byte`:`rbits`]
>     13.  type: 7 (`recurrance`)
>     14.  data:
>         -   [`byte`:`time_unit`]
>         -   [`u32`:`period`]
>         -   [`tu32`:`number`]
>     15.  type: 8 (`recurrance_base`)
>     16.  data:
>         -   [`u32`:`basetime`]
>         -   [`tu32`:`paywindow`]
>     17.  type: 9 (`quantity`)
>     18.  data:
>         -   [`tu64`:`max`]
>     19.  type: 10 (`refund_for`)
>     20.  data:
>         -   [`32*byte`:`payment_hash`]
> 7.  subtype: `path`
> 8.  data:
>     -   [`u16`:`num_hops`]
>     -   [`num_hops*hop`:`hops`]
> 9.  subtype: `hop`
> 10.  data:
>     -   [`pubkey`:`nodeid`]
>     -   [`short_channel_id`:`short_channel_id`]
>     -   [`u16`:`flen`]
>     -   [`flen*byte`:`features`]
>
> Requirements For Offers And Invrequests
>
> ----------------------------------------
>
> A writer of an offer or an invrequest:
>
> -   if it is connected only by private channels:
>     -   MUST include `paths` containing a path to the node.
> -   otherwise:
>     -   MAY include `paths` containing a path to the node.
> -   MUST describe the item(s) being offered or purpose of invoice in `description`.
> -   MUST include `expiry` if the offer/invrequest will not be valid after some time.
> -   if it includes `expiry`:
>     -   MUST set `seconds_from_epoch` to the expiry time in seconds since 1970 UTC.
>
> Requirements For Offers
>
> ------------------------
>
> A writer of an offer:
>
> -   MUST use a unique `offer_idenfitier` for each offer.
> -   MAY include `recurrence` to indicate offer should trigger time-spaced
>     invoices.
>
> -   MUST include `amount` if it includes `recurrence`.
> -   if it includes `amount`:
>     -   MUST specify `currency` as the ISO 4712 or BIP-0173, padded with zero bytes if required

I cannot find ISO 4712, but could find ISO 4217. BIP-173 does not have a list of currencies, but refers to SLIP-0173. Some of the listed currencies there seem to have more than 4 characters.

Should I assume encoding is ASCII? We will "never" see a non-ASCII currency code?

Quoted text (62 lines)
>     -   MUST specify `amount` to the amount expected for the invoice, as the ISO 4712 currency unit multiplied by exponent, OR the BIP-0173 minimum unit (eg. `satoshis`).
> -   if it requires specific fields in the invoice:
>     -   MUST set the corresponding even bits in the `additional_data` field
>
>         A reader of an offer:
>
> -   SHOULD gain user consent for recurring payments.
> -   SHOULD allow user to view and cancel recurring payments.
> -   SHOULD gain user consent to send `delivery_` fields.
> -   if it uses `amount` to provide the user with a cost estimate:
>     -   MUST warn user if amount of actual invoice differs significantly
>         from that expectation.
>
> -   FIXME: more!
>
> Recurrance
>
> -----------
>
> Some offers areperiodic, such as a subscription service or monthly
> dues, in that payment is expected to be repeated. There are many
> different flavors of repetition, consider:
>
> -   Payments due on the first of every month, for 6 months.
> -   Payments due on every Monday, 1pm Pacific Standard Time.
> -   Payments due once a year:
>     -   which must be made on January 1st, or
>     -   which are only valid if started January 1st 2020, or
>     -   which if paid after January 1st you (over) pay the full rate first year, or
>     -   which if paid after January 1st are paid pro-rata for the first year, or
>     -   which repeat from whenever you made the first payment
>
>         Thus, each payment has:
>
>
> 1.  A `time_unit` defining 0 (seconds), 1 (days), 2 (months), 3 (years).
> 2.  A `period`, defining how often (in `time_unit`) it has to be paid.
> 3.  An optional `number` of total payments to be paid.
> 4.  An optional `basetime`, defining when the first payment applies
>     in seconds since 1970-01-01 UTC.
>
> 5.  An optional `paywindow`, defining how many seconds into the period
>     a payment will be accepted: 0xFFFFFFFF being a special value meaning
>     "any time during the period, but you will have to pay proportionally
>     to the remaining time in the period".
>
>     Note that the `expiry` field covers the case where an offer is no longer
>     valid after January 1st 2020.
>
>
> Default Offer
>
> --------------
>
> The "default offer" of a node is a nominal offer used to send
> unsolicited payments. It is generally not actually sent, but can be
> used by any other node as if it has been. It has the following
> fields:
>
> -   `offer_idenfitier`: zero-length
> -   `d`: any
> -   `n`: the node id of the recipient.

In essence, this is an implicitly-existing offer that never expires, and which can be used by any node at any time to construct an invoice request?

Quoted text (20 lines)
>
> Invoice Request Encoding
>
> -------------------------
>
> Once it has an offer, the node can request an actual invoice using the
> `invoice_req` message inside `directed`'s `onion_routing_packet`. It
> would expect an `invoice_or_error_tlv` inside the `directed_reply`
> message.
>
> This includes a `tag` it can use to identify replies, the
> `offer_idenfitier` from the offer, a `key` it can use to prove it was
> the requester of this invoice, a `recurrence` number if this
> is a payment in a recurring series, and other codes as required.
>
> The `refund_proof` refers to a previous invoice paid by the sender for
> the specific case of a `refund_for` offer. It provides proof of
> payment (the `payment_preimage` and also a signature of the
> `payment_hash` from the `key` which requested the being-refunded
> invoice (which does not have to be the same as this `key`!).

An earlier requirement mentions that writers of offers or invoice request MUST have `paths` in some condition. The below does not have `paths`, but there is a "human-readable" alternate encoding which *does* have `paths`. It might be better to clarify this point.

Quoted text (81 lines)
>
> 1.  tlvs: `invoice_request_tlv`
> 2.  types:
>
>     1.  type: 1 (`tag`)
>     2.  data:
>         -   [`...*byte`:`tag`]
>     3.  type: 2 (`offer_identifier`)
>     4.  data:
>         -   [`...*byte`:`id`]
>     5.  type: 3 (`key`)
>     6.  data:
>
>     -   [`32`:`key`]
>
>     1.  type: 4 (`recurrence`)
>     2.  data:
>
>     -   [`tu64`:`number`]
>
>     1.  type: 5 (`quantity`)
>     2.  data:
>
>     -   [`tu64`:`n`]
>
>     1.  type: 6 (`delivery_address_name`)
>     2.  data:
>
>     -   [`...*byte`:`name`]
>
>     1.  type: 7 (`delivery_address1`)
>     2.  data:
>
>     -   [`...*byte`:`address1`]
>
>     1.  type: 8 (`delivery_address2`)
>     2.  data:
>
>     -   [`...*byte`:`address2`]
>
>     1.  type: 9 (`delivery_city`)
>     2.  data:
>
>     -   [`...*byte`:`city`]
>
>     1.  type: 10 (`delivery_state_province_or_region`)
>     2.  data:
>
>     -   [`...*byte`:`state_province_or_region`]
>
>     1.  type: 11 (`delivery_zip_or_postal_code`)
>     2.  data:
>
>     -   [`...*byte`:`zip_or_postal_code`]
>
>     1.  type: 12 (`delivery_country`)
>     2.  data:
>
>     -   [`2*byte`:`country_code`]
>
>     1.  type: 13 (`delivery_telephone_number`)
>     2.  data:
>
>     -   [`...*byte`:`tel`]
>
>     1.  type: 14 (`voucher_code`)
>     2.  data:
>
>     -   [`...*byte`:`code`]
>
>     1.  type: 15 (`refund_proof`)
>     2.  data:
>         -   [`32*byte`:`payment_preimage`]
>         -   [`signature`:`signature`]
>
> Requirements
>
> -------------
>
> FIXME: many more
> Sender MUST use ISO 3166 alpha-2 code for `delivery_country`.

What happens when my army of non-shiny robots rise from their crypts and start annexing entire countries to enforce global peace, prosperity, and greater world optimization? I suppose it would become immaterial then...

Quoted text (27 lines)
>
> Directed Messages
>
> ==================
>
> Directed messages allow peers to use existing connections to query for
> invoices (see BOLT 12). Like gossip messages,
> they are not associated with a particular local channel.
>
> The `id` is a unique, transient identifier between the peers, used to
> identify match messages and replies.
>
> The `directed` and `directed_reply` Messages
>
> ---------------------------------------------
>
> 1.  type: 384 (`directed`) (`option_directed_messages`)
> 2.  data:
>     -   [`chain_hash`:`chain_hash`]
>     -   [`u64`:`id`]
>     -   [`1366*byte`:`onion_routing_packet`]
> 3.  type: 384 (`directed_reply`) (`option_directed_messages`)
> 4.  data:
>     -   [`chain_hash`:`chain_hash`]
>     -   [`u64`:`id`]
>     -   [`u16`:`len`]
>     -   [`len*byte`:`reply`]

This new `directed` message will be the mechanism for sending invoice requests and receiving invoice request responses? What incentive is there for a forwarding node to actually forward a `directed` message?

Regards, ZmnSCPxj

Yaacov Akiba Slama in reply to Rusty Russell permalink original

Hi Rusty,

It seems that there are two kind of TLV fields in your proposition: 1) LN specific fields like `num_paths` and `payment_preimage`. 2) "Business" fields like `address1` and `currency`. I understand the need to define and include the first category, but I don't think that we need or can define the second category. These fields already exists in software like crm, erp, etc.. and are well defined by standard body. My suggestion is to have a generic field containing well defined structured and standardized data. See for instance https://en.wikipedia.org/wiki/EDIFACT and/or https://en.wikipedia.org/wiki/Universal_Business_Language. What do you think? PS: Sorry for crossposting here and in https://github.com/lightningnetwork/lightning-rfc/pull/694 --yas

On 05/11/2019 06:23, Rusty Russell wrote:

Quoted text (428 lines)
> Hi all,
>
>          This took longer than I'd indicated; for that I'm sorry.
> However, this should give us all something to chew on.  I've started
> with a draft "BOLT 12" (it might be BOLT 13 by the time it's finalized
> though!).
>
> I've also appended indications where we touch other BOLTs:
> 1. BOLT 7 gains a message/reply system, encoded like htlc onions and
>     failure messages.
> 2. BOLT 11 gains a `q` field for quantity; this avoids changing the
>     description when the user requests an invoice for more than one of something
>     (since changing the description between offer and invoice requires user
>     interaction: it's the *invoice* which you are committing to).
>
> There's definite handwaving in here; let's see if you can find it!
>
> Cheers,
> Rusty.
>
> # BOLT #12: Offer Protocols for Lightning Payments
>
> An higher-level, QR-code-ready protocol for dealing with invoices over
> Lightning.  There are two simple flows supported: in one, a user gets
> an offer (`lno...`) and requests an invoice over the lightning
> network, obtaining one (or an error) in reply.  In the other, a user
> gets an invoice request (`lni...`), and sends the invoice over the
> lightning network, retreiving an empty reply.
>
> # Table of Contents
>
>    * [Offers](#offers)
>      * [Encoding](#encoding)
>      * [TLV Fields](#tlv-fields)
>    * [Invrequests](#invrequests)
>      * [Encoding](#encoding)
>      * [TLV Fields](#tlv-fields)
>
> # Offers
>
> Offers supply a reader with enough information to request one or more
> invoices via the lightning network itself.
>
> ## Encoding
>
> The human-readable part of a Lightning offer is `lno`.  The data part
> consists of three parts:
>
> 1. 0 or more [TLV](01-messaging.md#type-length-value-format) encoded fields.
> 2. A 32-byte nodeid[1]
> 3. 64-byte signature of SHA256(hrp-as-utf8 | tlv | nodeid).
>
> ## TLV Fields
>
> The TLV fields define how to get the invoice, and what it's for.
> Each offer has a unique `offer_idenfitier` so the offering node can
> distinguish different invoice requests.
>
> Offers can request recurring payments of various kinds, and specify
> what base currency they are calculated in (the actual amount will be
> in the invoice).
>
> `additional_data` is a bitfield which indicates what information the
> invoice requester should (odd) or must (even) supply:
> 1. Bits `0/1`: include `delivery_address`
> 2. Bits `2/3`: include `delivery_telephone_number`
> 3. Bits `4/5`: include `voucher_code`
> 4. Bits `6/7`: include `refund_proof`
>
> `refund_for` indicates an offer for a (whole or part) refund for a
> previous invoice, as indicated by the `payment_hash`.
>
> 1. tlvs: `offer`
> 2. types:
>      1. type: 1 (`paths`)
>      2. data:
>          * [`u16`:`num_paths`]
>          * [`num_paths*path`:`path`]
>      1. type: 2 (`description`)
>      2. data:
>          * [`...*byte`:`description`]
>      1. type: 3 (`expiry`)
>      2. data:
>          * [`tu64`:`seconds_from_epoch`]
>      1. type: 4 (`offer_identifier`)
>      2. data:
>          * [`...*byte`:`id`]
>      1. type: 5 (`amount`)
>      2. data:
>          * [`4*byte`:`currency`]
>          * [`tu64`:`amount`]
>      1. type: 6 (`additional_data`)
>      2. data:
> 	    * [`...*byte`:`rbits`]
>      1. type: 7 (`recurrance`)
>      2. data:
> 	    * [`byte`:`time_unit`]
> 		* [`u32`:`period`]
> 		* [`tu32`:`number`]
>      1. type: 8 (`recurrance_base`)
>      2. data:
> 		* [`u32`:`basetime`]
> 		* [`tu32`:`paywindow`]
>      1. type: 9 (`quantity`)
>      2. data:
> 		* [`tu64`:`max`]
>      1. type: 10 (`refund_for`)
>      2. data:
>          * [`32*byte`:`payment_hash`]
>
> 1. subtype: `path`
> 2. data:
>     * [`u16`:`num_hops`]
>     * [`num_hops*hop`:`hops`]
>
> 1. subtype: `hop`
> 2. data:
>     * [`pubkey`:`nodeid`]
>     * [`short_channel_id`:`short_channel_id`]
>     * [`u16`:`flen`]
>     * [`flen*byte`:`features`]
>
> ## Requirements For Offers And Invrequests
>
> A writer of an offer or an invrequest:
>    - if it is connected only by private channels:
>      - MUST include `paths` containing a path to the node.
>    - otherwise:
>      - MAY include `paths` containing a path to the node.
>    - MUST describe the item(s) being offered or purpose of invoice in `description`.
>    - MUST include `expiry` if the offer/invrequest will not be valid after some time.
>    - if it includes `expiry`:
>      - MUST set `seconds_from_epoch` to the expiry time in seconds since 1970 UTC.
>
> ## Requirements For Offers
>
> A writer of an offer:
>    - MUST use a unique `offer_idenfitier` for each offer.
>    - MAY include `recurrence` to indicate offer should trigger time-spaced
>      invoices.
>    - MUST include `amount` if it includes `recurrence`.
>    - if it includes `amount`:
>      - MUST specify `currency` as the ISO 4712 or BIP-0173, padded with zero bytes if required
> 	- MUST specify `amount` to the amount expected for the invoice, as the ISO 4712 currency unit multiplied by exponent, OR the BIP-0173 minimum unit (eg. `satoshis`).
>    - if it requires specific fields in the invoice:
>      - MUST set the corresponding even bits in the `additional_data` field
>
> A reader of an offer:
>    - SHOULD gain user consent for recurring payments.
>    - SHOULD allow user to view and cancel recurring payments.
>    - SHOULD gain user consent to send `delivery_` fields.
>    - if it uses `amount` to provide the user with a cost estimate:
> 	- MUST warn user if amount of actual invoice differs significantly
> 		from that expectation.
>    - FIXME: more!
>
> ## Recurrance
>
> Some offers are *periodic*, such as a subscription service or monthly
> dues, in that payment is expected to be repeated.  There are many
> different flavors of repetition, consider:
>
> * Payments due on the first of every month, for 6 months.
> * Payments due on every Monday, 1pm Pacific Standard Time.
> * Payments due once a year:
>     * which must be made on January 1st, or
>     * which are only valid if started January 1st 2020, or
>     * which if paid after January 1st you (over) pay the full rate first year, or
>     * which if paid after January 1st are paid pro-rata for the first year, or
>     * which repeat from whenever you made the first payment
>
> Thus, each payment has:
> 1. A `time_unit` defining 0 (seconds), 1 (days), 2 (months), 3 (years).
> 2. A `period`, defining how often (in `time_unit`) it has to be paid.
> 3. An optional `number` of total payments to be paid.
> 4. An optional `basetime`, defining when the first payment applies
>     in seconds since 1970-01-01 UTC.
> 5. An optional `paywindow`, defining how many seconds into the period
>     a payment will be accepted: 0xFFFFFFFF being a special value meaning
>     "any time during the period, but you will have to pay proportionally
>     to the remaining time in the period".
>
> Note that the `expiry` field covers the case where an offer is no longer
> valid after January 1st 2020.
>
> ## Default Offer
>
> The "default offer" of a node is a nominal offer used to send
> unsolicited payments.  It is generally not actually sent, but can be
> used by any other node as if it has been.  It has the following
> fields:
>
> * `offer_idenfitier`: zero-length
> * `d`: any
> * `n`: the node id of the recipient.
>
> ## Invoice Request Encoding
>
> Once it has an offer, the node can request an actual invoice using the
> `invoice_req` message inside `directed`'s `onion_routing_packet`.  It
> would expect an `invoice_or_error_tlv` inside the `directed_reply`
> message.
>
> This includes a `tag` it can use to identify replies, the
> `offer_idenfitier` from the offer, a `key` it can use to prove it was
> the requester of this invoice, a `recurrence` number if this
> is a payment in a recurring series, and other codes as required.
>
> The `refund_proof` refers to a previous invoice paid by the sender for
> the specific case of a `refund_for` offer.  It provides proof of
> payment (the `payment_preimage` and also a signature of the
> `payment_hash` from the `key` which requested the being-refunded
> invoice (which does *not* have to be the same as this `key`!).
>
> 1. tlvs: `invoice_request_tlv`
> 2. types:
>     1. type: 1 (`tag`)
>     2. data:
>        * [`...*byte`:`tag`]
>     1. type: 2 (`offer_identifier`)
>     2. data:
>        * [`...*byte`:`id`]
>     1. type: 3 (`key`)
>     2. data:
>       * [`32`:`key`]
>     1. type: 4 (`recurrence`)
>     2. data:
>       * [`tu64`:`number`]
>     1. type: 5 (`quantity`)
>     2. data:
>       * [`tu64`:`n`]
>     1. type: 6 (`delivery_address_name`)
>     2. data:
> 	 * [`...*byte`:`name`]
>     1. type: 7 (`delivery_address1`)
>     2. data:
> 	 * [`...*byte`:`address1`]
>     1. type: 8 (`delivery_address2`)
>     2. data:
> 	 * [`...*byte`:`address2`]
>     1. type: 9 (`delivery_city`)
>     2. data:
> 	 * [`...*byte`:`city`]
>     1. type: 10 (`delivery_state_province_or_region`)
>     2. data:
> 	 * [`...*byte`:`state_province_or_region`]
>     1. type: 11 (`delivery_zip_or_postal_code`)
>     2. data:
> 	 * [`...*byte`:`zip_or_postal_code`]
>     1. type: 12 (`delivery_country`)
>     2. data:
> 	 * [`2*byte`:`country_code`]
>     1. type: 13 (`delivery_telephone_number`)
>     2. data:
> 	 * [`...*byte`:`tel`]
>     1. type: 14 (`voucher_code`)
>     2. data:
> 	 * [`...*byte`:`code`]
>     1. type: 15 (`refund_proof`)
>     2. data:
>        * [`32*byte`:`payment_preimage`]
>        * [`signature`:`signature`]
>
> ## Requirements
>
> FIXME: many more
> Sender MUST use ISO 3166 alpha-2 code for `delivery_country`.
> Sender MUST set offer_identifier to match offer.
> Sender MUST include `key`
>     - SHOULD use a transient unpredictable key
>     - MUST reuse key for successive recurring invoices.
> Sender MUST set `recurrence` for recurring invoices.
>
> Receiver MUST check `offer_identifier`
> Receiver MUST check `delivery_` fields.
> Receiver MUST check `recurrence`.
> Receiver MUST check `amount`.
>
> 1. tlvs: `invoice_or_error_tlv`
> 2. types:
>     1. type: 1 (`tag`)
>     2. data:
>       * [`...*byte`:`tag`]
>     1. type: 3 (`omitted`)
>     2. data:
> 	 * [`...*u64`:`omitted_fields`]
>     1. type: 4 (`invoice`)
>     2. data:
>       * [`...*byte`:`invoice`]
>     1. type: 5 (`message`)
>       * [`...*byte`:`message`]
>     1. type: 6 (`replacement`)
>     2. data:
>       * [`signature`:`signature`]
>       * [`...*byte`:`offer`]
>
> Sender:
> - MUST copy `tag` from sender.
> - MUST omit fields it does not use, and place number in order in `omitted_fields`.
> - if it includes `invoice`:
>    - MUST not include `message`
>    - MUST not include `replacement`
>    - MUST [merkle fields it used](#merkle-calculation) and place that in invoice `s` field.
> - otherwise, if it includes `replacement`:
>    - MAY include `message`
> - otherwise:
>    - MUST include `message` describing the error.
>
> Receiver:
> - MUST check that `tag` matches req.
> - if `replacement`:
>    - MUST fail if `signature` does not sign `offer` with same key as original.
>    - MUST only fetch once (no double-redirects!)
> - if description or amount significantly changes, must re-ask user.
>    - SHOULD note if description simply has something appended (eg "+ postage").
> - within invoice:
>      - MUST check that `s` matches merkle of fields, minus `omitted`.
> 	- MUST check that no vital fields are in `omitted`.
> 	- MUST check that `d` matches `description`
>      - MUST check that `q` DNE if `quantity` DNE, otherwise is equal.
>      - Must check valid signature, etc.
>
> ## Merkle Calculation
>
> 1. For each `invoice_req_tlv` field in ascending `tlv` type order:
>     1. If the field was omitted, it is added to `omitted_fields`.
>     2. Otherwise, the immediate parent merkle is:
>
>        SHA256(SHA256(`tag` | `be64-n`) | SHA256(`tlv-value`))
>
>        Where `be64-n` is a 64-bit big-endian counter starting at 0 and
>        incrementing for each leaf.
>
>     3. Order these nodes in increasing SHA256(`tag` | `be64-n`) order.
>
> 2. Create additional leaves until `be64-n` is the next power of 2:
>
>        SHA256(`tag` | `be64-n`)
>
> 3. Combine adjacent leaves using SHA256(leaf1 | leaf2) until none remain.
>
> By creating adjacent leaves using the `tag` field and a counter, and
> sorting the leaves, the only significant information revealed by a
> merkle proof on a node is the depth of tree (which implies the total
> number of TLV fields).
>
> # InvRequests
>
> There are times when it makes sense to request an invoice over another
> medium, such as HTTP or a QR code.
>
> ## Encoding
>
> The human-readable part of a Lightning invrequest is `lni`.  The data part
> consists of three parts:
>
> 1. 0 or more [TLV](01-messaging.md#type-length-value-format) encoded fields.
> 2. A 32-byte nodeid[1]
> 3. 64-byte signature of SHA256(hrp-as-utf8 | tlv | nodeid).
>
> ## TLV Fields
>
> 1. tlvs: `invreq`
> 2. types:
>      1. type: 1 (`paths`)
>      2. data:
>          * [`u16`:`num_paths`]
>          * [`num_paths*path`:`path`]
>      1. type: 2 (`description`)
>      2. data:
>          * [`...*byte`:`description`]
>      1. type: 3 (`expiry`)
>      2. data:
>          * [`tu64`:`seconds_from_epoch`]
>      1. type: 4 (`amount`)
>      2. data:
>          * [`tu64`:`millisatoshis`]
>
> The fields `paths`, `description`, and `expiry` fields are the
> same as those for offers; the optional `amount` field describes the
> amount an invoice will be accepted for.
>
> Upon parsing and accepting an `invreq`, the node sends an
> `invoice_or_error_tlv` within an onion.  The reply is empty.
>
> ## Requirements
>
> The requirements for `paths`, `description` and `expiry` are
> [described above](#requirements-for-offers-and-invrequests).
>
> FIXME: More.
>
> [1] Assuming we go for Schnorr sigs and 32-byte pubkeys.
> ----
> Addendum: BOLT #7: P2P Node and Channel Discovery and Directed Messages
> ...
> # Directed Messages
>
> Directed messages allow peers to use existing connections to query for
> invoices (see [BOLT 12](12-offer-encoding.md)).  Like gossip messages,
> they are not associated with a particular local channel.
>
> The `id` is a unique, transient identifier between the peers, used to
> identify match messages and replies.
>
> ## The `directed` and `directed_reply` Messages
>
> 1. type: 384 (`directed`) (`option_directed_messages`)
> 2. data:
>      * [`chain_hash`:`chain_hash`]
>      * [`u64`:`id`]
>      * [`1366*byte`:`onion_routing_packet`]
>
> 1. type: 384 (`directed_reply`) (`option_directed_messages`)
> 2. data:
>      * [`chain_hash`:`chain_hash`]
>      * [`u64`:`id`]
>      * [`u16`:`len`]
>      * [`len*byte`:`reply`]
>
> ## Requirements
>
> FIXME: similar to update_add_htlc and update_fail_htlc.
> FIXME: define reasonable timeout after which you can forget if not replied?
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
Rusty Russell in reply to ZmnSCPxj permalink original

ZmnSCPxj <ZmnSCPxj at protonmail.com> writes:

Quoted text (15 lines)
> First, please confirm my understanding of the message flow.
> Suppose I have a donation offer on my website and Rusty wants to donate to me.
> Then:
>
>          ZmnSCPxj                      Rusty
>             |                            |
>             +---------- `lno` ---------->+ (via non-Lightning communication channel e.g. https)
>             |                            |
>             +<---- `invoice_request` ----+ (via a normal Rusty->ZmnSCPxj payment)
>             |                            |
>             +---- `invoice_or_error` --->| (by failing the above payment and embedding in the failure blob)
>             |                            |
>             +<------- `sendpay` ---------+ (via a normal Rusty->ZmnSCPxj payment)
>
> Is it approximately correct?

Sorry for delayed response; yes, this is correct.

Quoted text (16 lines)
>> gets an invoice request (`lni...`), and sends the invoice over the
>> lightning network, retreiving an empty reply.
>
> Here are completely pointless counterproposals for the offer and invoice-request HRPs:
>
> * Offers:
>   * `lnpayme`
>   * `lnbuyit`
>   * `lnforsale`
> * Invoice Requests:
>   * `lnpaying`
>   * `lnbuying`
>   * `lnshutupandtakemymoney`
>
> `lno` and `lni` feel wrong to me.
> Their juxtaposition implies `lno` == output and `lni` == input to me, due to the use of `o` and `i`, though `lno` is where you get money in exchange for product and `lni` is the request-for-service.

lnx and lny? Nobody can interpret them at all, that way :)

Quoted text (6 lines)
>>     3.  type: 2 (`description`)
>>     4.  data:
>>         -   [`...*byte`:`description`]
>
> UTF-8?
> Null-terminated?

I was thinking UTF-8 like current field.

Quoted text (5 lines)
>> -   MUST include `amount` if it includes `recurrence`.
>> -   if it includes `amount`:
>>     -   MUST specify `currency` as the ISO 4712 or BIP-0173, padded with zero bytes if required
>
> I cannot find ISO 4712, but could find ISO 4217.

Oops, I fixed my typo wrong. Thanks.

Quoted text (2 lines)
> BIP-173 does not have a list of currencies, but refers to SLIP-0173.
> Some of the listed currencies there seem to have more than 4 characters.

Oh, I'd never seen SLIP-0173. Cool, I increased it to 5; SLIP-0173 has no limit but I find it hard to care about any of them anyway.

Quoted text (2 lines)
> Should I assume encoding is ASCII?
> We will "never" see a non-ASCII currency code?

Not really, but if you don't understand it you can't do much, ASCII or no.

Quoted text (10 lines)
>> The "default offer" of a node is a nominal offer used to send
>> unsolicited payments. It is generally not actually sent, but can be
>> used by any other node as if it has been. It has the following
>> fields:
>>
>> -   `offer_idenfitier`: zero-length
>> -   `d`: any
>> -   `n`: the node id of the recipient.
>
> In essence, this is an implicitly-existing offer that never expires, and which can be used by any node at any time to construct an invoice request?

Yep!

Quoted text (9 lines)
>> The `refund_proof` refers to a previous invoice paid by the sender for
>> the specific case of a `refund_for` offer. It provides proof of
>> payment (the `payment_preimage` and also a signature of the
>> `payment_hash` from the `key` which requested the being-refunded
>> invoice (which does not have to be the same as this `key`!).
>
> An earlier requirement mentions that writers of offers or invoice request MUST have `paths` in some condition.
> The below does not have `paths`, but there is a "human-readable" alternate encoding which *does* have `paths`.
> It might be better to clarify this point.

The in-wire one doesn't have paths, since you respond by reply; you don't need (and should not be able to) find the sender.

The non-wire one needs a path, since you need to initiate a reply.

Quoted text (17 lines)
>> The `directed` and `directed_reply` Messages
>>
>> ---------------------------------------------
>>
>> 1.  type: 384 (`directed`) (`option_directed_messages`)
>> 2.  data:
>>     -   [`chain_hash`:`chain_hash`]
>>     -   [`u64`:`id`]
>>     -   [`1366*byte`:`onion_routing_packet`]
>> 3.  type: 384 (`directed_reply`) (`option_directed_messages`)
>> 4.  data:
>>     -   [`chain_hash`:`chain_hash`]
>>     -   [`u64`:`id`]
>>     -   [`u16`:`len`]
>>     -   [`len*byte`:`reply`]
>
> This new `directed` message will be the mechanism for sending invoice requests and receiving invoice request responses?

Yes.

Quoted text (1 line)
> What incentive is there for a forwarding node to actually forward a `directed` message?

It's a strong liveness indicator to the sender, so they're likely to use the same path for the actual payment.

Cheers, Rusty.

Rusty Russell in reply to Yaacov Akiba Slama permalink original

Yaacov Akiba Slama <ya at slamail.org> writes:

Quoted text (13 lines)
> Hi Rusty,
>
> It seems that there are two kind of TLV fields in your proposition:
> 1) LN specific fields like `num_paths` and `payment_preimage`.
> 2) "Business" fields like `address1` and `currency`.
> I understand the need to define and include the first category, but I
> don't think that we need or can define the second category. These fields
> already exists in software like crm, erp, etc.. and are well defined by
> standard body.
> My suggestion is to have a generic field containing well defined
> structured and standardized data. See for instance
> https://en.wikipedia.org/wiki/EDIFACT and/or
> https://en.wikipedia.org/wiki/Universal_Business_Language.

Hi Yaacov,

        I've been pondering this since reading your comment on the PR!
        As a fan of standards, I am attracted to UBL (I've chaired an
OASIS TC in the past and have great respect for them); as a fan of
simplicity I am not.  Forcing UBL implementation on wallet providers is
simply not going to happen, whatever I were to propose.
	We also don't want duplication; what if the "UBL field" were to
say I were selling you a bridge for $1 and the description and amount
fields actually said I was selling you a coffee for $3?
	However, since invoices/offers and UBL are both structures, we
should have an explicit mapping between the two.  What fields should
have their own existence in the invoice/offer and what should be in a
general UBL field is a question we have to think on further.
        Anyway, you'll have to bear with me as I read this 172 page
standard...

Cheers, Rusty.

Quoted text (434 lines)
> What do you think?
> PS: Sorry for crossposting here and in
> https://github.com/lightningnetwork/lightning-rfc/pull/694
> --yas
>
> On 05/11/2019 06:23, Rusty Russell wrote:
>> Hi all,
>>
>>          This took longer than I'd indicated; for that I'm sorry.
>> However, this should give us all something to chew on.  I've started
>> with a draft "BOLT 12" (it might be BOLT 13 by the time it's finalized
>> though!).
>>
>> I've also appended indications where we touch other BOLTs:
>> 1. BOLT 7 gains a message/reply system, encoded like htlc onions and
>>     failure messages.
>> 2. BOLT 11 gains a `q` field for quantity; this avoids changing the
>>     description when the user requests an invoice for more than one of something
>>     (since changing the description between offer and invoice requires user
>>     interaction: it's the *invoice* which you are committing to).
>>
>> There's definite handwaving in here; let's see if you can find it!
>>
>> Cheers,
>> Rusty.
>>
>> # BOLT #12: Offer Protocols for Lightning Payments
>>
>> An higher-level, QR-code-ready protocol for dealing with invoices over
>> Lightning.  There are two simple flows supported: in one, a user gets
>> an offer (`lno...`) and requests an invoice over the lightning
>> network, obtaining one (or an error) in reply.  In the other, a user
>> gets an invoice request (`lni...`), and sends the invoice over the
>> lightning network, retreiving an empty reply.
>>
>> # Table of Contents
>>
>>    * [Offers](#offers)
>>      * [Encoding](#encoding)
>>      * [TLV Fields](#tlv-fields)
>>    * [Invrequests](#invrequests)
>>      * [Encoding](#encoding)
>>      * [TLV Fields](#tlv-fields)
>>
>> # Offers
>>
>> Offers supply a reader with enough information to request one or more
>> invoices via the lightning network itself.
>>
>> ## Encoding
>>
>> The human-readable part of a Lightning offer is `lno`.  The data part
>> consists of three parts:
>>
>> 1. 0 or more [TLV](01-messaging.md#type-length-value-format) encoded fields.
>> 2. A 32-byte nodeid[1]
>> 3. 64-byte signature of SHA256(hrp-as-utf8 | tlv | nodeid).
>>
>> ## TLV Fields
>>
>> The TLV fields define how to get the invoice, and what it's for.
>> Each offer has a unique `offer_idenfitier` so the offering node can
>> distinguish different invoice requests.
>>
>> Offers can request recurring payments of various kinds, and specify
>> what base currency they are calculated in (the actual amount will be
>> in the invoice).
>>
>> `additional_data` is a bitfield which indicates what information the
>> invoice requester should (odd) or must (even) supply:
>> 1. Bits `0/1`: include `delivery_address`
>> 2. Bits `2/3`: include `delivery_telephone_number`
>> 3. Bits `4/5`: include `voucher_code`
>> 4. Bits `6/7`: include `refund_proof`
>>
>> `refund_for` indicates an offer for a (whole or part) refund for a
>> previous invoice, as indicated by the `payment_hash`.
>>
>> 1. tlvs: `offer`
>> 2. types:
>>      1. type: 1 (`paths`)
>>      2. data:
>>          * [`u16`:`num_paths`]
>>          * [`num_paths*path`:`path`]
>>      1. type: 2 (`description`)
>>      2. data:
>>          * [`...*byte`:`description`]
>>      1. type: 3 (`expiry`)
>>      2. data:
>>          * [`tu64`:`seconds_from_epoch`]
>>      1. type: 4 (`offer_identifier`)
>>      2. data:
>>          * [`...*byte`:`id`]
>>      1. type: 5 (`amount`)
>>      2. data:
>>          * [`4*byte`:`currency`]
>>          * [`tu64`:`amount`]
>>      1. type: 6 (`additional_data`)
>>      2. data:
>> 	    * [`...*byte`:`rbits`]
>>      1. type: 7 (`recurrance`)
>>      2. data:
>> 	    * [`byte`:`time_unit`]
>> 		* [`u32`:`period`]
>> 		* [`tu32`:`number`]
>>      1. type: 8 (`recurrance_base`)
>>      2. data:
>> 		* [`u32`:`basetime`]
>> 		* [`tu32`:`paywindow`]
>>      1. type: 9 (`quantity`)
>>      2. data:
>> 		* [`tu64`:`max`]
>>      1. type: 10 (`refund_for`)
>>      2. data:
>>          * [`32*byte`:`payment_hash`]
>>
>> 1. subtype: `path`
>> 2. data:
>>     * [`u16`:`num_hops`]
>>     * [`num_hops*hop`:`hops`]
>>
>> 1. subtype: `hop`
>> 2. data:
>>     * [`pubkey`:`nodeid`]
>>     * [`short_channel_id`:`short_channel_id`]
>>     * [`u16`:`flen`]
>>     * [`flen*byte`:`features`]
>>
>> ## Requirements For Offers And Invrequests
>>
>> A writer of an offer or an invrequest:
>>    - if it is connected only by private channels:
>>      - MUST include `paths` containing a path to the node.
>>    - otherwise:
>>      - MAY include `paths` containing a path to the node.
>>    - MUST describe the item(s) being offered or purpose of invoice in `description`.
>>    - MUST include `expiry` if the offer/invrequest will not be valid after some time.
>>    - if it includes `expiry`:
>>      - MUST set `seconds_from_epoch` to the expiry time in seconds since 1970 UTC.
>>
>> ## Requirements For Offers
>>
>> A writer of an offer:
>>    - MUST use a unique `offer_idenfitier` for each offer.
>>    - MAY include `recurrence` to indicate offer should trigger time-spaced
>>      invoices.
>>    - MUST include `amount` if it includes `recurrence`.
>>    - if it includes `amount`:
>>      - MUST specify `currency` as the ISO 4712 or BIP-0173, padded with zero bytes if required
>> 	- MUST specify `amount` to the amount expected for the invoice, as the ISO 4712 currency unit multiplied by exponent, OR the BIP-0173 minimum unit (eg. `satoshis`).
>>    - if it requires specific fields in the invoice:
>>      - MUST set the corresponding even bits in the `additional_data` field
>>
>> A reader of an offer:
>>    - SHOULD gain user consent for recurring payments.
>>    - SHOULD allow user to view and cancel recurring payments.
>>    - SHOULD gain user consent to send `delivery_` fields.
>>    - if it uses `amount` to provide the user with a cost estimate:
>> 	- MUST warn user if amount of actual invoice differs significantly
>> 		from that expectation.
>>    - FIXME: more!
>>
>> ## Recurrance
>>
>> Some offers are *periodic*, such as a subscription service or monthly
>> dues, in that payment is expected to be repeated.  There are many
>> different flavors of repetition, consider:
>>
>> * Payments due on the first of every month, for 6 months.
>> * Payments due on every Monday, 1pm Pacific Standard Time.
>> * Payments due once a year:
>>     * which must be made on January 1st, or
>>     * which are only valid if started January 1st 2020, or
>>     * which if paid after January 1st you (over) pay the full rate first year, or
>>     * which if paid after January 1st are paid pro-rata for the first year, or
>>     * which repeat from whenever you made the first payment
>>
>> Thus, each payment has:
>> 1. A `time_unit` defining 0 (seconds), 1 (days), 2 (months), 3 (years).
>> 2. A `period`, defining how often (in `time_unit`) it has to be paid.
>> 3. An optional `number` of total payments to be paid.
>> 4. An optional `basetime`, defining when the first payment applies
>>     in seconds since 1970-01-01 UTC.
>> 5. An optional `paywindow`, defining how many seconds into the period
>>     a payment will be accepted: 0xFFFFFFFF being a special value meaning
>>     "any time during the period, but you will have to pay proportionally
>>     to the remaining time in the period".
>>
>> Note that the `expiry` field covers the case where an offer is no longer
>> valid after January 1st 2020.
>>
>> ## Default Offer
>>
>> The "default offer" of a node is a nominal offer used to send
>> unsolicited payments.  It is generally not actually sent, but can be
>> used by any other node as if it has been.  It has the following
>> fields:
>>
>> * `offer_idenfitier`: zero-length
>> * `d`: any
>> * `n`: the node id of the recipient.
>>
>> ## Invoice Request Encoding
>>
>> Once it has an offer, the node can request an actual invoice using the
>> `invoice_req` message inside `directed`'s `onion_routing_packet`.  It
>> would expect an `invoice_or_error_tlv` inside the `directed_reply`
>> message.
>>
>> This includes a `tag` it can use to identify replies, the
>> `offer_idenfitier` from the offer, a `key` it can use to prove it was
>> the requester of this invoice, a `recurrence` number if this
>> is a payment in a recurring series, and other codes as required.
>>
>> The `refund_proof` refers to a previous invoice paid by the sender for
>> the specific case of a `refund_for` offer.  It provides proof of
>> payment (the `payment_preimage` and also a signature of the
>> `payment_hash` from the `key` which requested the being-refunded
>> invoice (which does *not* have to be the same as this `key`!).
>>
>> 1. tlvs: `invoice_request_tlv`
>> 2. types:
>>     1. type: 1 (`tag`)
>>     2. data:
>>        * [`...*byte`:`tag`]
>>     1. type: 2 (`offer_identifier`)
>>     2. data:
>>        * [`...*byte`:`id`]
>>     1. type: 3 (`key`)
>>     2. data:
>>       * [`32`:`key`]
>>     1. type: 4 (`recurrence`)
>>     2. data:
>>       * [`tu64`:`number`]
>>     1. type: 5 (`quantity`)
>>     2. data:
>>       * [`tu64`:`n`]
>>     1. type: 6 (`delivery_address_name`)
>>     2. data:
>> 	 * [`...*byte`:`name`]
>>     1. type: 7 (`delivery_address1`)
>>     2. data:
>> 	 * [`...*byte`:`address1`]
>>     1. type: 8 (`delivery_address2`)
>>     2. data:
>> 	 * [`...*byte`:`address2`]
>>     1. type: 9 (`delivery_city`)
>>     2. data:
>> 	 * [`...*byte`:`city`]
>>     1. type: 10 (`delivery_state_province_or_region`)
>>     2. data:
>> 	 * [`...*byte`:`state_province_or_region`]
>>     1. type: 11 (`delivery_zip_or_postal_code`)
>>     2. data:
>> 	 * [`...*byte`:`zip_or_postal_code`]
>>     1. type: 12 (`delivery_country`)
>>     2. data:
>> 	 * [`2*byte`:`country_code`]
>>     1. type: 13 (`delivery_telephone_number`)
>>     2. data:
>> 	 * [`...*byte`:`tel`]
>>     1. type: 14 (`voucher_code`)
>>     2. data:
>> 	 * [`...*byte`:`code`]
>>     1. type: 15 (`refund_proof`)
>>     2. data:
>>        * [`32*byte`:`payment_preimage`]
>>        * [`signature`:`signature`]
>>
>> ## Requirements
>>
>> FIXME: many more
>> Sender MUST use ISO 3166 alpha-2 code for `delivery_country`.
>> Sender MUST set offer_identifier to match offer.
>> Sender MUST include `key`
>>     - SHOULD use a transient unpredictable key
>>     - MUST reuse key for successive recurring invoices.
>> Sender MUST set `recurrence` for recurring invoices.
>>
>> Receiver MUST check `offer_identifier`
>> Receiver MUST check `delivery_` fields.
>> Receiver MUST check `recurrence`.
>> Receiver MUST check `amount`.
>>
>> 1. tlvs: `invoice_or_error_tlv`
>> 2. types:
>>     1. type: 1 (`tag`)
>>     2. data:
>>       * [`...*byte`:`tag`]
>>     1. type: 3 (`omitted`)
>>     2. data:
>> 	 * [`...*u64`:`omitted_fields`]
>>     1. type: 4 (`invoice`)
>>     2. data:
>>       * [`...*byte`:`invoice`]
>>     1. type: 5 (`message`)
>>       * [`...*byte`:`message`]
>>     1. type: 6 (`replacement`)
>>     2. data:
>>       * [`signature`:`signature`]
>>       * [`...*byte`:`offer`]
>>
>> Sender:
>> - MUST copy `tag` from sender.
>> - MUST omit fields it does not use, and place number in order in `omitted_fields`.
>> - if it includes `invoice`:
>>    - MUST not include `message`
>>    - MUST not include `replacement`
>>    - MUST [merkle fields it used](#merkle-calculation) and place that in invoice `s` field.
>> - otherwise, if it includes `replacement`:
>>    - MAY include `message`
>> - otherwise:
>>    - MUST include `message` describing the error.
>>
>> Receiver:
>> - MUST check that `tag` matches req.
>> - if `replacement`:
>>    - MUST fail if `signature` does not sign `offer` with same key as original.
>>    - MUST only fetch once (no double-redirects!)
>> - if description or amount significantly changes, must re-ask user.
>>    - SHOULD note if description simply has something appended (eg "+ postage").
>> - within invoice:
>>      - MUST check that `s` matches merkle of fields, minus `omitted`.
>> 	- MUST check that no vital fields are in `omitted`.
>> 	- MUST check that `d` matches `description`
>>      - MUST check that `q` DNE if `quantity` DNE, otherwise is equal.
>>      - Must check valid signature, etc.
>>
>> ## Merkle Calculation
>>
>> 1. For each `invoice_req_tlv` field in ascending `tlv` type order:
>>     1. If the field was omitted, it is added to `omitted_fields`.
>>     2. Otherwise, the immediate parent merkle is:
>>
>>        SHA256(SHA256(`tag` | `be64-n`) | SHA256(`tlv-value`))
>>
>>        Where `be64-n` is a 64-bit big-endian counter starting at 0 and
>>        incrementing for each leaf.
>>
>>     3. Order these nodes in increasing SHA256(`tag` | `be64-n`) order.
>>
>> 2. Create additional leaves until `be64-n` is the next power of 2:
>>
>>        SHA256(`tag` | `be64-n`)
>>
>> 3. Combine adjacent leaves using SHA256(leaf1 | leaf2) until none remain.
>>
>> By creating adjacent leaves using the `tag` field and a counter, and
>> sorting the leaves, the only significant information revealed by a
>> merkle proof on a node is the depth of tree (which implies the total
>> number of TLV fields).
>>
>> # InvRequests
>>
>> There are times when it makes sense to request an invoice over another
>> medium, such as HTTP or a QR code.
>>
>> ## Encoding
>>
>> The human-readable part of a Lightning invrequest is `lni`.  The data part
>> consists of three parts:
>>
>> 1. 0 or more [TLV](01-messaging.md#type-length-value-format) encoded fields.
>> 2. A 32-byte nodeid[1]
>> 3. 64-byte signature of SHA256(hrp-as-utf8 | tlv | nodeid).
>>
>> ## TLV Fields
>>
>> 1. tlvs: `invreq`
>> 2. types:
>>      1. type: 1 (`paths`)
>>      2. data:
>>          * [`u16`:`num_paths`]
>>          * [`num_paths*path`:`path`]
>>      1. type: 2 (`description`)
>>      2. data:
>>          * [`...*byte`:`description`]
>>      1. type: 3 (`expiry`)
>>      2. data:
>>          * [`tu64`:`seconds_from_epoch`]
>>      1. type: 4 (`amount`)
>>      2. data:
>>          * [`tu64`:`millisatoshis`]
>>
>> The fields `paths`, `description`, and `expiry` fields are the
>> same as those for offers; the optional `amount` field describes the
>> amount an invoice will be accepted for.
>>
>> Upon parsing and accepting an `invreq`, the node sends an
>> `invoice_or_error_tlv` within an onion.  The reply is empty.
>>
>> ## Requirements
>>
>> The requirements for `paths`, `description` and `expiry` are
>> [described above](#requirements-for-offers-and-invrequests).
>>
>> FIXME: More.
>>
>> [1] Assuming we go for Schnorr sigs and 32-byte pubkeys.
>> ----
>> Addendum: BOLT #7: P2P Node and Channel Discovery and Directed Messages
>> ...
>> # Directed Messages
>>
>> Directed messages allow peers to use existing connections to query for
>> invoices (see [BOLT 12](12-offer-encoding.md)).  Like gossip messages,
>> they are not associated with a particular local channel.
>>
>> The `id` is a unique, transient identifier between the peers, used to
>> identify match messages and replies.
>>
>> ## The `directed` and `directed_reply` Messages
>>
>> 1. type: 384 (`directed`) (`option_directed_messages`)
>> 2. data:
>>      * [`chain_hash`:`chain_hash`]
>>      * [`u64`:`id`]
>>      * [`1366*byte`:`onion_routing_packet`]
>>
>> 1. type: 384 (`directed_reply`) (`option_directed_messages`)
>> 2. data:
>>      * [`chain_hash`:`chain_hash`]
>>      * [`u64`:`id`]
>>      * [`u16`:`len`]
>>      * [`len*byte`:`reply`]
>>
>> ## Requirements
>>
>> FIXME: similar to update_add_htlc and update_fail_htlc.
>> FIXME: define reasonable timeout after which you can forget if not replied?
>> _______________________________________________
>> Lightning-dev mailing list
>> Lightning-dev at lists.linuxfoundation.org
>> https://www.learnbitcoin.com/archive/lists/lightning-dev
Yaacov Akiba Slama in reply to Rusty Russell permalink original

Hi Rusty.

On 08/11/2019 05:09, Rusty Russell wrote:

Quoted text (7 lines)
> Hi Yaacov,
>          I've been pondering this since reading your comment on the PR!
>
>          As a fan of standards, I am attracted to UBL (I've chaired an
> OASIS TC in the past and have great respect for them); as a fan of
> simplicity I am not.  Forcing UBL implementation on wallet providers is
> simply not going to happen, whatever I were to propose.

In fact, using UBL in LN specification is simpler than trying to understand the semantic of each field needed by businesses. You are right that using such a standard put the burden into wallet providers instead of LN developers, but as a wallet (breez) provider, I can say that:

1) Most money transactions (currently in fiat) are between users and companies and not between two users. If we want to replace FIAT by bitcoin, we need to create an infrastructure which can be used by businesses. That means that LN needs to be able to be integrated easily into POS systems. So, as a wallet provider who want to help the transition from fiat to bitcoin, I need to be able to support standards even if that means that I have to implement using/parsing big and complicated standards.

For simple user to user transaction, the wallet can decide to use only a subset of the fields defined by the standard.

2) From a technical point of view, it seems that there are already UBL libraries in java and c#. I don't think such library is hard to write in go, rust.., so every wallet implementation can use them.

Quoted text (9 lines)
>
> 	We also don't want duplication; what if the "UBL field" were to
> say I were selling you a bridge for $1 and the description and amount
> fields actually said I was selling you a coffee for $3?
>
> 	However, since invoices/offers and UBL are both structures, we
> should have an explicit mapping between the two.  What fields should
> have their own existence in the invoice/offer and what should be in a
> general UBL field is a question we have to think on further.

I agree that we don't want duplication. This is the reason, I propose to use only ubl structure and add in the ln standard invoice an ubl "opaque" field which will be self-contained and only add in the invoice/offer/.. the fields specific to ln.

Quoted text (2 lines)
>          Anyway, you'll have to bear with me as I read this 172 page
> standard...

Sure :-)

BTW, Thanks a lot for your all your work. LN would not have been where it is without your push.

Ross Dyson in reply to Yaacov Akiba Slama permalink original

Hi Rusty,

We spoke in detail about this after your presentation at LNconf. I'm one of the contributors to LNURL so I am a little familiar with what you're trying to achieve and am very grateful you're considering implementing something similar to the mainnet protocol.

I can only see delivery address being a nightmare for the network or wallet providers. If you take a quick look at any Shopify website right now and try to buy something to be delivered you will see validation of address inputs before accepting payment.

This is the 'expected' UX of consumer applications in 2019. If offers were to not validate address inputs correctly the user will not receive the product, lose money, and have a [very] negative review of both the wallet-providing and the offer-providing businesses.

Handling these UX expectations will require either the wallet provider or the offer provider to validate the inputs before proceeding with the sale.

   1. If the offer provider handles validation then the network will have
   to accommodate potentially infinite validation attempts (big no no I assume)
   2. If the wallet provider were to provide the UX for input validation
   they are taking on significant workload to develop a robust address input
   UI, but more importantly the responsibility to correctly validate. There is
   plenty of room to screw up and create a catastrophic user experience.

So I think address validation input is only possible via 2. but I think it is too much workload and responsibility to expect from wallet providers.

Quoted text (1 line)
>From what I can see, it would not be impossible to bring delivery address

functionality into offers retroactively after offers was already in prod. Perhaps icebox it?

I am very excited for LNOs and LNIs. If we want to get offers in prod and being facilitated by wallet providers I think it would be best if it was streamlined a little first.

Thanks for reading,

Ross

On Fri, Nov 8, 2019 at 3:40 PM Yaacov Akiba Slama <ya at slamail.org> wrote:

Quoted text (58 lines)
> Hi Rusty.
>
> On 08/11/2019 05:09, Rusty Russell wrote:
> > Hi Yaacov,
> >          I've been pondering this since reading your comment on the PR!
> >
> >          As a fan of standards, I am attracted to UBL (I've chaired an
> > OASIS TC in the past and have great respect for them); as a fan of
> > simplicity I am not.  Forcing UBL implementation on wallet providers is
> > simply not going to happen, whatever I were to propose.
>
> In fact, using UBL in LN specification is simpler than trying to
> understand the semantic of each field needed by businesses. You are
> right that using such a standard put the burden into wallet providers
> instead of LN developers, but as a wallet (breez) provider, I can say that:
>
> 1) Most money transactions (currently in fiat) are between users and
> companies and not between two users. If we want to replace FIAT by
> bitcoin, we need to create an infrastructure which can be used by
> businesses. That means that LN needs to be able to be integrated easily
> into POS systems. So, as a wallet provider who want to help the
> transition from fiat to bitcoin, I need to be able to support standards
> even if that means that I have to implement using/parsing big and
> complicated standards.
>
> For simple user to user transaction, the wallet can decide to use only a
> subset of the fields defined by the standard.
>
> 2) From a technical point of view, it seems that there are already UBL
> libraries in java and c#. I don't think such library is hard to write in
> go, rust.., so every wallet implementation can use them.
>
> >
> >       We also don't want duplication; what if the "UBL field" were to
> > say I were selling you a bridge for $1 and the description and amount
> > fields actually said I was selling you a coffee for $3?
> >
> >       However, since invoices/offers and UBL are both structures, we
> > should have an explicit mapping between the two.  What fields should
> > have their own existence in the invoice/offer and what should be in a
> > general UBL field is a question we have to think on further.
> I agree that we don't want duplication. This is the reason, I propose to
> use only ubl structure and add in the ln standard invoice an ubl
> "opaque" field which will be self-contained and only add in the
> invoice/offer/.. the fields specific to ln.
> >          Anyway, you'll have to bear with me as I read this 172 page
> > standard...
>
> Sure :-)
>
> BTW, Thanks a lot for your all your work. LN would not have been where
> it is without your push.
>
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
>
Rusty Russell in reply to Yaacov Akiba Slama permalink original

Yaacov Akiba Slama <ya at slamail.org> writes:

Quoted text (31 lines)
> Hi Rusty.
>
> On 08/11/2019 05:09, Rusty Russell wrote:
>> Hi Yaacov,
>>          I've been pondering this since reading your comment on the PR!
>>
>>          As a fan of standards, I am attracted to UBL (I've chaired an
>> OASIS TC in the past and have great respect for them); as a fan of
>> simplicity I am not.  Forcing UBL implementation on wallet providers is
>> simply not going to happen, whatever I were to propose.
>
> In fact, using UBL in LN specification is simpler than trying to
> understand the semantic of each field needed by businesses. You are
> right that using such a standard put the burden into wallet providers
> instead of LN developers, but as a wallet (breez) provider, I can say that:
>
> 1) Most money transactions (currently in fiat) are between users and
> companies and not between two users. If we want to replace FIAT by
> bitcoin, we need to create an infrastructure which can be used by
> businesses. That means that LN needs to be able to be integrated easily
> into POS systems. So, as a wallet provider who want to help the
> transition from fiat to bitcoin, I need to be able to support standards
> even if that means that I have to implement using/parsing big and
> complicated standards.
>
> For simple user to user transaction, the wallet can decide to use only a
> subset of the fields defined by the standard.
>
> 2) From a technical point of view, it seems that there are already UBL
> libraries in java and c#. I don't think such library is hard to write in
> go, rust.., so every wallet implementation can use them.

That is not the problem. The problem is that our order flow is simple:

        Seller: Offer
        Buyer: Invoice Request
        Seller: Invoice (or updated Offer)
        Buyer/Seller: Payment & Acknowledgement (atomic)

(This could, of course, fit into a larger business flow.)

The closest UBL flow seems to be:

        Seller: Quotation
        Buyer: Order
        Seller: (Prepayment)Invoice (or updated Quotation)

It's also worth noting that, even compressed, none of the UBL examples fit into the 1023 byte limit of the existing invoice format:

        UBL-Quotation-2.1-Example.xml: 1864 bytes (gz)
        UBL-Order-2.1-Example.xml: 2515 bytes (gz)
        UBL-Invoice-2.1-Example.xml: 3163 bytes (gz)

Indeed, that Quotation alone requires a 32x32 QR code.

Quoted text (8 lines)
>> 	However, since invoices/offers and UBL are both structures, we
>> should have an explicit mapping between the two.  What fields should
>> have their own existence in the invoice/offer and what should be in a
>> general UBL field is a question we have to think on further.
> I agree that we don't want duplication. This is the reason, I propose to
> use only ubl structure and add in the ln standard invoice an ubl
> "opaque" field which will be self-contained and only add in the
> invoice/offer/.. the fields specific to ln.

Except we need to go through the UBL spec and indicate exactly what fields are permitted, and which are required.

Many UBI fields are not amenable to machine interpretation (eg. note fields). These must be either explicitly exposed to the buyer (in case the seller uses them) such as shipping conditions, or explicitly forbidden/ignored.

This is not a small task, and required intimiate knowledge of the UBL spec. It's not enough just to make something *look* like UBL.

Does anyone have expertise in this area? Shall we form a sub-group to investigate this properly?

Thanks! Rusty.

Rusty Russell in reply to Ross Dyson permalink original

Ross Dyson <me at rossdyson.com> writes:

Quoted text (29 lines)
> Hi Rusty,
>
> We spoke in detail about this after your presentation at LNconf. I'm one of
> the contributors to LNURL so I am a little familiar with what you're trying
> to achieve and am very grateful you're considering implementing something
> similar to the mainnet protocol.
>
> I can only see delivery address being a nightmare for the network or wallet
> providers. If you take a quick look at any Shopify website right now and
> try to buy something to be delivered you will see validation of address
> inputs before accepting payment.
>
> This is the 'expected' UX of consumer applications in 2019. If offers were
> to not validate address inputs correctly the user will not receive the
> product, lose money, and have a [very] negative review of both the
> wallet-providing and the offer-providing businesses.
>
> Handling these UX expectations will require either the wallet provider or
> the offer provider to validate the inputs before proceeding with the sale.
>
>    1. If the offer provider handles validation then the network will have
>    to accommodate potentially infinite validation attempts (big no no I assume)
>    2. If the wallet provider were to provide the UX for input validation
>    they are taking on significant workload to develop a robust address input
>    UI, but more importantly the responsibility to correctly validate. There is
>    plenty of room to screw up and create a catastrophic user experience.
>
> So I think address validation input is only possible via 2. but I think it
> is too much workload and responsibility to expect from wallet providers.

This is not the area I worry about, TBH, since every shopping website in existence has implemented address input (and some form of validation). I'm sure it'll be primitive to start with.

Of course, UBL has a standard 'AddressType' too:

        http://docs.oasis-open.org/ubl/os-UBL-2.2/xsd/common/UBL-CommonAggregateComponents-2.2.xsd
Quoted text (3 lines)
>>From what I can see, it would not be impossible to bring delivery address
> functionality into offers retroactively after offers was already in prod.
> Perhaps icebox it?

Quite possibly something we can delay; most current goods are virtual anyway. However, delivery address standardization would greatly improve the UX for such things.

Cheers, Rusty.

Yaacov Akiba Slama in reply to Rusty Russell permalink original

On 11/11/2019 06:11, Rusty Russell wrote:

Quoted text (17 lines)
> 2) From a technical point of view, it seems that there are already UBL
> libraries in java and c#. I don't think such library is hard to write in
> go, rust.., so every wallet implementation can use them.
> That is not the problem.  The problem is that our order flow is simple:
>
>         Seller: Offer
>         Buyer: Invoice Request
>         Seller: Invoice (or updated Offer)
>         Buyer/Seller: Payment & Acknowledgement (atomic)
>
> (This could, of course, fit into a larger business flow.)
>
> The closest UBL flow seems to be:
>
>         Seller: Quotation
>         Buyer: Order
>         Seller: (Prepayment)Invoice (or updated Quotation)

In UBL, 2 flows are defined (Traditional and Self Billing) and from what I know, the right flow depends on the country and even on the industry (services or goods for instance). What I suggest is to superpose the "strict" LN flow (invoice then payment) to the business flow. So for instance when a prepayment invoice is needed, the simplified (from UBL pov) flow will be:

  Seller: Quotation (UBL)
  Buyer: Order (UBL)
  Seller: Prepayment Invoice (UBL)
  Seller: Invoice (LN)
  Buyer/Seller: Payment & Ack (LN)
  Buyer: Receipt (UBL)

The advantage of such workflow are that we don't need to add any fields to the current invoice structure, nor to define in the LN protocol new messages like offer or invoice request, nor to intervene in the semantic of the business workflow and in the required/optional fields in these messages.

Quoted text (35 lines)
>
> It's also worth noting that, even compressed, none of the UBL examples
> fit into the 1023 byte limit of the existing invoice format:
>
>         UBL-Quotation-2.1-Example.xml: 1864 bytes (gz)
>         UBL-Order-2.1-Example.xml: 2515 bytes (gz)
>         UBL-Invoice-2.1-Example.xml: 3163 bytes (gz)
>
> Indeed, that Quotation alone requires a 32x32 QR code.
>
>>> 	However, since invoices/offers and UBL are both structures, we
>>> should have an explicit mapping between the two.  What fields should
>>> have their own existence in the invoice/offer and what should be in a
>>> general UBL field is a question we have to think on further.
>> I agree that we don't want duplication. This is the reason, I propose to
>> use only ubl structure and add in the ln standard invoice an ubl
>> "opaque" field which will be self-contained and only add in the
>> invoice/offer/.. the fields specific to ln.
> Except we need to go through the UBL spec and indicate exactly what
> fields are permitted, and which are required.
>
> Many UBI fields are not amenable to machine interpretation (eg. note
> fields).  These must be either explicitly exposed to the buyer (in case
> the seller uses them) such as shipping conditions, or explicitly
> forbidden/ignored.
>
> This is not a small task, and required intimiate knowledge of the UBL
> spec.  It's not enough just to make something *look* like UBL.
>
> Does anyone have expertise in this area?  Shall we form a sub-group to
> investigate this properly?
>
> Thanks!
> Rusty.
>
Rusty Russell in reply to Yaacov Akiba Slama permalink original

Yaacov Akiba Slama <ya at slamail.org> writes:

Quoted text (18 lines)
>   Seller: Quotation (UBL)
>
>   Buyer: Order (UBL)
>
>   Seller: Prepayment Invoice (UBL)
>
>   Seller: Invoice (LN)
>
>   Buyer/Seller: Payment & Ack (LN)
>
>   Buyer: Receipt (UBL)
>
>
> The advantage of such workflow are that we don't need to add any fields
> to the current invoice structure, nor to define in the LN protocol new
> messages like offer or invoice request, nor to intervene in the semantic
> of the business workflow and in the required/optional fields in these
> messages.

This would be UBL treating Lightning as a dumb payment layer, which is a little like faxing email, and not a use case I'd be promoting for Lightning.

To be clear: the full UBL spec is machine *parsable* but definitely not designed to be machine *handlable*. This makes sense, since a machine cannot generally choose between quotations or interpret general contract terms.

However, for the simpler (but very common!) case of an offer->purchase flow, we can define a subset of UBL for which this *can* be done, and a further-limited subset which must be examined by the user (e.g. description of goods, price details, shipping info).

In addition, the atomic nature of LN needs to be baked into the protocol; in LN taking the payment *requires* a cryptographic receipt, and neutering this property would be horribly short-sighted.

We need to define UBL extensions for the LN fields to tie them all together (e.g. payment_hash, node_id). We also need to define a transport mechanism for these over the Lightning Network.

This is all quite possible! But it will take time and is a signficant amount of work: I need to be sure that others feel the same way before I embark on this project.

Cheers, Rusty.

Quoted text (34 lines)
>> It's also worth noting that, even compressed, none of the UBL examples
>> fit into the 1023 byte limit of the existing invoice format:
>>
>>         UBL-Quotation-2.1-Example.xml: 1864 bytes (gz)
>>         UBL-Order-2.1-Example.xml: 2515 bytes (gz)
>>         UBL-Invoice-2.1-Example.xml: 3163 bytes (gz)
>>
>> Indeed, that Quotation alone requires a 32x32 QR code.
>>
>>>> 	However, since invoices/offers and UBL are both structures, we
>>>> should have an explicit mapping between the two.  What fields should
>>>> have their own existence in the invoice/offer and what should be in a
>>>> general UBL field is a question we have to think on further.
>>> I agree that we don't want duplication. This is the reason, I propose to
>>> use only ubl structure and add in the ln standard invoice an ubl
>>> "opaque" field which will be self-contained and only add in the
>>> invoice/offer/.. the fields specific to ln.
>> Except we need to go through the UBL spec and indicate exactly what
>> fields are permitted, and which are required.
>>
>> Many UBI fields are not amenable to machine interpretation (eg. note
>> fields).  These must be either explicitly exposed to the buyer (in case
>> the seller uses them) such as shipping conditions, or explicitly
>> forbidden/ignored.
>>
>> This is not a small task, and required intimiate knowledge of the UBL
>> spec.  It's not enough just to make something *look* like UBL.
>>
>> Does anyone have expertise in this area?  Shall we form a sub-group to
>> investigate this properly?
>>
>> Thanks!
>> Rusty.
>>
Yaacov Akiba Slama in reply to Rusty Russell permalink original

On 13/11/2019 05:44, Rusty Russell wrote:

Quoted text (17 lines)
> Yaacov Akiba Slama <ya at slamail.org> writes:
>>   Seller: Quotation (UBL)
>>
>>   Buyer: Order (UBL)
>>
>>   Seller: Prepayment Invoice (UBL)
>>
>>   Seller: Invoice (LN)
>>
>>   Buyer/Seller: Payment & Ack (LN)
>>
>>   Seller: Receipt (UBL)
>>
>
> This would be UBL treating Lightning as a dumb payment layer, which is a
> little like faxing email, and not a use case I'd be promoting for
> Lightning.

I don't understand the comparison. Both email an fax are way to transmit data and are not related to the *content* of what is transmitted. Consequently email and fax are in competing as a way to transmit data. But LN and UBL (as I understand them) are not in competition because:

* LN is a messaging protocol (soon) and a payment protocol.

* UBL is a protocol defining business interaction workflow *around* payments.

So we can integrate between them without intermixing the semantics of the protocols but we need only to define the interaction points between them.

In the previous worflow, the seller can for instance add in the LN
invoice H(Quotation (UBL)||Order(UBL)||Prepayment Invoice(UBL)), and use
H(Receipt(UBL)) as preimage. With such a workflow, the UBL documents are
cryptographically tied to the LN payment.

So the property of UBL of not being machine *handlable* is not altered but the LN cryptographic properties are still used to tie the workflow.

Am I missing something?

--yas

Quoted text (60 lines)
>
> To be clear: the full UBL spec is machine *parsable* but definitely not
> designed to be machine *handlable*.  This makes sense, since a machine
> cannot generally choose between quotations or interpret general contract
> terms.
>
> However, for the simpler (but very common!) case of an offer->purchase
> flow, we can define a subset of UBL for which this *can* be done, and a
> further-limited subset which must be examined by the user
> (e.g. description of goods, price details, shipping info).
>
> In addition, the atomic nature of LN needs to be baked into the
> protocol; in LN taking the payment *requires* a cryptographic receipt,
> and neutering this property would be horribly short-sighted.
>
> We need to define UBL extensions for the LN fields to tie them all
> together (e.g. payment_hash, node_id).  We also need to define a
> transport mechanism for these over the Lightning Network.
>
> This is all quite possible!  But it will take time and is a signficant
> amount of work: I need to be sure that others feel the same way before I
> embark on this project.
>
> Cheers,
> Rusty.
>
>>> It's also worth noting that, even compressed, none of the UBL examples
>>> fit into the 1023 byte limit of the existing invoice format:
>>>
>>>         UBL-Quotation-2.1-Example.xml: 1864 bytes (gz)
>>>         UBL-Order-2.1-Example.xml: 2515 bytes (gz)
>>>         UBL-Invoice-2.1-Example.xml: 3163 bytes (gz)
>>>
>>> Indeed, that Quotation alone requires a 32x32 QR code.
>>>
>>>>> 	However, since invoices/offers and UBL are both structures, we
>>>>> should have an explicit mapping between the two.  What fields should
>>>>> have their own existence in the invoice/offer and what should be in a
>>>>> general UBL field is a question we have to think on further.
>>>> I agree that we don't want duplication. This is the reason, I propose to
>>>> use only ubl structure and add in the ln standard invoice an ubl
>>>> "opaque" field which will be self-contained and only add in the
>>>> invoice/offer/.. the fields specific to ln.
>>> Except we need to go through the UBL spec and indicate exactly what
>>> fields are permitted, and which are required.
>>>
>>> Many UBI fields are not amenable to machine interpretation (eg. note
>>> fields).  These must be either explicitly exposed to the buyer (in case
>>> the seller uses them) such as shipping conditions, or explicitly
>>> forbidden/ignored.
>>>
>>> This is not a small task, and required intimiate knowledge of the UBL
>>> spec.  It's not enough just to make something *look* like UBL.
>>>
>>> Does anyone have expertise in this area?  Shall we form a sub-group to
>>> investigate this properly?
>>>
>>> Thanks!
>>> Rusty.
>>>
Rusty Russell in reply to Yaacov Akiba Slama permalink original

Yaacov Akiba Slama <ya at slamail.org> writes:

Quoted text (13 lines)
> So we can integrate between them without intermixing the semantics of
> the protocols but we need only to define the interaction points between
> them.
>
> In the previous worflow, the seller can for instance add in the LN
> invoice H(Quotation (UBL)||Order(UBL)||Prepayment Invoice(UBL)), and use
> H(Receipt(UBL)) as preimage. With such a workflow, the UBL documents are
> cryptographically tied to the LN payment.
>
> So the property of UBL of not being machine *handlable* is not altered
> but the LN cryptographic properties are still used to tie the workflow.
>
> Am I missing something?

Sure, people can do this today: simply set your `d` field to "UBL: <hash>".

But it provide what we want from offers: 1. Does not provide a "static invoice" flow. 2. Does not provide a donation flow. 3. Does not provide a method for wallets to do recurrence. 4. Does not provide end-to-end over LN (i.e. no HTTP(s) requests).

Cheers, Rusty.

Jorge Timón in reply to Yaacov Akiba Slama permalink original
Quoted text (3 lines)
> - if `replacement`:
>   - MUST fail if `signature` does not sign `offer` with same key as original.
>  - MUST only fetch once (no double-redirects!)

The offer can only ever be replaced once or only once every so often? Perhaps it can only be replaced once per invoice_request?

Quoted text (1 line)
> - if description or amount significantly changes, must re-ask user.

Let's say we're talking about a 20 year monthly subscription and the price rises 1% per year. Did the price "significantly changed" because it ends up adding to a lot in the 20 year compared to what the offer said, or it did not because the individual 1% increases are considered small? "significantly changes" sounds handwavy indeed.

Regarding slip-0173, I fear this end up becoming used in practice as ids, given that they rely on a centralized manual registry vulnerable to name squatting and all that. It's probably nothing to worry much about, since the software can configure them locally, for example, clightning does it in https://github.com/ElementsProject/lightning/blob/master/bitcoin/chainparams.c And thus if two chains happen to end up using the same string for the bip173 hrp, it is the software maintainer and not who pushed to slip-0173 first what decides.

I guess in addition to the hrp in bip173, we can add a chain_id field to the offer and perhaps even repeated hrp for different chains could be reused within the same software. Although it is certainly something chain creators should try to avoid if possible.

I guess this applies to bolt11 invoices too, although for the invoices sent through the lightning network it is redundant, since the chain_id is already included in all messages.

On another note, perhaps I'm wrong, but I think this lacks a way for people to potentially add their own custom fields, and I foresee people abusing the description field, and ending up putting json, protobuf or, even worse, xml in them. Oh, there's no description field in invoice request, then whatever is big enough and it's not needed for those specific use cases. I was thinking how to abuse it for my own use case as I was reading, and I don't think it's one covered by UBL. Perhaps it's too stupid to ever be covered there, but here we go.

I'm just playing with a little protocol in which an http server (bob) accepts requests from users (or alices) and gives an invoice in response. Then after alice pays the invoice, she can submit the payment hash and preimage to bob to prove him that the invoice has indeed been paid so that bob goes ahead and performs some "action", which can actually fail, in which case there should be a refund, which I currently don't handle in any way. But whether it fails in the "action" or not, bob gives some response. So far this seems pretty generic I bet many use cases follow this pattern, with different fields for the "request" which will somehow determine what the "action" will be. Perhaps not all require the extra step of actively proving the payment, since bob could detect the payment on his own. The current bolt12 draft could be used and take care of the refund part.

So if we can somehow brainstorm all possible fields in the request that can make sense for all cases, that would be great. The current draft doesn't cover my case though. In my case, the request contains a bolt11 invoice that bob is supposed to pay, that is the "action" here. But after alice pays the invoice to bob.

This probably sounds quite stupidly unnecessary (and I actually hope it is), but note that the invoice in the request may be for a different chain. In that case, during "action", bob would need to try to pay that invoice using another node connected to the lightning network of that other chain.

But the details of my concrete use case matter much. The point is, should we add a bolt11 field to invoice request only for my use case? I don't think so, if the invoices of the 2 chains can be paid atomically, this use case as it is would become obsolete, and then we would want to remove the field from the spec again. And I bet other people will think of other fields they would like to add to the invoice_request but being something too specific they will also feel it doesn't belong to the specs. So we'll simply put the fields in the description. Is that intended?

I'm assuming bob (the offer creator) will be able to use plugins or something to do extra or custom validations on the invoice requests. And perhaps alice could use plugins too to better handle custom errors from particular types of bobs. Perhaps I'm assuming too much? Does that make sense to you?

If you're really curious, the code for my use case is in https://github.com/jtimon/multi-ln-demo/tree/master/py-ln-gateway but I already said I don't handle refunds and it doesn't even have a database yet, so anybody using this with production chains is not reckless but simply stupid.

Yaacov Akiba Slama in reply to Rusty Russell permalink original

On 14/11/2019 03:59, Rusty Russell wrote:

Quoted text (16 lines)
> Yaacov Akiba Slama <ya at slamail.org> writes:
>> So we can integrate between them without intermixing the semantics of
>> the protocols but we need only to define the interaction points between
>> them.
>>
>> In the previous worflow, the seller can for instance add in the LN
>> invoice H(Quotation (UBL)||Order(UBL)||Prepayment Invoice(UBL)), and use
>> H(Receipt(UBL)) as preimage. With such a workflow, the UBL documents are
>> cryptographically tied to the LN payment.
>>
>> So the property of UBL of not being machine *handlable* is not altered
>> but the LN cryptographic properties are still used to tie the workflow.
>>
>> Am I missing something?
> Sure, people can do this today: simply set your `d` field to "UBL:
> <hash>".

Exactly. But we can add a BOLT which contains 1) references to UBL as a way to exchange the business information needed in the payer<->payee interactions and 2) describe the process of tying the document(s) to the payment(s).

Quoted text (2 lines)
> But it provide what we want from offers:
> 1. Does not provide a "static invoice" flow.

What do you mean by "static invoice" flow? Perhaps:

* Seller -> Buyer: Invoice (UBL)
* Seller -> Buyer: Invoice (LN)
* Buyer & Seller: Payment + Ack (LN)
* Seller -> Buyer: Receipt (UBL)
Quoted text (1 line)
> 2. Does not provide a donation flow.
* Payer -> Payee: Order (UBL)
* Payee ->Payer: Invoice (LN)
* Payer & Payee: Payment + Ack (LN)
* Payer -> Payer: Receipt (UBL)
Quoted text (1 line)
> 3. Does not provide a method for wallets to do recurrence.

A simplified workflow can be:

* Seller -> Buyer: Quotation (UBL) containing recurring information
* Buyer -> Seller: Order (UBL) containing recurring information

Then at every beginning/end of period (depending on the info in the quotation/order)

* Seller -> Buyer: Invoice (UBL)

* Seller -> Buyer: Invoice (LN) (d contain the hash of the invoice + f(previous docs))

* Buyer & Seller: Payment & Ack (LN)
* Seller -> Buyer: Receipt (UBL)
Quoted text (1 line)
> 4. Does not provide end-to-end over LN (i.e. no HTTP(s) requests).

Yes as soon as LN support messaging (using [1] or [2] for instance)

--yas

[1]: https://www.learnbitcoin.com/archive/lists/lightning-dev/t/2019/a-proposal-for-up-front-payments#m-06c2a149

[2]: https://www.learnbitcoin.com/archive/lists/lightning-dev/t/2019/hornet-via-circular-self-payment-routing-in-a-decorrelation-payment-points#m-eff06b77