From c4d12ed1cb54c459bd70c79d604295ddae9f4b05 Mon Sep 17 00:00:00 2001 From: user Date: Sat, 2 Mar 2024 00:57:30 +0300 Subject: [PATCH] dev@locazia: make init external ton message --- app/core/_blockchain/ton/wallet_v3cr3.py | 35 +++++++++++++++++++++++- app/core/background/ton_service.py | 16 +++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/app/core/_blockchain/ton/wallet_v3cr3.py b/app/core/_blockchain/ton/wallet_v3cr3.py index 129078c..4cc08e2 100644 --- a/app/core/_blockchain/ton/wallet_v3cr3.py +++ b/app/core/_blockchain/ton/wallet_v3cr3.py @@ -4,7 +4,8 @@ from time import time from tonsdk.boc import Cell, begin_cell from tonsdk.contract import Contract from tonsdk.contract.wallet import WalletContract -from tonsdk.utils import Address +from tonsdk.utils import Address, sign_message + WALLET_V3_CR3_CODE_HEX = 'b5ee9c7241021001000162000114ff00f4a413f4bcf2c80b01020120020f02014803080202ce0407020120050600510ccc741d35c87e900c3e910c7b513420405035c874ffcc19aea6f0003cb41a750c341ffc00a456f8a000730074c7c860802ab06ea65b0874c1f50c007ec0380860802aa82ea384cc407cb81a75350c087ec100743b47bb54fb55380c0c7000372103fcbc20002349521d74ac2009801d401d022f00101e85b8020120090c0201200a0b0019bb39ced44d08020d721d3ff3080011b8c97ed44d0d31f3080201580d0e001bb71e3da89a1020481ae43a61e610001bb49f3da89a1020281ae43a7fe61000f6f28308d71820d31fd31fd31f3001f823bbf2d06ced44d0d31fd3ffd31fd3ff305151baf2e0695132baf2e06924f901541066f910f2e069f8007054715226ed44ed45ed479131ed67ed65ed64747fed118e1104d430d023c000917f9170e2f002045023ed41edf101f2ff04a4c8cb1f13cbffcb1fcbffcb0fc9ed542675fc7e' @@ -74,3 +75,35 @@ class WalletV3CR3(WalletContract): ) signing_message = signing_message.store_ref(_commands.end_cell()) return self.create_external_message(signing_message.end_cell(), 0, True) + + def create_init_external_message(self): + create_state_init = self.create_state_init() + state_init = create_state_init["state_init"] + address = create_state_init["address"] + code = create_state_init["code"] + data = create_state_init["data"] + + signing_message = begin_cell().store_cell(self.create_signing_message(seqno=seqno, timeout=timeout)) + _commands = begin_cell().store_uint(0, 32) + signing_message = signing_message.store_ref(_commands.end_cell()) + signature = sign_message( + bytes(signing_message.bytes_hash()), self.options['private_key']).signature + + body = Cell() + body.bits.write_bytes(signature) + body.write_cell(signing_message) + + header = Contract.create_external_message_header(address) + external_message = Contract.create_common_msg_info( + header, state_init, body) + + return { + "address": address, + "message": external_message, + + "body": body, + "signing_message": signing_message, + "state_init": state_init, + "code": code, + "data": data, + } diff --git a/app/core/background/ton_service.py b/app/core/background/ton_service.py index 4bd2713..82babf1 100644 --- a/app/core/background/ton_service.py +++ b/app/core/background/ton_service.py @@ -7,9 +7,21 @@ import asyncio async def main(): - make_log("TON", "Service started", level="info") + make_log("TON", f"Service started, SW = {service_wallet.address.to_string(1, 1, 1)}", level="info") sw_seqno_result = await toncenter.run_get_method(service_wallet.address.to_string(1, 1, 1), 'seqno') - make_log("TON", f"Service wallet run seqno method: {sw_seqno_result}", level="info") + if sw_seqno_result.get('exit_code', -1) != 0: + sw_seqno_value = 0 + else: + sw_seqno_value = int(sw_seqno_result.get('stack', [['num', '0x0']])[0][1].replace('0x', ''), 16) + + make_log("TON", f"Service wallet run seqno method: {sw_seqno_value}", level="info") + if sw_seqno_value == 0: + make_log("TON", "Service wallet is not deployed, deploying...", level="info") + await toncenter.send_boc( + service_wallet.create_init_external_message()['message'].to_boc(False) + ) + await asyncio.sleep(5) + return await main() while True: make_log("TON", "Service running", level="debug")