|
| 1 | +package deposit |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/btcsuite/btcd/wire" |
| 9 | + "github.com/btcsuite/btcwallet/wtxmgr" |
| 10 | + "github.com/lightninglabs/taproot-assets/proof" |
| 11 | +) |
| 12 | + |
| 13 | +// State is the enum used for deposit states. |
| 14 | +type State uint8 |
| 15 | + |
| 16 | +const ( |
| 17 | + // StateInitiated indicates that the deposit has been initiated by the |
| 18 | + // client. |
| 19 | + StateInitiated State = 0 |
| 20 | + |
| 21 | + // StatePending indicates that the deposit is pending confirmation on |
| 22 | + // the blockchain. |
| 23 | + StatePending State = 1 |
| 24 | + |
| 25 | + // StateConfirmed indicates that the deposit has been confirmed on the |
| 26 | + // blockchain. |
| 27 | + StateConfirmed State = 2 |
| 28 | + |
| 29 | + // StateExpired indicates that the deposit has expired. |
| 30 | + StateExpired State = 3 |
| 31 | + |
| 32 | + // StateTimeoutSweepPublished indicates that the timeout sweep has been |
| 33 | + // published. |
| 34 | + StateTimeoutSweepPublished State = 4 |
| 35 | + |
| 36 | + // StateWithdrawn indicates that the deposit has been withdrawn. |
| 37 | + StateWithdrawn State = 5 |
| 38 | + |
| 39 | + // StateCooperativeSweepPublished indicates that the cooperative sweep |
| 40 | + // withdrawing the deposit has been published. |
| 41 | + StateCooperativeSweepPublished State = 6 |
| 42 | + |
| 43 | + // StateKeyRevealed indicates that the client has revealed a valid key |
| 44 | + // for the deposit which is now ready to be swept. |
| 45 | + StateKeyRevealed State = 7 |
| 46 | + |
| 47 | + // StateSpent indicates that the deposit has been spent. |
| 48 | + StateSpent State = 8 |
| 49 | + |
| 50 | + // StateSwept indicates that the deposit has been swept, either by a |
| 51 | + // timeout sweep or a cooperative (ie withdrawal) sweep. |
| 52 | + StateSwept State = 9 |
| 53 | +) |
| 54 | + |
| 55 | +// String coverts a deposit state to human readable string. |
| 56 | +func (s State) String() string { |
| 57 | + switch s { |
| 58 | + case StateInitiated: |
| 59 | + return "Initiated" |
| 60 | + |
| 61 | + case StatePending: |
| 62 | + return "Pending" |
| 63 | + |
| 64 | + case StateConfirmed: |
| 65 | + return "Confirmed" |
| 66 | + |
| 67 | + case StateExpired: |
| 68 | + return "Expired" |
| 69 | + |
| 70 | + case StateTimeoutSweepPublished: |
| 71 | + return "TimeoutSweepPublished" |
| 72 | + |
| 73 | + case StateWithdrawn: |
| 74 | + return "Withdrawn" |
| 75 | + |
| 76 | + case StateCooperativeSweepPublished: |
| 77 | + return "CooperativeSweepPublished" |
| 78 | + |
| 79 | + case StateKeyRevealed: |
| 80 | + return "KeyRevealed" |
| 81 | + |
| 82 | + case StateSpent: |
| 83 | + return "Spent" |
| 84 | + |
| 85 | + case StateSwept: |
| 86 | + return "Swept" |
| 87 | + |
| 88 | + default: |
| 89 | + return "Unknown" |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// IsFinal returns true if the deposit state is final, meaning that no further |
| 94 | +// actions can be taken on the deposit. |
| 95 | +func (s State) IsFinal() bool { |
| 96 | + return s == StateSpent || s == StateSwept |
| 97 | +} |
| 98 | + |
| 99 | +// DepositInfo holds publicly available information about an asset deposit. |
| 100 | +// It is used to communicate deposit details to clients of the deposit Manager. |
| 101 | +type DepositInfo struct { |
| 102 | + // ID is the unique identifier for this deposit which will also be used |
| 103 | + // to store the deposit in both the server and client databases. |
| 104 | + ID string |
| 105 | + |
| 106 | + // Verison is the protocol version of the deposit. |
| 107 | + Version AssetDepositProtocolVersion |
| 108 | + |
| 109 | + // CreatedAt is the time when the deposit was created (on the client). |
| 110 | + CreatedAt time.Time |
| 111 | + |
| 112 | + // Amount is the amount of asset to be deposited. |
| 113 | + Amount uint64 |
| 114 | + |
| 115 | + // Addr is the TAP deposit address where the asset will be sent. |
| 116 | + Addr string |
| 117 | + |
| 118 | + // State is the deposit state. |
| 119 | + State State |
| 120 | + |
| 121 | + // ConfirmationHeight is the block height at which the deposit was |
| 122 | + // confirmed. |
| 123 | + ConfirmationHeight uint32 |
| 124 | + |
| 125 | + // Outpoint is the anchor outpoint of the deposit. It is only set if the |
| 126 | + // deposit has been confirmed. |
| 127 | + Outpoint *wire.OutPoint |
| 128 | + |
| 129 | + // Expiry is the block height at which the deposit will expire. It is |
| 130 | + // only set if the deposit has been confirmed. |
| 131 | + Expiry uint32 |
| 132 | + |
| 133 | + // SweepAddr is the address we'll use to sweep the deposit back after |
| 134 | + // timeout or if cooperatively withdrawing. |
| 135 | + SweepAddr string |
| 136 | +} |
| 137 | + |
| 138 | +// Copy creates a copy of the DepositInfo struct. |
| 139 | +func (d *DepositInfo) Copy() *DepositInfo { |
| 140 | + info := &DepositInfo{ |
| 141 | + ID: d.ID, |
| 142 | + Version: d.Version, |
| 143 | + CreatedAt: d.CreatedAt, |
| 144 | + Amount: d.Amount, |
| 145 | + Addr: d.Addr, |
| 146 | + State: d.State, |
| 147 | + ConfirmationHeight: d.ConfirmationHeight, |
| 148 | + Expiry: d.Expiry, |
| 149 | + SweepAddr: d.SweepAddr, |
| 150 | + } |
| 151 | + |
| 152 | + if d.Outpoint != nil { |
| 153 | + info.Outpoint = &wire.OutPoint{ |
| 154 | + Hash: d.Outpoint.Hash, |
| 155 | + Index: d.Outpoint.Index, |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return info |
| 160 | +} |
| 161 | + |
| 162 | +// Deposit is the struct that holds all the information about an asset deposit. |
| 163 | +type Deposit struct { |
| 164 | + *Kit |
| 165 | + |
| 166 | + *DepositInfo |
| 167 | + |
| 168 | + // PkScript is the pkscript of the deposit anchor output. |
| 169 | + PkScript []byte |
| 170 | + |
| 171 | + // Proof is the proof of the deposit transfer. |
| 172 | + Proof *proof.Proof |
| 173 | + |
| 174 | + // AnchorRootHash is the root hash of the deposit anchor output. |
| 175 | + AnchorRootHash []byte |
| 176 | +} |
| 177 | + |
| 178 | +// label returns a string label that we can use for marking a transfer funding |
| 179 | +// the deposit. This is useful if we need to filter deposits. |
| 180 | +func (d *Deposit) label() string { |
| 181 | + return fmt.Sprintf("deposit %v", d.ID) |
| 182 | +} |
| 183 | + |
| 184 | +// lockID converts a deposit ID to a lock ID. The lock ID is used to lock inputs |
| 185 | +// used for the deposit sweep transaction. Note that we assume that the deposit |
| 186 | +// ID is a hex-encoded string of the same length as the lock ID. |
| 187 | +func (d *Deposit) lockID() (wtxmgr.LockID, error) { |
| 188 | + var lockID wtxmgr.LockID |
| 189 | + depositIDBytes, err := hex.DecodeString(d.ID) |
| 190 | + if err != nil { |
| 191 | + return wtxmgr.LockID{}, err |
| 192 | + } |
| 193 | + |
| 194 | + if len(depositIDBytes) != len(lockID) { |
| 195 | + return wtxmgr.LockID{}, fmt.Errorf("invalid deposit ID "+ |
| 196 | + "length: %d", len(depositIDBytes)) |
| 197 | + } |
| 198 | + |
| 199 | + copy(lockID[:], depositIDBytes) |
| 200 | + |
| 201 | + return lockID, nil |
| 202 | +} |
0 commit comments