Skip to content

Commit

Permalink
cheqd migration
Browse files Browse the repository at this point in the history
  • Loading branch information
maycon-mello committed Nov 28, 2024
1 parent 139bf7c commit 6996f08
Show file tree
Hide file tree
Showing 9 changed files with 1,639 additions and 76 deletions.
59 changes: 59 additions & 0 deletions integration-tests/cheqd-credentials.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { IWallet } from '@docknetwork/wallet-sdk-core/lib/types';
import { createVerificationController } from '@docknetwork/wallet-sdk-core/src/verification-controller';
import { CheqdCredentialNonZKP } from './data/credentials/cheqd-credentials';
import { closeWallet, createNewWallet, getCredentialProvider, getWallet } from './helpers';
import { ProofTemplateIds, createProofRequest } from './helpers/certs-helpers';

describe('Cheq integration tests', () => {
beforeAll(async () => {
await createNewWallet();
});

it('should verify a non ZKP cheqd credential', async () => {


const wallet: IWallet = await getWallet();

getCredentialProvider().addCredential(CheqdCredentialNonZKP);

const proofRequest = await createProofRequest(
ProofTemplateIds.ANY_CREDENTIAL,
);

const result: any = await getCredentialProvider().isValid(CheqdCredentialNonZKP);

expect(result).toBeTruthy();

const controller = await createVerificationController({
wallet,
});

await controller.start({
template: proofRequest,
});

controller.selectedCredentials.set(CheqdCredentialNonZKP.id, {
credential: CheqdCredentialNonZKP,
});

const presentation = await controller.createPresentation();
console.log('Presentation generated');
console.log(JSON.stringify(presentation, null, 2));
console.log('Sending presentation to Certs API');

let certsResponse;
try {
certsResponse = await controller.submitPresentation(presentation);
console.log('CERTS response');
console.log(JSON.stringify(certsResponse, null, 2));
} catch (err) {
certsResponse = err.response.data;
console.log('Certs API returned an error');
console.log(JSON.stringify(certsResponse, null, 2));
}

expect(certsResponse.verified).toBe(true);
});

afterAll(() => closeWallet());
});
49 changes: 49 additions & 0 deletions integration-tests/data/credentials/cheqd-credentials.js

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,13 @@
"@babel/plugin-transform-modules-commonjs": "^7.18.6",
"@babel/preset-flow": "^7.16.7",
"@babel/preset-typescript": "^7.22.5",
"@cosmjs/proto-signing": "^0.32.4",
"@digitalbazaar/x25519-key-agreement-key-2020": "2.1.0",
"@docknetwork/credential-sdk": "^0.6.0",
"@docknetwork/dock-blockchain-api": "^0.4.1",
"@docknetwork/dock-blockchain-modules": "^0.5.1",
"@docknetwork/cheqd-blockchain-api": "0.11.0",
"@docknetwork/cheqd-blockchain-modules": "0.10.0",
"@docknetwork/credential-sdk": "0.12.0",
"@docknetwork/dock-blockchain-api": "0.6.1",
"@docknetwork/dock-blockchain-modules": "0.8.0",
"@inquirer/checkbox": "^1.3.8",
"@inquirer/input": "^1.2.8",
"@inquirer/prompts": "^3.0.3",
Expand Down
27 changes: 21 additions & 6 deletions packages/core/src/wallet-wasm.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {keyringService} from '@docknetwork/wallet-sdk-wasm/src/services/keyring';
import {utilCryptoService} from '@docknetwork/wallet-sdk-wasm/src/services/util-crypto';
import {dockService} from '@docknetwork/wallet-sdk-wasm/src/services/dock';
import { dockService } from '@docknetwork/wallet-sdk-wasm/src/services/dock';
import { keyringService } from '@docknetwork/wallet-sdk-wasm/src/services/keyring';
import { utilCryptoService } from '@docknetwork/wallet-sdk-wasm/src/services/util-crypto';

import {IWallet} from './types';
import {Network} from '@docknetwork/wallet-sdk-data-store/src/types';
import {WalletEvents} from '@docknetwork/wallet-sdk-wasm/src/modules/wallet';
import { Network } from '@docknetwork/wallet-sdk-data-store/src/types';
import { WalletEvents } from '@docknetwork/wallet-sdk-wasm/src/modules/wallet';
import { captureException } from './helpers';
import { IWallet } from './types';

