generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ChildIdentity.ts
80 lines (69 loc) · 2.1 KB
/
ChildIdentity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { UniqueIdentifiers } from '~/api/entities/Identity';
import { unlinkChildIdentity } from '~/api/procedures/unlinkChildIdentity';
import { Context, Identity } from '~/internal';
import { NoArgsProcedureMethod } from '~/types';
import { identityIdToString, stringToIdentityId } from '~/utils/conversion';
import { createProcedureMethod } from '~/utils/internal';
/**
* Represents a child identity
*/
export class ChildIdentity extends Identity {
/**
* @hidden
*/
public constructor(identifiers: UniqueIdentifiers, context: Context) {
super(identifiers, context);
this.unlinkFromParent = createProcedureMethod(
{
getProcedureAndArgs: () => [unlinkChildIdentity, { child: this }],
voidArgs: true,
},
context
);
}
/**
* Returns the parent of this Identity (if any)
*/
public async getParentDid(): Promise<Identity | null> {
const {
context: {
polymeshApi: {
query: { identity },
},
},
context,
did,
} = this;
const rawIdentityId = stringToIdentityId(did, context);
const rawParentDid = await identity.parentDid(rawIdentityId);
if (rawParentDid.isEmpty) {
return null;
}
const parentDid = identityIdToString(rawParentDid.unwrap());
return new Identity({ did: parentDid }, context);
}
/**
* @hidden
* since a child Identity doesn't has any other children, this method overrides the base implementation to return empty array
*/
public override getChildIdentities(): Promise<ChildIdentity[]> {
return Promise.resolve([]);
}
/**
* Determine whether this child Identity exists
*
* @note asset Identities aren't considered to exist for this check
*/
public override async exists(): Promise<boolean> {
const parentDid = await this.getParentDid();
return parentDid !== null;
}
/**
* Unlinks this child identity from its parent
*
* @throws if
* - this identity doesn't have a parent
* - the transaction signer is not the primary key of the child identity
*/
public unlinkFromParent: NoArgsProcedureMethod<void>;
}