Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
inputs = {
logos-module-builder.url = "github:logos-co/logos-module-builder";
nix-bundle-lgx.url = "github:logos-co/nix-bundle-lgx";
logos-execution-zone.url = "github:3esmit/logos-execution-zone?rev=3a96a23f7feecfc32b3f31beafd4f1768c691e42";
logos-execution-zone.url = "github:3esmit/logos-execution-zone?rev=7b47fba28e8c1e1ebff30ed16989df94820c0dee";
};

outputs = inputs@{ logos-module-builder, ... }:
Expand Down
44 changes: 42 additions & 2 deletions src/lez_core_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,31 @@ constexpr auto Secrets = "secrets";
constexpr std::string_view LezTestnetSequencerAddress = "https://testnet.lez.logos.co";
constexpr std::string_view LezTestnetAuthenticatedTransferProgramId =
"dcbbfebcd59399961ed9973b8307dc475fd4c5ca5779aacfe7588f7dbc3f4a71";
constexpr std::string_view LezLocalDevelopmentSequencerAddress =
"http://127.0.0.1:3040";

bool isLezTestnetSequencer(std::string address) {
void normalizeSequencerAddress(std::string& address) {
while (!address.empty() && address.back() == '/') {
address.pop_back();
}
}

bool isLezTestnetSequencer(std::string address) {
normalizeSequencerAddress(address);
return address == LezTestnetSequencerAddress;
}

bool isLezLocalDevelopmentSequencer(std::string address) {
normalizeSequencerAddress(address);
return address == LezLocalDevelopmentSequencerAddress;
Comment thread
3esmit marked this conversation as resolved.
}

std::string programIdToHex(const FfiProgramId& programId) {
return bytesToHex(
reinterpret_cast<const uint8_t*>(programId.data),
sizeof(programId.data));
}

std::vector<uint32_t> authenticatedTransferWords(const uint8_t (&amount)[16]) {
std::vector<uint32_t> words;
words.reserve(5);
Expand Down Expand Up @@ -895,7 +912,8 @@ std::string LEZCoreModule::register_public_account(const std::string& account_id
return transferResultToJson(nullptr, "register_public_account: invalid account_id_hex");
}

if (isLezTestnetSequencer(get_sequencer_addr())) {
const std::string sequencerAddress = get_sequencer_addr();
if (isLezTestnetSequencer(sequencerAddress)) {
// AuthenticatedTransferInstruction::Initialize is enum variant 1.
return send_generic_public_transaction(
{account_id_hex},
Expand All @@ -905,6 +923,28 @@ std::string LEZCoreModule::register_public_account(const std::string& account_id
);
}

if (isLezLocalDevelopmentSequencer(sequencerAddress)) {
FfiProgramId programId{};
const WalletFfiError error =
wallet_ffi_authenticated_transfer_program_id(&programId);
if (error != SUCCESS) {
fprintf(stderr,
"register_public_account: local program id FFI error %d\n",
error);
return transferResultToJson(
nullptr,
"register_public_account: local program id FFI error "
+ std::to_string(error));
}
// AuthenticatedTransferInstruction::Initialize is enum variant 1.
return send_generic_public_transaction(
{account_id_hex},
{true},
{1},
programIdToHex(programId)
);
}

FfiTransferResult result{};
const WalletFfiError error = wallet_ffi_register_public_account(walletHandle, &id, &result);
if (error != SUCCESS) {
Expand Down
9 changes: 9 additions & 0 deletions tests/mocks/mock_wallet_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,15 @@ WalletFfiError wallet_ffi_register_public_account(WalletHandle*, const FfiBytes3
return fillTransferResult("wallet_ffi_register_public_account", out_result);
}

WalletFfiError wallet_ffi_authenticated_transfer_program_id(FfiProgramId* out_program_id) {
LOGOS_CMOCK_RECORD("wallet_ffi_authenticated_transfer_program_id");
const int err = LOGOS_CMOCK_RETURN(int, "wallet_ffi_authenticated_transfer_program_id");
if (err == 0 && out_program_id) {
memset(out_program_id->data, 0x42, sizeof(out_program_id->data));
}
return static_cast<WalletFfiError>(err);
}

WalletFfiError wallet_ffi_register_private_account(WalletHandle*, const FfiBytes32*, FfiTransferResult* out_result) {
LOGOS_CMOCK_RECORD("wallet_ffi_register_private_account");
return fillTransferResult("wallet_ffi_register_private_account", out_result);
Expand Down
1 change: 1 addition & 0 deletions tests/stubs/wallet_ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ WalletFfiError wallet_ffi_transfer_private_owned(

WalletFfiError wallet_ffi_register_public_account(WalletHandle* handle, const FfiBytes32* account_id, FfiTransferResult* out_result);
WalletFfiError wallet_ffi_register_private_account(WalletHandle* handle, const FfiBytes32* account_id, FfiTransferResult* out_result);
WalletFfiError wallet_ffi_authenticated_transfer_program_id(FfiProgramId* out_program_id);

WalletFfiError wallet_ffi_transfer_elf(FfiProgram *ffi_program);
WalletFfiError wallet_ffi_token_elf(FfiProgram *ffi_program);
Expand Down
23 changes: 23 additions & 0 deletions tests/test_lez_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,29 @@ LOGOS_TEST(register_public_account_uses_convenience_path_outside_testnet) {
LOGOS_ASSERT_TRUE(obj["success"].get<bool>());
}

LOGOS_TEST(register_public_account_uses_local_development_program) {
auto t = LogosTestContext("logos_execution_zone");
t.mockCFunction("wallet_ffi_get_sequencer_addr").returns("http://127.0.0.1:3040/");
LEZCoreModule module;

const nlohmann::json obj = parseObject(module.register_public_account(VALID_ID));

LOGOS_ASSERT_EQ(t.cFunctionCallCount("wallet_ffi_get_sequencer_addr"), 1);
LOGOS_ASSERT(t.cFunctionCalled("wallet_ffi_authenticated_transfer_program_id"));
LOGOS_ASSERT(t.cFunctionCalled("wallet_ffi_send_generic_public_transaction"));
LOGOS_ASSERT_FALSE(t.cFunctionCalled("wallet_ffi_register_public_account"));
LOGOS_ASSERT_TRUE(obj["success"].get<bool>());
LOGOS_ASSERT_EQ(MockWalletFfiCapture::lastGenericPublicInstructionWords.size(), static_cast<size_t>(1));
LOGOS_ASSERT_EQ(MockWalletFfiCapture::lastGenericPublicInstructionWords[0], static_cast<uint32_t>(1));
const std::array<uint8_t, 32> expected_program_id = {
0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
};
LOGOS_ASSERT(MockWalletFfiCapture::lastGenericPublicProgramId == expected_program_id);
}

LOGOS_TEST(register_public_account_uses_deployed_testnet_program) {
auto t = LogosTestContext("logos_execution_zone");
t.mockCFunction("wallet_ffi_get_sequencer_addr").returns("https://testnet.lez.logos.co");
Expand Down