Skip to content

refactor: internal version of transact #885

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 2 commits into from
Jul 18, 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
14 changes: 9 additions & 5 deletions packages/sdk-js/src/DidHelpers/deactivateDid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import {
} from '../../../../tests/testUtils/index.js'
import { ConfigService } from '../index.js'
import { deactivateDid } from './deactivateDid.js'
import { transact } from './transact.js'
import { transactInternal } from './transact.js'

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

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

describe('deactivate', () => {
Expand Down Expand Up @@ -50,8 +50,8 @@ describe('deactivate', () => {
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[0]>>({
call: expect.any(Object),
expect.objectContaining<Partial<Parameters<typeof transactInternal>[0]>>({
callFactory: expect.any(Function),
expectedEvents: expect.arrayContaining([
{
section: 'did',
Expand All @@ -64,7 +64,11 @@ describe('deactivate', () => {
signers: [keypair],
})
)
expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({
expect(
await mockedTransact.mock.lastCall?.[0]
.callFactory()
.then((f) => f.toHuman())
).toMatchObject({
method: { method: 'delete', section: 'did' },
})
})
Expand Down
7 changes: 4 additions & 3 deletions packages/sdk-js/src/DidHelpers/deactivateDid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import type { SharedArguments, TransactionHandlers } from './interfaces.js'
import { transact } from './transact.js'
import { transactInternal } from './transact.js'

/**
* _Permanently_ deactivates the DID, removing all verification methods and services from its document.
Expand All @@ -18,9 +18,10 @@ import { transact } from './transact.js'
*/
export function deactivateDid(options: SharedArguments): TransactionHandlers {
const { api, didDocument } = options
return transact({
return transactInternal({
...options,
call: api.tx.did.delete(didDocument.service?.length ?? 0),
callFactory: async () =>
api.tx.did.delete(didDocument.service?.length ?? 0),
expectedEvents: [{ section: 'did', method: 'DidDeleted' }],
})
}
28 changes: 20 additions & 8 deletions packages/sdk-js/src/DidHelpers/service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
createLocalDemoFullDidFromKeypair,
} from '../../../../tests/testUtils/index.js'
import { ConfigService } from '../index.js'
import { transact } from './index.js'
import { addService, removeService } from './service.js'
import { transactInternal } from './transact.js'

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

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

describe.each(['#my_service', 'did:kilt:4abctest#my_service'])(
Expand Down Expand Up @@ -57,8 +57,10 @@ describe.each(['#my_service', 'did:kilt:4abctest#my_service'])(
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[0]>>({
call: expect.any(Object),
expect.objectContaining<
Partial<Parameters<typeof transactInternal>[0]>
>({
callFactory: expect.any(Function),
expectedEvents: expect.arrayContaining([
{
section: 'did',
Expand All @@ -71,7 +73,11 @@ describe.each(['#my_service', 'did:kilt:4abctest#my_service'])(
signers: [keypair],
})
)
expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({
expect(
await mockedTransact.mock.lastCall?.[0]
.callFactory()
.then((f) => f.toHuman())
).toMatchObject({
method: {
args: {
service_endpoint: {
Expand All @@ -96,8 +102,10 @@ describe.each(['#my_service', 'did:kilt:4abctest#my_service'])(
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[0]>>({
call: expect.any(Object),
expect.objectContaining<
Partial<Parameters<typeof transactInternal>[0]>
>({
callFactory: expect.any(Function),
expectedEvents: expect.arrayContaining([
{
section: 'did',
Expand All @@ -110,7 +118,11 @@ describe.each(['#my_service', 'did:kilt:4abctest#my_service'])(
signers: [keypair],
})
)
expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({
expect(
await mockedTransact.mock.lastCall?.[0]
.callFactory()
.then((f) => f.toHuman())
).toMatchObject({
method: {
args: {
service_id: 'my_service',
Expand Down
18 changes: 7 additions & 11 deletions packages/sdk-js/src/DidHelpers/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { serviceToChain, urlFragmentToChain } from '@kiltprotocol/did'
import type { DidUrl, Service, UriFragment } from '@kiltprotocol/types'
import { SharedArguments, TransactionHandlers } from './interfaces.js'
import { transact } from './transact.js'
import { transactInternal } from './transact.js'

/**
* Adds a service to the DID Document.
Expand All @@ -23,12 +23,10 @@ export function addService(
service: Service<DidUrl | UriFragment>
}
): TransactionHandlers {
const didServiceUpdateTx = options.api.tx.did.addServiceEndpoint(
serviceToChain(options.service)
)
return transact({
return transactInternal({
...options,
call: didServiceUpdateTx,
callFactory: async () =>
options.api.tx.did.addServiceEndpoint(serviceToChain(options.service)),
expectedEvents: [{ section: 'did', method: 'DidUpdated' }],
})
}
Expand All @@ -46,12 +44,10 @@ export function removeService(
id: DidUrl | UriFragment
}
): TransactionHandlers {
const didServiceUpdateTx = options.api.tx.did.removeServiceEndpoint(
urlFragmentToChain(options.id)
)
return transact({
return transactInternal({
...options,
call: didServiceUpdateTx,
callFactory: async () =>
options.api.tx.did.removeServiceEndpoint(urlFragmentToChain(options.id)),
expectedEvents: [{ section: 'did', method: 'DidUpdated' }],
})
}
9 changes: 5 additions & 4 deletions packages/sdk-js/src/DidHelpers/transact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
* found in the LICENSE file in the root directory of this source tree.
*/

import { SubmittableResult } from '@polkadot/api'
import { authorizeTx, resolver } from '@kiltprotocol/did'
import type { DidDocument, KiltKeyringPair } from '@kiltprotocol/types'
import { Crypto } from '@kiltprotocol/utils'
import { ConfigService } from '../index.js'
import { SubmittableResult } from '@polkadot/api'

import {
ApiMocks,
createLocalDemoFullDidFromKeypair,
} from '../../../../tests/testUtils/index.js'
import { transact } from './index.js'
import { TransactionResult } from './interfaces.js'
import { makeAttestationCreatedEvents } from '../../../../tests/testUtils/testData.js'
import { ConfigService } from '../index.js'
import { TransactionResult } from './interfaces.js'
import { transact } from './transact.js'

jest.mock('@kiltprotocol/did', () => {
return {
Expand Down
33 changes: 28 additions & 5 deletions packages/sdk-js/src/DidHelpers/transact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import type { SharedArguments, TransactionHandlers } from './interfaces.js'
* Instructs a transaction (state transition) as this DID (with this DID as the origin).
*
* @param options Any {@link SharedArguments} and additional parameters.
* @param options.call The transaction / call to execute.
* @param options.callFactory Async callback producing the transaction / call to execute.
* @returns A set of {@link TransactionHandlers}.
*/
export function transact(
export function transactInternal(
options: SharedArguments & {
call: Extrinsic | SubmittableExtrinsic
callFactory: () => Promise<Extrinsic | SubmittableExtrinsic>
expectedEvents?: Array<{ section: string; method: string }>
}
): TransactionHandlers {
Expand All @@ -35,9 +35,16 @@ export function transact(
}
| undefined = {}
) => {
const { didDocument, signers, submitter, call, api, expectedEvents } =
options
const {
didDocument,
signers,
submitter,
callFactory,
api,
expectedEvents,
} = options
const { didNonce, signSubmittable = true } = submitOptions
const call = await callFactory()
const didSigners = await signersForDid(didDocument, ...signers)

const submitterAccount = (
Expand Down Expand Up @@ -75,3 +82,19 @@ export function transact(
getSubmittable,
}
}

/**
* Instructs a transaction (state transition) as this DID (with this DID as the origin).
*
* @param options Any {@link SharedArguments} and additional parameters.
* @param options.call The transaction / call to execute.
* @returns A set of {@link TransactionHandlers}.
*/
export function transact(
options: SharedArguments & {
call: Extrinsic | SubmittableExtrinsic
expectedEvents?: Array<{ section: string; method: string }>
}
): TransactionHandlers {
return transactInternal({ ...options, callFactory: async () => options.call })
}
58 changes: 39 additions & 19 deletions packages/sdk-js/src/DidHelpers/verificationMethod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import {
createLocalDemoFullDidFromKeypair,
} from '../../../../tests/testUtils/index.js'
import { ConfigService } from '../index.js'
import { transactInternal } from './transact.js'
import {
removeVerificationMethod,
setVerificationMethod,
} from './verificationMethod.js'
import { transact } from './transact.js'

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

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

let didDocument: DidDocument
Expand Down Expand Up @@ -55,8 +55,8 @@ describe('signing keys', () => {
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[0]>>({
call: expect.any(Object),
expect.objectContaining<Partial<Parameters<typeof transactInternal>[0]>>({
callFactory: expect.any(Function),
expectedEvents: expect.arrayContaining([
{
section: 'did',
Expand All @@ -69,7 +69,11 @@ describe('signing keys', () => {
signers: [keypair],
})
)
expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({
expect(
await mockedTransact.mock.lastCall?.[0]
.callFactory()
.then((f) => f.toHuman())
).toMatchObject({
method: {
section: 'did',
method: 'setAttestationKey',
Expand All @@ -90,8 +94,8 @@ describe('signing keys', () => {
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[0]>>({
call: expect.any(Object),
expect.objectContaining<Partial<Parameters<typeof transactInternal>[0]>>({
callFactory: expect.any(Function),
expectedEvents: expect.arrayContaining([
{
section: 'did',
Expand All @@ -104,7 +108,11 @@ describe('signing keys', () => {
signers: [keypair],
})
)
expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({
expect(
await mockedTransact.mock.lastCall?.[0]
.callFactory()
.then((f) => f.toHuman())
).toMatchObject({
method: {
section: 'did',
method: 'removeAttestationKey',
Expand All @@ -120,13 +128,13 @@ describe('key agreement keys', () => {
api: mockedApi,
submitter: keypair,
signers: [keypair],
publicKey: { publicKey: keypair.publicKey, type: 'x25519' } as any,
publicKey: { publicKey: keypair.publicKey, type: 'x25519' },
relationship: 'keyAgreement',
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[0]>>({
call: expect.any(Object),
expect.objectContaining<Partial<Parameters<typeof transactInternal>[0]>>({
callFactory: expect.any(Function),
expectedEvents: expect.arrayContaining([
{
section: 'did',
Expand All @@ -139,7 +147,11 @@ describe('key agreement keys', () => {
signers: [keypair],
})
)
expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({
expect(
await mockedTransact.mock.lastCall?.[0]
.callFactory()
.then((f) => f.toHuman())
).toMatchObject({
method: {
section: 'utility',
method: 'batchAll',
Expand All @@ -165,13 +177,13 @@ describe('key agreement keys', () => {
api: mockedApi,
submitter: keypair,
signers: [keypair],
publicKey: { publicKey: keypair.publicKey, type: 'x25519' } as any,
publicKey: { publicKey: keypair.publicKey, type: 'x25519' },
relationship: 'keyAgreement',
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[0]>>({
call: expect.any(Object),
expect.objectContaining<Partial<Parameters<typeof transactInternal>[0]>>({
callFactory: expect.any(Function),
expectedEvents: expect.arrayContaining([
{
section: 'did',
Expand All @@ -184,7 +196,11 @@ describe('key agreement keys', () => {
signers: [keypair],
})
)
expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({
expect(
await mockedTransact.mock.lastCall?.[0]
.callFactory()
.then((f) => f.toHuman())
).toMatchObject({
method: {
section: 'utility',
method: 'batchAll',
Expand Down Expand Up @@ -217,8 +233,8 @@ describe('key agreement keys', () => {
})

expect(mockedTransact).toHaveBeenLastCalledWith(
expect.objectContaining<Partial<Parameters<typeof transact>[0]>>({
call: expect.any(Object),
expect.objectContaining<Partial<Parameters<typeof transactInternal>[0]>>({
callFactory: expect.any(Function),
expectedEvents: expect.arrayContaining([
{
section: 'did',
Expand All @@ -231,7 +247,11 @@ describe('key agreement keys', () => {
signers: [keypair],
})
)
expect(mockedTransact.mock.lastCall?.[0].call.toHuman()).toMatchObject({
expect(
await mockedTransact.mock.lastCall?.[0]
.callFactory()
.then((f) => f.toHuman())
).toMatchObject({
method: {
section: 'did',
method: 'removeKeyAgreementKey',
Expand Down
Loading