function isSubstrateNetwork(network: Network) {
return !!network.configs.substrateUrl;
Expand Down Expand Up @@ -37,13 +37,28 @@ export async function setSubstrateNetwork(wallet: IWallet) {
const network = wallet.dataStore.network;
const networkConfigs = network.configs;


let cheqdMnemonicDoc = await wallet.getDocumentById('cheqd-mnemonic');

if (!cheqdMnemonicDoc) {
cheqdMnemonicDoc = {
id: 'cheqd-mnemonic',
value: await utilCryptoService.mnemonicGenerate(12),
}

await wallet.addDocument(cheqdMnemonicDoc);
}

await keyringService.initialize({
ss58Format: networkConfigs.addressPrefix,
});

dockService
.init({
address: networkConfigs.substrateUrl,
cheqdApiUrl: networkConfigs.cheqdApiUrl,
networkId: network.id,
cheqdMnemonic: cheqdMnemonicDoc.value,
})
.then(() => {
wallet.eventManager.emit(WalletEvents.networkConnected);
Expand Down
2 changes: 1 addition & 1 deletion packages/relay-service/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/wallet-sdk-relay-service",
"version": "0.4.19",
"version": "0.4.23",
"license": "https://github.com/docknetwork/react-native-sdk/LICENSE",
"repository": {
"type": "git",
Expand Down
3 changes: 3 additions & 0 deletions packages/wasm/src/modules/network-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type NetworkInfo = {
name: string,
substrateUrl: string | string[],
addressPrefix: number,
cheqdApiUrl: string,
};

export type NetworkId = 'mainnet' | 'testnet' | 'local' | 'custom';
Expand All @@ -14,11 +15,13 @@ export const SUBSTRATE_NETWORKS : Record<NetworkId, NetworkInfo> = {
name: 'Dock PoS Mainnet',
substrateUrl: ['wss://mainnet-node.dock.io', 'wss://mainnet-node-2.dock.io'],
addressPrefix: 22,
cheqdApiUrl: 'https://cheqd-mainnet-rpc.autostake.com/',
},
testnet: {
name: 'Dock PoS Testnet',
substrateUrl: 'wss://knox-1.dock.io',
addressPrefix: 21,
cheqdApiUrl: 'https://cheqd-testnet.docknode.io/',
},
local: {
name: 'Local Node',
Expand Down
3 changes: 3 additions & 0 deletions packages/wasm/src/services/dock/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ export const validation = {

export type InitParams = {
address: string,
cheqdApiUrl: string,
networkId: string,
cheqdMnemonic?: string,
};
68 changes: 51 additions & 17 deletions packages/wasm/src/services/dock/service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
// @ts-nocheck
import { DockAPI } from '@docknetwork/dock-blockchain-api';

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 {
DIDKeyResolver,
ResolverRouter,
CoreResolver,
UniversalResolver,
WILDCARD,
DIDResolver,
ResolverRouter,
} from '@docknetwork/credential-sdk/resolver';
import {DockDIDModule} from '@docknetwork/dock-blockchain-modules';
import {initializeWasm} from '@docknetwork/crypto-wasm-ts/lib/index';
import {EventEmitter} from 'events';
import {Logger} from '../../core/logger';
import {once} from '../../modules/event-manager';
import {InitParams, validation} from './configs';
import {DockCoreModules} from '@docknetwork/dock-blockchain-modules';
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';

let dockInstance;

Expand All @@ -39,6 +44,7 @@ export class DockService {
dock;
modules;
didModule;
cheqdApi;
isDockReady = false;
resolver: any;
static Events = {
Expand All @@ -56,8 +62,12 @@ export class DockService {
constructor() {
this.name = 'dock';
this.dock = new DockAPI();
this.cheqdApi = new CheqdAPI();
this.didModule = new DockDIDModule(this.dock);
this.modules = new DockCoreModules(this.dock);
this.dockModules = new DockCoreModules(this.dock);
this.cheqdModules = new CheqdCoreModules(this.cheqdApi);
this.modules = new MultiApiCoreModules([this.dockModules]);

dockInstance = this.dock;
this.emitter = new EventEmitter();
this.resolver = this.createDIDResolver();
Expand All @@ -78,10 +88,9 @@ export class DockService {
createDIDResolver() {
return new AnyDIDResolver([
new DIDKeyResolver(),
new DIDResolver(this.didModule),
new CoreResolver(this.modules),
new UniversalResolver(universalResolverUrl),
]);

}
/**
*
Expand All @@ -90,7 +99,6 @@ export class DockService {
*/
async init(params: InitParams) {
validation.init(params);


if (this.dock.isConnected) {
await this.dock.disconnect();
Expand All @@ -100,16 +108,42 @@ export class DockService {

await getDock().init(params);

Logger.info(`Substrate initialized at: ${params.address}`);

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}`);
Logger.info(`Using cheqd account: ${address}`);

await this.cheqdApi.init({
wallet,
url: checkdApiUrl,
network: cheqdNetworkId,
});

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') {
if (
process.env.NODE_ENV !== 'test' ||
process.env.API_MOCK_DISABLED === 'true'
) {
await initializeWasm();
}

Logger.info(`Substrate initialized at: ${params.address}`);

this._setDockReady(true);

return true;
Expand Down
Loading

0 comments on commit 6996f08

Please sign in to comment.