2023-01-25 21:07:44 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
import 'package:bip32/bip32.dart' as bip32;
|
|
|
|
import 'package:bip47/bip47.dart';
|
|
|
|
import 'package:bip47/src/util.dart';
|
|
|
|
import 'package:bitcoindart/bitcoindart.dart' as btc_dart;
|
|
|
|
import 'package:bitcoindart/src/utils/constants/op.dart' as op;
|
|
|
|
import 'package:bitcoindart/src/utils/script.dart' as bscript;
|
|
|
|
import 'package:isar/isar.dart';
|
|
|
|
import 'package:pointycastle/digests/sha256.dart';
|
|
|
|
import 'package:stackwallet/db/main_db.dart';
|
|
|
|
import 'package:stackwallet/electrumx_rpc/electrumx.dart';
|
|
|
|
import 'package:stackwallet/exceptions/wallet/insufficient_balance_exception.dart';
|
|
|
|
import 'package:stackwallet/exceptions/wallet/paynym_send_exception.dart';
|
|
|
|
import 'package:stackwallet/models/isar/models/isar_models.dart';
|
|
|
|
import 'package:stackwallet/services/coins/dogecoin/dogecoin_wallet.dart';
|
|
|
|
import 'package:stackwallet/utilities/bip32_utils.dart';
|
|
|
|
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
|
|
|
import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart';
|
|
|
|
import 'package:stackwallet/utilities/format.dart';
|
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
|
|
|
import 'package:tuple/tuple.dart';
|
|
|
|
|
|
|
|
const kPaynymDerivePath = "m/47'/0'/0'";
|
|
|
|
|
2023-01-25 21:49:57 +00:00
|
|
|
mixin PaynymWalletInterface {
|
2023-01-25 21:07:44 +00:00
|
|
|
// passed in wallet data
|
2023-01-25 21:49:57 +00:00
|
|
|
late final String _walletId;
|
|
|
|
late final String _walletName;
|
|
|
|
late final btc_dart.NetworkType _network;
|
|
|
|
late final Coin _coin;
|
|
|
|
late final MainDB _db;
|
|
|
|
late final ElectrumX _electrumXClient;
|
2023-01-25 21:07:44 +00:00
|
|
|
|
|
|
|
// passed in wallet functions
|
2023-01-25 21:49:57 +00:00
|
|
|
late final Future<List<String>> Function() _getMnemonic;
|
|
|
|
late final Future<int> Function() _getChainHeight;
|
|
|
|
late final Future<String> Function() _getCurrentChangeAddress;
|
2023-01-25 21:07:44 +00:00
|
|
|
late final int Function({
|
|
|
|
required int vSize,
|
|
|
|
required int feeRatePerKB,
|
2023-01-25 21:49:57 +00:00
|
|
|
}) _estimateTxFee;
|
2023-01-25 21:07:44 +00:00
|
|
|
late final Future<Map<String, dynamic>> Function({
|
|
|
|
required String address,
|
|
|
|
required int satoshiAmount,
|
|
|
|
Map<String, dynamic>? args,
|
2023-01-25 21:49:57 +00:00
|
|
|
}) _prepareSend;
|
2023-01-25 21:07:44 +00:00
|
|
|
late final Future<int> Function({
|
|
|
|
required String address,
|
2023-01-25 21:49:57 +00:00
|
|
|
}) _getTxCount;
|
2023-01-25 21:07:44 +00:00
|
|
|
late final Future<Map<String, dynamic>> Function(
|
|
|
|
List<UTXO> utxosToUse,
|
2023-01-25 21:49:57 +00:00
|
|
|
) _fetchBuildTxData;
|
|
|
|
late final Future<void> Function() _refresh;
|
|
|
|
late final Future<void> Function() _checkChangeAddressForTransactions;
|
2023-01-26 22:17:26 +00:00
|
|
|
late final Future<void> Function({
|
|
|
|
required int chain,
|
|
|
|
required String address,
|
|
|
|
required String pubKey,
|
|
|
|
required String wif,
|
|
|
|
required DerivePathType derivePathType,
|
|
|
|
}) _addDerivation;
|
|
|
|
late final Future<void> Function({
|
|
|
|
required int chain,
|
|
|
|
required DerivePathType derivePathType,
|
|
|
|
required Map<String, dynamic> derivationsToAdd,
|
|
|
|
}) _addDerivations;
|
2023-01-25 21:07:44 +00:00
|
|
|
|
|
|
|
// initializer
|
2023-01-25 21:49:57 +00:00
|
|
|
void initPaynymWalletInterface({
|
2023-01-25 21:07:44 +00:00
|
|
|
required String walletId,
|
|
|
|
required String walletName,
|
|
|
|
required btc_dart.NetworkType network,
|
|
|
|
required Coin coin,
|
|
|
|
required MainDB db,
|
|
|
|
required ElectrumX electrumXClient,
|
|
|
|
required Future<List<String>> Function() getMnemonic,
|
|
|
|
required Future<int> Function() getChainHeight,
|
|
|
|
required Future<String> Function() getCurrentChangeAddress,
|
|
|
|
required int Function({
|
|
|
|
required int vSize,
|
|
|
|
required int feeRatePerKB,
|
|
|
|
})
|
|
|
|
estimateTxFee,
|
|
|
|
required Future<Map<String, dynamic>> Function({
|
|
|
|
required String address,
|
|
|
|
required int satoshiAmount,
|
|
|
|
Map<String, dynamic>? args,
|
|
|
|
})
|
|
|
|
prepareSend,
|
|
|
|
required Future<int> Function({
|
|
|
|
required String address,
|
|
|
|
})
|
|
|
|
getTxCount,
|
|
|
|
required Future<Map<String, dynamic>> Function(
|
|
|
|
List<UTXO> utxosToUse,
|
|
|
|
)
|
|
|
|
fetchBuildTxData,
|
|
|
|
required Future<void> Function() refresh,
|
|
|
|
required Future<void> Function() checkChangeAddressForTransactions,
|
2023-01-26 22:17:26 +00:00
|
|
|
required Future<void> Function({
|
|
|
|
required int chain,
|
|
|
|
required String address,
|
|
|
|
required String pubKey,
|
|
|
|
required String wif,
|
|
|
|
required DerivePathType derivePathType,
|
|
|
|
})
|
|
|
|
addDerivation,
|
|
|
|
required Future<void> Function({
|
|
|
|
required int chain,
|
|
|
|
required DerivePathType derivePathType,
|
|
|
|
required Map<String, dynamic> derivationsToAdd,
|
|
|
|
})
|
|
|
|
addDerivations,
|
2023-01-25 21:07:44 +00:00
|
|
|
}) {
|
2023-01-25 21:49:57 +00:00
|
|
|
_walletId = walletId;
|
|
|
|
_walletName = walletName;
|
|
|
|
_network = network;
|
|
|
|
_coin = coin;
|
|
|
|
_db = db;
|
|
|
|
_electrumXClient = electrumXClient;
|
|
|
|
_getMnemonic = getMnemonic;
|
|
|
|
_getChainHeight = getChainHeight;
|
|
|
|
_getCurrentChangeAddress = getCurrentChangeAddress;
|
|
|
|
_estimateTxFee = estimateTxFee;
|
|
|
|
_prepareSend = prepareSend;
|
|
|
|
_getTxCount = getTxCount;
|
|
|
|
_fetchBuildTxData = fetchBuildTxData;
|
|
|
|
_refresh = refresh;
|
|
|
|
_checkChangeAddressForTransactions = checkChangeAddressForTransactions;
|
2023-01-26 22:17:26 +00:00
|
|
|
_addDerivation = addDerivation;
|
|
|
|
_addDerivations = addDerivations;
|
2023-01-25 21:07:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 18:16:38 +00:00
|
|
|
// convenience getter
|
|
|
|
btc_dart.NetworkType get networkType => _network;
|
|
|
|
|
2023-01-26 21:25:53 +00:00
|
|
|
Future<Address> currentReceivingPaynymAddress(PaymentCode sender) async {
|
|
|
|
final address = await _db
|
|
|
|
.getAddresses(_walletId)
|
|
|
|
.filter()
|
2023-01-27 20:43:00 +00:00
|
|
|
.subTypeEqualTo(AddressSubType.paynymReceive)
|
|
|
|
.and()
|
|
|
|
.otherDataEqualTo(sender.toString())
|
2023-01-26 21:25:53 +00:00
|
|
|
.sortByDerivationIndexDesc()
|
|
|
|
.findFirst();
|
|
|
|
|
|
|
|
if (address == null) {
|
|
|
|
final generatedAddress = await _generatePaynymReceivingAddress(sender, 0);
|
|
|
|
await _db.putAddress(generatedAddress);
|
|
|
|
return currentReceivingPaynymAddress(sender);
|
|
|
|
} else {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Address> _generatePaynymReceivingAddress(
|
|
|
|
PaymentCode sender,
|
|
|
|
int index,
|
|
|
|
) async {
|
2023-01-27 20:43:00 +00:00
|
|
|
final myPrivateKey = await deriveReceivingPrivateKey(
|
|
|
|
mnemonic: await _getMnemonic(),
|
|
|
|
index: index,
|
|
|
|
);
|
|
|
|
|
2023-01-26 21:25:53 +00:00
|
|
|
final paymentAddress = PaymentAddress.initWithPrivateKey(
|
|
|
|
myPrivateKey,
|
|
|
|
sender,
|
2023-01-27 20:43:00 +00:00
|
|
|
0,
|
2023-01-26 21:25:53 +00:00
|
|
|
);
|
|
|
|
final pair = paymentAddress.getReceiveAddressKeyPair();
|
2023-01-26 22:17:26 +00:00
|
|
|
final address = await generatePaynymReceivingAddressFromKeyPair(
|
2023-01-26 21:25:53 +00:00
|
|
|
pair: pair,
|
|
|
|
derivationIndex: index,
|
|
|
|
derivePathType: DerivePathType.bip44,
|
|
|
|
fromPaymentCode: sender,
|
|
|
|
);
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> checkCurrentPaynymReceivingAddressForTransactions(
|
|
|
|
PaymentCode sender) async {
|
|
|
|
final address = await currentReceivingPaynymAddress(sender);
|
|
|
|
final txCount = await _getTxCount(address: address.value);
|
|
|
|
if (txCount > 0) {
|
|
|
|
// generate next address and add to db
|
|
|
|
final nextAddress = await _generatePaynymReceivingAddress(
|
|
|
|
sender,
|
|
|
|
address.derivationIndex + 1,
|
|
|
|
);
|
|
|
|
await _db.putAddress(nextAddress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> checkAllCurrentReceivingPaynymAddressesForTransactions() async {
|
|
|
|
final codes = await getAllPaymentCodesFromNotificationTransactions();
|
|
|
|
final List<Future<void>> futures = [];
|
|
|
|
for (final code in codes) {
|
|
|
|
futures.add(checkCurrentPaynymReceivingAddressForTransactions(code));
|
|
|
|
}
|
|
|
|
await Future.wait(futures);
|
|
|
|
}
|
|
|
|
|
2023-01-25 21:07:44 +00:00
|
|
|
// generate bip32 payment code root
|
|
|
|
Future<bip32.BIP32> getRootNode({
|
|
|
|
required List<String> mnemonic,
|
|
|
|
}) async {
|
2023-01-25 21:49:57 +00:00
|
|
|
final root = await Bip32Utils.getBip32Root(mnemonic.join(" "), _network);
|
2023-01-25 21:07:44 +00:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Uint8List> deriveNotificationPrivateKey({
|
|
|
|
required List<String> mnemonic,
|
|
|
|
}) async {
|
|
|
|
final root = await getRootNode(mnemonic: mnemonic);
|
|
|
|
final node = root.derivePath(kPaynymDerivePath).derive(0);
|
|
|
|
return node.privateKey!;
|
|
|
|
}
|
|
|
|
|
2023-01-27 20:43:00 +00:00
|
|
|
Future<Uint8List> deriveReceivingPrivateKey({
|
|
|
|
required List<String> mnemonic,
|
|
|
|
required int index,
|
|
|
|
}) async {
|
|
|
|
final root = await getRootNode(mnemonic: mnemonic);
|
|
|
|
final node = root.derivePath(kPaynymDerivePath).derive(index);
|
|
|
|
return node.privateKey!;
|
|
|
|
}
|
|
|
|
|
2023-01-25 21:07:44 +00:00
|
|
|
/// fetch or generate this wallet's bip47 payment code
|
|
|
|
Future<PaymentCode> getPaymentCode(
|
|
|
|
DerivePathType derivePathType,
|
|
|
|
) async {
|
|
|
|
final address = await getMyNotificationAddress(derivePathType);
|
|
|
|
final paymentCode = PaymentCode.fromPaymentCode(
|
|
|
|
address.otherData!,
|
2023-01-25 21:49:57 +00:00
|
|
|
_network,
|
2023-01-25 21:07:44 +00:00
|
|
|
);
|
|
|
|
return paymentCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Uint8List> signWithNotificationKey(Uint8List data) async {
|
|
|
|
final privateKey =
|
2023-01-25 21:49:57 +00:00
|
|
|
await deriveNotificationPrivateKey(mnemonic: await _getMnemonic());
|
|
|
|
final pair = btc_dart.ECPair.fromPrivateKey(privateKey, network: _network);
|
2023-01-25 21:07:44 +00:00
|
|
|
final signed = pair.sign(SHA256Digest().process(data));
|
|
|
|
return signed;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> signStringWithNotificationKey(String data) async {
|
|
|
|
final bytes =
|
|
|
|
await signWithNotificationKey(Uint8List.fromList(utf8.encode(data)));
|
|
|
|
return Format.uint8listToString(bytes);
|
|
|
|
}
|
|
|
|
|
2023-01-26 18:16:38 +00:00
|
|
|
Future<Map<String, dynamic>> preparePaymentCodeSend(
|
2023-01-25 21:07:44 +00:00
|
|
|
{required PaymentCode paymentCode,
|
|
|
|
required int satoshiAmount,
|
|
|
|
Map<String, dynamic>? args}) async {
|
|
|
|
if (!(await hasConnected(paymentCode.notificationAddressP2PKH()))) {
|
|
|
|
throw PaynymSendException(
|
|
|
|
"No notification transaction sent to $paymentCode");
|
|
|
|
} else {
|
|
|
|
final myPrivateKey =
|
2023-01-25 21:49:57 +00:00
|
|
|
await deriveNotificationPrivateKey(mnemonic: await _getMnemonic());
|
2023-01-25 21:07:44 +00:00
|
|
|
final sendToAddress = await nextUnusedSendAddressFrom(
|
|
|
|
pCode: paymentCode,
|
|
|
|
privateKey: myPrivateKey,
|
|
|
|
);
|
|
|
|
|
2023-01-25 21:49:57 +00:00
|
|
|
return _prepareSend(
|
2023-01-26 18:16:38 +00:00
|
|
|
address: sendToAddress.value,
|
|
|
|
satoshiAmount: satoshiAmount,
|
|
|
|
args: args,
|
|
|
|
);
|
2023-01-25 21:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get the next unused address to send to given the receiver's payment code
|
|
|
|
/// and your own private key
|
|
|
|
Future<Address> nextUnusedSendAddressFrom({
|
|
|
|
required PaymentCode pCode,
|
|
|
|
required Uint8List privateKey,
|
|
|
|
int startIndex = 0,
|
|
|
|
}) async {
|
|
|
|
// https://en.bitcoin.it/wiki/BIP_0047#Path_levels
|
|
|
|
const maxCount = 2147483647;
|
|
|
|
|
|
|
|
for (int i = startIndex; i < maxCount; i++) {
|
2023-01-25 21:49:57 +00:00
|
|
|
final address = await _db
|
|
|
|
.getAddresses(_walletId)
|
2023-01-25 21:07:44 +00:00
|
|
|
.filter()
|
|
|
|
.subTypeEqualTo(AddressSubType.paynymSend)
|
|
|
|
.and()
|
|
|
|
.otherDataEqualTo(pCode.toString())
|
|
|
|
.and()
|
|
|
|
.derivationIndexEqualTo(i)
|
|
|
|
.findFirst();
|
|
|
|
|
|
|
|
if (address != null) {
|
2023-01-25 21:49:57 +00:00
|
|
|
final count = await _getTxCount(address: address.value);
|
2023-01-25 21:07:44 +00:00
|
|
|
// return address if unused, otherwise continue to next index
|
|
|
|
if (count == 0) {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
final pair = PaymentAddress.initWithPrivateKey(
|
|
|
|
privateKey,
|
|
|
|
pCode,
|
|
|
|
i, // index to use
|
|
|
|
).getSendAddressKeyPair();
|
|
|
|
|
|
|
|
// add address to local db
|
|
|
|
final address = generatePaynymSendAddressFromKeyPair(
|
|
|
|
pair: pair,
|
|
|
|
derivationIndex: i,
|
|
|
|
derivePathType: DerivePathType.bip44,
|
|
|
|
toPaymentCode: pCode,
|
|
|
|
);
|
|
|
|
|
2023-01-27 17:20:12 +00:00
|
|
|
final storedAddress = await _db.getAddress(_walletId, address.value);
|
|
|
|
if (storedAddress == null) {
|
|
|
|
await _db.putAddress(address);
|
|
|
|
} else {
|
|
|
|
await _db.updateAddress(storedAddress, address);
|
|
|
|
}
|
2023-01-25 21:49:57 +00:00
|
|
|
final count = await _getTxCount(address: address.value);
|
2023-01-25 21:07:44 +00:00
|
|
|
// return address if unused, otherwise continue to next index
|
|
|
|
if (count == 0) {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw PaynymSendException("Exhausted unused send addresses!");
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Map<String, dynamic>> prepareNotificationTx({
|
|
|
|
required int selectedTxFeeRate,
|
|
|
|
required String targetPaymentCodeString,
|
|
|
|
int additionalOutputs = 0,
|
|
|
|
List<UTXO>? utxos,
|
|
|
|
}) async {
|
|
|
|
const amountToSend = DUST_LIMIT;
|
|
|
|
final List<UTXO> availableOutputs =
|
2023-01-25 21:49:57 +00:00
|
|
|
utxos ?? await _db.getUTXOs(_walletId).findAll();
|
2023-01-25 21:07:44 +00:00
|
|
|
final List<UTXO> spendableOutputs = [];
|
|
|
|
int spendableSatoshiValue = 0;
|
|
|
|
|
|
|
|
// Build list of spendable outputs and totaling their satoshi amount
|
|
|
|
for (var i = 0; i < availableOutputs.length; i++) {
|
|
|
|
if (availableOutputs[i].isBlocked == false &&
|
2023-01-25 21:49:57 +00:00
|
|
|
availableOutputs[i].isConfirmed(
|
|
|
|
await _getChainHeight(), MINIMUM_CONFIRMATIONS) ==
|
2023-01-25 21:07:44 +00:00
|
|
|
true) {
|
|
|
|
spendableOutputs.add(availableOutputs[i]);
|
|
|
|
spendableSatoshiValue += availableOutputs[i].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spendableSatoshiValue < amountToSend) {
|
|
|
|
// insufficient balance
|
|
|
|
throw InsufficientBalanceException(
|
|
|
|
"Spendable balance is less than the minimum required for a notification transaction.");
|
|
|
|
} else if (spendableSatoshiValue == amountToSend) {
|
|
|
|
// insufficient balance due to missing amount to cover fee
|
|
|
|
throw InsufficientBalanceException(
|
|
|
|
"Remaining balance does not cover the network fee.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort spendable by age (oldest first)
|
|
|
|
spendableOutputs.sort((a, b) => b.blockTime!.compareTo(a.blockTime!));
|
|
|
|
|
|
|
|
int satoshisBeingUsed = 0;
|
|
|
|
int outputsBeingUsed = 0;
|
|
|
|
List<UTXO> utxoObjectsToUse = [];
|
|
|
|
|
|
|
|
for (int i = 0;
|
|
|
|
satoshisBeingUsed < amountToSend && i < spendableOutputs.length;
|
|
|
|
i++) {
|
|
|
|
utxoObjectsToUse.add(spendableOutputs[i]);
|
|
|
|
satoshisBeingUsed += spendableOutputs[i].value;
|
|
|
|
outputsBeingUsed += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add additional outputs if required
|
|
|
|
for (int i = 0;
|
|
|
|
i < additionalOutputs && outputsBeingUsed < spendableOutputs.length;
|
|
|
|
i++) {
|
|
|
|
utxoObjectsToUse.add(spendableOutputs[outputsBeingUsed]);
|
|
|
|
satoshisBeingUsed += spendableOutputs[outputsBeingUsed].value;
|
|
|
|
outputsBeingUsed += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// gather required signing data
|
2023-01-25 21:49:57 +00:00
|
|
|
final utxoSigningData = await _fetchBuildTxData(utxoObjectsToUse);
|
2023-01-25 21:07:44 +00:00
|
|
|
|
|
|
|
final int vSizeForNoChange = (await _createNotificationTx(
|
|
|
|
targetPaymentCodeString: targetPaymentCodeString,
|
|
|
|
utxosToUse: utxoObjectsToUse,
|
|
|
|
utxoSigningData: utxoSigningData,
|
|
|
|
change: 0))
|
|
|
|
.item2;
|
|
|
|
|
|
|
|
final int vSizeForWithChange = (await _createNotificationTx(
|
|
|
|
targetPaymentCodeString: targetPaymentCodeString,
|
|
|
|
utxosToUse: utxoObjectsToUse,
|
|
|
|
utxoSigningData: utxoSigningData,
|
|
|
|
change: satoshisBeingUsed - amountToSend))
|
|
|
|
.item2;
|
|
|
|
|
|
|
|
// Assume 2 outputs, for recipient and payment code script
|
2023-01-25 21:49:57 +00:00
|
|
|
int feeForNoChange = _estimateTxFee(
|
2023-01-25 21:07:44 +00:00
|
|
|
vSize: vSizeForNoChange,
|
|
|
|
feeRatePerKB: selectedTxFeeRate,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Assume 3 outputs, for recipient, payment code script, and change
|
2023-01-25 21:49:57 +00:00
|
|
|
int feeForWithChange = _estimateTxFee(
|
2023-01-25 21:07:44 +00:00
|
|
|
vSize: vSizeForWithChange,
|
|
|
|
feeRatePerKB: selectedTxFeeRate,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (feeForNoChange < vSizeForNoChange * 1000) {
|
|
|
|
feeForNoChange = vSizeForNoChange * 1000;
|
|
|
|
}
|
|
|
|
if (feeForWithChange < vSizeForWithChange * 1000) {
|
|
|
|
feeForWithChange = vSizeForWithChange * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (satoshisBeingUsed - amountToSend > feeForNoChange + DUST_LIMIT) {
|
|
|
|
// try to add change output due to "left over" amount being greater than
|
|
|
|
// the estimated fee + the dust limit
|
|
|
|
int changeAmount = satoshisBeingUsed - amountToSend - feeForWithChange;
|
|
|
|
|
|
|
|
// check estimates are correct and build notification tx
|
|
|
|
if (changeAmount >= DUST_LIMIT &&
|
|
|
|
satoshisBeingUsed - amountToSend - changeAmount == feeForWithChange) {
|
|
|
|
final txn = await _createNotificationTx(
|
|
|
|
targetPaymentCodeString: targetPaymentCodeString,
|
|
|
|
utxosToUse: utxoObjectsToUse,
|
|
|
|
utxoSigningData: utxoSigningData,
|
|
|
|
change: changeAmount,
|
|
|
|
);
|
|
|
|
|
|
|
|
int feeBeingPaid = satoshisBeingUsed - amountToSend - changeAmount;
|
|
|
|
|
|
|
|
Map<String, dynamic> transactionObject = {
|
|
|
|
"hex": txn.item1,
|
|
|
|
"recipientPaynym": targetPaymentCodeString,
|
|
|
|
"amount": amountToSend,
|
|
|
|
"fee": feeBeingPaid,
|
|
|
|
"vSize": txn.item2,
|
|
|
|
};
|
|
|
|
return transactionObject;
|
|
|
|
} else {
|
|
|
|
// something broke during fee estimation or the change amount is smaller
|
|
|
|
// than the dust limit. Try without change
|
|
|
|
final txn = await _createNotificationTx(
|
|
|
|
targetPaymentCodeString: targetPaymentCodeString,
|
|
|
|
utxosToUse: utxoObjectsToUse,
|
|
|
|
utxoSigningData: utxoSigningData,
|
|
|
|
change: 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
int feeBeingPaid = satoshisBeingUsed - amountToSend;
|
|
|
|
|
|
|
|
Map<String, dynamic> transactionObject = {
|
|
|
|
"hex": txn.item1,
|
|
|
|
"recipientPaynym": targetPaymentCodeString,
|
|
|
|
"amount": amountToSend,
|
|
|
|
"fee": feeBeingPaid,
|
|
|
|
"vSize": txn.item2,
|
|
|
|
};
|
|
|
|
return transactionObject;
|
|
|
|
}
|
|
|
|
} else if (satoshisBeingUsed - amountToSend >= feeForNoChange) {
|
|
|
|
// since we already checked if we need to add a change output we can just
|
|
|
|
// build without change here
|
|
|
|
final txn = await _createNotificationTx(
|
|
|
|
targetPaymentCodeString: targetPaymentCodeString,
|
|
|
|
utxosToUse: utxoObjectsToUse,
|
|
|
|
utxoSigningData: utxoSigningData,
|
|
|
|
change: 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
int feeBeingPaid = satoshisBeingUsed - amountToSend;
|
|
|
|
|
|
|
|
Map<String, dynamic> transactionObject = {
|
|
|
|
"hex": txn.item1,
|
|
|
|
"recipientPaynym": targetPaymentCodeString,
|
|
|
|
"amount": amountToSend,
|
|
|
|
"fee": feeBeingPaid,
|
|
|
|
"vSize": txn.item2,
|
|
|
|
};
|
|
|
|
return transactionObject;
|
|
|
|
} else {
|
|
|
|
// if we get here we do not have enough funds to cover the tx total so we
|
|
|
|
// check if we have any more available outputs and try again
|
|
|
|
if (spendableOutputs.length > outputsBeingUsed) {
|
|
|
|
return prepareNotificationTx(
|
|
|
|
selectedTxFeeRate: selectedTxFeeRate,
|
|
|
|
targetPaymentCodeString: targetPaymentCodeString,
|
|
|
|
additionalOutputs: additionalOutputs + 1,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
throw InsufficientBalanceException(
|
|
|
|
"Remaining balance does not cover the network fee.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return tuple with string value equal to the raw tx hex and the int value
|
|
|
|
// equal to its vSize
|
|
|
|
Future<Tuple2<String, int>> _createNotificationTx({
|
|
|
|
required String targetPaymentCodeString,
|
|
|
|
required List<UTXO> utxosToUse,
|
|
|
|
required Map<String, dynamic> utxoSigningData,
|
|
|
|
required int change,
|
|
|
|
}) async {
|
|
|
|
final targetPaymentCode =
|
2023-01-25 21:49:57 +00:00
|
|
|
PaymentCode.fromPaymentCode(targetPaymentCodeString, _network);
|
2023-01-25 21:07:44 +00:00
|
|
|
final myCode = await getPaymentCode(DerivePathType.bip44);
|
|
|
|
|
|
|
|
final utxo = utxosToUse.first;
|
|
|
|
final txPoint = utxo.txid.fromHex.toList();
|
|
|
|
final txPointIndex = utxo.vout;
|
|
|
|
|
|
|
|
final rev = Uint8List(txPoint.length + 4);
|
|
|
|
Util.copyBytes(Uint8List.fromList(txPoint), 0, rev, 0, txPoint.length);
|
|
|
|
final buffer = rev.buffer.asByteData();
|
|
|
|
buffer.setUint32(txPoint.length, txPointIndex, Endian.little);
|
|
|
|
|
|
|
|
final myKeyPair = utxoSigningData[utxo.txid]["keyPair"] as btc_dart.ECPair;
|
|
|
|
|
|
|
|
final S = SecretPoint(
|
|
|
|
myKeyPair.privateKey!,
|
|
|
|
targetPaymentCode.notificationPublicKey(),
|
|
|
|
);
|
|
|
|
|
|
|
|
final blindingMask = PaymentCode.getMask(S.ecdhSecret(), rev);
|
|
|
|
|
|
|
|
final blindedPaymentCode = PaymentCode.blind(
|
|
|
|
myCode.getPayload(),
|
|
|
|
blindingMask,
|
|
|
|
);
|
|
|
|
|
|
|
|
final opReturnScript = bscript.compile([
|
|
|
|
(op.OPS["OP_RETURN"] as int),
|
|
|
|
blindedPaymentCode,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// build a notification tx
|
2023-01-25 21:49:57 +00:00
|
|
|
final txb = btc_dart.TransactionBuilder(network: _network);
|
2023-01-25 21:07:44 +00:00
|
|
|
txb.setVersion(1);
|
|
|
|
|
|
|
|
txb.addInput(
|
|
|
|
utxo.txid,
|
|
|
|
txPointIndex,
|
|
|
|
);
|
|
|
|
|
|
|
|
// todo: modify address once segwit support is in our bip47
|
|
|
|
txb.addOutput(targetPaymentCode.notificationAddressP2PKH(), DUST_LIMIT);
|
|
|
|
txb.addOutput(opReturnScript, 0);
|
|
|
|
|
|
|
|
// TODO: add possible change output and mark output as dangerous
|
|
|
|
if (change > 0) {
|
|
|
|
// generate new change address if current change address has been used
|
2023-01-25 21:49:57 +00:00
|
|
|
await _checkChangeAddressForTransactions();
|
|
|
|
final String changeAddress = await _getCurrentChangeAddress();
|
2023-01-25 21:07:44 +00:00
|
|
|
txb.addOutput(changeAddress, change);
|
|
|
|
}
|
|
|
|
|
|
|
|
txb.sign(
|
|
|
|
vin: 0,
|
|
|
|
keyPair: myKeyPair,
|
|
|
|
);
|
|
|
|
|
|
|
|
// sign rest of possible inputs
|
|
|
|
for (var i = 1; i < utxosToUse.length - 1; i++) {
|
|
|
|
final txid = utxosToUse[i].txid;
|
|
|
|
txb.sign(
|
|
|
|
vin: i,
|
|
|
|
keyPair: utxoSigningData[txid]["keyPair"] as btc_dart.ECPair,
|
|
|
|
// witnessValue: utxosToUse[i].value,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
final builtTx = txb.build();
|
|
|
|
|
|
|
|
return Tuple2(builtTx.toHex(), builtTx.virtualSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> broadcastNotificationTx(
|
|
|
|
{required Map<String, dynamic> preparedTx}) async {
|
|
|
|
try {
|
|
|
|
Logging.instance.log("confirmNotificationTx txData: $preparedTx",
|
|
|
|
level: LogLevel.Info);
|
2023-01-25 21:49:57 +00:00
|
|
|
final txHash = await _electrumXClient.broadcastTransaction(
|
2023-01-25 21:07:44 +00:00
|
|
|
rawTx: preparedTx["hex"] as String);
|
|
|
|
Logging.instance.log("Sent txHash: $txHash", level: LogLevel.Info);
|
|
|
|
|
|
|
|
// TODO: only refresh transaction data
|
|
|
|
try {
|
2023-01-25 21:49:57 +00:00
|
|
|
await _refresh();
|
2023-01-25 21:07:44 +00:00
|
|
|
} catch (e) {
|
|
|
|
Logging.instance.log(
|
2023-01-25 21:49:57 +00:00
|
|
|
"refresh() failed in confirmNotificationTx ($_walletName::$_walletId): $e",
|
2023-01-25 21:07:44 +00:00
|
|
|
level: LogLevel.Error,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return txHash;
|
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log("Exception rethrown from confirmSend(): $e\n$s",
|
|
|
|
level: LogLevel.Error);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO optimize
|
|
|
|
Future<bool> hasConnected(String paymentCodeString) async {
|
|
|
|
final myNotificationAddress =
|
2023-01-25 21:49:57 +00:00
|
|
|
await getMyNotificationAddress(DerivePathTypeExt.primaryFor(_coin));
|
2023-01-25 21:07:44 +00:00
|
|
|
|
2023-01-25 21:49:57 +00:00
|
|
|
final txns = await _db
|
|
|
|
.getTransactions(_walletId)
|
2023-01-25 21:07:44 +00:00
|
|
|
.filter()
|
|
|
|
.subTypeEqualTo(TransactionSubType.bip47Notification)
|
|
|
|
.findAll();
|
|
|
|
|
|
|
|
for (final tx in txns) {
|
|
|
|
// quick check that may cause problems?
|
|
|
|
if (tx.address.value?.value == myNotificationAddress.value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
final unBlindedPaymentCode = await unBlindedPaymentCodeFromTransaction(
|
|
|
|
transaction: tx,
|
|
|
|
myNotificationAddress: myNotificationAddress,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (paymentCodeString == unBlindedPaymentCode.toString()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise return no
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<PaymentCode?> unBlindedPaymentCodeFromTransaction({
|
|
|
|
required Transaction transaction,
|
|
|
|
required Address myNotificationAddress,
|
|
|
|
}) async {
|
|
|
|
if (transaction.address.value != null &&
|
|
|
|
transaction.address.value!.value != myNotificationAddress.value) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
final blindedCode =
|
|
|
|
transaction.outputs.elementAt(1).scriptPubKeyAsm!.split(" ")[1];
|
|
|
|
|
|
|
|
final designatedInput = transaction.inputs.first;
|
|
|
|
|
|
|
|
final txPoint = designatedInput.txid.fromHex.toList();
|
|
|
|
final txPointIndex = designatedInput.vout;
|
|
|
|
|
|
|
|
final rev = Uint8List(txPoint.length + 4);
|
|
|
|
Util.copyBytes(Uint8List.fromList(txPoint), 0, rev, 0, txPoint.length);
|
|
|
|
final buffer = rev.buffer.asByteData();
|
|
|
|
buffer.setUint32(txPoint.length, txPointIndex, Endian.little);
|
|
|
|
|
|
|
|
final pubKey = designatedInput.scriptSigAsm!.split(" ")[1].fromHex;
|
|
|
|
|
|
|
|
final myPrivateKey =
|
2023-01-25 21:49:57 +00:00
|
|
|
await deriveNotificationPrivateKey(mnemonic: await _getMnemonic());
|
2023-01-25 21:07:44 +00:00
|
|
|
|
|
|
|
final S = SecretPoint(myPrivateKey, pubKey);
|
|
|
|
|
|
|
|
final mask = PaymentCode.getMask(S.ecdhSecret(), rev);
|
|
|
|
|
|
|
|
final unBlindedPayload = PaymentCode.blind(blindedCode.fromHex, mask);
|
|
|
|
|
|
|
|
final unBlindedPaymentCode =
|
|
|
|
PaymentCode.initFromPayload(unBlindedPayload);
|
|
|
|
|
|
|
|
return unBlindedPaymentCode;
|
|
|
|
} catch (e) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"unBlindedPaymentCodeFromTransaction() failed: $e",
|
|
|
|
level: LogLevel.Warning,
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<PaymentCode>>
|
|
|
|
getAllPaymentCodesFromNotificationTransactions() async {
|
|
|
|
final myAddress =
|
2023-01-25 21:49:57 +00:00
|
|
|
await getMyNotificationAddress(DerivePathTypeExt.primaryFor(_coin));
|
|
|
|
final txns = await _db
|
|
|
|
.getTransactions(_walletId)
|
2023-01-25 21:07:44 +00:00
|
|
|
.filter()
|
|
|
|
.subTypeEqualTo(TransactionSubType.bip47Notification)
|
|
|
|
.findAll();
|
|
|
|
|
|
|
|
List<PaymentCode> unBlindedList = [];
|
|
|
|
|
|
|
|
for (final tx in txns) {
|
|
|
|
final unBlinded = await unBlindedPaymentCodeFromTransaction(
|
|
|
|
transaction: tx,
|
|
|
|
myNotificationAddress: myAddress,
|
|
|
|
);
|
2023-01-27 19:10:35 +00:00
|
|
|
if (unBlinded != null &&
|
|
|
|
unBlindedList
|
|
|
|
.where((e) => e.toString() == unBlinded.toString())
|
|
|
|
.isEmpty) {
|
2023-01-25 21:07:44 +00:00
|
|
|
unBlindedList.add(unBlinded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return unBlindedList;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> restoreHistoryWith(
|
|
|
|
PaymentCode other,
|
|
|
|
int maxUnusedAddressGap,
|
|
|
|
int maxNumberOfIndexesToCheck,
|
|
|
|
) async {
|
|
|
|
// https://en.bitcoin.it/wiki/BIP_0047#Path_levels
|
|
|
|
const maxCount = 2147483647;
|
|
|
|
assert(maxNumberOfIndexesToCheck < maxCount);
|
|
|
|
|
2023-01-27 20:43:00 +00:00
|
|
|
final mnemonic = await _getMnemonic();
|
|
|
|
|
|
|
|
final mySendPrivateKey =
|
|
|
|
await deriveNotificationPrivateKey(mnemonic: mnemonic);
|
|
|
|
final receivingNode =
|
|
|
|
(await getRootNode(mnemonic: mnemonic)).derivePath(kPaynymDerivePath);
|
2023-01-25 21:07:44 +00:00
|
|
|
|
|
|
|
List<Address> addresses = [];
|
|
|
|
int receivingGapCounter = 0;
|
|
|
|
int outgoingGapCounter = 0;
|
|
|
|
|
|
|
|
for (int i = 0;
|
|
|
|
i < maxNumberOfIndexesToCheck &&
|
|
|
|
(receivingGapCounter < maxUnusedAddressGap ||
|
|
|
|
outgoingGapCounter < maxUnusedAddressGap);
|
|
|
|
i++) {
|
2023-01-26 22:17:26 +00:00
|
|
|
if (outgoingGapCounter < maxUnusedAddressGap) {
|
2023-01-27 20:43:00 +00:00
|
|
|
final paymentAddressSending = PaymentAddress.initWithPrivateKey(
|
|
|
|
mySendPrivateKey,
|
|
|
|
other,
|
|
|
|
i, // index to use
|
|
|
|
);
|
|
|
|
final pair = paymentAddressSending.getSendAddressKeyPair();
|
2023-01-25 21:07:44 +00:00
|
|
|
final address = generatePaynymSendAddressFromKeyPair(
|
|
|
|
pair: pair,
|
|
|
|
derivationIndex: i,
|
|
|
|
derivePathType: DerivePathType.bip44,
|
|
|
|
toPaymentCode: other,
|
|
|
|
);
|
|
|
|
addresses.add(address);
|
|
|
|
|
2023-01-25 21:49:57 +00:00
|
|
|
final count = await _getTxCount(address: address.value);
|
2023-01-25 21:07:44 +00:00
|
|
|
|
|
|
|
if (count > 0) {
|
2023-01-26 22:17:26 +00:00
|
|
|
outgoingGapCounter = 0;
|
2023-01-27 20:43:00 +00:00
|
|
|
} else {
|
|
|
|
outgoingGapCounter++;
|
2023-01-25 21:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 22:17:26 +00:00
|
|
|
if (receivingGapCounter < maxUnusedAddressGap) {
|
2023-01-27 20:43:00 +00:00
|
|
|
final myReceivingPrivateKey = receivingNode.derive(i).privateKey!;
|
|
|
|
final paymentAddressReceiving = PaymentAddress.initWithPrivateKey(
|
|
|
|
myReceivingPrivateKey,
|
|
|
|
other,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
final pair = paymentAddressReceiving.getReceiveAddressKeyPair();
|
2023-01-26 22:17:26 +00:00
|
|
|
final address = await generatePaynymReceivingAddressFromKeyPair(
|
2023-01-25 21:07:44 +00:00
|
|
|
pair: pair,
|
|
|
|
derivationIndex: i,
|
|
|
|
derivePathType: DerivePathType.bip44,
|
|
|
|
fromPaymentCode: other,
|
|
|
|
);
|
|
|
|
addresses.add(address);
|
|
|
|
|
2023-01-25 21:49:57 +00:00
|
|
|
final count = await _getTxCount(address: address.value);
|
2023-01-25 21:07:44 +00:00
|
|
|
|
|
|
|
if (count > 0) {
|
2023-01-26 22:17:26 +00:00
|
|
|
receivingGapCounter = 0;
|
2023-01-27 20:43:00 +00:00
|
|
|
} else {
|
|
|
|
receivingGapCounter++;
|
2023-01-25 21:07:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-27 20:43:00 +00:00
|
|
|
await _db.updateOrPutAddresses(addresses);
|
2023-01-25 21:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Address generatePaynymSendAddressFromKeyPair({
|
|
|
|
required btc_dart.ECPair pair,
|
|
|
|
required int derivationIndex,
|
|
|
|
required DerivePathType derivePathType,
|
|
|
|
required PaymentCode toPaymentCode,
|
|
|
|
}) {
|
|
|
|
final data = btc_dart.PaymentData(pubkey: pair.publicKey);
|
|
|
|
|
|
|
|
String addressString;
|
|
|
|
switch (derivePathType) {
|
|
|
|
case DerivePathType.bip44:
|
|
|
|
addressString =
|
2023-01-25 21:49:57 +00:00
|
|
|
btc_dart.P2PKH(data: data, network: _network).data.address!;
|
2023-01-25 21:07:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// The following doesn't apply currently
|
|
|
|
// case DerivePathType.bip49:
|
|
|
|
// addressString = btc_dart
|
|
|
|
// .P2SH(
|
|
|
|
// data: btc_dart.PaymentData(
|
|
|
|
// redeem: btc_dart
|
|
|
|
// .P2WPKH(
|
|
|
|
// data: data,
|
|
|
|
// network: network,
|
|
|
|
// )
|
|
|
|
// .data),
|
|
|
|
// network: network,
|
|
|
|
// )
|
|
|
|
// .data
|
|
|
|
// .address!;
|
|
|
|
// break;
|
|
|
|
//
|
|
|
|
// case DerivePathType.bip84:
|
|
|
|
// addressString = btc_dart
|
|
|
|
// .P2WPKH(
|
|
|
|
// network: network,
|
|
|
|
// data: data,
|
|
|
|
// )
|
|
|
|
// .data
|
|
|
|
// .address!;
|
|
|
|
// break;
|
|
|
|
default:
|
|
|
|
throw UnimplementedError("segwit paynyms not implemented yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
final address = Address(
|
2023-01-25 21:49:57 +00:00
|
|
|
walletId: _walletId,
|
2023-01-25 21:07:44 +00:00
|
|
|
value: addressString,
|
|
|
|
publicKey: pair.publicKey,
|
|
|
|
derivationIndex: derivationIndex,
|
|
|
|
type: AddressType.nonWallet,
|
|
|
|
subType: AddressSubType.paynymSend,
|
|
|
|
otherData: toPaymentCode.toString(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2023-01-26 22:17:26 +00:00
|
|
|
Future<Address> generatePaynymReceivingAddressFromKeyPair({
|
2023-01-25 21:07:44 +00:00
|
|
|
required btc_dart.ECPair pair,
|
|
|
|
required int derivationIndex,
|
|
|
|
required DerivePathType derivePathType,
|
|
|
|
required PaymentCode fromPaymentCode,
|
2023-01-26 22:17:26 +00:00
|
|
|
}) async {
|
2023-01-25 21:07:44 +00:00
|
|
|
final data = btc_dart.PaymentData(pubkey: pair.publicKey);
|
|
|
|
|
|
|
|
String addressString;
|
|
|
|
AddressType addrType;
|
|
|
|
switch (derivePathType) {
|
|
|
|
case DerivePathType.bip44:
|
|
|
|
addressString = btc_dart
|
|
|
|
.P2PKH(
|
|
|
|
data: data,
|
2023-01-25 21:49:57 +00:00
|
|
|
network: _network,
|
2023-01-25 21:07:44 +00:00
|
|
|
)
|
|
|
|
.data
|
|
|
|
.address!;
|
|
|
|
addrType = AddressType.p2pkh;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// The following doesn't apply currently
|
|
|
|
// case DerivePathType.bip49:
|
|
|
|
// addressString = btc_dart
|
|
|
|
// .P2SH(
|
|
|
|
// data: btc_dart.PaymentData(
|
|
|
|
// redeem: btc_dart
|
|
|
|
// .P2WPKH(
|
|
|
|
// data: data,
|
|
|
|
// network: network,
|
|
|
|
// )
|
|
|
|
// .data),
|
|
|
|
// network: network,
|
|
|
|
// )
|
|
|
|
// .data
|
|
|
|
// .address!;
|
|
|
|
// addrType = AddressType.p2sh;
|
|
|
|
// break;
|
|
|
|
//
|
|
|
|
// case DerivePathType.bip84:
|
|
|
|
// addressString = btc_dart
|
|
|
|
// .P2WPKH(
|
|
|
|
// network: network,
|
|
|
|
// data: data,
|
|
|
|
// )
|
|
|
|
// .data
|
|
|
|
// .address!;
|
|
|
|
// addrType = AddressType.p2wpkh;
|
|
|
|
// break;
|
|
|
|
default:
|
|
|
|
throw UnimplementedError("segwit paynyms not implemented yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
final address = Address(
|
2023-01-25 21:49:57 +00:00
|
|
|
walletId: _walletId,
|
2023-01-25 21:07:44 +00:00
|
|
|
value: addressString,
|
|
|
|
publicKey: pair.publicKey,
|
|
|
|
derivationIndex: derivationIndex,
|
|
|
|
type: addrType,
|
|
|
|
subType: AddressSubType.paynymReceive,
|
|
|
|
otherData: fromPaymentCode.toString(),
|
|
|
|
);
|
|
|
|
|
2023-01-26 22:17:26 +00:00
|
|
|
final myCode = await getPaymentCode(DerivePathType.bip44);
|
|
|
|
|
|
|
|
final bip32NetworkType = bip32.NetworkType(
|
|
|
|
wif: _network.wif,
|
|
|
|
bip32: bip32.Bip32Type(
|
|
|
|
public: _network.bip32.public,
|
|
|
|
private: _network.bip32.private,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
final bip32.BIP32 node = bip32.BIP32.fromPrivateKey(
|
|
|
|
pair.privateKey!,
|
|
|
|
myCode.getChain(),
|
|
|
|
bip32NetworkType,
|
|
|
|
);
|
|
|
|
|
|
|
|
await _addDerivation(
|
|
|
|
chain: 0,
|
|
|
|
address: address.value,
|
|
|
|
derivePathType: DerivePathType.bip44,
|
|
|
|
pubKey: Format.uint8listToString(node.publicKey),
|
|
|
|
wif: node.toWIF(),
|
|
|
|
);
|
|
|
|
|
2023-01-25 21:07:44 +00:00
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Address> getMyNotificationAddress(
|
|
|
|
DerivePathType derivePathType,
|
|
|
|
) async {
|
|
|
|
// TODO: fix when segwit is here
|
|
|
|
derivePathType = DerivePathType.bip44;
|
|
|
|
|
|
|
|
AddressType type;
|
|
|
|
switch (derivePathType) {
|
|
|
|
case DerivePathType.bip44:
|
|
|
|
type = AddressType.p2pkh;
|
|
|
|
break;
|
|
|
|
case DerivePathType.bip49:
|
|
|
|
type = AddressType.p2sh;
|
|
|
|
break;
|
|
|
|
case DerivePathType.bip84:
|
|
|
|
type = AddressType.p2wpkh;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-25 21:49:57 +00:00
|
|
|
final storedAddress = await _db
|
|
|
|
.getAddresses(_walletId)
|
2023-01-25 21:07:44 +00:00
|
|
|
.filter()
|
|
|
|
.subTypeEqualTo(AddressSubType.paynymNotification)
|
|
|
|
.and()
|
|
|
|
.typeEqualTo(type)
|
|
|
|
.and()
|
|
|
|
.not()
|
|
|
|
.typeEqualTo(AddressType.nonWallet)
|
|
|
|
.findFirst();
|
|
|
|
|
|
|
|
if (storedAddress != null) {
|
|
|
|
return storedAddress;
|
|
|
|
} else {
|
2023-01-25 21:49:57 +00:00
|
|
|
final root = await getRootNode(mnemonic: await _getMnemonic());
|
2023-01-25 21:07:44 +00:00
|
|
|
final node = root.derivePath(kPaynymDerivePath);
|
|
|
|
final paymentCode = PaymentCode.initFromPubKey(
|
|
|
|
node.publicKey,
|
|
|
|
node.chainCode,
|
2023-01-25 21:49:57 +00:00
|
|
|
_network,
|
2023-01-25 21:07:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
String addressString;
|
|
|
|
final data =
|
|
|
|
btc_dart.PaymentData(pubkey: paymentCode.notificationPublicKey());
|
|
|
|
switch (derivePathType) {
|
|
|
|
case DerivePathType.bip44:
|
|
|
|
addressString = btc_dart
|
|
|
|
.P2PKH(
|
|
|
|
data: data,
|
2023-01-25 21:49:57 +00:00
|
|
|
network: _network,
|
2023-01-25 21:07:44 +00:00
|
|
|
)
|
|
|
|
.data
|
|
|
|
.address!;
|
|
|
|
break;
|
|
|
|
// case DerivePathType.bip49:
|
|
|
|
// addressString = btc_dart
|
|
|
|
// .P2SH(
|
|
|
|
// data: btc_dart.PaymentData(
|
|
|
|
// redeem: btc_dart
|
|
|
|
// .P2WPKH(
|
|
|
|
// data: data,
|
|
|
|
// network: network,
|
|
|
|
// )
|
|
|
|
// .data),
|
|
|
|
// network: network,
|
|
|
|
// )
|
|
|
|
// .data
|
|
|
|
// .address!;
|
|
|
|
// break;
|
|
|
|
// case DerivePathType.bip84:
|
|
|
|
// addressString = btc_dart
|
|
|
|
// .P2WPKH(
|
|
|
|
// network: network,
|
|
|
|
// data: data,
|
|
|
|
// )
|
|
|
|
// .data
|
|
|
|
// .address!;
|
|
|
|
// break;
|
|
|
|
default:
|
|
|
|
throw UnimplementedError("segwit paynyms not implemented yet");
|
|
|
|
}
|
|
|
|
|
|
|
|
final address = Address(
|
2023-01-25 21:49:57 +00:00
|
|
|
walletId: _walletId,
|
2023-01-25 21:07:44 +00:00
|
|
|
value: addressString,
|
|
|
|
publicKey: paymentCode.getPubKey(),
|
|
|
|
derivationIndex: 0,
|
|
|
|
type: type,
|
|
|
|
subType: AddressSubType.paynymNotification,
|
|
|
|
otherData: paymentCode.toString(),
|
|
|
|
);
|
|
|
|
|
2023-01-27 16:22:55 +00:00
|
|
|
await _addDerivation(
|
|
|
|
chain: 0,
|
|
|
|
address: address.value,
|
|
|
|
derivePathType: DerivePathType.bip44,
|
|
|
|
pubKey: Format.uint8listToString(node.publicKey),
|
|
|
|
wif: node.toWIF(),
|
|
|
|
);
|
|
|
|
|
2023-01-25 21:49:57 +00:00
|
|
|
await _db.putAddress(address);
|
2023-01-25 21:07:44 +00:00
|
|
|
return address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|