Skip to content

fix: new-sdk-draft1 #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions code_examples/sdk_examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test": "ts-node src/test.ts"
},
"dependencies": {
"@kiltprotocol/sdk-js": "0.35.0",
"@kiltprotocol/sdk-js": "^1.0.0",
"axios": "^1.5.1",
"commander": "^11.1.0",
"dotenv": "^16.3.1",
Expand All @@ -36,4 +36,4 @@
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/* eslint-disable prefer-const */
import * as Kilt from '@kiltprotocol/sdk-js'
import * as Did from '@kiltprotocol/did'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fairly certain this doesn't exist… DID-related methods are part of the Kilt.DidHelpers. and ``Kilt.DidResolver.`.

Or is this some other way to access the methods @rflechtner ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I couldn’t find another way to access the Did.linkedInfoFromChain function. I found apiConfig.call.did.linkedInfoFromChain(encodedKiltnerd123Details), but it doesn’t seem to work the same way and gives errors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are indeed missing a function to resolve a DID document based on a w3n. This could be something worth adding to the DidResolver that Chris mentioned


export async function main(): Promise<Kilt.DidUri | null> {
export async function main(): Promise<String | null> {
let apiConfig = Kilt.ConfigService.get('api')
const encodedKiltnerd123Details =
await apiConfig.call.did.queryByWeb3Name('kiltnerd123')

// This function will throw if kiltnerd123 does not exist
const {
document: { uri }
} = Kilt.Did.linkedInfoFromChain(encodedKiltnerd123Details)
console.log(`My name is kiltnerd123 and this is my DID: "${uri}"`)
document: { id }
} = Did.linkedInfoFromChain(encodedKiltnerd123Details)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't an ideal way of doing things, but it's missing from the SDK, so @rflechtner is going to add a new method and then we can update this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as I explained above, unfortunately (as far as I saw), it’s not included in the new versions of the SDK.

console.log(`My name is kiltnerd123 and this is my DID: "${id}"`)

return uri
return id
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import * as Kilt from '@kiltprotocol/sdk-js'
import {Did} from "@kiltprotocol/types"

export async function main(
uri: Kilt.DidUri
): Promise<Kilt.DidServiceEndpoint[]> {
const kiltnerd123DidDocument = await Kilt.Did.resolve(uri)
export async function main(id: Did): Promise<Object[]> {
const kiltnerd123DidDocument = await Kilt.DidResolver.resolve(id)
console.log(`kiltnerd123's DID Document:`)
console.log(JSON.stringify(kiltnerd123DidDocument, null, 2))

const endpoints = kiltnerd123DidDocument?.document?.service
const endpoints = kiltnerd123DidDocument?.didDocument?.service
if (!endpoints) {
console.log('No endpoints for the DID.')
return []
}

console.log('Endpoints:')
console.log(JSON.stringify(endpoints, null, 2))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import axios from 'axios'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aybarsayan Not introduced by you, but this may be a good time to remove the axios package and replace it with a native fetch to make the HTTP requests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I can change this in a new pr 😊


import * as Kilt from '@kiltprotocol/sdk-js'
import { types } from '@kiltprotocol/credentials'

export async function main(
endpoints: Kilt.DidServiceEndpoint[]
): Promise<Kilt.ICredential> {
const {
data: [{ credential }]
} = await axios.get<Kilt.KiltPublishedCredentialCollectionV1>(
endpoints: types.DidUrl[]
): Promise<types.VerifiableCredential> {
const { data: credential } = await axios.get<types.VerifiableCredential>(
endpoints[0].serviceEndpoint[0]
)
console.log(`Credentials: ${JSON.stringify(credential, null, 2)}`)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import * as Kilt from '@kiltprotocol/sdk-js'
import { VerifiableCredential } from '@kiltprotocol/credentials/lib/cjs/V1/types'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a valid import, you're probably looking for import { types } from '@kiltprotocol/credentials' & then types.VerifiableCredential


export async function main(credential: Kilt.ICredential): Promise<void> {
export async function main(credential: VerifiableCredential): Promise<void> {
try {
const { attester, revoked } =
await Kilt.Credential.verifyCredential(credential)

// Verify that the credential is not revoked. Exception caught by the catch {} block below.
if (revoked) {
throw new Error('The credential has been revoked, hence it is not valid.')
const result = await Kilt.Verifier.verifyCredential({ credential })
console.log(JSON.stringify(result, null, 2))
if (result.verified == false) {
throw new Error("kiltnerd123's credential is not valid.")
} else {
console.log(`kiltnerd123's credential is valid`)
}
console.log(
`kiltnerd123's credential is valid and has been attested by ${attester}!`
)
} catch {
console.log("kiltnerd123's credential is not valid.")
}
Expand Down
Loading
Loading