Skip to content

Commit 65fb7bc

Browse files
Merge pull request #8336 from BitGo/T1-142.handle-no-m-prefix
fix(abstract-lightning): gracefully handle no m prefix
2 parents e987553 + 0987e49 commit 65fb7bc

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

modules/abstract-lightning/src/lightning/parseWithdrawPsbt.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ function parseDerivationPath(derivationPath: string): {
1717
addressIndex: number;
1818
} {
1919
const pathSegments = derivationPath.split('/');
20-
const purpose = Number(pathSegments[1].replace(/'/g, ''));
20+
// Paths may or may not include the 'm/' prefix depending on how they were created:
21+
// bip174 binary deserialization omits it (e.g. "84'/0'/0'/1/6"),
22+
// while manually constructed paths typically include it (e.g. "m/84'/0'/0'/1/6").
23+
const offset = pathSegments[0] === 'm' ? 1 : 0;
24+
const purpose = Number(pathSegments[offset].replace(/'/g, ''));
2125
const change = Number(pathSegments[pathSegments.length - 2]);
2226
const addressIndex = Number(pathSegments[pathSegments.length - 1]);
2327
if (purpose !== PURPOSE_WRAPPED_P2WKH && purpose !== PURPOSE_P2WKH && purpose !== PURPOSE_P2TR) {

modules/abstract-lightning/test/unit/lightning/parseWithdrawPsbt.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { validatePsbtForWithdraw } from '../../../src';
2-
import * as utxolib from '@bitgo/utxo-lib';
31
import assert from 'assert';
2+
3+
import * as utxolib from '@bitgo/utxo-lib';
4+
5+
import { validatePsbtForWithdraw } from '../../../src';
46
import { createTestPsbt } from './createPsbt';
57

68
describe('parseWithdrawPsbt', () => {
@@ -42,6 +44,47 @@ describe('parseWithdrawPsbt', () => {
4244
});
4345
});
4446

47+
describe('regression tests with created PSBT from LND (no `m/` prefix)', () => {
48+
const unsignedPsbtHex =
49+
'70736274ff01007d0200000001814f07febb372cf9e303c40938d7f1372cf7f5bd3d43de763be10c1b2ac539200000000000ffffffff028813000000000000220020ac06117832f08e952b16c220dde35dd505b5c54fa45a4eb9f2b040efd27ecb12f86a040000000000160014f1f4753cf0f9171dd3724f17ea511baa49e3c22600000000000100fde70401000000000104e55600b7d0003c6b2be7dc105f6c3499b8d426e92fd56c3dca06728e060c80720000000000fdffffff366f5356038e883dd7dd621d0b340c526018329664ccbeb1a723b0a6f8f312a40000000023220020b4b69dea98d8d5fb32a22a0c9ad208fc28efb44943f8e576d5978d215d784d49fdffffff15978f903d63dac56026cec90fc568fbe6e8a5a8b53bf8b5a34eee3723e63eff0000000000fdffffff58a9e6be1da0f25e39af4c95ed102336a64baf9bc8eb7c1ffca97ff0d0d651ff0000000000fdffffff0145860400000000001600146b618e8d1d709711104f92a857032a8d66f1bf7d0400483045022100d85bc99bf6d610978630518f79eeee025c8ce40eb65f12360f0f47d5b30ca547022036d79384a55bbba00520d626c3ae05a5f341fd7d65f211fa64bfedad5e827c7301483045022100dacce37fbffeb2e9ee198ab3a78cfdf01e34655313482ff75376bb5e2152a05d02205a7d1b7072480368d274de0229bbb9e6f4e4cda202f6750b9a90f8517604413e0169522102dd6b15d5c483080aaeaf3be51c8ee6cd794dbfa65e5c8036fe2c67f0080929702103370c44e05dddfc29fdb418ae583652b4fb7ee62d07562fc1e5b1696caaf3908f2103c6d980811cb03048a7b48f639bbb0575444af1f670af8462bb1c93112af2080a53ae040048304502210094646e8c1c25368ed9cdf5ba10b54a88ce20bb9b452877d0d2f27823897ab9d40220183c80560c1860d22d7ae8aaeee712d930e427252345bddece1a333d345c6d9801483045022100d123c1f5a79d32905f9d725b590160afc8e14bb01cb1a5d258304c6b6673113c0220722003050f57366b3f3486c1fdd5559579f1c67c3d644d31ac6f20bb8022f7d201695221034a2c8e0891e30698aa7015f1210451f1e5336be96bf846cd43528c15b0645b032103e6305f797838299f4188595b1b01bd9fe60db0e4bb9331d957750248d055f299210383c028341d5d273afca5f5265e2c2ee9619c0f1b328956ef3000ab09eccaed3b53ae0400473044022044a43ae38b86c375aa57b3d96386fca52f74a5bc4fb37c77738efa4c0566662202200d1af93d2022c48f3f3cc134d3d75bbcfce30615e53d9246c200c5349a7d978c0147304402207ce3043211d9344dad8f4e2bf848bf1ca151310a188e707663b28475e12099d0022060d2b3533e7c362d39b041102edf17e0486b1aee55a01c7895485b8630daa39c0169522103c71a350e2b06ec9d0710a13f608bb76f35a35fed491fbb5b97280caed4c9590f2103692ea685162a1da7e86e0beeb1ed0331e8d0fbbf2e05f002071b15044c402b782102960087d0d5b8e1a449ed562553ef3149885a930f60c40324165221a7fc7627ea53ae0400483045022100a8382cfa2582abdc77417f9aa53e5ce0e2d31eb5e14b7836961da8c045de3a500220398555b93816ef7ec417bb7555bc51e0477b3c65f83d0951cff2a5040457314e0147304402204f66fd4507bd54cc25028b5660c00fdcfe15ce6121eabff7911e7307d24c7f1102200591e969897109a2372a6b22e41d158d4e0ae53ca0963166ffd8a23e06522ac9016952210282aaf28d528e6b6326880b1539a14a96dcee5d9a0920a31f3581e5ac21a9de462103f84e26e78f2c587821b581761b134f7721e33b1ad97bd1599125ee23369ea1202103da6eddca9c0eaaa08b179c035ed0adb516a592ba1321ffc5cb11dcae8c3a704f53ae0000000001011f45860400000000001600146b618e8d1d709711104f92a857032a8d66f1bf7d010304010000002206038361bfad9b6896c938690df1e67c669ecd7a021b117382bbfa81ff6de8ec153718000000005400008000000080000000800000000000000000000022020376cb4ac899d130e2d655fca6d362fe7c745cc15486849e401f7c21b3d3df37c81800000000540000800000008000000080010000000800000000';
50+
const recipients = [
51+
{
52+
amountSat: 5000n,
53+
address: 'tb1q4srpz7pj7z8f22ckcgsdmc6a65zmt32053dyaw0jkpqwl5n7evfqhc07u4',
54+
},
55+
];
56+
const accounts = [
57+
{
58+
purpose: 49,
59+
coin_type: 0,
60+
account: 0,
61+
xpub: 'upub5EW2cim2FSeBDqote8jcKnBSohtZjPRbw9HsXNototipVVj31AzWjgXUSonbvi7RNALY73rkgXTQY4YUpovrG1WV4ATKXyQpMa1sMnJdCbm',
62+
},
63+
{
64+
purpose: 84,
65+
coin_type: 0,
66+
account: 0,
67+
xpub: 'vpub5Z8kVs9L6fSK4qc1G36VqKdTiSdfCGSzrcz4EYGkywkWX43EJiLcSBEd1XE4fxgXir7Wkwaia4eTwbakngVDHHnToc9qQMBcxerr2U5MHnN',
68+
},
69+
{
70+
purpose: 86,
71+
coin_type: 0,
72+
account: 0,
73+
xpub: 'tpubDCyXDnLqhmTxjcGV2KexrojfJPBept2gWs2mg9V46y6HAp9nLzg3idmESEJ1RDtfaLyVL6x2MoJsv66hQPWrCxoR2jYQbGbHd5FmEa6BfW3',
74+
},
75+
];
76+
77+
it('should parse a valid withdraw PSBT', () => {
78+
validatePsbtForWithdraw(unsignedPsbtHex, network, recipients, accounts);
79+
});
80+
81+
it('should throw for invalid PSBT hex', () => {
82+
assert.throws(() => {
83+
validatePsbtForWithdraw('asdasd', network, recipients, accounts);
84+
}, /ERR_BUFFER_OUT_OF_BOUNDS/);
85+
});
86+
});
87+
4588
describe('test cases with creating psbt on the go', () => {
4689
it('should validate PSBT with P2WPKH (purpose 84) change address', () => {
4790
const { psbt, masterKey } = createTestPsbt({

0 commit comments

Comments
 (0)