stack_wallet/lib/services/mixins/paynym_wallet_interface.dart

1286 lines
40 KiB
Dart
Raw Normal View History

2023-01-25 21:07:44 +00:00
import 'dart:convert';
2023-01-28 00:18:58 +00:00
import 'dart:math';
2023-01-25 21:07:44 +00:00
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/utilities/bip32_utils.dart';
import 'package:stackwallet/utilities/bip47_utils.dart';
2023-01-25 21:07:44 +00:00
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart';
2023-01-28 00:18:58 +00:00
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
2023-01-25 21:07:44 +00:00
import 'package:stackwallet/utilities/format.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:tuple/tuple.dart';
const kPaynymDerivePath = "m/47'/0'/0'";
mixin PaynymWalletInterface {
2023-01-25 21:07:44 +00:00
// passed in wallet data
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-28 00:18:58 +00:00
late final SecureStorageInterface _secureStorage;
2023-01-30 14:28:54 +00:00
late final int _dustLimitP2PKH;
late final int _minConfirms;
2023-01-25 21:07:44 +00:00
// passed in wallet functions
late final Future<String?> Function() _getMnemonicString;
late final Future<String?> Function() _getMnemonicPassphrase;
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,
}) _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,
}) _prepareSend;
2023-01-25 21:07:44 +00:00
late final Future<int> Function({
required String address,
}) _getTxCount;
2023-01-25 21:07:44 +00:00
late final Future<Map<String, dynamic>> Function(
List<UTXO> utxosToUse,
) _fetchBuildTxData;
late final Future<void> Function() _refresh;
late final Future<void> Function() _checkChangeAddressForTransactions;
late final Future<void> Function({
required int chain,
required String address,
required String pubKey,
required String wif,
required DerivePathType derivePathType,
}) _addDerivation;
2023-01-25 21:07:44 +00:00
// initializer
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,
2023-01-28 00:18:58 +00:00
required SecureStorageInterface secureStorage,
2023-01-30 14:28:54 +00:00
required int dustLimitP2PKH,
required int minConfirms,
required Future<String?> Function() getMnemonicString,
required Future<String?> Function() getMnemonicPassphrase,
2023-01-25 21:07:44 +00:00
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,
required Future<void> Function({
required int chain,
required String address,
required String pubKey,
required String wif,
required DerivePathType derivePathType,
})
addDerivation,
2023-01-25 21:07:44 +00:00
}) {
_walletId = walletId;
_walletName = walletName;
_network = network;
_coin = coin;
_db = db;
_electrumXClient = electrumXClient;
2023-01-28 00:18:58 +00:00
_secureStorage = secureStorage;
2023-01-30 14:28:54 +00:00
_dustLimitP2PKH = dustLimitP2PKH;
_minConfirms = minConfirms;
_getMnemonicString = getMnemonicString;
_getMnemonicPassphrase = getMnemonicPassphrase;
_getChainHeight = getChainHeight;
_getCurrentChangeAddress = getCurrentChangeAddress;
_estimateTxFee = estimateTxFee;
_prepareSend = prepareSend;
_getTxCount = getTxCount;
_fetchBuildTxData = fetchBuildTxData;
_refresh = refresh;
_checkChangeAddressForTransactions = checkChangeAddressForTransactions;
_addDerivation = addDerivation;
2023-01-25 21:07:44 +00:00
}
2023-01-26 18:16:38 +00:00
// convenience getter
btc_dart.NetworkType get networkType => _network;
Future<Address> currentReceivingPaynymAddress(PaymentCode sender) async {
2023-02-01 22:46:49 +00:00
final keys = await lookupKey(sender.toString());
final address = await _db
.getAddresses(_walletId)
.filter()
2023-01-27 20:43:00 +00:00
.subTypeEqualTo(AddressSubType.paynymReceive)
.and()
2023-02-01 22:46:49 +00:00
.anyOf<String, Address>(keys, (q, String e) => q.otherDataEqualTo(e))
.sortByDerivationIndexDesc()
.findFirst();
if (address == null) {
final generatedAddress = await _generatePaynymReceivingAddress(sender, 0);
2023-02-01 17:23:09 +00:00
final existing = await _db
.getAddresses(_walletId)
.filter()
.valueEqualTo(generatedAddress.value)
.findFirst();
if (existing == null) {
// Add that new address
await _db.putAddress(generatedAddress);
} else {
// we need to update the address
await _db.updateAddress(existing, 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 _getMnemonicString())!,
mnemonicPassphrase: (await _getMnemonicPassphrase())!,
2023-01-27 20:43:00 +00:00
index: index,
);
final paymentAddress = PaymentAddress.initWithPrivateKey(
myPrivateKey,
sender,
2023-01-27 20:43:00 +00:00
0,
);
final pair = paymentAddress.getReceiveAddressKeyPair();
final address = await generatePaynymReceivingAddressFromKeyPair(
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,
);
2023-02-01 17:23:09 +00:00
final existing = await _db
.getAddresses(_walletId)
.filter()
.valueEqualTo(nextAddress.value)
.findFirst();
if (existing == null) {
// Add that new address
await _db.putAddress(nextAddress);
} else {
// we need to update the address
await _db.updateAddress(existing, nextAddress);
}
// keep checking until address with no tx history is set as current
await checkCurrentPaynymReceivingAddressForTransactions(sender);
}
}
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 String mnemonic,
required String mnemonicPassphrase,
2023-01-25 21:07:44 +00:00
}) async {
final root = await Bip32Utils.getBip32Root(
mnemonic,
mnemonicPassphrase,
_network,
);
2023-01-25 21:07:44 +00:00
return root;
}
Future<Uint8List> deriveNotificationPrivateKey({
required String mnemonic,
required String mnemonicPassphrase,
2023-01-25 21:07:44 +00:00
}) async {
final root = await _getRootNode(
mnemonic: mnemonic,
mnemonicPassphrase: mnemonicPassphrase,
);
2023-01-25 21:07:44 +00:00
final node = root.derivePath(kPaynymDerivePath).derive(0);
return node.privateKey!;
}
2023-01-27 20:43:00 +00:00
Future<Uint8List> deriveReceivingPrivateKey({
required String mnemonic,
required String mnemonicPassphrase,
2023-01-27 20:43:00 +00:00
required int index,
}) async {
final root = await _getRootNode(
mnemonic: mnemonic,
mnemonicPassphrase: mnemonicPassphrase,
);
2023-01-27 20:43:00 +00:00
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);
2023-01-28 00:18:58 +00:00
final pCodeString = await paymentCodeStringByKey(address.otherData!);
2023-01-25 21:07:44 +00:00
final paymentCode = PaymentCode.fromPaymentCode(
2023-01-28 00:18:58 +00:00
pCodeString!,
_network,
2023-01-25 21:07:44 +00:00
);
return paymentCode;
}
Future<Uint8List> signWithNotificationKey(Uint8List data) async {
final privateKey = await deriveNotificationPrivateKey(
mnemonic: (await _getMnemonicString())!,
mnemonicPassphrase: (await _getMnemonicPassphrase())!,
);
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 {
2023-02-10 17:51:54 +00:00
if (!(await hasConnected(paymentCode.toString()))) {
2023-01-25 21:07:44 +00:00
throw PaynymSendException(
"No notification transaction sent to $paymentCode");
} else {
final myPrivateKey = await deriveNotificationPrivateKey(
mnemonic: (await _getMnemonicString())!,
mnemonicPassphrase: (await _getMnemonicPassphrase())!,
);
2023-01-25 21:07:44 +00:00
final sendToAddress = await nextUnusedSendAddressFrom(
pCode: paymentCode,
privateKey: myPrivateKey,
);
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-02-01 22:46:49 +00:00
final keys = await lookupKey(pCode.toString());
final address = await _db
.getAddresses(_walletId)
2023-01-25 21:07:44 +00:00
.filter()
.subTypeEqualTo(AddressSubType.paynymSend)
.and()
2023-02-01 22:46:49 +00:00
.anyOf<String, Address>(keys, (q, String e) => q.otherDataEqualTo(e))
2023-01-25 21:07:44 +00:00
.and()
.derivationIndexEqualTo(i)
.findFirst();
if (address != null) {
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
2023-01-28 00:18:58 +00:00
final address = await generatePaynymSendAddressFromKeyPair(
2023-01-25 21:07:44 +00:00
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);
}
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 {
2023-01-30 14:28:54 +00:00
final amountToSend = _dustLimitP2PKH;
2023-01-25 21:07:44 +00:00
final List<UTXO> availableOutputs =
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-30 14:28:54 +00:00
availableOutputs[i]
.isConfirmed(await _getChainHeight(), _minConfirms) ==
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
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
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
int feeForWithChange = _estimateTxFee(
2023-01-25 21:07:44 +00:00
vSize: vSizeForWithChange,
feeRatePerKB: selectedTxFeeRate,
);
2023-01-31 16:03:16 +00:00
if (_coin == Coin.dogecoin || _coin == Coin.dogecoinTestNet) {
if (feeForNoChange < vSizeForNoChange * 1000) {
feeForNoChange = vSizeForNoChange * 1000;
}
if (feeForWithChange < vSizeForWithChange * 1000) {
feeForWithChange = vSizeForWithChange * 1000;
}
2023-01-25 21:07:44 +00:00
}
2023-01-30 14:28:54 +00:00
if (satoshisBeingUsed - amountToSend > feeForNoChange + _dustLimitP2PKH) {
2023-01-25 21:07:44 +00:00
// 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
2023-01-30 14:28:54 +00:00
if (changeAmount >= _dustLimitP2PKH &&
2023-01-25 21:07:44 +00:00
satoshisBeingUsed - amountToSend - changeAmount == feeForWithChange) {
2023-01-31 16:37:03 +00:00
var txn = await _createNotificationTx(
2023-01-25 21:07:44 +00:00
targetPaymentCodeString: targetPaymentCodeString,
utxosToUse: utxoObjectsToUse,
utxoSigningData: utxoSigningData,
change: changeAmount,
);
int feeBeingPaid = satoshisBeingUsed - amountToSend - changeAmount;
2023-01-31 16:37:03 +00:00
// make sure minimum fee is accurate if that is being used
if (txn.item2 - feeBeingPaid == 1) {
changeAmount -= 1;
feeBeingPaid += 1;
txn = await _createNotificationTx(
targetPaymentCodeString: targetPaymentCodeString,
utxosToUse: utxoObjectsToUse,
utxoSigningData: utxoSigningData,
change: changeAmount,
);
}
2023-01-25 21:07:44 +00:00
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 =
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
final txb = btc_dart.TransactionBuilder(network: _network);
2023-01-25 21:07:44 +00:00
txb.setVersion(1);
txb.addInput(
utxo.txid,
txPointIndex,
2023-01-31 18:17:32 +00:00
null,
utxoSigningData[utxo.txid]["output"] as Uint8List,
2023-01-25 21:07:44 +00:00
);
// todo: modify address once segwit support is in our bip47
2023-01-30 14:28:54 +00:00
txb.addOutput(
targetPaymentCode.notificationAddressP2PKH(), _dustLimitP2PKH);
2023-01-25 21:07:44 +00:00
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
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,
2023-01-31 18:17:32 +00:00
witnessValue: utxo.value,
witnessScript: utxoSigningData[utxo.txid]["redeemScript"] as Uint8List?,
2023-01-25 21:07:44 +00:00
);
// sign rest of possible inputs
2023-01-31 18:17:32 +00:00
for (var i = 1; i < utxosToUse.length; i++) {
2023-01-25 21:07:44 +00:00
final txid = utxosToUse[i].txid;
txb.sign(
vin: i,
keyPair: utxoSigningData[txid]["keyPair"] as btc_dart.ECPair,
2023-01-31 18:17:32 +00:00
witnessValue: utxosToUse[i].value,
witnessScript: utxoSigningData[utxo.txid]["redeemScript"] as Uint8List?,
2023-01-25 21:07:44 +00:00
);
}
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);
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 {
await _refresh();
2023-01-25 21:07:44 +00:00
} catch (e) {
Logging.instance.log(
"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 =
await getMyNotificationAddress(DerivePathTypeExt.primaryFor(_coin));
2023-01-25 21:07:44 +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;
}
if (tx.address.value?.otherData != null) {
final code = await paymentCodeStringByKey(tx.address.value!.otherData!);
if (code == paymentCodeString) {
return true;
}
}
2023-01-25 21:07:44 +00:00
}
// 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 blindedCodeBytes =
Bip47Utils.getBlindedPaymentCodeBytesFrom(transaction);
2023-01-31 20:09:45 +00:00
// transaction does not contain a payment code
if (blindedCodeBytes == null) {
return null;
}
2023-01-25 21:07:44 +00:00
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 = await deriveNotificationPrivateKey(
mnemonic: (await _getMnemonicString())!,
mnemonicPassphrase: (await _getMnemonicPassphrase())!,
);
2023-01-25 21:07:44 +00:00
final S = SecretPoint(myPrivateKey, pubKey);
final mask = PaymentCode.getMask(S.ecdhSecret(), rev);
2023-01-31 20:09:45 +00:00
final unBlindedPayload = PaymentCode.blind(blindedCodeBytes, mask);
2023-01-25 21:07:44 +00:00
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 =
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> codes = [];
2023-01-25 21:07:44 +00:00
for (final tx in txns) {
// tx is sent so we can check the address's otherData for the code String
if (tx.type == TransactionType.outgoing &&
tx.address.value?.otherData != null) {
final codeString =
await paymentCodeStringByKey(tx.address.value!.otherData!);
if (codeString != null &&
codes.where((e) => e.toString() == codeString).isEmpty) {
codes.add(PaymentCode.fromPaymentCode(codeString, _network));
}
} else {
// otherwise we need to un blind the code
final unBlinded = await unBlindedPaymentCodeFromTransaction(
transaction: tx,
myNotificationAddress: myAddress,
);
if (unBlinded != null &&
codes.where((e) => e.toString() == unBlinded.toString()).isEmpty) {
codes.add(unBlinded);
}
2023-01-25 21:07:44 +00:00
}
}
return codes;
}
Future<void> checkForNotificationTransactionsTo(
Set<String> otherCodeStrings) async {
final sentNotificationTransactions = await _db
.getTransactions(_walletId)
.filter()
.subTypeEqualTo(TransactionSubType.bip47Notification)
.and()
.typeEqualTo(TransactionType.outgoing)
.findAll();
final List<PaymentCode> codes = [];
for (final codeString in otherCodeStrings) {
codes.add(PaymentCode.fromPaymentCode(codeString, _network));
}
for (final tx in sentNotificationTransactions) {
if (tx.address.value != null && tx.address.value!.otherData == null) {
final oldAddress =
await _db.getAddress(_walletId, tx.address.value!.value);
for (final code in codes) {
final notificationAddress = code.notificationAddressP2PKH();
if (notificationAddress == oldAddress!.value) {
final address = Address(
walletId: _walletId,
value: notificationAddress,
publicKey: [],
derivationIndex: 0,
derivationPath:
null, // might as well use null due to complexity of context
type: oldAddress.type,
subType: AddressSubType.paynymNotification,
otherData: await storeCode(code.toString()),
);
await _db.updateAddress(oldAddress, address);
}
}
}
}
2023-01-25 21:07:44 +00:00
}
2023-01-27 20:43:38 +00:00
Future<void> restoreAllHistory({
required int maxUnusedAddressGap,
required int maxNumberOfIndexesToCheck,
required Set<String> paymentCodeStrings,
2023-01-27 20:43:38 +00:00
}) async {
final codes = await getAllPaymentCodesFromNotificationTransactions();
final List<PaymentCode> extraCodes = [];
for (final codeString in paymentCodeStrings) {
if (codes.where((e) => e.toString() == codeString).isEmpty) {
final extraCode = PaymentCode.fromPaymentCode(codeString, _network);
if (extraCode.isValid()) {
extraCodes.add(extraCode);
}
}
}
codes.addAll(extraCodes);
2023-01-27 20:43:38 +00:00
final List<Future<void>> futures = [];
for (final code in codes) {
futures.add(
restoreHistoryWith(
code,
maxUnusedAddressGap,
maxNumberOfIndexesToCheck,
),
);
2023-01-27 20:43:38 +00:00
}
await Future.wait(futures);
}
2023-01-25 21:07:44 +00:00
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);
final mnemonic = (await _getMnemonicString())!;
final mnemonicPassphrase = (await _getMnemonicPassphrase())!;
2023-01-27 20:43:00 +00:00
final mySendPrivateKey = await deriveNotificationPrivateKey(
mnemonic: mnemonic,
mnemonicPassphrase: mnemonicPassphrase,
);
final receivingNode = (await _getRootNode(
mnemonic: mnemonic,
mnemonicPassphrase: mnemonicPassphrase,
))
.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++) {
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-28 00:18:58 +00:00
final address = await generatePaynymSendAddressFromKeyPair(
2023-01-25 21:07:44 +00:00
pair: pair,
derivationIndex: i,
derivePathType: DerivePathType.bip44,
toPaymentCode: other,
);
addresses.add(address);
final count = await _getTxCount(address: address.value);
2023-01-25 21:07:44 +00:00
if (count > 0) {
outgoingGapCounter = 0;
2023-01-27 20:43:00 +00:00
} else {
outgoingGapCounter++;
2023-01-25 21:07:44 +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();
final address = await generatePaynymReceivingAddressFromKeyPair(
2023-01-25 21:07:44 +00:00
pair: pair,
derivationIndex: i,
derivePathType: DerivePathType.bip44,
fromPaymentCode: other,
);
addresses.add(address);
final count = await _getTxCount(address: address.value);
2023-01-25 21:07:44 +00:00
if (count > 0) {
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
}
2023-01-28 00:18:58 +00:00
Future<Address> generatePaynymSendAddressFromKeyPair({
2023-01-25 21:07:44 +00:00
required btc_dart.ECPair pair,
required int derivationIndex,
required DerivePathType derivePathType,
required PaymentCode toPaymentCode,
2023-01-28 00:18:58 +00:00
}) async {
2023-01-25 21:07:44 +00:00
final data = btc_dart.PaymentData(pubkey: pair.publicKey);
String addressString;
switch (derivePathType) {
case DerivePathType.bip44:
addressString =
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(
walletId: _walletId,
2023-01-25 21:07:44 +00:00
value: addressString,
publicKey: pair.publicKey,
derivationIndex: derivationIndex,
derivationPath:
null, // might as well use null due to complexity of context
2023-01-25 21:07:44 +00:00
type: AddressType.nonWallet,
subType: AddressSubType.paynymSend,
2023-01-28 00:18:58 +00:00
otherData: await storeCode(toPaymentCode.toString()),
2023-01-25 21:07:44 +00:00
);
return address;
}
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,
}) 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,
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(
walletId: _walletId,
2023-01-25 21:07:44 +00:00
value: addressString,
publicKey: pair.publicKey,
derivationIndex: derivationIndex,
derivationPath:
null, // might as well use null due to complexity of context
2023-01-25 21:07:44 +00:00
type: addrType,
subType: AddressSubType.paynymReceive,
2023-01-28 00:18:58 +00:00
otherData: await storeCode(fromPaymentCode.toString()),
2023-01-25 21:07:44 +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;
default:
throw Exception("DerivePathType $derivePathType not supported");
2023-01-25 21:07:44 +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 {
final root = await _getRootNode(
mnemonic: (await _getMnemonicString())!,
mnemonicPassphrase: (await _getMnemonicPassphrase())!,
);
2023-01-25 21:07:44 +00:00
final node = root.derivePath(kPaynymDerivePath);
final paymentCode = PaymentCode.initFromPubKey(
node.publicKey,
node.chainCode,
_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,
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(
walletId: _walletId,
2023-01-25 21:07:44 +00:00
value: addressString,
publicKey: paymentCode.getPubKey(),
derivationIndex: 0,
derivationPath:
null, // might as well use null due to complexity of context
2023-01-25 21:07:44 +00:00
type: type,
subType: AddressSubType.paynymNotification,
2023-01-28 00:18:58 +00:00
otherData: await storeCode(paymentCode.toString()),
2023-01-25 21:07:44 +00:00
);
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(),
);
await _db.putAddress(address);
2023-01-25 21:07:44 +00:00
return address;
}
}
2023-01-28 00:18:58 +00:00
/// look up a key that corresponds to a payment code string
2023-02-01 22:46:49 +00:00
Future<List<String>> lookupKey(String paymentCodeString) async {
2023-01-28 00:18:58 +00:00
final keys =
(await _secureStorage.keys).where((e) => e.startsWith(kPCodeKeyPrefix));
2023-02-01 22:46:49 +00:00
final List<String> result = [];
2023-01-28 00:18:58 +00:00
for (final key in keys) {
final value = await _secureStorage.read(key: key);
if (value == paymentCodeString) {
2023-02-01 22:46:49 +00:00
result.add(key);
2023-01-28 00:18:58 +00:00
}
}
2023-02-01 22:46:49 +00:00
return result;
2023-01-28 00:18:58 +00:00
}
/// fetch a payment code string
Future<String?> paymentCodeStringByKey(String key) async {
final value = await _secureStorage.read(key: key);
return value;
}
/// store payment code string and return the generated key used
Future<String> storeCode(String paymentCodeString) async {
final key = _generateKey();
await _secureStorage.write(key: key, value: paymentCodeString);
return key;
}
/// generate a new payment code string storage key
String _generateKey() {
final bytes = _randomBytes(24);
return "$kPCodeKeyPrefix${bytes.toHex}";
}
// https://github.com/AaronFeickert/stack_wallet_backup/blob/master/lib/secure_storage.dart#L307-L311
/// Generate cryptographically-secure random bytes
Uint8List _randomBytes(int n) {
final Random rng = Random.secure();
return Uint8List.fromList(
List<int>.generate(n, (_) => rng.nextInt(0xFF + 1)));
}
2023-01-25 21:07:44 +00:00
}
2023-01-28 00:18:58 +00:00
const String kPCodeKeyPrefix = "pCode_key_";