Use function keyword
This commit is contained in:
parent
e15b515367
commit
09fcb1c6ee
2 changed files with 68 additions and 62 deletions
28
src/psbt.js
28
src/psbt.js
|
@ -591,7 +591,7 @@ function checkScriptForPubkey(pubkey, script, action) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const getHashForSig = (inputIndex, input, unsignedTx, cache) => {
|
function getHashForSig(inputIndex, input, unsignedTx, cache) {
|
||||||
const sighashType =
|
const sighashType =
|
||||||
input.sighashType || transaction_1.Transaction.SIGHASH_ALL;
|
input.sighashType || transaction_1.Transaction.SIGHASH_ALL;
|
||||||
let hash;
|
let hash;
|
||||||
|
@ -672,12 +672,9 @@ const getHashForSig = (inputIndex, input, unsignedTx, cache) => {
|
||||||
sighashType,
|
sighashType,
|
||||||
hash,
|
hash,
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
const scriptCheckerFactory = (payment, paymentScriptName) => (
|
function scriptCheckerFactory(payment, paymentScriptName) {
|
||||||
inputIndex,
|
return (inputIndex, scriptPubKey, redeemScript) => {
|
||||||
scriptPubKey,
|
|
||||||
redeemScript,
|
|
||||||
) => {
|
|
||||||
const redeemScriptOutput = payment({
|
const redeemScriptOutput = payment({
|
||||||
redeem: { output: redeemScript },
|
redeem: { output: redeemScript },
|
||||||
}).output;
|
}).output;
|
||||||
|
@ -687,12 +684,14 @@ const scriptCheckerFactory = (payment, paymentScriptName) => (
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
const checkRedeemScript = scriptCheckerFactory(payments.p2sh, 'Redeem script');
|
const checkRedeemScript = scriptCheckerFactory(payments.p2sh, 'Redeem script');
|
||||||
const checkWitnessScript = scriptCheckerFactory(
|
const checkWitnessScript = scriptCheckerFactory(
|
||||||
payments.p2wsh,
|
payments.p2wsh,
|
||||||
'Witness script',
|
'Witness script',
|
||||||
);
|
);
|
||||||
const isPaymentFactory = payment => script => {
|
function isPaymentFactory(payment) {
|
||||||
|
return script => {
|
||||||
try {
|
try {
|
||||||
payment({ output: script });
|
payment({ output: script });
|
||||||
return true;
|
return true;
|
||||||
|
@ -700,17 +699,18 @@ const isPaymentFactory = payment => script => {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
const isP2WPKH = isPaymentFactory(payments.p2wpkh);
|
const isP2WPKH = isPaymentFactory(payments.p2wpkh);
|
||||||
const isP2PKH = isPaymentFactory(payments.p2pkh);
|
const isP2PKH = isPaymentFactory(payments.p2pkh);
|
||||||
const isP2MS = isPaymentFactory(payments.p2ms);
|
const isP2MS = isPaymentFactory(payments.p2ms);
|
||||||
const isP2PK = isPaymentFactory(payments.p2pk);
|
const isP2PK = isPaymentFactory(payments.p2pk);
|
||||||
const classifyScript = script => {
|
function classifyScript(script) {
|
||||||
if (isP2WPKH(script)) return 'witnesspubkeyhash';
|
if (isP2WPKH(script)) return 'witnesspubkeyhash';
|
||||||
if (isP2PKH(script)) return 'pubkeyhash';
|
if (isP2PKH(script)) return 'pubkeyhash';
|
||||||
if (isP2MS(script)) return 'multisig';
|
if (isP2MS(script)) return 'multisig';
|
||||||
if (isP2PK(script)) return 'pubkey';
|
if (isP2PK(script)) return 'pubkey';
|
||||||
return 'nonstandard';
|
return 'nonstandard';
|
||||||
};
|
}
|
||||||
function getScriptFromInput(inputIndex, input, unsignedTx, cache) {
|
function getScriptFromInput(inputIndex, input, unsignedTx, cache) {
|
||||||
const res = {
|
const res = {
|
||||||
script: null,
|
script: null,
|
||||||
|
@ -748,11 +748,11 @@ function getScriptFromInput(inputIndex, input, unsignedTx, cache) {
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
const hasSigs = (neededSigs, partialSig) => {
|
function hasSigs(neededSigs, partialSig) {
|
||||||
if (!partialSig) return false;
|
if (!partialSig) return false;
|
||||||
if (partialSig.length > neededSigs) throw new Error('Too many signatures');
|
if (partialSig.length > neededSigs) throw new Error('Too many signatures');
|
||||||
return partialSig.length === neededSigs;
|
return partialSig.length === neededSigs;
|
||||||
};
|
}
|
||||||
function witnessStackToScriptWitness(witness) {
|
function witnessStackToScriptWitness(witness) {
|
||||||
let buffer = Buffer.allocUnsafe(0);
|
let buffer = Buffer.allocUnsafe(0);
|
||||||
function writeSlice(slice) {
|
function writeSlice(slice) {
|
||||||
|
@ -797,7 +797,9 @@ function scriptWitnessToWitnessStack(buffer) {
|
||||||
}
|
}
|
||||||
return readVector();
|
return readVector();
|
||||||
}
|
}
|
||||||
const range = n => [...Array(n).keys()];
|
function range(n) {
|
||||||
|
return [...Array(n).keys()];
|
||||||
|
}
|
||||||
function checkTxEmpty(tx) {
|
function checkTxEmpty(tx) {
|
||||||
const isEmpty = tx.ins.every(
|
const isEmpty = tx.ins.every(
|
||||||
input =>
|
input =>
|
||||||
|
|
|
@ -749,12 +749,12 @@ interface HashForSigData {
|
||||||
sighashType: number;
|
sighashType: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const getHashForSig = (
|
function getHashForSig(
|
||||||
inputIndex: number,
|
inputIndex: number,
|
||||||
input: PsbtInput,
|
input: PsbtInput,
|
||||||
unsignedTx: Transaction,
|
unsignedTx: Transaction,
|
||||||
cache: PsbtCache,
|
cache: PsbtCache,
|
||||||
): HashForSigData => {
|
): HashForSigData {
|
||||||
const sighashType = input.sighashType || Transaction.SIGHASH_ALL;
|
const sighashType = input.sighashType || Transaction.SIGHASH_ALL;
|
||||||
let hash: Buffer;
|
let hash: Buffer;
|
||||||
let script: Buffer;
|
let script: Buffer;
|
||||||
|
@ -839,14 +839,15 @@ const getHashForSig = (
|
||||||
sighashType,
|
sighashType,
|
||||||
hash,
|
hash,
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
type ScriptCheckerFunction = (idx: number, spk: Buffer, rs: Buffer) => void;
|
type ScriptCheckerFunction = (idx: number, spk: Buffer, rs: Buffer) => void;
|
||||||
|
|
||||||
const scriptCheckerFactory = (
|
function scriptCheckerFactory(
|
||||||
payment: any,
|
payment: any,
|
||||||
paymentScriptName: string,
|
paymentScriptName: string,
|
||||||
): ScriptCheckerFunction => (
|
): ScriptCheckerFunction {
|
||||||
|
return (
|
||||||
inputIndex: number,
|
inputIndex: number,
|
||||||
scriptPubKey: Buffer,
|
scriptPubKey: Buffer,
|
||||||
redeemScript: Buffer,
|
redeemScript: Buffer,
|
||||||
|
@ -861,6 +862,7 @@ const scriptCheckerFactory = (
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const checkRedeemScript = scriptCheckerFactory(payments.p2sh, 'Redeem script');
|
const checkRedeemScript = scriptCheckerFactory(payments.p2sh, 'Redeem script');
|
||||||
const checkWitnessScript = scriptCheckerFactory(
|
const checkWitnessScript = scriptCheckerFactory(
|
||||||
|
@ -870,9 +872,8 @@ const checkWitnessScript = scriptCheckerFactory(
|
||||||
|
|
||||||
type isPaymentFunction = (script: Buffer) => boolean;
|
type isPaymentFunction = (script: Buffer) => boolean;
|
||||||
|
|
||||||
const isPaymentFactory = (payment: any): isPaymentFunction => (
|
function isPaymentFactory(payment: any): isPaymentFunction {
|
||||||
script: Buffer,
|
return (script: Buffer): boolean => {
|
||||||
): boolean => {
|
|
||||||
try {
|
try {
|
||||||
payment({ output: script });
|
payment({ output: script });
|
||||||
return true;
|
return true;
|
||||||
|
@ -880,18 +881,19 @@ const isPaymentFactory = (payment: any): isPaymentFunction => (
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
const isP2WPKH = isPaymentFactory(payments.p2wpkh);
|
const isP2WPKH = isPaymentFactory(payments.p2wpkh);
|
||||||
const isP2PKH = isPaymentFactory(payments.p2pkh);
|
const isP2PKH = isPaymentFactory(payments.p2pkh);
|
||||||
const isP2MS = isPaymentFactory(payments.p2ms);
|
const isP2MS = isPaymentFactory(payments.p2ms);
|
||||||
const isP2PK = isPaymentFactory(payments.p2pk);
|
const isP2PK = isPaymentFactory(payments.p2pk);
|
||||||
|
|
||||||
const classifyScript = (script: Buffer): string => {
|
function classifyScript(script: Buffer): string {
|
||||||
if (isP2WPKH(script)) return 'witnesspubkeyhash';
|
if (isP2WPKH(script)) return 'witnesspubkeyhash';
|
||||||
if (isP2PKH(script)) return 'pubkeyhash';
|
if (isP2PKH(script)) return 'pubkeyhash';
|
||||||
if (isP2MS(script)) return 'multisig';
|
if (isP2MS(script)) return 'multisig';
|
||||||
if (isP2PK(script)) return 'pubkey';
|
if (isP2PK(script)) return 'pubkey';
|
||||||
return 'nonstandard';
|
return 'nonstandard';
|
||||||
};
|
}
|
||||||
|
|
||||||
interface GetScriptReturn {
|
interface GetScriptReturn {
|
||||||
script: Buffer | null;
|
script: Buffer | null;
|
||||||
|
@ -942,11 +944,11 @@ function getScriptFromInput(
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
const hasSigs = (neededSigs: number, partialSig?: any[]): boolean => {
|
function hasSigs(neededSigs: number, partialSig?: any[]): boolean {
|
||||||
if (!partialSig) return false;
|
if (!partialSig) return false;
|
||||||
if (partialSig.length > neededSigs) throw new Error('Too many signatures');
|
if (partialSig.length > neededSigs) throw new Error('Too many signatures');
|
||||||
return partialSig.length === neededSigs;
|
return partialSig.length === neededSigs;
|
||||||
};
|
}
|
||||||
|
|
||||||
function witnessStackToScriptWitness(witness: Buffer[]): Buffer {
|
function witnessStackToScriptWitness(witness: Buffer[]): Buffer {
|
||||||
let buffer = Buffer.allocUnsafe(0);
|
let buffer = Buffer.allocUnsafe(0);
|
||||||
|
@ -1006,7 +1008,9 @@ function scriptWitnessToWitnessStack(buffer: Buffer): Buffer[] {
|
||||||
return readVector();
|
return readVector();
|
||||||
}
|
}
|
||||||
|
|
||||||
const range = (n: number): number[] => [...Array(n).keys()];
|
function range(n: number): number[] {
|
||||||
|
return [...Array(n).keys()];
|
||||||
|
}
|
||||||
|
|
||||||
function checkTxEmpty(tx: Transaction): void {
|
function checkTxEmpty(tx: Transaction): void {
|
||||||
const isEmpty = tx.ins.every(
|
const isEmpty = tx.ins.every(
|
||||||
|
|
Loading…
Add table
Reference in a new issue