-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathKeyApi.cs
117 lines (98 loc) · 3.67 KB
/
KeyApi.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
107
108
109
110
111
112
113
114
115
116
117
using Ipfs.CoreApi;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Ipfs.Http
{
class KeyApi : IKeyApi
{
/// <summary>
/// Information about a local key.
/// </summary>
public class KeyInfo : IKey
{
/// <inheritdoc />
public Cid Id { get; set; }
/// <inheritdoc />
public string Name { get; set; }
/// <inheritdoc />
public override string ToString()
{
return Name;
}
}
IpfsClient ipfs;
internal KeyApi(IpfsClient ipfs)
{
this.ipfs = ipfs;
}
public async Task<IKey> CreateAsync(string name, string keyType, int size, CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("key/gen", cancel, name, $"type={keyType}", $"size={size}", "ipns-base=base36");
var jobject = JObject.Parse(json);
string id = (string)jobject["Id"];
string apiName = (string)jobject["Name"];
return new KeyInfo
{
Id = id,
Name = apiName
};
}
public async Task<IEnumerable<IKey>> ListAsync(CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("key/list", cancel, null, "l=true", "ipns-base=base36");
var keys = (JArray)(JObject.Parse(json)["Keys"]);
return keys
.Select(k =>
{
string id = (string)k["Id"];
string name = (string)k["Name"];
return new KeyInfo
{
Id = id,
Name = name
};
});
}
public async Task<IKey> RemoveAsync(string name, CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("key/rm", cancel, name, "ipns-base=base36");
var keys = JObject.Parse(json)["Keys"] as JArray;
return keys?
.Select(k =>
{
string id = (string)k["Id"];
string keyName = (string)k["Name"];
return new KeyInfo
{
Id = id,
Name = keyName
};
})
.First();
}
public async Task<IKey> RenameAsync(string oldName, string newName, CancellationToken cancel = default(CancellationToken))
{
var json = await ipfs.DoCommandAsync("key/rename", cancel, oldName, $"arg={newName}", "ipns-base=base36");
var jobject = JObject.Parse(json);
string id = (string)jobject["Id"];
string currentName = (string)jobject["Now"];
return new KeyInfo
{
Id = id,
Name = currentName
};
}
public Task<string> ExportAsync(string name, char[] password, CancellationToken cancel = default(CancellationToken))
{
throw new NotImplementedException();
}
public Task<IKey> ImportAsync(string name, string pem, char[] password = null, CancellationToken cancel = default(CancellationToken))
{
throw new NotImplementedException();
}
}
}