mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
integrating the existing state of TON Storage / TON Payments / CPS Fift development branches
This commit is contained in:
parent
040df63c98
commit
4e2624459b
153 changed files with 10760 additions and 1695 deletions
|
@ -21,13 +21,17 @@
|
|||
#include "td/utils/common.h"
|
||||
#include "Ed25519.h"
|
||||
#include "block/block.h"
|
||||
#include "block/block-parse.h"
|
||||
#include "vm/cells/CellString.h"
|
||||
|
||||
#include "SmartContract.h"
|
||||
#include "SmartContractCode.h"
|
||||
#include "GenericAccount.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace ton {
|
||||
class WalletInterface {
|
||||
class WalletInterface : public SmartContract {
|
||||
public:
|
||||
struct Gift {
|
||||
block::StdAddress destination;
|
||||
|
@ -39,49 +43,91 @@ class WalletInterface {
|
|||
td::Ref<vm::Cell> body;
|
||||
td::Ref<vm::Cell> init_state;
|
||||
};
|
||||
struct DefaultInitData {
|
||||
td::SecureString public_key;
|
||||
td::uint32 wallet_id{0};
|
||||
td::uint32 seqno{0};
|
||||
DefaultInitData() = default;
|
||||
DefaultInitData(td::Slice key, td::uint32 wallet_id) : public_key(key), wallet_id(wallet_id) {
|
||||
}
|
||||
};
|
||||
|
||||
WalletInterface(State state) : SmartContract(std::move(state)) {
|
||||
}
|
||||
|
||||
virtual ~WalletInterface() {
|
||||
}
|
||||
|
||||
virtual size_t get_max_gifts_size() const = 0;
|
||||
virtual size_t get_max_message_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("Unsupported");
|
||||
|
||||
virtual td::Result<td::uint32> get_seqno() const;
|
||||
virtual td::Result<td::uint32> get_wallet_id() const;
|
||||
virtual td::Result<td::uint64> get_balance(td::uint64 account_balance, td::uint32 now) const;
|
||||
virtual td::Result<td::Ed25519::PublicKey> get_public_key() const;
|
||||
|
||||
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()) const;
|
||||
|
||||
static td::Ref<vm::Cell> create_int_message(const Gift &gift);
|
||||
static void store_gift_message(vm::CellBuilder &cb, const Gift &gift);
|
||||
};
|
||||
|
||||
template <class WalletT, class TraitsT>
|
||||
class WalletBase : public WalletInterface {
|
||||
public:
|
||||
using Traits = TraitsT;
|
||||
using InitData = typename Traits::InitData;
|
||||
|
||||
explicit WalletBase(State state) : WalletInterface(std::move(state)) {
|
||||
}
|
||||
|
||||
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()) const {
|
||||
return make_a_gift_message(private_key, valid_until, {});
|
||||
size_t get_max_gifts_size() const override {
|
||||
return Traits::max_gifts_size;
|
||||
}
|
||||
static td::Ref<vm::Cell> create_int_message(const Gift &gift) {
|
||||
vm::CellBuilder cbi;
|
||||
GenericAccount::store_int_message(cbi, gift.destination, gift.gramms < 0 ? 0 : gift.gramms);
|
||||
if (gift.init_state.not_null()) {
|
||||
cbi.store_ones(2);
|
||||
cbi.store_ref(gift.init_state);
|
||||
} else {
|
||||
cbi.store_zeroes(1);
|
||||
}
|
||||
cbi.store_zeroes(1);
|
||||
store_gift_message(cbi, gift);
|
||||
return cbi.finalize();
|
||||
size_t get_max_message_size() const override {
|
||||
return Traits::max_message_size;
|
||||
}
|
||||
static void store_gift_message(vm::CellBuilder &cb, const Gift &gift) {
|
||||
if (gift.body.not_null()) {
|
||||
auto body = vm::load_cell_slice(gift.body);
|
||||
//TODO: handle error
|
||||
CHECK(cb.append_cellslice_bool(body));
|
||||
return;
|
||||
}
|
||||
|
||||
if (gift.is_encrypted) {
|
||||
cb.store_long(1, 32);
|
||||
} else {
|
||||
cb.store_long(0, 32);
|
||||
static td::Ref<WalletT> create(State state) {
|
||||
return td::Ref<WalletT>(true, std::move(state));
|
||||
}
|
||||
static td::Ref<vm::Cell> get_init_code(int revision) {
|
||||
return SmartContractCode::get_code(get_code_type(), revision);
|
||||
};
|
||||
static State get_init_state(int revision, const InitData &init_data) {
|
||||
return {get_init_code(revision), WalletT::get_init_data(init_data)};
|
||||
}
|
||||
static SmartContractCode::Type get_code_type() {
|
||||
return Traits::code_type;
|
||||
}
|
||||
static td::optional<td::int32> guess_revision(const vm::Cell::Hash &code_hash) {
|
||||
for (auto revision : ton::SmartContractCode::get_revisions(get_code_type())) {
|
||||
auto code = get_init_code(revision);
|
||||
if (code->get_hash() == code_hash) {
|
||||
return revision;
|
||||
}
|
||||
}
|
||||
vm::CellString::store(cb, gift.message, 35 * 8).ensure();
|
||||
return {};
|
||||
}
|
||||
static td::Span<td::int32> get_revisions() {
|
||||
return ton::SmartContractCode::get_revisions(get_code_type());
|
||||
}
|
||||
static td::optional<td::int32> guess_revision(block::StdAddress &address, const InitData &init_data) {
|
||||
for (auto revision : get_revisions()) {
|
||||
if (WalletT(get_init_state(revision, init_data)).get_address(address.workchain) == address) {
|
||||
return revision;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
static td::Ref<WalletT> create(const InitData &init_data, int revision) {
|
||||
return td::Ref<WalletT>(true, State{get_init_code(revision), WalletT::get_init_data(init_data)});
|
||||
}
|
||||
CntObject *make_copy() const override {
|
||||
return new WalletT(get_state());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue