Skip to content

test: vm helpers tests #880

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

Merged
merged 6 commits into from
Jul 17, 2024
Merged
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
5 changes: 4 additions & 1 deletion packages/sdk-js/src/DidHelpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import type { SharedArguments } from './interfaces.js'

export { createDid } from './createDid.js'
export { addService, removeService } from './service.js'
export { setVerificationMethod } from './setVerificationMethod.js'
export { transact } from './transact.js'
export {
removeVerificationMethod,
setVerificationMethod,
} from './verificationMethod.js'
export { claimWeb3Name, releaseWeb3Name } from './w3names.js'

/**
Expand Down
7 changes: 6 additions & 1 deletion packages/sdk-js/src/DidHelpers/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ export type SharedArguments = {
submitter: KeyringPair | Blockchain.TransactionSigner
}

type PublicKeyAndType = {
publicKey: Uint8Array
type: KeyringPair['type'] | 'x25519'
}

export type AcceptedPublicKeyEncodings =
| KeyMultibaseEncoded
| { publicKeyMultibase: KeyMultibaseEncoded }
| Pick<KeyringPair, 'publicKey' | 'type'> // interface allows KeyringPair too
| PublicKeyAndType // interface allows KeyringPair too
8 changes: 5 additions & 3 deletions packages/sdk-js/src/DidHelpers/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
*/

import { serviceToChain, urlFragmentToChain } from '@kiltprotocol/did'
import { DidUrl, Service, UriFragment } from '@kiltprotocol/types'
import type { DidUrl, Service, UriFragment } from '@kiltprotocol/types'
import { SharedArguments, TransactionHandlers } from './interfaces.js'
import { transact } from './transact.js'

