2021-10-01 15:13:10 +00:00
|
|
|
import 'package:cake_wallet/core/transaction_history.dart';
|
|
|
|
import 'package:cake_wallet/core/wallet_base.dart';
|
|
|
|
import 'package:cake_wallet/entities/balance.dart';
|
|
|
|
import 'package:cake_wallet/entities/transaction_info.dart';
|
|
|
|
import 'package:cake_wallet/store/app_store.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
import 'package:cake_wallet/bitcoin/electrum_wallet.dart';
|
|
|
|
import 'package:cake_wallet/entities/wallet_type.dart';
|
|
|
|
import 'package:cake_wallet/monero/monero_subaddress_list.dart';
|
|
|
|
import 'package:cake_wallet/monero/monero_wallet.dart';
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:cake_wallet/store/yat/yat_exception.dart';
|
|
|
|
import 'package:http/http.dart';
|
2021-11-02 09:17:24 +00:00
|
|
|
import 'dart:async';
|
2021-10-01 15:13:10 +00:00
|
|
|
|
|
|
|
part 'yat_store.g.dart';
|
|
|
|
|
2021-10-04 13:03:35 +00:00
|
|
|
class YatLink {
|
2021-11-02 09:17:24 +00:00
|
|
|
static const partnerId = 'CW';
|
2021-10-04 13:03:35 +00:00
|
|
|
static const baseDevUrl = 'https://yat.fyi';
|
|
|
|
static const baseReleaseUrl = 'https://y.at';
|
2021-11-02 09:17:24 +00:00
|
|
|
static const signInSuffix = '/partner/$partnerId/link-email';
|
2021-10-04 13:03:35 +00:00
|
|
|
static const createSuffix = '/create';
|
2021-11-02 09:17:24 +00:00
|
|
|
static const managePath = '/partner/$partnerId/manage';
|
2021-10-04 13:03:35 +00:00
|
|
|
static const queryParameter = '?addresses=';
|
|
|
|
static const requestDevUrl = 'https://a.yat.fyi/emoji_id/';
|
|
|
|
static const requestReleaseUrl = 'https://a.y.at/emoji_id/';
|
2021-11-02 09:17:24 +00:00
|
|
|
static const startFlowUrl = 'https://www.y03btrk.com/4RQSJ/55M6S/';
|
|
|
|
static const isDevMode = false;
|
2021-10-04 13:03:35 +00:00
|
|
|
static const tags = <String, List<String>>{"XMR" : ['0x1001', '0x1002'],
|
|
|
|
"BTC" : ['0x1003'], "LTC" : ['0x3fff']};
|
2021-11-02 09:17:24 +00:00
|
|
|
|
|
|
|
static String get baseUrl => YatLink.isDevMode
|
|
|
|
? YatLink.baseDevUrl
|
|
|
|
: YatLink.baseReleaseUrl;
|
2021-10-04 13:03:35 +00:00
|
|
|
}
|
2021-10-01 15:13:10 +00:00
|
|
|
|
|
|
|
Future<List<String>> fetchYatAddress(String emojiId, String ticker) async {
|
2021-10-04 13:03:35 +00:00
|
|
|
final requestURL = YatLink.isDevMode
|
|
|
|
? YatLink.requestDevUrl
|
|
|
|
: YatLink.requestReleaseUrl;
|
|
|
|
final url = requestURL + emojiId;
|
2021-10-01 15:13:10 +00:00
|
|
|
final response = await get(url);
|
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
throw YatException(text: response.body.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final result = responseJSON['result'] as List<dynamic>;
|
|
|
|
|
|
|
|
if (result?.isEmpty ?? true) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
final List<String> addresses = [];
|
2021-10-04 13:03:35 +00:00
|
|
|
final currency = ticker.toUpperCase();
|
2021-10-01 15:13:10 +00:00
|
|
|
|
|
|
|
for (var elem in result) {
|
2021-10-04 13:03:35 +00:00
|
|
|
final tag = elem['tag'] as String;
|
|
|
|
if (tag?.isEmpty ?? true) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (YatLink.tags[currency]?.contains(tag) ?? false) {
|
|
|
|
final yatAddress = elem['data'] as String;
|
|
|
|
if (yatAddress?.isNotEmpty ?? false) {
|
|
|
|
addresses.add(yatAddress);
|
|
|
|
}
|
2021-10-01 15:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses;
|
|
|
|
}
|
|
|
|
|
2021-11-02 09:17:24 +00:00
|
|
|
Future<String> visualisationForEmojiId(String emojiId) async {
|
|
|
|
final requestURL = YatLink.isDevMode
|
|
|
|
? YatLink.requestDevUrl
|
|
|
|
: YatLink.requestReleaseUrl;
|
|
|
|
final url = requestURL + emojiId + '/json/VisualizerFileLocations';
|
|
|
|
final response = await get(url);
|
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
|
|
|
final data = responseJSON['data'] as Map<String, dynamic>;
|
|
|
|
final result = data['gif'] as String ?? '';
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-01 15:13:10 +00:00
|
|
|
class YatStore = YatStoreBase with _$YatStore;
|
|
|
|
|
|
|
|
abstract class YatStoreBase with Store {
|
|
|
|
YatStoreBase({@required this.appStore}) {
|
|
|
|
_wallet ??= appStore.wallet;
|
|
|
|
emoji = _wallet?.walletInfo?.yatEmojiId ?? '';
|
|
|
|
refreshToken = _wallet?.walletInfo?.yatToken ?? '';
|
|
|
|
reaction((_) => appStore.wallet, _onWalletChange);
|
|
|
|
reaction((_) => emoji, (String emoji) => _onEmojiChange());
|
2021-11-02 09:17:24 +00:00
|
|
|
emojiIncommingSC = StreamController<String>();
|
2021-10-01 15:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AppStore appStore;
|
|
|
|
|
|
|
|
@observable
|
|
|
|
String emoji;
|
|
|
|
|
|
|
|
@observable
|
|
|
|
String refreshToken;
|
|
|
|
|
2021-11-02 09:17:24 +00:00
|
|
|
StreamController<String> emojiIncommingSC;
|
|
|
|
|
|
|
|
Stream<String> get emojiIncommingStream => emojiIncommingSC.stream;
|
|
|
|
|
2021-10-01 15:13:10 +00:00
|
|
|
@observable
|
|
|
|
WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>
|
|
|
|
_wallet;
|
|
|
|
|
|
|
|
@action
|
|
|
|
void _onWalletChange(
|
|
|
|
WalletBase<Balance, TransactionHistoryBase<TransactionInfo>,
|
|
|
|
TransactionInfo>
|
|
|
|
wallet) {
|
|
|
|
this._wallet = wallet;
|
|
|
|
emoji = wallet?.walletInfo?.yatEmojiId ?? '';
|
|
|
|
refreshToken = wallet?.walletInfo?.yatToken ?? '';
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
void _onEmojiChange() {
|
|
|
|
try {
|
|
|
|
final walletInfo = _wallet.walletInfo;
|
|
|
|
|
|
|
|
if (walletInfo == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
walletInfo.yatEid = emoji;
|
|
|
|
walletInfo.yatRefreshToken = refreshToken;
|
|
|
|
|
|
|
|
if (walletInfo.isInBox) {
|
|
|
|
walletInfo.save();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String defineQueryParameters() {
|
|
|
|
String parameters = '';
|
|
|
|
switch (_wallet.type) {
|
|
|
|
case WalletType.monero:
|
|
|
|
final wallet = _wallet as MoneroWallet;
|
|
|
|
final subaddressList = MoneroSubaddressList();
|
|
|
|
var isFirstAddress = true;
|
|
|
|
|
|
|
|
wallet.walletAddresses.accountList.accounts.forEach((account) {
|
|
|
|
subaddressList.update(accountIndex: account.id);
|
|
|
|
subaddressList.subaddresses.forEach((subaddress) {
|
|
|
|
if (!isFirstAddress) {
|
|
|
|
parameters += '%7C';
|
|
|
|
} else {
|
|
|
|
isFirstAddress = !isFirstAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
parameters += subaddress.address.startsWith('4')
|
2021-10-04 13:03:35 +00:00
|
|
|
? YatLink.tags["XMR"].first + '%3D'
|
|
|
|
: YatLink.tags["XMR"].last + '%3D';
|
2021-10-01 15:13:10 +00:00
|
|
|
|
|
|
|
parameters += subaddress.address;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case WalletType.bitcoin:
|
|
|
|
final wallet = _wallet as ElectrumWallet;
|
|
|
|
var isFirstAddress = true;
|
|
|
|
|
|
|
|
wallet.walletAddresses.addresses.forEach((record) {
|
|
|
|
if (!isFirstAddress) {
|
|
|
|
parameters += '%7C';
|
|
|
|
} else {
|
|
|
|
isFirstAddress = !isFirstAddress;
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:03:35 +00:00
|
|
|
parameters += YatLink.tags["BTC"].first + '%3D' + record.address;
|
2021-10-01 15:13:10 +00:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
case WalletType.litecoin:
|
|
|
|
final wallet = _wallet as ElectrumWallet;
|
|
|
|
var isFirstAddress = true;
|
|
|
|
|
|
|
|
wallet.walletAddresses.addresses.forEach((record) {
|
|
|
|
if (!isFirstAddress) {
|
|
|
|
parameters += '%7C';
|
|
|
|
} else {
|
|
|
|
isFirstAddress = !isFirstAddress;
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:03:35 +00:00
|
|
|
parameters += YatLink.tags["LTC"].first + '%3D' + record.address;
|
2021-10-01 15:13:10 +00:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
parameters = '';
|
|
|
|
}
|
|
|
|
return parameters;
|
|
|
|
}
|
|
|
|
}
|