fix signing paynym received inputs

This commit is contained in:
julian 2023-04-26 15:12:36 -06:00
parent d0bb0183f3
commit 1dc2a3ce15
4 changed files with 91 additions and 33 deletions

View file

@ -2540,43 +2540,67 @@ class BitcoinWallet extends CoinServiceAPI
String? pubKey;
String? wif;
// fetch receiving derivations if null
receiveDerivations[sd.derivePathType] ??= await _fetchDerivations(
chain: 0,
derivePathType: sd.derivePathType,
);
final receiveDerivation =
receiveDerivations[sd.derivePathType]![sd.utxo.address!];
final address = await db.getAddress(walletId, sd.utxo.address!);
if (address?.derivationPath != null) {
final bip32.BIP32 node;
if (address!.subType == isar_models.AddressSubType.paynymReceive) {
final code = await paymentCodeStringByKey(address.otherData!);
if (receiveDerivation != null) {
pubKey = receiveDerivation["pubKey"] as String;
wif = receiveDerivation["wif"] as String;
} else {
// fetch change derivations if null
changeDerivations[sd.derivePathType] ??= await _fetchDerivations(
chain: 1,
derivePathType: sd.derivePathType,
);
final changeDerivation =
changeDerivations[sd.derivePathType]![sd.utxo.address!];
if (changeDerivation != null) {
pubKey = changeDerivation["pubKey"] as String;
wif = changeDerivation["wif"] as String;
}
}
final bip47base = await getBip47BaseNode();
if (wif == null || pubKey == null) {
final address = await db.getAddress(walletId, sd.utxo.address!);
if (address?.derivationPath != null) {
final node = await Bip32Utils.getBip32Node(
final privateKey = await getPrivateKeyForPaynymReceivingAddress(
paymentCodeString: code!,
index: address.derivationIndex,
);
node = bip32.BIP32.fromPrivateKey(
privateKey,
bip47base.chainCode,
bip32.NetworkType(
wif: _network.wif,
bip32: bip32.Bip32Type(
public: _network.bip32.public,
private: _network.bip32.private,
),
),
);
} else {
node = await Bip32Utils.getBip32Node(
(await mnemonicString)!,
(await mnemonicPassphrase)!,
_network,
address!.derivationPath!.value,
address.derivationPath!.value,
);
}
wif = node.toWIF();
pubKey = Format.uint8listToString(node.publicKey);
wif = node.toWIF();
pubKey = Format.uint8listToString(node.publicKey);
}
if (wif == null || pubKey == null) {
// fetch receiving derivations if null
receiveDerivations[sd.derivePathType] ??= await _fetchDerivations(
chain: 0,
derivePathType: sd.derivePathType,
);
final receiveDerivation =
receiveDerivations[sd.derivePathType]![sd.utxo.address!];
if (receiveDerivation != null) {
pubKey = receiveDerivation["pubKey"] as String;
wif = receiveDerivation["wif"] as String;
} else {
// fetch change derivations if null
changeDerivations[sd.derivePathType] ??= await _fetchDerivations(
chain: 1,
derivePathType: sd.derivePathType,
);
final changeDerivation =
changeDerivations[sd.derivePathType]![sd.utxo.address!];
if (changeDerivation != null) {
pubKey = changeDerivation["pubKey"] as String;
wif = changeDerivation["wif"] as String;
}
}
}

View file

@ -142,6 +142,40 @@ mixin PaynymWalletInterface {
// convenience getter
btc_dart.NetworkType get networkType => _network;
Future<bip32.BIP32> getBip47BaseNode() async {
final root = await _getRootNode(
mnemonic: (await _getMnemonicString())!,
mnemonicPassphrase: (await _getMnemonicPassphrase())!,
);
final node = root.derivePath(
_basePaynymDerivePath(
testnet: _coin.isTestNet,
),
);
return node;
}
Future<Uint8List> getPrivateKeyForPaynymReceivingAddress({
required String paymentCodeString,
required int index,
}) async {
final bip47base = await getBip47BaseNode();
final paymentAddress = PaymentAddress(
bip32Node: bip47base.derive(index),
paymentCode: PaymentCode.fromPaymentCode(
paymentCodeString,
networkType: networkType,
),
networkType: networkType,
index: 0,
);
final pair = paymentAddress.getReceiveAddressKeyPair();
return pair.privateKey!;
}
Future<Address> currentReceivingPaynymAddress({
required PaymentCode sender,
required bool isSegwit,

View file

@ -111,8 +111,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: "388c2f4d59af358eba4c92bce3c33e018d5ac03b"
resolved-ref: "388c2f4d59af358eba4c92bce3c33e018d5ac03b"
ref: "38847255d035c0f6ec5bc93d19130ec804cf90e9"
resolved-ref: "38847255d035c0f6ec5bc93d19130ec804cf90e9"
url: "https://github.com/cypherstack/bip47.git"
source: git
version: "2.0.0"

View file

@ -60,7 +60,7 @@ dependencies:
bip47:
git:
url: https://github.com/cypherstack/bip47.git
ref: 388c2f4d59af358eba4c92bce3c33e018d5ac03b
ref: 38847255d035c0f6ec5bc93d19130ec804cf90e9
# Utility plugins
# provider: ^6.0.1