Skip to content

Commit

Permalink
Revert "feat!: native DIDDocuments and proper DID resolution interface (
Browse files Browse the repository at this point in the history
#799)"

This reverts squash commit 801211d to merge this PR with a merge commit instead.
  • Loading branch information
rflechtner committed Oct 26, 2023
1 parent 801211d commit 4403825
Show file tree
Hide file tree
Showing 72 changed files with 2,880 additions and 3,770 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
*/

import type {
AssetDid,
AssetDidUri,
CTypeHash,
IDelegationNode,
IPublicCredentialInput,
IPublicCredential,
Did,
DidUri,
HexString,
} from '@kiltprotocol/types'
import type { ApiPromise } from '@polkadot/api'
Expand All @@ -29,11 +29,11 @@ import { fromChain as didFromChain } from '@kiltprotocol/did'
import { SDKErrors, cbor } from '@kiltprotocol/utils'

import { getIdForCredential } from './PublicCredential.js'
import { validateDid } from '../dids/index.js'
import { validateUri } from '../dids/index.js'

export interface EncodedPublicCredential {
ctypeHash: CTypeHash
subject: AssetDid
subject: AssetDidUri
claims: HexString
authorization: IDelegationNode['id'] | null
}
Expand Down Expand Up @@ -68,12 +68,12 @@ function credentialInputFromChain({
subject,
}: PublicCredentialsCredentialsCredential): IPublicCredentialInput {
const credentialSubject = subject.toUtf8()
validateDid(credentialSubject)
validateUri(credentialSubject)
return {
claims: cbor.decode(claims),
cTypeHash: ctypeHash.toHex(),
delegationId: authorization.unwrapOr(undefined)?.toHex() ?? null,
subject: credentialSubject as AssetDid,
subject: credentialSubject as AssetDidUri,
}
}

