From 9c44dc6c6b31ff766a676bc401269481e84cba49 Mon Sep 17 00:00:00 2001 From: julian Date: Wed, 25 Jan 2023 14:33:59 -0600 Subject: [PATCH] add support to make adding segwit paynym support simple once implemented --- lib/pages/paynym/paynym_claim_view.dart | 4 +- .../sub_widgets/wallet_navigation_bar.dart | 4 +- .../wallet_view/desktop_wallet_view.dart | 4 +- lib/services/coins/coin_paynym_extension.dart | 184 +++++++++++------- .../paynym_follow_toggle_button.dart | 7 +- pubspec.lock | 4 +- pubspec.yaml | 2 +- 7 files changed, 129 insertions(+), 80 deletions(-) diff --git a/lib/pages/paynym/paynym_claim_view.dart b/lib/pages/paynym/paynym_claim_view.dart index cf5ffc671..f141e04a4 100644 --- a/lib/pages/paynym/paynym_claim_view.dart +++ b/lib/pages/paynym/paynym_claim_view.dart @@ -12,6 +12,7 @@ import 'package:stackwallet/providers/wallet/my_paynym_account_state_provider.da import 'package:stackwallet/services/coins/coin_paynym_extension.dart'; import 'package:stackwallet/services/coins/dogecoin/dogecoin_wallet.dart'; import 'package:stackwallet/utilities/assets.dart'; +import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/utilities/util.dart'; @@ -168,7 +169,8 @@ class _PaynymClaimViewState extends ConsumerState { if (shouldCancel) return; // get payment code - final pCode = await wallet.getPaymentCode(); + final pCode = await wallet.getPaymentCode( + DerivePathTypeExt.primaryFor(wallet.coin)); if (shouldCancel) return; diff --git a/lib/pages/wallet_view/sub_widgets/wallet_navigation_bar.dart b/lib/pages/wallet_view/sub_widgets/wallet_navigation_bar.dart index 28b4e735c..f251dedd4 100644 --- a/lib/pages/wallet_view/sub_widgets/wallet_navigation_bar.dart +++ b/lib/pages/wallet_view/sub_widgets/wallet_navigation_bar.dart @@ -12,6 +12,7 @@ import 'package:stackwallet/services/coins/coin_paynym_extension.dart'; import 'package:stackwallet/services/coins/dogecoin/dogecoin_wallet.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; +import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart'; import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; @@ -120,7 +121,8 @@ class _WalletNavigationBarState extends State { .getManager(widget.walletId) .wallet as DogecoinWallet); - final code = await wallet.getPaymentCode(); + final code = await wallet.getPaymentCode( + DerivePathTypeExt.primaryFor(wallet.coin)); final account = await ref .read(paynymAPIProvider) diff --git a/lib/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart b/lib/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart index d4a77bae8..d48646860 100644 --- a/lib/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart +++ b/lib/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart @@ -31,6 +31,7 @@ import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/enums/backup_frequency_type.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; +import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart'; import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; @@ -304,7 +305,8 @@ class _DesktopWalletViewState extends ConsumerState { .getManager(widget.walletId) .wallet as DogecoinWallet); - final code = await wallet.getPaymentCode(); + final code = + await wallet.getPaymentCode(DerivePathTypeExt.primaryFor(wallet.coin)); final account = await ref.read(paynymAPIProvider).nym(code.toString()); diff --git a/lib/services/coins/coin_paynym_extension.dart b/lib/services/coins/coin_paynym_extension.dart index f80f78961..7a631e154 100644 --- a/lib/services/coins/coin_paynym_extension.dart +++ b/lib/services/coins/coin_paynym_extension.dart @@ -14,12 +14,11 @@ 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/derive_path_type_enum.dart'; import 'package:stackwallet/utilities/format.dart'; import 'package:stackwallet/utilities/logger.dart'; import 'package:tuple/tuple.dart'; -import '../../utilities/enums/derive_path_type_enum.dart'; - const kPaynymDerivePath = "m/47'/0'/0'"; extension PayNym on DogecoinWallet { @@ -66,8 +65,10 @@ extension PayNym on DogecoinWallet { } Future>> preparePaymentCodeSend( - PaymentCode paymentCode, int satoshiAmount) async { - if (!(await hasConnected(paymentCode.notificationAddress()))) { + {required PaymentCode paymentCode, + required int satoshiAmount, + Map? args}) async { + if (!(await hasConnected(paymentCode.notificationAddressP2PKH()))) { throw PaynymSendException( "No notification transaction sent to $paymentCode"); } else { @@ -360,7 +361,8 @@ extension PayNym on DogecoinWallet { txPointIndex, ); - txb.addOutput(targetPaymentCode.notificationAddress(), DUST_LIMIT); + // 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 @@ -420,8 +422,8 @@ extension PayNym on DogecoinWallet { // TODO optimize Future hasConnected(String paymentCodeString) async { - final myCode = await getPaymentCode(DerivePathType.bip44); - final myNotificationAddress = myCode.notificationAddress(); + final myNotificationAddress = + await getMyNotificationAddress(DerivePathTypeExt.primaryFor(coin)); final txns = await db .getTransactions(walletId) @@ -431,13 +433,13 @@ extension PayNym on DogecoinWallet { for (final tx in txns) { // quick check that may cause problems? - if (tx.address.value?.value == myNotificationAddress) { + if (tx.address.value?.value == myNotificationAddress.value) { return true; } final unBlindedPaymentCode = await unBlindedPaymentCodeFromTransaction( transaction: tx, - myCode: myCode, + myNotificationAddress: myNotificationAddress, ); if (paymentCodeString == unBlindedPaymentCode.toString()) { @@ -451,10 +453,10 @@ extension PayNym on DogecoinWallet { Future unBlindedPaymentCodeFromTransaction({ required Transaction transaction, - required PaymentCode myCode, + required Address myNotificationAddress, }) async { if (transaction.address.value != null && - transaction.address.value!.value != myCode.notificationAddress()) { + transaction.address.value!.value != myNotificationAddress.value) { return null; } @@ -498,7 +500,8 @@ extension PayNym on DogecoinWallet { Future> getAllPaymentCodesFromNotificationTransactions() async { - final myCode = await getPaymentCode(DerivePathType.bip44); + final myAddress = + await getMyNotificationAddress(DerivePathTypeExt.primaryFor(coin)); final txns = await db .getTransactions(walletId) .filter() @@ -510,7 +513,7 @@ extension PayNym on DogecoinWallet { for (final tx in txns) { final unBlinded = await unBlindedPaymentCodeFromTransaction( transaction: tx, - myCode: myCode, + myNotificationAddress: myAddress, ); if (unBlinded != null) { unBlindedList.add(unBlinded); @@ -603,32 +606,34 @@ extension PayNym on DogecoinWallet { btc_dart.P2PKH(data: data, network: network).data.address!; break; - // The following doesn't apply to doge 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; + // 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( @@ -666,34 +671,36 @@ extension PayNym on DogecoinWallet { addrType = AddressType.p2pkh; break; - // The following doesn't apply to doge 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; + // 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( @@ -712,6 +719,9 @@ extension PayNym on DogecoinWallet { Future
getMyNotificationAddress( DerivePathType derivePathType, ) async { + // TODO: fix when segwit is here + derivePathType = DerivePathType.bip44; + AddressType type; switch (derivePathType) { case DerivePathType.bip44: @@ -748,14 +758,44 @@ extension PayNym on DogecoinWallet { ); String addressString; + final data = + btc_dart.PaymentData(pubkey: paymentCode.notificationPublicKey()); switch (derivePathType) { case DerivePathType.bip44: - addressString = paymentCode.notificationAddress(); + addressString = btc_dart + .P2PKH( + data: data, + network: network, + ) + .data + .address!; break; - case DerivePathType.bip49: - throw Exception("DerivePathType not supported."); - case DerivePathType.bip84: - throw Exception("DerivePathType not supported."); + // 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( diff --git a/lib/widgets/custom_buttons/paynym_follow_toggle_button.dart b/lib/widgets/custom_buttons/paynym_follow_toggle_button.dart index ea3771d95..a93a4510c 100644 --- a/lib/widgets/custom_buttons/paynym_follow_toggle_button.dart +++ b/lib/widgets/custom_buttons/paynym_follow_toggle_button.dart @@ -12,6 +12,7 @@ import 'package:stackwallet/providers/wallet/my_paynym_account_state_provider.da import 'package:stackwallet/services/coins/coin_paynym_extension.dart'; import 'package:stackwallet/services/coins/dogecoin/dogecoin_wallet.dart'; import 'package:stackwallet/utilities/assets.dart'; +import 'package:stackwallet/utilities/enums/derive_path_type_enum.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/utilities/util.dart'; import 'package:stackwallet/widgets/desktop/primary_button.dart'; @@ -67,7 +68,8 @@ class _PaynymFollowToggleButtonState .read(paynymAPIProvider) .nym(widget.paymentCodeStringToFollow, true); - final myPCode = await wallet.getPaymentCode(); + final myPCode = + await wallet.getPaymentCode(DerivePathTypeExt.primaryFor(wallet.coin)); PaynymResponse token = await ref.read(paynymAPIProvider).token(myPCode.toString()); @@ -167,7 +169,8 @@ class _PaynymFollowToggleButtonState .read(paynymAPIProvider) .nym(widget.paymentCodeStringToFollow, true); - final myPCode = await wallet.getPaymentCode(); + final myPCode = + await wallet.getPaymentCode(DerivePathTypeExt.primaryFor(wallet.coin)); PaynymResponse token = await ref.read(paynymAPIProvider).token(myPCode.toString()); diff --git a/pubspec.lock b/pubspec.lock index 0c8768e2a..8721ccc48 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -100,8 +100,8 @@ packages: dependency: "direct main" description: path: "." - ref: "2d800225329498841affa1386d96d2696c62b713" - resolved-ref: "2d800225329498841affa1386d96d2696c62b713" + ref: "87bb760be323228aed6ca7bd4532a709a4f10690" + resolved-ref: "87bb760be323228aed6ca7bd4532a709a4f10690" url: "https://github.com/cypherstack/bip47.git" source: git version: "1.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 967c9cba4..759fc51f5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -59,7 +59,7 @@ dependencies: bip47: git: url: https://github.com/cypherstack/bip47.git - ref: 2d800225329498841affa1386d96d2696c62b713 + ref: 87bb760be323228aed6ca7bd4532a709a4f10690 # Utility plugins # provider: ^6.0.1