Skip to content

Commit

Permalink
Problem: Disorganised wallet feature (fix #183)
Browse files Browse the repository at this point in the history
- Add WalletComponent
  • Loading branch information
damoncro committed Oct 21, 2022
1 parent c73674b commit 969f70a
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 0 deletions.
93 changes: 93 additions & 0 deletions Source/CronosPlayUnreal/Private/WalletComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Fill out your copyright notice in the Description page of Project Settings.

#include "WalletComponent.h"

// Sets default values for this class's properties
UWallet::UWallet() {}

// Sets default values for this component's properties
UWalletComponent::UWalletComponent() {
// Set this component to be initialized when the game starts, and to be ticked
// every frame. You can turn these features off to improve performance if you
// don't need them.
PrimaryComponentTick.bCanEverTick = false;

// ...
}

// Called when the game starts
void UWalletComponent::BeginPlay() {
Super::BeginPlay();

// ...
}

// Called every frame
void UWalletComponent::TickComponent(
float DeltaTime, ELevelTick TickType,
FActorComponentTickFunction *ThisTickFunction) {
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

// ...
}

UWallet *UWalletComponent::InitializeWallet(FString mnemonics, FString password,
FString &output_message) {
UWallet *wallet = NewObject<UWallet>();
try {
wallet->_coreWallet =
restore_wallet(TCHAR_TO_UTF8(*mnemonics), TCHAR_TO_UTF8(*password))
.into_raw();
} catch (const rust::cxxbridge1::Error &e) {
output_message = FString::Printf(TEXT("CronosPlayUnreal Error: %s"),
UTF8_TO_TCHAR(e.what()));
}
return wallet;
}

void UWallet::GetAddress(int32 index, ECoinType coin_type, FString &address,
FString &output_message) {
try {
switch (coin_type) {
case ECoinType::CryptoOrgMainnet:
address = UTF8_TO_TCHAR(
_coreWallet
->get_address(org::defi_wallet_core::CoinType::CronosMainnet,
index)
.c_str());
break;
case ECoinType::CryptoOrgTestnet:
address = UTF8_TO_TCHAR(
_coreWallet
->get_address(org::defi_wallet_core::CoinType::CryptoOrgTestnet,
index)
.c_str());
break;
case ECoinType::CronosMainnet:
address = UTF8_TO_TCHAR(
_coreWallet
->get_address(org::defi_wallet_core::CoinType::CronosMainnet,
index)
.c_str());
break;
case ECoinType::CosmosHub:
address = UTF8_TO_TCHAR(
_coreWallet
->get_address(org::defi_wallet_core::CoinType::CosmosHub, index)
.c_str());
break;
case ECoinType::Ethereum:
address = UTF8_TO_TCHAR(
_coreWallet
->get_address(org::defi_wallet_core::CoinType::Ethereum, index)
.c_str());
break;
default:
break;
}

} catch (const rust::cxxbridge1::Error &e) {
output_message = FString::Printf(TEXT("CronosPlayUnreal Error: %s"),
UTF8_TO_TCHAR(e.what()));
}
}
86 changes: 86 additions & 0 deletions Source/CronosPlayUnreal/Public/WalletComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Components/ActorComponent.h"
#include "CoreMinimal.h"
#include "PlayCppSdkLibrary/Include/defi-wallet-core-cpp/src/lib.rs.h"
#include "WalletComponent.generated.h"

/// wallet connect session state
UENUM(BlueprintType)
enum class ECoinType : uint8 {
/// Crypto.org Chain mainnet
CryptoOrgMainnet,
/// Crypto.org Chain testnet
CryptoOrgTestnet,
/// Cronos mainnet beta
CronosMainnet,
/// Cosmos Hub mainnet
CosmosHub,
/// Ethereum
Ethereum,
};

ENUM_RANGE_BY_FIRST_AND_LAST(ECoinType, ECoinType::CryptoOrgMainnet,
ECoinType::Ethereum);

// Wrapper of org::defi_wallet_core::Wallet
UCLASS(BlueprintType)
class CRONOSPLAYUNREAL_API UWallet : public UObject {
GENERATED_BODY()
public:
UWallet();
/**
* Get address with index
* @param index wallet index which starts from 0
* @param coin_type describes what coin type to use (for HD derivation or
* address generation)
* @param output get eth address
* @param success whether succeed or not
* @param message error message, "" if succeed
*/
UFUNCTION(BlueprintCallable,
meta = (DisplayName = "GetAddress", Keywords = "Wallet"),
Category = "CronosPlayUnreal")
void GetAddress(int32 index, ECoinType coin_type, FString &address,
FString &output_message);

/**
Opaque pointer to store wallet
*/
org::defi_wallet_core::Wallet *_coreWallet;
};

UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class CRONOSPLAYUNREAL_API UWalletComponent : public UActorComponent {
GENERATED_BODY()

public:
// Sets default values for this component's properties
UWalletComponent();

protected:
// Called when the game starts
virtual void BeginPlay() override;

public:
// Called every frame
virtual void
TickComponent(float DeltaTime, ELevelTick TickType,
FActorComponentTickFunction *ThisTickFunction) override;

/**
* Restore wallet with mnemonics and password.
* @param mnemonics mnemonics to restore
* @param password salt in mnemonics restoration
* @param output generated address (index=0)
* @param success whether succeed or not
* @param message error message, "" if succeed
*/
UFUNCTION(BlueprintCallable,
meta = (DisplayName = "IntializeWallet", Keywords = "Wallet"),
Category = "CronosPlayUnreal")
UWallet *InitializeWallet(FString mnemonics, FString password,
FString &output_message);
};

0 comments on commit 969f70a

Please sign in to comment.