Expand All @@ -86,9 +86,9 @@ export interface PublicCredentialEntry {
*/
ctypeHash: HexString
/**
* DID of the attester.
* DID URI of the attester.
*/
attester: Did
attester: DidUri
/**
* Flag indicating whether the credential is currently revoked.
*/
Expand Down Expand Up @@ -244,13 +244,13 @@ export async function fetchCredentialFromChain(
/**
* Retrieves from the blockchain the [[IPublicCredential]]s that have been issued to the provided AssetDID.
*
* This is the **only** secure way for users to retrieve and verify all the credentials issued to a given [[AssetDid]].
* This is the **only** secure way for users to retrieve and verify all the credentials issued to a given [[AssetDidUri]].
*
* @param subject The AssetDID of the subject.
* @returns An array of [[IPublicCredential]] as the result of combining the on-chain information and the information present in the tx history.
*/
export async function fetchCredentialsFromChain(
subject: AssetDid
subject: AssetDidUri
): Promise<IPublicCredential[]> {
const api = ConfigService.get('api')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { ConfigService } from '@kiltprotocol/config'
import { CType } from '@kiltprotocol/core'
import * as Did from '@kiltprotocol/did'
import type {
AssetDid,
Did as KiltDid,
AssetDidUri,
DidUri,
IAssetClaim,
IClaimContents,
IPublicCredential,
Expand All @@ -39,7 +39,7 @@ const assetIdentifier =

// Build a public credential with fake attestation (i.e., attester, block number, revocation status) information.
function buildCredential(
assetDid: AssetDid,
assetDid: AssetDidUri,
contents: IClaimContents
): IPublicCredential {
const claim: IAssetClaim = {
Expand All @@ -48,7 +48,7 @@ function buildCredential(
subject: assetDid,
}
const credential = PublicCredential.fromClaim(claim)
const attester: KiltDid = Did.getFullDid(devAlice.address)
const attester: DidUri = Did.getFullDidUri(devAlice.address)
return {
...credential,
attester,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { AccountId } from '@polkadot/types/interfaces'
import type { PublicCredentialsCredentialsCredential } from '@kiltprotocol/augment-api'
import type {
HexString,
Did as KiltDid,
DidUri,
IAssetClaim,
ICType,
IDelegationNode,
Expand All @@ -30,15 +30,15 @@ import { toChain as publicCredentialToChain } from './PublicCredential.chain.js'
/**
* Calculates the ID of a [[IPublicCredentialInput]], to be used to retrieve the full credential content from the blockchain.
*
* The ID is formed by first concatenating the SCALE-encoded [[IPublicCredentialInput]] with the SCALE-encoded [[Did]] and then Blake2b hashing the result.
* The ID is formed by first concatenating the SCALE-encoded [[IPublicCredentialInput]] with the SCALE-encoded [[DidUri]] and then Blake2b hashing the result.
*
* @param credential The input credential object.
* @param attester The DID of the credential attester.
* @returns The credential ID.
*/
export function getIdForCredential(
credential: IPublicCredentialInput,
attester: KiltDid
attester: DidUri
): HexString {
const api = ConfigService.get('api')

Expand All @@ -62,7 +62,7 @@ function verifyClaimStructure(input: IAssetClaim | PartialAssetClaim): void {
throw new SDKErrors.CTypeHashMissingError()
}
if (input.subject) {
AssetDid.validateDid(input.subject)
AssetDid.validateUri(input.subject)
}
if (input.contents) {
Object.entries(input.contents).forEach(([key, value]) => {
Expand Down
28 changes: 14 additions & 14 deletions packages/asset-credentials/src/dids/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import type {
AssetDid,
AssetDidUri,
Caip19AssetId,
Caip19AssetInstance,
Caip19AssetNamespace,
Expand All @@ -22,7 +22,7 @@ const ASSET_DID_REGEX =
/^did:asset:(?<chainId>(?<chainNamespace>[-a-z0-9]{3,8}):(?<chainReference>[-a-zA-Z0-9]{1,32}))\.(?<assetId>(?<assetNamespace>[-a-z0-9]{3,8}):(?<assetReference>[-a-zA-Z0-9]{1,64})(:(?<assetInstance>[-a-zA-Z0-9]{1,78}))?)$/

type IAssetDidParsingResult = {
did: AssetDid
uri: AssetDidUri
chainId: Caip2ChainId
chainNamespace: Caip2ChainNamespace
chainReference: Caip2ChainReference
Expand All @@ -33,35 +33,35 @@ type IAssetDidParsingResult = {
}

/**
* Parses an AssetDID and returns the information contained within in a structured form.
* Parses an AssetDID uri and returns the information contained within in a structured form.
* @param assetDid An AssetDID as a string.
* @returns Object containing information extracted from the AssetDID.
* @param assetDidUri An AssetDID uri as a string.
* @returns Object containing information extracted from the AssetDID uri.
*/
export function parse(assetDid: AssetDid): IAssetDidParsingResult {
const matches = ASSET_DID_REGEX.exec(assetDid)?.groups
export function parse(assetDidUri: AssetDidUri): IAssetDidParsingResult {
const matches = ASSET_DID_REGEX.exec(assetDidUri)?.groups
if (!matches) {
throw new SDKErrors.InvalidDidFormatError(assetDid)
throw new SDKErrors.InvalidDidFormatError(assetDidUri)
}

const { chainId, assetId } = matches as Omit<IAssetDidParsingResult, 'did'>
const { chainId, assetId } = matches as Omit<IAssetDidParsingResult, 'uri'>

return {
...(matches as Omit<IAssetDidParsingResult, 'did'>),
did: `did:asset:${chainId}.${assetId}`,
...(matches as Omit<IAssetDidParsingResult, 'uri'>),
uri: `did:asset:${chainId}.${assetId}`,
}
}

/**
* Checks that a string (or other input) is a valid AssetDID.
* Checks that a string (or other input) is a valid AssetDID uri.
* Throws otherwise.
*
* @param input Arbitrary input.
*/
export function validateDid(input: unknown): void {
export function validateUri(input: unknown): void {
if (typeof input !== 'string') {
throw new TypeError(`Asset DID string expected, got ${typeof input}`)
}

parse(input as AssetDid)
parse(input as AssetDidUri)
}
4 changes: 2 additions & 2 deletions packages/core/src/attestation/Attestation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */

import { ConfigService } from '@kiltprotocol/config'
import type { CTypeHash, Did, IAttestation } from '@kiltprotocol/types'
import type { CTypeHash, DidUri, IAttestation } from '@kiltprotocol/types'
import { SDKErrors } from '@kiltprotocol/utils'

import { ApiMocks } from '../../../../tests/testUtils'
Expand All @@ -22,7 +22,7 @@ beforeAll(() => {
})

describe('Attestation', () => {
const identityAlice: Did =
const identityAlice: DidUri =
'did:kilt:4nwPAmtsK5toZfBM9WvmAe4Fa3LyZ3X3JHt7EUFfrcPPAZAm'

const cTypeHash: CTypeHash =
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/attestation/Attestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
IAttestation,
IDelegationHierarchyDetails,
ICredential,
Did as KiltDid,
DidUri,
} from '@kiltprotocol/types'
import { DataUtils, SDKErrors } from '@kiltprotocol/utils'
import * as Did from '@kiltprotocol/did'
Expand Down Expand Up @@ -49,7 +49,7 @@ export function verifyDataStructure(input: IAttestation): void {
if (!input.owner) {
throw new SDKErrors.OwnerMissingError()
}
Did.validateDid(input.owner, 'Did')
Did.validateUri(input.owner, 'Did')

if (typeof input.revoked !== 'boolean') {
throw new SDKErrors.RevokedTypeError()
Expand All @@ -65,7 +65,7 @@ export function verifyDataStructure(input: IAttestation): void {
*/
export function fromCredentialAndDid(
credential: ICredential,
attesterDid: KiltDid
attesterDid: DidUri
): IAttestation {
const attestation = {
claimHash: credential.rootHash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { encodeAddress, randomAsHex, randomAsU8a } from '@polkadot/util-crypto'
import { u8aToHex, u8aToU8a } from '@polkadot/util'

import { parse } from '@kiltprotocol/did'
import type { Did } from '@kiltprotocol/types'
import type { DidUri } from '@kiltprotocol/types'

import {
attestation,
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('proofs', () => {
})

it('checks delegation node owners', async () => {
const delegator: Did = `did:kilt:${encodeAddress(randomAsU8a(32), 38)}`
const delegator: DidUri = `did:kilt:${encodeAddress(randomAsU8a(32), 38)}`
const credentialWithDelegators: KiltCredentialV1 = {
...VC,
federatedTrustModel: VC.federatedTrustModel?.map((i) => {
Expand Down
16 changes: 8 additions & 8 deletions packages/core/src/credentialsV1/KiltAttestationProofV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import type { IEventData, Signer } from '@polkadot/types/types'

import {
authorizeTx,
getFullDid,
validateDid,
getFullDidUri,
validateUri,
fromChain as didFromChain,
} from '@kiltprotocol/did'
import { JsonSchema, SDKErrors, Caip19 } from '@kiltprotocol/utils'
Expand All @@ -43,7 +43,7 @@ import type {
RuntimeCommonAuthorizationAuthorizationId,
} from '@kiltprotocol/augment-api'
import type {
Did,
DidUri,
ICType,
IDelegationNode,
KiltAddress,
Expand Down Expand Up @@ -220,7 +220,7 @@ async function verifyAttestedAt(
): Promise<{
verified: boolean
timestamp: number
attester: Did
attester: DidUri
cTypeId: ICType['$id']
delegationId: IDelegationNode['id'] | null
}> {
Expand Down Expand Up @@ -256,7 +256,7 @@ async function verifyAttestedAt(
Option<Hash> | Option<RuntimeCommonAuthorizationAuthorizationId>
] &
IEventData
const attester = getFullDid(encodeAddress(att.toU8a(), 38))
const attester = getFullDidUri(encodeAddress(att.toU8a(), 38))
const cTypeId = CType.hashToId(cTypeHash.toHex())
const delegationId = authorization.isSome
? (
Expand All @@ -276,7 +276,7 @@ async function verifyAttestedAt(
async function verifyAuthoritiesInHierarchy(
api: ApiPromise,
nodeId: Uint8Array | string,
delegators: Set<Did>
delegators: Set<DidUri>
): Promise<void> {
const node = (await api.query.delegation.delegationNodes(nodeId)).unwrapOr(
null
Expand Down Expand Up @@ -347,7 +347,7 @@ export async function verify(
validateCredentialStructure(credential)
const { nonTransferable, credentialStatus, credentialSubject, issuer } =
credential
validateDid(issuer, 'Did')
validateUri(issuer, 'Did')
await validateSubject(credential, opts)
// 4. check nonTransferable
if (nonTransferable !== true)
Expand Down Expand Up @@ -660,7 +660,7 @@ export type AttestationHandler = (
}>

export interface DidSigner {
did: Did
did: DidUri
signer: SignExtrinsicCallback
}

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/credentialsV1/KiltCredentialV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { JsonSchema, SDKErrors } from '@kiltprotocol/utils'
import type {
ICType,
ICredential,
Did,
DidUri,
IDelegationNode,
} from '@kiltprotocol/types'

Expand Down Expand Up @@ -215,10 +215,10 @@ export function validateStructure(
}

interface CredentialInput {
subject: Did
subject: DidUri
claims: ICredential['claim']['contents']
cType: ICType['$id']
issuer: Did
issuer: DidUri
timestamp?: number
chainGenesisHash?: Uint8Array
claimHash?: ICredential['rootHash']
Expand Down
Loading

0 comments on commit 4403825

Please sign in to comment.