def test_get_owned_ids_calls_get_outputs_filtered(): from bigchaindb.core import Bigchain with patch('bigchaindb.core.Bigchain.get_outputs_filtered') as gof: b = Bigchain() res = b.get_owned_ids('abc') gof.assert_called_once_with('abc', spent=False) assert res == gof()
import hashlib # monkey patch hashlib with sha3 functions import sha3 import json from bigchaindb import Bigchain import writeout b = Bigchain() user1priv, user1pub = writeout.importData("user1") user2priv, user2pub = writeout.importData("user2") user3priv, user3pub = writeout.importData("user3") govt_priv, govt_pub = writeout.importData("govt") gids = b.get_owned_ids(govt_pub) for id in gids: t = b.get_transaction(id) print(t) #data = {'vote': 30} #tx_serialized = bytes(json.dumps(data, skipkeys=False, ensure_ascii=False, separators=(',', ':')).encode("utf-8")) #tx_hash = hashlib.sha3_256(tx_serialized).hexdigest() #txs = b.get_tx_by_payload_hash(tx_hash) #for tx in txs: # print(tx)
__author__ = 'PC-LiNing' from bigchaindb import Bigchain b = Bigchain() testuser1_pub = '3EQyWna5Gniok73MXdT8m9kgSx6DuRYWUbmehCe4cmXU' testuser1_priv = 'EZYk1RJtMkySRwNtpiY7AeGQyYLaeu5XsrJHzzVBL3wn' testuser2_pub = '9ooi6zKXdLVmM2ZyUZjRTWcW3TXt2CtnHSbVZnXLxHJM' testuser2_priv = 'AZQ1Y4odynAuYEkSQhVKGqNKU4Zp5QidoLwmkfZCjYqL' tx_retrieved_id1 = b.get_owned_ids(testuser1_pub) print(tx_retrieved_id1) print("pop:") print(tx_retrieved_id1.pop()) print('####') tx_retrieved_id2 = b.get_owned_ids(testuser2_pub) print(tx_retrieved_id2) print("pop:") print(tx_retrieved_id2.pop())
__author__ = 'PC-LiNing' from bigchaindb import Bigchain b=Bigchain() testuser1_pub='3EQyWna5Gniok73MXdT8m9kgSx6DuRYWUbmehCe4cmXU' testuser1_priv='EZYk1RJtMkySRwNtpiY7AeGQyYLaeu5XsrJHzzVBL3wn' testuser2_pub='9ooi6zKXdLVmM2ZyUZjRTWcW3TXt2CtnHSbVZnXLxHJM' testuser2_priv='AZQ1Y4odynAuYEkSQhVKGqNKU4Zp5QidoLwmkfZCjYqL' tx_retrieved_id1=b.get_owned_ids(testuser1_pub) print(tx_retrieved_id1) print("pop:") print(tx_retrieved_id1.pop()) print('####') tx_retrieved_id2=b.get_owned_ids(testuser2_pub) print(tx_retrieved_id2) print("pop:") print(tx_retrieved_id2.pop())
print(json.dumps(tx_retrieved, sort_keys=True, indent=4, separators=(',', ':'))) print(testuser1_pub) print(b.me) print(tx_retrieved['id']) """ Transfer the Digital Asset """ # create a second testuser testuser2_priv, testuser2_pub = crypto.generate_key_pair() # retrieve the transaction with condition id tx_retrieved_id = b.get_owned_ids(testuser1_pub).pop() print(json.dumps(tx_retrieved_id, sort_keys=True, indent=4, separators=(',', ':'))) # create a transfer transaction tx_transfer = b.create_transaction(testuser1_pub, testuser2_pub, tx_retrieved_id, 'TRANSFER') # sign the transaction tx_transfer_signed = b.sign_transaction(tx_transfer, testuser1_priv) b.validate_transaction(tx_transfer_signed) # write the transaction b.write_transaction(tx_transfer_signed) sleep(8)
def transactions(request, format=None): ''' URL: transactions/ Use for: get or create bill transaction Para: GET: field - filter every json dict operation - CREATE or TRANSFER limit - limit list length, less than 100 sortby - sort by specified field, for timestamp order - the order for sort, asc or des, default asc receive_pubk - public key to get all transactions`id by receive, do not support some paras like limit origin_pubk - public key to get all transactions`id by origin POST: origin_pubk - transaction origin public key, if no this value, will create one CREATE transaction origin_prik - transaction origin private key, if no this value, will create one CREATE transaction pre_txid - previous transaction id receive_pubk - transaction receive public key data - data json string ''' if request.method == 'GET': # get paras fields = request.GET.getlist('field') limit = request.GET.get('limit', None) sortby = request.GET.get('sortby', None) order = request.GET.get('order', None) receive_pubk = request.GET.get('receive_pubk', None) origin_pubk = request.GET.get('origin_pubk', None) operation = request.GET.get('operation', None) # make sort function for rethinkdb driver sort_func = None if sortby != None: sort_func = lambda ts: ts['transaction'][sortby] # make filter filtes_funcs = [] if receive_pubk != None: filtes_funcs.append(lambda x: x['transaction'].contains(lambda x: \ x['conditions'].contains(lambda x: x['new_owners'].\ contains(receive_pubk)))) elif origin_pubk != None: filtes_funcs.append(lambda x: x['transaction'].contains(lambda x: \ x['fulfillments'].contains(lambda x: x['current_owners'].\ contains(origin_pubk)))) if operation != None: filtes_funcs.append(lambda t: t.contains(\ lambda x: x['transaction']['operation'] == operation)) # get datas datas = rdb_select(sortby=sort_func, order=order, filtes=filtes_funcs, keys=['block', 'transactions'], limit=limit) # can`t pluck, limit this data by rethinkdb driver, handle with hand # limit try: limit = int(limit) except TypeError: limit = 100 limit = min(100, limit) # limit less than 100 # return datas by max operation if limit == 1 and sortby != None: return Response(field_filter(datas[0], fields)) # return normal datas, it`s format willbe [[d1, ...], [d2, ..], ...] # change format to [d1, d2, d3...] ans = [] for alist in datas: for data in alist: if len(ans) >= limit: return Response(ans) ans.append(field_filter(data, fields)) # filter data return Response(ans) elif request.method == 'POST': # get paras origin_pubk = request.POST.get('origin_pubk', None) origin_prik = request.POST.get('origin_prik', None) pre_txid = request.POST.get('pre_txid', None) receive_pubk = request.POST.get('receive_pubk', None) data = request.POST.get('data', '{}') # make sure paras receive_prik = '' bdb_input = None bdb_type = 'CREATE' bdb = Bigchain() try: data = json.loads(data) # json string to json data except JSONDecodeError: return error(400, 'Wrong json string format') # bill data format checker left_money = 0 pre_data = None billm = 'Bill_amount_money' bill = 'bill' if billm not in data.keys(): return error(400, 'Wrong data format') # make sure paras if receive_pubk == None: # create a pair receive_prik, receive_pubk = crypto.generate_key_pair() if origin_pubk == None: # create transaction origin_pubk = bdb.me origin_prik = bdb.me_private else: # transfer transaction bdb_type = 'TRANSFER' if origin_prik == None: return error(400, 'need private key') if pre_txid == None: return error(400, 'need pre_txid when transfer') # get previous bill, and check it pre_tx = bdb.get_transaction(pre_txid) if pre_tx == None: return error(400, 'This pre_txid does not exist') # make input, in our case, the key 'cid' will always be 0 bdb_input = {'cid': 0, 'txid': pre_txid} # check bdb_input belong origin if bdb_input not in bdb.get_owned_ids(origin_pubk): return error(400, 'This pre_txid does not belong the origin') # money check pre_data = pre_tx['transaction']['data']['payload'] pre_money = pre_data[bill][billm] now_money = data[billm] if now_money > pre_money: return error(400, 'money to less') left_money = pre_money - now_money # start transaction tx = bdb.create_transaction(origin_pubk, receive_pubk, bdb_input, bdb_type, payload={ 'pre_txid': pre_txid, 'bill': data }) bdb.write_transaction(bdb.sign_transaction(tx, origin_prik)) # create new bill with left money if pre_data != None: # change data pre_data[bill][billm] = left_money pre_data['pre_txid'] = pre_txid ltx = bdb.create_transaction(bdb.me, origin_pubk, None, 'CREATE', payload=pre_data) bdb.write_transaction(bdb.sign_transaction(ltx, bdb.me_private)) return Response({ 'txid': tx['id'], 'receive_pubk': receive_pubk, 'receive_prik': receive_prik })
__author__ = 'PC-LiNing' from bigchaindb import Bigchain b=Bigchain() testuser1_pub='3EQyWna5Gniok73MXdT8m9kgSx6DuRYWUbmehCe4cmXU' testuser1_priv='EZYk1RJtMkySRwNtpiY7AeGQyYLaeu5XsrJHzzVBL3wn' testuser2_pub='9ooi6zKXdLVmM2ZyUZjRTWcW3TXt2CtnHSbVZnXLxHJM' testuser2_priv='AZQ1Y4odynAuYEkSQhVKGqNKU4Zp5QidoLwmkfZCjYqL' # node create transaction for A,-50 testuser1_last=b.get_owned_ids(testuser1_pub).pop() payload_A = { "msg" : "node send -50 to A,is -50", "issue" : "cost", "category" : "currency", "amount" : 50, "asset":"", # final owner 's account "account":300, "previous":testuser1_last['txid'], "trader":testuser2_pub } tx_create= b.create_transaction(b.me, testuser1_pub, None, 'CREATE', payload=payload_A) tx_create_signed = b.sign_transaction(tx_create, b.me_private) if b.is_valid_transaction(tx_create_signed): b.write_transaction(tx_create_signed)