Skip to content

Commit 916da00

Browse files
committed
Revamp signTx function
1 parent 255d179 commit 916da00

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

src/client/LumClient.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -267,41 +267,49 @@ export class LumClient {
267267
/**
268268
* Signs the messages using the provided wallet and builds the transaction
269269
*
270-
* @param wallet signing wallet or wallets for multi signature
270+
* @param wallets signing wallets for multi signature
271271
* @param doc document to sign
272272
*/
273-
signTx = async (wallet: LumWallet | LumWallet[], doc: LumTypes.Doc): Promise<Uint8Array> => {
274-
let wallets: LumWallet[];
275-
if (Array.isArray(wallet)) {
276-
wallets = wallet;
277-
} else {
278-
wallets = [wallet];
279-
}
273+
signTx = async <T>(wallets: T, doc: LumTypes.Doc): Promise<Uint8Array> => {
274+
let signDoc: LumTypes.SignDoc | undefined = undefined;
275+
const signatures: Uint8Array[] = [];
276+
277+
if (wallets instanceof LumWallet) {
278+
const [walletSignedDoc, signature] = await this.signTxFromWallet(wallets, doc);
280279

281-
if (wallets.length < 1) {
282-
throw new Error('At least one wallet is required to sign the transaction');
280+
signatures.push(signature);
281+
signDoc = walletSignedDoc;
283282
}
284283

285-
let signDoc: LumTypes.SignDoc | undefined = undefined;
286-
const signatures: Uint8Array[] = [];
284+
if (wallets instanceof Array) {
285+
for (const wallet of wallets) {
286+
const [walletSignedDoc, signature] = await this.signTxFromWallet(wallet, doc);
287287

288-
for (let i = 0; i < wallets.length; i++) {
289-
const account = await this.getAccount(wallets[i].getAddress());
290-
if (!account) {
291-
throw new Error(`Account not found for wallet at index ${i}`);
292-
}
293-
const [walletSignedDoc, signature] = await wallets[i].signTransaction(doc);
294-
if (i === 0) {
295-
signDoc = walletSignedDoc;
288+
signatures.push(signature);
289+
290+
if (!signDoc) {
291+
signDoc = walletSignedDoc;
292+
}
296293
}
297-
signatures.push(signature);
298294
}
299-
if (!signDoc) {
300-
throw new Error('Impossible error to avoid typescript warnings');
295+
296+
if (!signDoc || signatures.length === 0) {
297+
throw new Error('No wallet provided');
301298
}
299+
302300
return LumUtils.generateTxBytes(signDoc, signatures);
303301
};
304302

303+
signTxFromWallet = async (wallet: LumWallet, doc: LumTypes.Doc) => {
304+
const account = await this.getAccount(wallet.getAddress());
305+
306+
if (!account) {
307+
throw new Error(`Account not found for wallet ${wallet.getAddress()}`);
308+
}
309+
310+
return wallet.signTransaction(doc);
311+
};
312+
305313
/**
306314
* Broadcast a signed transaction
307315
* Basic usage would be to use the signTx method prior to calling this method

0 commit comments

Comments
 (0)