|
1 | 1 | import * as Crypto from '@cardano-sdk/crypto';
|
2 |
| -import { BlockfrostClient, BlockfrostProvider } from '../blockfrost'; |
| 2 | +import { BlockfrostClient, BlockfrostProvider, fetchSequentially } from '../blockfrost'; |
| 3 | +import { Cardano } from '@cardano-sdk/core'; |
| 4 | +import { KeyPurpose } from '@cardano-sdk/key-management'; |
3 | 5 | import { Logger } from 'ts-log';
|
4 |
| -import { MULTI_SIG_LABEL, MultiSigTransaction, SharedWalletProvider } from '@cardano-sdk/core'; |
| 6 | +import { MultiSigRegistration, MultiSigTransaction, SharedWalletProvider } from './types'; |
| 7 | +import type { Responses } from '@blockfrost/blockfrost-js'; |
| 8 | + |
| 9 | +const MULTI_SIG_LABEL = KeyPurpose.MULTI_SIG; |
| 10 | + |
| 11 | +const isMultiSigRegistration = (metadata: unknown): metadata is MultiSigRegistration => |
| 12 | + !!metadata && typeof metadata === 'object' && 'participants' in metadata; |
5 | 13 |
|
6 | 14 | export class BlockfrostSharedWalletProvider extends BlockfrostProvider implements SharedWalletProvider {
|
7 | 15 | constructor(client: BlockfrostClient, logger: Logger) {
|
8 | 16 | super(client, logger);
|
9 | 17 | }
|
10 | 18 |
|
11 | 19 | async discoverWallets(pubKey: Crypto.Ed25519KeyHashHex): Promise<MultiSigTransaction[]> {
|
12 |
| - let page = 1; |
13 |
| - const pageSize = 100; |
14 |
| - let allTransactions: MultiSigTransaction[] = []; |
15 |
| - |
16 |
| - // eslint-disable-next-line no-constant-condition |
17 |
| - while (true) { |
18 |
| - const response = await this.request<MultiSigTransaction[]>(`metadata/txs/labels/${MULTI_SIG_LABEL}?page=${page}`); |
19 |
| - allTransactions = [...allTransactions, ...response]; |
20 |
| - if (response.length < pageSize) break; |
21 |
| - page++; |
22 |
| - } |
| 20 | + const batchSize = 100; |
23 | 21 |
|
24 |
| - return allTransactions.filter((wallet) => wallet.json_metadata.participants?.[pubKey]); |
| 22 | + return fetchSequentially<Responses['tx_metadata_label_json'][0], MultiSigTransaction>( |
| 23 | + { |
| 24 | + haveEnoughItems: (wallets, _) => wallets.length < batchSize, |
| 25 | + paginationOptions: { count: batchSize }, |
| 26 | + request: (paginationQueryString) => |
| 27 | + this.request<Responses['tx_metadata_label_json']>( |
| 28 | + `metadata/txs/labels/${MULTI_SIG_LABEL}?${paginationQueryString}` |
| 29 | + ), |
| 30 | + responseTranslator: (wallets) => |
| 31 | + wallets |
| 32 | + .filter((wallet) => { |
| 33 | + const metadata = wallet.json_metadata; |
| 34 | + return isMultiSigRegistration(metadata) && metadata?.participants?.[pubKey]; |
| 35 | + }) |
| 36 | + .map((wallet) => ({ |
| 37 | + metadata: wallet.json_metadata as unknown as MultiSigRegistration, |
| 38 | + txId: Cardano.TransactionId(wallet.tx_hash) |
| 39 | + })) |
| 40 | + }, |
| 41 | + [] |
| 42 | + ); |
25 | 43 | }
|
26 | 44 | }
|
0 commit comments