-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathtransferBuilder.ts
More file actions
199 lines (183 loc) · 5.73 KB
/
transferBuilder.ts
File metadata and controls
199 lines (183 loc) · 5.73 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { InvalidTransactionError, PublicKey, TransactionType } from '@bitgo/sdk-core';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import { TransactionBuilder } from './transactionBuilder';
import { Transaction } from './transaction/transaction';
import { CantonPrepareCommandResponse, CantonTransferRequest } from './iface';
import utils from './utils';
export class TransferBuilder extends TransactionBuilder {
private _commandId: string;
private _senderId: string;
private _receiverId: string;
private _amount: number;
private _sendOneStep = false;
private _expiryEpoch: number;
private _memoId: string;
private _tokenName: string;
constructor(_coinConfig: Readonly<CoinConfig>) {
super(_coinConfig);
}
initBuilder(tx: Transaction): void {
super.initBuilder(tx);
this.setTransactionType();
}
get transactionType(): TransactionType {
return TransactionType.Send;
}
setTransactionType(): void {
this.transaction.transactionType = TransactionType.Send;
}
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
* 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 sender party id for the transfer
* @param id - sender address (party id)
* @returns The current builder instance for chaining.
* @throws Error if id is empty.
*/
senderId(id: string): this {
if (!id || !id.trim()) {
throw new Error('senderId must be a non-empty string');
}
this._senderId = id.trim();
return this;
}
/**
* Sets the receiver party id for the transfer
* @param id - receiver address (party id)
* @returns The current builder instance for chaining.
* @throws Error if id is empty.
*/
receiverId(id: string): this {
if (!id || !id.trim()) {
throw new Error('receiverId must be a non-empty string');
}
this._receiverId = id.trim();
return this;
}
/**
* Sets the transfer amount
* @param amount - transfer amount
* @returns The current builder instance for chaining.
* @throws Error if amount not present or negative
*/
amount(amount: number): this {
if (!amount || amount < 0) {
throw new Error('amount must be a positive number');
}
this._amount = amount;
return this;
}
/**
* Sets the 1-step enablement flag to send via 1-step, works only if recipient
* enabled the 1-step, defaults to `false`
* @param flag boolean value
* @returns The current builder for chaining
*/
sendOneStep(flag: boolean): this {
this._sendOneStep = flag;
return this;
}
/**
* Sets the transfer expiry
* @param epoch - the expiry for 2-step transfer, defaults to 90 days and
* not applicable if sending via 1-step
* @returns The current builder for chaining
* @throws Error if the expiry value is invalid
*/
expiryEpoch(epoch: number): this {
if (!epoch || epoch < 0) {
throw new Error('epoch must be a positive number');
}
this._expiryEpoch = epoch;
return this;
}
/**
* Sets the optional memoId if present
* @param id - memoId of the recipient
* @returns The current builder for chaining
* @throws Error if the memoId value is invalid
*/
memoId(id: string): this {
if (!id || !id.trim()) {
throw new Error('memoId must be a non-empty string');
}
this._memoId = id.trim();
return this;
}
/**
* Sets the optional token field if present, used for canton token transaction
* @param name - the bitgo name of the token
* @returns The current builder for chaining
* @throws Error if name is invalid
*/
tokenName(name: string): this {
if (!name || !name.trim()) {
throw new Error('token name must be a non-empty string');
}
this._tokenName = name.trim();
return this;
}
/**
* Get the canton transfer request object
* @returns CantonTransferRequest
* @throws Error if any required params are missing
*/
toRequestObject(): CantonTransferRequest {
this.validate();
const data: CantonTransferRequest = {
commandId: this._commandId,
senderPartyId: this._senderId,
receiverPartyId: this._receiverId,
amount: this._amount,
expiryEpoch: this._expiryEpoch,
sendViaOneStep: this._sendOneStep,
};
if (this._memoId) {
data.memoId = this._memoId;
}
if (this._tokenName) {
data.tokenName = this._tokenName;
}
return data;
}
/**
* Method to validate the required fields
* @throws Error if required fields are not set
* @private
*/
private validate(): void {
if (!this._commandId) throw new Error('commandId is missing');
if (!this._senderId) throw new Error('senderId is missing');
if (!this._receiverId) throw new Error('receiverId is missing');
if (!this._amount) throw new Error('amount is missing');
if (!this._expiryEpoch) throw new Error('expiryEpoch is missing');
}
}