-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathtransferOfferWithdrawnBuilder.ts
More file actions
139 lines (126 loc) · 4.3 KB
/
transferOfferWithdrawnBuilder.ts
File metadata and controls
139 lines (126 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { InvalidTransactionError, PublicKey, TransactionType } from '@bitgo/sdk-core';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { CantonPrepareCommandResponse, CantonTransferOfferWithdrawnRequest } from './iface';
import { TransactionBuilder } from './transactionBuilder';
import { Transaction } from './transaction/transaction';
import utils from './utils';
export class TransferOfferWithdrawnBuilder extends TransactionBuilder {
private _commandId: string;
private _contractId: string;
private _actAsPartyId: string;
private _tokenName: string;
constructor(_coinConfig: Readonly<CoinConfig>) {
super(_coinConfig);
}
initBuilder(tx: Transaction): void {
super.initBuilder(tx);
this.setTransactionType();
}
get transactionType(): TransactionType {
return TransactionType.TransferOfferWithdrawn;
}
setTransactionType(): void {
this.transaction.transactionType = TransactionType.TransferOfferWithdrawn;
}
setTransaction(transaction: CantonPrepareCommandResponse): void {
this.transaction.prepareCommand = transaction;
}
/** @inheritDoc */
addSignature(publicKey: PublicKey, signature: Buffer): void {
if (!this.transaction) {
throw new InvalidTransactionError('transaction is empty!');
}
this._signatures.push({ publicKey, signature });
const pubKeyBase64 = utils.getBase64FromHex(publicKey.pub);
this.transaction.signerFingerprint = utils.getAddressFromPublicKey(pubKeyBase64);
this.transaction.signatures = signature.toString('base64');
}
/**
* Sets the unique id for the transfer offer withdrawn
* Also sets the _id of the transaction
*
* @param id - A uuid
* @returns The current builder instance for chaining.
* @throws Error if id is empty.
*/
commandId(id: string): this {
if (!id || !id.trim()) {
throw new Error('commandId must be a non-empty string');
}
this._commandId = id.trim();
// also set the transaction _id
this.transaction.id = id.trim();
return this;
}
/**
* Sets the contract id the receiver needs to withdraw
* @param id - canton withdrawn contract id
* @returns The current builder instance for chaining.
* @throws Error if id is empty.
*/
contractId(id: string): this {
if (!id || !id.trim()) {
throw new Error('contractId must be a non-empty string');
}
this._contractId = id.trim();
return this;
}
/**
* The sender who wants to withdraw the offer
*
* @param id - the sender party id
* @returns The current builder instance for chaining.
* @throws Error if id is empty.
*/
actAs(id: string): this {
if (!id || !id.trim()) {
throw new Error('actAsPartyId must be a non-empty string');
}
this._actAsPartyId = id.trim();
return this;
}
/**
* The token name to withdraw the offer
* @param name - the bitgo name of the asset
* @returns The current builder instance for chaining.
* @throws Error if name is empty.
*/
tokenName(name: string): this {
if (!name || !name.trim()) {
throw new Error('tokenName must be a non-empty string');
}
this._tokenName = name.trim();
return this;
}
/**
* Builds and returns the CantonTransferOfferWithdrawnRequest object from the builder's internal state.
*
* This method performs validation before constructing the object. If required fields are
* missing or invalid, it throws an error.
*
* @returns {CantonTransferOfferWithdrawnRequest} - A fully constructed and validated request object for transfer offer withdrawal.
* @throws {Error} If any required field is missing or fails validation.
*/
toRequestObject(): CantonTransferOfferWithdrawnRequest {
this.validate();
return {
commandId: this._commandId,
contractId: this._contractId,
verboseHashing: false,
actAs: [this._actAsPartyId],
readAs: [],
tokenName: this._tokenName,
};
}
/**
* Validates the internal state of the builder before building the request object.
*
* @private
* @throws {Error} If any required field is missing or invalid.
*/
private validate(): void {
if (!this._commandId) throw new Error('commandId is missing');
if (!this._contractId) throw new Error('contractId is missing');
if (!this._actAsPartyId) throw new Error('receiver partyId is missing');
}
}