-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.ts
198 lines (165 loc) · 4.82 KB
/
service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// @ts-nocheck
import {DirectSecp256k1HdWallet} from '@cosmjs/proto-signing';
import {CheqdAPI} from '@docknetwork/cheqd-blockchain-api';
import {CheqdCoreModules} from '@docknetwork/cheqd-blockchain-modules';
import {MultiApiCoreModules} from '@docknetwork/credential-sdk/modules';
import {
CoreResolver,
DIDKeyResolver,
ResolverRouter,
UniversalResolver,
WILDCARD,
} from '@docknetwork/credential-sdk/resolver';
import {initializeWasm} from '@docknetwork/crypto-wasm-ts/lib/index';
import {DockAPI} from '@docknetwork/dock-blockchain-api';
import {
DockCoreModules,
DockDIDModule,
} from '@docknetwork/dock-blockchain-modules';
import {EventEmitter} from 'events';
import {Logger} from '../../core/logger';
import {once} from '../../modules/event-manager';
import {utilCryptoService} from '../util-crypto';
import {InitParams, validation} from './configs';
// Create a resolver in order to lookup DIDs for verifying
export const universalResolverUrl = 'https://uniresolver.io';
class AnyDIDResolver extends ResolverRouter {
method = WILDCARD;
}
/**
*
*/
export class BlockchainService {
dock;
modules;
didModule;
cheqdApi;
isBlockchainReady = false;
resolver: any;
static Events = {
BLOCKCHAIN_READY: 'blockchain-ready',
};
rpcMethods = [
BlockchainService.prototype.disconnect,
BlockchainService.prototype.ensureBlockchainReady,
BlockchainService.prototype.init,
BlockchainService.prototype.isApiConnected,
BlockchainService.prototype.getAddress,
];
constructor() {
this.name = 'blockchain';
this.dock = new DockAPI();
this.cheqdApi = new CheqdAPI();
this.didModule = new DockDIDModule(this.dock);
this.dockModules = new DockCoreModules(this.dock);
this.cheqdModules = new CheqdCoreModules(this.cheqdApi);
this.modules = new MultiApiCoreModules([this.dockModules]);
this.emitter = new EventEmitter();
this.resolver = this.createDIDResolver();
}
/**
*
* @returns
*/
async ensureBlockchainReady() {
if (this.isBlockchainReady) {
return;
}
return once(this.emitter, BlockchainService.Events.BLOCKCHAIN_READY);
}
createDIDResolver() {
return new AnyDIDResolver([
new DIDKeyResolver(),
new CoreResolver(this.modules),
new UniversalResolver(universalResolverUrl),
]);
}
/**
*
* @param {*} params
* @returns
*/
async init(params: InitParams) {
validation.init(params);
if (this.dock.isConnected) {
await this.dock.disconnect();
}
if (this.cheqdApi && this.cheqdApi.isInitialized()) {
await this.cheqdApi.disconnect();
}
Logger.info(`Attempt to initialized substrate at: ${params.address}`);
await this.dock.init(params);
Logger.info(`Substrate initialized at: ${params.address}`);
if (params?.cheqdApiUrl) {
const checkdApiUrl = params?.cheqdApiUrl;
const cheqdNetworkId = params?.networkId;
const cheqdMnemonic =
params?.cheqdMnemonic || (await utilCryptoService.mnemonicGenerate(12));
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(cheqdMnemonic, {
prefix: 'cheqd',
});
const walletAccounts = await wallet.getAccounts();
const [{address}] = walletAccounts;
console.log('Using cheqd account:', address);
Logger.info(
`Attempt to initialized cheqd at: ${checkdApiUrl} with networkId: ${cheqdNetworkId}`,
);
Logger.info(`Using cheqd account: ${address}`);
try {
await this.cheqdApi.init({
wallet,
url: checkdApiUrl,
network: cheqdNetworkId,
});
} catch (err) {
Logger.error(`Failed to initialize cheqd at: ${checkdApiUrl}`);
}
Logger.info(`Cheqd initialized at: ${checkdApiUrl}`);
}
this.address = params.address;
this.resolver = this.createDIDResolver();
if (
process.env.NODE_ENV !== 'test' ||
process.env.API_MOCK_DISABLED === 'true'
) {
await initializeWasm();
}
this._setBlockchainReady(true);
return true;
}
/**
*
* @returns
*/
async disconnect() {
const result = await this.dock.disconnect();
this._setBlockchainReady(false);
return result;
}
async waitBlockchainReady() {
return new Promise(resolve => {
if (this.isBlockchainReady) {
resolve();
} else {
this.emitter.once(BlockchainService.Events.BLOCKCHAIN_READY, resolve);
}
});
}
/**
*
* @returns
*/
async isApiConnected() {
return this.isBlockchainReady;
}
async getAddress() {
return this.dock.address;
}
_setBlockchainReady(isBlockchainReady) {
this.isBlockchainReady = isBlockchainReady;
if (isBlockchainReady) {
this.emitter.emit(BlockchainService.Events.BLOCKCHAIN_READY);
}
}
}
export const blockchainService: BlockchainService = new BlockchainService();