stack_wallet/lib/wallets/crypto_currency/coins/tezos.dart

222 lines
6 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'dart:typed_data';
import 'package:bip39/bip39.dart' as bip39;
import 'package:coinlib_flutter/coinlib_flutter.dart';
2024-05-27 23:56:22 +00:00
import 'package:tezart/src/crypto/crypto.dart';
import 'package:tezart/tezart.dart';
import '../../../models/isar/models/blockchain_data/address.dart';
import '../../../models/node_model.dart';
import '../../../utilities/default_nodes.dart';
import '../../../utilities/enums/derive_path_type_enum.dart';
import '../crypto_currency.dart';
import '../intermediate/bip39_currency.dart';
2023-11-20 16:37:28 +00:00
class Tezos extends Bip39Currency {
Tezos(super.network) {
2024-05-15 21:20:45 +00:00
_idMain = "tezos";
_uriScheme = "tezos";
2023-11-20 16:37:28 +00:00
switch (network) {
case CryptoCurrencyNetwork.main:
2024-05-15 21:20:45 +00:00
_id = _idMain;
_name = "Tezos";
_ticker = "XTZ";
2023-11-20 16:37:28 +00:00
default:
throw Exception("Unsupported network: $network");
}
}
2024-05-15 21:20:45 +00:00
late final String _id;
@override
String get identifier => _id;
late final String _idMain;
@override
String get mainNetId => _idMain;
late final String _name;
@override
String get prettyName => _name;
late final String _uriScheme;
@override
String get uriScheme => _uriScheme;
late final String _ticker;
@override
String get ticker => _ticker;
// ===========================================================================
// =========== Public ========================================================
static DerivationPath get standardDerivationPath =>
2023-11-23 00:21:55 +00:00
DerivationPath()..value = "m/44'/1729'/0'/0'";
static List<DerivationPath> get possibleDerivationPaths => [
2023-11-23 00:21:55 +00:00
standardDerivationPath,
DerivationPath()..value = "",
DerivationPath()..value = "m/44'/1729'/0'/0'/0'",
DerivationPath()..value = "m/44'/1729'/0'/0/0",
];
static Keystore mnemonicToKeyStore({
required String mnemonic,
String mnemonicPassphrase = "",
String derivationPath = "",
}) {
if (derivationPath.isEmpty) {
return Keystore.fromMnemonic(mnemonic, password: mnemonicPassphrase);
}
final pathArray = _derivationPathToArray(derivationPath);
final seed = bip39.mnemonicToSeed(mnemonic, passphrase: mnemonicPassphrase);
({Uint8List privateKey, Uint8List chainCode}) node = _deriveRootNode(seed);
for (final index in pathArray) {
node = _deriveChildNode(node, index);
}
final encoded = encodeWithPrefix(
prefix: Prefixes.edsk2,
bytes: node.privateKey,
);
return Keystore.fromSeed(encoded);
}
// ===========================================================================
// =========== Overrides =====================================================
2023-11-20 16:37:28 +00:00
@override
2023-11-22 18:30:09 +00:00
String get genesisHash => throw UnimplementedError(
"Not used in tezos at the moment",
);
2023-11-20 16:37:28 +00:00
@override
2023-11-20 19:55:22 +00:00
int get minConfirms => 1;
2023-11-20 16:37:28 +00:00
2024-04-25 19:03:28 +00:00
@override
bool get torSupport => true;
2023-11-20 16:37:28 +00:00
@override
bool validateAddress(String address) {
2023-11-20 19:55:22 +00:00
return RegExp(r"^tz[1-9A-HJ-NP-Za-km-z]{34}$").hasMatch(address);
}
@override
NodeModel get defaultNode {
switch (network) {
case CryptoCurrencyNetwork.main:
return NodeModel(
2024-05-15 21:20:45 +00:00
// TODO: ?Change this to stack wallet one?
2023-11-20 19:55:22 +00:00
host: "https://mainnet.api.tez.ie",
port: 443,
name: DefaultNodes.defaultName,
2024-05-15 21:20:45 +00:00
id: DefaultNodes.buildId(this),
2023-11-20 19:55:22 +00:00
useSSL: true,
enabled: true,
2024-05-15 21:20:45 +00:00
coinName: identifier,
2023-11-20 19:55:22 +00:00
isFailover: true,
isDown: false,
);
default:
throw UnimplementedError();
}
2023-11-20 16:37:28 +00:00
}
// ===========================================================================
// =========== Private =======================================================
static ({Uint8List privateKey, Uint8List chainCode}) _deriveRootNode(
2024-05-27 23:56:22 +00:00
Uint8List seed,
) {
return _deriveNode(seed, Uint8List.fromList(utf8.encode("ed25519 seed")));
}
static ({Uint8List privateKey, Uint8List chainCode}) _deriveNode(
2024-05-27 23:56:22 +00:00
Uint8List msg,
Uint8List key,
) {
final hMac = hmacSha512(key, msg);
final privateKey = hMac.sublist(0, 32);
final chainCode = hMac.sublist(32);
return (privateKey: privateKey, chainCode: chainCode);
}
static ({Uint8List privateKey, Uint8List chainCode}) _deriveChildNode(
2024-05-27 23:56:22 +00:00
({Uint8List privateKey, Uint8List chainCode}) node,
int index,
) {
final Uint8List indexBuf = Uint8List(4);
ByteData.view(indexBuf.buffer).setUint32(0, index, Endian.big);
2024-05-27 23:56:22 +00:00
final Uint8List message = Uint8List.fromList([
Uint8List(1)[0],
...node.privateKey,
...indexBuf,
]);
return _deriveNode(message, Uint8List.fromList(node.chainCode));
}
static List<int> _derivationPathToArray(String derivationPath) {
if (derivationPath.isEmpty) {
return [];
}
derivationPath = derivationPath.replaceAll('m/', '').replaceAll("'", 'h');
return derivationPath.split('/').map((level) {
if (level.endsWith("h")) {
level = level.substring(0, level.length - 1);
}
final int levelNumber = int.parse(level);
if (levelNumber >= 0x80000000) {
throw ArgumentError('Invalid derivation path. Out of bound.');
}
return levelNumber + 0x80000000;
}).toList();
}
2024-05-15 21:20:45 +00:00
@override
int get defaultSeedPhraseLength => 24;
@override
2024-05-15 21:20:45 +00:00
int get fractionDigits => 6;
@override
bool get hasBuySupport => false;
@override
bool get hasMnemonicPassphraseSupport => false;
@override
2024-05-15 21:20:45 +00:00
List<int> get possibleMnemonicLengths => [defaultSeedPhraseLength, 12];
@override
AddressType get defaultAddressType => AddressType.tezos;
2024-05-15 21:20:45 +00:00
@override
BigInt get satsPerCoin => BigInt.from(1000000);
@override
int get targetBlockTimeSeconds => 60;
@override
DerivePathType get defaultDerivePathType =>
2024-05-15 21:20:45 +00:00
throw UnsupportedError("Is this even used?");
@override
Uri defaultBlockExplorer(String txid) {
switch (network) {
case CryptoCurrencyNetwork.main:
return Uri.parse("https://tzstats.com/$txid");
default:
throw Exception(
"Unsupported network for defaultBlockExplorer(): $network",
);
}
}
2023-11-20 16:37:28 +00:00
}