-
Notifications
You must be signed in to change notification settings - Fork 4
/
participant.controller.ts
99 lines (89 loc) · 2.6 KB
/
participant.controller.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import * as yup from 'yup';
import {
Controller,
ConvectorController,
Invokable,
Param,
BaseStorage
} from '@worldsibu/convector-core';
import { ChaincodeTx } from '@worldsibu/convector-platform-fabric';
import { Participant } from './participant.model';
import { ClientIdentity } from 'fabric-shim';
@Controller('participant')
export class ParticipantController extends ConvectorController<ChaincodeTx> {
@Invokable()
public async register(
@Param(yup.string())
id: string,
@Param(yup.string())
name: string
) {
// Retrieve to see if exists
const existing = await Participant.getOne(id);
if (!existing || !existing.id) {
let participant = new Participant();
participant.id = id;
participant.name = name || id;
participant.msp = this.tx.identity.getMSPID();
// Create a new identity
participant.identities = [{
fingerprint: this.sender,
status: true
}];
console.log(JSON.stringify(participant));
await participant.save();
} else {
throw new Error('Identity exists already, please call changeIdentity fn for updates');
}
}
@Invokable()
public async changeIdentity(
@Param(yup.string())
id: string,
@Param(yup.string())
newIdentity: string
) {
// Check permissions
let isAdmin = this.tx.identity.getAttributeValue('admin');
console.log(this.tx.identity);
console.log(isAdmin);
let requesterMSP = this.tx.identity.getMSPID();
// Retrieve to see if exists
const existing = await Participant.getOne(id);
console.log('Existing participant:');
console.log(existing);
if (!existing || !existing.id) {
throw new Error('No identity exists with that ID');
}
console.log(`existing.msp=${existing.msp} requesterMSP=${requesterMSP}`);
if (existing.msp != requesterMSP) {
throw new Error('Unathorized. MSPs do not match');
}
console.log(`isAdmin=${isAdmin}`);
if (!isAdmin) {
throw new Error('Unathorized. Requester identity is not an admin');
}
// Disable previous identities!
existing.identities = existing.identities.map(identity => {
identity.status = false;
return identity;
});
// Set the enrolling identity
existing.identities.push({
fingerprint: newIdentity,
status: true
});
await existing.save();
}
@Invokable()
public async get(
@Param(yup.string())
id: string
) {
const existing = await Participant.getOne(id);
if (!existing || !existing.id) {
throw new Error(`No identity exists with that ID ${id}`);
}
return existing;
}
}