|
| 1 | +import * as t from 'io-ts'; |
| 2 | +import { BigIntFromString } from 'io-ts-types'; |
| 3 | +import { httpRoute, httpRequest, optional } from '@api-ts/io-ts-http'; |
| 4 | +import { BitgoExpressError } from '../../schemas/error'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Path parameters for lightning withdraw |
| 8 | + */ |
| 9 | +export const LightningWithdrawParams = { |
| 10 | + /** The coin identifier (e.g., 'lnbtc', 'tlnbtc') */ |
| 11 | + coin: t.string, |
| 12 | + /** The ID of the wallet */ |
| 13 | + id: t.string, |
| 14 | +} as const; |
| 15 | + |
| 16 | +/** |
| 17 | + * Lightning onchain recipient |
| 18 | + */ |
| 19 | +const LightningOnchainRecipient = t.type({ |
| 20 | + /** Amount in satoshis (as string that will be converted to BigInt) */ |
| 21 | + amountSat: BigIntFromString, |
| 22 | + /** Bitcoin address to send to */ |
| 23 | + address: t.string, |
| 24 | +}); |
| 25 | + |
| 26 | +/** |
| 27 | + * Request body for lightning onchain withdrawal |
| 28 | + */ |
| 29 | +export const LightningWithdrawRequestBody = { |
| 30 | + /** Array of recipients to pay */ |
| 31 | + recipients: t.array(LightningOnchainRecipient), |
| 32 | + /** Wallet passphrase for signing */ |
| 33 | + passphrase: t.string, |
| 34 | + /** Fee rate in satoshis per virtual byte (as string that will be converted to BigInt) */ |
| 35 | + satsPerVbyte: optional(BigIntFromString), |
| 36 | + /** Target number of blocks for confirmation */ |
| 37 | + numBlocks: optional(t.number), |
| 38 | + /** Optional sequence ID for the withdraw transfer */ |
| 39 | + sequenceId: optional(t.string), |
| 40 | + /** Optional comment for the withdraw transfer */ |
| 41 | + comment: optional(t.string), |
| 42 | +} as const; |
| 43 | + |
| 44 | +/** |
| 45 | + * Withdraw status codec |
| 46 | + */ |
| 47 | +const WithdrawStatus = t.union([t.literal('delivered'), t.literal('failed')]); |
| 48 | + |
| 49 | +/** |
| 50 | + * LND create withdraw response |
| 51 | + */ |
| 52 | +const LndCreateWithdrawResponse = t.intersection([ |
| 53 | + t.type({ |
| 54 | + /** Status of the withdrawal */ |
| 55 | + status: WithdrawStatus, |
| 56 | + }), |
| 57 | + t.partial({ |
| 58 | + /** Transaction ID (txid) if delivered */ |
| 59 | + txid: t.string, |
| 60 | + /** Failure reason if failed */ |
| 61 | + failureReason: t.string, |
| 62 | + }), |
| 63 | +]); |
| 64 | + |
| 65 | +/** |
| 66 | + * Transaction request state |
| 67 | + */ |
| 68 | +const TxRequestState = t.union([ |
| 69 | + t.literal('pendingCommitment'), |
| 70 | + t.literal('pendingApproval'), |
| 71 | + t.literal('canceled'), |
| 72 | + t.literal('rejected'), |
| 73 | + t.literal('initialized'), |
| 74 | + t.literal('pendingDelivery'), |
| 75 | + t.literal('delivered'), |
| 76 | + t.literal('pendingUserSignature'), |
| 77 | + t.literal('signed'), |
| 78 | +]); |
| 79 | + |
| 80 | +/** |
| 81 | + * Pending approval state |
| 82 | + */ |
| 83 | +const PendingApprovalState = t.union([ |
| 84 | + t.literal('pending'), |
| 85 | + t.literal('awaitingSignature'), |
| 86 | + t.literal('pendingBitGoAdminApproval'), |
| 87 | + t.literal('pendingIdVerification'), |
| 88 | + t.literal('pendingCustodianApproval'), |
| 89 | + t.literal('pendingFinalApproval'), |
| 90 | + t.literal('approved'), |
| 91 | + t.literal('processing'), |
| 92 | + t.literal('rejected'), |
| 93 | +]); |
| 94 | + |
| 95 | +/** |
| 96 | + * Pending approval type |
| 97 | + */ |
| 98 | +const PendingApprovalType = t.union([ |
| 99 | + t.literal('userChangeRequest'), |
| 100 | + t.literal('transactionRequest'), |
| 101 | + t.literal('policyRuleRequest'), |
| 102 | + t.literal('updateApprovalsRequiredRequest'), |
| 103 | + t.literal('transactionRequestFull'), |
| 104 | +]); |
| 105 | + |
| 106 | +/** |
| 107 | + * Transaction request details in pending approval info |
| 108 | + * When transactionRequest is present, coinSpecific, recipients, and buildParams are REQUIRED |
| 109 | + * Only sourceWallet is optional |
| 110 | + */ |
| 111 | +const TransactionRequestDetails = t.intersection([ |
| 112 | + t.type({ |
| 113 | + /** Coin-specific transaction details - REQUIRED */ |
| 114 | + coinSpecific: t.record(t.string, t.unknown), |
| 115 | + /** Recipients of the transaction - REQUIRED */ |
| 116 | + recipients: t.unknown, |
| 117 | + /** Build parameters for the transaction - REQUIRED */ |
| 118 | + buildParams: t.intersection([ |
| 119 | + t.partial({ |
| 120 | + /** Type of transaction (fanout or consolidate) - OPTIONAL */ |
| 121 | + type: t.union([t.literal('fanout'), t.literal('consolidate')]), |
| 122 | + }), |
| 123 | + t.record(t.string, t.unknown), |
| 124 | + ]), |
| 125 | + }), |
| 126 | + t.partial({ |
| 127 | + /** Source wallet for the transaction - OPTIONAL */ |
| 128 | + sourceWallet: t.string, |
| 129 | + }), |
| 130 | +]); |
| 131 | + |
| 132 | +/** |
| 133 | + * Pending approval info nested object |
| 134 | + */ |
| 135 | +const PendingApprovalInfo = t.intersection([ |
| 136 | + t.type({ |
| 137 | + /** Type of pending approval */ |
| 138 | + type: PendingApprovalType, |
| 139 | + }), |
| 140 | + t.partial({ |
| 141 | + /** Transaction request associated with approval */ |
| 142 | + transactionRequest: TransactionRequestDetails, |
| 143 | + }), |
| 144 | +]); |
| 145 | + |
| 146 | +/** |
| 147 | + * Pending approval data |
| 148 | + */ |
| 149 | +const PendingApproval = t.intersection([ |
| 150 | + t.type({ |
| 151 | + /** Pending approval ID */ |
| 152 | + id: t.string, |
| 153 | + /** State of the pending approval */ |
| 154 | + state: PendingApprovalState, |
| 155 | + /** Creator of the pending approval */ |
| 156 | + creator: t.string, |
| 157 | + /** Information about the pending approval */ |
| 158 | + info: PendingApprovalInfo, |
| 159 | + }), |
| 160 | + t.partial({ |
| 161 | + /** Wallet ID if wallet-specific */ |
| 162 | + wallet: t.string, |
| 163 | + /** Enterprise ID if enterprise-specific */ |
| 164 | + enterprise: t.string, |
| 165 | + /** Number of approvals required */ |
| 166 | + approvalsRequired: t.number, |
| 167 | + /** Associated transaction request ID */ |
| 168 | + txRequestId: t.string, |
| 169 | + }), |
| 170 | +]); |
| 171 | + |
| 172 | +/** |
| 173 | + * Response for lightning onchain withdrawal |
| 174 | + */ |
| 175 | +const LightningWithdrawResponse = t.intersection([ |
| 176 | + t.type({ |
| 177 | + /** Unique identifier for withdraw request submitted to BitGo */ |
| 178 | + txRequestId: t.string, |
| 179 | + /** Status of withdraw request submission to BitGo */ |
| 180 | + txRequestState: TxRequestState, |
| 181 | + }), |
| 182 | + t.partial({ |
| 183 | + /** Pending approval details, if applicable */ |
| 184 | + pendingApproval: PendingApproval, |
| 185 | + /** Current snapshot of withdraw status (if available) */ |
| 186 | + withdrawStatus: LndCreateWithdrawResponse, |
| 187 | + }), |
| 188 | +]); |
| 189 | + |
| 190 | +/** |
| 191 | + * Response type mapping for lightning withdraw |
| 192 | + */ |
| 193 | +export const LightningWithdrawResponseType = { |
| 194 | + 200: LightningWithdrawResponse, |
| 195 | + 400: BitgoExpressError, |
| 196 | + 401: BitgoExpressError, |
| 197 | + 404: BitgoExpressError, |
| 198 | + 500: BitgoExpressError, |
| 199 | +} as const; |
| 200 | + |
| 201 | +/** |
| 202 | + * Lightning Onchain Withdrawal API |
| 203 | + * |
| 204 | + * Withdraws lightning balance to an onchain Bitcoin address |
| 205 | + * |
| 206 | + * @operationId express.v2.wallet.lightningWithdraw |
| 207 | + * @tag express |
| 208 | + */ |
| 209 | +export const PostLightningWalletWithdraw = httpRoute({ |
| 210 | + path: '/api/v2/{coin}/wallet/{id}/lightning/withdraw', |
| 211 | + method: 'POST', |
| 212 | + request: httpRequest({ |
| 213 | + params: LightningWithdrawParams, |
| 214 | + body: LightningWithdrawRequestBody, |
| 215 | + }), |
| 216 | + response: LightningWithdrawResponseType, |
| 217 | +}); |
0 commit comments