-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFairy.Wallet.cs
260 lines (241 loc) · 12.8 KB
/
Fairy.Wallet.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using Neo.Cryptography;
using Neo.IO;
using Neo.Json;
using Neo.Network.P2P;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.Wallets;
using Neo.Wallets.NEP6;
using static System.IO.Path;
namespace Neo.Plugins
{
public partial class Fairy
{
public class FairyAccount : WalletAccount
{
public readonly Wallet wallet;
public readonly KeyPair key;
public FairyAccount(Wallet wallet, UInt160 scriptHash, KeyPair key)
: base(scriptHash, wallet.ProtocolSettings)
{
this.wallet = wallet;
this.key = key;
}
public override bool HasKey => true;
public override KeyPair GetKey() => key;
}
public class FairyWallet : Wallet
{
public readonly List<FairyAccount> accounts = new();
public FairyAccount account { get => accounts[0]; }
public override string Name => "Fairy";
public override Version Version => Version.Parse("68");
public FairyWallet(ProtocolSettings settings, string nep2 = "6PYKrXGB2bhiux49bKYJPMMpaVic6SRrJcCLC8tdrz3YPLgktpe3H3PN35", string password = "1", int N = 16384, int r = 8, int p = 8) : base("./fairy/path", settings) => CreateAccountFromNep2(nep2, password, N, r, p);
public FairyWallet(string wif, ProtocolSettings settings) : base("./fairy/path", settings) => CreateAccountFromWif(wif);
public override bool ChangePassword(string oldPassword, string newPassword) => throw new NotImplementedException($"Password is unnecessary for {nameof(FairyWallet)}");
public override bool Contains(UInt160 scriptHash) => GetAccount(scriptHash) != null;
public WalletAccount CreateAccountFromNep2(string nep2, string password, int N = 16384, int r = 8, int p = 8) => CreateAccount(GetPrivateKeyFromNEP2(nep2, password, ProtocolSettings.AddressVersion, N, r, p));
public WalletAccount CreateAccountFromWif(string wif) => CreateAccount(GetPrivateKeyFromWIF(wif));
public override WalletAccount CreateAccount(byte[] privateKey)
{
KeyPair key = new(privateKey);
Contract contract = new()
{
Script = Contract.CreateSignatureRedeemScript(key.PublicKey),
ParameterList = new[] { ContractParameterType.Signature },
};
return CreateAccount(contract, key);
}
public override WalletAccount CreateAccount(Contract contract, KeyPair? key = null)
{
foreach (WalletAccount acc in accounts)
if (acc.ScriptHash == contract.ScriptHash)
return acc;
if (key == null)
throw new NotImplementedException("privateKey==null not supported for now");
FairyAccount account = new FairyAccount(this, contract.ScriptHash, key);
account.Contract = contract;
accounts.Add(account);
return account;
}
public override WalletAccount CreateAccount(UInt160 scriptHash) => throw new NotImplementedException("privateKey==null not supported for now");
public override void Delete() => throw new NotImplementedException();
public override bool DeleteAccount(UInt160 scriptHash)
{
foreach (FairyAccount account in accounts)
if (account.ScriptHash == scriptHash)
{
accounts.Remove(account);
return true;
}
return false;
}
public override WalletAccount? GetAccount(UInt160 scriptHash)
{
foreach (WalletAccount account in accounts)
if (account.ScriptHash == scriptHash)
return account;
return accounts[0]; // Return the default account! Otherwise it becomes difficult to simulate MakeTransaction for a single account
}
public override IEnumerable<WalletAccount> GetAccounts() => accounts;
public override bool VerifyPassword(string password) => true;
public override void Save() => throw new NotImplementedException();
}
protected Wallet defaultFairyWallet;
protected Witness[] defaultWitness = { new Witness { InvocationScript = (new byte[2] { 0x0c, 0x40 }).Concat(new byte[64]).ToArray(), VerificationScript = (new byte[2] { 0x0c, 0x21 }).Concat(new byte[33]).Concat(new byte[5] { 0x41, 0x56, 0xe7, 0xb3, 0x27 }).ToArray() } };
[FairyRpcMethod]
protected virtual JToken OpenDefaultFairyWallet(JArray _params)
{
string path = _params[0]!.AsString();
string password = _params[1]!.AsString();
if (!File.Exists(path)) throw new FileNotFoundException();
switch (GetExtension(path))
{
case ".json":
{
NEP6Wallet nep6wallet = new(path, password, system.Settings);
defaultFairyWallet = nep6wallet;
break;
}
default:
throw new NotSupportedException();
}
return true;
}
[FairyRpcMethod]
protected virtual JToken ResetDefaultFairyWallet(JArray _params)
{
FairyWallet defaultWallet = new FairyWallet(system.Settings);
defaultFairyWallet = defaultWallet;
JObject json = new();
json[defaultWallet.account.key.PublicKey.ToString()] = defaultWallet.account.ScriptHash.ToString();
return true;
}
[FairyRpcMethod]
protected virtual JToken SetSessionFairyWalletWithNep2(JArray _params)
{
string sessionString = _params[0]!.AsString();
FairySession session = sessionStringToFairySession[sessionString];
JObject json = new();
string nep2 = _params[1]!.AsString();
string password = _params[2]!.AsString();
FairyWallet wallet = new FairyWallet(system.Settings, nep2: nep2, password: password);
json[wallet.account.key.PublicKey.ToString()] = wallet.account.ScriptHash.ToString();
for (int i = 3; i < _params.Count; i += 2)
{
nep2 = _params[i]!.AsString();
password = _params[i + 1]!.AsString();
wallet.CreateAccountFromNep2(nep2, password);
json[wallet.accounts.Last().key.PublicKey.ToString()] = wallet.accounts.Last().ScriptHash.ToString();
}
session.engine.runtimeArgs.fairyWallet = wallet;
return json;
}
[FairyRpcMethod]
protected virtual JToken SetSessionFairyWalletWithWif(JArray _params)
{
string sessionString = _params[0]!.AsString();
FairySession session = sessionStringToFairySession[sessionString];
string wif = _params[1]!.AsString();
FairyWallet wallet = new FairyWallet(wif, system.Settings);
JObject json = new();
json[wallet.account.key.PublicKey.ToString()] = wallet.account.ScriptHash.ToString();
for (int i = 2; i < _params.Count; ++i)
{
wif = _params[i]!.AsString();
wallet.CreateAccountFromWif(wif);
json[wallet.accounts.Last().key.PublicKey.ToString()] = wallet.accounts.Last().ScriptHash.ToString();
}
session.engine.runtimeArgs.fairyWallet = wallet;
return json;
}
internal static UInt160 AddressToScriptHash(string address, byte version)
{
if (UInt160.TryParse(address, out var scriptHash))
return scriptHash;
return address.ToScriptHash(version);
}
[FairyRpcMethod]
protected virtual JObject ForceVerifyWithECDsa(JArray _params)
{
byte[] message = Convert.FromBase64String(_params[0]!.AsString());
byte[] pubkey = Convert.FromBase64String(_params[1]!.AsString());
byte[] signature = Convert.FromBase64String(_params[2]!.AsString());
NamedCurveHash namedCurveHash = _params.Count > 3 ? _params[3]!.AsEnum<NamedCurveHash>() : NamedCurveHash.secp256r1SHA256;
JObject json = new();
json["result"] = CryptoLib.VerifyWithECDsa(message, pubkey, signature, namedCurveHash);
return json;
}
[FairyRpcMethod]
protected virtual JObject ForceSignMessage(JArray _params)
{
string session = _params[0]!.AsString();
FairySession fairySession = GetOrCreateFairySession(session);
Wallet signatureWallet = fairySession.engine.runtimeArgs.fairyWallet == null ? defaultFairyWallet : fairySession.engine.runtimeArgs.fairyWallet;
byte[] message = Convert.FromBase64String(_params[1]!.AsString());
NamedCurveHash namedCurveHash = _params.Count > 2 ? _params[2]!.AsEnum<NamedCurveHash>() : NamedCurveHash.secp256r1SHA256;
(Cryptography.ECC.ECCurve curve, Hasher hasher) = namedCurveHash switch
{
NamedCurveHash.secp256k1SHA256 =>
(Cryptography.ECC.ECCurve.Secp256k1, Hasher.SHA256),
NamedCurveHash.secp256r1SHA256 =>
(Cryptography.ECC.ECCurve.Secp256r1, Hasher.SHA256),
NamedCurveHash.secp256k1Keccak256 =>
(Cryptography.ECC.ECCurve.Secp256k1, Hasher.Keccak256),
NamedCurveHash.secp256r1Keccak256 =>
(Cryptography.ECC.ECCurve.Secp256r1, Hasher.Keccak256),
_ => throw new NotImplementedException($"Invalid namedCurveHash {namedCurveHash}"),
};
JObject json = new();
KeyPair keyPair = signatureWallet.GetAccounts().First().GetKey();
json["signed"] = Convert.ToBase64String(
Crypto.Sign(message, keyPair.PrivateKey, curve, hasher)
);
return json;
}
[FairyRpcMethod]
protected virtual JObject ForceSignTransaction(JArray _params)
{
string session = _params[0]!.AsString();
FairySession fairySession = GetOrCreateFairySession(session);
Wallet signatureWallet = fairySession.engine.runtimeArgs.fairyWallet == null ? defaultFairyWallet : fairySession.engine.runtimeArgs.fairyWallet;
DataCache snapshotForSignature = fairySession.engine.Snapshot.CreateSnapshot();
byte[] script = Convert.FromBase64String(_params[1]!.AsString());
Signer[]? signers = _params.Count >= 3 ? SignersFromJson((JArray)_params[2]!, system.Settings) : null;
if (signers != null && (signers.Length > 1 || signers[0].Account != signatureWallet.GetAccounts().First().ScriptHash))
throw new("Multiple signature not supported by FairyWallet for now");
//JArray WIFprivateKeys = (JArray)_params[...];
long systemFee = _params.Count >= 4 ? long.Parse(_params[3]!.AsString()) : 1000_0000;
long? networkFee = _params.Count >= 5 ? long.Parse(_params[4]!.AsString()) : null;
uint validUntilBlock = _params.Count >= 6 ? uint.Parse(_params[5]!.AsString()) : NativeContract.Ledger.CurrentIndex(snapshotForSignature) + 5760;
uint nonce = _params.Count >= 7 ? uint.Parse(_params[6]!.AsString()) : (uint)new Random().Next();
Transaction tx = new()
{
Version = 0,
Nonce = nonce,
Script = script,
ValidUntilBlock = validUntilBlock,
Signers = signers,
Attributes = Array.Empty<TransactionAttribute>(),
};
tx.SystemFee = systemFee;
tx.NetworkFee = networkFee ?? tx.CalculateNetworkFee(snapshotForSignature, system.Settings, (a) => signatureWallet.GetAccount(a)?.Contract?.Script);
ContractParametersContext context = new(snapshotForSignature, tx, system.Settings.Network);
signatureWallet.Sign(context);
tx.Witnesses = context.GetWitnesses();
JObject result = new();
result["gasconsumed"] = systemFee;
result["tx"] = Convert.ToBase64String(tx.ToArray());
result["txHash"] = tx.Hash.ToString();
result["txSignData"] = Convert.ToBase64String(tx.GetSignData(system.Settings.Network));
result["networkfee"] = tx.NetworkFee;
result["nonce"] = nonce;
result["witness"] = tx.Witnesses.Select(w => w.ToJson()).ToArray();
if (!context.Completed)
result["pendingsignature"] = context.ToJson();
return result;
}
}
}