mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
updated tonlib
- updated tonlib - updated validator - updated documentation - first version of http over rldp proxy
This commit is contained in:
parent
53ec9684bd
commit
77842f9b63
128 changed files with 10555 additions and 2285 deletions
|
@ -14,7 +14,7 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#include "HighloadWallet.h"
|
||||
#include "GenericAccount.h"
|
||||
|
@ -51,7 +51,7 @@ td::Ref<vm::Cell> HighloadWallet::get_init_message(const td::Ed25519::PrivateKey
|
|||
td::Ref<vm::Cell> HighloadWallet::make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 wallet_id,
|
||||
td::uint32 seqno, td::uint32 valid_until,
|
||||
td::Span<Gift> gifts) noexcept {
|
||||
CHECK(gifts.size() <= 254);
|
||||
CHECK(gifts.size() <= max_gifts_size);
|
||||
vm::Dictionary messages(16);
|
||||
for (size_t i = 0; i < gifts.size(); i++) {
|
||||
auto& gift = gifts[i];
|
||||
|
@ -64,7 +64,7 @@ td::Ref<vm::Cell> HighloadWallet::make_a_gift_message(const td::Ed25519::Private
|
|||
vm::CellBuilder cb;
|
||||
GenericAccount::store_int_message(cb, gift.destination, gramms);
|
||||
cb.store_bytes("\0\0\0\0", 4);
|
||||
//vm::CellString::store(cb, gift.message, 35 * 8).ensure();
|
||||
vm::CellString::store(cb, gift.message, 35 * 8).ensure();
|
||||
auto message_inner = cb.finalize();
|
||||
cb = {};
|
||||
cb.store_long(send_mode, 8).store_ref(message_inner);
|
||||
|
@ -123,4 +123,20 @@ td::Result<td::uint32> HighloadWallet::get_wallet_id_or_throw() const {
|
|||
return static_cast<td::uint32>(cs.fetch_ulong(32));
|
||||
}
|
||||
|
||||
td::Result<td::Ed25519::PublicKey> HighloadWallet::get_public_key() const {
|
||||
return TRY_VM(get_public_key_or_throw());
|
||||
}
|
||||
|
||||
td::Result<td::Ed25519::PublicKey> HighloadWallet::get_public_key_or_throw() const {
|
||||
if (state_.data.is_null()) {
|
||||
return td::Status::Error("data is null");
|
||||
}
|
||||
//FIXME use get method
|
||||
auto cs = vm::load_cell_slice(state_.data);
|
||||
cs.skip_first(64);
|
||||
td::SecureString res(td::Ed25519::PublicKey::LENGTH);
|
||||
cs.fetch_bytes(res.as_mutable_slice().ubegin(), td::narrow_cast<td::int32>(res.size()));
|
||||
return td::Ed25519::PublicKey(std::move(res));
|
||||
}
|
||||
|
||||
} // namespace ton
|
||||
|
|
|
@ -14,29 +14,26 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "smc-envelope/SmartContract.h"
|
||||
#include "smc-envelope/WalletInterface.h"
|
||||
#include "vm/cells.h"
|
||||
#include "Ed25519.h"
|
||||
#include "block/block.h"
|
||||
#include "vm/cells/CellString.h"
|
||||
|
||||
namespace ton {
|
||||
class HighloadWallet : ton::SmartContract {
|
||||
class HighloadWallet : ton::SmartContract, public WalletInterface {
|
||||
public:
|
||||
explicit HighloadWallet(State state) : ton::SmartContract(std::move(state)) {
|
||||
}
|
||||
static constexpr unsigned max_message_size = vm::CellString::max_bytes;
|
||||
static constexpr unsigned max_gifts_size = 254;
|
||||
static td::Ref<vm::Cell> get_init_state(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id) noexcept;
|
||||
static td::Ref<vm::Cell> get_init_message(const td::Ed25519::PrivateKey& private_key, td::uint32 wallet_id) noexcept;
|
||||
struct Gift {
|
||||
block::StdAddress destination;
|
||||
td::int64 gramms;
|
||||
std::string message;
|
||||
};
|
||||
static td::Ref<vm::Cell> make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 wallet_id,
|
||||
td::uint32 seqno, td::uint32 valid_until, td::Span<Gift> gifts) noexcept;
|
||||
|
||||
|
@ -47,8 +44,20 @@ class HighloadWallet : ton::SmartContract {
|
|||
td::Result<td::uint32> get_seqno() const;
|
||||
td::Result<td::uint32> get_wallet_id() const;
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 valid_until,
|
||||
td::Span<Gift> gifts) const override {
|
||||
TRY_RESULT(seqno, get_seqno());
|
||||
TRY_RESULT(wallet_id, get_wallet_id());
|
||||
return make_a_gift_message(private_key, wallet_id, seqno, valid_until, gifts);
|
||||
}
|
||||
size_t get_max_gifts_size() const override {
|
||||
return max_gifts_size;
|
||||
}
|
||||
td::Result<td::Ed25519::PublicKey> get_public_key() const override;
|
||||
|
||||
private:
|
||||
td::Result<td::uint32> get_seqno_or_throw() const;
|
||||
td::Result<td::uint32> get_wallet_id_or_throw() const;
|
||||
td::Result<td::Ed25519::PublicKey> get_public_key_or_throw() const;
|
||||
};
|
||||
} // namespace ton
|
||||
|
|
151
crypto/smc-envelope/HighloadWalletV2.cpp
Normal file
151
crypto/smc-envelope/HighloadWalletV2.cpp
Normal file
|
@ -0,0 +1,151 @@
|
|||
/*
|
||||
This file is part of TON Blockchain Library.
|
||||
|
||||
TON Blockchain Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
TON Blockchain Library 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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#include "HighloadWalletV2.h"
|
||||
#include "GenericAccount.h"
|
||||
#include "SmartContractCode.h"
|
||||
|
||||
#include "vm/boc.h"
|
||||
#include "vm/cells/CellString.h"
|
||||
#include "td/utils/base64.h"
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace ton {
|
||||
td::optional<td::int32> HighloadWalletV2::guess_revision(const vm::Cell::Hash& code_hash) {
|
||||
for (td::int32 i = 1; i <= 2; i++) {
|
||||
if (get_init_code(i)->get_hash() == code_hash) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
td::optional<td::int32> HighloadWalletV2::guess_revision(const block::StdAddress& address,
|
||||
const td::Ed25519::PublicKey& public_key,
|
||||
td::uint32 wallet_id) {
|
||||
for (td::int32 i = 1; i <= 2; i++) {
|
||||
if (GenericAccount::get_address(address.workchain, get_init_state(public_key, wallet_id, i)) == address) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
td::Ref<vm::Cell> HighloadWalletV2::get_init_state(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id,
|
||||
td::int32 revision) noexcept {
|
||||
auto code = get_init_code(revision);
|
||||
auto data = get_init_data(public_key, wallet_id);
|
||||
return GenericAccount::get_init_state(std::move(code), std::move(data));
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> HighloadWalletV2::get_init_message(const td::Ed25519::PrivateKey& private_key, td::uint32 wallet_id,
|
||||
td::uint32 valid_until) noexcept {
|
||||
td::uint32 id = -1;
|
||||
auto append_message = [&](auto&& cb) -> vm::CellBuilder& {
|
||||
cb.store_long(wallet_id, 32).store_long(valid_until, 32).store_long(id, 32);
|
||||
CHECK(cb.store_maybe_ref({}));
|
||||
return cb;
|
||||
};
|
||||
auto signature = private_key.sign(append_message(vm::CellBuilder()).finalize()->get_hash().as_slice()).move_as_ok();
|
||||
|
||||
return append_message(vm::CellBuilder().store_bytes(signature)).finalize();
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> HighloadWalletV2::make_a_gift_message(const td::Ed25519::PrivateKey& private_key,
|
||||
td::uint32 wallet_id, td::uint32 valid_until,
|
||||
td::Span<Gift> gifts) noexcept {
|
||||
CHECK(gifts.size() <= max_gifts_size);
|
||||
vm::Dictionary messages(16);
|
||||
for (size_t i = 0; i < gifts.size(); i++) {
|
||||
auto& gift = gifts[i];
|
||||
td::int32 send_mode = 3;
|
||||
auto gramms = gift.gramms;
|
||||
if (gramms == -1) {
|
||||
gramms = 0;
|
||||
send_mode += 128;
|
||||
}
|
||||
vm::CellBuilder cb;
|
||||
GenericAccount::store_int_message(cb, gift.destination, gramms);
|
||||
cb.store_bytes("\0\0\0\0", 4);
|
||||
vm::CellString::store(cb, gift.message, 35 * 8).ensure();
|
||||
auto message_inner = cb.finalize();
|
||||
cb = {};
|
||||
cb.store_long(send_mode, 8).store_ref(message_inner);
|
||||
auto key = messages.integer_key(td::make_refint(i), 16, false);
|
||||
messages.set_builder(key.bits(), 16, cb);
|
||||
}
|
||||
std::string hash;
|
||||
{
|
||||
vm::CellBuilder cb;
|
||||
CHECK(cb.store_maybe_ref(messages.get_root_cell()));
|
||||
hash = cb.finalize()->get_hash().as_slice().substr(28, 4).str();
|
||||
}
|
||||
|
||||
vm::CellBuilder cb;
|
||||
cb.store_long(wallet_id, 32).store_long(valid_until, 32).store_bytes(hash);
|
||||
CHECK(cb.store_maybe_ref(messages.get_root_cell()));
|
||||
auto message_outer = cb.finalize();
|
||||
auto signature = private_key.sign(message_outer->get_hash().as_slice()).move_as_ok();
|
||||
return vm::CellBuilder().store_bytes(signature).append_cellslice(vm::load_cell_slice(message_outer)).finalize();
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> HighloadWalletV2::get_init_code(td::int32 revision) noexcept {
|
||||
return SmartContractCode::highload_wallet_v2(revision);
|
||||
}
|
||||
|
||||
vm::CellHash HighloadWalletV2::get_init_code_hash() noexcept {
|
||||
return get_init_code(0)->get_hash();
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> HighloadWalletV2::get_init_data(const td::Ed25519::PublicKey& public_key,
|
||||
td::uint32 wallet_id) noexcept {
|
||||
vm::CellBuilder cb;
|
||||
cb.store_long(wallet_id, 32).store_long(0, 64).store_bytes(public_key.as_octet_string());
|
||||
CHECK(cb.store_maybe_ref({}));
|
||||
return cb.finalize();
|
||||
}
|
||||
|
||||
td::Result<td::uint32> HighloadWalletV2::get_wallet_id() const {
|
||||
return TRY_VM(get_wallet_id_or_throw());
|
||||
}
|
||||
|
||||
td::Result<td::uint32> HighloadWalletV2::get_wallet_id_or_throw() const {
|
||||
if (state_.data.is_null()) {
|
||||
return 0;
|
||||
}
|
||||
//FIXME use get method
|
||||
auto cs = vm::load_cell_slice(state_.data);
|
||||
return static_cast<td::uint32>(cs.fetch_ulong(32));
|
||||
}
|
||||
|
||||
td::Result<td::Ed25519::PublicKey> HighloadWalletV2::get_public_key() const {
|
||||
return TRY_VM(get_public_key_or_throw());
|
||||
}
|
||||
|
||||
td::Result<td::Ed25519::PublicKey> HighloadWalletV2::get_public_key_or_throw() const {
|
||||
if (state_.data.is_null()) {
|
||||
return td::Status::Error("data is null");
|
||||
}
|
||||
//FIXME use get method
|
||||
auto cs = vm::load_cell_slice(state_.data);
|
||||
cs.skip_first(96);
|
||||
td::SecureString res(td::Ed25519::PublicKey::LENGTH);
|
||||
cs.fetch_bytes(res.as_mutable_slice().ubegin(), td::narrow_cast<td::int32>(res.size()));
|
||||
return td::Ed25519::PublicKey(std::move(res));
|
||||
}
|
||||
|
||||
} // namespace ton
|
66
crypto/smc-envelope/HighloadWalletV2.h
Normal file
66
crypto/smc-envelope/HighloadWalletV2.h
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
This file is part of TON Blockchain Library.
|
||||
|
||||
TON Blockchain Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
TON Blockchain Library 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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "smc-envelope/SmartContract.h"
|
||||
#include "smc-envelope/WalletInterface.h"
|
||||
#include "vm/cells.h"
|
||||
#include "Ed25519.h"
|
||||
#include "block/block.h"
|
||||
#include "vm/cells/CellString.h"
|
||||
|
||||
namespace ton {
|
||||
class HighloadWalletV2 : ton::SmartContract, public WalletInterface {
|
||||
public:
|
||||
explicit HighloadWalletV2(State state) : ton::SmartContract(std::move(state)) {
|
||||
}
|
||||
static constexpr unsigned max_message_size = vm::CellString::max_bytes;
|
||||
static constexpr unsigned max_gifts_size = 254;
|
||||
static td::Ref<vm::Cell> get_init_state(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id,
|
||||
td::int32 revision) noexcept;
|
||||
static td::Ref<vm::Cell> get_init_message(const td::Ed25519::PrivateKey& private_key, td::uint32 wallet_id,
|
||||
td::uint32 valid_until) noexcept;
|
||||
|
||||
static td::Ref<vm::Cell> make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 wallet_id,
|
||||
td::uint32 valid_until, td::Span<Gift> gifts) noexcept;
|
||||
|
||||
static td::Ref<vm::Cell> get_init_code(td::int32 revision) noexcept;
|
||||
static vm::CellHash get_init_code_hash() noexcept;
|
||||
static td::Ref<vm::Cell> get_init_data(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id) noexcept;
|
||||
static td::optional<td::int32> guess_revision(const vm::Cell::Hash& code_hash);
|
||||
static td::optional<td::int32> guess_revision(const block::StdAddress& address,
|
||||
const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id);
|
||||
|
||||
td::Result<td::uint32> get_wallet_id() const;
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 valid_until,
|
||||
td::Span<Gift> gifts) const override {
|
||||
TRY_RESULT(wallet_id, get_wallet_id());
|
||||
return make_a_gift_message(private_key, wallet_id, valid_until, gifts);
|
||||
}
|
||||
size_t get_max_gifts_size() const override {
|
||||
return max_gifts_size;
|
||||
}
|
||||
td::Result<td::Ed25519::PublicKey> get_public_key() const override;
|
||||
|
||||
private:
|
||||
td::Result<td::uint32> get_wallet_id_or_throw() const;
|
||||
td::Result<td::Ed25519::PublicKey> get_public_key_or_throw() const;
|
||||
};
|
||||
} // namespace ton
|
542
crypto/smc-envelope/ManualDns.cpp
Normal file
542
crypto/smc-envelope/ManualDns.cpp
Normal file
|
@ -0,0 +1,542 @@
|
|||
/*
|
||||
This file is part of TON Blockchain Library.
|
||||
|
||||
TON Blockchain Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
TON Blockchain Library 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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2019-2020 Telegram Systems LLP
|
||||
*/
|
||||
#include "ManualDns.h"
|
||||
|
||||
#include "smc-envelope/SmartContractCode.h"
|
||||
|
||||
#include "vm/dict.h"
|
||||
|
||||
#include "td/utils/format.h"
|
||||
#include "td/utils/overloaded.h"
|
||||
#include "td/utils/Parser.h"
|
||||
#include "td/utils/Random.h"
|
||||
|
||||
#include "block/block-auto.h"
|
||||
#include "block/block-parse.h"
|
||||
|
||||
namespace ton {
|
||||
|
||||
//proto_list_nil$0 = ProtoList;
|
||||
//proto_list_next$1 head:Protocol tail:ProtoList = ProtoList;
|
||||
//proto_http#4854 = Protocol;
|
||||
|
||||
//cap_list_nil$0 = SmcCapList;
|
||||
//cap_list_next$1 head:SmcCapability tail:SmcCapList = SmcCapList;
|
||||
//cap_method_seqno#5371 = SmcCapability;
|
||||
//cap_method_pubkey#71f4 = SmcCapability;
|
||||
//cap_is_wallet#2177 = SmcCapability;
|
||||
//cap_name#ff name:Text = SmcCapability;
|
||||
//
|
||||
td::Result<td::Ref<vm::Cell>> DnsInterface::EntryData::as_cell() const {
|
||||
td::Ref<vm::Cell> res;
|
||||
td::Status error;
|
||||
data.visit(td::overloaded(
|
||||
[&](const EntryDataText& text) {
|
||||
block::gen::DNSRecord::Record_dns_text dns;
|
||||
vm::CellBuilder cb;
|
||||
vm::CellText::store(cb, text.text);
|
||||
dns.x = vm::load_cell_slice_ref(cb.finalize());
|
||||
tlb::pack_cell(res, dns);
|
||||
},
|
||||
[&](const EntryDataNextResolver& resolver) {
|
||||
block::gen::DNSRecord::Record_dns_next_resolver dns;
|
||||
vm::CellBuilder cb;
|
||||
block::tlb::t_MsgAddressInt.store_std_address(cb, resolver.resolver.workchain, resolver.resolver.addr);
|
||||
dns.resolver = vm::load_cell_slice_ref(cb.finalize());
|
||||
tlb::pack_cell(res, dns);
|
||||
},
|
||||
[&](const EntryDataAdnlAddress& adnl_address) {
|
||||
block::gen::DNSRecord::Record_dns_adnl_address dns;
|
||||
dns.adnl_addr = adnl_address.adnl_address;
|
||||
dns.flags = 0;
|
||||
tlb::pack_cell(res, dns);
|
||||
},
|
||||
[&](const EntryDataSmcAddress& smc_address) {
|
||||
block::gen::DNSRecord::Record_dns_smc_address dns;
|
||||
vm::CellBuilder cb;
|
||||
block::tlb::t_MsgAddressInt.store_std_address(cb, smc_address.smc_address.workchain,
|
||||
smc_address.smc_address.addr);
|
||||
dns.smc_addr = vm::load_cell_slice_ref(cb.finalize());
|
||||
tlb::pack_cell(res, dns);
|
||||
}));
|
||||
if (error.is_error()) {
|
||||
return error;
|
||||
}
|
||||
if (res.is_null()) {
|
||||
return td::Status::Error("Entry data is emtpy");
|
||||
}
|
||||
return res;
|
||||
//dns_text#1eda _:Text = DNSRecord;
|
||||
|
||||
//dns_next_resolver#ba93 resolver:MsgAddressInt = DNSRecord; // usually in record #-1
|
||||
//dns_adnl_address#ad01 adnl_addr:bits256 flags:(## 8) { flags <= 1 } proto_list:flags . 0?ProtoList = DNSRecord; // often in record #2
|
||||
|
||||
//dns_smc_address#9fd3 smc_addr:MsgAddressInt flags:(## 8) { flags <= 1 } cap_list:flags . 0?SmcCapList = DNSRecord; // often in record #1
|
||||
}
|
||||
|
||||
td::Result<DnsInterface::EntryData> DnsInterface::EntryData::from_cellslice(vm::CellSlice& cs) {
|
||||
switch (block::gen::t_DNSRecord.get_tag(cs)) {
|
||||
case block::gen::DNSRecord::dns_text: {
|
||||
block::gen::DNSRecord::Record_dns_text dns;
|
||||
tlb::unpack(cs, dns);
|
||||
TRY_RESULT(text, vm::CellText::load(dns.x.write()));
|
||||
return EntryData::text(std::move(text));
|
||||
}
|
||||
case block::gen::DNSRecord::dns_next_resolver: {
|
||||
block::gen::DNSRecord::Record_dns_next_resolver dns;
|
||||
tlb::unpack(cs, dns);
|
||||
ton::WorkchainId wc;
|
||||
ton::StdSmcAddress addr;
|
||||
if (!block::tlb::t_MsgAddressInt.extract_std_address(dns.resolver, wc, addr)) {
|
||||
return td::Status::Error("Invalid address");
|
||||
}
|
||||
return EntryData::next_resolver(block::StdAddress(wc, addr));
|
||||
}
|
||||
case block::gen::DNSRecord::dns_adnl_address: {
|
||||
block::gen::DNSRecord::Record_dns_adnl_address dns;
|
||||
tlb::unpack(cs, dns);
|
||||
return EntryData::adnl_address(dns.adnl_addr);
|
||||
}
|
||||
case block::gen::DNSRecord::dns_smc_address: {
|
||||
block::gen::DNSRecord::Record_dns_smc_address dns;
|
||||
tlb::unpack(cs, dns);
|
||||
ton::WorkchainId wc;
|
||||
ton::StdSmcAddress addr;
|
||||
if (!block::tlb::t_MsgAddressInt.extract_std_address(dns.smc_addr, wc, addr)) {
|
||||
return td::Status::Error("Invalid address");
|
||||
}
|
||||
return EntryData::smc_address(block::StdAddress(wc, addr));
|
||||
}
|
||||
}
|
||||
return td::Status::Error("Unknown entry data");
|
||||
}
|
||||
|
||||
td::Result<std::vector<DnsInterface::Entry>> DnsInterface::resolve(td::Slice name, td::int32 category) const {
|
||||
TRY_RESULT(raw_entries, resolve_raw(name, category));
|
||||
std::vector<Entry> entries;
|
||||
entries.reserve(raw_entries.size());
|
||||
for (auto& raw_entry : raw_entries) {
|
||||
Entry entry;
|
||||
entry.name = std::move(raw_entry.name);
|
||||
entry.category = raw_entry.category;
|
||||
auto cs = vm::load_cell_slice(raw_entry.data);
|
||||
TRY_RESULT(data, EntryData::from_cellslice(cs));
|
||||
entry.data = std::move(data);
|
||||
entries.push_back(std::move(entry));
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
/*
|
||||
External message structure:
|
||||
[Bytes<512b>:signature] [UInt<32b>:seqno] [UInt<6b>:operation]
|
||||
[Either b0: inline name (<= 58-x Bytes) or b1: reference-stored name)
|
||||
x depends on operation
|
||||
Use of 6-bit op instead of 32-bit allows to save 4 bytes for inline name
|
||||
Inline [Name] structure: [UInt<6b>:length] [Bytes<lengthB>:data]
|
||||
Operations (continuation of message):
|
||||
00 Contract initialization message (only if seqno = 0) (x=-)
|
||||
31 TSet: replace ENTIRE DOMAIN TABLE with the provided tree root cell (x=-)
|
||||
[Cell<1r>:new_domains_table]
|
||||
51 OSet: replace owner public key with a new one (x=-)
|
||||
[UInt<256b>:new_public_key]
|
||||
*/
|
||||
// creation
|
||||
td::Ref<ManualDns> ManualDns::create(td::Ref<vm::Cell> data) {
|
||||
return td::Ref<ManualDns>(true, State{ton::SmartContractCode::dns_manual(), std::move(data)});
|
||||
}
|
||||
td::Ref<ManualDns> ManualDns::create(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id) {
|
||||
return create(create_init_data_fast(public_key, wallet_id));
|
||||
}
|
||||
|
||||
td::Result<td::uint32> ManualDns::get_wallet_id() const {
|
||||
return TRY_VM(get_wallet_id_or_throw());
|
||||
}
|
||||
td::Result<td::uint32> ManualDns::get_wallet_id_or_throw() const {
|
||||
if (state_.data.is_null()) {
|
||||
return 0;
|
||||
}
|
||||
//FIXME use get method
|
||||
return static_cast<td::uint32>(vm::load_cell_slice(state_.data).fetch_ulong(32));
|
||||
}
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> ManualDns::create_set_value_unsigned(td::int16 category, td::Slice name,
|
||||
td::Ref<vm::Cell> data) const {
|
||||
//11 VSet: set specified value to specified subdomain->category (x=2)
|
||||
//[Int<16b>:category] [Name<?>:subdomain] [Cell<1r>:value]
|
||||
vm::CellBuilder cb;
|
||||
cb.store_long(11, 6);
|
||||
if (name.size() <= 58 - 2) {
|
||||
cb.store_long(0, 1);
|
||||
cb.store_long(category, 16);
|
||||
cb.store_long(name.size(), 6);
|
||||
cb.store_bytes(name);
|
||||
} else {
|
||||
cb.store_long(1, 1);
|
||||
cb.store_long(category, 16);
|
||||
cb.store_ref(vm::CellBuilder().store_bytes(name).finalize());
|
||||
}
|
||||
cb.store_maybe_ref(std::move(data));
|
||||
return cb.finalize();
|
||||
}
|
||||
td::Result<td::Ref<vm::Cell>> ManualDns::create_delete_value_unsigned(td::int16 category, td::Slice name) const {
|
||||
//12 VDel: delete specified subdomain->category (x=2)
|
||||
//[Int<16b>:category] [Name<?>:subdomain]
|
||||
vm::CellBuilder cb;
|
||||
cb.store_long(12, 6);
|
||||
if (name.size() <= 58 - 2) {
|
||||
cb.store_long(0, 1);
|
||||
cb.store_long(category, 16);
|
||||
cb.store_long(name.size(), 6);
|
||||
cb.store_bytes(name);
|
||||
} else {
|
||||
cb.store_long(1, 1);
|
||||
cb.store_long(category, 16);
|
||||
cb.store_ref(vm::CellBuilder().store_bytes(name).finalize());
|
||||
}
|
||||
cb.store_long(0, 1);
|
||||
return cb.finalize();
|
||||
}
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> ManualDns::create_delete_all_unsigned() const {
|
||||
// 32 TDel: nullify ENTIRE DOMAIN TABLE (x=-)
|
||||
vm::CellBuilder cb;
|
||||
cb.store_long(32, 6);
|
||||
cb.store_long(0, 1);
|
||||
return cb.finalize();
|
||||
}
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> ManualDns::create_set_all_unsigned(td::Span<Action> entries) const {
|
||||
vm::PrefixDictionary pdict(1023);
|
||||
for (auto& action : entries) {
|
||||
auto name_key = encode_name(action.name);
|
||||
int zero_cnt = 0;
|
||||
for (auto c : name_key) {
|
||||
if (c == 0) {
|
||||
zero_cnt++;
|
||||
}
|
||||
}
|
||||
auto new_name_key = vm::load_cell_slice(vm::CellBuilder().store_long(zero_cnt, 7).store_bytes(name_key).finalize());
|
||||
auto ptr = new_name_key.data_bits();
|
||||
auto ptr_size = new_name_key.size();
|
||||
auto o_dict = pdict.lookup(ptr, ptr_size);
|
||||
td::Ref<vm::Cell> dict_root;
|
||||
if (o_dict.not_null()) {
|
||||
o_dict->prefetch_maybe_ref(dict_root);
|
||||
}
|
||||
vm::Dictionary dict(dict_root, 16);
|
||||
if (!action.data.value().is_null()) {
|
||||
auto key = dict.integer_key(td::make_refint(action.category), 16);
|
||||
dict.set_ref(key.bits(), 16, action.data.value());
|
||||
}
|
||||
pdict.set(ptr, ptr_size, dict.get_root());
|
||||
}
|
||||
|
||||
vm::CellBuilder cb;
|
||||
cb.store_long(31, 6);
|
||||
cb.store_long(1, 1);
|
||||
|
||||
cb.store_maybe_ref(pdict.get_root_cell());
|
||||
|
||||
return cb.finalize();
|
||||
}
|
||||
|
||||
//21 DSet: replace entire category dictionary of domain with provided (x=0)
|
||||
//[Name<?>:subdomain] [Cell<1r>:new_cat_table]
|
||||
//22 DDel: delete entire category dictionary of specified domain (x=0)
|
||||
//[Name<?>:subdomain]
|
||||
td::Result<td::Ref<vm::Cell>> ManualDns::create_delete_name_unsigned(td::Slice name) const {
|
||||
vm::CellBuilder cb;
|
||||
cb.store_long(22, 6);
|
||||
if (name.size() <= 58) {
|
||||
cb.store_long(0, 1);
|
||||
cb.store_long(name.size(), 6);
|
||||
cb.store_bytes(name);
|
||||
} else {
|
||||
cb.store_long(1, 1);
|
||||
cb.store_ref(vm::CellBuilder().store_bytes(name).finalize());
|
||||
}
|
||||
cb.store_long(0, 1);
|
||||
return cb.finalize();
|
||||
}
|
||||
td::Result<td::Ref<vm::Cell>> ManualDns::create_set_name_unsigned(td::Slice name, td::Span<Action> entries) const {
|
||||
vm::CellBuilder cb;
|
||||
cb.store_long(21, 6);
|
||||
if (name.size() <= 58) {
|
||||
cb.store_long(0, 1);
|
||||
cb.store_long(name.size(), 6);
|
||||
cb.store_bytes(name);
|
||||
} else {
|
||||
cb.store_long(1, 1);
|
||||
cb.store_ref(vm::CellBuilder().store_bytes(name).finalize());
|
||||
}
|
||||
|
||||
vm::Dictionary dict(16);
|
||||
|
||||
for (auto& action : entries) {
|
||||
if (action.data.value().is_null()) {
|
||||
continue;
|
||||
}
|
||||
auto key = dict.integer_key(td::make_refint(action.category), 16);
|
||||
dict.set_ref(key.bits(), 16, action.data.value());
|
||||
}
|
||||
cb.store_maybe_ref(dict.get_root_cell());
|
||||
|
||||
return cb.finalize();
|
||||
}
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> ManualDns::prepare(td::Ref<vm::Cell> data, td::uint32 valid_until) const {
|
||||
TRY_RESULT(wallet_id, get_wallet_id());
|
||||
auto hash = data->get_hash().as_slice().substr(28, 4).str();
|
||||
|
||||
vm::CellBuilder cb;
|
||||
cb.store_long(wallet_id, 32).store_long(valid_until, 32);
|
||||
//cb.store_bytes(hash);
|
||||
cb.store_long(td::Random::secure_uint32(), 32);
|
||||
cb.append_cellslice(vm::load_cell_slice(data));
|
||||
return cb.finalize();
|
||||
}
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> ManualDns::sign(const td::Ed25519::PrivateKey& private_key, td::Ref<vm::Cell> data) {
|
||||
auto signature = private_key.sign(data->get_hash().as_slice()).move_as_ok();
|
||||
vm::CellBuilder cb;
|
||||
cb.store_bytes(signature.as_slice());
|
||||
cb.append_cellslice(vm::load_cell_slice(data));
|
||||
return cb.finalize();
|
||||
}
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> ManualDns::create_init_query(const td::Ed25519::PrivateKey& private_key,
|
||||
td::uint32 valid_until) const {
|
||||
vm::CellBuilder cb;
|
||||
cb.store_long(0, 6);
|
||||
cb.store_long(0, 1);
|
||||
|
||||
TRY_RESULT(prepared, prepare(cb.finalize(), valid_until));
|
||||
return sign(private_key, std::move(prepared));
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> ManualDns::create_init_data_fast(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id) {
|
||||
vm::CellBuilder cb;
|
||||
cb.store_long(wallet_id, 32).store_long(0, 64).store_bytes(public_key.as_octet_string());
|
||||
CHECK(cb.store_maybe_ref({}));
|
||||
CHECK(cb.store_maybe_ref({}));
|
||||
return cb.finalize();
|
||||
}
|
||||
|
||||
size_t ManualDns::get_max_name_size() const {
|
||||
return 128;
|
||||
}
|
||||
|
||||
td::Result<std::vector<ManualDns::RawEntry>> ManualDns::resolve_raw(td::Slice name, td::int32 category_big) const {
|
||||
return TRY_VM(resolve_raw_or_throw(name, category_big));
|
||||
}
|
||||
td::Result<std::vector<ManualDns::RawEntry>> ManualDns::resolve_raw_or_throw(td::Slice name,
|
||||
td::int32 category_big) const {
|
||||
TRY_RESULT(category, td::narrow_cast_safe<td::int16>(category_big));
|
||||
if (name.size() > get_max_name_size()) {
|
||||
return td::Status::Error("Name is too long");
|
||||
}
|
||||
auto encoded_name = encode_name(name);
|
||||
auto res = run_get_method(
|
||||
"dnsresolve",
|
||||
{vm::load_cell_slice_ref(vm::CellBuilder().store_bytes(encoded_name).finalize()), td::make_refint(category)});
|
||||
if (!res.success) {
|
||||
return td::Status::Error("get method failed");
|
||||
}
|
||||
std::vector<RawEntry> vec;
|
||||
auto data = res.stack.write().pop_maybe_cell();
|
||||
if (data.is_null()) {
|
||||
return vec;
|
||||
}
|
||||
size_t prefix_size = res.stack.write().pop_smallint_range((int)encoded_name.size() * 8);
|
||||
if (prefix_size % 8 != 0) {
|
||||
return td::Status::Error("Prefix size is not divisible by 8");
|
||||
}
|
||||
prefix_size /= 8;
|
||||
if (prefix_size < encoded_name.size()) {
|
||||
vec.push_back({decode_name(td::Slice(encoded_name).substr(0, prefix_size)), -1, data});
|
||||
} else {
|
||||
if (category == 0) {
|
||||
vm::Dictionary dict(std::move(data), 16);
|
||||
dict.check_for_each([&](auto cs, auto x, auto y) {
|
||||
td::BigInt256 cat;
|
||||
cat.import_bits(x, y, true);
|
||||
vec.push_back({name.str(), td::narrow_cast<td::int16>(cat.to_long()), cs->prefetch_ref()});
|
||||
return true;
|
||||
});
|
||||
} else {
|
||||
vec.push_back({name.str(), category, data});
|
||||
}
|
||||
}
|
||||
|
||||
return vec;
|
||||
}
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> ManualDns::create_update_query(CombinedActions<Action>& combined) const {
|
||||
if (combined.name.empty()) {
|
||||
if (combined.actions.value().empty()) {
|
||||
return create_delete_all_unsigned();
|
||||
}
|
||||
return create_set_all_unsigned(combined.actions.value());
|
||||
}
|
||||
if (combined.category == 0) {
|
||||
if (!combined.actions) {
|
||||
return create_delete_name_unsigned(encode_name(combined.name));
|
||||
}
|
||||
return create_set_name_unsigned(encode_name(combined.name), combined.actions.value());
|
||||
}
|
||||
CHECK(combined.actions.value().size() == 1);
|
||||
auto& action = combined.actions.value()[0];
|
||||
if (action.data) {
|
||||
return create_set_value_unsigned(action.category, encode_name(action.name), action.data.value());
|
||||
} else {
|
||||
return create_delete_value_unsigned(action.category, encode_name(action.name));
|
||||
}
|
||||
}
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> ManualDns::create_update_query(td::Ed25519::PrivateKey& pk, td::Span<Action> actions,
|
||||
td::uint32 valid_until) const {
|
||||
auto combined = combine_actions(actions);
|
||||
std::vector<td::Ref<vm::Cell>> queries;
|
||||
for (auto& c : combined) {
|
||||
TRY_RESULT(q, create_update_query(c));
|
||||
queries.push_back(std::move(q));
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> combined_query;
|
||||
for (auto& query : td::reversed(queries)) {
|
||||
if (combined_query.is_null()) {
|
||||
combined_query = std::move(query);
|
||||
} else {
|
||||
auto next = vm::load_cell_slice(combined_query);
|
||||
combined_query = vm::CellBuilder()
|
||||
.append_cellslice(vm::load_cell_slice(query))
|
||||
.store_ref(vm::CellBuilder().append_cellslice(next).finalize())
|
||||
.finalize();
|
||||
}
|
||||
}
|
||||
|
||||
TRY_RESULT(prepared, prepare(std::move(combined_query), valid_until));
|
||||
return sign(pk, std::move(prepared));
|
||||
}
|
||||
|
||||
std::string ManualDns::encode_name(td::Slice name) {
|
||||
std::string res;
|
||||
while (!name.empty()) {
|
||||
auto pos = name.rfind('.');
|
||||
if (pos == name.npos) {
|
||||
res += name.str();
|
||||
name = td::Slice();
|
||||
} else {
|
||||
res += name.substr(pos + 1).str();
|
||||
name.truncate(pos);
|
||||
}
|
||||
res += '\0';
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string ManualDns::decode_name(td::Slice name) {
|
||||
std::string res;
|
||||
if (!name.empty() && name.back() == 0) {
|
||||
name.remove_suffix(1);
|
||||
}
|
||||
while (!name.empty()) {
|
||||
auto pos = name.rfind('\0');
|
||||
if (!res.empty()) {
|
||||
res += '.';
|
||||
}
|
||||
if (pos == name.npos) {
|
||||
res += name.str();
|
||||
name = td::Slice();
|
||||
} else {
|
||||
res += name.substr(pos + 1).str();
|
||||
name.truncate(pos);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string ManualDns::serialize_data(const EntryData& data) {
|
||||
std::string res;
|
||||
data.data.visit(td::overloaded([&](const ton::ManualDns::EntryDataText& text) { res = "UNSUPPORTED"; },
|
||||
[&](const ton::ManualDns::EntryDataNextResolver& resolver) { res = "UNSUPPORTED"; },
|
||||
[&](const ton::ManualDns::EntryDataAdnlAddress& adnl_address) { res = "UNSUPPORTED"; },
|
||||
[&](const ton::ManualDns::EntryDataSmcAddress& text) { res = "UNSUPPORTED"; }));
|
||||
return res;
|
||||
}
|
||||
|
||||
td::Result<td::optional<ManualDns::EntryData>> ManualDns::parse_data(td::Slice cmd) {
|
||||
td::ConstParser parser(cmd);
|
||||
parser.skip_whitespaces();
|
||||
auto type = parser.read_till(':');
|
||||
parser.advance(1);
|
||||
if (type == "TEXT") {
|
||||
return ManualDns::EntryData::text(parser.read_all().str());
|
||||
} else if (type == "DELETED") {
|
||||
return {};
|
||||
}
|
||||
return td::Status::Error(PSLICE() << "Unknown entry type: " << type);
|
||||
}
|
||||
|
||||
td::Result<ManualDns::ActionExt> ManualDns::parse_line(td::Slice cmd) {
|
||||
// Cmd =
|
||||
// set name category data |
|
||||
// delete.name name |
|
||||
// delete.all
|
||||
// data =
|
||||
// TEXT:<text> |
|
||||
// DELETED
|
||||
td::ConstParser parser(cmd);
|
||||
auto type = parser.read_word();
|
||||
if (type == "set") {
|
||||
auto name = parser.read_word();
|
||||
auto category_str = parser.read_word();
|
||||
TRY_RESULT(category, td::to_integer_safe<td::int16>(category_str));
|
||||
TRY_RESULT(data, parse_data(parser.read_all()));
|
||||
return ManualDns::ActionExt{name.str(), category, std::move(data)};
|
||||
} else if (type == "delete.name") {
|
||||
auto name = parser.read_word();
|
||||
if (name.empty()) {
|
||||
return td::Status::Error("name is empty");
|
||||
}
|
||||
return ManualDns::ActionExt{name.str(), 0, {}};
|
||||
} else if (type == "delete.all") {
|
||||
return ManualDns::ActionExt{"", 0, {}};
|
||||
}
|
||||
return td::Status::Error(PSLICE() << "Unknown command: " << type);
|
||||
}
|
||||
|
||||
td::Result<std::vector<ManualDns::ActionExt>> ManualDns::parse(td::Slice cmd) {
|
||||
auto lines = td::full_split(cmd, '\n');
|
||||
std::vector<ManualDns::ActionExt> res;
|
||||
res.reserve(lines.size());
|
||||
for (auto& line : lines) {
|
||||
td::ConstParser parser(line);
|
||||
parser.skip_whitespaces();
|
||||
if (parser.empty()) {
|
||||
continue;
|
||||
}
|
||||
TRY_RESULT(action, parse_line(parser.read_all()));
|
||||
res.push_back(std::move(action));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace ton
|
339
crypto/smc-envelope/ManualDns.h
Normal file
339
crypto/smc-envelope/ManualDns.h
Normal file
|
@ -0,0 +1,339 @@
|
|||
/*
|
||||
This file is part of TON Blockchain Library.
|
||||
|
||||
TON Blockchain Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
TON Blockchain Library 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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2019-2020 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
#include "td/utils/Variant.h"
|
||||
#include "td/utils/Status.h"
|
||||
#include "vm/cells/Cell.h"
|
||||
#include "vm/cells/CellSlice.h"
|
||||
#include "vm/cells/CellString.h"
|
||||
|
||||
#include "smc-envelope/SmartContract.h"
|
||||
|
||||
#include "Ed25519.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace ton {
|
||||
class DnsInterface {
|
||||
public:
|
||||
struct EntryDataText {
|
||||
std::string text;
|
||||
bool operator==(const EntryDataText& other) const {
|
||||
return text == other.text;
|
||||
}
|
||||
};
|
||||
|
||||
struct EntryDataNextResolver {
|
||||
block::StdAddress resolver;
|
||||
bool operator==(const EntryDataNextResolver& other) const {
|
||||
return resolver == other.resolver;
|
||||
}
|
||||
};
|
||||
|
||||
struct EntryDataAdnlAddress {
|
||||
ton::Bits256 adnl_address;
|
||||
// TODO: proto
|
||||
bool operator==(const EntryDataAdnlAddress& other) const {
|
||||
return adnl_address == other.adnl_address;
|
||||
}
|
||||
};
|
||||
|
||||
struct EntryDataSmcAddress {
|
||||
block::StdAddress smc_address;
|
||||
bool operator==(const EntryDataSmcAddress& other) const {
|
||||
return smc_address == other.smc_address;
|
||||
}
|
||||
// TODO: capability
|
||||
};
|
||||
|
||||
struct EntryData {
|
||||
enum Type { Empty, Text, NextResolver, AdnlAddress, SmcAddress } type{Empty};
|
||||
td::Variant<EntryDataText, EntryDataNextResolver, EntryDataAdnlAddress, EntryDataSmcAddress> data;
|
||||
|
||||
static EntryData text(std::string text) {
|
||||
return {Text, EntryDataText{text}};
|
||||
}
|
||||
static EntryData next_resolver(block::StdAddress resolver) {
|
||||
return {NextResolver, EntryDataNextResolver{resolver}};
|
||||
}
|
||||
static EntryData adnl_address(ton::Bits256 adnl_address) {
|
||||
return {AdnlAddress, EntryDataAdnlAddress{adnl_address}};
|
||||
}
|
||||
static EntryData smc_address(block::StdAddress smc_address) {
|
||||
return {SmcAddress, EntryDataSmcAddress{smc_address}};
|
||||
}
|
||||
|
||||
bool operator==(const EntryData& other) const {
|
||||
return data == other.data;
|
||||
}
|
||||
friend td::StringBuilder& operator<<(td::StringBuilder& sb, const EntryData& data) {
|
||||
switch (data.type) {
|
||||
case Type::Empty:
|
||||
return sb << "<empty>";
|
||||
case Type::Text:
|
||||
return sb << "text{" << data.data.get<EntryDataText>().text << "}";
|
||||
case Type::NextResolver:
|
||||
return sb << "next{" << data.data.get<EntryDataNextResolver>().resolver.rserialize() << "}";
|
||||
case Type::AdnlAddress:
|
||||
return sb << "adnl{" << data.data.get<EntryDataAdnlAddress>().adnl_address.to_hex() << "}";
|
||||
case Type::SmcAddress:
|
||||
return sb << "smc{" << data.data.get<EntryDataSmcAddress>().smc_address.rserialize() << "}";
|
||||
}
|
||||
return sb << "<unknown>";
|
||||
}
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> as_cell() const;
|
||||
static td::Result<EntryData> from_cellslice(vm::CellSlice& cs);
|
||||
};
|
||||
|
||||
struct Entry {
|
||||
std::string name;
|
||||
td::int16 category;
|
||||
EntryData data;
|
||||
auto key() const {
|
||||
return std::tie(name, category);
|
||||
}
|
||||
bool operator<(const Entry& other) const {
|
||||
return key() < other.key();
|
||||
}
|
||||
bool operator==(const Entry& other) const {
|
||||
return key() == other.key() && data == other.data;
|
||||
}
|
||||
friend td::StringBuilder& operator<<(td::StringBuilder& sb, const Entry& entry) {
|
||||
sb << entry.name << ":" << entry.category << ":" << entry.data;
|
||||
return sb;
|
||||
}
|
||||
};
|
||||
struct RawEntry {
|
||||
std::string name;
|
||||
td::int16 category;
|
||||
td::Ref<vm::Cell> data;
|
||||
};
|
||||
|
||||
struct ActionExt {
|
||||
std::string name;
|
||||
td::int16 category;
|
||||
td::optional<EntryData> data;
|
||||
static td::Result<ActionExt> parse(td::Slice);
|
||||
};
|
||||
|
||||
struct Action {
|
||||
std::string name;
|
||||
td::int16 category;
|
||||
td::optional<td::Ref<vm::Cell>> data;
|
||||
|
||||
bool does_create_category() const {
|
||||
CHECK(!name.empty());
|
||||
CHECK(category != 0);
|
||||
return static_cast<bool>(data);
|
||||
}
|
||||
bool does_change_empty() const {
|
||||
CHECK(!name.empty());
|
||||
CHECK(category != 0);
|
||||
return static_cast<bool>(data) && data.value().not_null();
|
||||
}
|
||||
void make_non_empty() {
|
||||
CHECK(!name.empty());
|
||||
CHECK(category != 0);
|
||||
if (!data) {
|
||||
data = td::Ref<vm::Cell>();
|
||||
}
|
||||
}
|
||||
friend td::StringBuilder& operator<<(td::StringBuilder& sb, const Action& action) {
|
||||
sb << action.name << ":" << action.category << ":";
|
||||
if (action.data) {
|
||||
if (action.data.value().is_null()) {
|
||||
sb << "<null>";
|
||||
} else {
|
||||
sb << "<data>";
|
||||
}
|
||||
} else {
|
||||
sb << "<empty>";
|
||||
}
|
||||
return sb;
|
||||
}
|
||||
};
|
||||
|
||||
virtual ~DnsInterface() {
|
||||
}
|
||||
virtual size_t get_max_name_size() const = 0;
|
||||
virtual td::Result<std::vector<RawEntry>> resolve_raw(td::Slice name, td::int32 category) const = 0;
|
||||
virtual td::Result<td::Ref<vm::Cell>> create_update_query(
|
||||
td::Ed25519::PrivateKey& pk, td::Span<Action> actions,
|
||||
td::uint32 valid_until = std::numeric_limits<td::uint32>::max()) const = 0;
|
||||
|
||||
td::Result<std::vector<Entry>> resolve(td::Slice name, td::int32 category) const;
|
||||
};
|
||||
|
||||
class ManualDns : public ton::SmartContract, public DnsInterface {
|
||||
public:
|
||||
ManualDns(State state) : SmartContract(std::move(state)) {
|
||||
}
|
||||
|
||||
ManualDns* make_copy() const override {
|
||||
return new ManualDns{state_};
|
||||
}
|
||||
|
||||
// creation
|
||||
static td::Ref<ManualDns> create(State state) {
|
||||
return td::Ref<ManualDns>(true, std::move(state));
|
||||
}
|
||||
static td::Ref<ManualDns> create(td::Ref<vm::Cell> data = {});
|
||||
static td::Ref<ManualDns> create(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id);
|
||||
|
||||
static std::string serialize_data(const EntryData& data);
|
||||
static td::Result<td::optional<ManualDns::EntryData>> parse_data(td::Slice cmd);
|
||||
static td::Result<ManualDns::ActionExt> parse_line(td::Slice cmd);
|
||||
static td::Result<std::vector<ManualDns::ActionExt>> parse(td::Slice cmd);
|
||||
|
||||
td::Ref<vm::Cell> create_init_data(const td::Ed25519::PublicKey& public_key, td::uint32 valid_until) const {
|
||||
return create_init_data_fast(public_key, valid_until);
|
||||
}
|
||||
|
||||
td::Result<td::uint32> get_wallet_id() const;
|
||||
td::Result<td::uint32> get_wallet_id_or_throw() const;
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> create_set_value_unsigned(td::int16 category, td::Slice name,
|
||||
td::Ref<vm::Cell> data) const;
|
||||
td::Result<td::Ref<vm::Cell>> create_delete_value_unsigned(td::int16 category, td::Slice name) const;
|
||||
td::Result<td::Ref<vm::Cell>> create_delete_all_unsigned() const;
|
||||
td::Result<td::Ref<vm::Cell>> create_set_all_unsigned(td::Span<Action> entries) const;
|
||||
td::Result<td::Ref<vm::Cell>> create_delete_name_unsigned(td::Slice name) const;
|
||||
td::Result<td::Ref<vm::Cell>> create_set_name_unsigned(td::Slice name, td::Span<Action> entries) const;
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> prepare(td::Ref<vm::Cell> data, td::uint32 valid_until) const;
|
||||
|
||||
static td::Result<td::Ref<vm::Cell>> sign(const td::Ed25519::PrivateKey& private_key, td::Ref<vm::Cell> data);
|
||||
static td::Ref<vm::Cell> create_init_data_fast(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id);
|
||||
|
||||
size_t get_max_name_size() const override;
|
||||
td::Result<std::vector<RawEntry>> resolve_raw(td::Slice name, td::int32 category_big) const override;
|
||||
td::Result<std::vector<RawEntry>> resolve_raw_or_throw(td::Slice name, td::int32 category_big) const;
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> create_init_query(
|
||||
const td::Ed25519::PrivateKey& private_key,
|
||||
td::uint32 valid_until = std::numeric_limits<td::uint32>::max()) const;
|
||||
td::Result<td::Ref<vm::Cell>> create_update_query(
|
||||
td::Ed25519::PrivateKey& pk, td::Span<Action> actions,
|
||||
td::uint32 valid_until = std::numeric_limits<td::uint32>::max()) const override;
|
||||
|
||||
static std::string encode_name(td::Slice name);
|
||||
static std::string decode_name(td::Slice name);
|
||||
|
||||
template <class ActionT>
|
||||
struct CombinedActions {
|
||||
std::string name;
|
||||
td::int16 category{0};
|
||||
td::optional<std::vector<ActionT>> actions;
|
||||
friend td::StringBuilder& operator<<(td::StringBuilder& sb, const CombinedActions& action) {
|
||||
sb << action.name << ":" << action.category << ":";
|
||||
if (action.actions) {
|
||||
sb << "<data>" << action.actions.value().size();
|
||||
} else {
|
||||
sb << "<empty>";
|
||||
}
|
||||
return sb;
|
||||
}
|
||||
};
|
||||
|
||||
template <class ActionT = Action>
|
||||
static std::vector<CombinedActions<ActionT>> combine_actions(td::Span<ActionT> actions) {
|
||||
struct Info {
|
||||
std::set<td::int16> known_category;
|
||||
std::vector<ActionT> actions;
|
||||
bool closed{false};
|
||||
bool non_empty{false};
|
||||
};
|
||||
|
||||
std::map<std::string, Info> mp;
|
||||
std::vector<CombinedActions<ActionT>> res;
|
||||
for (auto& action : td::reversed(actions)) {
|
||||
if (action.name.empty()) {
|
||||
CombinedActions<ActionT> set_all;
|
||||
set_all.actions = std::vector<ActionT>();
|
||||
for (auto& it : mp) {
|
||||
for (auto& e : it.second.actions) {
|
||||
if (e.does_create_category()) {
|
||||
set_all.actions.value().push_back(std::move(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
res.push_back(std::move(set_all));
|
||||
return res;
|
||||
}
|
||||
|
||||
Info& info = mp[action.name];
|
||||
if (info.closed) {
|
||||
continue;
|
||||
}
|
||||
if (action.category != 0 && action.does_create_category()) {
|
||||
info.non_empty = true;
|
||||
}
|
||||
if (!info.known_category.insert(action.category).second) {
|
||||
continue;
|
||||
}
|
||||
if (action.category == 0) {
|
||||
info.closed = true;
|
||||
auto old_actions = std::move(info.actions);
|
||||
bool is_empty = true;
|
||||
for (auto& action : old_actions) {
|
||||
if (is_empty && action.does_create_category()) {
|
||||
info.actions.push_back(std::move(action));
|
||||
is_empty = false;
|
||||
} else if (!is_empty && action.does_change_empty()) {
|
||||
info.actions.push_back(std::move(action));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
info.actions.push_back(std::move(action));
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& it : mp) {
|
||||
auto& info = it.second;
|
||||
if (info.closed) {
|
||||
CombinedActions<ActionT> ca;
|
||||
ca.name = it.first;
|
||||
ca.category = 0;
|
||||
if (!info.actions.empty() || info.non_empty) {
|
||||
ca.actions = std::move(info.actions);
|
||||
}
|
||||
res.push_back(std::move(ca));
|
||||
} else {
|
||||
bool need_non_empty = info.non_empty;
|
||||
for (auto& a : info.actions) {
|
||||
if (need_non_empty) {
|
||||
a.make_non_empty();
|
||||
need_non_empty = false;
|
||||
}
|
||||
CombinedActions<ActionT> ca;
|
||||
ca.name = a.name;
|
||||
ca.category = a.category;
|
||||
ca.actions = std::vector<ActionT>();
|
||||
ca.actions.value().push_back(std::move(a));
|
||||
res.push_back(ca);
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
td::Result<td::Ref<vm::Cell>> create_update_query(CombinedActions<Action>& combined) const;
|
||||
};
|
||||
|
||||
} // namespace ton
|
|
@ -14,7 +14,7 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#include "SmartContract.h"
|
||||
|
||||
|
@ -24,7 +24,7 @@
|
|||
#include "block/block-auto.h"
|
||||
#include "vm/cellslice.h"
|
||||
#include "vm/cp0.h"
|
||||
#include "vm/continuation.h"
|
||||
#include "vm/vm.h"
|
||||
|
||||
#include "td/utils/crypto.h"
|
||||
|
||||
|
|
|
@ -14,13 +14,13 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "vm/cells.h"
|
||||
#include "vm/stack.hpp"
|
||||
#include "vm/continuation.h"
|
||||
#include "vm/vm.h"
|
||||
|
||||
#include "td/utils/optional.h"
|
||||
#include "td/utils/crypto.h"
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#include "SmartContractCode.h"
|
||||
|
||||
|
@ -25,6 +25,11 @@
|
|||
|
||||
namespace ton {
|
||||
namespace {
|
||||
constexpr static int WALLET_REVISION = 2;
|
||||
constexpr static int WALLET2_REVISION = 2;
|
||||
constexpr static int WALLET3_REVISION = 2;
|
||||
constexpr static int HIGHLOAD_WALLET_REVISION = 2;
|
||||
constexpr static int HIGHLOAD_WALLET2_REVISION = 2;
|
||||
const auto& get_map() {
|
||||
static auto map = [] {
|
||||
std::map<std::string, td::Ref<vm::Cell>, std::less<>> map;
|
||||
|
@ -36,6 +41,58 @@ const auto& get_map() {
|
|||
#include "smartcont/auto/simple-wallet-code.cpp"
|
||||
#include "smartcont/auto/wallet-code.cpp"
|
||||
#include "smartcont/auto/highload-wallet-code.cpp"
|
||||
#include "smartcont/auto/highload-wallet-v2-code.cpp"
|
||||
#include "smartcont/auto/dns-manual-code.cpp"
|
||||
|
||||
with_tvm_code("highload-wallet-r1",
|
||||
"te6ccgEBBgEAhgABFP8A9KQT9KDyyAsBAgEgAgMCAUgEBQC88oMI1xgg0x/TH9Mf+CMTu/Jj7UTQ0x/TH9P/"
|
||||
"0VEyuvKhUUS68qIE+QFUEFX5EPKj9ATR+AB/jhghgBD0eG+hb6EgmALTB9QwAfsAkTLiAbPmWwGkyMsfyx/L/"
|
||||
"8ntVAAE0DAAEaCZL9qJoa4WPw==");
|
||||
with_tvm_code("highload-wallet-r2",
|
||||
"te6ccgEBCAEAmQABFP8A9KQT9LzyyAsBAgEgAgMCAUgEBQC88oMI1xgg0x/TH9Mf+CMTu/Jj7UTQ0x/TH9P/"
|
||||
"0VEyuvKhUUS68qIE+QFUEFX5EPKj9ATR+AB/jhghgBD0eG+hb6EgmALTB9QwAfsAkTLiAbPmWwGkyMsfyx/L/"
|
||||
"8ntVAAE0DACAUgGBwAXuznO1E0NM/MdcL/4ABG4yX7UTQ1wsfg=");
|
||||
with_tvm_code("highload-wallet-v2-r1",
|
||||
"te6ccgEBBwEA1gABFP8A9KQT9KDyyAsBAgEgAgMCAUgEBQHu8oMI1xgg0x/TP/gjqh9TILnyY+1E0NMf0z/T//"
|
||||
"QE0VNggED0Dm+hMfJgUXO68qIH+QFUEIf5EPKjAvQE0fgAf44YIYAQ9HhvoW+"
|
||||
"hIJgC0wfUMAH7AJEy4gGz5luDJaHIQDSAQPRDiuYxyBLLHxPLP8v/9ADJ7VQGAATQMABBoZfl2omhpj5jpn+n/"
|
||||
"mPoCaKkQQCB6BzfQmMktv8ld0fFADgggED0lm+hb6EyURCUMFMDud4gkzM2AZIyMOKz");
|
||||
with_tvm_code("highload-wallet-v2-r2",
|
||||
"te6ccgEBCQEA6QABFP8A9KQT9LzyyAsBAgEgAgMCAUgEBQHu8oMI1xgg0x/TP/gjqh9TILnyY+1E0NMf0z/T//"
|
||||
"QE0VNggED0Dm+hMfJgUXO68qIH+QFUEIf5EPKjAvQE0fgAf44YIYAQ9HhvoW+"
|
||||
"hIJgC0wfUMAH7AJEy4gGz5luDJaHIQDSAQPRDiuYxyBLLHxPLP8v/9ADJ7VQIAATQMAIBIAYHABe9nOdqJoaa+Y64X/"
|
||||
"wAQb5fl2omhpj5jpn+n/mPoCaKkQQCB6BzfQmMktv8ld0fFAA4IIBA9JZvoW+hMlEQlDBTA7neIJMzNgGSMjDisw==");
|
||||
with_tvm_code("simple-wallet-r1",
|
||||
"te6ccgEEAQEAAAAAUwAAov8AIN0gggFMl7qXMO1E0NcLH+Ck8mCBAgDXGCDXCx/tRNDTH9P/"
|
||||
"0VESuvKhIvkBVBBE+RDyovgAAdMfMSDXSpbTB9QC+wDe0aTIyx/L/8ntVA==");
|
||||
with_tvm_code("simple-wallet-r2",
|
||||
"te6ccgEBAQEAXwAAuv8AIN0gggFMl7ohggEznLqxnHGw7UTQ0x/XC//jBOCk8mCBAgDXGCDXCx/tRNDTH9P/"
|
||||
"0VESuvKhIvkBVBBE+RDyovgAAdMfMSDXSpbTB9QC+wDe0aTIyx/L/8ntVA==");
|
||||
with_tvm_code("wallet-r1",
|
||||
"te6ccgEBAQEAVwAAqv8AIN0gggFMl7qXMO1E0NcLH+Ck8mCDCNcYINMf0x8B+CO78mPtRNDTH9P/0VExuvKhA/"
|
||||
"kBVBBC+RDyovgAApMg10qW0wfUAvsA6NGkyMsfy//J7VQ=");
|
||||
with_tvm_code("wallet-r2",
|
||||
"te6ccgEBAQEAYwAAwv8AIN0gggFMl7ohggEznLqxnHGw7UTQ0x/XC//jBOCk8mCDCNcYINMf0x8B+CO78mPtRNDTH9P/"
|
||||
"0VExuvKhA/kBVBBC+RDyovgAApMg10qW0wfUAvsA6NGkyMsfy//J7VQ=");
|
||||
with_tvm_code("wallet3-r1",
|
||||
"te6ccgEBAQEAYgAAwP8AIN0gggFMl7qXMO1E0NcLH+Ck8mCDCNcYINMf0x/TH/gjE7vyY+1E0NMf0x/T/"
|
||||
"9FRMrryoVFEuvKiBPkBVBBV+RDyo/gAkyDXSpbTB9QC+wDo0QGkyMsfyx/L/8ntVA==");
|
||||
with_tvm_code("wallet3-r2",
|
||||
"te6ccgEBAQEAcQAA3v8AIN0gggFMl7ohggEznLqxn3Gw7UTQ0x/THzHXC//jBOCk8mCDCNcYINMf0x/TH/gjE7vyY+1E0NMf0x/"
|
||||
"T/9FRMrryoVFEuvKiBPkBVBBV+RDyo/gAkyDXSpbTB9QC+wDo0QGkyMsfyx/L/8ntVA==");
|
||||
auto check_revision = [&](td::Slice name, td::int32 default_revision) {
|
||||
auto it = map.find(name);
|
||||
CHECK(it != map.end());
|
||||
auto other_it = map.find(PSLICE() << name << "-r" << default_revision);
|
||||
CHECK(other_it != map.end());
|
||||
CHECK(it->second->get_hash() == other_it->second->get_hash());
|
||||
};
|
||||
check_revision("highload-wallet", HIGHLOAD_WALLET_REVISION);
|
||||
check_revision("highload-wallet-v2", HIGHLOAD_WALLET2_REVISION);
|
||||
|
||||
//check_revision("simple-wallet", WALLET_REVISION);
|
||||
//check_revision("wallet", WALLET2_REVISION);
|
||||
//check_revision("wallet3", WALLET3_REVISION);
|
||||
return map;
|
||||
}();
|
||||
return map;
|
||||
|
@ -46,7 +103,7 @@ td::Result<td::Ref<vm::Cell>> SmartContractCode::load(td::Slice name) {
|
|||
auto& map = get_map();
|
||||
auto it = map.find(name);
|
||||
if (it == map.end()) {
|
||||
return td::Status::Error(PSLICE() << "Can't load td::ref<vm::cell " << name);
|
||||
return td::Status::Error(PSLICE() << "Can't load td::Ref<vm::Cell> " << name);
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
@ -54,20 +111,47 @@ td::Ref<vm::Cell> SmartContractCode::multisig() {
|
|||
auto res = load("multisig").move_as_ok();
|
||||
return res;
|
||||
}
|
||||
td::Ref<vm::Cell> SmartContractCode::wallet() {
|
||||
auto res = load("wallet").move_as_ok();
|
||||
td::Ref<vm::Cell> SmartContractCode::wallet3(int revision) {
|
||||
if (revision == 0) {
|
||||
revision = WALLET3_REVISION;
|
||||
}
|
||||
auto res = load(PSLICE() << "wallet3-r" << revision).move_as_ok();
|
||||
return res;
|
||||
}
|
||||
td::Ref<vm::Cell> SmartContractCode::simple_wallet() {
|
||||
auto res = load("simple-wallet").move_as_ok();
|
||||
td::Ref<vm::Cell> SmartContractCode::wallet(int revision) {
|
||||
if (revision == 0) {
|
||||
revision = WALLET2_REVISION;
|
||||
}
|
||||
auto res = load(PSLICE() << "wallet-r" << revision).move_as_ok();
|
||||
return res;
|
||||
}
|
||||
td::Ref<vm::Cell> SmartContractCode::simple_wallet(int revision) {
|
||||
if (revision == 0) {
|
||||
revision = WALLET_REVISION;
|
||||
}
|
||||
auto res = load(PSLICE() << "simple-wallet-r" << revision).move_as_ok();
|
||||
return res;
|
||||
}
|
||||
td::Ref<vm::Cell> SmartContractCode::simple_wallet_ext() {
|
||||
static auto res = load("simple-wallet-ext").move_as_ok();
|
||||
return res;
|
||||
}
|
||||
td::Ref<vm::Cell> SmartContractCode::highload_wallet() {
|
||||
static auto res = load("highload-wallet").move_as_ok();
|
||||
td::Ref<vm::Cell> SmartContractCode::highload_wallet(int revision) {
|
||||
if (revision == 0) {
|
||||
revision = HIGHLOAD_WALLET_REVISION;
|
||||
}
|
||||
auto res = load(PSLICE() << "highload-wallet-r" << revision).move_as_ok();
|
||||
return res;
|
||||
}
|
||||
td::Ref<vm::Cell> SmartContractCode::highload_wallet_v2(int revision) {
|
||||
if (revision == 0) {
|
||||
revision = HIGHLOAD_WALLET2_REVISION;
|
||||
}
|
||||
auto res = load(PSLICE() << "highload-wallet-v2-r" << revision).move_as_ok();
|
||||
return res;
|
||||
}
|
||||
td::Ref<vm::Cell> SmartContractCode::dns_manual() {
|
||||
static auto res = load("dns-manual").move_as_ok();
|
||||
return res;
|
||||
}
|
||||
} // namespace ton
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#include "vm/cells.h"
|
||||
|
||||
|
@ -23,9 +23,12 @@ class SmartContractCode {
|
|||
public:
|
||||
static td::Result<td::Ref<vm::Cell>> load(td::Slice name);
|
||||
static td::Ref<vm::Cell> multisig();
|
||||
static td::Ref<vm::Cell> wallet();
|
||||
static td::Ref<vm::Cell> simple_wallet();
|
||||
static td::Ref<vm::Cell> wallet3(int revision = 0);
|
||||
static td::Ref<vm::Cell> wallet(int revision = 0);
|
||||
static td::Ref<vm::Cell> simple_wallet(int revision = 0);
|
||||
static td::Ref<vm::Cell> simple_wallet_ext();
|
||||
static td::Ref<vm::Cell> highload_wallet();
|
||||
static td::Ref<vm::Cell> highload_wallet(int revision = 0);
|
||||
static td::Ref<vm::Cell> highload_wallet_v2(int revision = 0);
|
||||
static td::Ref<vm::Cell> dns_manual();
|
||||
};
|
||||
} // namespace ton
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#include "TestGiver.h"
|
||||
#include "GenericAccount.h"
|
||||
|
@ -35,14 +35,23 @@ vm::CellHash TestGiver::get_init_code_hash() noexcept {
|
|||
//return vm::CellHash::from_slice(td::base64_decode("YV/IANhoI22HVeatFh6S5LbCHp+5OilARfzW+VQPZgQ=").move_as_ok());
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> TestGiver::make_a_gift_message(td::uint32 seqno, td::uint64 gramms, td::Slice message,
|
||||
const block::StdAddress& dest_address) noexcept {
|
||||
td::Ref<vm::Cell> TestGiver::make_a_gift_message_static(td::uint32 seqno, td::Span<Gift> gifts) noexcept {
|
||||
CHECK(gifts.size() <= max_gifts_size);
|
||||
|
||||
vm::CellBuilder cb;
|
||||
GenericAccount::store_int_message(cb, dest_address, gramms);
|
||||
cb.store_bytes("\0\0\0\0", 4);
|
||||
vm::CellString::store(cb, message, 35 * 8).ensure();
|
||||
auto message_inner = cb.finalize();
|
||||
return vm::CellBuilder().store_long(seqno, 32).store_long(1, 8).store_ref(message_inner).finalize();
|
||||
cb.store_long(seqno, 32);
|
||||
|
||||
for (auto& gift : gifts) {
|
||||
td::int32 send_mode = 1;
|
||||
auto gramms = gift.gramms;
|
||||
vm::CellBuilder cbi;
|
||||
GenericAccount::store_int_message(cbi, gift.destination, gramms);
|
||||
store_gift_message(cbi, gift);
|
||||
auto message_inner = cbi.finalize();
|
||||
cb.store_long(send_mode, 8).store_ref(std::move(message_inner));
|
||||
}
|
||||
|
||||
return cb.finalize();
|
||||
}
|
||||
|
||||
td::Result<td::uint32> TestGiver::get_seqno() const {
|
||||
|
|
|
@ -14,25 +14,38 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
#include "SmartContract.h"
|
||||
#include "smc-envelope/WalletInterface.h"
|
||||
#include "block/block.h"
|
||||
#include "vm/cells/CellString.h"
|
||||
namespace ton {
|
||||
class TestGiver : public SmartContract {
|
||||
class TestGiver : public SmartContract, public WalletInterface {
|
||||
public:
|
||||
explicit TestGiver(State state) : ton::SmartContract(std::move(state)) {
|
||||
}
|
||||
TestGiver() : ton::SmartContract({}) {
|
||||
}
|
||||
static constexpr unsigned max_message_size = vm::CellString::max_bytes;
|
||||
static constexpr unsigned max_gifts_size = 1;
|
||||
static const block::StdAddress& address() noexcept;
|
||||
static vm::CellHash get_init_code_hash() noexcept;
|
||||
static td::Ref<vm::Cell> make_a_gift_message(td::uint32 seqno, td::uint64 gramms, td::Slice message,
|
||||
const block::StdAddress& dest_address) noexcept;
|
||||
static td::Ref<vm::Cell> make_a_gift_message_static(td::uint32 seqno, td::Span<Gift>) noexcept;
|
||||
|
||||
td::Result<td::uint32> get_seqno() const;
|
||||
|
||||
using WalletInterface::get_init_message;
|
||||
td::Result<td::Ref<vm::Cell>> make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 valid_until,
|
||||
td::Span<Gift> gifts) const override {
|
||||
TRY_RESULT(seqno, get_seqno());
|
||||
return make_a_gift_message_static(seqno, gifts);
|
||||
}
|
||||
size_t get_max_gifts_size() const override {
|
||||
return max_gifts_size;
|
||||
}
|
||||
|
||||
private:
|
||||
td::Result<td::uint32> get_seqno_or_throw() const;
|
||||
};
|
||||
|
|
|
@ -13,17 +13,19 @@
|
|||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#include "TestWallet.h"
|
||||
#include "GenericAccount.h"
|
||||
|
||||
#include "SmartContractCode.h"
|
||||
|
||||
#include "vm/boc.h"
|
||||
#include "td/utils/base64.h"
|
||||
|
||||
namespace ton {
|
||||
td::Ref<vm::Cell> TestWallet::get_init_state(const td::Ed25519::PublicKey& public_key) noexcept {
|
||||
auto code = get_init_code();
|
||||
td::Ref<vm::Cell> TestWallet::get_init_state(const td::Ed25519::PublicKey& public_key, td::int32 revision) noexcept {
|
||||
auto code = get_init_code(revision);
|
||||
auto data = get_init_data(public_key);
|
||||
return GenericAccount::get_init_state(std::move(code), std::move(data));
|
||||
}
|
||||
|
@ -35,42 +37,46 @@ td::Ref<vm::Cell> TestWallet::get_init_message(const td::Ed25519::PrivateKey& pr
|
|||
return vm::CellBuilder().store_bytes(signature).store_bytes(seq_no).finalize();
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> TestWallet::make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 seqno,
|
||||
td::int64 gramms, td::Slice message,
|
||||
const block::StdAddress& dest_address) noexcept {
|
||||
td::int32 send_mode = 3;
|
||||
if (gramms == -1) {
|
||||
gramms = 0;
|
||||
send_mode += 128;
|
||||
}
|
||||
td::Ref<vm::Cell> TestWallet::make_a_gift_message_static(const td::Ed25519::PrivateKey& private_key, td::uint32 seqno,
|
||||
td::Span<Gift> gifts) noexcept {
|
||||
CHECK(gifts.size() <= max_gifts_size);
|
||||
|
||||
vm::CellBuilder cb;
|
||||
GenericAccount::store_int_message(cb, dest_address, gramms);
|
||||
cb.store_bytes("\0\0\0\0", 4);
|
||||
vm::CellString::store(cb, message, 35 * 8).ensure();
|
||||
auto message_inner = cb.finalize();
|
||||
auto message_outer =
|
||||
vm::CellBuilder().store_long(seqno, 32).store_long(send_mode, 8).store_ref(message_inner).finalize();
|
||||
cb.store_long(seqno, 32);
|
||||
|
||||
for (auto& gift : gifts) {
|
||||
td::int32 send_mode = 3;
|
||||
auto gramms = gift.gramms;
|
||||
if (gramms == -1) {
|
||||
gramms = 0;
|
||||
send_mode += 128;
|
||||
}
|
||||
vm::CellBuilder cbi;
|
||||
GenericAccount::store_int_message(cbi, gift.destination, gramms);
|
||||
store_gift_message(cbi, gift);
|
||||
auto message_inner = cbi.finalize();
|
||||
cb.store_long(send_mode, 8).store_ref(std::move(message_inner));
|
||||
}
|
||||
|
||||
auto message_outer = cb.finalize();
|
||||
auto signature = private_key.sign(message_outer->get_hash().as_slice()).move_as_ok();
|
||||
return vm::CellBuilder().store_bytes(signature).append_cellslice(vm::load_cell_slice(message_outer)).finalize();
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> TestWallet::get_init_code() noexcept {
|
||||
static auto res = [] {
|
||||
auto serialized_code = td::base64_decode(
|
||||
"te6ccgEEAQEAAAAAUwAAov8AIN0gggFMl7qXMO1E0NcLH+Ck8mCBAgDXGCDXCx/tRNDTH9P/"
|
||||
"0VESuvKhIvkBVBBE+RDyovgAAdMfMSDXSpbTB9QC+wDe0aTIyx/L/8ntVA==")
|
||||
.move_as_ok();
|
||||
return vm::std_boc_deserialize(serialized_code).move_as_ok();
|
||||
}();
|
||||
return res;
|
||||
td::Ref<vm::Cell> TestWallet::get_init_code(td::int32 revision) noexcept {
|
||||
return ton::SmartContractCode::simple_wallet(revision);
|
||||
}
|
||||
|
||||
vm::CellHash TestWallet::get_init_code_hash() noexcept {
|
||||
return get_init_code()->get_hash();
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> TestWallet::get_data(const td::Ed25519::PublicKey& public_key, td::uint32 seqno) noexcept {
|
||||
return vm::CellBuilder().store_long(seqno, 32).store_bytes(public_key.as_octet_string()).finalize();
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> TestWallet::get_init_data(const td::Ed25519::PublicKey& public_key) noexcept {
|
||||
return vm::CellBuilder().store_long(0, 32).store_bytes(public_key.as_octet_string()).finalize();
|
||||
return get_data(public_key, 0);
|
||||
}
|
||||
|
||||
td::Result<td::uint32> TestWallet::get_seqno() const {
|
||||
|
@ -88,4 +94,20 @@ td::Result<td::uint32> TestWallet::get_seqno_or_throw() const {
|
|||
return static_cast<td::uint32>(seqno);
|
||||
}
|
||||
|
||||
td::Result<td::Ed25519::PublicKey> TestWallet::get_public_key() const {
|
||||
return TRY_VM(get_public_key_or_throw());
|
||||
}
|
||||
|
||||
td::Result<td::Ed25519::PublicKey> TestWallet::get_public_key_or_throw() const {
|
||||
if (state_.data.is_null()) {
|
||||
return td::Status::Error("data is null");
|
||||
}
|
||||
//FIXME use get method
|
||||
auto cs = vm::load_cell_slice(state_.data);
|
||||
cs.skip_first(32);
|
||||
td::SecureString res(td::Ed25519::PublicKey::LENGTH);
|
||||
cs.fetch_bytes(res.as_mutable_slice().ubegin(), td::narrow_cast<td::int32>(res.size()));
|
||||
return td::Ed25519::PublicKey(std::move(res));
|
||||
}
|
||||
|
||||
} // namespace ton
|
||||
|
|
|
@ -14,35 +14,53 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "smc-envelope/SmartContract.h"
|
||||
#include "smc-envelope/WalletInterface.h"
|
||||
#include "vm/cells.h"
|
||||
#include "Ed25519.h"
|
||||
#include "block/block.h"
|
||||
#include "vm/cells/CellString.h"
|
||||
|
||||
namespace ton {
|
||||
class TestWallet : public ton::SmartContract {
|
||||
class TestWallet : public ton::SmartContract, public WalletInterface {
|
||||
public:
|
||||
explicit TestWallet(State state) : ton::SmartContract(std::move(state)) {
|
||||
}
|
||||
explicit TestWallet(const td::Ed25519::PublicKey& public_key, td::uint32 seqno)
|
||||
: TestWallet(State{get_init_code(), get_data(public_key, seqno)}) {
|
||||
}
|
||||
static constexpr unsigned max_message_size = vm::CellString::max_bytes;
|
||||
static td::Ref<vm::Cell> get_init_state(const td::Ed25519::PublicKey& public_key) noexcept;
|
||||
static constexpr unsigned max_gifts_size = 1;
|
||||
static td::Ref<vm::Cell> get_init_state(const td::Ed25519::PublicKey& public_key, td::int32 revision = 0) noexcept;
|
||||
static td::Ref<vm::Cell> get_init_message(const td::Ed25519::PrivateKey& private_key) noexcept;
|
||||
static td::Ref<vm::Cell> make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 seqno,
|
||||
td::int64 gramms, td::Slice message,
|
||||
const block::StdAddress& dest_address) noexcept;
|
||||
static td::Ref<vm::Cell> make_a_gift_message_static(const td::Ed25519::PrivateKey& private_key, td::uint32 seqno,
|
||||
td::Span<Gift> gifts) noexcept;
|
||||
|
||||
static td::Ref<vm::Cell> get_init_code() noexcept;
|
||||
static td::Ref<vm::Cell> get_init_code(td::int32 revision = 0) noexcept;
|
||||
static vm::CellHash get_init_code_hash() noexcept;
|
||||
static td::Ref<vm::Cell> get_data(const td::Ed25519::PublicKey& public_key, td::uint32 seqno) noexcept;
|
||||
static td::Ref<vm::Cell> get_init_data(const td::Ed25519::PublicKey& public_key) noexcept;
|
||||
|
||||
td::Result<td::uint32> get_seqno() const;
|
||||
|
||||
using WalletInterface::get_init_message;
|
||||
td::Result<td::Ref<vm::Cell>> make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 valid_until,
|
||||
td::Span<Gift> gifts) const override {
|
||||
TRY_RESULT(seqno, get_seqno());
|
||||
return make_a_gift_message_static(private_key, seqno, gifts);
|
||||
}
|
||||
size_t get_max_gifts_size() const override {
|
||||
return max_gifts_size;
|
||||
}
|
||||
|
||||
td::Result<td::Ed25519::PublicKey> get_public_key() const override;
|
||||
|
||||
private:
|
||||
td::Result<td::uint32> get_seqno_or_throw() const;
|
||||
td::Result<td::Ed25519::PublicKey> get_public_key_or_throw() const;
|
||||
};
|
||||
} // namespace ton
|
||||
|
|
|
@ -14,10 +14,11 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#include "Wallet.h"
|
||||
#include "GenericAccount.h"
|
||||
#include "SmartContractCode.h"
|
||||
|
||||
#include "vm/boc.h"
|
||||
#include "vm/cells/CellString.h"
|
||||
|
@ -26,8 +27,8 @@
|
|||
#include <limits>
|
||||
|
||||
namespace ton {
|
||||
td::Ref<vm::Cell> Wallet::get_init_state(const td::Ed25519::PublicKey& public_key) noexcept {
|
||||
auto code = get_init_code();
|
||||
td::Ref<vm::Cell> Wallet::get_init_state(const td::Ed25519::PublicKey& public_key, td::int32 revision) noexcept {
|
||||
auto code = get_init_code(revision);
|
||||
auto data = get_init_data(public_key);
|
||||
return GenericAccount::get_init_state(std::move(code), std::move(data));
|
||||
}
|
||||
|
@ -43,46 +44,45 @@ td::Ref<vm::Cell> Wallet::get_init_message(const td::Ed25519::PrivateKey& privat
|
|||
}
|
||||
|
||||
td::Ref<vm::Cell> Wallet::make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 seqno,
|
||||
td::uint32 valid_until, td::int64 gramms, td::Slice message,
|
||||
const block::StdAddress& dest_address) noexcept {
|
||||
td::int32 send_mode = 3;
|
||||
if (gramms == -1) {
|
||||
gramms = 0;
|
||||
send_mode += 128;
|
||||
}
|
||||
vm::CellBuilder cb;
|
||||
GenericAccount::store_int_message(cb, dest_address, gramms);
|
||||
cb.store_bytes("\0\0\0\0", 4);
|
||||
vm::CellString::store(cb, message, 35 * 8).ensure();
|
||||
auto message_inner = cb.finalize();
|
||||
td::uint32 valid_until, td::Span<Gift> gifts) noexcept {
|
||||
CHECK(gifts.size() <= max_gifts_size);
|
||||
|
||||
auto message_outer = vm::CellBuilder()
|
||||
.store_long(seqno, 32)
|
||||
.store_long(valid_until, 32)
|
||||
.store_long(send_mode, 8)
|
||||
.store_ref(message_inner)
|
||||
.finalize();
|
||||
vm::CellBuilder cb;
|
||||
cb.store_long(seqno, 32).store_long(valid_until, 32);
|
||||
|
||||
for (auto& gift : gifts) {
|
||||
td::int32 send_mode = 3;
|
||||
auto gramms = gift.gramms;
|
||||
if (gramms == -1) {
|
||||
gramms = 0;
|
||||
send_mode += 128;
|
||||
}
|
||||
vm::CellBuilder cbi;
|
||||
GenericAccount::store_int_message(cbi, gift.destination, gramms);
|
||||
store_gift_message(cbi, gift);
|
||||
auto message_inner = cbi.finalize();
|
||||
cb.store_long(send_mode, 8).store_ref(std::move(message_inner));
|
||||
}
|
||||
|
||||
auto message_outer = cb.finalize();
|
||||
auto signature = private_key.sign(message_outer->get_hash().as_slice()).move_as_ok();
|
||||
return vm::CellBuilder().store_bytes(signature).append_cellslice(vm::load_cell_slice(message_outer)).finalize();
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> Wallet::get_init_code() noexcept {
|
||||
static auto res = [] {
|
||||
auto serialized_code = td::base64_decode(
|
||||
"te6ccgEEAQEAAAAAVwAAqv8AIN0gggFMl7qXMO1E0NcLH+Ck8mCDCNcYINMf0x8B+CO78mPtRNDTH9P/"
|
||||
"0VExuvKhA/kBVBBC+RDyovgAApMg10qW0wfUAvsA6NGkyMsfy//J7VQ=")
|
||||
.move_as_ok();
|
||||
return vm::std_boc_deserialize(serialized_code).move_as_ok();
|
||||
}();
|
||||
return res;
|
||||
td::Ref<vm::Cell> Wallet::get_init_code(td::int32 revision) noexcept {
|
||||
return SmartContractCode::wallet(revision);
|
||||
}
|
||||
|
||||
vm::CellHash Wallet::get_init_code_hash() noexcept {
|
||||
return get_init_code()->get_hash();
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> Wallet::get_data(const td::Ed25519::PublicKey& public_key, td::uint32 seqno) noexcept {
|
||||
return vm::CellBuilder().store_long(seqno, 32).store_bytes(public_key.as_octet_string()).finalize();
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> Wallet::get_init_data(const td::Ed25519::PublicKey& public_key) noexcept {
|
||||
return vm::CellBuilder().store_long(0, 32).store_bytes(public_key.as_octet_string()).finalize();
|
||||
return get_data(public_key, 0);
|
||||
}
|
||||
|
||||
td::Result<td::uint32> Wallet::get_seqno() const {
|
||||
|
@ -97,4 +97,20 @@ td::Result<td::uint32> Wallet::get_seqno_or_throw() const {
|
|||
return static_cast<td::uint32>(vm::load_cell_slice(state_.data).fetch_ulong(32));
|
||||
}
|
||||
|
||||
td::Result<td::Ed25519::PublicKey> Wallet::get_public_key() const {
|
||||
return TRY_VM(get_public_key_or_throw());
|
||||
}
|
||||
|
||||
td::Result<td::Ed25519::PublicKey> Wallet::get_public_key_or_throw() const {
|
||||
if (state_.data.is_null()) {
|
||||
return td::Status::Error("data is null");
|
||||
}
|
||||
//FIXME use get method
|
||||
auto cs = vm::load_cell_slice(state_.data);
|
||||
cs.skip_first(32);
|
||||
td::SecureString res(td::Ed25519::PublicKey::LENGTH);
|
||||
cs.fetch_bytes(res.as_mutable_slice().ubegin(), td::narrow_cast<td::int32>(res.size()));
|
||||
return td::Ed25519::PublicKey(std::move(res));
|
||||
}
|
||||
|
||||
} // namespace ton
|
||||
|
|
|
@ -14,35 +14,53 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "smc-envelope/SmartContract.h"
|
||||
#include "smc-envelope/WalletInterface.h"
|
||||
#include "vm/cells.h"
|
||||
#include "Ed25519.h"
|
||||
#include "block/block.h"
|
||||
#include "vm/cells/CellString.h"
|
||||
|
||||
namespace ton {
|
||||
class Wallet : ton::SmartContract {
|
||||
class Wallet : ton::SmartContract, public WalletInterface {
|
||||
public:
|
||||
explicit Wallet(State state) : ton::SmartContract(std::move(state)) {
|
||||
}
|
||||
explicit Wallet(const td::Ed25519::PublicKey& public_key, td::uint32 seqno)
|
||||
: Wallet(State{get_init_code(), get_data(public_key, seqno)}) {
|
||||
}
|
||||
static constexpr unsigned max_message_size = vm::CellString::max_bytes;
|
||||
static td::Ref<vm::Cell> get_init_state(const td::Ed25519::PublicKey& public_key) noexcept;
|
||||
static constexpr unsigned max_gifts_size = 4;
|
||||
static td::Ref<vm::Cell> get_init_state(const td::Ed25519::PublicKey& public_key, td::int32 revision = 0) noexcept;
|
||||
static td::Ref<vm::Cell> get_init_message(const td::Ed25519::PrivateKey& private_key) noexcept;
|
||||
static td::Ref<vm::Cell> make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 seqno,
|
||||
td::uint32 valid_until, td::int64 gramms, td::Slice message,
|
||||
const block::StdAddress& dest_address) noexcept;
|
||||
td::uint32 valid_until, td::Span<Gift> gifts) noexcept;
|
||||
|
||||
static td::Ref<vm::Cell> get_init_code() noexcept;
|
||||
static td::Ref<vm::Cell> get_init_code(td::int32 revision = 0) noexcept;
|
||||
static vm::CellHash get_init_code_hash() noexcept;
|
||||
static td::Ref<vm::Cell> get_init_data(const td::Ed25519::PublicKey& public_key) noexcept;
|
||||
static td::Ref<vm::Cell> get_data(const td::Ed25519::PublicKey& public_key, td::uint32 seqno) noexcept;
|
||||
|
||||
td::Result<td::uint32> get_seqno() const;
|
||||
|
||||
using WalletInterface::get_init_message;
|
||||
td::Result<td::Ref<vm::Cell>> make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 valid_until,
|
||||
td::Span<Gift> gifts) const override {
|
||||
TRY_RESULT(seqno, get_seqno());
|
||||
return make_a_gift_message(private_key, seqno, valid_until, gifts);
|
||||
}
|
||||
size_t get_max_gifts_size() const override {
|
||||
return max_gifts_size;
|
||||
}
|
||||
|
||||
td::Result<td::Ed25519::PublicKey> get_public_key() const override;
|
||||
|
||||
private:
|
||||
td::Result<td::uint32> get_seqno_or_throw() const;
|
||||
td::Result<td::Ed25519::PublicKey> get_public_key_or_throw() const;
|
||||
};
|
||||
} // namespace ton
|
||||
|
|
62
crypto/smc-envelope/WalletInterface.h
Normal file
62
crypto/smc-envelope/WalletInterface.h
Normal file
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
This file is part of TON Blockchain Library.
|
||||
|
||||
TON Blockchain Library is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
TON Blockchain Library 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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "td/utils/common.h"
|
||||
#include "Ed25519.h"
|
||||
#include "block/block.h"
|
||||
#include "vm/cells/CellString.h"
|
||||
|
||||
#include "SmartContract.h"
|
||||
|
||||
namespace ton {
|
||||
class WalletInterface {
|
||||
public:
|
||||
struct Gift {
|
||||
block::StdAddress destination;
|
||||
td::int64 gramms;
|
||||
bool is_encrypted{false};
|
||||
std::string message;
|
||||
};
|
||||
|
||||
virtual ~WalletInterface() {
|
||||
}
|
||||
|
||||
virtual size_t get_max_gifts_size() const = 0;
|
||||
virtual td::Result<td::Ref<vm::Cell>> make_a_gift_message(const td::Ed25519::PrivateKey &private_key,
|
||||
td::uint32 valid_until, td::Span<Gift> gifts) const = 0;
|
||||
virtual td::Result<td::Ed25519::PublicKey> get_public_key() const {
|
||||
return td::Status::Error("TODO");
|
||||
}
|
||||
|
||||
td::Result<td::Ref<vm::Cell>> get_init_message(const td::Ed25519::PrivateKey &private_key,
|
||||
td::uint32 valid_until = std::numeric_limits<td::uint32>::max()) {
|
||||
return make_a_gift_message(private_key, valid_until, {});
|
||||
}
|
||||
static void store_gift_message(vm::CellBuilder &cb, const Gift &gift) {
|
||||
if (gift.is_encrypted) {
|
||||
cb.store_long(1, 32);
|
||||
} else {
|
||||
cb.store_long(0, 32);
|
||||
}
|
||||
vm::CellString::store(cb, gift.message, 35 * 8).ensure();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ton
|
|
@ -14,10 +14,11 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#include "WalletV3.h"
|
||||
#include "GenericAccount.h"
|
||||
#include "SmartContractCode.h"
|
||||
|
||||
#include "vm/boc.h"
|
||||
#include "vm/cells/CellString.h"
|
||||
|
@ -26,76 +27,70 @@
|
|||
#include <limits>
|
||||
|
||||
namespace ton {
|
||||
td::Ref<vm::Cell> WalletV3::get_init_state(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id) noexcept {
|
||||
auto code = get_init_code();
|
||||
td::Ref<vm::Cell> WalletV3::get_init_state(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id,
|
||||
td::int32 revision) noexcept {
|
||||
auto code = get_init_code(revision);
|
||||
auto data = get_init_data(public_key, wallet_id);
|
||||
return GenericAccount::get_init_state(std::move(code), std::move(data));
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> WalletV3::get_init_message(const td::Ed25519::PrivateKey& private_key,
|
||||
td::uint32 wallet_id) noexcept {
|
||||
td::uint32 seqno = 0;
|
||||
td::uint32 valid_until = std::numeric_limits<td::uint32>::max();
|
||||
auto signature = private_key
|
||||
.sign(vm::CellBuilder()
|
||||
.store_long(wallet_id, 32)
|
||||
.store_long(valid_until, 32)
|
||||
.store_long(seqno, 32)
|
||||
.finalize()
|
||||
->get_hash()
|
||||
.as_slice())
|
||||
.move_as_ok();
|
||||
return vm::CellBuilder()
|
||||
.store_bytes(signature)
|
||||
.store_long(wallet_id, 32)
|
||||
.store_long(valid_until, 32)
|
||||
.store_long(seqno, 32)
|
||||
.finalize();
|
||||
td::optional<td::int32> WalletV3::guess_revision(const vm::Cell::Hash& code_hash) {
|
||||
for (td::int32 i = 1; i <= 2; i++) {
|
||||
if (get_init_code(i)->get_hash() == code_hash) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
td::optional<td::int32> WalletV3::guess_revision(const block::StdAddress& address,
|
||||
const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id) {
|
||||
for (td::int32 i = 1; i <= 2; i++) {
|
||||
if (GenericAccount::get_address(address.workchain, get_init_state(public_key, wallet_id, i)) == address) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> WalletV3::make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 wallet_id,
|
||||
td::uint32 seqno, td::uint32 valid_until, td::int64 gramms,
|
||||
td::Slice message, const block::StdAddress& dest_address) noexcept {
|
||||
td::int32 send_mode = 3;
|
||||
if (gramms == -1) {
|
||||
gramms = 0;
|
||||
send_mode += 128;
|
||||
}
|
||||
vm::CellBuilder cb;
|
||||
GenericAccount::store_int_message(cb, dest_address, gramms);
|
||||
cb.store_bytes("\0\0\0\0", 4);
|
||||
vm::CellString::store(cb, message, 35 * 8).ensure();
|
||||
auto message_inner = cb.finalize();
|
||||
td::uint32 seqno, td::uint32 valid_until,
|
||||
td::Span<Gift> gifts) noexcept {
|
||||
CHECK(gifts.size() <= max_gifts_size);
|
||||
|
||||
auto message_outer = vm::CellBuilder()
|
||||
.store_long(wallet_id, 32)
|
||||
.store_long(valid_until, 32)
|
||||
.store_long(seqno, 32)
|
||||
.store_long(send_mode, 8)
|
||||
.store_ref(message_inner)
|
||||
.finalize();
|
||||
vm::CellBuilder cb;
|
||||
cb.store_long(wallet_id, 32).store_long(valid_until, 32).store_long(seqno, 32);
|
||||
|
||||
for (auto& gift : gifts) {
|
||||
td::int32 send_mode = 3;
|
||||
auto gramms = gift.gramms;
|
||||
if (gramms == -1) {
|
||||
gramms = 0;
|
||||
send_mode += 128;
|
||||
}
|
||||
vm::CellBuilder cbi;
|
||||
GenericAccount::store_int_message(cbi, gift.destination, gramms);
|
||||
store_gift_message(cbi, gift);
|
||||
auto message_inner = cbi.finalize();
|
||||
cb.store_long(send_mode, 8).store_ref(std::move(message_inner));
|
||||
}
|
||||
|
||||
auto message_outer = cb.finalize();
|
||||
auto signature = private_key.sign(message_outer->get_hash().as_slice()).move_as_ok();
|
||||
return vm::CellBuilder().store_bytes(signature).append_cellslice(vm::load_cell_slice(message_outer)).finalize();
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> WalletV3::get_init_code() noexcept {
|
||||
static auto res = [] {
|
||||
auto serialized_code = td::base64_decode(
|
||||
"te6ccgEBAQEAYgAAwP8AIN0gggFMl7qXMO1E0NcLH+Ck8mCDCNcYINMf0x/TH/gjE7vyY+1E0NMf0x/T/"
|
||||
"9FRMrryoVFEuvKiBPkBVBBV+RDyo/gAkyDXSpbTB9QC+wDo0QGkyMsfyx/L/8ntVA==")
|
||||
.move_as_ok();
|
||||
return vm::std_boc_deserialize(serialized_code).move_as_ok();
|
||||
}();
|
||||
return res;
|
||||
td::Ref<vm::Cell> WalletV3::get_init_code(td::int32 revision) noexcept {
|
||||
return SmartContractCode::wallet3(revision);
|
||||
}
|
||||
|
||||
vm::CellHash WalletV3::get_init_code_hash() noexcept {
|
||||
return get_init_code()->get_hash();
|
||||
}
|
||||
|
||||
td::Ref<vm::Cell> WalletV3::get_init_data(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id) noexcept {
|
||||
td::Ref<vm::Cell> WalletV3::get_init_data(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id,
|
||||
td::uint32 seqno) noexcept {
|
||||
return vm::CellBuilder()
|
||||
.store_long(0, 32)
|
||||
.store_long(seqno, 32)
|
||||
.store_long(wallet_id, 32)
|
||||
.store_bytes(public_key.as_octet_string())
|
||||
.finalize();
|
||||
|
@ -127,4 +122,20 @@ td::Result<td::uint32> WalletV3::get_wallet_id_or_throw() const {
|
|||
return static_cast<td::uint32>(cs.fetch_ulong(32));
|
||||
}
|
||||
|
||||
td::Result<td::Ed25519::PublicKey> WalletV3::get_public_key() const {
|
||||
return TRY_VM(get_public_key_or_throw());
|
||||
}
|
||||
|
||||
td::Result<td::Ed25519::PublicKey> WalletV3::get_public_key_or_throw() const {
|
||||
if (state_.data.is_null()) {
|
||||
return td::Status::Error("data is null");
|
||||
}
|
||||
//FIXME use get method
|
||||
auto cs = vm::load_cell_slice(state_.data);
|
||||
cs.skip_first(64);
|
||||
td::SecureString res(td::Ed25519::PublicKey::LENGTH);
|
||||
cs.fetch_bytes(res.as_mutable_slice().ubegin(), td::narrow_cast<td::int32>(res.size()));
|
||||
return td::Ed25519::PublicKey(std::move(res));
|
||||
}
|
||||
|
||||
} // namespace ton
|
||||
|
|
|
@ -14,37 +14,59 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "smc-envelope/SmartContract.h"
|
||||
#include "smc-envelope/WalletInterface.h"
|
||||
#include "vm/cells.h"
|
||||
#include "Ed25519.h"
|
||||
#include "block/block.h"
|
||||
#include "vm/cells/CellString.h"
|
||||
|
||||
namespace ton {
|
||||
class WalletV3 : ton::SmartContract {
|
||||
class WalletV3 : ton::SmartContract, public WalletInterface {
|
||||
public:
|
||||
explicit WalletV3(State state) : ton::SmartContract(std::move(state)) {
|
||||
}
|
||||
explicit WalletV3(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id, td::uint32 seqno = 0)
|
||||
: WalletV3(State{get_init_code(), get_init_data(public_key, wallet_id, seqno)}) {
|
||||
}
|
||||
static constexpr unsigned max_message_size = vm::CellString::max_bytes;
|
||||
static td::Ref<vm::Cell> get_init_state(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id) noexcept;
|
||||
static td::Ref<vm::Cell> get_init_message(const td::Ed25519::PrivateKey& private_key, td::uint32 wallet_id) noexcept;
|
||||
static td::Ref<vm::Cell> make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 wallet_id,
|
||||
td::uint32 seqno, td::uint32 valid_until, td::int64 gramms,
|
||||
td::Slice message, const block::StdAddress& dest_address) noexcept;
|
||||
static constexpr unsigned max_gifts_size = 4;
|
||||
|
||||
static td::Ref<vm::Cell> get_init_code() noexcept;
|
||||
static td::optional<td::int32> guess_revision(const vm::Cell::Hash& code_hash);
|
||||
static td::optional<td::int32> guess_revision(const block::StdAddress& address,
|
||||
const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id);
|
||||
static td::Ref<vm::Cell> get_init_state(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id,
|
||||
td::int32 revision = 0) noexcept;
|
||||
static td::Ref<vm::Cell> make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 wallet_id,
|
||||
td::uint32 seqno, td::uint32 valid_until, td::Span<Gift> gifts) noexcept;
|
||||
|
||||
static td::Ref<vm::Cell> get_init_code(td::int32 revision = 0) noexcept;
|
||||
static vm::CellHash get_init_code_hash() noexcept;
|
||||
static td::Ref<vm::Cell> get_init_data(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id) noexcept;
|
||||
static td::Ref<vm::Cell> get_init_data(const td::Ed25519::PublicKey& public_key, td::uint32 wallet_id,
|
||||
td::uint32 seqno = 0) noexcept;
|
||||
|
||||
td::Result<td::uint32> get_seqno() const;
|
||||
td::Result<td::uint32> get_wallet_id() const;
|
||||
|
||||
using WalletInterface::get_init_message;
|
||||
td::Result<td::Ref<vm::Cell>> make_a_gift_message(const td::Ed25519::PrivateKey& private_key, td::uint32 valid_until,
|
||||
td::Span<Gift> gifts) const override {
|
||||
TRY_RESULT(seqno, get_seqno());
|
||||
TRY_RESULT(wallet_id, get_wallet_id());
|
||||
return make_a_gift_message(private_key, wallet_id, seqno, valid_until, gifts);
|
||||
}
|
||||
size_t get_max_gifts_size() const override {
|
||||
return max_gifts_size;
|
||||
}
|
||||
td::Result<td::Ed25519::PublicKey> get_public_key() const override;
|
||||
|
||||
private:
|
||||
td::Result<td::uint32> get_seqno_or_throw() const;
|
||||
td::Result<td::uint32> get_wallet_id_or_throw() const;
|
||||
td::Result<td::Ed25519::PublicKey> get_public_key_or_throw() const;
|
||||
};
|
||||
} // namespace ton
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue