forked from stx-labs/stacks.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
917 lines (692 loc) · 31.5 KB
/
data.py
File metadata and controls
917 lines (692 loc) · 31.5 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Blockstack-client
~~~~~
copyright: (c) 2014-2015 by Halfmoon Labs, Inc.
copyright: (c) 2016 by Blockstack.org
This file is part of Blockstack-client.
Blockstack-client is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Blockstack-client is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Blockstack-client. If not, see <http://www.gnu.org/licenses/>.
"""
import argparse
import sys
import json
import traceback
import types
import socket
import uuid
import os
import importlib
import pprint
import random
import time
import copy
import blockstack_profiles
import zone_file
import urllib
import user as user_db
import storage
from keys import *
from profile import *
from proxy import *
import pybitcoin
import bitcoin
import binascii
from utilitybelt import is_hex
from config import get_logger, DEBUG, MAX_RPC_LEN, find_missing, BLOCKSTACKD_SERVER, \
BLOCKSTACKD_PORT, BLOCKSTACK_METADATA_DIR, BLOCKSTACK_DEFAULT_STORAGE_DRIVERS, \
FIRST_BLOCK_MAINNET, NAME_OPCODES, OPFIELDS, CONFIG_DIR, SPV_HEADERS_PATH, BLOCKCHAIN_ID_MAGIC, \
NAME_PREORDER, NAME_REGISTRATION, NAME_UPDATE, NAME_TRANSFER, NAMESPACE_PREORDER, NAME_IMPORT, \
USER_ZONEFILE_TTL, CONFIG_PATH
log = get_logger()
import virtualchain
def serialize_mutable_data_id( data_id ):
"""
Turn a data ID into a suitable filesystem name
"""
return urllib.quote(data_id.replace("\0", "\\0")).replace("/", r"\x2f")
def load_mutable_data_version(conf, name, fq_data_id):
"""
Get the version field of a piece of mutable data from local cache.
"""
# try to get the current, locally-cached version
if conf is None:
conf = config.get_config()
metadata_dir = None
if conf is not None:
metadata_dir = conf.get('metadata', None)
if metadata_dir is not None and os.path.isdir(metadata_dir):
# find the version file for this data
serialized_data_id = serialize_mutable_data_id( fq_data_id )
version_file_path = os.path.join(metadata_dir, serialized_data_id + ".ver")
if os.path.exists(version_file_path):
ver = None
try:
with open(version_file_path, "r") as f:
ver_txt = f.read()
ver = int(ver_txt.strip())
# success!
return ver
except ValueError, ve:
# not an int
log.warn("Not an integer: '%s'" % version_file_path)
return None
except Exception, e:
# can't read
log.warn("Failed to read '%s': %s" % (version_file_path))
return None
else:
log.debug("No version path found")
return None
else:
log.debug("No config found; cannot load version for '%s'" % fq_data_id)
return None
def store_mutable_data_version(conf, fq_data_id, ver):
"""
Locally store the version of a piece of mutable data,
so we can ensure that its version is incremented on
subsequent puts.
Return True if stored
Return False if not
"""
assert storage.is_fq_data_id( fq_data_id ) or storage.is_valid_name( fq_data_id ), "data ID must be a Blockstack DNS name or a fully-qualified data ID"
if conf is None:
conf = config.get_config()
if conf is None:
log.warning("No config found; cannot store version for '%s'" % fq_data_id)
return False
assert 'metadata' in conf, "Missing metadata directory"
metadata_dir = conf['metadata']
if not os.path.isdir(metadata_dir):
log.warning("No metadata directory found; cannot store version of '%s'" % fq_data_id)
return False
serialized_data_id = serialize_mutable_data_id( fq_data_id )
version_file_path = os.path.join(metadata_dir, serialized_data_id + ".ver")
try:
with open(version_file_path, "w+") as f:
f.write("%s" % ver)
return True
except Exception, e:
# failed for whatever reason
log.exception(e)
log.warn("Failed to store version of '%s' to '%s'" % (fq_data_id, version_file_path))
return False
def delete_mutable_data_version(conf, data_id):
"""
Locally delete the version of a piece of mutable data.
Return True if deleted.
Return False if not
"""
if conf is None:
conf = config.get_config()
if conf is None:
log.warning("No config found; cannot store version for '%s'" % data_id)
return False
metadata_dir = conf['metadata']
if not os.path.isdir(metadata_dir):
log.warning("No metadata directory found; cannot store version of '%s'" % data_id)
return False
serialized_data_id = data_id.replace("/", "\x2f").replace('\0', "\\0")
version_file_path = os.path.join(metadata_dir, serialized_data_id + ".ver")
try:
os.unlink(version_file_path)
return True
except Exception, e:
# failed for whatever reason
log.warn("Failed to remove version file '%s'" % (version_file_path))
return False
def get_immutable(name, data_hash, data_id=None, proxy=None):
"""
get_immutable
Fetch a piece of immutable data. Use @data_hash to look it up
in the user's zonefile, and then fetch and verify the data itself
from the configured storage providers.
Return {'data': the data, 'hash': hash} on success
Return {'error': ...} on failure
"""
if proxy is None:
proxy = get_default_proxy()
user_zonefile = get_name_zonefile(name, proxy=proxy)
if user_zonefile is None:
return {'error': 'No user zonefile defined'}
if 'error' in user_zonefile:
return user_zonefile
if blockstack_profiles.is_profile_in_legacy_format( user_zonefile ):
# zonefile is really a legacy profile
return {'error': 'Profile is in a legacy format that does not support immutable data.'}
if data_id is not None:
# look up hash by name
h = user_db.get_immutable_data_hash( user_zonefile, data_id )
if h is None:
return {'error': 'No such immutable datum'}
if type(h) == list:
# this tool doesn't allow this to happen (one ID matches one hash),
# but that doesn't preclude the user from doing this with other tools.
if data_hash is not None and data_hash not in h:
return {'error': 'Data ID/hash mismatch'}
else:
return {'error': "Multiple matches for '%s': %s" % (data_id, ",".join(h))}
if data_hash is not None:
if h != data_hash:
return {'error': 'Data ID/hash mismatch'}
else:
data_hash = h
elif not user_db.has_immutable_data( user_zonefile, data_hash ):
return {'error': 'No such immutable datum'}
data_url_hint = user_db.get_immutable_data_url( user_zonefile, data_hash )
data = storage.get_immutable_data( data_hash, data_url=data_url_hint )
if data is None:
return {'error': 'No immutable data returned'}
return {'data': data, 'hash': data_hash}
def get_immutable_by_name( name, data_id, proxy=None ):
"""
get_immutable_by_name
Fetch a piece of immutable data, using a human-meaningful name.
Look up the hash in the user's zonefile, and use it to fetch
and verify the immutable data from the configured storage providers.
Return {'data': the data, 'hash': hash} on success
Return {'error': ...} on failure
"""
return get_immutable( name, None, data_id=data_id, proxy=proxy )
def list_update_history( name, current_block=None, proxy=None ):
"""
list_update_history
List all prior zonefile hashes of a name, in historic order.
Return a list of hashes on success.
Return None on error
"""
if proxy is None:
proxy = get_default_proxy()
if current_block is None:
info = proxy.getinfo()
current_block = info['last_block']+1
name_history = proxy.get_name_blockchain_history( name, 0, current_block )
all_update_hashes = []
for state in name_history:
if state.has_key('value_hash') and state['value_hash'] is not None:
if len(all_update_hashes) == 0 or all_update_hashes[-1] != state['value_hash']:
# changed
all_update_hashes.append( state['value_hash'] )
return all_update_hashes
def list_zonefile_history( name, current_block=None, proxy=None ):
"""
list_zonefile_history
List all prior zonefiles of a name, in historic order.
Return the list of zonefiles. Each zonefile will be a dict with either the zonefile data,
or a dict with only the key 'error' defined. This method can successfully return
some but not all zonefiles.
"""
zonefile_hashes = list_update_history( name, current_block=current_block, proxy=proxy )
zonefiles = []
for zh in zonefile_hashes:
zonefile = load_name_zonefile( zh )
if zonefile is None:
zonefile = {'error': 'Failed to load zonefile %s' % zh}
zonefiles.append( zonefile )
return zonefiles
def list_immutable_data_history( name, data_id, current_block=None, proxy=None ):
"""
list_immutable_data_history
List all prior hashes of an immutable datum, given its unchanging ID.
If the zonefile at a particular update is missing, the string "missing zonefile" will be
appended in its place. If the zonefile did not define data_id at that time,
the string "data not defined" will be placed in the hash's place.
Returns the list of hashes.
If there are multiple matches for the data ID in a zonefile, then return the list of hashes for that zonefile.
"""
zonefiles = list_zonefile_history( name, current_block=current_block, proxy=proxy )
hashes = []
for zf in zonefiles:
if 'error' in zf and len(zf.keys()) == 1:
# invalid
hashes.append("missing zonefile")
continue
if not user_db.is_user_zonefile(zf):
# legacy profile
hashes.append("missing zonefile")
continue
data_hash_or_hashes = user_db.get_immutable_data_hash( zf, data_id )
if data_hash_or_hashes is None:
hashes.append("data not defined")
continue
else:
hashes.append(data_hash_or_hashes)
return hashes
def get_mutable(name, data_id, proxy=None, ver_min=None, ver_max=None, ver_check=None, conf=None, wallet_keys=None):
"""
get_mutable
Fetch a piece of mutable data. Use @data_id to look it up in the user's
pofile, and then fetch and erify the data itself from the configured
storage providers.
If @ver_min is given, ensure the data's version is greater or equal to it.
If @ver_max is given, ensure the data's version is less than it.
If @ver_check is given, it must be a callable that takes the name, data and version and returns True/False
Return {'data': the data, 'version': the version} on success
Return {'error': ...} on error
"""
if proxy is None:
proxy = get_default_proxy()
if conf is None:
conf = proxy.conf
fq_data_id = storage.make_fq_data_id( name, data_id )
user_profile, user_zonefile = get_name_profile( name, proxy=proxy, wallet_keys=wallet_keys )
if user_profile is None:
return user_zonefile # will be an error message
if blockstack_profiles.is_profile_in_legacy_format( user_zonefile ):
# profile has not been converted to the new zonefile format yet.
return {'error': 'Profile is in a legacy format that does not support mutable data.'}
# get the mutable data zonefile
if not user_db.has_mutable_data( user_profile, data_id ):
return {'error': "No such mutable datum"}
mutable_data_zonefile = user_db.get_mutable_data_zonefile( user_profile, data_id )
assert mutable_data_zonefile is not None, "BUG: could not look up mutable datum '%s'.'%s'" % (name, data_id)
# get user's data public key
data_pubkey = user_db.user_zonefile_data_pubkey( user_zonefile )
if data_pubkey is None:
return {'error': "No data public key defined in this user's zonefile"}
# get the mutable data itself
urls = user_db.mutable_data_zonefile_urls( mutable_data_zonefile )
mutable_data = storage.get_mutable_data(fq_data_id, data_pubkey, urls=urls )
if mutable_data is None:
return {'error': "Failed to look up mutable datum"}
expected_version = load_mutable_data_version( conf, name, data_id )
if expected_version is None:
expected_version = 0
# check consistency
version = user_db.mutable_data_version( user_profile, data_id )
if ver_min is not None and ver_min > version:
return {'error': 'Mutable data is stale'}
if ver_max is not None and ver_max <= version:
return {'error': 'Mutable data is in the future'}
if ver_check is not None:
rc = ver_check( name, mutable_data, version )
if not rc:
return {'error': 'Mutable data consistency check failed'}
elif expected_version > version:
return {'error': 'Mutable data is stale; a later version was previously fetched'}
rc = store_mutable_data_version( conf, fq_data_id, version )
if not rc:
return {'error': 'Failed to store consistency information'}
return {'data': mutable_data, 'version': version}
def put_immutable(name, data_id, data_json, data_url=None, txid=None, proxy=None, wallet_keys=None ):
"""
put_immutable
Given a user's name, the data ID, and a JSON-ified chunk of data,
put it into the user's zonefile.
If the user's zonefile corresponds to a legacy profile, then automatically
convert it into a mutable profile and a modern zonefile, and then proceed
to add the data record.
If @txid is given, then don't re-send the NAME_UPDATE. Just try to store
the data to the immutable storage providers (again). This is to allow
for retries in the case where the NAME_UPDATE went through but the
storage providers did not receive data.
Return {'status': True, 'transaction_hash': txid, 'immutable_data_hash': data_hash, ...} on success
Return {'error': ...} on error
"""
if type(data_json) not in [dict]:
raise ValueError("Immutable data must be a dict")
legacy = False
if proxy is None:
proxy = get_default_proxy()
user_profile, user_zonefile, legacy = get_and_migrate_profile( name, create_if_absent=True, proxy=proxy, wallet_keys=wallet_keys )
if 'error' in user_profile:
log.debug("Unable to load user zonefile for '%s'" % name)
return user_profile
if legacy:
log.debug("User profile is legacy")
return {'error': "User profile is in legacy format, which does not support this operation. You must first migrate it with the 'migrate' command."}
data_text = storage.serialize_immutable_data( data_json )
data_hash = storage.get_data_hash( data_text )
value_hash = None
# insert into user zonefile, overwriting if need be
if user_db.has_immutable_data_id( user_zonefile, data_id ):
log.debug("WARN: overwriting old '%s'" % data_id)
old_hash = user_db.get_immutable_data_hash( user_zonefile, data_id )
# NOTE: can be a list, if the name matches multiple hashes.
# this tool doesn't do this, but it's still possible for the user to use other tools to do this.
if type(old_hash) != list:
old_hash = [old_hash]
for oh in old_hash:
rc = user_db.remove_immutable_data_zonefile( user_zonefile, oh )
if not rc:
return {'error': 'Failed to overwrite old immutable data'}
rc = user_db.put_immutable_data_zonefile( user_zonefile, data_id, data_hash, data_url=data_url )
if not rc:
return {'error': 'Failed to insert immutable data into user zonefile'}
# update zonefile, if we haven't already
if txid is None:
_, owner_privkey = get_owner_keypair(wallet_keys=wallet_keys, config_path=proxy.conf['path'])
update_result = update( name, user_zonefile, owner_privkey, proxy=proxy )
if 'error' in update_result:
# failed to replicate user zonefile hash
# the caller should simply try again, with the 'transaction_hash' given in the result.
return update_result
txid = update_result['transaction_hash']
value_hash = update_result['value_hash']
result = {
'immutable_data_hash': data_hash,
'transaction_hash': txid
}
if value_hash is not None:
result['zonefile_hash'] = value_hash
# replicate immutable data
rc = storage.put_immutable_data( data_json, txid )
if not rc:
result['error'] = 'Failed to store immutable data'
return result
# success!
result['status'] = True
return result
def put_mutable_get_version( user_profile, data_id, data_json, make_version=None ):
"""
Given the user profile, data_id, desired version, and callback to create a version,
find out what the next version of the mutable datum should be.
"""
version = None
mutable_version = user_db.mutable_data_version( user_profile, data_id )
if make_version is not None:
version = make_version( data_id, data_json, mutable_version )
else:
if mutable_version is not None:
version = mutable_version + 1
else:
version = 1
return version
def put_mutable(name, data_id, data_json, proxy=None, create_only=False, update_only=False,
txid=None, version=None, make_version=None, wallet_keys=None):
"""
put_mutable
Given a name, an ID for the data, and the data itself, sign and upload the data to the
configured storage providers. Add an entry for it into the user's profile as well.
** Consistency **
@version, if given, is the version to include in the data.
@make_version, if given, is a callback that takes the data_id, data_json, and current version as arguments, and generates the version to be included in the data record uploaded.
If ver is not given, but make_ver is, then make_ver will be used to generate the version.
If neither ver nor make_ver are given, the mutable data (if it already exists) is fetched, and the version is calculated as the larget known version + 1.
** Durability **
Replication is best-effort. If one storage provider driver succeeds, the put_mutable succeeds. If they all fail, then put_mutable fails.
More complex behavior can be had by creating a "meta-driver" that calls existing drivers' methods in the desired manner.
Returns a dict with {'status': True, 'version': version, ...} on success
Returns a dict with 'error' set on failure
"""
if type(data_json) not in [dict]:
raise ValueError("Mutable data must be a dict")
if proxy is None:
proxy = get_default_proxy()
fq_data_id = storage.make_fq_data_id( name, data_id )
user_profile, user_zonefile, created_new_zonefile = get_and_migrate_profile( name, create_if_absent=True, proxy=proxy, wallet_keys=wallet_keys )
if 'error' in user_profile:
return user_profile
if created_new_zonefile:
log.debug("User profile is legacy")
return {'error': "User profile is in legacy format, which does not support this operation. You must first migrate it with the 'migrate' command."}
log.debug("Profile for %s is currently:\n%s" % (name, json.dumps(user_profile, indent=4, sort_keys=True)))
exists = user_db.has_mutable_data( user_profile, data_id )
if not exists and update_only:
return {'error': 'Mutable datum does not exist'}
if exists and create_only:
return {'error': 'Mutable datum already exists'}
# get the version to use
if version is None:
version = put_mutable_get_version( user_profile, data_id, data_json, make_version=make_version )
# generate the mutable zonefile
data_privkey = get_data_privkey( wallet_keys=wallet_keys, config_path=proxy.conf['path'] )
urls = storage.make_mutable_data_urls( fq_data_id )
mutable_zonefile = user_db.make_mutable_data_zonefile( data_id, version, urls )
# add the mutable zonefile to the profile
rc = user_db.put_mutable_data_zonefile( user_profile, data_id, version, mutable_zonefile )
assert rc, "Failed to put mutable data zonefile"
# for legacy migration...
txid = None
zonefile_hash = None
result = {}
# update the profile with the new zonefile
rc = storage.put_mutable_data( name, user_profile, data_privkey )
if not rc:
result['error'] = 'Failed to store mutable data zonefile to profile'
return result
# put the mutable data record itself
rc = storage.put_mutable_data( fq_data_id, data_json, data_privkey )
if not rc:
result['error'] = "Failed to store mutable data"
return result
# remember which version this was
rc = store_mutable_data_version(proxy.conf, fq_data_id, version)
if not rc:
result['error'] = "Failed to store mutable data version"
return result
result['status'] = True
result['version'] = version
log.debug("Put '%s' to %s mutable data (version %s)\nProfile is now:\n%s" % (data_id, name, version, json.dumps(user_profile, indent=4, sort_keys=True)))
return result
def delete_immutable(name, data_key, data_id=None, proxy=None, txid=None, wallet_keys=None):
"""
delete_immutable
Remove an immutable datum from a name's profile, given by @data_key.
Return a dict with {'status': True} on success
Return a dict with {'error': ...} on failure
"""
if proxy is None:
proxy = get_default_proxy()
legacy = False
user_zonefile = get_name_zonefile( name, proxy=proxy )
if user_zonefile is None or 'error' in user_zonefile:
if user_zonefile is None:
return {'error': 'No user zonefile'}
else:
return user_zonefile
if blockstack_profiles.is_profile_in_legacy_format( user_zonefile ):
# zonefile is a legacy profile. There is no immutable data
log.info("Profile is in legacy format. No immutable data.")
return {'status': True}
if data_key is None:
if data_id is not None:
# look up the key (or list of keys)
# shouldn't be a list--this tool prevents that--but deal with it nevertheless
data_key = user_db.get_immutable_data_hash( user_zonefile, data_id )
if type(data_key) == list:
return {'error': "Multiple hashes for '%s': %s" % (data_id, ",".join(data_key)) }
if data_key is None:
return {'error': "No hash for '%s'" % data_id}
else:
return {'error': 'No data hash or data ID given'}
# already deleted?
if not user_db.has_immutable_data( user_zonefile, data_key ):
return {'status': True}
# remove
user_db.remove_immutable_data_zonefile( user_zonefile, data_key )
value_hash = None
if txid is None:
# actually send the transaction
_, owner_privkey = get_owner_keypair(wallet_keys=wallet_keys, config_path=proxy.conf['path'])
update_result = update( name, user_zonefile, owner_privkey, proxy=proxy )
if 'error' in update_result:
# failed to remove from zonefile
return update_result
txid = update_result['transaction_hash']
value_hash = update_result['value_hash']
result = {
'zonefile_hash': value_hash,
'transaction_hash': txid
}
# delete immutable data
data_privkey = get_data_privkey( wallet_keys=wallet_keys, config_path=proxy.conf['path'] )
rc = storage.delete_immutable_data( data_key, txid, data_privkey )
if not rc:
result['error'] = 'Failed to delete immutable data'
return result
else:
result['status'] = True
return result
def delete_mutable(name, data_id, proxy=None, wallet_keys=None):
"""
delete_mutable
Remove a piece of mutable data from the user's profile. Delete it from
the storage providers as well.
Returns a dict with {'status': True} on success
Returns a dict with {'error': ...} on failure
"""
if proxy is None:
proxy = get_default_proxy()
fq_data_id = storage.make_fq_data_id( name, data_id )
legacy = False
user_profile, user_zonefile = get_name_profile( name, proxy=proxy, wallet_keys=wallet_keys )
if user_profile is None:
return user_zonefile # will be an error message
if blockstack_profiles.is_profile_in_legacy_format( user_zonefile ):
# zonefile is a legacy profile. There is no immutable data
log.info("Profile is in legacy format. No immutable data.")
return {'status': True}
# already deleted?
if not user_db.has_mutable_data( user_profile, data_id ):
return {'status': True}
# unlink
user_db.remove_mutable_data_zonefile( user_profile, data_id )
# put new profile
data_privkey = get_data_privkey( wallet_keys=wallet_keys, config_path=proxy.conf['path'] )
rc = storage.put_mutable_data( name, user_profile, data_privkey )
if not rc:
return {'error': 'Failed to unlink mutable data from profile'}
# remove the data itself
rc = storage.delete_mutable_data( fq_data_id, data_privkey )
if not rc:
return {'error': 'Failed to delete mutable data from storage providers'}
return {'status': True}
def list_immutable_data( name, proxy=None ):
"""
List the names and hashes of all immutable data in a user's zonefile.
Returns {"data": [{"data_id": data_id, "hash": hash}]} on success
"""
if proxy is None:
proxy = get_default_proxy()
user_zonefile = get_name_zonefile(name, proxy=proxy)
if user_zonefile is None:
return {'error': 'No user zonefile defined'}
if 'error' in user_zonefile:
return user_zonefile
if blockstack_profiles.is_profile_in_legacy_format( user_zonefile ):
# zonefile is really a legacy profile
return {"data": []}
names_and_hashes = user_db.list_immutable_data( user_zonefile )
listing = [ {"data_id": nh[0], "hash": nh[1]} for nh in names_and_hashes ]
return {"data": listing}
def list_mutable_data( name, proxy=None, wallet_keys=None ):
"""
List the names and versions of all mutable data in a user's zonefile
Returns {"data": [{"data_id": data ID, "version": version}]}
"""
if proxy is None:
proxy = get_default_proxy()
user_profile, user_zonefile = get_name_profile( name, proxy=proxy, wallet_keys=wallet_keys )
if user_zonefile is None:
# user_profile will contain an error message
return user_profile
if blockstack_profiles.is_profile_in_legacy_format( user_zonefile ):
# zonefile is really a legacy profile
return {"data": []}
names_and_versions = user_db.list_mutable_data( user_profile )
listing = [ {"data_id": nv[0], "version": nv[1]} for nv in names_and_versions ]
return {"data": listing}
def blockstack_url_fetch( url, proxy=None, wallet_keys=None ):
"""
Given a blockstack:// url, fetch its data.
If the data is an immutable data url, and the hash is not given, then look up the hash first.
If the data is a mutable data url, and the version is not given, then look up the version as well.
Return {"data": data} on success
Return {"error": error message} on error
"""
mutable = False
immutable = False
blockchain_id = None
data_id = None
version = None
data_hash = None
try:
blockchain_id, data_id, version = storage.blockstack_mutable_data_url_parse( url )
mutable = True
except ValueError:
blockchain_id, data_id, data_hash = storage.blockstack_immutable_data_url_parse( url )
immutable = True
if mutable:
if data_id is not None:
# get single data
if version is not None:
return get_mutable( blockchain_id, data_id, proxy=proxy, wallet_keys=wallet_keys, ver_min=version, ver_max=version+1 )
else:
return get_mutable( blockchain_id, data_id, proxy=proxy, wallet_keys=wallet_keys )
else:
# list data
return list_mutable_data( blockchain_id, proxy=proxy, wallet_keys=wallet_keys )
else:
if data_id is not None:
# get single data
if data_hash is not None:
return get_immutable( blockchain_id, data_hash, data_id=data_id, proxy=proxy )
else:
return get_immutable_by_name( blockchain_id, data_id, proxy=proxy )
else:
# list data
return list_immutable_data( blockchain_id, proxy=proxy )
def data_get( blockstack_url, proxy=None, wallet_keys=None, **kw ):
"""
Resolve a blockstack URL to data (be it mutable or immutable).
"""
begin = None
end = None
begin = time.time()
ret = blockstack_url_fetch( blockstack_url, proxy=proxy, wallet_keys=wallet_keys )
end = time.time()
if os.environ.get("BLOCKSTACK_TEST") == "1":
log.debug("[BENCHMARK] data_get %s" % (end - begin))
return ret
def data_put( blockstack_url, data, proxy=None, wallet_keys=None, **kw ):
"""
Put data to a blockstack URL (be it mutable or immutable).
"""
parts = storage.blockstack_data_url_parse( blockstack_url )
assert parts is not None, "invalid url '%s'" % blockstack_url
end = None
begin = None
if parts['type'] == 'immutable':
begin = time.time()
ret = put_immutable( parts['blockchain_id'], parts['data_id'], data, proxy=proxy, wallet_keys=wallet_keys, **kw )
end = time.time()
else:
begin = time.time()
ret = put_mutable( parts['blockchain_id'], parts['data_id'], data, proxy=proxy, wallet_keys=wallet_keys, **kw )
end = time.time()
if os.environ.get("BLOCKSTACK_TEST") == "1":
log.debug("[BENCHMARK] data_put %s" % (end - begin))
return ret
def data_delete( blockstack_url, proxy=None, wallet_keys=None, **kw ):
"""
Delete data from a blockstack URL (be it mutable or immutable).
"""
parts = storage.blockstack_data_url_parse( blockstack_url )
assert parts is not None, "invalid url '%s'" % blockstack_url
if parts['type'] == 'immutable':
return delete_immutable( parts['blockchain_id'], parts['fields']['data_hash'], data_id=parts['data_id'], proxy=proxy, wallet_keys=wallet_keys, **kw )
else:
return delete_mutable( parts['blockchain_id'], parts['data_id'], proxy=proxy, wallet_keys=wallet_keys )
def data_list( name, proxy=None, wallet_keys=None ):
"""
List all data for a blockchain ID
Return {'status': True, 'listing': [...]} on success
Return {'error': ...} on failure
"""
immutable_listing = list_immutable_data( name, proxy=proxy )
mutable_listing = list_mutable_data( name, proxy=proxy, wallet_keys=wallet_keys )
if 'error' in immutable_listing:
return immutable_listing
if 'error' in mutable_listing:
return mutable_listing
return {'status': True, 'listing': immutable_listing['data'] + mutable_listing['data']}