-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
120 lines (107 loc) · 4.13 KB
/
Program.cs
File metadata and controls
120 lines (107 loc) · 4.13 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
111
112
113
114
115
116
117
118
119
120
//ReSharper disable All
using System;
using System.Collections.Generic;
using System.Linq;
using NBitcoin;
using NBitcoin.OpenAsset;
namespace _4._2LiquidDemocracy
{
internal class Program
{
private static void Main()
{
var powerCoin = new Key();
var alice = new Key();
var bob = new Key();
var satoshi = new Key();
var init = new Transaction()
{
Outputs =
{
new TxOut(Money.Coins(1.0m), powerCoin),
new TxOut(Money.Coins(1.0m), alice),
new TxOut(Money.Coins(1.0m), bob),
new TxOut(Money.Coins(1.0m), satoshi),
}
};
var repo = new NoSqlColoredTransactionRepository();
repo.Transactions.Put(init);
var issuance = GetCoins(init, powerCoin)
.Select(c => new IssuanceCoin(c))
.ToArray();
var builder = new TransactionBuilder();
var toAlice =
builder
.AddCoins(issuance)
.AddKeys(powerCoin)
.IssueAsset(alice, new AssetMoney(powerCoin, 2))
.SetChange(powerCoin)
.Then()
.AddCoins(GetCoins(init, alice))
.AddKeys(alice)
.Send(alice, Money.Coins(0.2m))
.SetChange(alice)
.BuildTransaction(true);
repo.Transactions.Put(toAlice);
var votingCoin = new Key();
var init2 = new Transaction()
{
Outputs =
{
new TxOut(Money.Coins(1.0m), votingCoin),
}
};
repo.Transactions.Put(init2);
issuance = GetCoins(init2, votingCoin).Select(c => new IssuanceCoin(c)).ToArray();
builder = new TransactionBuilder();
var toVoters =
builder
.AddCoins(issuance)
.AddKeys(votingCoin)
.IssueAsset(alice, new AssetMoney(votingCoin, 1))
.IssueAsset(satoshi, new AssetMoney(votingCoin, 1))
.SetChange(votingCoin)
.BuildTransaction(true);
repo.Transactions.Put(toVoters);
var aliceVotingCoin = ColoredCoin.Find(toVoters, repo)
.Where(c => c.ScriptPubKey == alice.ScriptPubKey)
.ToArray();
builder = new TransactionBuilder();
var toBob =
builder
.AddCoins(aliceVotingCoin)
.AddKeys(alice)
.SendAsset(bob, new AssetMoney(votingCoin, 1))
.BuildTransaction(true);
repo.Transactions.Put(toBob);
var bobVotingCoin = ColoredCoin.Find(toVoters, repo)
.Where(c => c.ScriptPubKey == bob.ScriptPubKey)
.ToArray();
builder = new TransactionBuilder();
var vote =
builder
.AddCoins(bobVotingCoin)
.AddKeys(bob)
.SendAsset(BitcoinAddress.Create("1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN"),
new AssetMoney(votingCoin, 1))
.BuildTransaction(true);
issuance = GetCoins(init2, votingCoin).Select(c => new IssuanceCoin(c)).ToArray();
issuance[0].DefinitionUrl = new Uri("http://boss.com/vote01.json");
builder = new TransactionBuilder();
toVoters =
builder
.AddCoins(issuance)
.AddKeys(votingCoin)
.IssueAsset(alice, new AssetMoney(votingCoin, 1))
.IssueAsset(satoshi, new AssetMoney(votingCoin, 1))
.SetChange(votingCoin)
.BuildTransaction(true);
repo.Transactions.Put(toVoters);
Console.ReadLine();
}
private static IEnumerable<Coin> GetCoins(Transaction tx, Key owner)
{
return tx.Outputs.AsCoins().Where(c => c.ScriptPubKey == owner.ScriptPubKey);
}
}
}