/**
* Adds a service to the DID Document.
*
* @param options Any {@link SharedArguments} and additional parameters.
* @param options.service The service entry to add to the document.
* If the service id is relative (begins with #) it is automatically expanded with the DID taken from didDocument.id.
* @param options
* @returns A set of {@link TransactionHandlers}.
*/
export function addService(
options: SharedArguments & {
Expand All @@ -35,9 +36,10 @@ export function addService(
/**
* Removes a service from the DID Document.
*
* @param options Any {@link SharedArguments} and additional parameters.
* @param options.id The id of the service to remove from the document.
* If the service id is relative (begins with #) it is automatically expanded with the DID taken from didDocument.id.
* @param options
* @returns A set of {@link TransactionHandlers}.
*/
export function removeService(
options: SharedArguments & {
Expand Down
241 changes: 241 additions & 0 deletions packages/sdk-js/src/DidHelpers/verificationMethod.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
/**
* Copyright (c) 2018-2024, BOTLabs GmbH.
*
* This source code is licensed under the BSD 4-Clause "Original" license
* found in the LICENSE file in the root directory of this source tree.
*/

import type { DidDocument, KiltKeyringPair } from '@kiltprotocol/types'
import { Crypto } from '@kiltprotocol/utils'
import {
ApiMocks,
createLocalDemoFullDidFromKeypair,
} from '../../../../tests/testUtils/index.js'
import { ConfigService } from '../index.js'
import {
removeVerificationMethod,
setVerificationMethod,
} from './verificationMethod.js'
import { transact } from './transact.js'

jest.mock('./transact.js')

const mockedTransact = jest.mocked(transact)
const mockedApi = ApiMocks.createAugmentedApi()

let didDocument: DidDocument
let keypair: KiltKeyringPair
beforeAll(async () => {
ConfigService.set({ api: mockedApi })

keypair = Crypto.makeKeypairFromUri('//Alice')
const { id, verificationMethod, authentication } =
await createLocalDemoFullDidFromKeypair(keypair, {
verificationRelationships: new Set(['assertionMethod']),
})
didDocument = {
id,
authentication,
assertionMethod: authentication,
verificationMethod: verificationMethod?.filter(
(vm) => vm.id === authentication![0]
),
}
})

describe('signing keys', () => {
it('creates a set VM tx', async () => {
setVerificationMethod({
didDocument,
api: mockedApi,
submitter: keypair,
signers: [keypair],
publicKey: keypair,
relationship: 'assertionMethod',
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[0]>>({
call: expect.any(Object),
expectedEvents: expect.arrayContaining([
{
section: 'did',
method: 'DidUpdated',
},
]),
didDocument,
api: mockedApi,
submitter: keypair,
signers: [keypair],
})
)
expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({
method: {
section: 'did',
method: 'setAttestationKey',
args: { new_key: { Ed25519: Crypto.u8aToHex(keypair.publicKey) } },
},
})
})

it('creates a remove VM tx', async () => {
didDocument.assertionMethod = didDocument.authentication
removeVerificationMethod({
didDocument,
api: mockedApi,
submitter: keypair,
signers: [keypair],
verificationMethodId: didDocument.assertionMethod![0],
relationship: 'assertionMethod',
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[0]>>({
call: expect.any(Object),
expectedEvents: expect.arrayContaining([
{
section: 'did',
method: 'DidUpdated',
},
]),
didDocument,
api: mockedApi,
submitter: keypair,
signers: [keypair],
})
)
expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({
method: {
section: 'did',
method: 'removeAttestationKey',
},
})
})
})

describe('key agreement keys', () => {
it('creates a set VM tx for the first key agreement', async () => {
setVerificationMethod({
didDocument,
api: mockedApi,
submitter: keypair,
signers: [keypair],
publicKey: { publicKey: keypair.publicKey, type: 'x25519' } as any,
relationship: 'keyAgreement',
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[0]>>({
call: expect.any(Object),
expectedEvents: expect.arrayContaining([
{
section: 'did',
method: 'DidUpdated',
},
]),
didDocument,
api: mockedApi,
submitter: keypair,
signers: [keypair],
})
)
expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({
method: {
section: 'utility',
method: 'batchAll',
args: {
calls: [
{
section: 'did',
method: 'addKeyAgreementKey',
args: { new_key: { X25519: Crypto.u8aToHex(keypair.publicKey) } },
},
],
},
},
})
})

it('creates a set VM tx for the second key agreement', async () => {
didDocument.keyAgreement = [
`${didDocument.id}#${Crypto.hashStr('keyAgreement1')}`,
]
setVerificationMethod({
didDocument,
api: mockedApi,
submitter: keypair,
signers: [keypair],
publicKey: { publicKey: keypair.publicKey, type: 'x25519' } as any,
relationship: 'keyAgreement',
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[0]>>({
call: expect.any(Object),
expectedEvents: expect.arrayContaining([
{
section: 'did',
method: 'DidUpdated',
},
]),
didDocument,
api: mockedApi,
submitter: keypair,
signers: [keypair],
})
)
expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({
method: {
section: 'utility',
method: 'batchAll',
args: {
calls: [
{
section: 'did',
method: 'removeKeyAgreementKey',
args: { key_id: expect.stringContaining('0x') },
},
{
section: 'did',
method: 'addKeyAgreementKey',
args: { new_key: { X25519: Crypto.u8aToHex(keypair.publicKey) } },
},
],
},
},
})
})

it('creates a remove VM tx', async () => {
removeVerificationMethod({
didDocument,
api: mockedApi,
submitter: keypair,
signers: [keypair],
verificationMethodId: didDocument.keyAgreement![0],
relationship: 'keyAgreement',
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[0]>>({
call: expect.any(Object),
expectedEvents: expect.arrayContaining([
{
section: 'did',
method: 'DidUpdated',
},
]),
didDocument,
api: mockedApi,
submitter: keypair,
signers: [keypair],
})
)
expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({
method: {
section: 'did',
method: 'removeKeyAgreementKey',
},
})
})
})
Loading