Skip to content

Commit

Permalink
feat: add hybrid esm and cjs support
Browse files Browse the repository at this point in the history
  • Loading branch information
Nesopie committed Jun 15, 2024
1 parent 25fd684 commit afb9f9a
Show file tree
Hide file tree
Showing 215 changed files with 5,542 additions and 1,487 deletions.
2,711 changes: 1,904 additions & 807 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 16 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@
"name": "bip174",
"version": "2.1.1",
"description": "",
"main": "src/lib/psbt.js",
"types": "src/lib/psbt.d.ts",
"main": "src/cjs/lib/psbt.js",
"module": "src/esm/lib/psbt.js",
"types": "src/cjs/lib/psbt.d.ts",
"exports": {
".": {
"types": "./src/cjs/lib/psbt.d.ts",
"import": "./src/esm/lib/psbt.js",
"require": "./src/cjs/lib/psbt.js"
}
},
"type": "module",
"files": [
"src/lib"
"src/cjs/lib",
"src/esm/lib"
],
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"build": "npm run clean && tsc -p ./tsconfig.json && rimraf 'src/tests/**/*.d.ts' && npm run formatjs",
"build": "npm run clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.cjs.json && npm run formatjs",
"clean": "rimraf src types",
"coverage": "nyc --check-coverage --exclude='src/tests/**/*.js' --reporter=lcov --reporter=text --branches 90 --functions 90 --lines 90 npm run unit",
"flb": "npm run format && npm run lint && npm run build",
Expand All @@ -23,7 +33,7 @@
"prettier": "prettier 'ts_src/**/*.ts' --ignore-path ./.prettierignore",
"prettierjs": "prettier 'src/**/*.js' --ignore-path ./.prettierignore",
"test": "npm run format:ci && npm run lint && npm run build && npm run coverage",
"unit": "tape src/tests/*.js src/tests/fromBIP/*.js"
"unit": "tape src/esm/tests/*.js src/tests/fromBIP/*.js"
},
"repository": {
"type": "git",
Expand All @@ -36,7 +46,7 @@
"nyc": "^14.1.1",
"prettier": "^1.18.2",
"rimraf": "^2.6.3",
"tape": "^4.10.2",
"tape": "^5.3.0",
"tslint": "5.17.0",
"typescript": "3.5.2"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { PsbtAttributes } from '../parser';
import { PsbtAttributes } from '../parser/index.js';
export declare function combine(psbts: PsbtAttributes[]): PsbtAttributes;
8 changes: 4 additions & 4 deletions src/lib/combiner/index.js → src/cjs/lib/combiner/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const parser_1 = require('../parser');
const index_js_1 = require('../parser/index.js');
function combine(psbts) {
const self = psbts[0];
const selfKeyVals = parser_1.psbtToKeyVals(self);
const selfKeyVals = index_js_1.psbtToKeyVals(self);
const others = psbts.slice(1);
if (others.length === 0) throw new Error('Combine: Nothing to combine');
const selfTx = getTx(self);
Expand All @@ -23,7 +23,7 @@ function combine(psbts) {
'Combine: One of the Psbts does not have the same transaction.',
);
}
const otherKeyVals = parser_1.psbtToKeyVals(other);
const otherKeyVals = index_js_1.psbtToKeyVals(other);
const otherGlobalSet = getKeySet(otherKeyVals.globalKeyVals);
otherGlobalSet.forEach(
keyPusher(
Expand Down Expand Up @@ -53,7 +53,7 @@ function combine(psbts) {
),
);
}
return parser_1.psbtFromKeyVals(selfTx, {
return index_js_1.psbtFromKeyVals(selfTx, {
globalMapKeyVals: selfKeyVals.globalKeyVals,
inputKeyVals: selfKeyVals.inputKeyVals,
outputKeyVals: selfKeyVals.outputKeyVals,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const typeFields_1 = require('../../typeFields');
const typeFields_js_1 = require('../../typeFields.js');
const range = n => [...Array(n).keys()];
function decode(keyVal) {
if (keyVal.key[0] !== typeFields_1.GlobalTypes.GLOBAL_XPUB) {
if (keyVal.key[0] !== typeFields_js_1.GlobalTypes.GLOBAL_XPUB) {
throw new Error(
'Decode Error: could not decode globalXpub with key 0x' +
keyVal.key.toString('hex'),
Expand Down Expand Up @@ -36,7 +36,7 @@ function decode(keyVal) {
}
exports.decode = decode;
function encode(data) {
const head = Buffer.from([typeFields_1.GlobalTypes.GLOBAL_XPUB]);
const head = Buffer.from([typeFields_js_1.GlobalTypes.GLOBAL_XPUB]);
const key = Buffer.concat([head, data.extendedPubkey]);
const splitPath = data.path.split('/');
const value = Buffer.allocUnsafe(splitPath.length * 4);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const typeFields_1 = require('../../typeFields');
const typeFields_js_1 = require('../../typeFields.js');
function encode(data) {
return {
key: Buffer.from([typeFields_1.GlobalTypes.UNSIGNED_TX]),
key: Buffer.from([typeFields_js_1.GlobalTypes.UNSIGNED_TX]),
value: data.toBuffer(),
};
}
Expand Down
109 changes: 109 additions & 0 deletions src/cjs/lib/converter/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/// <reference types="node" />
import * as globalXpub from './global/globalXpub.js';
import * as unsignedTx from './global/unsignedTx.js';
import * as finalScriptSig from './input/finalScriptSig.js';
import * as finalScriptWitness from './input/finalScriptWitness.js';
import * as nonWitnessUtxo from './input/nonWitnessUtxo.js';
import * as partialSig from './input/partialSig.js';
import * as porCommitment from './input/porCommitment.js';
import * as sighashType from './input/sighashType.js';
import * as tapKeySig from './input/tapKeySig.js';
import * as tapLeafScript from './input/tapLeafScript.js';
import * as tapMerkleRoot from './input/tapMerkleRoot.js';
import * as tapScriptSig from './input/tapScriptSig.js';
import * as witnessUtxo from './input/witnessUtxo.js';
import * as tapTree from './output/tapTree.js';
declare const globals: {
unsignedTx: typeof unsignedTx;
globalXpub: typeof globalXpub;
checkPubkey: (keyVal: import("../interfaces.js").KeyValue) => Buffer | undefined;
};
declare const inputs: {
nonWitnessUtxo: typeof nonWitnessUtxo;
partialSig: typeof partialSig;
sighashType: typeof sighashType;
finalScriptSig: typeof finalScriptSig;
finalScriptWitness: typeof finalScriptWitness;
porCommitment: typeof porCommitment;
witnessUtxo: typeof witnessUtxo;
bip32Derivation: {
decode: (keyVal: import("../interfaces.js").KeyValue) => import("../interfaces.js").Bip32Derivation;
encode: (data: import("../interfaces.js").Bip32Derivation) => import("../interfaces.js").KeyValue;
check: (data: any) => data is import("../interfaces.js").Bip32Derivation;
expected: string;
canAddToArray: (array: import("../interfaces.js").Bip32Derivation[], item: import("../interfaces.js").Bip32Derivation, dupeSet: Set<string>) => boolean;
};
redeemScript: {
decode: (keyVal: import("../interfaces.js").KeyValue) => Buffer;
encode: (data: Buffer) => import("../interfaces.js").KeyValue;
check: (data: any) => data is Buffer;
expected: string;
canAdd: (currentData: any, newData: any) => boolean;
};
witnessScript: {
decode: (keyVal: import("../interfaces.js").KeyValue) => Buffer;
encode: (data: Buffer) => import("../interfaces.js").KeyValue;
check: (data: any) => data is Buffer;
expected: string;
canAdd: (currentData: any, newData: any) => boolean;
};
checkPubkey: (keyVal: import("../interfaces.js").KeyValue) => Buffer | undefined;
tapKeySig: typeof tapKeySig;
tapScriptSig: typeof tapScriptSig;
tapLeafScript: typeof tapLeafScript;
tapBip32Derivation: {
decode: (keyVal: import("../interfaces.js").KeyValue) => import("../interfaces.js").TapBip32Derivation;
encode: (data: import("../interfaces.js").TapBip32Derivation) => import("../interfaces.js").KeyValue;
check: (data: any) => data is import("../interfaces.js").TapBip32Derivation;
expected: string;
canAddToArray: (array: import("../interfaces.js").TapBip32Derivation[], item: import("../interfaces.js").TapBip32Derivation, dupeSet: Set<string>) => boolean;
};
tapInternalKey: {
decode: (keyVal: import("../interfaces.js").KeyValue) => Buffer;
encode: (data: Buffer) => import("../interfaces.js").KeyValue;
check: (data: any) => data is Buffer;
expected: string;
canAdd: (currentData: any, newData: any) => boolean;
};
tapMerkleRoot: typeof tapMerkleRoot;
};
declare const outputs: {
bip32Derivation: {
decode: (keyVal: import("../interfaces.js").KeyValue) => import("../interfaces.js").Bip32Derivation;
encode: (data: import("../interfaces.js").Bip32Derivation) => import("../interfaces.js").KeyValue;
check: (data: any) => data is import("../interfaces.js").Bip32Derivation;
expected: string;
canAddToArray: (array: import("../interfaces.js").Bip32Derivation[], item: import("../interfaces.js").Bip32Derivation, dupeSet: Set<string>) => boolean;
};
redeemScript: {
decode: (keyVal: import("../interfaces.js").KeyValue) => Buffer;
encode: (data: Buffer) => import("../interfaces.js").KeyValue;
check: (data: any) => data is Buffer;
expected: string;
canAdd: (currentData: any, newData: any) => boolean;
};
witnessScript: {
decode: (keyVal: import("../interfaces.js").KeyValue) => Buffer;
encode: (data: Buffer) => import("../interfaces.js").KeyValue;
check: (data: any) => data is Buffer;
expected: string;
canAdd: (currentData: any, newData: any) => boolean;
};
checkPubkey: (keyVal: import("../interfaces.js").KeyValue) => Buffer | undefined;
tapBip32Derivation: {
decode: (keyVal: import("../interfaces.js").KeyValue) => import("../interfaces.js").TapBip32Derivation;
encode: (data: import("../interfaces.js").TapBip32Derivation) => import("../interfaces.js").KeyValue;
check: (data: any) => data is import("../interfaces.js").TapBip32Derivation;
expected: string;
canAddToArray: (array: import("../interfaces.js").TapBip32Derivation[], item: import("../interfaces.js").TapBip32Derivation, dupeSet: Set<string>) => boolean;
};
tapTree: typeof tapTree;
tapInternalKey: {
decode: (keyVal: import("../interfaces.js").KeyValue) => Buffer;
encode: (data: Buffer) => import("../interfaces.js").KeyValue;
check: (data: any) => data is Buffer;
expected: string;
canAdd: (currentData: any, newData: any) => boolean;
};
};
export { globals, inputs, outputs };
100 changes: 100 additions & 0 deletions src/cjs/lib/converter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use strict';
var __importStar =
(this && this.__importStar) ||
function(mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null)
for (var k in mod)
if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result['default'] = mod;
return result;
};
Object.defineProperty(exports, '__esModule', { value: true });
const typeFields_js_1 = require('../typeFields.js');
const globalXpub = __importStar(require('./global/globalXpub.js'));
const unsignedTx = __importStar(require('./global/unsignedTx.js'));
const finalScriptSig = __importStar(require('./input/finalScriptSig.js'));
const finalScriptWitness = __importStar(
require('./input/finalScriptWitness.js'),
);
const nonWitnessUtxo = __importStar(require('./input/nonWitnessUtxo.js'));
const partialSig = __importStar(require('./input/partialSig.js'));
const porCommitment = __importStar(require('./input/porCommitment.js'));
const sighashType = __importStar(require('./input/sighashType.js'));
const tapKeySig = __importStar(require('./input/tapKeySig.js'));
const tapLeafScript = __importStar(require('./input/tapLeafScript.js'));
const tapMerkleRoot = __importStar(require('./input/tapMerkleRoot.js'));
const tapScriptSig = __importStar(require('./input/tapScriptSig.js'));
const witnessUtxo = __importStar(require('./input/witnessUtxo.js'));
const tapTree = __importStar(require('./output/tapTree.js'));
const bip32Derivation = __importStar(require('./shared/bip32Derivation.js'));
const checkPubkey = __importStar(require('./shared/checkPubkey.js'));
const redeemScript = __importStar(require('./shared/redeemScript.js'));
const tapBip32Derivation = __importStar(
require('./shared/tapBip32Derivation.js'),
);
const tapInternalKey = __importStar(require('./shared/tapInternalKey.js'));
const witnessScript = __importStar(require('./shared/witnessScript.js'));
const globals = {
unsignedTx,
globalXpub,
// pass an Array of key bytes that require pubkey beside the key
checkPubkey: checkPubkey.makeChecker([]),
};
exports.globals = globals;
const inputs = {
nonWitnessUtxo,
partialSig,
sighashType,
finalScriptSig,
finalScriptWitness,
porCommitment,
witnessUtxo,
bip32Derivation: bip32Derivation.makeConverter(
typeFields_js_1.InputTypes.BIP32_DERIVATION,
),
redeemScript: redeemScript.makeConverter(
typeFields_js_1.InputTypes.REDEEM_SCRIPT,
),
witnessScript: witnessScript.makeConverter(
typeFields_js_1.InputTypes.WITNESS_SCRIPT,
),
checkPubkey: checkPubkey.makeChecker([
typeFields_js_1.InputTypes.PARTIAL_SIG,
typeFields_js_1.InputTypes.BIP32_DERIVATION,
]),
tapKeySig,
tapScriptSig,
tapLeafScript,
tapBip32Derivation: tapBip32Derivation.makeConverter(
typeFields_js_1.InputTypes.TAP_BIP32_DERIVATION,
),
tapInternalKey: tapInternalKey.makeConverter(
typeFields_js_1.InputTypes.TAP_INTERNAL_KEY,
),
tapMerkleRoot,
};
exports.inputs = inputs;
const outputs = {
bip32Derivation: bip32Derivation.makeConverter(
typeFields_js_1.OutputTypes.BIP32_DERIVATION,
),
redeemScript: redeemScript.makeConverter(
typeFields_js_1.OutputTypes.REDEEM_SCRIPT,
),
witnessScript: witnessScript.makeConverter(
typeFields_js_1.OutputTypes.WITNESS_SCRIPT,
),
checkPubkey: checkPubkey.makeChecker([
typeFields_js_1.OutputTypes.BIP32_DERIVATION,
]),
tapBip32Derivation: tapBip32Derivation.makeConverter(
typeFields_js_1.OutputTypes.TAP_BIP32_DERIVATION,
),
tapTree,
tapInternalKey: tapInternalKey.makeConverter(
typeFields_js_1.OutputTypes.TAP_INTERNAL_KEY,
),
};
exports.outputs = outputs;
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const typeFields_1 = require('../../typeFields');
const typeFields_js_1 = require('../../typeFields.js');
function decode(keyVal) {
if (keyVal.key[0] !== typeFields_1.InputTypes.FINAL_SCRIPTSIG) {
if (keyVal.key[0] !== typeFields_js_1.InputTypes.FINAL_SCRIPTSIG) {
throw new Error(
'Decode Error: could not decode finalScriptSig with key 0x' +
keyVal.key.toString('hex'),
Expand All @@ -12,7 +12,7 @@ function decode(keyVal) {
}
exports.decode = decode;
function encode(data) {
const key = Buffer.from([typeFields_1.InputTypes.FINAL_SCRIPTSIG]);
const key = Buffer.from([typeFields_js_1.InputTypes.FINAL_SCRIPTSIG]);
return {
key,
value: data,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const typeFields_1 = require('../../typeFields');
const typeFields_js_1 = require('../../typeFields.js');
function decode(keyVal) {
if (keyVal.key[0] !== typeFields_1.InputTypes.FINAL_SCRIPTWITNESS) {
if (keyVal.key[0] !== typeFields_js_1.InputTypes.FINAL_SCRIPTWITNESS) {
throw new Error(
'Decode Error: could not decode finalScriptWitness with key 0x' +
keyVal.key.toString('hex'),
Expand All @@ -12,7 +12,7 @@ function decode(keyVal) {
}
exports.decode = decode;
function encode(data) {
const key = Buffer.from([typeFields_1.InputTypes.FINAL_SCRIPTWITNESS]);
const key = Buffer.from([typeFields_js_1.InputTypes.FINAL_SCRIPTWITNESS]);
return {
key,
value: data,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const typeFields_1 = require('../../typeFields');
const typeFields_js_1 = require('../../typeFields.js');
function decode(keyVal) {
if (keyVal.key[0] !== typeFields_1.InputTypes.NON_WITNESS_UTXO) {
if (keyVal.key[0] !== typeFields_js_1.InputTypes.NON_WITNESS_UTXO) {
throw new Error(
'Decode Error: could not decode nonWitnessUtxo with key 0x' +
keyVal.key.toString('hex'),
Expand All @@ -13,7 +13,7 @@ function decode(keyVal) {
exports.decode = decode;
function encode(data) {
return {
key: Buffer.from([typeFields_1.InputTypes.NON_WITNESS_UTXO]),
key: Buffer.from([typeFields_js_1.InputTypes.NON_WITNESS_UTXO]),
value: data,
};
}
Expand Down
File renamed without changes.
Loading

0 comments on commit afb9f9a

Please sign in to comment.