Skip to content

Commit

Permalink
Adding token in fetch call (#85)
Browse files Browse the repository at this point in the history
Signed-off-by: vikastc <vikas.tc@dhiway.com>
  • Loading branch information
Vikastc authored Jun 26, 2023
1 parent 49571d0 commit 3919d5f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
API_URL = http://localhost:5103/api/v1
API_URL = http://localhost:5103/api/v1
CORD_API_TOKEN = 'testToken'
14 changes: 11 additions & 3 deletions demo/src/demo-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ 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()
}

async function main() {
const networkAddress = 'ws://127.0.0.1:9944'
//const networkAddress = 'wss://staging.cord.network'
Cord.ConfigService.set({ submitTxResolveOn: Cord.Chain.IS_IN_BLOCK })
Cord.ConfigService.set({
submitTxResolveOn: Cord.Chain.IS_IN_BLOCK,
token: CORD_API_TOKEN,
})
await Cord.connect(networkAddress)

// Step 1: Setup Authority
Expand All @@ -40,8 +45,11 @@ async function main() {
'//Alice',
'sr25519'
)
console.log("Alice (AuthorIdentity for this run): ", authorityAuthorIdentity.address);

console.log(
'Alice (AuthorIdentity for this run): ',
authorityAuthorIdentity.address
)

// Step 2: Setup Identities
console.log(`\n❄️ Demo Identities (KeyRing)`)

Expand Down
12 changes: 10 additions & 2 deletions helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import fetch from 'node-fetch'
import type { SubmittableExtrinsic } from '@cord.network/types'
import { API_URL } from './packages/network/src/chain/Chain'

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

export async function cord_api_query(
modules: any,
section: any,
Expand All @@ -14,7 +16,10 @@ export async function cord_api_query(
try {
const resp = await fetch(cordApiUrl, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${CORD_API_TOKEN}`,
},
})
const data = resp.json()
return data
Expand All @@ -37,7 +42,10 @@ export async function cordApiTx(tx: SubmittableExtrinsic, modules: any) {
extrinsic: tx.toHex(),
}),
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${CORD_API_TOKEN}`,
},
})
const data = submit.json()
return data
Expand Down
9 changes: 6 additions & 3 deletions packages/network/src/chain/Chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,11 @@ export async function signAndSubmitTx(
): Promise<any> {
// : Promise<ISubmittableResult>
// const signedTx = await tx.signAsync(signer, { tip, nonce: -1 })
const submit = await cordApiTx(tx, 'signAndSubmit')

return submit
// return submitSignedTx(signedTx, opts)
try {
const submit = await cordApiTx(tx, 'signAndSubmit')
return submit
} catch (error) {
return error
}
}

0 comments on commit 3919d5f

Please sign in to comment.