use derive path type in paynym extension

This commit is contained in:
julian 2023-01-25 12:13:01 -06:00
parent 6b08acf225
commit 35ba2f9d79

View file

@ -18,6 +18,8 @@ import 'package:stackwallet/utilities/format.dart';
import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
import 'package:tuple/tuple.dart'; import 'package:tuple/tuple.dart';
import '../../utilities/enums/derive_path_type_enum.dart';
const kPaynymDerivePath = "m/47'/0'/0'"; const kPaynymDerivePath = "m/47'/0'/0'";
extension PayNym on DogecoinWallet { extension PayNym on DogecoinWallet {
@ -38,39 +40,14 @@ extension PayNym on DogecoinWallet {
} }
/// fetch or generate this wallet's bip47 payment code /// fetch or generate this wallet's bip47 payment code
Future<PaymentCode> getPaymentCode() async { Future<PaymentCode> getPaymentCode(
final address = await db DerivePathType derivePathType,
.getAddresses(walletId) ) async {
.filter() final address = await getMyNotificationAddress(derivePathType);
.subTypeEqualTo(AddressSubType.paynymNotification) final paymentCode = PaymentCode.fromPaymentCode(
.and() address.otherData!,
.not() network,
.typeEqualTo(AddressType.nonWallet) );
.findFirst();
PaymentCode paymentCode;
if (address == null) {
final root = await getRootNode(mnemonic: await mnemonic);
final node = root.derivePath(kPaynymDerivePath);
paymentCode = PaymentCode.initFromPubKey(
node.publicKey,
node.chainCode,
network,
);
await db.putAddress(
Address(
walletId: walletId,
value: paymentCode.notificationAddress(),
publicKey: paymentCode.getPubKey(),
derivationIndex: 0,
type: AddressType.p2pkh, // todo change this for btc
subType: AddressSubType.paynymNotification,
otherData: paymentCode.toString(),
),
);
} else {
paymentCode = PaymentCode.fromPaymentCode(address.otherData!, network);
}
return paymentCode; return paymentCode;
} }
@ -344,7 +321,7 @@ extension PayNym on DogecoinWallet {
}) async { }) async {
final targetPaymentCode = final targetPaymentCode =
PaymentCode.fromPaymentCode(targetPaymentCodeString, network); PaymentCode.fromPaymentCode(targetPaymentCodeString, network);
final myCode = await getPaymentCode(); final myCode = await getPaymentCode(DerivePathType.bip44);
final utxo = utxosToUse.first; final utxo = utxosToUse.first;
final txPoint = utxo.txid.fromHex.toList(); final txPoint = utxo.txid.fromHex.toList();
@ -443,7 +420,7 @@ extension PayNym on DogecoinWallet {
// TODO optimize // TODO optimize
Future<bool> hasConnected(String paymentCodeString) async { Future<bool> hasConnected(String paymentCodeString) async {
final myCode = await getPaymentCode(); final myCode = await getPaymentCode(DerivePathType.bip44);
final myNotificationAddress = myCode.notificationAddress(); final myNotificationAddress = myCode.notificationAddress();
final txns = await db final txns = await db
@ -521,7 +498,7 @@ extension PayNym on DogecoinWallet {
Future<List<PaymentCode>> Future<List<PaymentCode>>
getAllPaymentCodesFromNotificationTransactions() async { getAllPaymentCodesFromNotificationTransactions() async {
final myCode = await getPaymentCode(); final myCode = await getPaymentCode(DerivePathType.bip44);
final txns = await db final txns = await db
.getTransactions(walletId) .getTransactions(walletId)
.filter() .filter()
@ -627,21 +604,31 @@ extension PayNym on DogecoinWallet {
break; break;
// The following doesn't apply to doge currently // The following doesn't apply to doge currently
// case DerivePathType.bip49:
// case DerivePathType.bip49: addressString = btc_dart
// addressString = btc_dart.P2SH( .P2SH(
// data: btc_dart.PaymentData( data: btc_dart.PaymentData(
// redeem: btc_dart.P2WPKH(data: data, network: network).data), redeem: btc_dart
// network: network) .P2WPKH(
// .data data: data,
// .address!; network: network,
// addrType = AddressType.p2sh; )
// break; .data),
// case DerivePathType.bip84: network: network,
// addressString = btc_dart.P2WPKH(network: network, data: data).data.address!; )
// addrType = AddressType.p2wpkh; .data
// break; .address!;
break;
case DerivePathType.bip84:
addressString = btc_dart
.P2WPKH(
network: network,
data: data,
)
.data
.address!;
break;
} }
final address = Address( final address = Address(
@ -669,27 +656,44 @@ extension PayNym on DogecoinWallet {
AddressType addrType; AddressType addrType;
switch (derivePathType) { switch (derivePathType) {
case DerivePathType.bip44: case DerivePathType.bip44:
addressString = addressString = btc_dart
btc_dart.P2PKH(data: data, network: network).data.address!; .P2PKH(
data: data,
network: network,
)
.data
.address!;
addrType = AddressType.p2pkh; addrType = AddressType.p2pkh;
break; break;
// The following doesn't apply to doge currently // The following doesn't apply to doge currently
// case DerivePathType.bip49:
// case DerivePathType.bip49: addressString = btc_dart
// addressString = btc_dart.P2SH( .P2SH(
// data: btc_dart.PaymentData( data: btc_dart.PaymentData(
// redeem: btc_dart.P2WPKH(data: data, network: network).data), redeem: btc_dart
// network: network) .P2WPKH(
// .data data: data,
// .address!; network: network,
// addrType = AddressType.p2sh; )
// break; .data),
// case DerivePathType.bip84: network: network,
// addressString = btc_dart.P2WPKH(network: network, data: data).data.address!; )
// addrType = AddressType.p2wpkh; .data
// break; .address!;
addrType = AddressType.p2sh;
break;
case DerivePathType.bip84:
addressString = btc_dart
.P2WPKH(
network: network,
data: data,
)
.data
.address!;
addrType = AddressType.p2wpkh;
break;
} }
final address = Address( final address = Address(
@ -704,4 +708,68 @@ extension PayNym on DogecoinWallet {
return address; return address;
} }
Future<Address> getMyNotificationAddress(
DerivePathType derivePathType,
) async {
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;
}
final storedAddress = await db
.getAddresses(walletId)
.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 mnemonic);
final node = root.derivePath(kPaynymDerivePath);
final paymentCode = PaymentCode.initFromPubKey(
node.publicKey,
node.chainCode,
network,
);
String addressString;
switch (derivePathType) {
case DerivePathType.bip44:
addressString = paymentCode.notificationAddress();
break;
case DerivePathType.bip49:
throw Exception("DerivePathType not supported.");
case DerivePathType.bip84:
throw Exception("DerivePathType not supported.");
}
final address = Address(
walletId: walletId,
value: addressString,
publicKey: paymentCode.getPubKey(),
derivationIndex: 0,
type: type,
subType: AddressSubType.paynymNotification,
otherData: paymentCode.toString(),
);
await db.putAddress(address);
return address;
}
}
} }