|
| 1 | +import { PublicKey, TransactionType } from '@bitgo/sdk-core'; |
| 2 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 3 | +import { CantonPrepareCommandResponse, EndInvestorOnboardingOfferData } from './iface'; |
| 4 | +import { TransactionBuilder } from './transactionBuilder'; |
| 5 | +import { Transaction } from './transaction/transaction'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Builder for an EndInvestorOnboardingOffer txRequest — an internal, non-signable transaction |
| 9 | + * that records an on-chain EndInvestorOnboardingOffer contract on the end investor's wallet |
| 10 | + * so they can accept or reject. Built locally without an IMS call because the inviting |
| 11 | + * participant may be external (non-BitGo) and therefore not registered in BitGo's IMS. |
| 12 | + * |
| 13 | + * setTransaction and addSignature are intentionally not implemented because this |
| 14 | + * transaction type is never signed or broadcast directly. |
| 15 | + */ |
| 16 | +export class EndInvestorOnboardingOfferBuilder extends TransactionBuilder { |
| 17 | + private _contractId: string; |
| 18 | + private _endInvestorPartyId: string; |
| 19 | + private _participantPartyId: string; |
| 20 | + private _comment?: string; |
| 21 | + |
| 22 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 23 | + super(_coinConfig); |
| 24 | + } |
| 25 | + |
| 26 | + initBuilder(tx: Transaction): void { |
| 27 | + super.initBuilder(tx); |
| 28 | + this.setTransactionType(); |
| 29 | + } |
| 30 | + |
| 31 | + get transactionType(): TransactionType { |
| 32 | + return TransactionType.EndInvestorOnboardingOffer; |
| 33 | + } |
| 34 | + |
| 35 | + setTransactionType(): void { |
| 36 | + this.transaction.transactionType = TransactionType.EndInvestorOnboardingOffer; |
| 37 | + } |
| 38 | + |
| 39 | + setTransaction(_transaction: CantonPrepareCommandResponse): void { |
| 40 | + throw new Error('Not implemented!'); |
| 41 | + } |
| 42 | + |
| 43 | + /** @inheritDoc */ |
| 44 | + addSignature(_publicKey: PublicKey, _signature: Buffer): void { |
| 45 | + throw new Error('Not implemented!'); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Sets the on-chain contractId of the EndInvestorOnboardingOffer. |
| 50 | + * Also sets the transaction id. |
| 51 | + * @param id - contract id (from pendingContractId) |
| 52 | + */ |
| 53 | + contractId(id: string): this { |
| 54 | + if (!id || !id.trim()) { |
| 55 | + throw new Error('contractId must be a non-empty string'); |
| 56 | + } |
| 57 | + this._contractId = id.trim(); |
| 58 | + this.transaction.id = id.trim(); |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Sets the Canton party ID of the end investor being onboarded. |
| 64 | + * @param id - end investor party id |
| 65 | + */ |
| 66 | + endInvestorPartyId(id: string): this { |
| 67 | + if (!id || !id.trim()) { |
| 68 | + throw new Error('endInvestorPartyId must be a non-empty string'); |
| 69 | + } |
| 70 | + this._endInvestorPartyId = id.trim(); |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Sets the Canton party ID of the participant that created the offer. |
| 76 | + * @param id - participant party id (may be external to BitGo) |
| 77 | + */ |
| 78 | + participantPartyId(id: string): this { |
| 79 | + if (!id || !id.trim()) { |
| 80 | + throw new Error('participantPartyId must be a non-empty string'); |
| 81 | + } |
| 82 | + this._participantPartyId = id.trim(); |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Sets an optional free-form comment from the participant. |
| 88 | + * @param comment - comment string |
| 89 | + */ |
| 90 | + comment(comment: string): this { |
| 91 | + this._comment = comment; |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Builds and returns the EndInvestorOnboardingOfferData object from the builder's internal state. |
| 97 | + * |
| 98 | + * @returns {EndInvestorOnboardingOfferData} - A fully constructed and validated data object. |
| 99 | + * @throws {Error} If any required field is missing. |
| 100 | + */ |
| 101 | + toRequestObject(): EndInvestorOnboardingOfferData { |
| 102 | + this.validate(); |
| 103 | + const result: EndInvestorOnboardingOfferData = { |
| 104 | + contractId: this._contractId, |
| 105 | + endInvestorPartyId: this._endInvestorPartyId, |
| 106 | + participantPartyId: this._participantPartyId, |
| 107 | + }; |
| 108 | + if (this._comment !== undefined) { |
| 109 | + result.comment = this._comment; |
| 110 | + } |
| 111 | + return result; |
| 112 | + } |
| 113 | + |
| 114 | + protected async buildImplementation(): Promise<Transaction> { |
| 115 | + this.validate(); |
| 116 | + this.transaction.endInvestorOnboardingOfferData = this.toRequestObject(); |
| 117 | + return this.transaction; |
| 118 | + } |
| 119 | + |
| 120 | + private validate(): void { |
| 121 | + if (!this._contractId) throw new Error('contractId is missing'); |
| 122 | + if (!this._endInvestorPartyId) throw new Error('endInvestorPartyId is missing'); |
| 123 | + if (!this._participantPartyId) throw new Error('participantPartyId is missing'); |
| 124 | + } |
| 125 | +} |
0 commit comments