Skip to content

Commit

Permalink
Allow to pass wallet to CheqdAPI, misc tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
olegnn committed Nov 22, 2024
1 parent cf62e35 commit 4eb91b8
Show file tree
Hide file tree
Showing 22 changed files with 144 additions and 59 deletions.
9 changes: 9 additions & 0 deletions examples/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# @docknetwork/sdk-examples

## 0.5.1

### Patch Changes

- Updated dependencies
- @docknetwork/dock-blockchain-modules@0.8.0
- @docknetwork/credential-sdk@0.12.0
- @docknetwork/dock-blockchain-api@0.6.1

## 0.5.0

### Minor Changes
Expand Down
8 changes: 4 additions & 4 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@docknetwork/sdk-examples",
"private": true,
"type": "module",
"version": "0.5.0",
"version": "0.5.1",
"scripts": {
"bbs-dock-example": "babel-node ./bbs-dock.js",
"claim-deduction-example": "babel-node ./claim-deduction.js",
Expand All @@ -19,9 +19,9 @@
"lint": "eslint \"*.js\""
},
"dependencies": {
"@docknetwork/credential-sdk": "0.11.0",
"@docknetwork/dock-blockchain-api": "0.6.0",
"@docknetwork/dock-blockchain-modules": "0.7.0"
"@docknetwork/credential-sdk": "0.12.0",
"@docknetwork/dock-blockchain-api": "0.6.1",
"@docknetwork/dock-blockchain-modules": "0.8.0"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
Expand Down
11 changes: 11 additions & 0 deletions packages/cheqd-blockchain-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @docknetwork/cheqd-blockchain-api

## 0.11.0

### Minor Changes

- Allow to pass `wallet` to `CheqdAPI`, misc tweaks

### Patch Changes

- Updated dependencies
- @docknetwork/credential-sdk@0.12.0

## 0.10.0

### Minor Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/cheqd-blockchain-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/cheqd-blockchain-api",
"version": "0.10.0",
"version": "0.11.0",
"license": "MIT",
"main": "./dist/esm/index.js",
"type": "module",
Expand Down Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {
"@cheqd/sdk": "cjs",
"@docknetwork/credential-sdk": "0.11.0"
"@docknetwork/credential-sdk": "0.12.0"
},
"devDependencies": {
"@babel/cli": "^7.24.1",
Expand Down
27 changes: 13 additions & 14 deletions packages/cheqd-blockchain-api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
maybeToJSONString,
fmtIter,
} from '@docknetwork/credential-sdk/utils';
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
import {
DIDModule,
ResourceModule,
Expand Down Expand Up @@ -55,28 +54,25 @@ export class CheqdAPI extends AbstractApiProvider {
};

/**
* Initializes `CheqdAPI` with the supplied url and mnemonic.
* Initializes `CheqdAPI` with the supplied url, wallet and network type.
* @param {object} configuration
* @param {string} [configuration.url]
* @param {string} [configuration.mnemonic]
* @param {*} [configuration.wallet]
* @param {string} [configuration.network]
* @returns {this}
*/
async init({ url, mnemonic, network } = {}) {
async init({ url, wallet, network } = {}) {
if (network !== CheqdNetwork.Mainnet && network !== CheqdNetwork.Testnet) {
throw new Error(
`Invalid network provided: \`${network}\`, expected one of \`${fmtIter(
Object.values(CheqdNetwork),
)}\``,
);
} else if (!mnemonic) {
throw new Error('`mnemonic` must be provided');
} else if (wallet == null) {
throw new Error('`wallet` must be provided');
}

this.ensureNotInitialized();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
prefix: 'cheqd',
});
const options = {
modules: [DIDModule, ResourceModule],
rpcUrl: url,
Expand All @@ -85,8 +81,8 @@ export class CheqdAPI extends AbstractApiProvider {
};
const sdk = await createCheqdSDK(options);

this.ensureNotInitialized().sdk = sdk;
this.network = network;
this.ensureNotInitialized();
this.sdk = sdk;

return this;
}
Expand All @@ -95,8 +91,9 @@ export class CheqdAPI extends AbstractApiProvider {
* @returns {void}
*/
async disconnect() {
this.ensureInitialized();

delete this.sdk;
delete this.network;
}

/**
Expand Down Expand Up @@ -184,9 +181,9 @@ export class CheqdAPI extends AbstractApiProvider {
if (id instanceof NamespaceDid) {
if (id.isCheqd) {
if (id.asCheqd.isTestnet) {
return this.network === CheqdNetwork.Testnet;
return this.sdk.options.network === CheqdNetwork.Testnet;
} else if (id.asCheqd.isMainnet) {
return this.network === CheqdNetwork.Mainnet;
return this.sdk.options.network === CheqdNetwork.Mainnet;
}
}
} else if (id instanceof DIDRef) {
Expand All @@ -200,3 +197,5 @@ export class CheqdAPI extends AbstractApiProvider {
return false;
}
}

export { CheqdNetwork };
1 change: 1 addition & 0 deletions packages/cheqd-blockchain-api/src/wallet/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@cosmjs/proto-signing';
11 changes: 11 additions & 0 deletions packages/cheqd-blockchain-modules/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @docknetwork/cheqd-blockchain-modules

## 0.10.0

### Minor Changes

- Allow to pass `wallet` to `CheqdAPI`, misc tweaks

### Patch Changes

- Updated dependencies
- @docknetwork/credential-sdk@0.12.0

## 0.9.0

### Minor Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/cheqd-blockchain-modules/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/cheqd-blockchain-modules",
"version": "0.9.0",
"version": "0.10.0",
"type": "module",
"license": "MIT",
"main": "./dist/esm/index.js",
Expand Down Expand Up @@ -33,7 +33,7 @@
"node": ">=18.0.0"
},
"dependencies": {
"@docknetwork/credential-sdk": "0.11.0"
"@docknetwork/credential-sdk": "0.12.0"
},
"devDependencies": {
"@babel/cli": "^7.24.1",
Expand All @@ -42,7 +42,7 @@
"@babel/plugin-syntax-import-attributes": "^7.25.6",
"@babel/plugin-transform-modules-commonjs": "^7.24.1",
"@babel/preset-env": "^7.24.3",
"@docknetwork/cheqd-blockchain-api": "0.10.0",
"@docknetwork/cheqd-blockchain-api": "0.11.0",
"@rollup/plugin-alias": "^4.0.2",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/cheqd-blockchain-modules/tests/attest-module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CheqdTestnetDid } from "@docknetwork/credential-sdk/types";
import { MultiApiDIDModule, MultiApiAttestModule } from "@docknetwork/credential-sdk/modules";
import generateAttestModuleTests from "@docknetwork/credential-sdk/modules/tests/attest-module";
import CheqdDIDModule from "../src/did/module";
import { faucet, url } from "./constants";
import { faucet, url, network } from "./constants";
import CheqdAttestModule from "../src/attest/module";

describe("AttestModule", () => {
Expand All @@ -12,8 +12,8 @@ describe("AttestModule", () => {
beforeAll(async () => {
await cheqd.init({
url: url,
mnemonic: faucet.mnemonic,
network: "testnet",
wallet: await faucet.wallet(),
network,
});
});

Expand Down
6 changes: 3 additions & 3 deletions packages/cheqd-blockchain-modules/tests/blob-module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { MultiApiBlobModule } from "@docknetwork/credential-sdk/modules";
import generateBlobModuleTests from "@docknetwork/credential-sdk/modules/tests/blob-module";
import CheqdDIDModule from "../src/did/module";
import { faucet, url } from "./constants";
import { faucet, url, network } from "./constants";
import CheqdBlobModule from "../src/blob/module";
import { MultiApiDIDModule } from "@docknetwork/credential-sdk/modules";

Expand All @@ -16,8 +16,8 @@ describe("BlobModule", () => {
beforeAll(async () => {
await cheqd.init({
url,
mnemonic: faucet.mnemonic,
network: "testnet",
wallet: await faucet.wallet(),
network,
});
});

Expand Down
8 changes: 7 additions & 1 deletion packages/cheqd-blockchain-modules/tests/constants.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { DirectSecp256k1HdWallet } from '@docknetwork/cheqd-blockchain-api/wallet';
import { CheqdNetwork } from '@docknetwork/cheqd-blockchain-api';

export const faucet = {
prefix: "cheqd",
minimalDenom: "ncheq",
mnemonic: process.env.CHEQD_MNEMONIC ||
"steak come surprise obvious remain black trouble measure design volume retreat float coach amused match album moment radio stuff crack orphan ranch dose endorse",
address: process.env.CHEQD_SENDER_ADDRESS || "cheqd1fgl67nvjdkrnaemjzg5sqvck9fcst4vt99gmma",
async wallet() {
return await DirectSecp256k1HdWallet.fromMnemonic(this.mnemonic, { prefix: 'cheqd' })
}
};

export const url = process.env.CHEQD_RPC_URL || "http://localhost:26657";
export const network = process.env.CHEQD_NETWORK || CheqdNetwork.Testnet;
6 changes: 3 additions & 3 deletions packages/cheqd-blockchain-modules/tests/did-module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CheqdAPI } from "@docknetwork/cheqd-blockchain-api";
import { CheqdTestnetDid } from "@docknetwork/credential-sdk/types";
import didModuleTests from "@docknetwork/credential-sdk/modules/tests/did-module";
import CheqdDIDModule from "../src/did/module";
import { faucet, url } from "./constants";
import { faucet, url, network } from "./constants";
import { MultiApiDIDModule } from "@docknetwork/credential-sdk/modules";

describe("DIDModule", () => {
Expand All @@ -11,8 +11,8 @@ describe("DIDModule", () => {
beforeAll(async () => {
await cheqd.init({
url,
mnemonic: faucet.mnemonic,
network: "testnet",
wallet: await faucet.wallet(),
network,
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { CheqdAPI } from "@docknetwork/cheqd-blockchain-api";
import { CheqdAPI, CheqdNetwork } from "@docknetwork/cheqd-blockchain-api";
import {
CheqdTestnetDid,
CheqdOffchainSignatureParamsRef,
} from "@docknetwork/credential-sdk/types";
import { MultiApiOffchainSignaturesModule } from "@docknetwork/credential-sdk/modules";
import generateOffchainSignaturesModuleTests from "@docknetwork/credential-sdk/modules/tests/offchain-signatures-module";
import { faucet, url } from "./constants";
import { MultiApiDIDModule, MultiApiCoreModules } from "@docknetwork/credential-sdk/modules";
import { faucet, url, network } from "./constants";
import {
CheqdOffchainSignaturesModule,
CheqdDIDModule,
CheqdCoreModules
} from "../src";
import { MultiApiDIDModule, MultiApiCoreModules } from "@docknetwork/credential-sdk/modules";
import CheqdBBSModule from "../src/offchain-signatures/bbs";
import CheqdBBSPlusModule from "../src/offchain-signatures/bbs-plus";
import CheqdPSModule from "../src/offchain-signatures/ps";
Expand All @@ -22,8 +22,8 @@ describe("OffchainSignaturesModule", () => {
beforeAll(async () => {
await cheqd.init({
url,
mnemonic: faucet.mnemonic,
network: "testnet",
wallet: await faucet.wallet(),
network,
});
});

Expand Down
6 changes: 6 additions & 0 deletions packages/credential-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @docknetwork/credential-sdk

## 0.12.0

### Minor Changes

- Allow to pass `wallet` to `CheqdAPI`, misc tweaks

## 0.11.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/credential-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/credential-sdk",
"version": "0.11.0",
"version": "0.12.0",
"license": "MIT",
"type": "module",
"files": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ export class CheqdDid extends withQualifier(TypedEnum, true) {
return this.Class?.Qualifier;
}

static random() {
return new this(this.Class.random());
static random(network) {
if (this.Class != null) {
return new this(this.Class.random());
} else if (network === 'testnet') {
// eslint-disable-next-line no-use-before-define
return CheqdTestnetDid.random();
} else if (network === 'mainnet') {
// eslint-disable-next-line no-use-before-define
return CheqdMainnetDid.random();
} else {
throw new Error(`Unknown network provided: \`${network}\`, expected \`mainnet\` or \`testnet\``);
}
}

toJSON() {
Expand Down
Loading

0 comments on commit 4eb91b8

Please sign in to comment.