-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathAuthChain.cs
More file actions
110 lines (88 loc) · 3.28 KB
/
Copy pathAuthChain.cs
File metadata and controls
110 lines (88 loc) · 3.28 KB
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
107
108
109
110
using DCL.Optimization.ThreadSafePool;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
namespace DCL.Web3.Chains
{
public class AuthChain : IEnumerable<AuthLink>, IDisposable
{
private static readonly ThreadSafeObjectPool<AuthChain> POOL = new (() => new AuthChain());
// If you ever change the container or key type, please check that your change does not break
// the AuthChainHeaderNames class, if it still exists at the time.
private readonly Dictionary<AuthLinkType, AuthLink> chain = new ();
private bool disposed;
public static AuthChain Create()
{
// Reset the recycled instance: a pooled AuthChain comes back with disposed == true
// (set by the Dispose that released it), which made its next Dispose a no-op — the
// instance never returned to the pool again and reads saw stale state.
AuthChain instance = POOL.Get()!;
instance.disposed = false;
instance.chain.Clear();
return instance;
}
private AuthChain() { }
~AuthChain()
{
Dispose();
}
public void Dispose()
{
if (disposed)
return;
chain.Clear();
POOL.Release(this);
disposed = true;
}
public bool TryGet(AuthLinkType type, out AuthLink link) =>
chain.TryGetValue(type, out link);
public AuthLink Get(AuthLinkType type) =>
chain[type];
public void Set(AuthLink link) =>
chain[link.type] = link;
public void SetSigner(string signerAddress)
{
Set(new AuthLink
{
type = AuthLinkType.SIGNER,
payload = signerAddress,
signature = "",
});
}
public IEnumerator<AuthLink> GetEnumerator() =>
((IEnumerable<AuthLink>)chain.Values).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() =>
chain.GetEnumerator();
public override string ToString() =>
ToJson();
public string ToJson() =>
new Serialized(this).ToJson();
[Serializable]
private struct Serialized
{
private List<AuthLink> chain;
public Serialized(AuthChain authChain)
{
chain = new List<AuthLink>(authChain.chain.Values);
chain.Sort(Comparer.INSTANCE);
}
public string ToJson() =>
JsonConvert.SerializeObject(chain);
private class Comparer : IComparer<AuthLink>
{
public static readonly Comparer INSTANCE = new ();
private static readonly List<AuthLinkType> ORDER = new ()
{
AuthLinkType.SIGNER,
AuthLinkType.ECDSA_EPHEMERAL,
AuthLinkType.ECDSA_SIGNED_ENTITY,
AuthLinkType.ECDSA_EIP_1654_EPHEMERAL,
AuthLinkType.ECDSA_EIP_1654_SIGNED_ENTITY,
};
public int Compare(AuthLink x, AuthLink y) =>
ORDER.IndexOf(x.type).CompareTo(ORDER.IndexOf(y.type));
}
}
}
}