diff --git a/submodules/MtProtoKit/TON/TON.h b/submodules/MtProtoKit/TON/TON.h index a8fa40f193..5391d9220c 100644 --- a/submodules/MtProtoKit/TON/TON.h +++ b/submodules/MtProtoKit/TON/TON.h @@ -41,6 +41,7 @@ NS_ASSUME_NONNULL_BEGIN - (MTSignal *)exportKey:(TONKey *)key localPassword:(NSString *)localPassword; - (MTSignal *)importKeyWithLocalPassword:(NSString *)localPassword mnemonicPassword:(NSString *)mnemonicPassword wordList:(NSArray *)wordList; - (MTSignal *)deleteKeyWithPublicKey:(NSString *)publicKey; +- (MTSignal *)makeWalletInitialized:(TONKey *)key localPassword:(NSString *)localPassword; @end diff --git a/submodules/MtProtoKit/TON/TON.mm b/submodules/MtProtoKit/TON/TON.mm index 8cedfcd4ed..ea7044b655 100644 --- a/submodules/MtProtoKit/TON/TON.mm +++ b/submodules/MtProtoKit/TON/TON.mm @@ -366,6 +366,37 @@ using tonlib_api::make_object; }] startOn:[MTQueue mainQueue]] deliverOn:[MTQueue mainQueue]]; } +- (MTSignal *)makeWalletInitialized:(TONKey *)key localPassword:(NSString *)localPassword { + return [[[[MTSignal alloc] initWithGenerator:^id(MTSubscriber *subscriber) { + uint64_t requestId = _nextRequestId; + _nextRequestId += 1; + + _requestHandlers[@(requestId)] = [[TONRequestHandler alloc] initWithCompletion:^(tonlib_api::object_ptr &object) { + if (object->get_id() == tonlib_api::error::ID) { + auto error = tonlib_api::move_object_as(object); + [subscriber putError:[[TONError alloc] initWithText:[[NSString alloc] initWithUTF8String:error->message_.c_str()]]]; + } else { + [subscriber putCompletion]; + } + }]; + + NSData *publicKeyData = [[NSData alloc] initWithBase64EncodedString:key.publicKey options:0]; + std::string publicKeyString((uint8_t *)publicKeyData.bytes, (uint8_t *)publicKeyData.bytes + publicKeyData.length); + + NSData *secretData = [[NSData alloc] initWithBase64EncodedString:key.secret options:0]; + std::string secretString((uint8_t *)secretData.bytes, (uint8_t *)secretData.bytes + secretData.length); + + NSData *localPasswordData = [localPassword dataUsingEncoding:NSUTF8StringEncoding]; + std::string localPasswordString((uint8_t *)localPasswordData.bytes, (uint8_t *)localPasswordData.bytes + localPasswordData.length); + + auto query = make_object(make_object(make_object(publicKeyString, secretString), localPasswordString)); + _client->send({ requestId, std::move(query) }); + + return [[MTBlockDisposable alloc] initWithBlock:^{ + }]; + }] startOn:[MTQueue mainQueue]] deliverOn:[MTQueue mainQueue]]; +} + - (MTSignal *)importKeyWithLocalPassword:(NSString *)localPassword mnemonicPassword:(NSString *)mnemonicPassword wordList:(NSArray *)wordList { return [[[[MTSignal alloc] initWithGenerator:^id(MTSubscriber *subscriber) { uint64_t requestId = _nextRequestId; diff --git a/submodules/TelegramUI/TelegramUI/ChatController.swift b/submodules/TelegramUI/TelegramUI/ChatController.swift index 3df4df5b47..54f654e2cf 100644 --- a/submodules/TelegramUI/TelegramUI/ChatController.swift +++ b/submodules/TelegramUI/TelegramUI/ChatController.swift @@ -2406,7 +2406,7 @@ public final class ChatController: TelegramController, GalleryHiddenMediaTarget, } let timestamp = strongSelf.context.account.network.getApproximateRemoteTimestamp() let _ = (strongSelf.context.account.postbox.transaction { transaction in - let message1 = StoreMessage(peerId: peerId, namespace: Namespaces.Message.Local, globallyUniqueId: nil, groupingKey: nil, timestamp: timestamp, flags: [.Incoming], tags: [], globalTags: [], localTags: [], forwardInfo: nil, authorId: peerId, text: "balance: \(state.balance)", attributes: [], media: []) + let message1 = StoreMessage(peerId: peerId, namespace: Namespaces.Message.Local, globallyUniqueId: nil, groupingKey: nil, timestamp: timestamp, flags: [.Incoming], tags: [], globalTags: [], localTags: [], forwardInfo: nil, authorId: peerId, text: "balance: \(state.balance), seqno: \(state.seqno)", attributes: [], media: []) let _ = transaction.addMessages([message1], location: .UpperHistoryBlock) }).start() }) @@ -2444,6 +2444,15 @@ public final class ChatController: TelegramController, GalleryHiddenMediaTarget, guard let localAddress = localAddress as? String else { return } + let _ = strongSelf.context.ton?.getAccountState(withAddress: localAddress).start(next: { state in + guard let state = state as? TONAccountState else { + return + } + let _ = (strongSelf.context.account.postbox.transaction { transaction in + let message1 = StoreMessage(peerId: peerId, namespace: Namespaces.Message.Local, globallyUniqueId: nil, groupingKey: nil, timestamp: timestamp, flags: [.Incoming], tags: [], globalTags: [], localTags: [], forwardInfo: nil, authorId: peerId, text: "balance: \(state.balance), seqno: \(state.seqno)", attributes: [], media: []) + let _ = transaction.addMessages([message1], location: .UpperHistoryBlock) + }).start() + }) let _ = strongSelf.context.ton?.sendGrams(from: TONKey(publicKey: components[2], secret: components[3]), localPassword: components[4], fromAddress: localAddress, toAddress: components[5], amount: amount).start(next: nil, error: { error in guard let error = error as? TONError else { return @@ -2496,6 +2505,30 @@ public final class ChatController: TelegramController, GalleryHiddenMediaTarget, }).start() }, completed: nil) } + } else if components[1] == "initwallet" { + if components.count >= 5 { + let timestamp = strongSelf.context.account.network.getApproximateRemoteTimestamp() + let _ = (strongSelf.context.account.postbox.transaction { transaction in + let message = StoreMessage(peerId: peerId, namespace: Namespaces.Message.Local, globallyUniqueId: nil, groupingKey: nil, timestamp: timestamp, flags: [], tags: [], globalTags: [], localTags: [], forwardInfo: nil, authorId: strongSelf.context.account.peerId, text: message.text, attributes: [], media: []) + let _ = transaction.addMessages([message], location: .UpperHistoryBlock) + }).start() + + let _ = strongSelf.context.ton?.makeWalletInitialized(TONKey(publicKey: components[2], secret: components[3]), localPassword: components[4]).start(next: nil, error: { error in + guard let error = error as? TONError else { + return + } + let _ = (strongSelf.context.account.postbox.transaction { transaction in + let message1 = StoreMessage(peerId: peerId, namespace: Namespaces.Message.Local, globallyUniqueId: nil, groupingKey: nil, timestamp: timestamp, flags: [.Incoming], tags: [], globalTags: [], localTags: [], forwardInfo: nil, authorId: peerId, text: "error: \(error.text)", attributes: [], media: []) + let _ = transaction.addMessages([message1], location: .UpperHistoryBlock) + }).start() + }, completed: { + let timestamp = strongSelf.context.account.network.getApproximateRemoteTimestamp() + let _ = (strongSelf.context.account.postbox.transaction { transaction in + let message1 = StoreMessage(peerId: peerId, namespace: Namespaces.Message.Local, globallyUniqueId: nil, groupingKey: nil, timestamp: timestamp, flags: [.Incoming], tags: [], globalTags: [], localTags: [], forwardInfo: nil, authorId: peerId, text: "done", attributes: [], media: []) + let _ = transaction.addMessages([message1], location: .UpperHistoryBlock) + }).start() + }) + } } else if components[1] == "importkey" { if components.count >= 4 { let timestamp = strongSelf.context.account.network.getApproximateRemoteTimestamp()