Skip to content

Commit

Permalink
FIX: cant use funds that landed on bip47 payment code
Browse files Browse the repository at this point in the history
  • Loading branch information
Overtorment committed Mar 18, 2023
1 parent f454d42 commit 26708e8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions class/wallets/abstract-hd-electrum-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,12 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {
for (let c = 0; c < this.next_free_change_address_index + this.gap_limit; c++) {
if (this._getInternalAddressByIndex(c) === address) return path + '/1/' + c;
}
for (const pc of this._sender_payment_codes) {
for (let c = 0; c < this._getNextFreePaymentCodeAddress(pc) + this.gap_limit; c++) {
// not technically correct but well, to have at least somethign in PSBT...
if (this._getBIP47Address(pc, c) === address) return "m/47'/0'/0'/" + c;
}
}

return false;
}
Expand Down
31 changes: 30 additions & 1 deletion tests/unit/bip47.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import ecc from 'tiny-secp256k1';
import assert from 'assert';

import { HDSegwitBech32Wallet, WatchOnlyWallet } from '../../class';
import { ECPairFactory } from 'ecpair';
const bitcoin = require('bitcoinjs-lib');

const ECPair = ECPairFactory(ecc);

describe('Bech32 Segwit HD (BIP84) with BIP47', () => {
it('should work', async () => {
Expand Down Expand Up @@ -64,7 +68,32 @@ describe('Bech32 Segwit HD (BIP84) with BIP47', () => {

expect(ourNotificationAddress).toEqual('1EiP2kSqxNqRhn8MPMkrtSEqaWiCWLYyTS'); // our notif address

assert.ok(!w.weOwnAddress('1JDdmqFLhpzcUwPeinhJbUPw4Co3aWLyzW')); // alice notif address, we dont own it
// since we dont do network calls in unit test we cant get counterparties payment codes from our notif address,
// and thus, dont know collaborative addresses with our payers. lets hardcode our counterparty payment code to test
// this functionality

assert.deepStrictEqual(w.getBIP47SenderPaymentCodes(), []);

w._sender_payment_codes = [
'PM8TJi1RuCrgSHTzGMoayUf8xUW6zYBGXBPSWwTiMhMMwqto7G6NA4z9pN5Kn8Pbhryo2eaHMFRRcidCGdB3VCDXJD4DdPD2ZyG3ScLMEvtStAetvPMo',
];

assert.deepStrictEqual(w.getBIP47SenderPaymentCodes(), [
'PM8TJi1RuCrgSHTzGMoayUf8xUW6zYBGXBPSWwTiMhMMwqto7G6NA4z9pN5Kn8Pbhryo2eaHMFRRcidCGdB3VCDXJD4DdPD2ZyG3ScLMEvtStAetvPMo',
]);

assert.ok(w.weOwnAddress('bc1q57nwf9vfq2qsl80q37wq5h0tjytsk95vgjq4fe'));
const pubkey = w._getPubkeyByAddress('bc1q57nwf9vfq2qsl80q37wq5h0tjytsk95vgjq4fe');
const path = w._getDerivationPathByAddress('bc1q57nwf9vfq2qsl80q37wq5h0tjytsk95vgjq4fe');
assert.ok(pubkey);
assert.ok(path);

const keyPair2 = ECPair.fromWIF(w._getWIFbyAddress('bc1q57nwf9vfq2qsl80q37wq5h0tjytsk95vgjq4fe') || '');
const address = bitcoin.payments.p2wpkh({
pubkey: keyPair2.publicKey,
}).address;

assert.strictEqual(address, 'bc1q57nwf9vfq2qsl80q37wq5h0tjytsk95vgjq4fe');
});

it('should work (sparrow)', async () => {
Expand Down

0 comments on commit 26708e8

Please sign in to comment.