Skip to content

Commit

Permalink
refactor!: simplify CType (#656)
Browse files Browse the repository at this point in the history
* refactor: make ICTypeSchema the new ICType
* feat: function to extract hash from CType id
* chore: rename getIdForCTypeHash
* refactor: create ctypes from properties & title
* feat: cTypeHash type alias

Co-authored-by: Tom Adler <[email protected]>
  • Loading branch information
rflechtner and arty-name authored Oct 11, 2022
1 parent 3fdc3b5 commit 97c005e
Show file tree
Hide file tree
Showing 34 changed files with 330 additions and 674 deletions.
30 changes: 11 additions & 19 deletions packages/core/src/__integrationtests__/Attestation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,19 +241,13 @@ describe('When there is an attester, claimer and ctype drivers license', () => {
}, 60_000)

it('should not be possible to attest a claim on a Ctype that is not on chain', async () => {
const badCtype = CType.fromSchema({
$id: 'kilt:ctype:0x1',
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
title: 'badDriversLicense',
properties: {
name: {
type: 'string',
},
weight: {
type: 'integer',
},
const badCtype = CType.fromProperties('badDriversLicense', {
name: {
type: 'string',
},
weight: {
type: 'integer',
},
type: 'object',
})

const content = { name: 'Ralph', weight: 120 }
Expand Down Expand Up @@ -420,19 +414,17 @@ describe('When there is an attester, claimer and ctype drivers license', () => {
})

describe('when there is another Ctype that works as a legitimation', () => {
const officialLicenseAuthorityCType = CType.fromSchema({
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
title: 'License Authority',
properties: {
const officialLicenseAuthorityCType = CType.fromProperties(
'License Authority',
{
LicenseType: {
type: 'string',
},
LicenseSubtypes: {
type: 'string',
},
},
type: 'object',
})
}
)

beforeAll(async () => {
if (await isCtypeOnChain(officialLicenseAuthorityCType)) return
Expand Down
58 changes: 15 additions & 43 deletions packages/core/src/__integrationtests__/Ctypes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,9 @@ describe('When there is an CtypeCreator and a verifier', () => {

function makeCType(): ICType {
ctypeCounter += 1
return CType.fromSchema({
$id: `kilt:ctype:0x${ctypeCounter}`,
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
title: `ctype${ctypeCounter}`,
properties: {
name: { type: 'string' },
},
type: 'object',
} as ICType['schema'])
return CType.fromProperties(`ctype${ctypeCounter}`, {
name: { type: 'string' },
})
}

beforeAll(async () => {
Expand Down Expand Up @@ -77,12 +71,9 @@ describe('When there is an CtypeCreator and a verifier', () => {
)
await submitTx(authorizedStoreTx, paymentAccount)

expect(CType.fromChain(await api.query.ctype.ctypes(ctype.hash))).toBe(
ctypeCreator.uri
)
await expect(CType.verifyStored(ctype)).resolves.not.toThrow()

ctype.owner = ctypeCreator.uri
expect(
CType.fromChain(await api.query.ctype.ctypes(CType.idToChain(ctype.$id)))
).toBe(ctypeCreator.uri)
await expect(CType.verifyStored(ctype)).resolves.not.toThrow()
}, 40_000)

Expand All @@ -108,42 +99,23 @@ describe('When there is an CtypeCreator and a verifier', () => {
submitTx(authorizedStoreTx2, paymentAccount)
).rejects.toMatchObject({ section: 'ctype', name: 'CTypeAlreadyExists' })

expect(CType.fromChain(await api.query.ctype.ctypes(ctype.hash))).toBe(
ctypeCreator.uri
)
expect(
CType.fromChain(await api.query.ctype.ctypes(CType.idToChain(ctype.$id)))
).toBe(ctypeCreator.uri)
}, 45_000)

it('should tell when a ctype is not on chain', async () => {
const iAmNotThere = CType.fromSchema({
$id: 'kilt:ctype:0x2',
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
title: 'ctype2',
properties: {
game: { type: 'string' },
},
type: 'object',
} as ICType['schema'])

const iAmNotThereWithOwner = CType.fromSchema(
{
$id: 'kilt:ctype:0x3',
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
title: 'ctype2',
properties: {
game: { type: 'string' },
},
type: 'object',
},
ctypeCreator.uri
)
const iAmNotThere = CType.fromProperties('ctype2', {
game: { type: 'string' },
})

await expect(CType.verifyStored(iAmNotThere)).rejects.toThrow()
expect((await api.query.ctype.ctypes(iAmNotThere.hash)).isNone).toBe(true)
expect(
(await api.query.ctype.ctypes(CType.idToChain(iAmNotThere.$id))).isNone
).toBe(true)

const fakeHash = Crypto.hashStr('abcdefg')
expect((await api.query.ctype.ctypes(fakeHash)).isNone).toBe(true)

await expect(CType.verifyStored(iAmNotThereWithOwner)).rejects.toThrow()
})
})

Expand Down
20 changes: 10 additions & 10 deletions packages/core/src/__integrationtests__/Delegation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ let attesterKey: KeyTool

async function writeHierarchy(
delegator: DidDocument,
ctypeHash: ICType['hash'],
cTypeId: ICType['$id'],
sign: SignCallback
): Promise<DelegationNode> {
const rootNode = DelegationNode.newRoot({
account: delegator.uri,
permissions: [Permission.DELEGATE],
cTypeHash: ctypeHash,
cTypeHash: CType.idToHash(cTypeId),
})

const storeTx = await rootNode.getStoreTx()
Expand Down Expand Up @@ -136,7 +136,7 @@ it('fetches the correct deposit amount', async () => {
it('should be possible to delegate attestation rights', async () => {
const rootNode = await writeHierarchy(
root,
driversLicenseCType.hash,
driversLicenseCType.$id,
rootKey.getSignCallback(root)
)
const delegatedNode = await addDelegation(
Expand All @@ -158,7 +158,7 @@ describe('and attestation rights have been delegated', () => {
beforeAll(async () => {
rootNode = await writeHierarchy(
root,
driversLicenseCType.hash,
driversLicenseCType.$id,
rootKey.getSignCallback(root)
)
delegatedNode = await addDelegation(
Expand Down Expand Up @@ -262,7 +262,7 @@ describe('revocation', () => {
it('delegator can revoke but not remove delegation', async () => {
const rootNode = await writeHierarchy(
delegator,
driversLicenseCType.hash,
driversLicenseCType.$id,
delegatorSign
)
const delegationA = await addDelegation(
Expand Down Expand Up @@ -310,7 +310,7 @@ describe('revocation', () => {
it('delegate cannot revoke root but can revoke own delegation', async () => {
const delegationRoot = await writeHierarchy(
delegator,
driversLicenseCType.hash,
driversLicenseCType.$id,
delegatorSign
)
const delegationA = await addDelegation(
Expand Down Expand Up @@ -350,7 +350,7 @@ describe('revocation', () => {
it('delegator can revoke root, revoking all delegations in tree', async () => {
let delegationRoot = await writeHierarchy(
delegator,
driversLicenseCType.hash,
driversLicenseCType.$id,
delegatorSign
)
const delegationA = await addDelegation(
Expand Down Expand Up @@ -390,7 +390,7 @@ describe('Deposit claiming', () => {
// Delegation nodes are written on the chain using `paymentAccount`.
const rootNode = await writeHierarchy(
root,
driversLicenseCType.hash,
driversLicenseCType.$id,
rootKey.getSignCallback(root)
)
const delegatedNode = await addDelegation(
Expand Down Expand Up @@ -443,7 +443,7 @@ describe('hierarchyDetails', () => {
it('can fetch hierarchyDetails', async () => {
const rootNode = await writeHierarchy(
root,
driversLicenseCType.hash,
driversLicenseCType.$id,
rootKey.getSignCallback(root)
)
const delegatedNode = await addDelegation(
Expand All @@ -457,7 +457,7 @@ describe('hierarchyDetails', () => {

const details = await delegatedNode.getHierarchyDetails()

expect(details.cTypeHash).toBe(driversLicenseCType.hash)
expect(CType.hashToId(details.cTypeHash)).toBe(driversLicenseCType.$id)
expect(details.id).toBe(rootNode.id)
}, 60_000)
})
Expand Down
48 changes: 9 additions & 39 deletions packages/core/src/__integrationtests__/Did.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,12 +499,7 @@ describe('DID authorization', () => {
}, 60_000)

it('authorizes ctype creation with DID signature', async () => {
const ctype = CType.fromSchema({
title: UUID.generate(),
properties: {},
type: 'object',
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
})
const ctype = CType.fromProperties(UUID.generate(), {})
const call = api.tx.ctype.add(CType.toChain(ctype))
const tx = await Did.authorizeTx(
did.uri,
Expand All @@ -530,12 +525,7 @@ describe('DID authorization', () => {
)
await submitTx(tx, paymentAccount)

const ctype = CType.fromSchema({
title: UUID.generate(),
properties: {},
type: 'object',
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
})
const ctype = CType.fromProperties(UUID.generate(), {})
const call = api.tx.ctype.add(CType.toChain(ctype))
const tx2 = await Did.authorizeTx(
did.uri,
Expand Down Expand Up @@ -975,17 +965,12 @@ describe('DID extrinsics batching', () => {
}, 50_000)

it('simple batch succeeds despite failures of some extrinsics', async () => {
const ctype = CType.fromSchema({
title: UUID.generate(),
properties: {},
type: 'object',
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
})
const ctype = CType.fromProperties(UUID.generate(), {})
const ctypeStoreTx = api.tx.ctype.add(CType.toChain(ctype))
const rootNode = DelegationNode.newRoot({
account: fullDid.uri,
permissions: [Permission.DELEGATE],
cTypeHash: ctype.hash,
cTypeHash: CType.idToHash(ctype.$id),
})
const delegationStoreTx = await rootNode.getStoreTx()
const delegationRevocationTx = await rootNode.getRevokeTx(fullDid.uri)
Expand All @@ -1010,17 +995,12 @@ describe('DID extrinsics batching', () => {
})

it('batchAll fails if any extrinsics fail', async () => {
const ctype = CType.fromSchema({
title: UUID.generate(),
properties: {},
type: 'object',
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
})
const ctype = CType.fromProperties(UUID.generate(), {})
const ctypeStoreTx = api.tx.ctype.add(CType.toChain(ctype))
const rootNode = DelegationNode.newRoot({
account: fullDid.uri,
permissions: [Permission.DELEGATE],
cTypeHash: ctype.hash,
cTypeHash: CType.idToHash(ctype.$id),
})
const delegationStoreTx = await rootNode.getStoreTx()
const delegationRevocationTx = await rootNode.getRevokeTx(fullDid.uri)
Expand Down Expand Up @@ -1082,30 +1062,20 @@ describe('DID extrinsics batching', () => {
// Authentication key
const web3NameReleaseExt = api.tx.web3Names.releaseByOwner()
// Attestation key
const ctype1 = CType.fromSchema({
title: UUID.generate(),
properties: {},
type: 'object',
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
})
const ctype1 = CType.fromProperties(UUID.generate(), {})
const ctype1Creation = api.tx.ctype.add(CType.toChain(ctype1))
// Delegation key
const rootNode = DelegationNode.newRoot({
account: fullDid.uri,
permissions: [Permission.DELEGATE],
cTypeHash: ctype1.hash,
cTypeHash: CType.idToHash(ctype1.$id),
})
const delegationHierarchyCreation = await rootNode.getStoreTx()

// Authentication key
const web3NameNewClaimExt = api.tx.web3Names.claim('test-2')
// Attestation key
const ctype2 = CType.fromSchema({
title: UUID.generate(),
properties: {},
type: 'object',
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
})
const ctype2 = CType.fromProperties(UUID.generate(), {})
const ctype2Creation = api.tx.ctype.add(CType.toChain(ctype2))
// Delegation key
const delegationHierarchyRemoval = await rootNode.getRevokeTx(fullDid.uri)
Expand Down
29 changes: 11 additions & 18 deletions packages/core/src/__integrationtests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,18 @@ export async function isCtypeOnChain(ctype: ICType): Promise<boolean> {
}
}

export const driversLicenseCType = CType.fromSchema({
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
title: 'Drivers License',
properties: {
name: {
type: 'string',
},
age: {
type: 'integer',
},
export const driversLicenseCType = CType.fromProperties('Drivers License', {
name: {
type: 'string',
},
age: {
type: 'integer',
},
type: 'object',
})

export const driversLicenseCTypeForDeposit = CType.fromSchema({
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
title: 'Drivers License for deposit test',
properties: {
export const driversLicenseCTypeForDeposit = CType.fromProperties(
'Drivers License for deposit test',
{
name: {
type: 'string',
},
Expand All @@ -130,9 +124,8 @@ export const driversLicenseCTypeForDeposit = CType.fromSchema({
location: {
type: 'string',
},
},
type: 'object',
})
}
)

// Submits resolving when IS_IN_BLOCK
export async function submitTx(
Expand Down
Loading

0 comments on commit 97c005e

Please sign in to comment.