Browse Source

Some type fixes and js build

master
junderw 5 years ago
parent
commit
dc60342c7e
No known key found for this signature in database GPG Key ID: A9273B5AD3E47B45
  1. 23
      src/index.js
  2. 20
      ts_src/index.ts

23
src/index.js

@ -1,7 +1,6 @@
'use strict'; 'use strict';
Object.defineProperty(exports, '__esModule', { value: true }); Object.defineProperty(exports, '__esModule', { value: true });
const bitcoinjs_lib_1 = require('bitcoinjs-lib'); const bitcoinjs_lib_1 = require('bitcoinjs-lib');
const bitcoinjs_lib_2 = require('bitcoinjs-lib');
async function requestPayjoinWithCustomRemoteCall(psbt, remoteCall) { async function requestPayjoinWithCustomRemoteCall(psbt, remoteCall) {
const clonedPsbt = psbt.clone(); const clonedPsbt = psbt.clone();
clonedPsbt.finalizeAllInputs(); clonedPsbt.finalizeAllInputs();
@ -15,12 +14,6 @@ async function requestPayjoinWithCustomRemoteCall(psbt, remoteCall) {
delete clonedPsbt.data.globalMap.globalXpub; delete clonedPsbt.data.globalMap.globalXpub;
const payjoinPsbt = await remoteCall(clonedPsbt); const payjoinPsbt = await remoteCall(clonedPsbt);
if (!payjoinPsbt) throw new Error("We did not get the receiver's PSBT"); if (!payjoinPsbt) throw new Error("We did not get the receiver's PSBT");
// no inputs were added?
if (clonedPsbt.inputCount <= payjoinPsbt.inputCount) {
throw new Error(
"There were less inputs than before in the receiver's PSBT",
);
}
if ( if (
payjoinPsbt.data.globalMap.globalXpub && payjoinPsbt.data.globalMap.globalXpub &&
payjoinPsbt.data.globalMap.globalXpub.length > 0 payjoinPsbt.data.globalMap.globalXpub.length > 0
@ -62,7 +55,7 @@ async function requestPayjoinWithCustomRemoteCall(psbt, remoteCall) {
// Can we assume output will contain redeemScript and witnessScript? // Can we assume output will contain redeemScript and witnessScript?
// If so, we could decompile scriptPubkey, RS, and WS, and search for // If so, we could decompile scriptPubkey, RS, and WS, and search for
// the pubkey and its hash160. // the pubkey and its hash160.
bitcoinjs_lib_2.payments.p2wpkh({ bitcoinjs_lib_1.payments.p2wpkh({
pubkey: originalOutput.bip32Derivation[0].pubkey, pubkey: originalOutput.bip32Derivation[0].pubkey,
}).output, }).output,
) )
@ -70,8 +63,18 @@ async function requestPayjoinWithCustomRemoteCall(psbt, remoteCall) {
payjoinPsbt.updateOutput(index, originalOutput); payjoinPsbt.updateOutput(index, originalOutput);
}); });
} }
// TODO: check payjoinPsbt.version == psbt.version if (
// TODO: check payjoinPsbt.locktime == psbt.locktime getGlobalTransaction(payjoinPsbt).version !==
getGlobalTransaction(psbt).version
) {
throw new Error('The version field of the transaction has been modified');
}
if (
getGlobalTransaction(payjoinPsbt).locktime !==
getGlobalTransaction(psbt).locktime
) {
throw new Error('The LockTime field of the transaction has been modified');
}
// TODO: check payjoinPsbt.inputs where input belongs to us, that it is not finalized // TODO: check payjoinPsbt.inputs where input belongs to us, that it is not finalized
// TODO: check payjoinPsbt.inputs where input belongs to us, that it is was included in psbt.inputs // TODO: check payjoinPsbt.inputs where input belongs to us, that it is was included in psbt.inputs
// TODO: check payjoinPsbt.inputs where input belongs to us, that its sequence has not changed from that of psbt.inputs // TODO: check payjoinPsbt.inputs where input belongs to us, that its sequence has not changed from that of psbt.inputs

20
ts_src/index.ts

@ -1,4 +1,4 @@
import { Psbt, Transaction, payments } from 'bitcoinjs-lib'; import { payments, Psbt, Transaction, TxInput } from 'bitcoinjs-lib';
import { import {
Bip32Derivation, Bip32Derivation,
GlobalXpub, GlobalXpub,
@ -6,13 +6,6 @@ import {
} from 'bip174/src/lib/interfaces'; } from 'bip174/src/lib/interfaces';
type Nullable<T> = T | null; type Nullable<T> = T | null;
interface TxInput {
hash: Buffer;
index: number;
script: Buffer;
sequence: number;
witness: Buffer[];
}
export async function requestPayjoinWithCustomRemoteCall( export async function requestPayjoinWithCustomRemoteCall(
psbt: Psbt, psbt: Psbt,
@ -33,7 +26,6 @@ export async function requestPayjoinWithCustomRemoteCall(
const payjoinPsbt = await remoteCall(clonedPsbt); const payjoinPsbt = await remoteCall(clonedPsbt);
if (!payjoinPsbt) throw new Error("We did not get the receiver's PSBT"); if (!payjoinPsbt) throw new Error("We did not get the receiver's PSBT");
if ( if (
payjoinPsbt.data.globalMap.globalXpub && payjoinPsbt.data.globalMap.globalXpub &&
(payjoinPsbt.data.globalMap.globalXpub as GlobalXpub[]).length > 0 (payjoinPsbt.data.globalMap.globalXpub as GlobalXpub[]).length > 0
@ -87,10 +79,16 @@ export async function requestPayjoinWithCustomRemoteCall(
}); });
} }
if (getGlobalTransaction(payjoinPsbt).version !== getGlobalTransaction(psbt).version) { if (
getGlobalTransaction(payjoinPsbt).version !==
getGlobalTransaction(psbt).version
) {
throw new Error('The version field of the transaction has been modified'); throw new Error('The version field of the transaction has been modified');
} }
if (getGlobalTransaction(payjoinPsbt).locktime !== getGlobalTransaction(psbt).locktime) { if (
getGlobalTransaction(payjoinPsbt).locktime !==
getGlobalTransaction(psbt).locktime
) {
throw new Error('The LockTime field of the transaction has been modified'); throw new Error('The LockTime field of the transaction has been modified');
} }
// TODO: check payjoinPsbt.inputs where input belongs to us, that it is not finalized // TODO: check payjoinPsbt.inputs where input belongs to us, that it is not finalized

Loading…
Cancel
Save