def store_proc(data, approver_id, txid=None): # make transaction object transaction = bbclib.make_transaction_for_base_asset( asset_group_id=asset_group_id, event_num=1) transaction.events[0].add(mandatory_approver=approver_id, asset_group_id=asset_group_id) transaction.events[0].asset.add(user_id=user_id, asset_body=data) if txid: obj = { "jsonrpc": "2.0", "method": "bbc1_GetTransaction", "params": { "asset_group_id": bbclib.bin2str_base64(asset_group_id), "tx_id": txid, "user_id": bbclib.bin2str_base64(user_id), }, "id": 114514 } response = json_post(obj) prevtx = response["result"] prevtx = bbclib.BBcTransaction(jsonload=prevtx) bbclib.add_reference_to_transaction(asset_group_id, transaction, prevtx, 0) # get transaction digest jsontx = transaction.jsondump() obj = { "jsonrpc": "2.0", "method": "bbc1_GetTransactionDigest", "params": jsontx, "id": 114514 } response = json_post(obj) # add sign to transaction json sig = bbclib.bin2str_base64( key_pair.sign( binascii.a2b_base64(response["result"]["digest"].encode("utf-8")))) jsontx = json.loads(response["result"]["tx"]) sig = { "type": 1, "signature": sig, "pubkey": bbclib.bin2str_base64(key_pair.public_key) } jsontx["Signature"].append(sig) # Insert Transaction obj = { "jsonrpc": "2.0", "method": "bbc1_InsertTransaction", "params": json.dumps(jsontx), "id": 114514 } response = json_post(obj) print("TXID: %s" % response["result"]) print("ASID: %s" % jsontx["Event"][0]["Asset"]["asset_id"]) return response["result"]
def get_landdata(asid): obj = { "jsonrpc": "2.0", "method": "bbc1_GetTransactionfromAsset", "params": { "asset_group_id": bbclib.bin2str_base64(asset_group_id), "as_id": asid, "user_id": bbclib.bin2str_base64(user_id), }, "id": 114514 } response = json_post(obj) tx = response["result"] return tx
def registration(place): data = { "owner": bbclib.bin2str_base64(user_id), "place": place, "date": datetime.now().strftime('%s') } jsondata = json.dumps(data) store_proc(data=jsondata, approver_id=user_id, txid=None) print("Land registration is done!: %s" % jsondata)
def chown(new_owner, asid): prevtx = json.loads(get_landdata(asid)) asset = json.loads(prevtx["Event"][0]["Asset"]["body"]) if asset["owner"] != bbclib.bin2str_base64(user_id): print("Owner of this land is not you") return 0 asset["owner"] = new_owner asset["date"] = datetime.now().strftime('%s') data = json.dumps(asset) land = json.loads(get_landdata(asid)) transaction_id = land["transaction_id"] new_tx_id = store_proc(data, approver_id=binascii.a2b_base64(new_owner), txid=transaction_id) send_message(new_owner, new_tx_id)
def send_message(dst_id, msg): obj = { "jsonrpc": "2.0", "method": "bbc1_SendMessage", "params": { "user_id": bbclib.bin2str_base64(user_id), "dst_user_id": dst_id, "msg": msg }, "id": 114514 } print(obj) response = json_post(obj) res = response["result"] print(res) return True
send_message(new_owner, new_tx_id) if __name__ == '__main__': if (not os.path.exists(PRIVATE_KEY) and not os.path.exists(PUBLIC_KEY)): create_keypair() with open(PRIVATE_KEY, "rb") as fin: private_key = fin.read() with open(PUBLIC_KEY, "rb") as fin: public_key = fin.read() key_pair = bbclib.KeyPair(privkey=private_key, pubkey=public_key) user_id = bbclib.get_new_id(str(binascii.b2a_hex(key_pair.public_key)), include_timestamp=False) print("welcome to sample land manage!") print("Your id: %s" % bbclib.bin2str_base64(user_id)) print("Type command(help to see command list)") while (True): command = input('>> ') if command == "help": print("regist - regist land") print("get - get land info") print("chown - change owner of land") print("exit - exit land manage") elif command == "regist": print("Type regist address") address = input('>> ') registration(address) elif command == "get": print("Type AsID of land") asid = input('>> ')