-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.py
75 lines (62 loc) · 2.88 KB
/
interface.py
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
# This is the "front end" of our application
# In the interest of time, it uses the CLI to interact with the chaincode
import hashlib
import os
from cmd import Cmd
class MyPrompt(Cmd):
def do_sign_document(self, args):
"""Sign a document
Arguments: document_name string, signature string
"""
docName = "";
signature = "";
splitArgs = args.split(" ")
if len(splitArgs) < 2:
print "Need to provide document name and signature."
elif len(splitArgs) == 2:
docName = splitArgs[0]
signature = splitArgs[1]
print "DocName is " + docName
print "Signature is " + signature
hashedDoc = hashPDF(docName)
print hashedDoc
signDoc(hashedDoc, signature)
else:
print "Wrong number of arguments."
def do_query_document(self, args):
splitArgs = args.split(" ")
docName = splitArgs[0]
queryDoc(docName)
def do_quit(self, args):
"""Quits the program."""
print "Quitting."
raise SystemExit
def hashPDF(docName):
"""Hash a PDF document"""
BLOCKSIZE = 65536
hasher = hashlib.sha1()
with open(docName, 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
return hasher.hexdigest()
def signDoc(hashedDoc, signature):
"""Put the hashed document in the database"""
os.system("export FABRIC_CFG_PATH=$PWD")
signCommand = "docker exec -it cli bash -c 'export CHANNEL_NAME=signchannel && echo $CHANNEL_NAME && peer chaincode invoke -o orderer.docsign.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/docsign.com/orderers/orderer.docsign.com/msp/tlscacerts/tlsca.docsign.com-cert.pem -C $CHANNEL_NAME -n doccc -c" + " '\\''" + "{"'"'"Args"'"'":["'"'"put"'"'", "'"'" " + hashedDoc + ""'"'", "'"'"" + signature + ""'"'"]}" + "'\\''" + "&& exit'"
print signCommand
os.system(signCommand)
def queryDoc(docName):
"""Query the database for information on a doc"""
hashedDoc = hashPDF(docName)
os.system("export FABRIC_CFG_PATH=$PWD")
signCommand = "docker exec -it cli bash -c 'export CHANNEL_NAME=signchannel && echo $CHANNEL_NAME && peer chaincode invoke -o orderer.docsign.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/docsign.com/orderers/orderer.docsign.com/msp/tlscacerts/tlsca.docsign.com-cert.pem -C $CHANNEL_NAME -n doccc -c" + " '\\''" + "{"'"'"Args"'"'":["'"'"get"'"'", "'"'" " + hashedDoc + ""'"'"]}" + "'\\''" + "&& exit'"
print signCommand
os.system(signCommand)
def openCLI():
print "calling openCLI"
if __name__ == '__main__':
prompt = MyPrompt()
prompt.prompt = '> '
prompt.cmdloop('Welcome to Document Signing on Blockchain...')