Skip to content

Commit b765690

Browse files
committed
chg: Moved all examples under sample driver app.
1 parent 1219c3b commit b765690

File tree

12 files changed

+237
-261
lines changed

12 files changed

+237
-261
lines changed

HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Release History
33

44
.. :changelog:
55
6+
67
v0.2.0 (2017-02-04)
78
-------------------------
89

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ recursive-include ./tests *.py
1313

1414
exclude .cookiecutter.yml
1515
exclude .gitchangelog.rc
16+
exclude TODO
1617
global-exclude __pycache__
1718
global-exclude .editorconfig
1819
global-exclude *.py[co]

bin/sample.py

Lines changed: 0 additions & 92 deletions
This file was deleted.

clique/__main__.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

clique/app/__main__.py

Lines changed: 2 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
# -*- coding: utf-8 -*-
2-
import json
32
from nicfit import Application, getLogger
43
from argparse import RawDescriptionHelpFormatter
54

65
from ..__about__ import __version__
76
from .. import useCliqueServer
87
from . import keygen # noqa
98
from . import identity # noqa
9+
from . import blockchain # noqa
10+
from . import examples # noqa
1011

1112
log = getLogger(__name__)
1213

1314

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-
2115
def main(args):
2216
log.debug("Args: {}".format(args))
2317

@@ -41,143 +35,13 @@ def main(args):
4135
Use '%(prog)s <subcmd> --help' for detailed info about a particular command.
4236
""" # noqa
4337

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-
16238
argparse_opts = {"epilog": EPILOG,
16339
"formatter_class": RawDescriptionHelpFormatter,
16440
}
16541
app = Application(main, name="clique", version=__version__,
16642
extra_arg_parser_opts=argparse_opts)
16743
app.arg_parser.add_argument("--server", metavar="URL",
16844
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-
"""
18145
app.enableCommands(title="Subcommands", dest="subcmd")
18246

18347
if __name__ == "__main__": # pragma: nocover

bin/example.py renamed to clique/app/_authchain_example.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33
import sys
4-
from nose.tools import *
4+
from nose.tools import assert_equals, assert_dict_equal
55

66
from jwcrypto.jwk import JWK
77
from clique.authchain import Grant
88
from clique import Identity, IdentityChain, AuthChain, keystore, chainstore
9-
from clique import useCliqueServer
9+
from clique import useCliqueServer # noqa
1010

1111

1212
def main():
13-
#useCliqueServer("https://outer-planes.net/clique")
13+
#useCliqueServer("https://outer-planes.net/clique") # noqa
1414

1515
for id in global_identities:
1616
keystore().add(id.key)
@@ -144,7 +144,7 @@ def authchain():
144144

145145

146146
alice = Identity("acct:[email protected]",
147-
JWK(**{#"cmt": "alice",
147+
JWK(**{#"cmt": "alice", # noqa
148148
"crv": "P-256",
149149
"d": "z-CuqNSEkOyoXWlGcoqgaq_nh6WlgtcV8ZroeNQ5AS0",
150150
"kid": "ZansZqsUXsTucfJC6t3ApBXAsXfR4spy4MmFOHek5Qc",
@@ -154,7 +154,7 @@ def authchain():
154154
"y": "xkJVvFpTuTXou0xkXpiVlsldjH10Qj5831ZJbrFHNPQ"
155155
}))
156156
bob = Identity("acct:[email protected]",
157-
JWK(**{#"cmt": "bob",
157+
JWK(**{#"cmt": "bob", # noqa
158158
"crv": "P-256",
159159
"d": "fgug1klBZUQr83wVDVwLj9odTOwfTQ9wbTw2rMZyhJQ",
160160
"kid": "SETKquASXDG0k5ERRAa4kvXNAGGWfkZxvfEOxks9tHU",
@@ -164,7 +164,7 @@ def authchain():
164164
"y": "CCnBEEsnzAMU0YbyFRrDPuHoDHGD6mt54RwWtLH8e3E"
165165
}))
166166
jack = Identity("acct:[email protected]",
167-
JWK(**{#"cmt": "jack",
167+
JWK(**{#"cmt": "jack", # noqa
168168
"crv": "P-256",
169169
"d": "0FYEyIkPZT3xAKh226266nqUg5e9NWl88-TFn0pLcmY",
170170
"kid": "OsY_R82yHLKXxVVZl-f5aBaqpN4yH9wSyeLwxTy_8Lo",
@@ -174,7 +174,7 @@ def authchain():
174174
"y": "5digz6ao89Kw9p50VRk9P0SbbKwrqwQdBMqoKhjTCu8"
175175
}))
176176
diane = Identity("acct:[email protected]",
177-
JWK(**{#"cmt": "diane",
177+
JWK(**{#"cmt": "diane", # noqa
178178
"crv": "P-256",
179179
"d": "xqoqU02XMyVQfTYEwLDt1kNGM9IOqvfrbMk-K-c_thM",
180180
"kid": "2PgeWCJIN2Btmp_FcfIqephBFAYmJ8Gp0v1XZ0Gm-c8",
@@ -184,7 +184,7 @@ def authchain():
184184
"y": "2i4A23HuPwY7yu_p9-sb3xM9uGQkBD7jxOJ20Vx1mzY"
185185
}))
186186
steve = Identity("acct:[email protected]",
187-
JWK(**{#"cmt": "steve",
187+
JWK(**{#"cmt": "steve", # noqa
188188
"crv": "P-256",
189189
"d": "5yOV5PUmopGZUEZ1QJcnwvb8dMQaBv275h87QaK1U3o",
190190
"kid": "NP7aCbQTrZEKFfnNYxU1mPgDdYLokQt13cpFAEE7zAk",
@@ -199,4 +199,3 @@ def authchain():
199199

200200
if __name__ == "__main__":
201201
sys.exit(main() or 0)
202-

0 commit comments

Comments
 (0)