Skip to content

Commit

Permalink
Working direct sdk interaction (#97)
Browse files Browse the repository at this point in the history
* Removed env file

Signed-off-by: vikastc <vikas.tc@dhiway.com>

* update

Signed-off-by: vikastc <vikas.tc@dhiway.com>

* updated..

Signed-off-by: vikastc <vikas.tc@dhiway.com>

* direct sdk implementation

Signed-off-by: vikastc <vikas.tc@dhiway.com>

* Working flow of direct sdk interation

Signed-off-by: vikastc <vikas.tc@dhiway.com>

---------

Signed-off-by: vikastc <vikas.tc@dhiway.com>
  • Loading branch information
Vikastc authored and amarts committed Aug 3, 2023
1 parent 23f9f94 commit 93a2460
Show file tree
Hide file tree
Showing 20 changed files with 211 additions and 171 deletions.
2 changes: 0 additions & 2 deletions .env

This file was deleted.

12 changes: 6 additions & 6 deletions demo/src/bench.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import * as Cord from '@cord.network/sdk'
import moment from 'moment'
import Keyring from '@polkadot/keyring'
import { Crypto } from '@cord.network/utils'

import { cordApiTx } from '../../helper'

import { Crypto, cordApiTx } from '@cord.network/utils'

export const sleep = (ms: number): Promise<void> => {
return new Promise((resolve) => {
Expand Down Expand Up @@ -75,9 +72,12 @@ async function main() {
let BatchAuthor = keyring.addFromUri('//Charlie')
let batchAncStartTime = moment()
try {
// api.tx.utility.batchAll(tx_batch).signAndSend(BatchAuthor)
const tx = api.tx.utility.batchAll(tx_batch)
await cordApiTx(tx, 'signAndSend')
const value = await cordApiTx(tx, 'signAndSend')

if (!value) {
api.tx.utility.batchAll(tx_batch).signAndSend(BatchAuthor)
}
} catch (e: any) {
console.log(e.errorCode, '-', e.message)
}
Expand Down
10 changes: 6 additions & 4 deletions demo/src/demo-score-batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import moment from 'moment'
import Keyring from '@polkadot/keyring'
import { ApiPromise, WsProvider } from '@polkadot/api'
import { ScoreType } from '@cord.network/types'
import { UUID } from '@cord.network/utils'
import { cordApiTx } from '../../helper'
import { UUID, cordApiTx } from '@cord.network/utils'

export const sleep = (ms: number): Promise<void> => {
return new Promise((resolve) => {
Expand Down Expand Up @@ -185,9 +184,12 @@ async function main() {
}
}
try {
// api.tx.utility.batchAll(txBatch).signAndSend(batchTransactionAuthor)
const tx = api.tx.utility.batchAll(txBatch)
await cordApiTx(tx, 'signAndSend')
const value = await cordApiTx(tx, 'signAndSend')

if (!value) {
api.tx.utility.batchAll(txBatch).signAndSend(batchTransactionAuthor)
}
} catch (e: any) {
console.log(e.errorCode, '-', e.message)
}
Expand Down
3 changes: 0 additions & 3 deletions demo/src/demo-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import { generateRequestCredentialMessage } from './utils/request_credential_mes
import { getChainCredits, addAuthority } from './utils/createAuthorities'
import { createAccount } from './utils/createAccount'

const { CORD_WSS_URL, CORD_API_URL, CORD_API_TOKEN } = process.env

function getChallenge(): string {
return Cord.Utils.UUID.generate()
}
Expand All @@ -33,7 +31,6 @@ async function main() {
//const networkAddress = 'wss://staging.cord.network'
Cord.ConfigService.set({
submitTxResolveOn: Cord.Chain.IS_IN_BLOCK,
token: CORD_API_TOKEN,
})
await Cord.connect(networkAddress)

Expand Down
23 changes: 12 additions & 11 deletions demo/src/utils/generateDid.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as Cord from '@cord.network/sdk'
import { mnemonicGenerate } from '@polkadot/util-crypto'
import { generateKeypairs } from './generateKeypairs'
import { API_URL } from '../../../packages/network/src/chain/Chain'
import { cord_api_query } from '../../../helper'
import { cord_api_query } from '@cord.network/utils'

/**
* It creates a DID on chain, and returns the mnemonic and DID document
Expand All @@ -15,7 +14,7 @@ export async function createDid(
mnemonic: string
document: Cord.DidDocument
}> {
// const api = Cord.ConfigService.get('api')
const api = Cord.ConfigService.get('api')

const mnemonic = mnemonicGenerate(24)
const {
Expand Down Expand Up @@ -50,15 +49,17 @@ export async function createDid(

const didUri = Cord.Did.getDidUriFromKey(authentication)

const document = await cord_api_query('did', 'query', didUri)
let document: any

// const encodedDid = await api.call.did.query(Cord.Did.toChain(didUri))
// const { document } = Cord.Did.linkedInfoFromChain(encodedDid)
document = await cord_api_query('did', 'query', didUri)

// if (!document) {
// throw new Error('DID was not successfully created.')
// }
if (!document) {
const encodedDid = await api.call.did.query(Cord.Did.toChain(didUri))
document = Cord.Did.linkedInfoFromChain(encodedDid)
if (!document) {
throw new Error('DID was not successfully created.')
}
}


return { mnemonic, document: document }
return { mnemonic, document: document.document }
}
13 changes: 7 additions & 6 deletions demo/src/utils/queryDidName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ export async function getDidDocFromName(

// Query the owner of the provided didName.
const encodedDidNameOwner = await api.call.did.queryByName(didName)
if (encodedDidNameOwner.isSome) {
const {
document: { uri },
} = Cord.Did.linkedInfoFromChain(encodedDidNameOwner)

const {
document: { uri },
} = Cord.Did.linkedInfoFromChain(encodedDidNameOwner)

console.log(` uri: ${uri}`)
console.log('✅ DID name resolved!')
console.log(` uri: ${uri}`)
console.log('✅ DID name resolved!')
}
}
16 changes: 9 additions & 7 deletions demo/src/utils/verifyPresentation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as Cord from '@cord.network/sdk'
import { API_URL } from '../../../packages/network/src/chain/Chain'
import { cord_api_query } from '../../../helper'
import {cord_api_query} from '@cord.network/utils'

/**
* It verifies a presentation by checking the stream on the blockchain and verifying the presentation
Expand All @@ -20,19 +19,22 @@ export async function verifyPresentation(
} = {}
): Promise<boolean> {
try {
let stream: any
// Verify the presentation with the provided challenge.
await Cord.Document.verifyPresentation(presentation, { challenge })

// Verify the credential by checking the stream on the blockchain.

// const api = Cord.ConfigService.get('api')
const api = Cord.ConfigService.get('api')
const chainIdentifier = Cord.Stream.idToChain(presentation.identifier)

const stream = await cord_api_query('stream', 'streams', chainIdentifier)
stream = await cord_api_query('stream', 'streams', chainIdentifier)

if (!stream) {
const streamOnChain = await api.query.stream.streams(chainIdentifier)
stream = Cord.Stream.fromChain(streamOnChain, chainIdentifier)
}

// const streamOnChain = await api.query.stream.streams(chainIdentifier)
// const stream = Cord.Stream.fromChain(streamOnChai, chainIdentifier)

if (stream.revoked) {
return false
}
Expand Down
58 changes: 0 additions & 58 deletions helper.ts

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"version": "0.8.0-3",
"packageManager": "yarn@3.3.1",
"dependencies": {
"dotenv": "^16.1.4",
"node-fetch": "^3.3.1"
}
}
12 changes: 6 additions & 6 deletions packages/config/src/ConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export type configOpts = {
api: ApiPromise
logLevel: LogLevel
submitTxResolveOn: SubscriptionPromise.ResultEvaluator
apiUrl: string
wssUrl: string
token: string | undefined
apiUrl: string | null
wssUrl: string | null
token: string | null
} & { [key: string]: any }

/**
Expand All @@ -57,9 +57,9 @@ export function modifyLogLevel(level: LogLevel): LogLevel {

const defaultConfig: Partial<configOpts> = {
logLevel: DEFAULT_DEBUG_LEVEL,
apiUrl: CORD_API_URL ?? 'localhost:3009',
wssUrl: CORD_WSS_URL ?? 'localhost:9944',
token: CORD_API_TOKEN ?? 'dummyToken',
apiUrl: CORD_API_URL ?? null,
wssUrl: CORD_WSS_URL ?? null,
token: CORD_API_TOKEN ?? null,
}

let configuration: Partial<configOpts> = { ...defaultConfig }
Expand Down
18 changes: 12 additions & 6 deletions packages/did/src/Did.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import type { AccountId32, Extrinsic, Hash } from '@polkadot/types/interfaces'
import type { AnyNumber } from '@polkadot/types/types'
import { BN } from '@polkadot/util'

import fetch from 'node-fetch'
import { API_URL } from '../../network/src/chain/Chain'

import type {
DidDocument,
DidEncryptionKey,
Expand All @@ -24,7 +21,12 @@ import type {
VerificationKeyRelationship,
} from '@cord.network/types'
import { verificationKeyTypes } from '@cord.network/types'
import { Crypto, SDKErrors, ss58Format } from '@cord.network/utils'
import {
Crypto,
SDKErrors,
ss58Format,
cord_api_query,
} from '@cord.network/utils'
import { ConfigService } from '@cord.network/config'
import type {
PalletDidDidDetails,
Expand All @@ -33,7 +35,6 @@ import type {
PalletDidDidDetailsDidPublicKeyDetails,
PalletDidServiceEndpointsDidEndpoint,
} from '@cord.network/augment-api'
import { cord_api_query } from '../../../helper'

import {
EncodedEncryptionKey,
Expand Down Expand Up @@ -435,8 +436,13 @@ export async function generateDidAuthenticatedTx({
blockNumber,
}: AuthorizeCallInput & SigningOptions): Promise<SubmittableExtrinsic> {
const api = ConfigService.get('api')
let number: any

const number = await cord_api_query('system', 'section', 'number')
number = await cord_api_query('system', 'section', 'number')

if (!number) {
number = await api.query.system.number()
}

const signableCall =
api.registry.createType<PalletDidDidDetailsDidAuthorizedCallOperation>(
Expand Down
26 changes: 17 additions & 9 deletions packages/did/src/DidDetails/FullDidDetails.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Extrinsic } from '@polkadot/types/interfaces'
import type { SubmittableExtrinsicFunction } from '@polkadot/api/types'
import { BN } from '@polkadot/util'
import { ConfigService } from '@cord.network/config'

import type {
DidUri,
Expand All @@ -10,13 +11,13 @@ import type {
VerificationKeyRelationship,
} from '@cord.network/types'

import { SDKErrors } from '@cord.network/utils'
import { SDKErrors, cord_api_query } from '@cord.network/utils'

import fetch from 'node-fetch'
import { API_URL } from '../../../network/src/chain/Chain'

import { generateDidAuthenticatedTx } from '../Did.chain.js'
import { cord_api_query } from '../../../../helper'
import {
documentFromChain,
generateDidAuthenticatedTx,
toChain,
} from '../Did.chain.js'

const methodMapping: Record<string, VerificationKeyRelationship | undefined> = {
stream: 'assertionMethod',
Expand Down Expand Up @@ -90,11 +91,18 @@ function increaseNonce(currentNonce: BN, increment = 1): BN {
* @returns The next valid nonce, i.e., the nonce currently stored on the blockchain + 1, wrapping around the max value when reached.
*/
async function getNextNonce(did: DidUri): Promise<BN> {
// const api = ConfigService.get('api')
const api = ConfigService.get('api')
let queried: any

const queried = await cord_api_query('did', 'did', did)
queried = await cord_api_query('did', 'did', did)

// const queried = await api.query.did.did(toChain(did))
if (!queried) {
queried = await api.query.did.did(toChain(did))
const currentNonce = queried.isSome
? documentFromChain(queried).lastTxCounter
: new BN(0)
return increaseNonce(currentNonce)
}

const currentNonce = queried ? new BN(queried.lastTxCounter) : new BN(0)
return increaseNonce(currentNonce)
Expand Down
Loading

0 comments on commit 93a2460

Please sign in to comment.