1
1
# -*- coding: utf-8 -*-
2
- import json
3
2
from nicfit import Application , getLogger
4
3
from argparse import RawDescriptionHelpFormatter
5
4
6
5
from ..__about__ import __version__
7
6
from .. import useCliqueServer
8
7
from . import keygen # noqa
9
8
from . import identity # noqa
9
+ from . import blockchain # noqa
10
+ from . import examples # noqa
10
11
11
12
log = getLogger (__name__ )
12
13
13
14
14
- def blockchain (args ):
15
- from .. import BlockChain , chainFactory
16
- chain_data = args .chainfile .read ()
17
- chain = BlockChain .deserialize (chain_data , factory = chainFactory )
18
- print (chain )
19
-
20
-
21
15
def main (args ):
22
16
log .debug ("Args: {}" .format (args ))
23
17
@@ -41,143 +35,13 @@ def main(args):
41
35
Use '%(prog)s <subcmd> --help' for detailed info about a particular command.
42
36
""" # noqa
43
37
44
-
45
- def contract_example (args ):
46
- from uuid import uuid4
47
- import requests
48
- from clique .blockchain import BlockChain , Block , Identity
49
- from clique import keystore
50
-
51
- ipecac = Identity ("label:Ipecac" , Identity .generateKey ())
52
- patton = Identity ("artist:Mike Patton" , Identity .generateKey ())
53
- melvins = Identity ("artist:Melvins" , Identity .generateKey ())
54
- fantômas = Identity ("artist:Fantômas" , Identity .generateKey ())
55
- buzzo = Identity ("artist:King Buzzo" , Identity .generateKey ())
56
- unsane = Identity ("artist:Unsane" , Identity .generateKey ())
57
- fnm = Identity ("artist:Faith No More" , Identity .generateKey ())
58
- for k in [i .key for i in [ipecac , patton , melvins , fantômas , buzzo ,
59
- unsane , fnm ]]:
60
- keystore ().upload (k )
61
-
62
- c = BlockChain (ipecac )
63
- godblock = c .addBlock (ipecac ,
64
- sub = "Ipecac recording artists: " + str (uuid4 ()),
65
- tid = "GOD" )
66
- godblock .verify (ipecac .key )
67
- contract = c .addBlock (patton , thing = "contract" , blahblah = "...." )
68
- contract .verify (patton .key )
69
- # Multiple signers
70
- c .addBlock (fantômas , thing = "contract" , blahblah = "...." )
71
- # XXX: Multi signing not yet suported
72
- '''
73
- fantômas_contract.sign(patton.key)
74
- fantômas_contract.sign(melvins.key)
75
- fantômas_contract.sign(buzzo.key)
76
- '''
77
-
78
- print (c )
79
- GHASH = godblock .hash
80
-
81
- ######################################################
82
- CONTRACT_BLOCK_CHAIN = c .serialize ()
83
- print (CONTRACT_BLOCK_CHAIN )
84
-
85
- ipecac_contracts = BlockChain .deserialize (CONTRACT_BLOCK_CHAIN )
86
- ipecac_contracts .addBlock (buzzo , thing = "contract" , blahblah = "...." )
87
- ipecac_contracts .addBlock (melvins , thing = "contract" , blahblah = "...." )
88
-
89
- NEW_CHAIN = ipecac_contracts .serialize ()
90
- for new_block in ipecac_contracts [- 2 :]:
91
- # upload to block server, for example
92
- pass
93
-
94
- ######################################################
95
- download = NEW_CHAIN
96
- melvins_crew = BlockChain .deserialize (download )
97
- melvins_crew .validate (GHASH )
98
- print (melvins_crew )
99
- # += instead of addBlock, antecedents are computed as whith addBlock
100
- melvins_crew += Block (ipecac , None , ack = True ,
101
- ptk = "FIXME: get fprint from block being acked" )
102
- melvins_crew += Block (ipecac , None , ack = True ,
103
- ptk = "FIXME: get fprint from block being acked" )
104
- print (melvins_crew )
105
- CONTRACT_BLOCK_CHAIN = melvins_crew .serialize ()
106
-
107
- master = BlockChain .deserialize (CONTRACT_BLOCK_CHAIN )
108
- master .addBlock (ipecac , thing = "contract:offer" , new_signing = "Unsane" ,
109
- blahblah = "...." )
110
- master .addBlock (ipecac , thing = "contract:offer" , new_signing = "Faith No More" ,
111
- blahblah = "...." )
112
- CONTRACT_BLOCK_CHAIN = master .serialize ()
113
-
114
- ######################################################
115
- download = CONTRACT_BLOCK_CHAIN
116
- fnm_offer = BlockChain .deserialize (download )
117
- print (fnm_offer )
118
- fnm_offer .validate (GHASH )
119
- fnm_offer .addBlock (fnm , ack = False )
120
- deny_upload = fnm_offer .serialize ()
121
-
122
- #####################################################
123
- download = CONTRACT_BLOCK_CHAIN
124
- unsane_offer = BlockChain .deserialize (download )
125
- print (unsane_offer )
126
- unsane_offer .validate (GHASH )
127
- unsane_offer .addBlock (unsane , ack = True )
128
- accept_upload = unsane_offer .serialize ()
129
-
130
- ######################################################
131
-
132
- yes_from_unsane = BlockChain .deserialize (accept_upload )
133
- yes_from_unsane .validate (GHASH )
134
- no_from_ftm = BlockChain .deserialize (deny_upload )
135
- yes_from_unsane .validate (GHASH )
136
-
137
- # XXX: at this point there is a merge op
138
- print (yes_from_unsane )
139
- print (no_from_ftm )
140
-
141
- with open ("sample.json" , "w" ) as fp :
142
- fp .write (CONTRACT_BLOCK_CHAIN )
143
-
144
- if args .server :
145
- h = {"content-type" : "application/jose" }
146
- new_chain = BlockChain .deserialize (CONTRACT_BLOCK_CHAIN )
147
- for block in new_chain :
148
- print ("UPLOADING:" , block )
149
- resp = requests .post (args .server + "/blocks" , headers = h ,
150
- data = block .serialize (),
151
- timeout = 5 )
152
- if resp .status_code != 201 :
153
- log .error (resp )
154
- raise requests .RequestException (response = resp )
155
-
156
- resp = requests .get (args .server + "/chains/" +
157
- new_chain [0 ].payload ["sub" ])
158
- downloaded_chain = BlockChain .deserialize (json .dumps (resp .json ()))
159
- downloaded_chain .validate (new_chain [0 ].hash )
160
-
161
-
162
38
argparse_opts = {"epilog" : EPILOG ,
163
39
"formatter_class" : RawDescriptionHelpFormatter ,
164
40
}
165
41
app = Application (main , name = "clique" , version = __version__ ,
166
42
extra_arg_parser_opts = argparse_opts )
167
43
app .arg_parser .add_argument ("--server" , metavar = "URL" ,
168
44
help = "URL of remote key server." )
169
- # blockchain
170
- """
171
- #blockchain_parser = sub_parser.add_parser("blockchain",
172
- # help="Read blockchains.")
173
- #blockchain_parser.add_argument("chainfile", metavar="FILE",
174
- # type=FileType('r'),
175
- # help="File containing serialized block chain.")
176
- #blockchain_parser.set_defaults(func=blockchain)
177
- #
178
- #contract_parser = sub_parser.add_parser("contract_example", help="Toy")
179
- #contract_parser.set_defaults(func=contract_example)
180
- """
181
45
app .enableCommands (title = "Subcommands" , dest = "subcmd" )
182
46
183
47
if __name__ == "__main__" : # pragma: nocover
0 commit comments