|
| 1 | +package assets |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/btcsuite/btcd/btcutil" |
| 7 | + "github.com/btcsuite/btcd/btcutil/psbt" |
| 8 | + "github.com/btcsuite/btcd/chaincfg/chainhash" |
| 9 | + "github.com/btcsuite/btcd/wire" |
| 10 | + "github.com/lightninglabs/lndclient" |
| 11 | + "github.com/lightninglabs/loop/fsm" |
| 12 | + "github.com/lightninglabs/taproot-assets/asset" |
| 13 | + "github.com/lightninglabs/taproot-assets/tappsbt" |
| 14 | + "github.com/lightninglabs/taproot-assets/taprpc" |
| 15 | + wrpc "github.com/lightninglabs/taproot-assets/taprpc/assetwalletrpc" |
| 16 | + "github.com/lightninglabs/taproot-assets/taprpc/mintrpc" |
| 17 | + "github.com/lightninglabs/taproot-assets/taprpc/tapdevrpc" |
| 18 | + "github.com/lightninglabs/taproot-assets/taprpc/universerpc" |
| 19 | + "github.com/lightningnetwork/lnd/chainntnfs" |
| 20 | + "github.com/lightningnetwork/lnd/keychain" |
| 21 | + "github.com/lightningnetwork/lnd/lntypes" |
| 22 | + "github.com/lightningnetwork/lnd/lnwallet/chainfee" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + // DefaultSwapCSVExpiry is the default expiry for a swap in blocks. |
| 27 | + DefaultSwapCSVExpiry = int32(24) |
| 28 | + |
| 29 | + defaultHtlcFeeConfTarget = 3 |
| 30 | + defaultHtlcConfRequirement = 2 |
| 31 | + |
| 32 | + AssetKeyFamily = 696969 |
| 33 | +) |
| 34 | + |
| 35 | +// TapdClient is an interface that groups the methods required to interact with |
| 36 | +// the taproot-assets server and the wallet. |
| 37 | +type AssetClient interface { |
| 38 | + taprpc.TaprootAssetsClient |
| 39 | + wrpc.AssetWalletClient |
| 40 | + mintrpc.MintClient |
| 41 | + universerpc.UniverseClient |
| 42 | + tapdevrpc.TapDevClient |
| 43 | + |
| 44 | + // FundAndSignVpacket funds ands signs a vpacket. |
| 45 | + FundAndSignVpacket(ctx context.Context, |
| 46 | + vpkt *tappsbt.VPacket) (*tappsbt.VPacket, error) |
| 47 | + |
| 48 | + // PrepareAndCommitVirtualPsbts prepares and commits virtual psbts. |
| 49 | + PrepareAndCommitVirtualPsbts(ctx context.Context, |
| 50 | + vpkt *tappsbt.VPacket, feeRateSatPerKVByte chainfee.SatPerVByte) ( |
| 51 | + *psbt.Packet, []*tappsbt.VPacket, []*tappsbt.VPacket, |
| 52 | + *wrpc.CommitVirtualPsbtsResponse, error) |
| 53 | + |
| 54 | + // LogAndPublish logs and publishes the virtual psbts. |
| 55 | + LogAndPublish(ctx context.Context, btcPkt *psbt.Packet, |
| 56 | + activeAssets []*tappsbt.VPacket, passiveAssets []*tappsbt.VPacket, |
| 57 | + commitResp *wrpc.CommitVirtualPsbtsResponse) (*taprpc.SendAssetResponse, |
| 58 | + error) |
| 59 | + |
| 60 | + // CheckBalanceById checks the balance of an asset by its id. |
| 61 | + CheckBalanceById(ctx context.Context, assetId []byte, |
| 62 | + requestedBalance btcutil.Amount) error |
| 63 | + |
| 64 | + // DeriveNewKeys derives a new internal and script key. |
| 65 | + DeriveNewKeys(ctx context.Context) (asset.ScriptKey, |
| 66 | + keychain.KeyDescriptor, error) |
| 67 | +} |
| 68 | + |
| 69 | +// SwapStore is an interface that groups the methods required to store swap |
| 70 | +// information. |
| 71 | +type SwapStore interface { |
| 72 | + // CreateAssetSwapOut creates a new swap out in the store. |
| 73 | + CreateAssetSwapOut(ctx context.Context, swap *SwapOut) error |
| 74 | + |
| 75 | + // UpdateAssetSwapHtlcOutpoint updates the htlc outpoint of a swap out. |
| 76 | + UpdateAssetSwapHtlcOutpoint(ctx context.Context, swapHash lntypes.Hash, |
| 77 | + outpoint *wire.OutPoint, confirmationHeight int32) error |
| 78 | + |
| 79 | + // UpdateAssetSwapOutProof updates the proof of a swap out. |
| 80 | + UpdateAssetSwapOutProof(ctx context.Context, swapHash lntypes.Hash, |
| 81 | + rawProof []byte) error |
| 82 | + |
| 83 | + // UpdateAssetSwapOutSweepTx updates the sweep tx of a swap out. |
| 84 | + UpdateAssetSwapOutSweepTx(ctx context.Context, |
| 85 | + swapHash lntypes.Hash, sweepTxid chainhash.Hash, |
| 86 | + confHeight int32, sweepPkscript []byte) error |
| 87 | + |
| 88 | + // InsertAssetSwapUpdate inserts a new swap update in the store. |
| 89 | + InsertAssetSwapUpdate(ctx context.Context, |
| 90 | + swapHash lntypes.Hash, state fsm.StateType) error |
| 91 | + |
| 92 | + UpdateAssetSwapOutPreimage(ctx context.Context, |
| 93 | + swapHash lntypes.Hash, preimage lntypes.Preimage) error |
| 94 | +} |
| 95 | + |
| 96 | +// BlockHeightSubscriber is responsible for subscribing to the expiry height |
| 97 | +// of a swap, as well as getting the current block height. |
| 98 | +type BlockHeightSubscriber interface { |
| 99 | + // SubscribeExpiry subscribes to the expiry of a swap. It returns true |
| 100 | + // if the expiry is already past. Otherwise, it returns false and calls |
| 101 | + // the expiryFunc when the expiry height is reached. |
| 102 | + SubscribeExpiry(swapHash [32]byte, |
| 103 | + expiryHeight int32, expiryFunc func()) bool |
| 104 | + // GetBlockHeight returns the current block height. |
| 105 | + GetBlockHeight() int32 |
| 106 | +} |
| 107 | + |
| 108 | +// InvoiceSubscriber is responsible for subscribing to an invoice. |
| 109 | +type InvoiceSubscriber interface { |
| 110 | + // SubscribeInvoice subscribes to an invoice. The update callback is |
| 111 | + // called when the invoice is updated and the error callback is called |
| 112 | + // when an error occurs. |
| 113 | + SubscribeInvoice(ctx context.Context, invoiceHash lntypes.Hash, |
| 114 | + updateCallback func(lndclient.InvoiceUpdate, error)) error |
| 115 | +} |
| 116 | + |
| 117 | +// TxConfirmationSubscriber is responsible for subscribing to the confirmation |
| 118 | +// of a transaction. |
| 119 | +type TxConfirmationSubscriber interface { |
| 120 | + |
| 121 | + // SubscribeTxConfirmation subscribes to the confirmation of a |
| 122 | + // pkscript on the chain. The callback is called when the pkscript is |
| 123 | + // confirmed or when an error occurs. |
| 124 | + SubscribeTxConfirmation(ctx context.Context, swapHash lntypes.Hash, |
| 125 | + txid *chainhash.Hash, pkscript []byte, numConfs int32, |
| 126 | + eightHint int32, cb func(*chainntnfs.TxConfirmation, error)) error |
| 127 | +} |
| 128 | + |
| 129 | +// ExchangeRateProvider is responsible for providing the exchange rate between |
| 130 | +// assets. |
| 131 | +type ExchangeRateProvider interface { |
| 132 | + // GetSatsPerAssetUnit returns the amount of satoshis per asset unit. |
| 133 | + GetSatsPerAssetUnit(assetId []byte) (btcutil.Amount, error) |
| 134 | +} |
0 commit comments