Twine: Buy and Sell CKB Without Depositing on an Exchange (Fiber P2P PoC)

Thank you very much for your feedback.

The hold hash is now the buyer’s. The daemon stores no preimage at take. It learns payment_preimage only after paying the buyer’s invoice, then registers that with the watchtower and settles. Until that payment succeeds, the timelock refunds the seller.

On a sell post the invoice is on take. On a buy post both sides get need-invoice, and the buyer sends payout-invoice before any hold exists. A hash already used by another trade is rejected, and a replacement invoice, including one in a dispute or a solver award for the buyer, has to keep the same hash. The payout hash is written before send_payment, and the payout poll retries on error instead of stopping.

A buyer can still hand the preimage to someone else, and TWINE_SOLVER is still a key the operator configures. This needs a Fiber node that returns payment_preimage. Current develop does.

sequenceDiagram
    autonumber
    participant BN as Buyer node
    participant B as Buyer
    participant D as Daemon
    participant O as Operator node
    participant S as Seller

    alt Sell post
        S->>D: new-order
        BN->>BN: mint S_b, invoice under H(S_b)
        B->>D: take with that invoice
    else Buy post
        B->>D: new-order
        S->>D: take, no invoice
        D-->>B: need-invoice
        D-->>S: need-invoice
        BN->>BN: mint S_b, invoice under H(S_b)
        B->>D: payout-invoice
    end

    D->>O: hold under H(S_b), no preimage
    D-->>S: pay-invoice
    D-->>B: pay-invoice
    Note over O: no S_b, so no settle and no force-close claim
    S->>O: pays the hold
    O-->>D: Received
    D-->>B: waiting-fiat
    D-->>S: waiting-fiat
    B->>S: fiat, outside Twine
    B->>D: fiat-sent
    S->>D: release
    D->>D: record the hash before paying
    D->>O: send_payment
    O->>BN: pays H(S_b)
    O-->>D: payment_preimage is S_b
    D->>O: create_preimage, then settle
    D-->>B: settled
    D-->>S: settled
stateDiagram-v2
    [*] --> waitingInvoice: buy post taken
    [*] --> waitingHold: sell post taken
    waitingInvoice --> waitingHold: payout-invoice
    waitingInvoice --> canceled: cancel, or 1 hour with no invoice
    waitingHold --> waitingFiat: seller pays the hold
    waitingHold --> canceled: cancel while the invoice is Open
    waitingHold --> expired: unpaid hold expires
    waitingFiat --> fiatSent: fiat-sent
    waitingFiat --> refunding: 15 minutes with no fiat-sent
    waitingFiat --> disputed: dispute
    fiatSent --> releasing: seller releases
    fiatSent --> refunding: safety deadline
    fiatSent --> disputed: dispute
    releasing --> settled: buyer paid, then hold settled
    releasing --> awaitingInvoice: payout fails
    awaitingInvoice --> releasing: new invoice, same payment hash
    awaitingInvoice --> refunding: safety deadline
    awaitingInvoice --> disputed: dispute
    disputed --> releasing: seller releases, or solver awards the buyer
    disputed --> refunding: solver awards the seller, or safety deadline
    refunding --> expired: hold expires, slice returns to the post

One correction in the sequence: the operator node learns S_b by paying. The buyer node already had it.

2 Likes