Skip to content

Commit cfcbae8

Browse files
committed
fixup! feat: discover shared wallet by metadata label
1 parent e4b9be5 commit cfcbae8

File tree

7 files changed

+43
-24
lines changed

7 files changed

+43
-24
lines changed

packages/cardano-services-client/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"dependencies": {
6060
"@cardano-sdk/core": "workspace:~",
6161
"@cardano-sdk/crypto": "workspace:~",
62+
"@cardano-sdk/key-management": "workspace:~",
6263
"@cardano-sdk/util": "workspace:~",
6364
"axios": "^1.7.4",
6465
"class-validator": "^0.14.0",
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,44 @@
11
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';
35
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;
513

614
export class BlockfrostSharedWalletProvider extends BlockfrostProvider implements SharedWalletProvider {
715
constructor(client: BlockfrostClient, logger: Logger) {
816
super(client, logger);
917
}
1018

1119
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;
2321

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+
);
2543
}
2644
}

packages/core/src/Provider/SharedWalletProvider/types.ts packages/cardano-services-client/src/SharedWalletProvider/types.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { Cardano } from '@cardano-sdk/core';
12
import { Ed25519KeyHashHex } from '@cardano-sdk/crypto';
3+
import { KeyRole } from '@cardano-sdk/key-management';
24

3-
type ScriptType = number;
4-
5-
export const MULTI_SIG_LABEL = 1854;
5+
type ScriptType = KeyRole;
66

77
interface MultiSigParticipant {
88
name: string;
@@ -11,10 +11,10 @@ interface MultiSigParticipant {
1111
}
1212

1313
interface MultiSigParticipants {
14-
[key: string]: MultiSigParticipant;
14+
[key: Ed25519KeyHashHex]: MultiSigParticipant;
1515
}
1616

17-
interface MultiSigRegistration {
17+
export interface MultiSigRegistration {
1818
types: ScriptType[];
1919
name?: string;
2020
description?: string;
@@ -23,8 +23,9 @@ interface MultiSigRegistration {
2323
}
2424

2525
export interface MultiSigTransaction {
26-
tx_hash: string;
27-
json_metadata: MultiSigRegistration;
26+
txId: Cardano.TransactionId;
27+
metadata: MultiSigRegistration;
28+
nativeScripts?: Cardano.NativeScript[];
2829
}
2930

3031
export interface SharedWalletProvider {

packages/cardano-services-client/test/SharedWalletProvider/BlockfrostSharedWallet.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe('BlockfrostSharedWallet', () => {
156156
const wallets = await provider.discoverWallets(Crypto.Ed25519KeyHashHex(pubKey));
157157
expect(wallets.length).toEqual(3);
158158
for (const wallet of wallets) {
159-
expect(wallet.json_metadata.participants).toHaveProperty(pubKey);
159+
expect(wallet.metadata.participants).toHaveProperty(pubKey);
160160
}
161161
});
162162
});

packages/core/src/Provider/SharedWalletProvider/index.ts

-1
This file was deleted.

packages/core/src/Provider/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ export * from './providerFactory';
1212
export * from './types';
1313
export * from './HandleProvider';
1414
export * from './DRepProvider';
15-
export * from './SharedWalletProvider';

yarn.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3389,6 +3389,7 @@ __metadata:
33893389
"@blockfrost/blockfrost-js": ^5.7.0
33903390
"@cardano-sdk/core": "workspace:~"
33913391
"@cardano-sdk/crypto": "workspace:~"
3392+
"@cardano-sdk/key-management": "workspace:~"
33923393
"@cardano-sdk/util": "workspace:~"
33933394
"@cardano-sdk/util-dev": "workspace:~"
33943395
"@types/lodash": ^4.14.182

0 commit comments

Comments
 (0)