|
1 | | -import { PublicKey, TransactionType } from '@bitgo/sdk-core'; |
| 1 | +import { InvalidTransactionError, PublicKey, TransactionType } from '@bitgo/sdk-core'; |
2 | 2 | import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
3 | 3 | import { TransactionBuilder } from './transactionBuilder'; |
4 | | -import { CantonPrepareCommandResponse } from './iface'; |
| 4 | +import { Transaction } from './transaction/transaction'; |
| 5 | +import { CantonPrepareCommandResponse, CantonTransferRequest } from './iface'; |
| 6 | +import utils from './utils'; |
5 | 7 |
|
6 | 8 | export class TransferBuilder extends TransactionBuilder { |
| 9 | + private _commandId: string; |
| 10 | + private _senderId: string; |
| 11 | + private _receiverId: string; |
| 12 | + private _amount: number; |
| 13 | + private _sendOneStep = false; |
| 14 | + private _expiryEpoch: number; |
| 15 | + private _memoId: string; |
7 | 16 | constructor(_coinConfig: Readonly<CoinConfig>) { |
8 | 17 | super(_coinConfig); |
9 | 18 | } |
10 | 19 |
|
11 | | - protected get transactionType(): TransactionType { |
| 20 | + initBuilder(tx: Transaction): void { |
| 21 | + super.initBuilder(tx); |
| 22 | + this.setTransactionType(); |
| 23 | + } |
| 24 | + |
| 25 | + get transactionType(): TransactionType { |
12 | 26 | return TransactionType.Send; |
13 | 27 | } |
14 | 28 |
|
| 29 | + setTransactionType(): void { |
| 30 | + this.transaction.transactionType = TransactionType.Send; |
| 31 | + } |
| 32 | + |
15 | 33 | setTransaction(transaction: CantonPrepareCommandResponse): void { |
16 | 34 | this.transaction.prepareCommand = transaction; |
17 | 35 | } |
18 | 36 |
|
19 | 37 | /** @inheritDoc */ |
20 | 38 | addSignature(publicKey: PublicKey, signature: Buffer): void { |
21 | | - throw new Error('Not implemented'); |
| 39 | + if (!this.transaction) { |
| 40 | + throw new InvalidTransactionError('transaction is empty!'); |
| 41 | + } |
| 42 | + this._signatures.push({ publicKey, signature }); |
| 43 | + const pubKeyBase64 = utils.getBase64FromHex(publicKey.pub); |
| 44 | + this.transaction.signerFingerprint = utils.getAddressFromPublicKey(pubKeyBase64); |
| 45 | + this.transaction.signatures = signature.toString('base64'); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Sets the unique id for the transfer |
| 50 | + * Also sets the _id of the transaction |
| 51 | + * |
| 52 | + * @param id - A uuid |
| 53 | + * @returns The current builder instance for chaining. |
| 54 | + * @throws Error if id is empty. |
| 55 | + */ |
| 56 | + commandId(id: string): this { |
| 57 | + if (!id || !id.trim()) { |
| 58 | + throw new Error('commandId must be a non-empty string'); |
| 59 | + } |
| 60 | + this._commandId = id.trim(); |
| 61 | + // also set the transaction _id |
| 62 | + this.transaction.id = id.trim(); |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Sets the sender party id for the transfer |
| 68 | + * @param id - sender address (party id) |
| 69 | + * @returns The current builder instance for chaining. |
| 70 | + * @throws Error if id is empty. |
| 71 | + */ |
| 72 | + senderId(id: string): this { |
| 73 | + if (!id || !id.trim()) { |
| 74 | + throw new Error('senderId must be a non-empty string'); |
| 75 | + } |
| 76 | + this._senderId = id.trim(); |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Sets the receiver party id for the transfer |
| 82 | + * @param id - receiver address (party id) |
| 83 | + * @returns The current builder instance for chaining. |
| 84 | + * @throws Error if id is empty. |
| 85 | + */ |
| 86 | + receiverId(id: string): this { |
| 87 | + if (!id || !id.trim()) { |
| 88 | + throw new Error('receiverId must be a non-empty string'); |
| 89 | + } |
| 90 | + this._receiverId = id.trim(); |
| 91 | + return this; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Sets the transfer amount |
| 96 | + * @param amount - transfer amount |
| 97 | + * @returns The current builder instance for chaining. |
| 98 | + * @throws Error if amount not present or negative |
| 99 | + */ |
| 100 | + amount(amount: number): this { |
| 101 | + if (!amount || amount < 0) { |
| 102 | + throw new Error('amount must be a positive number'); |
| 103 | + } |
| 104 | + this._amount = amount; |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Sets the 1-step enablement flag to send via 1-step, works only if recipient |
| 110 | + * enabled the 1-step, defaults to `false` |
| 111 | + * @param flag boolean value |
| 112 | + * @returns The current builder for chaining |
| 113 | + */ |
| 114 | + sendOneStep(flag: boolean): this { |
| 115 | + this._sendOneStep = flag; |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Sets the transfer expiry |
| 121 | + * @param epoch - the expiry for 2-step transfer, defaults to 90 days and |
| 122 | + * not applicable if sending via 1-step |
| 123 | + * @returns The current builder for chaining |
| 124 | + * @throws Error if the expiry value is invalid |
| 125 | + */ |
| 126 | + expiryEpoch(epoch: number): this { |
| 127 | + if (!epoch || epoch < 0) { |
| 128 | + throw new Error('epoch must be a positive number'); |
| 129 | + } |
| 130 | + this._expiryEpoch = epoch; |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Sets the optional memoId if present |
| 136 | + * @param id - memoId of the recipient |
| 137 | + * @returns The current builder for chaining |
| 138 | + * @throws Error if the memoId value is invalid |
| 139 | + */ |
| 140 | + memoId(id: string): this { |
| 141 | + if (!id || !id.trim()) { |
| 142 | + throw new Error('memoId must be a non-empty string'); |
| 143 | + } |
| 144 | + this._memoId = id.trim(); |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Get the canton transfer request object |
| 150 | + * @returns CantonTransferRequest |
| 151 | + * @throws Error if any required params are missing |
| 152 | + */ |
| 153 | + toRequestObject(): CantonTransferRequest { |
| 154 | + this.validate(); |
| 155 | + const data: CantonTransferRequest = { |
| 156 | + commandId: this._commandId, |
| 157 | + senderPartyId: this._senderId, |
| 158 | + receiverPartyId: this._receiverId, |
| 159 | + amount: this._amount, |
| 160 | + expiryEpoch: this._expiryEpoch, |
| 161 | + sendViaOneStep: this._sendOneStep, |
| 162 | + }; |
| 163 | + if (this._memoId) { |
| 164 | + data.memoId = this._memoId; |
| 165 | + } |
| 166 | + return data; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Method to validate the required fields |
| 171 | + * @throws Error if required fields are not set |
| 172 | + * @private |
| 173 | + */ |
| 174 | + private validate(): void { |
| 175 | + if (!this._commandId) throw new Error('commandId is missing'); |
| 176 | + if (!this._senderId) throw new Error('senderId is missing'); |
| 177 | + if (!this._receiverId) throw new Error('receiverId is missing'); |
| 178 | + if (!this._amount) throw new Error('amount is missing'); |
| 179 | + if (!this._expiryEpoch) throw new Error('expiryEpoch is missing'); |
22 | 180 | } |
23 | 181 | } |
0 commit comments