Use function keyword

This commit is contained in:
junderw 2019-07-09 10:57:41 +09:00
parent e15b515367
commit 09fcb1c6ee
No known key found for this signature in database
GPG key ID: B256185D3A971908
2 changed files with 68 additions and 62 deletions

View file

@ -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,45 +672,45 @@ const getHashForSig = (inputIndex, input, unsignedTx, cache) => {
sighashType, sighashType,
hash, hash,
}; };
}; }
const scriptCheckerFactory = (payment, paymentScriptName) => ( function scriptCheckerFactory(payment, paymentScriptName) {
inputIndex, return (inputIndex, scriptPubKey, redeemScript) => {
scriptPubKey, const redeemScriptOutput = payment({
redeemScript, redeem: { output: redeemScript },
) => { }).output;
const redeemScriptOutput = payment({ if (!scriptPubKey.equals(redeemScriptOutput)) {
redeem: { output: redeemScript }, throw new Error(
}).output; `${paymentScriptName} for input #${inputIndex} doesn't match the scriptPubKey in the prevout`,
if (!scriptPubKey.equals(redeemScriptOutput)) { );
throw new Error( }
`${paymentScriptName} for input #${inputIndex} doesn't match the scriptPubKey in the prevout`, };
); }
}
};
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) {
try { return script => {
payment({ output: script }); try {
return true; payment({ output: script });
} catch (err) { return true;
return false; } catch (err) {
} 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 =>

View file

@ -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,28 +839,30 @@ 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 {
inputIndex: number, return (
scriptPubKey: Buffer, inputIndex: number,
redeemScript: Buffer, scriptPubKey: Buffer,
): void => { redeemScript: Buffer,
const redeemScriptOutput = payment({ ): void => {
redeem: { output: redeemScript }, const redeemScriptOutput = payment({
}).output as Buffer; redeem: { output: redeemScript },
}).output as Buffer;
if (!scriptPubKey.equals(redeemScriptOutput)) { if (!scriptPubKey.equals(redeemScriptOutput)) {
throw new Error( throw new Error(
`${paymentScriptName} for input #${inputIndex} doesn't match the scriptPubKey in the prevout`, `${paymentScriptName} for input #${inputIndex} doesn't match the scriptPubKey in the prevout`,
); );
} }
}; };
}
const checkRedeemScript = scriptCheckerFactory(payments.p2sh, 'Redeem script'); const checkRedeemScript = scriptCheckerFactory(payments.p2sh, 'Redeem script');
const checkWitnessScript = scriptCheckerFactory( const checkWitnessScript = scriptCheckerFactory(
@ -870,28 +872,28 @@ 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; } catch (err) {
} catch (err) { 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(