Skip to content

Commit

Permalink
feat: 🎸 Add procedure to create multiple child identities
Browse files Browse the repository at this point in the history
  • Loading branch information
prashantasdeveloper committed Jul 19, 2024
1 parent 5053f00 commit 8496491
Show file tree
Hide file tree
Showing 11 changed files with 652 additions and 19 deletions.
22 changes: 22 additions & 0 deletions src/api/client/Identities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AuthorizationRequest,
ChildIdentity,
Context,
createChildIdentities,
createChildIdentity,
createPortfolios,
Identity,
Expand All @@ -17,6 +18,7 @@ import {
import {
AllowIdentityToCreatePortfoliosParams,
AttestPrimaryKeyRotationParams,
CreateChildIdentitiesParams,
CreateChildIdentityParams,
ProcedureMethod,
RegisterIdentityParams,
Expand Down Expand Up @@ -89,6 +91,13 @@ export class Identities {
context
);

this.createChildren = createProcedureMethod(
{
getProcedureAndArgs: args => [createChildIdentities, args],
},
context
);

this.allowIdentityToCreatePortfolios = createProcedureMethod(
{ getProcedureAndArgs: args => [allowIdentityToCreatePortfolios, args] },
context
Expand Down Expand Up @@ -211,6 +220,19 @@ export class Identities {
*/
public createChild: ProcedureMethod<CreateChildIdentityParams, ChildIdentity>;

/**
* Create child identities using off chain authorization
*
* @note the list of `key` provided in the params should not be linked to any other account
*
* @throws if
* - the signing account is not a primary key
* - the signing Identity is already a child of some other identity
* - `expiresAt` is not a future date
* - the any `key` in `childKeyAuths` is already linked to an Identity
*/
public createChildren: ProcedureMethod<CreateChildIdentitiesParams, ChildIdentity[]>;

/**
* Gives permission to the Identity to create Portfolios on behalf of the signing Identity
*
Expand Down
28 changes: 27 additions & 1 deletion src/api/client/__tests__/Identities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '~/internal';
import { dsMockUtils, entityMockUtils, procedureMockUtils } from '~/testUtils/mocks';
import { Mocked } from '~/testUtils/types';
import { RotatePrimaryKeyToSecondaryParams } from '~/types';
import { CreateChildIdentitiesParams, RotatePrimaryKeyToSecondaryParams } from '~/types';
import { tuple } from '~/types/utils';
import * as utilsConversionModule from '~/utils/conversion';

Expand Down Expand Up @@ -95,6 +95,32 @@ describe('Identities Class', () => {
});
});

describe('method: createChildren', () => {
it('should prepare the procedure with the correct arguments and context, and return the resulting transaction', async () => {
const args: CreateChildIdentitiesParams = {
childKeyAuths: [
{
key: 'someKey',
authSignature: '0xsignature',
},
],
expiresAt: new Date('2050/01/01'),
};

const expectedTransaction = 'someTransaction' as unknown as PolymeshTransaction<
ChildIdentity[]
>;

when(procedureMockUtils.getPrepareMock())
.calledWith({ args, transformer: undefined }, context, {})
.mockResolvedValue(expectedTransaction);

const tx = await identities.createChildren(args);

expect(tx).toBe(expectedTransaction);
});
});

describe('method: registerIdentity', () => {
it('should prepare the procedure with the correct arguments and context, and return the resulting transaction', async () => {
const args = {
Expand Down
19 changes: 18 additions & 1 deletion src/api/procedures/__tests__/addSecondaryAccountsWithAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('addSecondaryAccounts procedure', () => {
});

beforeEach(() => {
// jest.spyOn()
identity = entityMockUtils.getIdentityInstance();
actingAccount = entityMockUtils.getAccountInstance();

Expand All @@ -61,7 +62,6 @@ describe('addSecondaryAccounts procedure', () => {
assets: null,
portfolios: null,
transactions: null,
transactionGroups: [],
},
},
authSignature: '0xSignature',
Expand Down Expand Up @@ -94,6 +94,23 @@ describe('addSecondaryAccounts procedure', () => {
dsMockUtils.cleanup();
});

it('should throw an error if the expiry date is not valid', () => {
const proc = procedureMockUtils.getInstance<AddSecondaryAccountsParams, Identity, Storage>(
mockContext,
{
identity,
actingAccount,
}
);

return expect(
prepareAddSecondaryKeysWithAuth.call(proc, {
...params,
expiresAt: new Date('2020/01/01'),
})
).rejects.toThrow('Expiry date must be in the future');
});

it('should throw an error if the one or more accounts are already linked to an Identity', () => {
const accounts = [
{
Expand Down
Loading

0 comments on commit 8496491

Please sign in to comment.