2022-08-18 01:31:47 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
2022-06-17 01:13:14 +02:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2022-08-28 05:08:41 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-02-18 00:20:21 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2022-05-19 07:00:25 +02:00
|
|
|
import 'package:gecko/globals.dart';
|
2022-05-28 21:15:47 +02:00
|
|
|
import 'package:gecko/models/chest_data.dart';
|
|
|
|
import 'package:gecko/models/wallet_data.dart';
|
2022-05-29 04:30:03 +02:00
|
|
|
import 'package:gecko/providers/home.dart';
|
2022-05-31 18:23:56 +02:00
|
|
|
import 'package:gecko/providers/my_wallets.dart';
|
2022-09-09 01:12:17 +02:00
|
|
|
import 'package:gecko/providers/wallet_options.dart';
|
|
|
|
import 'package:gecko/providers/wallets_profiles.dart';
|
2022-02-18 02:19:08 +01:00
|
|
|
import 'package:polkawallet_sdk/api/apiKeyring.dart';
|
2022-02-18 00:20:21 +01:00
|
|
|
import 'package:polkawallet_sdk/api/types/networkParams.dart';
|
2022-02-19 23:51:12 +01:00
|
|
|
import 'package:polkawallet_sdk/api/types/txInfoData.dart';
|
2022-02-18 00:20:21 +01:00
|
|
|
import 'package:polkawallet_sdk/polkawallet_sdk.dart';
|
|
|
|
import 'package:polkawallet_sdk/storage/keyring.dart';
|
2022-05-04 19:00:09 +02:00
|
|
|
import 'package:polkawallet_sdk/storage/types/keyPairData.dart';
|
2022-08-19 14:10:27 +02:00
|
|
|
import 'package:polkawallet_sdk/webviewWithExtension/types/signExtrinsicParam.dart';
|
2022-05-27 06:11:09 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2022-02-19 23:51:12 +01:00
|
|
|
import 'package:truncate/truncate.dart';
|
2022-08-16 01:44:45 +02:00
|
|
|
import 'package:pointycastle/pointycastle.dart' as pc;
|
|
|
|
import "package:hex/hex.dart";
|
2022-02-18 00:20:21 +01:00
|
|
|
|
|
|
|
class SubstrateSdk with ChangeNotifier {
|
|
|
|
final WalletSDK sdk = WalletSDK();
|
|
|
|
final Keyring keyring = Keyring();
|
2022-02-18 19:49:37 +01:00
|
|
|
String generatedMnemonic = '';
|
2022-02-18 00:20:21 +01:00
|
|
|
bool sdkReady = false;
|
2022-02-18 18:57:03 +01:00
|
|
|
bool sdkLoading = false;
|
2022-02-18 00:20:21 +01:00
|
|
|
bool nodeConnected = false;
|
2022-02-18 19:49:37 +01:00
|
|
|
bool importIsLoading = false;
|
2022-02-18 00:20:21 +01:00
|
|
|
int blocNumber = 0;
|
2022-05-20 15:15:29 +02:00
|
|
|
bool isLoadingEndpoint = false;
|
2022-05-25 20:40:55 +02:00
|
|
|
String debugConnection = '';
|
2022-05-26 02:19:29 +02:00
|
|
|
String transactionStatus = '';
|
2022-08-17 17:50:05 +02:00
|
|
|
final int initSs58 = 42;
|
|
|
|
Map<String, int> currencyParameters = {};
|
2022-08-18 01:31:47 +02:00
|
|
|
TextEditingController csSalt = TextEditingController();
|
|
|
|
TextEditingController csPassword = TextEditingController();
|
|
|
|
String g1V1NewAddress = '';
|
2022-08-29 06:03:05 +02:00
|
|
|
bool isCesiumIDVisible = false;
|
|
|
|
bool isCesiumAddresLoading = false;
|
2022-09-11 13:14:52 +02:00
|
|
|
late int udValue;
|
2022-02-18 00:20:21 +01:00
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
/////////////////////////////////////
|
|
|
|
////////// 1: API METHODS ///////////
|
|
|
|
/////////////////////////////////////
|
|
|
|
|
2022-08-19 23:15:41 +02:00
|
|
|
Future<String> _executeCall(TxInfoData txInfo, txOptions, String password,
|
2022-08-12 10:04:55 +02:00
|
|
|
[String? rawParams]) async {
|
2022-09-09 01:12:17 +02:00
|
|
|
final walletOptions =
|
|
|
|
Provider.of<WalletOptionsProvider>(homeContext, listen: false);
|
|
|
|
final walletProfiles =
|
|
|
|
Provider.of<WalletsProfilesProvider>(homeContext, listen: false);
|
2022-08-11 15:47:08 +02:00
|
|
|
try {
|
2022-09-08 20:14:45 +02:00
|
|
|
final hash = await sdk.api.tx.signAndSend(txInfo, txOptions, password,
|
|
|
|
rawParam: rawParams, onStatusChange: (p0) {
|
|
|
|
transactionStatus = p0;
|
|
|
|
notifyListeners();
|
|
|
|
}).timeout(
|
2022-09-09 10:41:53 +02:00
|
|
|
const Duration(seconds: 18),
|
2022-09-08 20:14:45 +02:00
|
|
|
onTimeout: () => {},
|
|
|
|
);
|
2022-08-11 15:47:08 +02:00
|
|
|
log.d(hash);
|
|
|
|
if (hash.isEmpty) {
|
|
|
|
transactionStatus = 'timeout';
|
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
return 'timeout';
|
|
|
|
} else {
|
2022-09-09 01:12:17 +02:00
|
|
|
// Success !
|
2022-08-11 15:47:08 +02:00
|
|
|
transactionStatus = hash.toString();
|
|
|
|
notifyListeners();
|
2022-09-09 01:12:17 +02:00
|
|
|
walletOptions.reload();
|
|
|
|
walletProfiles.reload();
|
2022-08-11 15:47:08 +02:00
|
|
|
return hash.toString();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
transactionStatus = e.toString();
|
|
|
|
notifyListeners();
|
|
|
|
return e.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 23:15:41 +02:00
|
|
|
Future _getStorage(String call) async {
|
2022-08-09 11:26:45 +02:00
|
|
|
return await sdk.webView!.evalJavascript('api.query.$call');
|
|
|
|
}
|
|
|
|
|
2022-08-19 23:15:41 +02:00
|
|
|
Future _getStorageConst(String call) async {
|
2022-08-17 17:50:05 +02:00
|
|
|
return (await sdk.webView!
|
2022-08-17 23:01:20 +02:00
|
|
|
.evalJavascript('api.consts.$call', wrapPromise: false) ??
|
|
|
|
[null])[0];
|
2022-08-17 17:50:05 +02:00
|
|
|
}
|
|
|
|
|
2022-08-27 23:26:37 +02:00
|
|
|
Future<TxSenderData> _setSender(String address) async {
|
|
|
|
final fromPubkey = await sdk.api.account.decodeAddress([address]);
|
2022-08-11 15:47:08 +02:00
|
|
|
return TxSenderData(
|
2022-08-27 23:26:37 +02:00
|
|
|
address,
|
|
|
|
fromPubkey!.keys.first,
|
2022-08-11 15:47:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-20 20:08:02 +02:00
|
|
|
Future<String> _signMessage(
|
|
|
|
Uint8List message, String address, String password) async {
|
|
|
|
final params = SignAsExtensionParam();
|
|
|
|
params.msgType = "pub(bytes.sign)";
|
|
|
|
params.request = {
|
|
|
|
"address": address,
|
|
|
|
"data": message,
|
|
|
|
};
|
|
|
|
|
|
|
|
final res = await sdk.api.keyring.signAsExtension(password, params);
|
|
|
|
return res?.signature ?? '';
|
|
|
|
}
|
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
////////////////////////////////////////////
|
|
|
|
////////// 2: GET ONCHAIN STORAGE //////////
|
|
|
|
////////////////////////////////////////////
|
|
|
|
|
2022-08-20 20:08:02 +02:00
|
|
|
Future<int> _getIdentityIndexOf(String address) async {
|
2022-08-19 23:15:41 +02:00
|
|
|
return await _getStorage('identity.identityIndexOf("$address")') ?? 0;
|
2022-08-11 15:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<int>> getCerts(String address) async {
|
2022-08-20 20:08:02 +02:00
|
|
|
final idtyIndex = await _getIdentityIndexOf(address);
|
2022-08-11 15:47:08 +02:00
|
|
|
final certsReceiver =
|
2022-08-19 23:15:41 +02:00
|
|
|
await _getStorage('cert.storageIdtyCertMeta($idtyIndex)') ?? [];
|
2022-08-11 15:47:08 +02:00
|
|
|
|
|
|
|
return [certsReceiver['receivedCount'], certsReceiver['issuedCount']];
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> getCertValidityPeriod(String from, String to) async {
|
2022-08-20 20:08:02 +02:00
|
|
|
final idtyIndexFrom = await _getIdentityIndexOf(from);
|
|
|
|
final idtyIndexTo = await _getIdentityIndexOf(to);
|
2022-08-11 15:47:08 +02:00
|
|
|
|
|
|
|
if (idtyIndexFrom == 0 || idtyIndexTo == 0) return 0;
|
|
|
|
|
|
|
|
final List certData =
|
2022-08-19 23:15:41 +02:00
|
|
|
await _getStorage('cert.certsByReceiver($idtyIndexTo)') ?? [];
|
2022-08-11 15:47:08 +02:00
|
|
|
|
|
|
|
if (certData.isEmpty) return 0;
|
|
|
|
for (List certInfo in certData) {
|
|
|
|
if (certInfo[0] == idtyIndexFrom) {
|
|
|
|
return certInfo[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> hasAccountConsumers(String address) async {
|
2022-08-19 23:15:41 +02:00
|
|
|
final accountInfo = await _getStorage('system.account("$address")');
|
2022-08-11 15:47:08 +02:00
|
|
|
final consumers = accountInfo['consumers'];
|
|
|
|
return consumers == 0 ? false : true;
|
|
|
|
}
|
|
|
|
|
2022-09-11 13:14:52 +02:00
|
|
|
Future<int> getUdValue() async {
|
|
|
|
udValue = int.parse(await _getStorage('universalDividend.currentUd()'));
|
|
|
|
return udValue;
|
|
|
|
}
|
2022-08-11 15:47:08 +02:00
|
|
|
|
2022-08-11 19:19:50 +02:00
|
|
|
Future<Map<String, double>> getBalance(String address) async {
|
2022-08-17 17:50:05 +02:00
|
|
|
// log.d('currencyParameters: $currencyParameters');
|
|
|
|
|
2022-08-14 16:34:24 +02:00
|
|
|
if (!nodeConnected) {
|
|
|
|
return {
|
|
|
|
'transferableBalance': 0,
|
|
|
|
'free': 0,
|
|
|
|
'unclaimedUds': 0,
|
|
|
|
'reserved': 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
// Get onchain storage values
|
2022-08-19 23:15:41 +02:00
|
|
|
final Map balanceGlobal = await _getStorage('system.account("$address")');
|
2022-08-11 15:47:08 +02:00
|
|
|
final int? idtyIndex =
|
2022-08-19 23:15:41 +02:00
|
|
|
await _getStorage('identity.identityIndexOf("$address")');
|
2022-08-11 15:47:08 +02:00
|
|
|
final Map? idtyData = idtyIndex == null
|
|
|
|
? null
|
2022-08-19 23:15:41 +02:00
|
|
|
: await _getStorage('identity.identities($idtyIndex)');
|
2022-08-11 15:47:08 +02:00
|
|
|
final int currentUdIndex =
|
2022-08-19 23:15:41 +02:00
|
|
|
int.parse(await _getStorage('universalDividend.currentUdIndex()'));
|
2022-08-11 15:47:08 +02:00
|
|
|
final List pastReevals =
|
2022-08-19 23:15:41 +02:00
|
|
|
await _getStorage('universalDividend.pastReevals()');
|
2022-08-11 15:47:08 +02:00
|
|
|
|
|
|
|
// Compute amount of claimable UDs
|
2022-08-11 19:19:50 +02:00
|
|
|
final int unclaimedUds = _computeUnclaimUds(currentUdIndex,
|
2022-08-11 15:47:08 +02:00
|
|
|
idtyData?['data']?['firstEligibleUd'] ?? 0, pastReevals);
|
|
|
|
|
|
|
|
// Calculate transferable and potential balance
|
|
|
|
final int transferableBalance =
|
2022-08-11 19:19:50 +02:00
|
|
|
(balanceGlobal['data']['free'] + unclaimedUds);
|
|
|
|
|
2022-09-11 13:14:52 +02:00
|
|
|
final bool isUdUnit = configBox.get('isUdUnit') ?? false;
|
|
|
|
final udValue = await getUdValue();
|
2022-09-11 14:04:52 +02:00
|
|
|
final double balanceRatio = isUdUnit ? round(udValue / 100, 6) : 1;
|
2022-09-11 13:14:52 +02:00
|
|
|
|
|
|
|
// log.d('udValue: $udValue');
|
|
|
|
|
2022-08-11 19:19:50 +02:00
|
|
|
Map<String, double> finalBalances = {
|
2022-09-11 13:14:52 +02:00
|
|
|
'transferableBalance': round((transferableBalance / balanceRatio) / 100),
|
|
|
|
'free': round((balanceGlobal['data']['free'] / balanceRatio) / 100),
|
|
|
|
'unclaimedUds': round((unclaimedUds / balanceRatio) / 100),
|
|
|
|
'reserved':
|
|
|
|
round((balanceGlobal['data']['reserved'] / balanceRatio) / 100),
|
2022-08-11 19:19:50 +02:00
|
|
|
};
|
2022-08-11 15:47:08 +02:00
|
|
|
|
2022-08-11 19:19:50 +02:00
|
|
|
// log.i(finalBalances);
|
2022-08-11 15:47:08 +02:00
|
|
|
|
2022-08-11 19:19:50 +02:00
|
|
|
return finalBalances;
|
2022-08-11 15:47:08 +02:00
|
|
|
}
|
|
|
|
|
2022-08-11 19:19:50 +02:00
|
|
|
int _computeUnclaimUds(
|
2022-08-11 15:47:08 +02:00
|
|
|
int currentUdIndex, int firstEligibleUd, List pastReevals) {
|
|
|
|
int totalAmount = 0;
|
|
|
|
|
|
|
|
if (firstEligibleUd == 0) return 0;
|
|
|
|
|
|
|
|
for (final List reval in pastReevals.reversed) {
|
|
|
|
final int revalNbr = reval[0];
|
|
|
|
final int revalValue = reval[1];
|
|
|
|
|
|
|
|
// Loop each UDs revaluations and sum unclaimed balance
|
|
|
|
if (revalNbr <= firstEligibleUd) {
|
|
|
|
final count = currentUdIndex - firstEligibleUd;
|
|
|
|
totalAmount += count * revalValue;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
final count = currentUdIndex - revalNbr;
|
|
|
|
totalAmount += count * revalValue;
|
|
|
|
currentUdIndex = revalNbr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> isMemberGet(String address) async {
|
|
|
|
return await idtyStatus(address) == 'Validated';
|
|
|
|
}
|
|
|
|
|
2022-09-10 06:26:49 +02:00
|
|
|
Future<bool> isSmithGet(String address) async {
|
|
|
|
var idtyIndex = await _getIdentityIndexOf(address);
|
|
|
|
|
|
|
|
final Map smithExpireOn =
|
|
|
|
(await _getStorage('smithsMembership.membership($idtyIndex)')) ?? {};
|
|
|
|
|
|
|
|
if (smithExpireOn.isEmpty) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
Future<Map<String, int>> certState(String from, String to) async {
|
|
|
|
Map<String, int> result = {};
|
2022-08-20 22:46:21 +02:00
|
|
|
final toStatus = await idtyStatus(to);
|
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
if (from != to && await isMemberGet(from)) {
|
|
|
|
final removableOn = await getCertValidityPeriod(from, to);
|
|
|
|
final certMeta = await getCertMeta(from);
|
|
|
|
final int nextIssuableOn = certMeta['nextIssuableOn'] ?? 0;
|
|
|
|
final certRemovableDuration = (removableOn - blocNumber) * 6;
|
|
|
|
const int renewDelay = 2 * 30 * 24 * 3600; // 2 months
|
|
|
|
|
|
|
|
if (certRemovableDuration >= renewDelay) {
|
|
|
|
final certRenewDuration = certRemovableDuration - renewDelay;
|
|
|
|
result.putIfAbsent('certRenewable', () => certRenewDuration);
|
|
|
|
} else if (nextIssuableOn > blocNumber) {
|
|
|
|
final certDelayDuration = (nextIssuableOn - blocNumber) * 6;
|
|
|
|
result.putIfAbsent('certDelay', () => certDelayDuration);
|
2022-08-20 22:46:21 +02:00
|
|
|
} else if (toStatus == 'Created') {
|
|
|
|
result.putIfAbsent('toStatus', () => 1);
|
2022-08-11 15:47:08 +02:00
|
|
|
} else {
|
|
|
|
result.putIfAbsent('canCert', () => 0);
|
|
|
|
}
|
|
|
|
}
|
2022-08-13 13:45:43 +02:00
|
|
|
|
2022-08-20 22:46:21 +02:00
|
|
|
// if (toStatus == 'Created') result.putIfAbsent('toStatus', () => 1);
|
2022-08-17 23:28:19 +02:00
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Map> getCertMeta(String address) async {
|
2022-08-20 20:08:02 +02:00
|
|
|
var idtyIndex = await _getIdentityIndexOf(address);
|
2022-08-11 15:47:08 +02:00
|
|
|
|
|
|
|
final certMeta =
|
2022-08-19 23:15:41 +02:00
|
|
|
await _getStorage('cert.storageIdtyCertMeta($idtyIndex)') ?? '';
|
2022-08-11 15:47:08 +02:00
|
|
|
|
|
|
|
return certMeta;
|
|
|
|
}
|
|
|
|
|
2022-08-18 14:26:12 +02:00
|
|
|
Future<String> idtyStatus(String address) async {
|
2022-09-09 01:12:17 +02:00
|
|
|
// WalletOptionsProvider walletOptions =
|
|
|
|
// Provider.of<WalletOptionsProvider>(homeContext, listen: false);
|
|
|
|
|
2022-08-20 20:08:02 +02:00
|
|
|
var idtyIndex = await _getIdentityIndexOf(address);
|
2022-08-11 15:47:08 +02:00
|
|
|
|
|
|
|
if (idtyIndex == 0) {
|
|
|
|
return 'noid';
|
|
|
|
}
|
|
|
|
|
2022-08-19 23:15:41 +02:00
|
|
|
final idtyStatus = await _getStorage('identity.identities($idtyIndex)');
|
2022-08-11 15:47:08 +02:00
|
|
|
|
|
|
|
if (idtyStatus != null) {
|
|
|
|
final String status = idtyStatus['status'];
|
|
|
|
|
2022-09-09 01:12:17 +02:00
|
|
|
// if (address == walletOptions.address.text && status == 'Validated') {
|
|
|
|
// walletOptions.reloadBuild();
|
|
|
|
// }
|
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
return (status);
|
|
|
|
} else {
|
|
|
|
return 'expired';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:36 +02:00
|
|
|
Future<String> getGenesisHash() async {
|
|
|
|
final String genesisHash = await sdk.webView!.evalJavascript(
|
|
|
|
'api.genesisHash.toHex()',
|
|
|
|
wrapPromise: false,
|
|
|
|
) ??
|
2022-08-19 23:15:41 +02:00
|
|
|
[];
|
2022-08-18 18:22:36 +02:00
|
|
|
// log.d('genesisHash: $genesisHash');
|
2022-08-19 23:15:41 +02:00
|
|
|
// log.d('genesisHash: ${HEX.decode(genesisHash.substring(2))}');
|
2022-08-18 18:22:36 +02:00
|
|
|
return genesisHash;
|
|
|
|
}
|
|
|
|
|
2022-08-19 19:23:38 +02:00
|
|
|
Future<Uint8List> addressToPubkey(String address) async {
|
2022-08-19 14:10:27 +02:00
|
|
|
final pubkey = await sdk.api.account.decodeAddress([address]);
|
|
|
|
final String pubkeyHex = pubkey!.keys.first;
|
|
|
|
final pubkeyByte = HEX.decode(pubkeyHex.substring(2)) as Uint8List;
|
2022-08-20 20:08:02 +02:00
|
|
|
// final pubkey58 = Base58Encode(pubkeyByte);
|
2022-08-19 14:10:27 +02:00
|
|
|
|
2022-08-19 19:23:38 +02:00
|
|
|
return pubkeyByte;
|
2022-08-18 18:22:36 +02:00
|
|
|
}
|
2022-08-17 17:50:05 +02:00
|
|
|
|
|
|
|
// Future pubkeyToAddress(String pubkey) async {
|
|
|
|
// await sdk.api.account.encodeAddress([pubkey]);
|
|
|
|
// }
|
|
|
|
|
|
|
|
Future initCurrencyParameters() async {
|
|
|
|
currencyParameters['ss58'] =
|
2022-08-19 23:15:41 +02:00
|
|
|
await _getStorageConst('system.ss58Prefix.words');
|
2022-08-17 17:50:05 +02:00
|
|
|
currencyParameters['minCertForMembership'] =
|
2022-08-19 23:15:41 +02:00
|
|
|
await _getStorageConst('wot.minCertForMembership.words');
|
2022-08-17 17:50:05 +02:00
|
|
|
currencyParameters['newAccountPrice'] =
|
2022-08-19 23:15:41 +02:00
|
|
|
await _getStorageConst('account.newAccountPrice.words');
|
2022-08-17 17:50:05 +02:00
|
|
|
currencyParameters['existentialDeposit'] =
|
2022-08-19 23:15:41 +02:00
|
|
|
await _getStorageConst('balances.existentialDeposit.words');
|
2022-08-17 17:50:05 +02:00
|
|
|
currencyParameters['certPeriod'] =
|
2022-08-19 23:15:41 +02:00
|
|
|
await _getStorageConst('cert.certPeriod.words');
|
2022-08-17 17:50:05 +02:00
|
|
|
currencyParameters['certMaxByIssuer'] =
|
2022-08-19 23:15:41 +02:00
|
|
|
await _getStorageConst('cert.maxByIssuer.words');
|
2022-08-17 17:50:05 +02:00
|
|
|
currencyParameters['certValidityPeriod'] =
|
2022-08-19 23:15:41 +02:00
|
|
|
await _getStorageConst('cert.validityPeriod.words');
|
2022-08-17 17:50:05 +02:00
|
|
|
|
|
|
|
log.i('currencyParameters: $currencyParameters');
|
|
|
|
}
|
2022-08-11 15:47:08 +02:00
|
|
|
|
2022-08-18 14:26:12 +02:00
|
|
|
void cesiumIDisVisible() {
|
|
|
|
isCesiumIDVisible = !isCesiumIDVisible;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
/////////////////////////////////////
|
|
|
|
////// 3: SUBSTRATE CONNECTION //////
|
|
|
|
/////////////////////////////////////
|
|
|
|
|
2022-02-18 00:20:21 +01:00
|
|
|
Future<void> initApi() async {
|
2022-02-18 18:57:03 +01:00
|
|
|
sdkLoading = true;
|
2022-08-17 17:50:05 +02:00
|
|
|
await keyring.init([initSs58]);
|
|
|
|
keyring.setSS58(initSs58);
|
2022-02-18 00:20:21 +01:00
|
|
|
|
|
|
|
await sdk.init(keyring);
|
|
|
|
sdkReady = true;
|
2022-02-18 18:57:03 +01:00
|
|
|
sdkLoading = false;
|
2022-02-18 00:20:21 +01:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
String? getConnectedEndpoint() {
|
|
|
|
return sdk.api.connectedNode?.endpoint;
|
|
|
|
}
|
|
|
|
|
2022-05-20 15:15:29 +02:00
|
|
|
Future<void> connectNode(BuildContext ctx) async {
|
2022-08-06 02:22:41 +02:00
|
|
|
HomeProvider homeProvider = Provider.of<HomeProvider>(ctx, listen: false);
|
2022-08-17 23:01:20 +02:00
|
|
|
MyWalletsProvider myWalletProvider =
|
|
|
|
Provider.of<MyWalletsProvider>(ctx, listen: false);
|
2022-05-27 06:11:09 +02:00
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
homeProvider.changeMessage("connectionPending".tr(), 0);
|
2022-06-01 21:00:17 +02:00
|
|
|
|
2022-07-14 18:18:19 +02:00
|
|
|
// configBox.delete('customEndpoint');
|
|
|
|
final List<NetworkParams> listEndpoints =
|
|
|
|
configBox.containsKey('customEndpoint')
|
|
|
|
? [getDuniterCustomEndpoint()]
|
|
|
|
: getDuniterBootstrap();
|
|
|
|
|
2022-05-25 20:40:55 +02:00
|
|
|
int timeout = 10000;
|
2022-05-23 12:39:01 +02:00
|
|
|
|
2022-05-20 15:15:29 +02:00
|
|
|
if (sdk.api.connectedNode?.endpoint != null) {
|
|
|
|
await sdk.api.setting.unsubscribeBestNumber();
|
2022-02-19 23:51:12 +01:00
|
|
|
}
|
2022-05-20 15:15:29 +02:00
|
|
|
|
|
|
|
isLoadingEndpoint = true;
|
|
|
|
notifyListeners();
|
2022-07-14 18:18:19 +02:00
|
|
|
final res = await sdk.api.connectNode(keyring, listEndpoints).timeout(
|
2022-05-23 12:39:01 +02:00
|
|
|
Duration(milliseconds: timeout),
|
2022-02-19 23:51:12 +01:00
|
|
|
onTimeout: () => null,
|
|
|
|
);
|
2022-05-20 15:15:29 +02:00
|
|
|
isLoadingEndpoint = false;
|
|
|
|
notifyListeners();
|
2022-02-18 00:20:21 +01:00
|
|
|
if (res != null) {
|
|
|
|
nodeConnected = true;
|
2022-08-08 18:56:59 +02:00
|
|
|
// await getSs58Prefix();
|
2022-05-25 20:40:55 +02:00
|
|
|
|
|
|
|
// Subscribe bloc number
|
|
|
|
sdk.api.setting.subscribeBestNumber((res) {
|
|
|
|
blocNumber = int.parse(res.toString());
|
2022-06-12 18:03:17 +02:00
|
|
|
// log.d(sdk.api.connectedNode?.endpoint);
|
2022-06-01 21:00:17 +02:00
|
|
|
if (sdk.api.connectedNode?.endpoint == null) {
|
2022-06-12 18:03:17 +02:00
|
|
|
nodeConnected = false;
|
2022-08-06 02:22:41 +02:00
|
|
|
homeProvider.changeMessage("networkLost".tr(), 0);
|
2022-06-12 18:03:17 +02:00
|
|
|
} else {
|
|
|
|
nodeConnected = true;
|
2022-06-01 21:00:17 +02:00
|
|
|
}
|
2022-05-25 20:40:55 +02:00
|
|
|
notifyListeners();
|
|
|
|
});
|
2022-06-12 18:03:17 +02:00
|
|
|
|
2022-08-17 23:01:20 +02:00
|
|
|
await initCurrencyParameters();
|
2022-02-18 00:20:21 +01:00
|
|
|
notifyListeners();
|
2022-08-06 02:22:41 +02:00
|
|
|
homeProvider.changeMessage(
|
2022-06-17 20:18:54 +02:00
|
|
|
"wellConnectedToNode"
|
|
|
|
.tr(args: [getConnectedEndpoint()!.split('/')[2]]),
|
2022-06-01 21:00:17 +02:00
|
|
|
5);
|
2022-05-29 04:30:03 +02:00
|
|
|
// snackNode(ctx, true);
|
2022-05-20 15:15:29 +02:00
|
|
|
} else {
|
|
|
|
nodeConnected = false;
|
2022-05-25 20:40:55 +02:00
|
|
|
debugConnection = res.toString();
|
2022-05-20 15:15:29 +02:00
|
|
|
notifyListeners();
|
2022-08-06 02:22:41 +02:00
|
|
|
homeProvider.changeMessage("noDuniterEndointAvailable".tr(), 0);
|
2022-08-17 23:01:20 +02:00
|
|
|
if (!myWalletProvider.checkIfWalletExist()) snackNode(homeContext, false);
|
2022-02-18 00:20:21 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 15:15:29 +02:00
|
|
|
log.d(sdk.api.connectedNode?.endpoint);
|
2022-02-18 00:20:21 +01:00
|
|
|
}
|
2022-02-18 02:19:08 +01:00
|
|
|
|
2022-07-14 18:18:19 +02:00
|
|
|
List<NetworkParams> getDuniterBootstrap() {
|
|
|
|
List<NetworkParams> node = [];
|
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
for (String endpoint in configBox.get('endpoint')) {
|
2022-07-14 18:18:19 +02:00
|
|
|
final n = NetworkParams();
|
|
|
|
n.name = currencyName;
|
2022-08-06 02:22:41 +02:00
|
|
|
n.endpoint = endpoint;
|
2022-08-17 17:50:05 +02:00
|
|
|
n.ss58 = currencyParameters['ss58'] ?? initSs58;
|
2022-07-14 18:18:19 +02:00
|
|
|
node.add(n);
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkParams getDuniterCustomEndpoint() {
|
|
|
|
final nodeParams = NetworkParams();
|
|
|
|
nodeParams.name = currencyName;
|
|
|
|
nodeParams.endpoint = configBox.get('customEndpoint');
|
2022-08-17 17:50:05 +02:00
|
|
|
nodeParams.ss58 = currencyParameters['ss58'] ?? initSs58;
|
2022-07-14 18:18:19 +02:00
|
|
|
return nodeParams;
|
|
|
|
}
|
|
|
|
|
2022-05-04 19:00:09 +02:00
|
|
|
Future<String> importAccount(
|
|
|
|
{String mnemonic = '',
|
2022-05-19 07:00:25 +02:00
|
|
|
String derivePath = '',
|
2022-08-15 17:41:12 +02:00
|
|
|
required String password}) async {
|
|
|
|
const keytype = KeyType.mnemonic;
|
|
|
|
if (mnemonic != '') generatedMnemonic = mnemonic;
|
2022-02-19 23:51:12 +01:00
|
|
|
|
2022-02-18 19:49:37 +01:00
|
|
|
importIsLoading = true;
|
|
|
|
notifyListeners();
|
2022-08-15 17:41:12 +02:00
|
|
|
|
|
|
|
final json = await sdk.api.keyring
|
2022-02-20 20:49:05 +01:00
|
|
|
.importAccount(keyring,
|
|
|
|
keyType: keytype,
|
2022-08-15 17:41:12 +02:00
|
|
|
key: generatedMnemonic,
|
2022-05-19 07:00:25 +02:00
|
|
|
name: derivePath,
|
|
|
|
password: password,
|
2022-03-02 21:33:50 +01:00
|
|
|
derivePath: derivePath,
|
|
|
|
cryptoType: CryptoType.sr25519)
|
2022-02-18 19:49:37 +01:00
|
|
|
.catchError((e) {
|
|
|
|
importIsLoading = false;
|
|
|
|
notifyListeners();
|
|
|
|
});
|
2022-05-04 19:00:09 +02:00
|
|
|
if (json == null) return '';
|
2022-08-08 18:56:59 +02:00
|
|
|
// log.d(json);
|
2022-02-19 23:51:12 +01:00
|
|
|
try {
|
2022-05-19 07:00:25 +02:00
|
|
|
await sdk.api.keyring.addAccount(
|
2022-02-19 23:51:12 +01:00
|
|
|
keyring,
|
2022-02-22 10:39:45 +01:00
|
|
|
keyType: keytype,
|
2022-02-19 23:51:12 +01:00
|
|
|
acc: json,
|
2022-05-19 07:00:25 +02:00
|
|
|
password: password,
|
2022-02-19 23:51:12 +01:00
|
|
|
);
|
|
|
|
} catch (e) {
|
2022-08-06 02:22:41 +02:00
|
|
|
log.e(e);
|
2022-02-18 19:49:37 +01:00
|
|
|
importIsLoading = false;
|
|
|
|
notifyListeners();
|
2022-02-19 23:51:12 +01:00
|
|
|
}
|
2022-02-18 02:19:08 +01:00
|
|
|
|
2022-02-18 19:49:37 +01:00
|
|
|
importIsLoading = false;
|
2022-05-30 05:31:33 +02:00
|
|
|
|
2022-02-18 02:19:08 +01:00
|
|
|
notifyListeners();
|
2022-05-30 06:22:43 +02:00
|
|
|
return keyring.allAccounts.last.address!;
|
2022-02-18 02:19:08 +01:00
|
|
|
}
|
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
//////////////////////////////////
|
|
|
|
/////// 4: CRYPTOGRAPHY //////////
|
|
|
|
//////////////////////////////////
|
2022-05-28 21:15:47 +02:00
|
|
|
|
2022-05-19 07:00:25 +02:00
|
|
|
KeyPairData getKeypair(String address) {
|
|
|
|
return keyring.keyPairs.firstWhere((kp) => kp.address == address,
|
|
|
|
orElse: (() => KeyPairData()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> checkPassword(String address, String pass) async {
|
|
|
|
final account = getKeypair(address);
|
2022-06-01 15:17:07 +02:00
|
|
|
|
2022-05-19 07:00:25 +02:00
|
|
|
return await sdk.api.keyring.checkPassword(account, pass);
|
|
|
|
}
|
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
Future<String> getSeed(String address, String pin) async {
|
2022-05-29 00:00:57 +02:00
|
|
|
final account = getKeypair(address);
|
|
|
|
keyring.setCurrent(account);
|
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
final seed = await sdk.api.keyring.getDecryptedSeed(keyring, pin);
|
2022-05-29 00:00:57 +02:00
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
String seedText;
|
|
|
|
if (seed == null) {
|
|
|
|
seedText = '';
|
2022-05-29 00:00:57 +02:00
|
|
|
} else {
|
2022-08-06 02:22:41 +02:00
|
|
|
seedText = seed.seed!.split('//')[0];
|
2022-05-29 00:00:57 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
log.d(seedText);
|
|
|
|
return seedText;
|
2022-05-29 00:00:57 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:00:25 +02:00
|
|
|
int getDerivationNumber(String address) {
|
|
|
|
final account = getKeypair(address);
|
2022-05-20 15:15:29 +02:00
|
|
|
final deriveNbr = account.name!.split('//')[1];
|
2022-05-19 07:00:25 +02:00
|
|
|
return int.parse(deriveNbr);
|
|
|
|
}
|
|
|
|
|
2022-05-31 18:23:56 +02:00
|
|
|
Future<KeyPairData?> changePassword(BuildContext context, String address,
|
|
|
|
String passOld, String? passNew) async {
|
2022-05-19 07:00:25 +02:00
|
|
|
final account = getKeypair(address);
|
2022-08-06 02:22:41 +02:00
|
|
|
MyWalletsProvider myWalletProvider =
|
2022-05-31 18:23:56 +02:00
|
|
|
Provider.of<MyWalletsProvider>(context, listen: false);
|
2022-05-19 07:00:25 +02:00
|
|
|
keyring.setCurrent(account);
|
2022-08-06 02:22:41 +02:00
|
|
|
myWalletProvider.resetPinCode();
|
2022-05-19 07:00:25 +02:00
|
|
|
|
|
|
|
return await sdk.api.keyring.changePassword(keyring, passOld, passNew);
|
|
|
|
}
|
|
|
|
|
2022-02-19 23:51:12 +01:00
|
|
|
Future<void> deleteAllAccounts() async {
|
|
|
|
for (var account in keyring.allAccounts) {
|
|
|
|
await sdk.api.keyring.deleteAccount(keyring, account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 16:51:40 +02:00
|
|
|
Future<void> deleteAccounts(List<String> address) async {
|
|
|
|
for (var a in address) {
|
|
|
|
final account = getKeypair(a);
|
|
|
|
await sdk.api.keyring.deleteAccount(keyring, account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:00:25 +02:00
|
|
|
Future<String> generateMnemonic({String lang = appLang}) async {
|
2022-08-17 17:50:05 +02:00
|
|
|
final gen = await sdk.api.keyring
|
|
|
|
.generateMnemonic(currencyParameters['ss58'] ?? initSs58);
|
2022-02-18 19:49:37 +01:00
|
|
|
generatedMnemonic = gen.mnemonic!;
|
2022-02-19 23:51:12 +01:00
|
|
|
|
2022-02-22 10:39:45 +01:00
|
|
|
return gen.mnemonic!;
|
2022-02-18 18:57:03 +01:00
|
|
|
}
|
2022-02-19 23:51:12 +01:00
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
Future<String> setCurrentWallet(WalletData wallet) async {
|
2022-05-28 21:15:47 +02:00
|
|
|
final currentChestNumber = configBox.get('currentChest');
|
2022-08-06 02:22:41 +02:00
|
|
|
ChestData newChestData = chestBox.get(currentChestNumber)!;
|
|
|
|
newChestData.defaultWallet = wallet.number;
|
|
|
|
await chestBox.put(currentChestNumber, newChestData);
|
2022-05-28 21:15:47 +02:00
|
|
|
|
2022-05-19 13:44:22 +02:00
|
|
|
try {
|
2022-08-06 02:22:41 +02:00
|
|
|
final acc = getKeypair(wallet.address!);
|
2022-05-19 13:44:22 +02:00
|
|
|
keyring.setCurrent(acc);
|
|
|
|
return acc.address!;
|
|
|
|
} catch (e) {
|
|
|
|
return (e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyPairData getCurrentWallet() {
|
|
|
|
try {
|
|
|
|
final acc = keyring.current;
|
|
|
|
return acc;
|
|
|
|
} catch (e) {
|
|
|
|
return KeyPairData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
Future<String> derive(
|
|
|
|
BuildContext context, String address, int number, String password) async {
|
|
|
|
final keypair = getKeypair(address);
|
|
|
|
|
|
|
|
final seedMap =
|
|
|
|
await keyring.store.getDecryptedSeed(keypair.pubKey, password);
|
|
|
|
|
|
|
|
if (seedMap?['type'] != 'mnemonic') return '';
|
|
|
|
final List seedList = seedMap!['seed'].split('//');
|
|
|
|
generatedMnemonic = seedList[0];
|
|
|
|
|
|
|
|
return await importAccount(
|
|
|
|
mnemonic: generatedMnemonic,
|
|
|
|
derivePath: '//$number',
|
|
|
|
password: password);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> generateRootKeypair(String address, String password) async {
|
|
|
|
final keypair = getKeypair(address);
|
|
|
|
|
|
|
|
final seedMap =
|
|
|
|
await keyring.store.getDecryptedSeed(keypair.pubKey, password);
|
|
|
|
|
|
|
|
if (seedMap?['type'] != 'mnemonic') return '';
|
|
|
|
final List seedList = seedMap!['seed'].split('//');
|
|
|
|
generatedMnemonic = seedList[0];
|
|
|
|
|
2022-08-15 17:41:12 +02:00
|
|
|
return await importAccount(password: password);
|
2022-08-11 15:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> isMnemonicValid(String mnemonic) async {
|
|
|
|
// Needed for bad encoding of UTF-8
|
|
|
|
mnemonic = mnemonic.replaceAll('é', 'é');
|
|
|
|
mnemonic = mnemonic.replaceAll('è', 'è');
|
|
|
|
|
|
|
|
return await sdk.api.keyring.checkMnemonicValid(mnemonic);
|
|
|
|
}
|
|
|
|
|
2022-08-20 16:55:55 +02:00
|
|
|
Future<String> csToV2Address(String salt, String password) async {
|
2022-08-16 16:13:11 +02:00
|
|
|
final scrypt = pc.KeyDerivator('scrypt');
|
|
|
|
|
|
|
|
scrypt.init(
|
|
|
|
pc.ScryptParameters(
|
|
|
|
4096,
|
|
|
|
16,
|
|
|
|
1,
|
|
|
|
32,
|
|
|
|
Uint8List.fromList(salt.codeUnits),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
final rawSeed = scrypt.process(Uint8List.fromList(password.codeUnits));
|
|
|
|
final rawSeedHex = '0x${HEX.encode(rawSeed)}';
|
|
|
|
|
|
|
|
// Just get the address without keystore
|
2022-08-18 01:31:47 +02:00
|
|
|
final newAddress = await sdk.api.keyring.addressFromRawSeed(
|
|
|
|
currencyParameters['ss58']!,
|
|
|
|
cryptoType: CryptoType.ed25519,
|
|
|
|
rawSeed: rawSeedHex);
|
|
|
|
|
|
|
|
g1V1NewAddress = newAddress.address!;
|
|
|
|
notifyListeners();
|
2022-08-20 16:55:55 +02:00
|
|
|
return g1V1NewAddress;
|
2022-08-16 16:13:11 +02:00
|
|
|
}
|
|
|
|
|
2022-08-20 20:08:02 +02:00
|
|
|
Future<List> getBalanceAndIdtyStatus(
|
|
|
|
String fromAddress, String toAddress) async {
|
|
|
|
final fromBalance = fromAddress == ''
|
|
|
|
? {'transferableBalance': 0}
|
|
|
|
: await getBalance(fromAddress);
|
|
|
|
final fromIdtyStatus =
|
|
|
|
fromAddress == '' ? 'noid' : await idtyStatus(fromAddress);
|
|
|
|
final fromHasConsumer =
|
|
|
|
fromAddress == '' ? false : await hasAccountConsumers(fromAddress);
|
|
|
|
final toIdtyStatus = await idtyStatus(toAddress);
|
2022-08-18 14:26:12 +02:00
|
|
|
|
2022-08-20 20:08:02 +02:00
|
|
|
return [fromBalance, fromIdtyStatus, toIdtyStatus, fromHasConsumer];
|
2022-08-18 14:26:12 +02:00
|
|
|
}
|
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
//////////////////////////////////////
|
|
|
|
///////// 5: CALLS EXECUTION /////////
|
|
|
|
//////////////////////////////////////
|
|
|
|
|
2022-05-27 08:54:29 +02:00
|
|
|
Future<String> pay(
|
2022-05-19 13:44:22 +02:00
|
|
|
{required String fromAddress,
|
|
|
|
required String destAddress,
|
|
|
|
required double amount,
|
|
|
|
required String password}) async {
|
2022-05-26 02:19:29 +02:00
|
|
|
transactionStatus = '';
|
2022-08-11 15:47:08 +02:00
|
|
|
final int amountUnit = (amount * 100).toInt();
|
|
|
|
|
2022-09-06 09:29:29 +02:00
|
|
|
final sender = await _setSender(fromAddress);
|
2022-08-11 15:47:08 +02:00
|
|
|
|
2022-08-11 19:19:50 +02:00
|
|
|
final globalBalance = await getBalance(fromAddress);
|
|
|
|
TxInfoData txInfo;
|
2022-08-12 10:04:55 +02:00
|
|
|
List txOptions = [];
|
|
|
|
String? rawParams;
|
2022-09-11 13:14:52 +02:00
|
|
|
final bool isUdUnit = configBox.get('isUdUnit') ?? false;
|
|
|
|
late String palette;
|
|
|
|
late String call;
|
|
|
|
late String tx2;
|
|
|
|
|
|
|
|
if (amount == -1) {
|
|
|
|
palette = 'balances';
|
|
|
|
call = 'transferAll';
|
|
|
|
txOptions = [destAddress, false];
|
|
|
|
tx2 = 'api.tx.balances.transferAll("$destAddress", false)';
|
|
|
|
} else {
|
|
|
|
if (isUdUnit) {
|
|
|
|
palette = 'universalDividend';
|
|
|
|
call = 'transferUd';
|
|
|
|
} else {
|
|
|
|
palette = 'balances';
|
|
|
|
call = 'transferKeepAlive';
|
|
|
|
}
|
|
|
|
txOptions = [destAddress, amountUnit];
|
|
|
|
tx2 = 'api.tx.$palette.$call("$destAddress", $amountUnit)';
|
|
|
|
}
|
2022-08-11 19:19:50 +02:00
|
|
|
|
|
|
|
if (globalBalance['unclaimedUds'] == 0) {
|
2022-09-11 13:14:52 +02:00
|
|
|
txInfo = TxInfoData(palette, call, sender);
|
2022-08-11 19:19:50 +02:00
|
|
|
} else {
|
|
|
|
txInfo = TxInfoData(
|
|
|
|
'utility',
|
|
|
|
'batchAll',
|
|
|
|
sender,
|
|
|
|
);
|
2022-08-12 10:04:55 +02:00
|
|
|
const tx1 = 'api.tx.universalDividend.claimUds()';
|
|
|
|
rawParams = '[[$tx1, $tx2]]';
|
2022-08-11 19:19:50 +02:00
|
|
|
}
|
|
|
|
|
2022-08-19 23:15:41 +02:00
|
|
|
return await _executeCall(txInfo, txOptions, password, rawParams);
|
2022-05-27 08:54:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> certify(
|
2022-08-27 05:43:43 +02:00
|
|
|
String fromAddress, String destAddress, String password) async {
|
2022-05-27 08:54:29 +02:00
|
|
|
transactionStatus = '';
|
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
final myIdtyStatus = await idtyStatus(fromAddress);
|
2022-08-27 05:43:43 +02:00
|
|
|
final toIdtyStatus = await idtyStatus(destAddress);
|
2022-05-27 08:54:29 +02:00
|
|
|
|
2022-08-20 20:08:02 +02:00
|
|
|
final fromIndex = await _getIdentityIndexOf(fromAddress);
|
2022-08-27 05:43:43 +02:00
|
|
|
final toIndex = await _getIdentityIndexOf(destAddress);
|
2022-08-08 16:24:40 +02:00
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
if (myIdtyStatus != 'Validated') {
|
2022-05-27 08:54:29 +02:00
|
|
|
transactionStatus = 'notMember';
|
|
|
|
notifyListeners();
|
|
|
|
return 'notMember';
|
|
|
|
}
|
|
|
|
|
2022-08-27 23:26:37 +02:00
|
|
|
final sender = await _setSender(fromAddress);
|
2022-05-27 08:54:29 +02:00
|
|
|
TxInfoData txInfo;
|
2022-08-10 07:18:04 +02:00
|
|
|
List txOptions = [];
|
2022-08-12 10:04:55 +02:00
|
|
|
String? rawParams;
|
2022-05-27 08:54:29 +02:00
|
|
|
|
2022-08-27 05:43:43 +02:00
|
|
|
final toCerts = await getCerts(destAddress);
|
2022-08-17 17:50:05 +02:00
|
|
|
|
|
|
|
// log.d('debug: ${currencyParameters['minCertForMembership']}');
|
2022-08-11 15:47:08 +02:00
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
if (toIdtyStatus == 'noid') {
|
2022-05-27 08:54:29 +02:00
|
|
|
txInfo = TxInfoData(
|
|
|
|
'identity',
|
|
|
|
'createIdentity',
|
|
|
|
sender,
|
|
|
|
);
|
2022-08-27 05:43:43 +02:00
|
|
|
txOptions = [destAddress];
|
2022-08-06 02:22:41 +02:00
|
|
|
} else if (toIdtyStatus == 'Validated' ||
|
|
|
|
toIdtyStatus == 'ConfirmedByOwner') {
|
2022-08-17 17:50:05 +02:00
|
|
|
if (toCerts[0] >= currencyParameters['minCertForMembership']! - 1 &&
|
2022-08-08 16:24:40 +02:00
|
|
|
toIdtyStatus != 'Validated') {
|
2022-08-08 18:56:59 +02:00
|
|
|
log.i('Batch cert and membership validation');
|
2022-08-12 10:04:55 +02:00
|
|
|
txInfo = TxInfoData(
|
|
|
|
'utility',
|
|
|
|
'batchAll',
|
|
|
|
sender,
|
|
|
|
);
|
2022-08-16 19:59:53 +02:00
|
|
|
final tx1 = 'api.tx.cert.addCert($fromIndex, $toIndex)';
|
|
|
|
final tx2 = 'api.tx.identity.validateIdentity($toIndex)';
|
2022-08-12 10:04:55 +02:00
|
|
|
|
|
|
|
rawParams = '[[$tx1, $tx2]]';
|
2022-08-08 16:24:40 +02:00
|
|
|
} else {
|
|
|
|
txInfo = TxInfoData(
|
|
|
|
'cert',
|
|
|
|
'addCert',
|
|
|
|
sender,
|
|
|
|
);
|
2022-08-10 07:18:04 +02:00
|
|
|
txOptions = [fromIndex, toIndex];
|
2022-08-08 16:24:40 +02:00
|
|
|
}
|
2022-05-27 08:54:29 +02:00
|
|
|
} else {
|
|
|
|
transactionStatus = 'cantBeCert';
|
|
|
|
notifyListeners();
|
|
|
|
return 'cantBeCert';
|
|
|
|
}
|
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
log.d('Cert action: ${txInfo.call!}');
|
2022-08-19 23:15:41 +02:00
|
|
|
return await _executeCall(txInfo, txOptions, password, rawParams);
|
2022-02-19 23:51:12 +01:00
|
|
|
}
|
2022-02-20 20:49:05 +01:00
|
|
|
|
2022-05-24 16:51:40 +02:00
|
|
|
Future<String> confirmIdentity(
|
2022-05-27 08:54:29 +02:00
|
|
|
String fromAddress, String name, String password) async {
|
2022-09-06 09:29:29 +02:00
|
|
|
final sender = await _setSender(fromAddress);
|
2022-05-24 16:51:40 +02:00
|
|
|
|
|
|
|
final txInfo = TxInfoData(
|
|
|
|
'identity',
|
|
|
|
'confirmIdentity',
|
|
|
|
sender,
|
|
|
|
);
|
2022-08-11 15:47:08 +02:00
|
|
|
final txOptions = [name];
|
2022-05-24 16:51:40 +02:00
|
|
|
|
2022-08-19 23:15:41 +02:00
|
|
|
return await _executeCall(txInfo, txOptions, password);
|
2022-06-04 17:24:33 +02:00
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:36 +02:00
|
|
|
Future<String> migrateIdentity(
|
|
|
|
{required String fromAddress,
|
|
|
|
required String destAddress,
|
2022-08-20 20:08:02 +02:00
|
|
|
required String fromPassword,
|
2022-08-20 16:55:55 +02:00
|
|
|
required String destPassword,
|
2022-08-20 20:08:02 +02:00
|
|
|
required Map fromBalance,
|
2022-08-20 16:55:55 +02:00
|
|
|
bool withBalance = false}) async {
|
2022-08-18 18:22:36 +02:00
|
|
|
transactionStatus = '';
|
2022-09-06 09:29:29 +02:00
|
|
|
final sender = await _setSender(fromAddress);
|
2022-08-18 18:22:36 +02:00
|
|
|
|
|
|
|
TxInfoData txInfo;
|
|
|
|
List txOptions = [];
|
|
|
|
String? rawParams;
|
|
|
|
|
2022-08-19 19:23:38 +02:00
|
|
|
final prefix = 'icok'.codeUnits;
|
2022-08-19 23:15:41 +02:00
|
|
|
final genesisHashString = await getGenesisHash();
|
|
|
|
final genesisHash = HEX.decode(genesisHashString.substring(2)) as Uint8List;
|
2022-08-20 20:08:02 +02:00
|
|
|
final idtyIndex = _int32bytes(await _getIdentityIndexOf(fromAddress));
|
2022-08-19 19:23:38 +02:00
|
|
|
final oldPubkey = await addressToPubkey(fromAddress);
|
|
|
|
final messageToSign =
|
|
|
|
Uint8List.fromList(prefix + genesisHash + idtyIndex + oldPubkey);
|
2022-08-19 23:15:41 +02:00
|
|
|
final messageToSignHex = HEX.encode(messageToSign);
|
2022-08-19 14:10:27 +02:00
|
|
|
final newKeySig =
|
2022-08-20 20:08:02 +02:00
|
|
|
await _signMessage(messageToSign, destAddress, destPassword);
|
2022-08-18 18:22:36 +02:00
|
|
|
|
2022-08-19 23:15:41 +02:00
|
|
|
// messageToSign: [105, 99, 111, 107, 7, 193, 18, 255, 106, 185, 215, 208, 213, 49, 235, 229, 159, 152, 179, 83, 24, 178, 129, 59, 22, 85, 87, 115, 128, 129, 157, 56, 214, 24, 45, 153, 21, 0, 0, 0, 181, 82, 178, 99, 198, 4, 156, 190, 78, 35, 102, 137, 255, 7, 162, 31, 16, 79, 255, 132, 130, 237, 230, 222, 176, 88, 245, 217, 237, 78, 196, 239]
|
|
|
|
|
2022-08-19 19:23:38 +02:00
|
|
|
log.d("""
|
2022-08-19 23:15:41 +02:00
|
|
|
fromAddress: $fromAddress
|
|
|
|
destAddress: $destAddress
|
|
|
|
genesisHashString: $genesisHashString
|
|
|
|
|
2022-08-19 19:23:38 +02:00
|
|
|
prefix: $prefix
|
|
|
|
genesisHash: $genesisHash
|
|
|
|
idtyIndex: $idtyIndex
|
|
|
|
oldPubkey: $oldPubkey
|
2022-08-19 23:15:41 +02:00
|
|
|
|
2022-08-19 19:23:38 +02:00
|
|
|
messageToSign: $messageToSign
|
2022-08-19 23:15:41 +02:00
|
|
|
messageToSignHex: $messageToSignHex
|
2022-08-19 19:23:38 +02:00
|
|
|
newKeySig: $newKeySig""");
|
|
|
|
|
2022-08-20 16:55:55 +02:00
|
|
|
if (withBalance) {
|
|
|
|
txInfo = TxInfoData(
|
|
|
|
'utility',
|
|
|
|
'batchAll',
|
|
|
|
sender,
|
|
|
|
);
|
2022-08-18 18:22:36 +02:00
|
|
|
|
2022-08-20 16:55:55 +02:00
|
|
|
const tx1 = 'api.tx.universalDividend.claimUds()';
|
|
|
|
final tx2 =
|
|
|
|
'api.tx.identity.changeOwnerKey("$destAddress", "$newKeySig")';
|
2022-08-29 20:50:43 +02:00
|
|
|
final tx3 = 'api.tx.balances.transferAll("$destAddress", false)';
|
2022-08-18 18:22:36 +02:00
|
|
|
|
2022-08-29 20:50:43 +02:00
|
|
|
rawParams = fromBalance['unclaimedUds'] == 0
|
|
|
|
? '[[$tx2, $tx3]]'
|
|
|
|
: '[[$tx1, $tx2, $tx3]]';
|
2022-08-20 16:55:55 +02:00
|
|
|
} else {
|
|
|
|
txInfo = TxInfoData(
|
|
|
|
'identity',
|
|
|
|
'changeOwnerKey',
|
|
|
|
sender,
|
|
|
|
);
|
2022-08-19 14:10:27 +02:00
|
|
|
|
2022-08-20 16:55:55 +02:00
|
|
|
txOptions = [destAddress, newKeySig];
|
|
|
|
}
|
2022-08-19 14:10:27 +02:00
|
|
|
|
2022-08-20 20:08:02 +02:00
|
|
|
return await _executeCall(txInfo, txOptions, fromPassword, rawParams);
|
2022-08-18 18:22:36 +02:00
|
|
|
}
|
|
|
|
|
2022-06-05 02:22:22 +02:00
|
|
|
Future revokeIdentity(String address, String password) async {
|
2022-08-20 20:08:02 +02:00
|
|
|
final idtyIndex = await _getIdentityIndexOf(address);
|
2022-09-06 09:29:29 +02:00
|
|
|
final sender = await _setSender(address);
|
2022-06-05 02:22:22 +02:00
|
|
|
|
2022-09-08 14:37:00 +02:00
|
|
|
final prefix = 'revo'.codeUnits;
|
2022-09-06 12:14:50 +02:00
|
|
|
final genesisHashString = await getGenesisHash();
|
|
|
|
final genesisHash = HEX.decode(genesisHashString.substring(2)) as Uint8List;
|
|
|
|
final idtyIndexBytes = _int32bytes(idtyIndex);
|
|
|
|
final messageToSign =
|
2022-09-08 20:15:19 +02:00
|
|
|
Uint8List.fromList(prefix + genesisHash + idtyIndexBytes);
|
2022-09-08 20:52:43 +02:00
|
|
|
final revocationSig =
|
|
|
|
(await _signMessage(messageToSign, address, password)).substring(2);
|
|
|
|
final revocationSigTyped = '0x01$revocationSig';
|
2022-06-05 02:22:22 +02:00
|
|
|
|
2022-09-06 12:14:50 +02:00
|
|
|
final txInfo = TxInfoData(
|
2022-09-06 09:29:29 +02:00
|
|
|
'identity',
|
|
|
|
'revokeIdentity',
|
2022-06-05 02:22:22 +02:00
|
|
|
sender,
|
|
|
|
);
|
|
|
|
|
2022-09-08 20:52:43 +02:00
|
|
|
final txOptions = [idtyIndex, address, revocationSigTyped];
|
2022-08-19 23:15:41 +02:00
|
|
|
return await _executeCall(txInfo, txOptions, password);
|
2022-06-05 02:22:22 +02:00
|
|
|
}
|
|
|
|
|
2022-08-18 14:26:12 +02:00
|
|
|
Future migrateCsToV2(String salt, String password, String destAddress,
|
2022-08-19 14:10:27 +02:00
|
|
|
{required destPassword,
|
2022-08-20 20:08:02 +02:00
|
|
|
required Map balance,
|
2022-08-19 14:10:27 +02:00
|
|
|
String idtyStatus = 'noid'}) async {
|
2022-08-18 14:26:12 +02:00
|
|
|
final scrypt = pc.KeyDerivator('scrypt');
|
|
|
|
|
|
|
|
scrypt.init(
|
|
|
|
pc.ScryptParameters(
|
|
|
|
4096,
|
|
|
|
16,
|
|
|
|
1,
|
|
|
|
32,
|
|
|
|
Uint8List.fromList(salt.codeUnits),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
final rawSeed = scrypt.process(Uint8List.fromList(password.codeUnits));
|
|
|
|
final rawSeedHex = '0x${HEX.encode(rawSeed)}';
|
|
|
|
|
|
|
|
final json = await sdk.api.keyring.importAccount(keyring,
|
|
|
|
keyType: KeyType.rawSeed,
|
|
|
|
key: rawSeedHex,
|
|
|
|
name: 'test',
|
|
|
|
password: 'password',
|
|
|
|
derivePath: '',
|
|
|
|
cryptoType: CryptoType.ed25519);
|
|
|
|
|
|
|
|
final keypair = await sdk.api.keyring.addAccount(
|
|
|
|
keyring,
|
|
|
|
keyType: KeyType.rawSeed,
|
|
|
|
acc: json!,
|
|
|
|
password: password,
|
|
|
|
);
|
|
|
|
|
2022-08-18 18:22:36 +02:00
|
|
|
log.d('g1migration idtyStatus: $idtyStatus');
|
|
|
|
if (idtyStatus != 'noid') {
|
|
|
|
await migrateIdentity(
|
|
|
|
fromAddress: keypair.address!,
|
|
|
|
destAddress: destAddress,
|
2022-08-20 20:08:02 +02:00
|
|
|
fromPassword: 'password',
|
2022-08-20 16:55:55 +02:00
|
|
|
destPassword: destPassword,
|
2022-08-20 20:08:02 +02:00
|
|
|
withBalance: true,
|
|
|
|
fromBalance: balance);
|
|
|
|
} else if (balance['transferableBalance'] != 0) {
|
2022-08-18 14:26:12 +02:00
|
|
|
await pay(
|
|
|
|
fromAddress: keypair.address!,
|
|
|
|
destAddress: destAddress,
|
|
|
|
amount: -1,
|
|
|
|
password: 'password');
|
|
|
|
}
|
|
|
|
|
|
|
|
await sdk.api.keyring.deleteAccount(keyring, keypair);
|
2022-08-18 01:31:47 +02:00
|
|
|
}
|
|
|
|
|
2022-08-28 05:08:41 +02:00
|
|
|
Future spawnBlock([int number = 1, int until = 0]) async {
|
|
|
|
if (!kDebugMode) return;
|
|
|
|
if (blocNumber < until) {
|
|
|
|
number = until - blocNumber;
|
|
|
|
}
|
2022-08-24 06:36:26 +02:00
|
|
|
for (var i = 1; i <= number; i++) {
|
2022-08-27 23:26:37 +02:00
|
|
|
await sdk.webView!
|
|
|
|
.evalJavascript('api.rpc.engine.createBlock(true, true)');
|
2022-08-24 06:36:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
void reload() {
|
|
|
|
notifyListeners();
|
2022-08-08 18:56:59 +02:00
|
|
|
}
|
2022-02-19 23:51:12 +01:00
|
|
|
}
|
|
|
|
|
2022-08-11 15:47:08 +02:00
|
|
|
////////////////////////////////////////////
|
|
|
|
/////// 6: UI ELEMENTS (off class) /////////
|
|
|
|
////////////////////////////////////////////
|
|
|
|
|
2022-02-19 23:51:12 +01:00
|
|
|
void snack(BuildContext context, String message, {int duration = 2}) {
|
|
|
|
final snackBar =
|
|
|
|
SnackBar(content: Text(message), duration: Duration(seconds: duration));
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
|
|
|
}
|
|
|
|
|
|
|
|
class AddressInfo {
|
|
|
|
final String? address;
|
|
|
|
double balance;
|
|
|
|
|
|
|
|
AddressInfo({@required this.address, this.balance = 0});
|
|
|
|
}
|
|
|
|
|
2022-05-20 15:15:29 +02:00
|
|
|
void snackNode(BuildContext context, bool isConnected) {
|
2022-08-06 02:22:41 +02:00
|
|
|
String message;
|
2022-05-20 15:15:29 +02:00
|
|
|
if (!isConnected) {
|
2022-08-17 23:28:19 +02:00
|
|
|
message = "noDuniterNodeAvailableTryLater".tr();
|
2022-05-20 15:15:29 +02:00
|
|
|
} else {
|
2022-08-06 02:22:41 +02:00
|
|
|
SubstrateSdk sub = Provider.of<SubstrateSdk>(context, listen: false);
|
2022-05-27 06:11:09 +02:00
|
|
|
|
2022-08-06 02:22:41 +02:00
|
|
|
message =
|
|
|
|
"${"youAreConnectedToNode".tr()}\n${sub.getConnectedEndpoint()!.split('//')[1]}";
|
2022-05-20 15:15:29 +02:00
|
|
|
}
|
2022-05-21 06:47:26 +02:00
|
|
|
final snackBar = SnackBar(
|
|
|
|
padding: const EdgeInsets.all(20),
|
2022-08-06 02:22:41 +02:00
|
|
|
content: Text(message, style: const TextStyle(fontSize: 16)),
|
2022-05-27 12:54:25 +02:00
|
|
|
duration: const Duration(seconds: 4));
|
2022-05-20 15:15:29 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
|
|
|
}
|
|
|
|
|
2022-02-19 23:51:12 +01:00
|
|
|
String getShortPubkey(String pubkey) {
|
2022-05-28 00:17:50 +02:00
|
|
|
String pubkeyShort = truncate(pubkey, 7,
|
2022-02-19 23:51:12 +01:00
|
|
|
omission: String.fromCharCode(0x2026),
|
|
|
|
position: TruncatePosition.end) +
|
2022-05-28 00:17:50 +02:00
|
|
|
truncate(pubkey, 6, omission: "", position: TruncatePosition.start);
|
2022-02-19 23:51:12 +01:00
|
|
|
return pubkeyShort;
|
2022-02-18 00:20:21 +01:00
|
|
|
}
|
2022-05-24 16:51:40 +02:00
|
|
|
|
|
|
|
class PasswordException implements Exception {
|
|
|
|
String cause;
|
|
|
|
PasswordException(this.cause);
|
|
|
|
}
|
2022-08-19 23:15:41 +02:00
|
|
|
|
2022-08-20 20:08:02 +02:00
|
|
|
Uint8List _int32bytes(int value) =>
|
2022-08-19 23:15:41 +02:00
|
|
|
Uint8List(4)..buffer.asInt32List()[0] = value;
|
2022-09-11 13:14:52 +02:00
|
|
|
|
|
|
|
double round(double number, [int decimal = 2]) {
|
|
|
|
return double.parse((number.toStringAsFixed(decimal)));
|
|
|
|
}
|