|
| 1 | +import { InvalidTransactionError, PublicKey, TransactionType } from '@bitgo/sdk-core'; |
| 2 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 3 | +import { CantonPrepareCommandResponse, CantonTransferAcceptRejectRequest } from './iface'; |
| 4 | +import { TransactionBuilder } from './transactionBuilder'; |
| 5 | +import { Transaction } from './transaction/transaction'; |
| 6 | +import utils from './utils'; |
| 7 | + |
| 8 | +export class CosignDelegationAcceptBuilder extends TransactionBuilder { |
| 9 | + private _commandId: string; |
| 10 | + private _contractId: string; |
| 11 | + private _actAsPartyId: string; |
| 12 | + |
| 13 | + constructor(_coinConfig: Readonly<CoinConfig>) { |
| 14 | + super(_coinConfig); |
| 15 | + } |
| 16 | + |
| 17 | + initBuilder(tx: Transaction): void { |
| 18 | + super.initBuilder(tx); |
| 19 | + this.setTransactionType(); |
| 20 | + } |
| 21 | + |
| 22 | + get transactionType(): TransactionType { |
| 23 | + return TransactionType.CosignDelegationAccept; |
| 24 | + } |
| 25 | + |
| 26 | + setTransactionType(): void { |
| 27 | + this.transaction.transactionType = TransactionType.CosignDelegationAccept; |
| 28 | + } |
| 29 | + |
| 30 | + setTransaction(transaction: CantonPrepareCommandResponse): void { |
| 31 | + this.transaction.prepareCommand = transaction; |
| 32 | + } |
| 33 | + |
| 34 | + /** @inheritDoc */ |
| 35 | + addSignature(publicKey: PublicKey, signature: Buffer): void { |
| 36 | + if (!this.transaction) { |
| 37 | + throw new InvalidTransactionError('transaction is empty!'); |
| 38 | + } |
| 39 | + this._signatures.push({ publicKey, signature }); |
| 40 | + const pubKeyBase64 = utils.getBase64FromHex(publicKey.pub); |
| 41 | + this.transaction.signerFingerprint = utils.getAddressFromPublicKey(pubKeyBase64); |
| 42 | + this.transaction.signatures = signature.toString('base64'); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Sets the unique command id for the cosign delegation accept |
| 47 | + * Also sets the _id of the transaction |
| 48 | + * |
| 49 | + * @param id - A uuid |
| 50 | + * @returns The current builder instance for chaining. |
| 51 | + * @throws Error if id is empty. |
| 52 | + */ |
| 53 | + commandId(id: string): this { |
| 54 | + if (!id || !id.trim()) { |
| 55 | + throw new Error('commandId must be a non-empty string'); |
| 56 | + } |
| 57 | + this._commandId = id.trim(); |
| 58 | + this.transaction.id = id.trim(); |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Sets the contract id of the delegation proposal to accept |
| 64 | + * @param id - canton contract id |
| 65 | + * @returns The current builder instance for chaining. |
| 66 | + * @throws Error if id is empty. |
| 67 | + */ |
| 68 | + contractId(id: string): this { |
| 69 | + if (!id || !id.trim()) { |
| 70 | + throw new Error('contractId must be a non-empty string'); |
| 71 | + } |
| 72 | + this._contractId = id.trim(); |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Sets the party acting as the acceptor |
| 78 | + * |
| 79 | + * @param id - the actor party id (address) |
| 80 | + * @returns The current builder instance for chaining. |
| 81 | + * @throws Error if id is empty. |
| 82 | + */ |
| 83 | + actAs(id: string): this { |
| 84 | + if (!id || !id.trim()) { |
| 85 | + throw new Error('actAsPartyId must be a non-empty string'); |
| 86 | + } |
| 87 | + this._actAsPartyId = id.trim(); |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Builds and returns the CantonTransferAcceptRejectRequest object from the builder's internal state. |
| 93 | + * |
| 94 | + * @returns {CantonTransferAcceptRejectRequest} - A fully constructed and validated request object. |
| 95 | + * @throws {Error} If any required field is missing or fails validation. |
| 96 | + */ |
| 97 | + toRequestObject(): CantonTransferAcceptRejectRequest { |
| 98 | + this.validate(); |
| 99 | + |
| 100 | + return { |
| 101 | + commandId: this._commandId, |
| 102 | + contractId: this._contractId, |
| 103 | + verboseHashing: false, |
| 104 | + actAs: [this._actAsPartyId], |
| 105 | + readAs: [], |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Validates the internal state of the builder before building the request object. |
| 111 | + * |
| 112 | + * @private |
| 113 | + * @throws {Error} If any required field is missing or invalid. |
| 114 | + */ |
| 115 | + private validate(): void { |
| 116 | + if (!this._commandId) throw new Error('commandId is missing'); |
| 117 | + if (!this._contractId) throw new Error('contractId is missing'); |
| 118 | + if (!this._actAsPartyId) throw new Error('actAs partyId is missing'); |
| 119 | + } |
| 120 | +} |
0 commit comments