Socket
Socket
Sign inDemoInstall

bip174

Package Overview
Dependencies
Maintainers
4
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bip174 - npm Package Compare versions

Comparing version 0.0.9 to 0.0.10

src/lib/utils.d.ts

4

package.json
{
"name": "bip174",
"version": "0.0.9",
"version": "0.0.10",
"description": "",

@@ -26,3 +26,3 @@ "main": "src/lib/psbt.js",

"test": "npm run format:ci && npm run lint && npm run build && npm run unit",
"unit": "tape src/tests/*.js"
"unit": "tape src/tests/*.js src/tests/fromBIP/*.js"
},

@@ -29,0 +29,0 @@ "repository": {

@@ -35,4 +35,5 @@ /// <reference types="node" />

addOutput<T>(outputData: T, allowNoInput?: boolean, transactionOutputAdder?: (output: T, txBuffer: Buffer) => Buffer): this;
clearFinalizedInput(inputIndex: number): this;
combine(...those: this[]): this;
getTransaction(): Buffer;
}

@@ -8,2 +8,3 @@ 'use strict';

const typeFields_1 = require('./typeFields');
const utils_1 = require('./utils');
const {

@@ -70,3 +71,3 @@ globals: {

addNonWitnessUtxoToInput(inputIndex, nonWitnessUtxo) {
const input = checkForInput(this.inputs, inputIndex);
const input = utils_1.checkForInput(this.inputs, inputIndex);
if (input.nonWitnessUtxo || input.witnessUtxo) {

@@ -82,3 +83,3 @@ throw new Error(`Input #${inputIndex} already has a Utxo attribute`);

addWitnessUtxoToInput(inputIndex, witnessUtxo) {
const input = checkForInput(this.inputs, inputIndex);
const input = utils_1.checkForInput(this.inputs, inputIndex);
if (input.nonWitnessUtxo || input.witnessUtxo) {

@@ -96,3 +97,3 @@ throw new Error(`Input #${inputIndex} already has a Utxo attribute`);

addPartialSigToInput(inputIndex, partialSig) {
const input = checkForInput(this.inputs, inputIndex);
const input = utils_1.checkForInput(this.inputs, inputIndex);
if (!interfaces_1.isPartialSig(partialSig)) {

@@ -108,3 +109,3 @@ throw new Error(

addSighashTypeToInput(inputIndex, sighashType) {
const input = checkForInput(this.inputs, inputIndex);
const input = utils_1.checkForInput(this.inputs, inputIndex);
if (typeof sighashType !== 'number') {

@@ -117,3 +118,3 @@ throw new Error('sighashType should be a number');

addRedeemScriptToInput(inputIndex, redeemScript) {
const input = checkForInput(this.inputs, inputIndex);
const input = utils_1.checkForInput(this.inputs, inputIndex);
if (!Buffer.isBuffer(redeemScript)) {

@@ -126,3 +127,3 @@ throw new Error('redeemScript should be a Buffer');

addWitnessScriptToInput(inputIndex, witnessScript) {
const input = checkForInput(this.inputs, inputIndex);
const input = utils_1.checkForInput(this.inputs, inputIndex);
if (!Buffer.isBuffer(witnessScript)) {

@@ -135,3 +136,3 @@ throw new Error('witnessScript should be a Buffer');

addBip32DerivationToInput(inputIndex, bip32Derivation) {
const input = checkForInput(this.inputs, inputIndex);
const input = utils_1.checkForInput(this.inputs, inputIndex);
if (!interfaces_1.isBip32Derivation(bip32Derivation)) {

@@ -148,3 +149,3 @@ throw new Error(

addFinalScriptSigToInput(inputIndex, finalScriptSig) {
const input = checkForInput(this.inputs, inputIndex);
const input = utils_1.checkForInput(this.inputs, inputIndex);
if (!Buffer.isBuffer(finalScriptSig)) {

@@ -157,3 +158,3 @@ throw new Error('finalScriptSig should be a Buffer');

addFinalScriptWitnessToInput(inputIndex, finalScriptWitness) {
const input = checkForInput(this.inputs, inputIndex);
const input = utils_1.checkForInput(this.inputs, inputIndex);
if (!Buffer.isBuffer(finalScriptWitness)) {

@@ -166,3 +167,3 @@ throw new Error('finalScriptWitness should be a Buffer');

addPorCommitmentToInput(inputIndex, porCommitment) {
const input = checkForInput(this.inputs, inputIndex);
const input = utils_1.checkForInput(this.inputs, inputIndex);
if (typeof porCommitment !== 'string') {

@@ -175,3 +176,3 @@ throw new Error('porCommitment should be a string');

addRedeemScriptToOutput(outputIndex, redeemScript) {
const output = checkForOutput(this.outputs, outputIndex);
const output = utils_1.checkForOutput(this.outputs, outputIndex);
if (!Buffer.isBuffer(redeemScript)) {

@@ -184,3 +185,3 @@ throw new Error('redeemScript should be a Buffer');

addWitnessScriptToOutput(outputIndex, witnessScript) {
const output = checkForOutput(this.outputs, outputIndex);
const output = utils_1.checkForOutput(this.outputs, outputIndex);
if (!Buffer.isBuffer(witnessScript)) {

@@ -193,3 +194,3 @@ throw new Error('witnessScript should be a Buffer');

addBip32DerivationToOutput(outputIndex, bip32Derivation) {
const output = checkForOutput(this.outputs, outputIndex);
const output = utils_1.checkForOutput(this.outputs, outputIndex);
if (!interfaces_1.isBip32Derivation(bip32Derivation)) {

@@ -206,6 +207,6 @@ throw new Error(

addKeyValToGlobal(keyVal) {
checkHasKey(
utils_1.checkHasKey(
keyVal,
this.globalMap.keyVals,
getEnumLength(typeFields_1.GlobalTypes),
utils_1.getEnumLength(typeFields_1.GlobalTypes),
);

@@ -216,4 +217,8 @@ this.globalMap.keyVals.push(keyVal);

addKeyValToInput(inputIndex, keyVal) {
const input = checkForInput(this.inputs, inputIndex);
checkHasKey(keyVal, input.keyVals, getEnumLength(typeFields_1.InputTypes));
const input = utils_1.checkForInput(this.inputs, inputIndex);
utils_1.checkHasKey(
keyVal,
input.keyVals,
utils_1.getEnumLength(typeFields_1.InputTypes),
);
input.keyVals.push(keyVal);

@@ -223,7 +228,7 @@ return this;

addKeyValToOutput(outputIndex, keyVal) {
const output = checkForOutput(this.outputs, outputIndex);
checkHasKey(
const output = utils_1.checkForOutput(this.outputs, outputIndex);
utils_1.checkHasKey(
keyVal,
output.keyVals,
getEnumLength(typeFields_1.OutputTypes),
utils_1.getEnumLength(typeFields_1.OutputTypes),
);

@@ -247,3 +252,3 @@ output.keyVals.push(keyVal);

}
insertTxInGlobalMap(newTxBuf, this.globalMap);
utils_1.insertTxInGlobalMap(newTxBuf, this.globalMap);
this.inputs.push({

@@ -273,3 +278,3 @@ keyVals: [],

}
insertTxInGlobalMap(newTxBuf, this.globalMap);
utils_1.insertTxInGlobalMap(newTxBuf, this.globalMap);
this.outputs.push({

@@ -280,2 +285,25 @@ keyVals: [],

}
clearFinalizedInput(inputIndex) {
const input = utils_1.checkForInput(this.inputs, inputIndex);
if (!utils_1.inputIsUncleanFinalized(input)) {
throw new Error(
`Input #${inputIndex} has too much or too little data to clean`,
);
}
for (const key of Object.keys(input)) {
if (
![
'witnessUtxo',
'nonWitnessUtxo',
'finalScriptSig',
'finalScriptWitness',
'keyVals',
].includes(key)
) {
// @ts-ignore
delete input[key];
}
}
return this;
}
combine(...those) {

@@ -304,45 +332,1 @@ // Combine this with those.

exports.Psbt = Psbt;
function insertTxInGlobalMap(txBuf, globalMap) {
const txKeyVals = globalMap.keyVals.filter(
kv => kv.key[0] === typeFields_1.GlobalTypes.UNSIGNED_TX,
);
const len = txKeyVals.length;
const tx = globalMap.unsignedTx;
const hasTx = tx !== undefined ? 1 : 0;
if (len + hasTx !== 1) {
throw new Error(
`Extract Transaction: Expected one Transaction, got ${len + hasTx}`,
);
}
if (tx !== undefined) globalMap.unsignedTx = txBuf;
else txKeyVals[0].value = txBuf;
}
function checkForInput(inputs, inputIndex) {
const input = inputs[inputIndex];
if (input === undefined) throw new Error(`No input #${inputIndex}`);
return input;
}
function checkForOutput(outputs, outputIndex) {
const output = outputs[outputIndex];
if (output === undefined) throw new Error(`No output #${outputIndex}`);
return output;
}
function checkHasKey(checkKeyVal, keyVals, enumLength) {
if (checkKeyVal.key[0] < enumLength) {
throw new Error(
`Use the method for your specific key instead of addKeyVal*`,
);
}
if (keyVals.filter(kv => kv.key.equals(checkKeyVal.key)).length !== 0) {
throw new Error(`Duplicate Key: ${checkKeyVal.key.toString('hex')}`);
}
}
function getEnumLength(myenum) {
let count = 0;
Object.keys(myenum).forEach(val => {
if (Number(isNaN(Number(val)))) {
count++;
}
});
return count;
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc