Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ax 166 0.2. #174

Merged
merged 6 commits into from
Jan 29, 2022
Merged
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
59 changes: 43 additions & 16 deletions lib/service/Controller/Controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ import 'package:bip39/bip39.dart'
as bip39; // Basics of BIP39 https://coldbit.com/bip-39-basics-from-randomness-to-mnemonic-words/

class Controller extends GetxController {
var client = Web3Client("url", Client()).obs;
var credentials;
var publicAddress =
EthereumAddress.fromHex("0xcdaa8c55fB92fbBE61948aDf4Ba8Cf7Ad33DBeF0").obs;
var networkID = 0.obs;
bool walletConnected = false;

/// VARIABLES
var rng = new Random().nextInt(999);
var seedHex = "";
var credentials;
var mnemonic = "";
var privateAddress = "";
var networkID = 0.obs;
bool activeChain = false;
int ACTIVE_CHAIN_ID = 137;
static String latestTx = "";
bool walletConnected = false;
var gas = EtherAmount.zero().obs;
var gasString = "0".obs;
bool activeChain = false;
static const MAINNET_CHAIN_ID = 137;
static const TESTNET_CHAIN_ID = 80001;
String mainRPCUrl = "https://polygon-rpc.com";
String testRPCUrl = "https://matic-mumbai.chainstacklabs.com/";
var client = Web3Client("url", Client()).obs;
var publicAddress =
EthereumAddress.fromHex("0xcdaa8c55fB92fbBE61948aDf4Ba8Cf7Ad33DBeF0").obs;

set axTokenAddress(EthereumAddress tokenAddress) {
axTokenAddress = EthereumAddress.fromHex("${tokenAddress.hex}");
Expand All @@ -44,17 +44,22 @@ class Controller extends GetxController {
getCurrentGas();
}

// This will create mnemonics & convert to seed hexes
void createNewMnemonic() {
mnemonic = bip39.generateMnemonic();
seedHex = bip39.mnemonicToSeedHex(mnemonic);
update();
}

Future<String> retrieveWallet([String? _mnemonic]) async {
mnemonic = _mnemonic!;
privateAddress = bip39.mnemonicToSeedHex(mnemonic);
credentials = EthPrivateKey.fromHex(privateAddress);
update();
return mnemonic;
// This will import mnemonics & convert to seed hexes
Future<bool> importMnemonic(String _mnemonic) async {
// validate if mnemonic
bool isValidMnemonic = bip39.validateMnemonic(_mnemonic);
if (isValidMnemonic) {
seedHex = bip39.mnemonicToSeedHex(_mnemonic);
mnemonic = _mnemonic;
}
return isValidMnemonic;
}

// Connect the dapp to metamask and update relevant values
Expand All @@ -73,6 +78,28 @@ class Controller extends GetxController {
update();
}

// Connect the client + set credentials
void connectNative() async {
String rpcUrl = "";
switch (ACTIVE_CHAIN_ID) {
case 137:
rpcUrl = mainRPCUrl;
break;
default:
rpcUrl = testRPCUrl;
}

client.value = Web3Client(rpcUrl, Client());

credentials = EthPrivateKey.fromHex(seedHex);
print("[Console] connecting to the decentralized web!");
networkID.value = await client.value.getNetworkId();
publicAddress.value = await credentials.extractAddress();
gas.value = await client.value.getGasPrice();
print("[Console] updated client: $client and credentials: $credentials");
update();
}

void getCurrentGas() async {
final eth = window.ethereum;
var rawGasPrice = await client.value.getGasPrice();
Expand Down
21 changes: 18 additions & 3 deletions test/Controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@
import 'package:ax_dapp/service/Controller/Controller.dart';
import 'package:test/test.dart';

void main() {
test('Testing Mainnet ChaID', () {
void main() async {
Controller controller = Controller();
String aValidMnemonic =
"update elbow source spin squeeze horror world become oak assist bomb nuclear";
String anInvalidMnemonic = "potatoe kendrick sparks";

expect(Controller.MAINNET_CHAIN_ID, 137);
bool returnFalseBoolean = await controller.importMnemonic(anInvalidMnemonic);
bool returnTrueBoolean = await controller.importMnemonic(aValidMnemonic);
test('Testing Invalid Mnemonic', () {
expect(returnFalseBoolean, false);
});

test('Testing Valid Mnemonic', () {
expect(returnTrueBoolean, true);
});

controller.connectNative();
test('Testing if valid native connection is made', () {
expect(controller.networkID.value, 80001);
});
}