forked from etotheipi/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 172
/
signannounce.py
233 lines (165 loc) · 7.09 KB
/
signannounce.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
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
from __future__ import print_function
################################################################################
# #
# Copyright (C) 2011-2015, Armory Technologies, Inc. #
# Distributed under the GNU Affero General Public License (AGPL v3) #
# See LICENSE or http://www.gnu.org/licenses/agpl.html #
# #
################################################################################
import sys
import os
import getpass
import shutil
sys.path.append('..')
sys.path.append('/usr/lib/armory')
from armoryengine.ALL import *
from jasvet import ASv1CS, readSigBlock, verifySignature
def signAnnounceFiles(wltPath):
if len(CLI_ARGS)<1:
print('Must specify a wallet file containing the signing key')
exit(1)
wltPath = CLI_ARGS[0]
if not os.path.exists(wltPath):
print('Wallet file was not found (%s)' % wltPath)
exit(1)
inDir = 'unsignedAnnounce'
outDir = 'signedAnnounce'
origDLFile = os.path.join(inDir, 'dllinks.txt')
newDLFile = os.path.join(inDir, 'dllinks_temp.txt')
################################################################################
################################################################################
# We may need to compute some installer hashes
doComputeDLLinks = (len(CLI_ARGS)>1)
if doComputeDLLinks:
instDir = CLI_ARGS[1]
if not os.path.exists(instDir):
print('Installers dir does not exist!')
exit(1)
instFileInfo = {}
with open('dlmap.txt','r') as f:
dlmapLines = [line.strip() for line in f.readlines()]
verName, verStr, urlPrefix = dlmapLines[0].split()
for line in dlmapLines[2:]:
fn,opsys,osver,osarch = line.replace('%ver',verStr).split()
instFileInfo[fn] = [opsys, osver, osarch]
OS,OSVER,OSARCH = range(3)
with open(newDLFile, 'w') as f:
# Write the existing data from rawfiles/dllinks into temp file
f.write(open(origDLFile, 'r').read())
f.write('\n')
# Now compute hashes of each installer and write info to temp file
for fn in os.listdir(instDir):
fullpath = os.path.join(instDir, fn)
if not fn in instFileInfo:
print('File in installer directory does not match any in info map')
print(' File:', fullpath)
print(' InfoMap:')
for filename in instFileInfo:
print(' ', filename)
exit(1)
fdata = open(fullpath, 'rb').read()
fhash = binary_to_hex(sha256(fdata))
outputStr = [verName,
verStr,
instFileInfo[fn][OS],
instFileInfo[fn][OSVER],
instFileInfo[fn][OSARCH],
os.path.join(urlPrefix, fn),
fhash]
f.write(' '.join(outputStr) + '\n')
################################################################################
###
if CLI_OPTIONS.testAnnounceCode:
signAddress = '1PpAJyNoocJt38Vcf4AfPffaxo76D4AAEe'
announceName = 'testannounce.txt'
pathPrefix = 'https://s3.amazonaws.com/bitcoinarmory-testing/'
else:
signAddress = '1NWvhByxfTXPYNT4zMBmEY3VL8QJQtQoei'
announceName = 'announce.txt'
pathPrefix = 'https://s3.amazonaws.com/bitcoinarmory-media/'
announcePath = os.path.join(outDir, announceName)
###
wlt = PyBtcWallet().readWalletFile(wltPath)
if not wlt.hasAddr(signAddress):
print('Supplied wallet does not have the correct signing key')
exit(1)
print('Must unlock wallet to sign the announce file...')
while True:
passwd = SecureBinaryData(getpass.getpass('Wallet passphrase: '))
if not wlt.verifyPassphrase(passwd):
print('Invalid passphrase!')
continue
break
wlt.unlock(securePassphrase=passwd)
passwd.destroy()
addrObj = wlt.getAddrByHash160(addrStr_to_hash160(signAddress)[1])
def doSignFile(inFile, outFile):
with open(inFile, 'rb') as f:
sigBlock = ASv1CS(addrObj.binPrivKey32_Plain.toBinStr(), f.read())
with open(outFile, 'wb') as f:
f.write(sigBlock)
fileMappings = {}
longestID = 0
longestURL = 0
print('Reading file mapping...')
with open('announcemap.txt','r') as f:
for line in f.readlines():
fname, fid = line.strip().split()
inputPath = os.path.join(inDir, fname)
if not os.path.exists(inputPath):
print('ERROR: Could not find %s-file (%s)' % (fid, inputPath))
exit(1)
print(' Map: %s --> %s' % (fname, fid))
if os.path.exists(outDir):
print('Wiping old, announced files...')
shutil.rmtree(outDir)
os.mkdir(outDir)
print('Signing and copying files to %s directory...' % outDir)
with open('announcemap.txt','r') as f:
for line in f.readlines():
fname, fid = line.strip().split()
inputPath = os.path.join(inDir, fname)
outputPath = os.path.join(outDir, fname)
# If we're using a modified DL file
if fname=='dllinks.txt' and doComputeDLLinks:
inputPath = newDLFile
if fname.endswith('.txt'):
doSignFile(inputPath, outputPath)
else:
shutil.copy(inputPath, outputPath)
fdata = open(outputPath, 'rb').read()
fhash = binary_to_hex(sha256(fdata))
fileMappings[fname] = [fid, fhash]
longestID = max(longestID, len(fid))
longestURL = max(longestURL, len(pathPrefix+fname))
print('Creating digest file...')
digestFile = open(announcePath, 'w')
###
for fname,vals in fileMappings.iteritems():
fid = vals[0].ljust(longestID + 3)
url = (pathPrefix + fname).ljust(longestURL + 3)
fhash = vals[1]
digestFile.write('%s %s %s\n' % (fid, url, fhash))
digestFile.close()
print('')
print('------')
with open(announcePath, 'r') as f:
dfile = f.read()
print(dfile)
print('------')
print('Please verify the above data to your satisfaction:')
raw_input('Hit <enter> when ready: ')
doSignFile(announcePath, os.path.join(outDir, announceName))
print('*'*80)
print(open(announcePath, 'r').read())
print('*'*80)
print('')
print('Verifying files')
for fname,vals in fileMappings.iteritems():
if 'bootstrap' in fname:
continue
with open(os.path.join(outDir, fname), 'rb') as f:
sig,msg = readSigBlock(f.read())
addrB58 = verifySignature(sig, msg, 'v1', ord(ADDRBYTE))
print('Sign addr for:', vals[0].ljust(longestID+3), addrB58)
print('Done!')