Skip to content

Commit d94d4a2

Browse files
authored
Merge pull request #40 from BitGo/utxo_psbt
Prep for BitGo PSBTs
2 parents 6624bd4 + e5add80 commit d94d4a2

File tree

16 files changed

+95
-31
lines changed

16 files changed

+95
-31
lines changed

package-lock.json

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"mocha:ts": "mocha --recursive --require test/ts-node-register",
3434
"nobuild:coverage-report": "nyc report --reporter=lcov",
3535
"nobuild:coverage-html": "nyc report --reporter=html",
36-
"nobuild:coverage": "npm run build:tests && nyc --check-coverage --branches 90 --functions 90 --lines 90 mocha && npm run clean:jstests",
36+
"nobuild:coverage": "npm run build:tests && nyc --check-coverage --branches 89 --functions 90 --lines 90 mocha && npm run clean:jstests",
3737
"nobuild:integration": "npm run mocha:ts -- --timeout 50000 'test/integration/*.ts'",
3838
"nobuild:unit": "npm run mocha:ts -- 'test/*.ts'",
3939
"prettier": "prettier \"ts_src/**/*.ts\" \"test/**/*.ts\" --ignore-path ./.prettierignore",
@@ -50,7 +50,7 @@
5050
],
5151
"dependencies": {
5252
"bech32": "^2.0.0",
53-
"bip174": "git+https://github.com/brandonblack/bip174#c2ca5fa9ccdb6244b9617b1d78d77c82c2403185",
53+
"bip174": "npm:@bitgo/bip174@3.0.0",
5454
"bs58check": "^2.1.2",
5555
"create-hash": "^1.1.0",
5656
"fastpriorityqueue": "^0.7.1",
@@ -77,7 +77,7 @@
7777
"bn.js": "^4.11.8",
7878
"bs58": "^4.0.0",
7979
"dhttp": "^3.0.0",
80-
"ecpair": "^2.0.1",
80+
"ecpair": "git+https://github.com/brandonblack/ecpair#2fd5696110aa8bba136c5a5240019cd1ced16431",
8181
"hoodwink": "^2.0.0",
8282
"minimaldata": "^1.0.2",
8383
"mocha": "^10.0.0",
@@ -87,7 +87,7 @@
8787
"randombytes": "^2.1.0",
8888
"regtest-client": "0.2.0",
8989
"rimraf": "^2.6.3",
90-
"tiny-secp256k1": "^2.2.0",
90+
"tiny-secp256k1": "^2.2.1",
9191
"ts-node": "^8.3.0",
9292
"tslint": "^6.1.3",
9393
"typescript": "^4.4.4"

src/address.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ function _toFutureSegwitAddress(output, network) {
3434
if (output[1] !== data.length)
3535
throw new TypeError('Invalid script for segwit address');
3636
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
37+
if (!network.bech32) {
38+
throw new TypeError("Network doesn't support native segwit");
39+
}
3740
return toBech32(data, version, network.bech32);
3841
}
3942
function fromBase58Check(address) {

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as ScriptSignature from './script_signature';
88
export { ScriptSignature, address, crypto, networks, payments, script, taproot, };
99
export { Block } from './block';
1010
export { TaggedHashPrefix } from './crypto';
11-
export { Psbt, PsbtTxInput, PsbtTxOutput, Signer, SignerAsync, HDSigner, HDSignerAsync, } from './psbt';
11+
export { Psbt, PsbtTransaction, PsbtTxInput, PsbtTxOutput, Signer, SignerAsync, HDSigner, HDSignerAsync, } from './psbt';
1212
export { OPS as opcodes } from './ops';
1313
export { Transaction } from './transaction';
1414
export { Network } from './networks';

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
Object.defineProperty(exports, '__esModule', { value: true });
3-
exports.Transaction = exports.opcodes = exports.Psbt = exports.Block = exports.taproot = exports.script = exports.payments = exports.networks = exports.crypto = exports.address = exports.ScriptSignature = void 0;
3+
exports.Transaction = exports.opcodes = exports.PsbtTransaction = exports.Psbt = exports.Block = exports.taproot = exports.script = exports.payments = exports.networks = exports.crypto = exports.address = exports.ScriptSignature = void 0;
44
const address = require('./address');
55
exports.address = address;
66
const crypto = require('./crypto');
@@ -29,6 +29,12 @@ Object.defineProperty(exports, 'Psbt', {
2929
return psbt_1.Psbt;
3030
},
3131
});
32+
Object.defineProperty(exports, 'PsbtTransaction', {
33+
enumerable: true,
34+
get: function() {
35+
return psbt_1.PsbtTransaction;
36+
},
37+
});
3238
var ops_1 = require('./ops');
3339
Object.defineProperty(exports, 'opcodes', {
3440
enumerable: true,

src/networks.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export interface Network {
22
messagePrefix: string;
3-
bech32: string;
3+
bech32?: string;
44
bip32: Bip32;
55
pubKeyHash: number;
66
scriptHash: number;

src/payments/p2wpkh.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ function p2wpkh(a, opts) {
4343
});
4444
const network = a.network || networks_1.bitcoin;
4545
const o = { name: 'p2wpkh', network };
46+
if (!network.bech32) {
47+
throw new TypeError("Network doesn't support native segwit");
48+
}
4649
lazy.prop(o, 'address', () => {
4750
if (!o.hash) return;
4851
const words = bech32_1.bech32.toWords(o.hash);

src/payments/p2wsh.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ function p2wsh(a, opts) {
7272
if (!network) {
7373
network = (a.redeem && a.redeem.network) || networks_1.bitcoin;
7474
}
75+
if (!network.bech32) {
76+
throw new TypeError("Network doesn't support native segwit");
77+
}
7578
const o = { network };
7679
lazy.prop(o, 'address', () => {
7780
if (!o.hash) return;

src/psbt.d.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="node" />
22
import { Psbt as PsbtBase } from 'bip174';
3-
import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate } from 'bip174/src/lib/interfaces';
3+
import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate, Transaction as ITransaction } from 'bip174/src/lib/interfaces';
44
import { Network } from './networks';
55
import { Transaction } from './transaction';
66
export interface TransactionInput {
@@ -163,6 +163,24 @@ export interface SignerAsync {
163163
sign(hash: Buffer, lowR?: boolean): Promise<Buffer>;
164164
getPublicKey?(): Buffer;
165165
}
166+
/**
167+
* This class implements the Transaction interface from bip174 library.
168+
* It contains a bitcoinjs-lib Transaction object.
169+
*/
170+
export declare class PsbtTransaction implements ITransaction {
171+
tx: Transaction<bigint>;
172+
constructor({ tx, buffer, }?: {
173+
tx?: Transaction<bigint>;
174+
buffer?: Buffer;
175+
});
176+
getInputOutputCounts(): {
177+
inputCount: number;
178+
outputCount: number;
179+
};
180+
addInput(input: any): void;
181+
addOutput(output: any): void;
182+
toBuffer(): Buffer;
183+
}
166184
/**
167185
* This function must do two things:
168186
* 1. Check if the `input` can be finalized. If it can not be finalized, throw.

src/psbt.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
Object.defineProperty(exports, '__esModule', { value: true });
3-
exports.Psbt = void 0;
3+
exports.PsbtTransaction = exports.Psbt = void 0;
44
const bip174_1 = require('bip174');
55
const varuint = require('bip174/src/lib/converter/varint');
66
const utils_1 = require('bip174/src/lib/utils');
@@ -613,14 +613,23 @@ exports.Psbt = Psbt;
613613
* It takes the "transaction buffer" portion of the psbt buffer and returns a
614614
* Transaction (From the bip174 library) interface.
615615
*/
616-
const transactionFromBuffer = buffer => new PsbtTransaction(buffer);
616+
const transactionFromBuffer = buffer => new PsbtTransaction({ buffer });
617617
/**
618618
* This class implements the Transaction interface from bip174 library.
619619
* It contains a bitcoinjs-lib Transaction object.
620620
*/
621621
class PsbtTransaction {
622-
constructor(buffer = Buffer.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0])) {
623-
this.tx = transaction_1.Transaction.fromBuffer(buffer, undefined, 'bigint');
622+
constructor({ tx, buffer } = {}) {
623+
if (tx !== undefined) {
624+
this.tx = tx;
625+
} else {
626+
buffer = buffer || Buffer.from([2, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
627+
this.tx = transaction_1.Transaction.fromBuffer(
628+
buffer,
629+
undefined,
630+
'bigint',
631+
);
632+
}
624633
checkTxEmpty(this.tx);
625634
Object.defineProperty(this, 'tx', {
626635
enumerable: false,
@@ -663,6 +672,7 @@ class PsbtTransaction {
663672
return this.tx.toBuffer();
664673
}
665674
}
675+
exports.PsbtTransaction = PsbtTransaction;
666676
function canFinalize(input, script, scriptType) {
667677
switch (scriptType) {
668678
case 'pubkey':

0 commit comments

Comments
 (0)