Skip to content

Commit 08d11a2

Browse files
authored
adding cheqd connection fallback (#454)
1 parent 88a0820 commit 08d11a2

7 files changed

Lines changed: 66 additions & 28 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {
2+
closeWallet,
3+
getWallet,
4+
} from './helpers';
5+
import { blockchainService } from '@docknetwork/wallet-sdk-wasm/src/services/blockchain';
6+
import { BLOCKCHAIN_NETWORKS } from '@docknetwork/wallet-sdk-wasm/src/modules/network-manager';
7+
8+
describe('Test cheqd blockchain connection', () => {
9+
beforeEach(() => getWallet());
10+
11+
it('should use fallback blockchain network if the first one is not working', async () => {
12+
const result = await blockchainService.init({
13+
cheqdApiUrl: [
14+
'https://some-invalid-network.local',
15+
...BLOCKCHAIN_NETWORKS.mainnet.cheqdApiUrl,
16+
],
17+
networkId: 'mainnet',
18+
})
19+
expect(result).toBe(true);
20+
});
21+
22+
it('should be able to connect to cheqd mainnet', async () => {
23+
const result = await blockchainService.init({
24+
cheqdApiUrl: BLOCKCHAIN_NETWORKS.mainnet.cheqdApiUrl,
25+
networkId: 'mainnet',
26+
})
27+
expect(result).toBe(true);
28+
});
29+
30+
it('should be able to connect to cheqd testnet', async () => {
31+
const result = await blockchainService.init({
32+
cheqdApiUrl: BLOCKCHAIN_NETWORKS.testnet.cheqdApiUrl,
33+
networkId: 'testnet',
34+
})
35+
expect(result).toBe(true);
36+
});
37+
38+
afterAll(() => closeWallet());
39+
});
40+

integration-tests/custom-networks.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ describe('Custom networks', () => {
1212
credentialHostnames: ['creds.polygon.com'],
1313
id: 'polygon',
1414
configs: {
15-
addressPrefix: 21,
1615
cheqdApiUrl: 'https://testnet.cheqd.docknode.io/',
1716
},
1817
},
@@ -21,7 +20,6 @@ describe('Custom networks', () => {
2120
credentialHostnames: ['creds.mumbai.polygon.com'],
2221
id: 'mumbai',
2322
configs: {
24-
addressPrefix: 21,
2523
cheqdApiUrl: 'https://testnet.cheqd.docknode.io/',
2624
},
2725
},

packages/core/src/wallet-wasm.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import { captureException } from './helpers';
55
import { IWallet, WalletEvents } from './types';
66

77
function isBlockchainNetwork(network: Network) {
8-
return !!(network.configs.substrateUrl || network.configs.cheqdApiUrl);
8+
return !!(network.configs.cheqdApiUrl);
99
}
1010

1111
/**
12-
* Update existing substrate network connection
13-
* Compare connected substrate connection with the current walle network
12+
* Update existing blockchain network connection
13+
* Compare connected blockchain connection with the current walle network
1414
* Disconnect and Establish a new connection if the network is different
1515
*/
1616
export async function handleBlockchainNetworkChange(
1717
wallet: IWallet,
1818
): Promise<void> {
1919
const currentAddress = await blockchainService.getAddress();
2020
const networkId = wallet.dataStore.networks.find(
21-
network => network.configs.substrateUrl === currentAddress || network.configs.cheqdApiUrl === currentAddress,
21+
network => network.configs.cheqdApiUrl === currentAddress,
2222
)?.id;
2323
const currentNetworkId = wallet.dataStore.network?.id;
2424

@@ -60,7 +60,6 @@ export async function setBlockchainNetwork(wallet: IWallet) {
6060

6161
blockchainService
6262
.init({
63-
substrateUrl: networkConfigs.substrateUrl,
6463
cheqdApiUrl: networkConfigs.cheqdApiUrl,
6564
networkId: network.id,
6665
cheqdMnemonic: cheqdMnemonicDoc.value,

packages/data-store/src/configs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {SUBSTRATE_NETWORKS} from '@docknetwork/wallet-sdk-wasm/src/modules/network-manager';
1+
import {BLOCKCHAIN_NETWORKS} from '@docknetwork/wallet-sdk-wasm/src/modules/network-manager';
22
import {DataStoreConfigs} from './types';
33
import {genericDocumentNetworkResolver} from './document-network-resolver';
44

@@ -11,13 +11,13 @@ export const DEFAULT_CONFIGS: DataStoreConfigs = {
1111
name: 'Mainnet',
1212
id: 'mainnet',
1313
credentialHostnames: [/creds\..*\.io/],
14-
configs: SUBSTRATE_NETWORKS.mainnet,
14+
configs: BLOCKCHAIN_NETWORKS.mainnet,
1515
},
1616
{
1717
name: 'Testnet',
1818
id: 'testnet',
1919
credentialHostnames: [/creds-.*\.*\.io/],
20-
configs: SUBSTRATE_NETWORKS.testnet,
20+
configs: BLOCKCHAIN_NETWORKS.testnet,
2121
},
2222
],
2323
};

packages/wasm/src/modules/network-manager.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,36 @@ import assert from 'assert';
33

44
export type NetworkInfo = {
55
name: string,
6-
substrateUrl: string | string[],
7-
addressPrefix: number,
8-
cheqdApiUrl: string,
6+
cheqdApiUrl: string | string[],
97
};
108

119
export type NetworkId = 'mainnet' | 'testnet' | 'local' | 'custom';
1210

13-
export const SUBSTRATE_NETWORKS : Record<NetworkId, NetworkInfo> = {
11+
export const BLOCKCHAIN_NETWORKS : Record<NetworkId, NetworkInfo> = {
1412
mainnet: {
1513
name: 'Cheqd Mainnet',
16-
substrateUrl: null,
17-
addressPrefix: 22,
18-
cheqdApiUrl: 'https://mainnet.cheqd.docknode.io',
14+
cheqdApiUrl: [
15+
'https://mainnet.cheqd.docknode.io',
16+
'https://rpc.cheqd.net',
17+
],
1918
},
2019
testnet: {
2120
name: 'Cheqd Testnet',
22-
substrateUrl: null,
23-
addressPrefix: 21,
24-
cheqdApiUrl: 'https://testnet.cheqd.docknode.io',
21+
cheqdApiUrl: [
22+
'https://testnet.cheqd.docknode.io',
23+
'https://api.cheqd.network',
24+
],
2525
},
2626
local: {
2727
name: 'Local Node',
28-
substrateUrl: 'ws://127.0.0.1:9944',
29-
addressPrefix: 21,
28+
cheqdApiUrl: [
29+
'http://localhost:8080',
30+
],
3031
},
3132
};
3233

3334
function getNetworkInfo(networkId): NetworkInfo {
34-
const networkInfo = SUBSTRATE_NETWORKS[networkId];
35+
const networkInfo = BLOCKCHAIN_NETWORKS[networkId];
3536

3637
assert(!!networkInfo, `Network ${networkId} not found`);
3738

@@ -57,7 +58,7 @@ export class NetworkManager {
5758
* @param {string} networkId
5859
*/
5960
setNetworkId(networkId: NetworkId) {
60-
assert(!!SUBSTRATE_NETWORKS[networkId], `invalid networkId ${networkId}`);
61+
assert(!!BLOCKCHAIN_NETWORKS[networkId], `invalid networkId ${networkId}`);
6162

6263
this.networkId = networkId;
6364
}

packages/wasm/src/services/blockchain/configs.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import assert from 'assert';
44
export const validation = {};
55

66
export type InitParams = {
7-
substrateUrl: string,
8-
cheqdApiUrl?: string,
7+
cheqdApiUrl?: string | string[],
98
networkId?: string,
109
cheqdMnemonic?: string,
1110
};

packages/wasm/src/services/blockchain/service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,12 @@ export class BlockchainService {
231231
try {
232232
await this.cheqdApi.init({
233233
wallet,
234-
url: checkdApiUrl,
234+
urls: Array.isArray(checkdApiUrl) ? checkdApiUrl : [checkdApiUrl],
235235
network: cheqdNetworkId,
236236
});
237237
Logger.info(`Cheqd initialized at: ${checkdApiUrl}`);
238238
} catch (err) {
239+
debugger
239240
Logger.error(`Failed to initialize cheqd at: ${checkdApiUrl}`, err);
240241
}
241242

@@ -314,7 +315,7 @@ export class BlockchainService {
314315
* @example
315316
* const apiUrl = await blockchainService.getAddress();
316317
*/
317-
async getAddress() {
318+
async getAddress(): Promise<string | string[]> {
318319
return this.cheqdApiUrl;
319320
}
320321

0 commit comments

Comments
 (0)