-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdentityProvider.cs
106 lines (87 loc) · 2.29 KB
/
IdentityProvider.cs
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
100
101
102
103
104
105
106
using Stratis.SmartContracts;
/// <summary>
/// A constrained version of ERC780.
/// contract only holds claims from one identity provider, the "Owner".
/// </summary>
public class IdentityProvider : SmartContract
{
private Address Owner
{
get => State.GetAddress(nameof(Owner));
set => State.SetAddress(nameof(Owner), value);
}
public IdentityProvider(ISmartContractState smartContractState) : base(smartContractState)
{
this.Owner = Message.Sender;
}
public void ChangeOwner(Address newOwner)
{
EnsureOwnerOnly();
Owner = newOwner;
}
private void EnsureOwnerOnly()
{
Assert(Owner == Message.Sender, "The method can be called by only owner.");
}
public void AddClaim(Address issuedTo, uint topic, byte[] data)
{
EnsureOwnerOnly();
byte[] oldData = GetClaim(issuedTo, topic);
SetClaim(issuedTo, topic, data);
Log(new ClaimChanged
{
IssuedTo = issuedTo,
Topic = topic,
Data = data,
OldData = oldData
});
}
public void RemoveClaim(Address issuedTo, uint topic)
{
EnsureOwnerOnly();
// Nothing to delete.
byte[] oldData = GetClaim(issuedTo, topic);
if (oldData.Length == 0)
{
return;
}
ClearClaim(issuedTo, topic);
Log(new ClaimRemoved
{
IssuedTo = issuedTo,
Topic = topic,
Data = oldData
});
}
public byte[] GetClaim(Address issuedTo, uint topic)
{
return State.GetBytes($"Claim[{issuedTo}][{topic}]");
}
private void SetClaim(Address issuedTo, uint topic, byte[] data)
{
State.SetBytes($"Claim[{issuedTo}][{topic}]", data);
}
private void ClearClaim(Address issuedTo, uint topic)
{
State.Clear($"Claim[{issuedTo}][{topic}]");
}
#region Events
public struct ClaimRemoved
{
[Index]
public Address IssuedTo;
[Index]
public uint Topic;
public byte[] Data;
}
public struct ClaimChanged
{
[Index]
public Address IssuedTo;
[Index]
public uint Topic;
public byte[] Data;
public byte[] OldData;
}
#endregion
}