Skip to content

Commit 8200432

Browse files
Merge pull request #6465 from BitGo/BTC-2143.fixToPlainObjectFromPsbt
fix(utxo-core): improve property extraction in toPlainObject
2 parents e612b96 + f0cad57 commit 8200432

17 files changed

+2187
-49
lines changed

modules/utxo-core/src/testutil/descriptor/psbt.utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export function toPlainObjectFromPsbt(v: utxolib.Psbt): unknown {
1212
matchPath(path, ['__CACHE']) ||
1313
matchPath(path, ['opts', 'network']) ||
1414
matchPath(path, ['data', 'globalMap', 'unsignedTx', 'tx', 'network']) ||
15+
matchPath(path, ['tx', 'network']) ||
1516
matchPath(path, ['network'])
1617
);
1718
},

modules/utxo-core/src/testutil/toPlainObject.utils.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,25 @@ function toPlainEntries(
3535
return [[key, toPlainObject(value, opts, [...path, key])]];
3636
}
3737

38+
function getAllDescriptors(v: unknown): PropertyDescriptorMap {
39+
if (v === null || typeof v !== 'object') {
40+
return {};
41+
}
42+
const descriptors: PropertyDescriptorMap = Object.getOwnPropertyDescriptors(v);
43+
const proto = Object.getPrototypeOf(v);
44+
if (proto) {
45+
Object.assign(descriptors, getAllDescriptors(proto));
46+
}
47+
return descriptors;
48+
}
49+
3850
function toPlainObjectFromPropertyDescriptors(v: unknown, opts: ToPlainObjectOpts, path: PathElement[]) {
39-
const descriptors = Object.getOwnPropertyDescriptors(v);
51+
const descriptors = getAllDescriptors(v);
4052
return Object.fromEntries(
4153
Object.entries(descriptors).flatMap(([key, descriptor]) => {
54+
if (typeof descriptor.value === 'function') {
55+
return [];
56+
}
4257
if (descriptor.value !== undefined) {
4358
return toPlainEntries(key, descriptor.value, opts, path);
4459
}

modules/utxo-core/test/descriptor/psbt/fixtures/Tr1Of3-NoKeyPath-Tree-Plain-TapLeafScript0.psbtStages.json

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,64 @@
195195
},
196196
"opts": {
197197
"maximumFeeRate": 5000
198-
}
198+
},
199+
"tx": {
200+
"version": 2,
201+
"locktime": 0,
202+
"ins": [
203+
{
204+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
205+
"index": 0,
206+
"script": "",
207+
"sequence": 4294967293,
208+
"witness": []
209+
},
210+
{
211+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
212+
"index": 1,
213+
"script": "",
214+
"sequence": 4294967293,
215+
"witness": []
216+
}
217+
],
218+
"outs": [
219+
{
220+
"value": "400000",
221+
"script": "002064d5e2fcca2e107ac79ef06388fa61e93ceaf31533a8d2a8c6e95423a1b1fdb8"
222+
},
223+
{
224+
"value": "400000",
225+
"script": "5120e8f110b8eee45bf2fffc239eefec0a7cd0bdaaada8d5eca9c1c822d900e75b3b"
226+
}
227+
]
228+
},
229+
"inputCount": 2,
230+
"version": 2,
231+
"locktime": 0,
232+
"txInputs": [
233+
{
234+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
235+
"index": 0,
236+
"sequence": 4294967293
237+
},
238+
{
239+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
240+
"index": 1,
241+
"sequence": 4294967293
242+
}
243+
],
244+
"txOutputs": [
245+
{
246+
"script": "002064d5e2fcca2e107ac79ef06388fa61e93ceaf31533a8d2a8c6e95423a1b1fdb8",
247+
"value": "400000",
248+
"address": "bc1qvn279lx29cg843u77p3c37npay7w4uc4xw5d92xxa92z8gd3lkuq4w8477"
249+
},
250+
{
251+
"script": "5120e8f110b8eee45bf2fffc239eefec0a7cd0bdaaada8d5eca9c1c822d900e75b3b",
252+
"value": "400000",
253+
"address": "bc1parc3pw8wu3dl9lluyw0wlmq20ngtm24d4r27e2wpeq3djq88tvashcams9"
254+
}
255+
]
199256
},
200257
"parsed": {
201258
"inputs": [
@@ -444,7 +501,64 @@
444501
},
445502
"opts": {
446503
"maximumFeeRate": 5000
447-
}
504+
},
505+
"tx": {
506+
"version": 2,
507+
"locktime": 0,
508+
"ins": [
509+
{
510+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
511+
"index": 0,
512+
"script": "",
513+
"sequence": 4294967293,
514+
"witness": []
515+
},
516+
{
517+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
518+
"index": 1,
519+
"script": "",
520+
"sequence": 4294967293,
521+
"witness": []
522+
}
523+
],
524+
"outs": [
525+
{
526+
"value": "400000",
527+
"script": "002064d5e2fcca2e107ac79ef06388fa61e93ceaf31533a8d2a8c6e95423a1b1fdb8"
528+
},
529+
{
530+
"value": "400000",
531+
"script": "5120e8f110b8eee45bf2fffc239eefec0a7cd0bdaaada8d5eca9c1c822d900e75b3b"
532+
}
533+
]
534+
},
535+
"inputCount": 2,
536+
"version": 2,
537+
"locktime": 0,
538+
"txInputs": [
539+
{
540+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
541+
"index": 0,
542+
"sequence": 4294967293
543+
},
544+
{
545+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
546+
"index": 1,
547+
"sequence": 4294967293
548+
}
549+
],
550+
"txOutputs": [
551+
{
552+
"script": "002064d5e2fcca2e107ac79ef06388fa61e93ceaf31533a8d2a8c6e95423a1b1fdb8",
553+
"value": "400000",
554+
"address": "bc1qvn279lx29cg843u77p3c37npay7w4uc4xw5d92xxa92z8gd3lkuq4w8477"
555+
},
556+
{
557+
"script": "5120e8f110b8eee45bf2fffc239eefec0a7cd0bdaaada8d5eca9c1c822d900e75b3b",
558+
"value": "400000",
559+
"address": "bc1parc3pw8wu3dl9lluyw0wlmq20ngtm24d4r27e2wpeq3djq88tvashcams9"
560+
}
561+
]
448562
},
449563
"parsed": {
450564
"inputs": [

modules/utxo-core/test/descriptor/psbt/fixtures/Tr1Of3-NoKeyPath-Tree-Plain-TapLeafScript1.psbtStages.json

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,64 @@
195195
},
196196
"opts": {
197197
"maximumFeeRate": 5000
198-
}
198+
},
199+
"tx": {
200+
"version": 2,
201+
"locktime": 0,
202+
"ins": [
203+
{
204+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
205+
"index": 0,
206+
"script": "",
207+
"sequence": 4294967293,
208+
"witness": []
209+
},
210+
{
211+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
212+
"index": 1,
213+
"script": "",
214+
"sequence": 4294967293,
215+
"witness": []
216+
}
217+
],
218+
"outs": [
219+
{
220+
"value": "400000",
221+
"script": "002064d5e2fcca2e107ac79ef06388fa61e93ceaf31533a8d2a8c6e95423a1b1fdb8"
222+
},
223+
{
224+
"value": "400000",
225+
"script": "5120e8f110b8eee45bf2fffc239eefec0a7cd0bdaaada8d5eca9c1c822d900e75b3b"
226+
}
227+
]
228+
},
229+
"inputCount": 2,
230+
"version": 2,
231+
"locktime": 0,
232+
"txInputs": [
233+
{
234+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
235+
"index": 0,
236+
"sequence": 4294967293
237+
},
238+
{
239+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
240+
"index": 1,
241+
"sequence": 4294967293
242+
}
243+
],
244+
"txOutputs": [
245+
{
246+
"script": "002064d5e2fcca2e107ac79ef06388fa61e93ceaf31533a8d2a8c6e95423a1b1fdb8",
247+
"value": "400000",
248+
"address": "bc1qvn279lx29cg843u77p3c37npay7w4uc4xw5d92xxa92z8gd3lkuq4w8477"
249+
},
250+
{
251+
"script": "5120e8f110b8eee45bf2fffc239eefec0a7cd0bdaaada8d5eca9c1c822d900e75b3b",
252+
"value": "400000",
253+
"address": "bc1parc3pw8wu3dl9lluyw0wlmq20ngtm24d4r27e2wpeq3djq88tvashcams9"
254+
}
255+
]
199256
},
200257
"parsed": {
201258
"inputs": [
@@ -444,7 +501,64 @@
444501
},
445502
"opts": {
446503
"maximumFeeRate": 5000
447-
}
504+
},
505+
"tx": {
506+
"version": 2,
507+
"locktime": 0,
508+
"ins": [
509+
{
510+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
511+
"index": 0,
512+
"script": "",
513+
"sequence": 4294967293,
514+
"witness": []
515+
},
516+
{
517+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
518+
"index": 1,
519+
"script": "",
520+
"sequence": 4294967293,
521+
"witness": []
522+
}
523+
],
524+
"outs": [
525+
{
526+
"value": "400000",
527+
"script": "002064d5e2fcca2e107ac79ef06388fa61e93ceaf31533a8d2a8c6e95423a1b1fdb8"
528+
},
529+
{
530+
"value": "400000",
531+
"script": "5120e8f110b8eee45bf2fffc239eefec0a7cd0bdaaada8d5eca9c1c822d900e75b3b"
532+
}
533+
]
534+
},
535+
"inputCount": 2,
536+
"version": 2,
537+
"locktime": 0,
538+
"txInputs": [
539+
{
540+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
541+
"index": 0,
542+
"sequence": 4294967293
543+
},
544+
{
545+
"hash": "0101010101010101010101010101010101010101010101010101010101010101",
546+
"index": 1,
547+
"sequence": 4294967293
548+
}
549+
],
550+
"txOutputs": [
551+
{
552+
"script": "002064d5e2fcca2e107ac79ef06388fa61e93ceaf31533a8d2a8c6e95423a1b1fdb8",
553+
"value": "400000",
554+
"address": "bc1qvn279lx29cg843u77p3c37npay7w4uc4xw5d92xxa92z8gd3lkuq4w8477"
555+
},
556+
{
557+
"script": "5120e8f110b8eee45bf2fffc239eefec0a7cd0bdaaada8d5eca9c1c822d900e75b3b",
558+
"value": "400000",
559+
"address": "bc1parc3pw8wu3dl9lluyw0wlmq20ngtm24d4r27e2wpeq3djq88tvashcams9"
560+
}
561+
]
448562
},
449563
"parsed": {
450564
"inputs": [

0 commit comments

Comments
 (0)