Skip to content

Commit

Permalink
feat: Added issuer + verifier gated / non-gated tools (#263)
Browse files Browse the repository at this point in the history
* Added encryption + status frame

* Added transact + observe exported methods

* Re-create lockfile
  • Loading branch information
Eengineer1 committed May 26, 2023
1 parent 4520222 commit db61e55
Show file tree
Hide file tree
Showing 10 changed files with 6,369 additions and 648 deletions.
4,187 changes: 3,780 additions & 407 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"build:esm": "tsc -p tsconfig.esm.json",
"build:cjs": "echo 'Experimental backwards compatibility! Use v2 major version or `@cjs` npm tag for deep CommonJS exports.' && tsc -p tsconfig.cjs.json",
"build:watch": "rm -rf build && npm run build:esm -- --watch",
"build:local": "npm run build && cp -r build ~/AppData/Roaming/npm/node_modules/@cheqd/did-provider-cheqd && veramo config verify",
"generate-plugin-schema": "veramo dev generate-plugin-schema",
"start": "veramo server",
"test:ci": "jest --config=jest.json",
Expand Down Expand Up @@ -65,16 +66,21 @@
"dependencies": {
"@cheqd/sdk": "^3.5.3",
"@cheqd/ts-proto": "^3.2.0",
"@cosmjs/amino": "^0.30.1",
"@cosmjs/crypto": "^0.30.1",
"@cosmjs/proto-signing": "^0.30.1",
"@cosmjs/utils": "^0.30.1",
"@digitalbazaar/vc-status-list": "^7.0.0",
"@lit-protocol/lit-node-client": "^2.1.161",
"@veramo/core": "^5.2.0",
"@veramo/did-manager": "^5.1.2",
"@veramo/did-provider-key": "^5.2.0",
"@veramo/key-manager": "^5.1.2",
"@veramo/utils": "^5.2.0",
"debug": "^4.3.4",
"did-jwt": "^7.2.0",
"did-resolver": "^4.1.0",
"generate-password": "^1.7.0",
"uint8arrays": "^4.0.3",
"uuid": "^9.0.0"
},
Expand Down
2,441 changes: 2,209 additions & 232 deletions src/agent/ICheqd.ts

Large diffs are not rendered by default.

63 changes: 55 additions & 8 deletions src/did-manager/cheqd-did-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import {
} from '@cheqd/sdk'
import { MsgCreateResourcePayload } from '@cheqd/ts-proto/cheqd/resource/v2/index.js'
import {
Coin,
DirectSecp256k1HdWallet,
DirectSecp256k1Wallet
} from '@cosmjs/proto-signing'
import { GasPrice, DeliverTxResponse } from '@cosmjs/stargate'
import { assert } from '@cosmjs/utils'
import { DIDDocument } from 'did-resolver'
import {
Expand Down Expand Up @@ -49,17 +51,28 @@ import { v4 } from 'uuid'

const debug = Debug('veramo:did-provider-cheqd')

type IContext = IAgentContext<IKeyManager>
export const DefaultRPCUrls = {
[CheqdNetwork.Mainnet]: 'https://rpc.cheqd.net',
[CheqdNetwork.Testnet]: 'https://rpc.cheqd.network'
} as const

export enum DefaultRPCUrl {
Mainnet = 'https://rpc.cheqd.net',
Testnet = 'https://rpc.cheqd.network'
}
export const DefaultRESTUrls = {
[CheqdNetwork.Mainnet]: 'https://api.cheqd.net',
[CheqdNetwork.Testnet]: 'https://api.cheqd.network'
} as const

export type IContext = IAgentContext<IKeyManager>

export type DefaultRPCUrl = typeof DefaultRPCUrls[CheqdNetwork.Mainnet] | typeof DefaultRPCUrls[CheqdNetwork.Testnet]

export type DefaultRESTUrl = typeof DefaultRESTUrls[CheqdNetwork.Mainnet] | typeof DefaultRESTUrls[CheqdNetwork.Testnet]

export type LinkedResource = Omit<MsgCreateResourcePayload, 'data'> & { data?: string }

export type ResourcePayload = Partial<MsgCreateResourcePayload>

export type StatusList2021ResourcePayload = ResourcePayload & { resourceType: 'StatusList2021' }

export type TImportableEd25519Key = Required<Pick<IKey, 'publicKeyHex' | 'privateKeyHex'>> & { kid: TImportableEd25519Key['publicKeyHex'], type: 'Ed25519' }

declare const TImportableEd25519Key: {
Expand All @@ -79,16 +92,18 @@ export class EnglishMnemonic extends _ {
export class CheqdDIDProvider extends AbstractIdentifierProvider {
private defaultKms: string
public readonly network: CheqdNetwork
private rpcUrl: string
public readonly rpcUrl: string
private readonly cosmosPayerWallet: Promise<DirectSecp256k1HdWallet | DirectSecp256k1Wallet>
private sdk?: CheqdSDK
private fee?: DidStdFee

static readonly defaultGasPrice = GasPrice.fromString('50ncheq')

constructor(options: { defaultKms: string, cosmosPayerSeed: string, networkType?: CheqdNetwork, rpcUrl?: string }) {
super()
this.defaultKms = options.defaultKms
this.network = options.networkType ? options.networkType : CheqdNetwork.Testnet
this.rpcUrl = options.rpcUrl ? options.rpcUrl : (this.network === CheqdNetwork.Testnet ? DefaultRPCUrl.Testnet : DefaultRPCUrl.Mainnet)
this.rpcUrl = options.rpcUrl ? options.rpcUrl : DefaultRPCUrls[this.network]

if (!options?.cosmosPayerSeed || options.cosmosPayerSeed === '') {
this.cosmosPayerWallet = DirectSecp256k1HdWallet.generate()
Expand All @@ -105,7 +120,7 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
)
}

private async getCheqdSDK(fee?: DidStdFee): Promise<CheqdSDK> {
private async getCheqdSDK(fee?: DidStdFee, gasPrice?: GasPrice): Promise<CheqdSDK> {
if (!this.sdk) {
const wallet = await this.cosmosPayerWallet.catch(() => {
throw new Error(`[did-provider-cheqd]: network: ${this.network} valid cosmosPayerSeed is required`)
Expand All @@ -114,6 +129,7 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
modules: [DIDModule as unknown as AbstractCheqdSDKModule, ResourceModule as unknown as AbstractCheqdSDKModule],
rpcUrl: this.rpcUrl,
wallet: wallet,
gasPrice
}

this.sdk = await createCheqdSDK(sdkOptions)
Expand Down Expand Up @@ -415,6 +431,37 @@ export class CheqdDIDProvider extends AbstractIdentifierProvider {
throw Error('CheqdDIDProvider removeService is not supported.')
}

async transactSendTokens(args: { recipientAddress: string, amount: Coin, memoNonce: string, txBytes?: Uint8Array, timeoutMs?: number, pollIntervalMs?: number }): Promise<DeliverTxResponse> {
const sdk = await this.getCheqdSDK(undefined, CheqdDIDProvider.defaultGasPrice)

if (args?.txBytes) {
// broadcast txBytes
const tx = await sdk.signer.broadcastTx(args.txBytes, args?.timeoutMs, args?.pollIntervalMs)

// assert tx code is 0, in other words, tx succeeded
assert(tx.code === 0, `cosmos_transaction: Failed to send tokens. Reason: ${tx.rawLog}`)

// keep log
debug('Sent tokens', 'txBytes', toString(args.txBytes, 'hex'))

return tx
}

const tx = await sdk.signer.sendTokens(
(await this.cosmosPayerWallet).getAccounts()[0],
args.recipientAddress,
[args.amount],
'auto',
args.memoNonce,
)

assert(tx.code === 0, `cosmos_transaction: Failed to send tokens. Reason: ${tx.rawLog}`)

debug('Sent tokens', args.amount.amount, args.amount.denom, 'to', args.recipientAddress)

return tx
}

private async signPayload(context: IAgentContext<IKeyManager>, data: Uint8Array, verificationMethod: VerificationMethod[] = []): Promise<SignInfo[]> {
return Promise.all(
verificationMethod.map(async (method) => {
Expand Down
8 changes: 7 additions & 1 deletion src/did-manager/cheqd-did-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ interface Options {
url: string
}

/**
* Default resolver url.
* @public
*/
export const resolverUrl = 'https://resolver.cheqd.net/1.0/identifiers/'

/**
* Creates a CheqdDIDResolver instance that can be used with `did-resolver`.
* @public
Expand All @@ -25,7 +31,7 @@ export function getResolver(options?: Options): Record<string, DIDResolver> {
* @public
*/
export class CheqdDidResolver {
private resolverUrl = 'https://resolver.cheqd.net/1.0/identifiers/'
private resolverUrl = resolverUrl

constructor(options?: Options) {
if (options?.url) this.resolverUrl = options.url
Expand Down
Loading

0 comments on commit db61e55

Please sign in to comment.