|
| 1 | +import { BuildTransactionError, TransactionType } from '@bitgo/sdk-core'; |
| 2 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 3 | +import { AtomicInCTransactionBuilder } from './atomicInCTransactionBuilder'; |
| 4 | + |
| 5 | +// Lightweight interface placeholders replacing Avalanche SDK transaction shapes |
| 6 | +interface FlareExportInputShape { |
| 7 | + address: string; |
| 8 | + amount: bigint; // includes exported amount + fee |
| 9 | + assetId: Buffer; |
| 10 | + nonce: bigint; |
| 11 | +} |
| 12 | + |
| 13 | +interface FlareExportOutputShape { |
| 14 | + addresses: string[]; // destination P-chain addresses |
| 15 | + amount: bigint; // exported amount |
| 16 | + assetId: Buffer; |
| 17 | +} |
| 18 | + |
| 19 | +interface FlareUnsignedExportTx { |
| 20 | + networkId: number; |
| 21 | + sourceBlockchainId: Buffer; // C-chain id |
| 22 | + destinationBlockchainId: Buffer; // P-chain id |
| 23 | + inputs: FlareExportInputShape[]; |
| 24 | + outputs: FlareExportOutputShape[]; |
| 25 | +} |
| 26 | + |
| 27 | +interface FlareSignedExportTx { |
| 28 | + unsignedTx: FlareUnsignedExportTx; |
| 29 | + // credentials placeholder |
| 30 | + credentials: unknown[]; |
| 31 | +} |
| 32 | + |
| 33 | +type RawFlareExportTx = FlareSignedExportTx; // for now treat them the same |
| 34 | + |
| 35 | +export class ExportInCTxBuilder extends AtomicInCTransactionBuilder { |
| 36 | + private _amount?: bigint; |
| 37 | + private _nonce?: bigint; |
| 38 | + |
| 39 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 40 | + super(_coinConfig); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Utxos are not required in Export Tx in C-Chain. |
| 45 | + * Override utxos to prevent used by throwing a error. |
| 46 | + * |
| 47 | + * @param {DecodedUtxoObj[]} value ignored |
| 48 | + */ |
| 49 | + utxos(_value: unknown[]): this { |
| 50 | + throw new BuildTransactionError('utxos are not required in Export Tx in C-Chain'); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Amount is a long that specifies the quantity of the asset that this output owns. Must be positive. |
| 55 | + * The transaction output amount add a fixed fee that will be paid upon import. |
| 56 | + * |
| 57 | + * @param {BN | string} amount The withdrawal amount |
| 58 | + */ |
| 59 | + amount(amount: bigint | number | string): this { |
| 60 | + const n = typeof amount === 'bigint' ? amount : BigInt(amount); |
| 61 | + this.validateAmount(n); |
| 62 | + this._amount = n; |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Set the nonce of C-Chain sender address |
| 68 | + * |
| 69 | + * @param {number | string} nonce - number that can be only used once |
| 70 | + */ |
| 71 | + nonce(nonce: bigint | number | string): this { |
| 72 | + const n = typeof nonce === 'bigint' ? nonce : BigInt(nonce); |
| 73 | + this.validateNonce(n); |
| 74 | + this._nonce = n; |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Export tx target P wallet. |
| 80 | + * |
| 81 | + * @param pAddresses |
| 82 | + */ |
| 83 | + to(pAddresses: string | string[]): this { |
| 84 | + const pubKeys = Array.isArray(pAddresses) ? pAddresses : pAddresses.split('~'); |
| 85 | + // For now ensure they are stored as bech32 / string addresses directly |
| 86 | + this.transaction._to = pubKeys.map((a) => a.toString()); |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + protected get transactionType(): TransactionType { |
| 91 | + return TransactionType.Export; |
| 92 | + } |
| 93 | + |
| 94 | + initBuilder(raw: RawFlareExportTx): this { |
| 95 | + // Basic shape validation |
| 96 | + const unsigned = raw.unsignedTx; |
| 97 | + if (unsigned.networkId !== this.transaction._networkID) { |
| 98 | + throw new Error('Network ID mismatch'); |
| 99 | + } |
| 100 | + if (!unsigned.sourceBlockchainId.equals(this.transaction._blockchainID)) { |
| 101 | + throw new Error('Blockchain ID mismatch'); |
| 102 | + } |
| 103 | + if (unsigned.outputs.length !== 1) { |
| 104 | + throw new BuildTransactionError('Transaction can have one output'); |
| 105 | + } |
| 106 | + if (unsigned.inputs.length !== 1) { |
| 107 | + throw new BuildTransactionError('Transaction can have one input'); |
| 108 | + } |
| 109 | + const out = unsigned.outputs[0]; |
| 110 | + const inp = unsigned.inputs[0]; |
| 111 | + if (!out.assetId.equals(this.transaction._assetId) || !inp.assetId.equals(this.transaction._assetId)) { |
| 112 | + throw new Error('AssetID mismatch'); |
| 113 | + } |
| 114 | + this.transaction._to = out.addresses; |
| 115 | + this._amount = out.amount; |
| 116 | + const fee = inp.amount - out.amount; |
| 117 | + if (fee < 0n) { |
| 118 | + throw new BuildTransactionError('Computed fee is negative'); |
| 119 | + } |
| 120 | + const fixed = this.fixedFee; |
| 121 | + const feeRate = fee - fixed; |
| 122 | + this.transaction._fee.feeRate = feeRate.toString(); |
| 123 | + this.transaction._fee.fee = fee.toString(); |
| 124 | + this.transaction._fee.size = 1; |
| 125 | + this.transaction._fromAddresses = [inp.address]; |
| 126 | + this._nonce = inp.nonce; |
| 127 | + this.transaction.setTransaction(raw); |
| 128 | + return this; |
| 129 | + } |
| 130 | + |
| 131 | + // For parity with Avalanche builder interfaces; always returns true for placeholder |
| 132 | + //TODO: WIN-6322 |
| 133 | + static verifyTxType(_tx: unknown): _tx is FlareUnsignedExportTx { |
| 134 | + return true; |
| 135 | + } |
| 136 | + |
| 137 | + verifyTxType(_tx: unknown): _tx is FlareUnsignedExportTx { |
| 138 | + return ExportInCTxBuilder.verifyTxType(_tx); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Build the export in C-chain transaction |
| 143 | + * @protected |
| 144 | + */ |
| 145 | + protected buildFlareTransaction(): void { |
| 146 | + if (this.transaction.hasCredentials) return; // placeholder: credentials not yet implemented |
| 147 | + if (this._amount === undefined) throw new Error('amount is required'); |
| 148 | + if (this.transaction._fromAddresses.length !== 1) throw new Error('sender is one and required'); |
| 149 | + if (this.transaction._to.length === 0) throw new Error('to is required'); |
| 150 | + if (!this.transaction._fee.feeRate) throw new Error('fee rate is required'); |
| 151 | + if (this._nonce === undefined) throw new Error('nonce is required'); |
| 152 | + |
| 153 | + // Compose placeholder unsigned tx shape |
| 154 | + const feeRate = BigInt(this.transaction._fee.feeRate); |
| 155 | + const fixed = this.fixedFee; |
| 156 | + const totalFee = feeRate + fixed; |
| 157 | + const input: FlareExportInputShape = { |
| 158 | + address: this.transaction._fromAddresses[0], |
| 159 | + amount: this._amount + totalFee, |
| 160 | + assetId: this.transaction._assetId, |
| 161 | + nonce: this._nonce, |
| 162 | + }; |
| 163 | + const output: FlareExportOutputShape = { |
| 164 | + addresses: this.transaction._to, |
| 165 | + amount: this._amount, |
| 166 | + assetId: this.transaction._assetId, |
| 167 | + }; |
| 168 | + const unsigned: FlareUnsignedExportTx = { |
| 169 | + networkId: this.transaction._networkID, |
| 170 | + sourceBlockchainId: this.transaction._blockchainID, |
| 171 | + destinationBlockchainId: this._externalChainId || Buffer.alloc(0), |
| 172 | + inputs: [input], |
| 173 | + outputs: [output], |
| 174 | + }; |
| 175 | + const signed: FlareSignedExportTx = { unsignedTx: unsigned, credentials: [] }; |
| 176 | + this.transaction._fee.fee = totalFee.toString(); |
| 177 | + this.transaction._fee.size = 1; |
| 178 | + this.transaction.setTransaction(signed); |
| 179 | + } |
| 180 | + |
| 181 | + /** @inheritdoc */ |
| 182 | + protected fromImplementation(raw: string | RawFlareExportTx): { _tx?: unknown } { |
| 183 | + if (typeof raw === 'string') { |
| 184 | + // Future: parse hex or serialized form. For now treat as opaque raw tx. |
| 185 | + this.transaction.setTransaction(raw); |
| 186 | + return this.transaction; |
| 187 | + } |
| 188 | + return this.initBuilder(raw).transaction; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Check the amount is positive. |
| 193 | + * @param amount |
| 194 | + */ |
| 195 | + validateNonce(nonce: bigint): void { |
| 196 | + if (nonce < 0n) { |
| 197 | + throw new BuildTransactionError('Nonce must be greater or equal than 0'); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments