/
delete.ts
72 lines (54 loc) · 2.38 KB
/
delete.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
import { Command, flags, run as runCommand } from '@oclif/command'
import { importJWK, CompactSign } from 'jose';
import { parseKidKey, verifyAdminIdentity, createIdentityAxiosClient } from '../../functions/identity';
const ora = require('ora');
const encoder = new TextEncoder();
/**
* Deletes an Identity from a Public Square Network hosting it.
*/
export default class IdentityDelete extends Command {
static description = 'Remove an Identity from a Public Square Network that hosts Identities.';
static flags = {
help: flags.help({ char: 'h' }),
}
static args = [
{
name: 'did',
description: 'DID URL string, expected format: did:(psqr|web):{hostname}(/|:){path}',
},
]
async run() {
const { args } = this.parse(IdentityDelete);
const oraStart = ora('Preparing command...').start();
if (typeof args.did === 'undefined' && !args.did) {
// if you want to run another command it must be returned like so
oraStart.fail('Insufficient arguments provided\n')
return runCommand(['identity:delete', '-h']);
}
// get did from command line
const did = args.did;
oraStart.succeed('Command ready');
const oraRun = ora('Deleting Identity...').start();
// verify admin identity via DID
const idResp = await verifyAdminIdentity(did);
if (idResp.success === false) return oraRun.fail(`Unable to delete identity because ${idResp.message}`);
// parse data
const keyPair = idResp.identity.keyPairs[0];
const key = await importJWK(keyPair.private);
const keyId = parseKidKey(keyPair.kid);
if (keyId === false) return { success: false, message: 'Unable to parse key name' };
const signature = await new CompactSign(encoder.encode(JSON.stringify(idResp.identity.didDoc)))
.setProtectedHeader({
alg: 'ES384',
kid: keyPair.kid,
})
.sign(key);
// create axios client for API endpoint with signature and PUT method
const axResp = await createIdentityAxiosClient(did, 'DELETE', signature);
if (axResp.success) {
oraRun.succeed(`Successfully deleted the Identity for: ${did}`);
} else {
oraRun.fail(`Unable to delete identity because ${axResp.message}`);
}
}
}