Skip to content

Commit 7c6b186

Browse files
fix(psbt): do not use BIP174 tobuffer method in clone
We want to use the toBuffer methods that we implement in child classes of PSBT, not just the bip174 serialization. For the PSBT class, this will automatically default to bip174 serialization.
1 parent d2ea5e4 commit 7c6b186

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/psbt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class Psbt {
152152
}
153153
clone() {
154154
// TODO: more efficient cloning
155-
const res = this.constructor.fromBuffer(this.data.toBuffer(), this.opts);
155+
const res = this.constructor.fromBuffer(this.toBuffer(), this.opts);
156156
res.opts = JSON.parse(JSON.stringify(this.opts));
157157
return res;
158158
}

test/psbt.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,20 @@ describe(`Psbt`, () => {
896896
assert.notStrictEqual(clone.toBase64(), notAClone.toBase64());
897897
assert.strictEqual(psbt.toBase64(), notAClone.toBase64());
898898
});
899+
900+
it('Should use the proper toBuffer method when cloning', () => {
901+
class PsbtSubclass extends Psbt {
902+
toBuffer(): Buffer {
903+
super.setLocktime(1000);
904+
return super.toBuffer();
905+
}
906+
}
907+
const psbt = new PsbtSubclass();
908+
assert.strictEqual(psbt.extractTransaction().locktime, 0);
909+
910+
const clone = psbt.clone();
911+
assert.strictEqual(clone.extractTransaction().locktime, 1000);
912+
});
899913
});
900914

901915
describe('setMaximumFeeRate', () => {

ts_src/psbt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class Psbt {
217217
clone(): Psbt {
218218
// TODO: more efficient cloning
219219
const res = (this.constructor as typeof Psbt).fromBuffer(
220-
this.data.toBuffer(),
220+
this.toBuffer(),
221221
this.opts,
222222
);
223223
res.opts = JSON.parse(JSON.stringify(this.opts));

0 commit comments

Comments
 (0)