FIX: import single-sig wallet descriptor (closes #5637)

This commit is contained in:
overtorment 2023-08-09 10:33:12 +01:00
parent 238b4f086d
commit f610906267
2 changed files with 48 additions and 0 deletions

View File

@ -301,6 +301,27 @@ export class AbstractWallet {
}
}
// is it output descriptor?
if (this.secret.startsWith('wpkh(') || this.secret.startsWith('pkh(') || this.secret.startsWith('sh(')) {
const xpubIndex = Math.max(this.secret.indexOf('xpub'), this.secret.indexOf('ypub'), this.secret.indexOf('zpub'));
const fpAndPath = this.secret.substring(this.secret.indexOf('(') + 1, xpubIndex);
const xpub = this.secret.substring(xpubIndex).replace(/\(|\)/, '');
const pathIndex = fpAndPath.indexOf('/');
const path = 'm' + fpAndPath.substring(pathIndex);
const fp = fpAndPath.substring(0, pathIndex);
this._derivationPath = path;
const mfp = Buffer.from(fp, 'hex').reverse().toString('hex');
this.masterFingerprint = parseInt(mfp, 16);
if (this.secret.startsWith('wpkh(')) {
this.secret = this._xpubToZpub(xpub);
} else {
// nop
this.secret = xpub;
}
}
return this;
}

View File

@ -331,6 +331,33 @@ describe('Watch only wallet', () => {
assert.ok(!w.useWithHardwareWalletEnabled());
});
it('can import wallet descriptor for BIP84 from Sparrow Wallet', async () => {
const payload =
'UR:CRYPTO-OUTPUT/TAADMWTAADDLOLAOWKAXHDCLAXINTOCTFTNNIERONTNYGALYEMAAWPHDAXDIEOWPJEGHKPGMKERHIABDTBLUBNMUMWAAHDCXFHSNBGTSGWSWPTDWVTDIHYHNHPLBBSJEOLSNFZBDIYJLTTPFIMEYTEECKTGSBZBDAHTAADEHOEADAEAOAEAMTAADDYOTADLNCSGHYKAEYKAEYKAOCYFNLBCYGMAXAXAYCYSRRTSPGADLMKBGTD';
const decoder = new BlueURDecoder();
decoder.receivePart(payload);
let data;
if (decoder.isComplete()) {
data = decoder.toString();
}
const w = new WatchOnlyWallet();
w.setSecret(data);
w.init();
assert.ok(w.valid());
assert.strictEqual(w.getMasterFingerprintHex(), '3c7f1a52');
assert.strictEqual(w.getDerivationPath(), "m/84'/0'/0'");
assert.strictEqual(w._getExternalAddressByIndex(0), 'bc1qr0y5c96xtfeulnzxnjl086f2njcmf8qmhenvpp');
assert.strictEqual(
w.getSecret(),
'zpub6rkkMBH6dE8bUPM9MC3WTMYQ3pDYR1kHnNDrqEGY3FotR4EUifR1S4xd7ynwczREFCbfWyk5S4mhzPL8YuGsCSgey1AwH7fk4w9AULpyDYL',
);
});
it('can combine signed PSBT and prepare it for broadcast', async () => {
const w = new WatchOnlyWallet();
w.setSecret('zpub6rjLjQVqVnj7crz9E4QWj4WgczmEseJq22u2B6k2HZr6NE2PQx3ZYg8BnbjN9kCfHymSeMd2EpwpM5iiz5Nrb3TzvddxW2RMcE3VXdVaXHk');