Skip to content

Commit ee69b57

Browse files
committed
feat: discover shared wallet by metadata label
1 parent b01e2b8 commit ee69b57

File tree

8 files changed

+227
-0
lines changed

8 files changed

+227
-0
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
@@ -0,0 +1,29 @@
1+
import * as Crypto from '@cardano-sdk/crypto';
2+
import { BlockfrostClient, BlockfrostProvider } from '../blockfrost';
3+
import { KeyPurpose } from '@cardano-sdk/key-management';
4+
import { Logger } from 'ts-log';
5+
import { MultiSigTransaction, SharedWalletProvider } from '@cardano-sdk/core';
6+
7+
const MULTI_SIG_LABEL = KeyPurpose.MULTI_SIG;
8+
9+
export class BlockfrostSharedWalletProvider extends BlockfrostProvider implements SharedWalletProvider {
10+
constructor(client: BlockfrostClient, logger: Logger) {
11+
super(client, logger);
12+
}
13+
14+
async discoverWallets(pubKey: Crypto.Ed25519KeyHashHex): Promise<MultiSigTransaction[]> {
15+
let page = 1;
16+
const pageSize = 100;
17+
let allTransactions: MultiSigTransaction[] = [];
18+
19+
// eslint-disable-next-line no-constant-condition
20+
while (true) {
21+
const response = await this.request<MultiSigTransaction[]>(`metadata/txs/labels/${MULTI_SIG_LABEL}?page=${page}`);
22+
allTransactions = [...allTransactions, ...response];
23+
if (response.length < pageSize) break;
24+
page++;
25+
}
26+
27+
return allTransactions.filter((wallet) => wallet.json_metadata.participants?.[pubKey]);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './BlockfrostSharedWalletProvider';

packages/cardano-services-client/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export * from './RewardAccountInfoProvider';
1010
export * from './NetworkInfoProvider';
1111
export * from './RewardsProvider';
1212
export * from './HandleProvider';
13+
export * from './SharedWalletProvider';
1314
export * from './version';
1415
export * from './WebSocket';
1516
export {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import * as Crypto from '@cardano-sdk/crypto';
2+
import { BlockfrostClient, BlockfrostSharedWalletProvider } from '../../src';
3+
import { logger } from '@cardano-sdk/util-dev';
4+
5+
const mockedResponses = [
6+
{
7+
json_metadata: {
8+
description: 'd mb-s+lnx+tmt02',
9+
name: 'mb-s+lnx+tmt02',
10+
participants: {},
11+
types: ['payment', 'stake']
12+
},
13+
tx_hash: 'c59b418d946b08554d8be35994420d0e9ba5b01a3cafb9979496f55b2fd9fda6'
14+
},
15+
{
16+
json_metadata: {
17+
description: 'This is really a test wallet, I think with mb-s',
18+
name: 'Another test ms wallet',
19+
participants: {},
20+
types: ['payment', 'stake']
21+
},
22+
tx_hash: '6c1a7652b189aaa3efe39e66c0ef8c894c6f6f8e37fceb58dc41064ac628a569'
23+
},
24+
{
25+
json_metadata: {
26+
description: 'A Multi-Sig test wallet',
27+
name: 'MS Test',
28+
participants: {
29+
'35769ace6c241e0afe467b0a3577af9adea271fc971ba7770ac88712': {
30+
name: 'Wallet 1'
31+
},
32+
'962746268ee907e18c895c9943c6684b01fa7a4956b0fe0fa76cfa6f': {
33+
name: 'Wallet 2'
34+
},
35+
c87b02ef2bed963db3892031ce9387b7d65a83008bad072ddb7409d6: {
36+
name: 'Wallet 1'
37+
},
38+
ebf94d78fb1b185f5b0136260d9192e1270c8303bba5155e773de3fb: {
39+
name: 'Wallet 2'
40+
}
41+
},
42+
types: ['payment', 'stake']
43+
},
44+
tx_hash: '37bbd91b177e0716d5943fb3de8649c9dcabc844553e7656744ffca1c11efddc'
45+
},
46+
{
47+
json_metadata: {
48+
description: 'A simple Multi-Sig wallet with 3/3 signatures needed.',
49+
name: 'MS Test 3/3',
50+
participants: {
51+
'7429c675051bb444a78d0850be2c45a48f8ed3d4ecdb6f059ed19873': {
52+
name: 'Wallet 3'
53+
},
54+
'35769ace6c241e0afe467b0a3577af9adea271fc971ba7770ac88712': {
55+
name: 'Wallet 1'
56+
},
57+
'15254521db8f70ac44aa585475361727e918465425d9fb53f0d754e3': {
58+
name: 'Wallet 3'
59+
},
60+
'962746268ee907e18c895c9943c6684b01fa7a4956b0fe0fa76cfa6f': {
61+
name: 'Wallet 2'
62+
},
63+
c87b02ef2bed963db3892031ce9387b7d65a83008bad072ddb7409d6: {
64+
name: 'Wallet 1'
65+
},
66+
ebf94d78fb1b185f5b0136260d9192e1270c8303bba5155e773de3fb: {
67+
name: 'Wallet 2'
68+
}
69+
},
70+
types: ['payment', 'stake']
71+
},
72+
tx_hash: '42c2eed5fabb3500b7b66e84c73d78633df567803f4a8afd38d485f71a7fcf84'
73+
},
74+
{
75+
json_metadata: {
76+
description: 'A simple Multi-Sig wallet with 3/3 signatures needed',
77+
name: 'MS 3/3',
78+
participants: {},
79+
types: ['payment', 'stake']
80+
},
81+
tx_hash: 'e51c93492c04fc6b8d475c5bbbac483961d1d2ebf592d019619cd199f17ed6f5'
82+
},
83+
{
84+
json_metadata: {
85+
description: 'dsfdsfdsf',
86+
name: 'MS Test',
87+
participants: {
88+
'35769ace6c241e0afe467b0a3577af9adea271fc971ba7770ac88712': {
89+
name: 'sdfdsf'
90+
},
91+
'962746268ee907e18c895c9943c6684b01fa7a4956b0fe0fa76cfa6f': {
92+
name: 'sdfsdf'
93+
},
94+
c87b02ef2bed963db3892031ce9387b7d65a83008bad072ddb7409d6: {
95+
name: 'sdfdsf'
96+
},
97+
ebf94d78fb1b185f5b0136260d9192e1270c8303bba5155e773de3fb: {
98+
name: 'sdfsdf'
99+
}
100+
},
101+
types: ['payment', 'stake']
102+
},
103+
tx_hash: '0c746630f885213618db4af244a8d257e8c03a4041a0fcbece10abe0a6526f5d'
104+
},
105+
{
106+
json_metadata: {
107+
description: 'L + T + K',
108+
name: 'HW MS Test',
109+
participants: {
110+
'7fb20197bb7e2c3b44539fb9784e70db308640c86a1ef45db711cd28': {
111+
name: 'Keystone'
112+
},
113+
'9d237cfa3da50b71859ac7045e4d296252c85f7d72d4c5c889a8c22e': {
114+
name: 'Keystone'
115+
},
116+
a4fb72bcb24a91cb1add70d3158704a4cf14a7909fbbe4edac39efb1: {
117+
name: 'Ledger'
118+
},
119+
ad773cd4bdb0f775c53d34c48e70bd46f1856e21c8103f8d292fcc7a: {
120+
name: 'Ledger'
121+
},
122+
afb321dabccdf5ea26ce4ac9c0cd5aaae6cb47a61e12cd8c8b3f41a0: {
123+
name: 'Trezor'
124+
},
125+
cc9adac917b5a7e191982f1bb979507349e5ae59df8d015a2842f4bd: {
126+
name: 'Trezor'
127+
}
128+
},
129+
types: ['payment', 'stake']
130+
},
131+
tx_hash: 'a160d298a6e49e6b39b33cde296baf171b2ad31c4520cbbc2086d99d3d64bc91'
132+
}
133+
];
134+
135+
describe('BlockfrostSharedWallet', () => {
136+
let request: jest.Mock;
137+
let provider: BlockfrostSharedWalletProvider;
138+
139+
beforeEach(async () => {
140+
request = jest.fn();
141+
const client = { request } as unknown as BlockfrostClient;
142+
provider = new BlockfrostSharedWalletProvider(client, logger);
143+
});
144+
145+
describe('discoverWallets', () => {
146+
it('should return an empty array if no wallets are found', () => {
147+
request.mockResolvedValueOnce(mockedResponses);
148+
return expect(
149+
provider.discoverWallets(Crypto.Ed25519KeyHashHex('0a0ba36b07e61f4b566a99521be1f8b2fdb1ce47246894807b63712b'))
150+
).resolves.toEqual([]);
151+
});
152+
153+
it('should return all wallets for a given public key', async () => {
154+
request.mockResolvedValueOnce(mockedResponses);
155+
const pubKey = '962746268ee907e18c895c9943c6684b01fa7a4956b0fe0fa76cfa6f';
156+
const wallets = await provider.discoverWallets(Crypto.Ed25519KeyHashHex(pubKey));
157+
expect(wallets.length).toEqual(3);
158+
for (const wallet of wallets) {
159+
expect(wallet.json_metadata.participants).toHaveProperty(pubKey);
160+
}
161+
});
162+
});
163+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './types';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Ed25519KeyHashHex } from '@cardano-sdk/crypto';
2+
3+
type ScriptType = number;
4+
5+
interface MultiSigParticipant {
6+
name: string;
7+
description?: string;
8+
icon?: string;
9+
}
10+
11+
interface MultiSigParticipants {
12+
[key: string]: MultiSigParticipant;
13+
}
14+
15+
interface MultiSigRegistration {
16+
types: ScriptType[];
17+
name?: string;
18+
description?: string;
19+
icon?: string;
20+
participants?: MultiSigParticipants;
21+
}
22+
23+
export interface MultiSigTransaction {
24+
tx_hash: string;
25+
json_metadata: MultiSigRegistration;
26+
}
27+
28+
export interface SharedWalletProvider {
29+
discoverWallets: (pubKey: Ed25519KeyHashHex) => Promise<MultiSigTransaction[]>;
30+
}

packages/core/src/Provider/index.ts

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

0 commit comments

Comments
 (0)