-
Notifications
You must be signed in to change notification settings - Fork 849
/
Neblio.cs
459 lines (402 loc) · 14.7 KB
/
Neblio.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
using System;
using NBitcoin.Crypto;
using NBitcoin.DataEncoders;
using NBitcoin.Protocol;
using Newtonsoft.Json.Linq;
namespace NBitcoin.Altcoins
{
// Reference: https://github.com/NeblioTeam/neblio/blob/master/wallet/chainparams.cpp
public class Neblio : NetworkSetBase
{
public static Neblio Instance { get; } = new Neblio();
public override string CryptoCode => "NEBL";
private Neblio()
{
}
public class NeblioConsensusFactory : ConsensusFactory
{
private NeblioConsensusFactory()
{
}
public static NeblioConsensusFactory Instance { get; } = new NeblioConsensusFactory();
public override BlockHeader CreateBlockHeader()
{
return new NeblioBlockHeader();
}
public override Block CreateBlock()
{
return new NeblioBlock(new NeblioBlockHeader());
}
public override Transaction CreateTransaction()
{
return new NeblioTransaction(this);
}
}
#pragma warning disable CS0618 // Type or member is obsolete
public class NeblioBlockHeader : BlockHeader
{
public override uint256 GetPoWHash()
{
throw new NotSupportedException("PoW for NEBL is not supported");
}
}
public class NeblioBlock : Block
{
public NeblioBlock(NeblioBlockHeader h) : base(h)
{
}
public override ConsensusFactory GetConsensusFactory()
{
return Instance.Mainnet.Consensus.ConsensusFactory;
}
}
public class NeblioTransaction : Transaction
{
public NeblioTransaction(ConsensusFactory consensusFactory)
{
_Factory = consensusFactory;
}
ConsensusFactory _Factory;
public override ConsensusFactory GetConsensusFactory()
{
return _Factory;
}
/// <summary>
/// POS Timestamp
/// </summary>
public uint Time { get; set; } = Utils.DateTimeToUnixTime(DateTime.UtcNow);
public override uint256 GetSignatureHash(Script scriptCode, int nIn, SigHash nHashType, TxOut spentOutput, HashVersion sigversion, PrecomputedTransactionData precomputedTransactionData)
{
if (sigversion == HashVersion.WitnessV0)
{
if (spentOutput?.Value == null || spentOutput.Value == TxOut.NullMoney)
throw new ArgumentException("The output being signed with the amount must be provided", nameof(spentOutput));
uint256 hashPrevouts = uint256.Zero;
uint256 hashSequence = uint256.Zero;
uint256 hashOutputs = uint256.Zero;
if ((nHashType & SigHash.AnyoneCanPay) == 0)
{
hashPrevouts = precomputedTransactionData == null ?
GetHashPrevouts() : precomputedTransactionData.HashPrevouts;
}
if ((nHashType & SigHash.AnyoneCanPay) == 0 && ((uint)nHashType & 0x1f) != (uint)SigHash.Single && ((uint)nHashType & 0x1f) != (uint)SigHash.None)
{
hashSequence = precomputedTransactionData == null ?
GetHashSequence() : precomputedTransactionData.HashSequence;
}
if (((uint)nHashType & 0x1f) != (uint)SigHash.Single && ((uint)nHashType & 0x1f) != (uint)SigHash.None)
{
hashOutputs = precomputedTransactionData == null ?
GetHashOutputs() : precomputedTransactionData.HashOutputs;
}
else if (((uint)nHashType & 0x1f) == (uint)SigHash.Single && nIn < this.Outputs.Count)
{
BitcoinStream ss = CreateHashWriter(sigversion);
ss.ReadWrite(this.Outputs[nIn]);
hashOutputs = GetHash(ss);
}
BitcoinStream sss = CreateHashWriter(sigversion);
// Version
sss.ReadWrite(this.Version);
sss.ReadWrite(this.Time); // Neblio Timestamp!!!
// Input prevouts/nSequence (none/all, depending on flags)
sss.ReadWrite(hashPrevouts);
sss.ReadWrite(hashSequence);
// The input being signed (replacing the scriptSig with scriptCode + amount)
// The prevout may already be contained in hashPrevout, and the nSequence
// may already be contain in hashSequence.
sss.ReadWrite(Inputs[nIn].PrevOut);
sss.ReadWrite(scriptCode);
sss.ReadWrite(spentOutput.Value.Satoshi);
sss.ReadWrite((uint)Inputs[nIn].Sequence);
// Outputs (none/one/all, depending on flags)
sss.ReadWrite(hashOutputs);
// Locktime
sss.ReadWriteStruct(LockTime);
// Sighash type
sss.ReadWrite((uint)nHashType);
return GetHash(sss);
}
bool fAnyoneCanPay = (nHashType & SigHash.AnyoneCanPay) != 0;
bool fHashSingle = ((byte)nHashType & 0x1f) == (byte)SigHash.Single;
bool fHashNone = ((byte)nHashType & 0x1f) == (byte)SigHash.None;
if (nIn >= Inputs.Count)
{
return uint256.One;
}
if (fHashSingle)
{
if (nIn >= Outputs.Count)
{
return uint256.One;
}
}
var stream = CreateHashWriter(sigversion);
stream.ReadWrite(Version);
stream.ReadWrite(this.Time); // Neblio Timestamp!!!
uint nInputs = (uint)(fAnyoneCanPay ? 1 : Inputs.Count);
stream.ReadWriteAsVarInt(ref nInputs);
for (int nInput = 0; nInput < nInputs; nInput++)
{
if (fAnyoneCanPay)
nInput = nIn;
stream.ReadWrite(Inputs[nInput].PrevOut);
if (nInput != nIn)
{
stream.ReadWrite(Script.Empty);
}
else
{
WriteScriptCode(stream, scriptCode);
}
if (nInput != nIn && (fHashSingle || fHashNone))
stream.ReadWrite((uint)0);
else
stream.ReadWrite((uint)Inputs[nInput].Sequence);
}
uint nOutputs = (uint)(fHashNone ? 0 : (fHashSingle ? nIn + 1 : Outputs.Count));
stream.ReadWriteAsVarInt(ref nOutputs);
for (int nOutput = 0; nOutput < nOutputs; nOutput++)
{
if (fHashSingle && nOutput != nIn)
{
this.Outputs.CreateNewTxOut().ReadWrite(stream);
}
else
{
Outputs[nOutput].ReadWrite(stream);
}
}
stream.ReadWriteStruct(LockTime);
stream.ReadWrite((uint)nHashType);
return GetHash(stream);
}
private static uint256 GetHash(BitcoinStream stream)
{
var preimage = ((HashStreamBase)stream.Inner).GetHash();
stream.Inner.Dispose();
return preimage;
}
private static void WriteScriptCode(BitcoinStream stream, Script scriptCode)
{
int nCodeSeparators = 0;
var reader = scriptCode.CreateReader();
OpcodeType opcode;
while (reader.TryReadOpCode(out opcode))
{
if (opcode == OpcodeType.OP_CODESEPARATOR)
nCodeSeparators++;
}
uint n = (uint)(scriptCode.Length - nCodeSeparators);
stream.ReadWriteAsVarInt(ref n);
reader = scriptCode.CreateReader();
int itBegin = 0;
while (reader.TryReadOpCode(out opcode))
{
if (opcode == OpcodeType.OP_CODESEPARATOR)
{
stream.Inner.Write(scriptCode.ToBytes(true), itBegin, (int)(reader.Inner.Position - itBegin - 1));
itBegin = (int)reader.Inner.Position;
}
}
if (itBegin != scriptCode.Length)
stream.Inner.Write(scriptCode.ToBytes(true), itBegin, (int)(reader.Inner.Position - itBegin));
}
public override void ReadWrite(BitcoinStream stream)
{
if (stream.Serializing)
SerializeTxn(stream);
else
DeserializeTxn(stream);
}
private void DeserializeTxn(BitcoinStream stream)
{
UInt32 nVersionTemp = 0;
stream.ReadWrite(ref nVersionTemp);
// POS time stamp
UInt32 nTimeTemp = 0;
stream.ReadWrite(ref nTimeTemp);
TxInList vinTemp = null;
TxOutList voutTemp = null;
// Try to read the vin.
stream.ReadWrite(ref vinTemp);
vinTemp.Transaction = this;
// Assume a normal vout follows.
stream.ReadWrite(ref voutTemp);
voutTemp.Transaction = this;
LockTime lockTimeTemp = LockTime.Zero;
stream.ReadWriteStruct(ref lockTimeTemp);
this.Version = nVersionTemp;
this.Time = nTimeTemp; // POS Timestamp
vinTemp.ForEach(i => this.Inputs.Add(i));
voutTemp.ForEach(i => this.Outputs.Add(i));
this.LockTime = lockTimeTemp;
}
private void SerializeTxn(BitcoinStream stream)
{
var version = this.Version;
stream.ReadWrite(ref version);
// POS Timestamp
var time = this.Time;
stream.ReadWrite(ref time);
TxInList vin = this.Inputs;
stream.ReadWrite(ref vin);
vin.Transaction = this;
TxOutList vout = this.Outputs;
stream.ReadWrite(ref vout);
vout.Transaction = this;
LockTime lockTime = this.LockTime;
stream.ReadWriteStruct(ref lockTime);
}
public string Hex { get; set; } = string.Empty;
private static void DeserializeFromJson(JObject json, ref NeblioTransaction tx)
{
tx.Version = json.Value<uint>("version");
//tx.Time = json.Value<uint>("time");
var tm = json.Value<double>("time");
tx.Time = Convert.ToUInt32(tm/1000);
tx.LockTime = json.Value<uint>("locktime");
tx.Hex = json.Value<string>("hex");
var vin = json.Value<JArray>("vin");
for (int i = 0; i < vin.Count; i++)
{
var jsonIn = (JObject)vin[i];
var txin = new TxIn();
var script = jsonIn.Value<JObject>("scriptSig");
if (script != null)
{
txin.ScriptSig = new Script(Encoders.Hex.DecodeData(script.Value<string>("hex")));
txin.PrevOut.Hash = uint256.Parse(jsonIn.Value<string>("txid"));
txin.PrevOut.N = jsonIn.Value<uint>("vout");
}
else
{
txin.ScriptSig = new Script(Encoders.Hex.DecodeData(jsonIn.Value<string>("coinbase")));
}
txin.Sequence = jsonIn.Value<uint>("sequence");
tx.Inputs.Add(txin);
}
var vout = json.Value<JArray>("vout");
for (int i = 0; i < vout.Count; i++)
{
var jsonOut = (JObject)vout[i];
var txout = new TxOut()
{
Value = Money.Coins(jsonOut.Value<decimal>("value")/100000000)
};
tx.Outputs.Add(txout);
var script = jsonOut.Value<JObject>("scriptPubKey");
txout.ScriptPubKey = new Script(Encoders.Hex.DecodeData(script.Value<string>("hex")));
}
}
}
#pragma warning restore CS0618 // Type or member is obsolete
protected override void PostInit()
{
RegisterDefaultCookiePath("NEBL");
}
protected override NetworkBuilder CreateMainnet()
{
var builder = new NetworkBuilder();
builder.SetConsensus(new Consensus
{
PowLimit = new Target(0 >> 1),
MinimumChainWork = new uint256("0x000000000000000000000000000000000000000000000000000a0c3931735170"),
PowTargetTimespan = TimeSpan.FromSeconds(2 * 60 * 60),
PowTargetSpacing = TimeSpan.FromSeconds(30),
PowAllowMinDifficultyBlocks = false,
CoinbaseMaturity = 120,
PowNoRetargeting = false,
ConsensusFactory = NeblioConsensusFactory.Instance,
SupportSegwit = false,
CoinType = 146 // BIP44 CoinType
})
.SetBase58Bytes(Base58Type.PUBKEY_ADDRESS, new byte[] { 53 }) // 0x35
.SetBase58Bytes(Base58Type.SCRIPT_ADDRESS, new byte[] { 112 }) // 0x70
.SetBase58Bytes(Base58Type.SECRET_KEY, new byte[] { 181 }) // 0xb5
.SetBase58Bytes(Base58Type.EXT_PUBLIC_KEY, new byte[] { 0x04, 0x88, 0xB2, 0x1E })
.SetBase58Bytes(Base58Type.EXT_SECRET_KEY, new byte[] { 0x04, 0x88, 0xAD, 0xE4 })
.SetMagic(0x325e6f86)
.SetPort(6325)
.SetRPCPort(6326)
.SetMaxP2PVersion(60320)
.SetName("Neblio-main")
.AddAlias("Neblio-mainnet")
.AddDNSSeeds(new[]
{
new DNSSeedData("seed.nebl.io", "seed.nebl.io"),
new DNSSeedData("seed2.nebl.io", "seed2.nebl.io"),
})
.AddSeeds(new NetworkAddress[0])
.SetGenesis("010000000000000000000000000000000000000000000000000000000000000000000000e7ae9132c789d33c38b735ae562ef57c9780c7328b0d1cb0121a321432d13f20137a7259ffff7f20252100000101000000137a7259010000000000000000000000000000000000000000000000000000000000000000ffffffff2900012a2532316a756c32303137202d204e65626c696f204669727374204e6574204c61756e63686573ffffffff010000000000000000000000000000");
return builder;
}
protected override NetworkBuilder CreateTestnet()
{
var builder = new NetworkBuilder();
builder.SetConsensus(new Consensus
{
PowLimit = new Target(0 >> 1),
MinimumChainWork = new uint256("0x000000000000000000000000000000000000000000000000000a0c3931735170"),
PowTargetTimespan = TimeSpan.FromSeconds(2 * 60 * 60),
PowTargetSpacing = TimeSpan.FromSeconds(30),
PowAllowMinDifficultyBlocks = true,
CoinbaseMaturity = 120,
PowNoRetargeting = false,
ConsensusFactory = NeblioConsensusFactory.Instance,
SupportSegwit = false,
CoinType = 146
})
.SetBase58Bytes(Base58Type.PUBKEY_ADDRESS, new byte[] { 65 }) // 0x41
.SetBase58Bytes(Base58Type.SCRIPT_ADDRESS, new byte[] { 127 }) // 0x7f
.SetBase58Bytes(Base58Type.SECRET_KEY, new byte[] { 193 }) // 0xc1
.SetBase58Bytes(Base58Type.EXT_PUBLIC_KEY, new byte[] { 0x04, 0x35, 0x87, 0xCF })
.SetBase58Bytes(Base58Type.EXT_SECRET_KEY, new byte[] { 0x04, 0x35, 0x83, 0x94 })
.SetMagic(0x1bba63c5)
.SetPort(16325)
.SetRPCPort(16326)
.SetMaxP2PVersion(60320)
.SetName("Neblio-test")
.AddAlias("Neblio-testnet")
.AddDNSSeeds(new[]
{
new DNSSeedData("testnet-seed.nebl.io", "testnet-seed.nebl.io"),
})
.AddSeeds(new NetworkAddress[0])
.SetGenesis("010000000000000000000000000000000000000000000000000000000000000000000000e7ae9132c789d33c38b735ae562ef57c9780c7328b0d1cb0121a321432d13f20137a7259ffff7f20252100000101000000137a7259010000000000000000000000000000000000000000000000000000000000000000ffffffff2900012a2532316a756c32303137202d204e65626c696f204669727374204e6574204c61756e63686573ffffffff010000000000000000000000000000");
return builder;
}
protected override NetworkBuilder CreateRegtest()
{
var builder = new NetworkBuilder();
var res = builder.SetConsensus(new Consensus
{
PowLimit = new Target(0 >> 1),
MinimumChainWork = new uint256("0000000000000000000000000000000000000000000000000000000000000000"),
PowTargetTimespan = TimeSpan.FromSeconds(2 * 60 * 60),
PowTargetSpacing = TimeSpan.FromSeconds(30),
PowAllowMinDifficultyBlocks = false,
CoinbaseMaturity = 10,
PowNoRetargeting = false,
ConsensusFactory = NeblioConsensusFactory.Instance,
SupportSegwit = false,
CoinType = 146
})
.SetBase58Bytes(Base58Type.PUBKEY_ADDRESS, new byte[] { 65 }) // 0x41
.SetBase58Bytes(Base58Type.SCRIPT_ADDRESS, new byte[] { 127 }) // 0x7f
.SetBase58Bytes(Base58Type.SECRET_KEY, new byte[] { 193 }) // 0xc1
.SetBase58Bytes(Base58Type.EXT_PUBLIC_KEY, new byte[] { 0x04, 0x35, 0x87, 0xCF })
.SetBase58Bytes(Base58Type.EXT_SECRET_KEY, new byte[] { 0x04, 0x35, 0x83, 0x94 })
.SetMagic(0xcdf3e0ee)
.SetPort(26325)
.SetRPCPort(26326)
.SetMaxP2PVersion(60320)
.SetName("Neblio-reg")
.AddAlias("Neblio-regtest")
.AddDNSSeeds(new DNSSeedData[0])
.AddSeeds(new NetworkAddress[0])
.SetGenesis("010000000000000000000000000000000000000000000000000000000000000000000000e7ae9132c789d33c38b735ae562ef57c9780c7328b0d1cb0121a321432d13f20137a7259ffff7f20252100000101000000137a7259010000000000000000000000000000000000000000000000000000000000000000ffffffff2900012a2532316a756c32303137202d204e65626c696f204669727374204e6574204c61756e63686573ffffffff010000000000000000000000000000");
return builder;
}
}
}