Browse Source

Fix lint payments p2sh

fixTypes
junderw 6 years ago
parent
commit
fe62e13023
No known key found for this signature in database GPG Key ID: B256185D3A971908
  1. 26
      src/payments/p2sh.js
  2. 2
      ts_src/payments/index.ts
  3. 62
      ts_src/payments/p2sh.ts
  4. 1
      types/payments/index.d.ts

26
src/payments/p2sh.js

@ -1,8 +1,8 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const bcrypto = require("../crypto");
const networks_1 = require("../networks"); const networks_1 = require("../networks");
const bscript = require("../script"); const bscript = require("../script");
const bcrypto = require("../crypto");
const lazy = require("./lazy"); const lazy = require("./lazy");
const typef = require('typeforce'); const typef = require('typeforce');
const OPS = bscript.OPS; const OPS = bscript.OPS;
@ -10,7 +10,7 @@ const bs58check = require('bs58check');
function stacksEqual(a, b) { function stacksEqual(a, b) {
if (a.length !== b.length) if (a.length !== b.length)
return false; return false;
return a.every(function (x, i) { return a.every((x, i) => {
return x.equals(b[i]); return x.equals(b[i]);
}); });
} }
@ -40,16 +40,16 @@ function p2sh(a, opts) {
network = (a.redeem && a.redeem.network) || networks_1.bitcoin; network = (a.redeem && a.redeem.network) || networks_1.bitcoin;
} }
const o = { network }; const o = { network };
const _address = lazy.value(function () { const _address = lazy.value(() => {
const payload = bs58check.decode(a.address); const payload = bs58check.decode(a.address);
const version = payload.readUInt8(0); const version = payload.readUInt8(0);
const hash = payload.slice(1); const hash = payload.slice(1);
return { version, hash }; return { version, hash };
}); });
const _chunks = lazy.value(function () { const _chunks = lazy.value(() => {
return bscript.decompile(a.input); return bscript.decompile(a.input);
}); });
const _redeem = lazy.value(function () { const _redeem = lazy.value(() => {
const chunks = _chunks(); const chunks = _chunks();
return { return {
network, network,
@ -59,7 +59,7 @@ function p2sh(a, opts) {
}; };
}); });
// output dependents // output dependents
lazy.prop(o, 'address', function () { lazy.prop(o, 'address', () => {
if (!o.hash) if (!o.hash)
return; return;
const payload = Buffer.allocUnsafe(21); const payload = Buffer.allocUnsafe(21);
@ -67,7 +67,7 @@ function p2sh(a, opts) {
o.hash.copy(payload, 1); o.hash.copy(payload, 1);
return bs58check.encode(payload); return bs58check.encode(payload);
}); });
lazy.prop(o, 'hash', function () { lazy.prop(o, 'hash', () => {
// in order of least effort // in order of least effort
if (a.output) if (a.output)
return a.output.slice(2, 22); return a.output.slice(2, 22);
@ -76,23 +76,23 @@ function p2sh(a, opts) {
if (o.redeem && o.redeem.output) if (o.redeem && o.redeem.output)
return bcrypto.hash160(o.redeem.output); return bcrypto.hash160(o.redeem.output);
}); });
lazy.prop(o, 'output', function () { lazy.prop(o, 'output', () => {
if (!o.hash) if (!o.hash)
return; return;
return bscript.compile([OPS.OP_HASH160, o.hash, OPS.OP_EQUAL]); return bscript.compile([OPS.OP_HASH160, o.hash, OPS.OP_EQUAL]);
}); });
// input dependents // input dependents
lazy.prop(o, 'redeem', function () { lazy.prop(o, 'redeem', () => {
if (!a.input) if (!a.input)
return; return;
return _redeem(); return _redeem();
}); });
lazy.prop(o, 'input', function () { lazy.prop(o, 'input', () => {
if (!a.redeem || !a.redeem.input || !a.redeem.output) if (!a.redeem || !a.redeem.input || !a.redeem.output)
return; return;
return bscript.compile([].concat(bscript.decompile(a.redeem.input), a.redeem.output)); return bscript.compile([].concat(bscript.decompile(a.redeem.input), a.redeem.output));
}); });
lazy.prop(o, 'witness', function () { lazy.prop(o, 'witness', () => {
if (o.redeem && o.redeem.witness) if (o.redeem && o.redeem.witness)
return o.redeem.witness; return o.redeem.witness;
if (o.input) if (o.input)
@ -126,7 +126,7 @@ function p2sh(a, opts) {
hash = hash2; hash = hash2;
} }
// inlined to prevent 'no-inner-declarations' failing // inlined to prevent 'no-inner-declarations' failing
const checkRedeem = function (redeem) { const checkRedeem = (redeem) => {
// is the redeem output empty/invalid? // is the redeem output empty/invalid?
if (redeem.output) { if (redeem.output) {
const decompile = bscript.decompile(redeem.output); const decompile = bscript.decompile(redeem.output);
@ -147,7 +147,7 @@ function p2sh(a, opts) {
if (hasInput && hasWitness) if (hasInput && hasWitness)
throw new TypeError('Input and witness provided'); throw new TypeError('Input and witness provided');
if (hasInput) { if (hasInput) {
const richunks = (bscript.decompile(redeem.input)); const richunks = bscript.decompile(redeem.input);
if (!bscript.isPushOnly(richunks)) if (!bscript.isPushOnly(richunks))
throw new TypeError('Non push-only scriptSig'); throw new TypeError('Non push-only scriptSig');
} }

2
ts_src/payments/index.ts

@ -24,6 +24,8 @@ export interface Payment {
witness?: Buffer[]; witness?: Buffer[];
} }
export type PaymentFunction = () => Payment;
export interface PaymentOpts { export interface PaymentOpts {
validate?: boolean; validate?: boolean;
allowIncomplete?: boolean; allowIncomplete?: boolean;

62
ts_src/payments/p2sh.ts

@ -1,17 +1,23 @@
import { Payment, PaymentOpts } from './index'; import * as bcrypto from '../crypto';
import { bitcoin as BITCOIN_NETWORK } from '../networks'; import { bitcoin as BITCOIN_NETWORK } from '../networks';
import * as bscript from '../script'; import * as bscript from '../script';
import * as bcrypto from '../crypto'; import {
Payment,
PaymentFunction,
PaymentOpts,
Stack,
StackFunction,
} from './index';
import * as lazy from './lazy'; import * as lazy from './lazy';
const typef = require('typeforce'); const typef = require('typeforce');
const OPS = bscript.OPS; const OPS = bscript.OPS;
const bs58check = require('bs58check'); const bs58check = require('bs58check');
function stacksEqual(a: Array<Buffer>, b: Array<Buffer>): boolean { function stacksEqual(a: Buffer[], b: Buffer[]): boolean {
if (a.length !== b.length) return false; if (a.length !== b.length) return false;
return a.every(function(x, i) { return a.every((x, i) => {
return x.equals(b[i]); return x.equals(b[i]);
}); });
} }
@ -51,27 +57,29 @@ export function p2sh(a: Payment, opts?: PaymentOpts): Payment {
const o: Payment = { network }; const o: Payment = { network };
const _address = lazy.value(function() { const _address = lazy.value(() => {
const payload = bs58check.decode(a.address); const payload = bs58check.decode(a.address);
const version = payload.readUInt8(0); const version = payload.readUInt8(0);
const hash = payload.slice(1); const hash = payload.slice(1);
return { version, hash }; return { version, hash };
}); });
const _chunks = <() => Array<Buffer | number>>lazy.value(function() { const _chunks = lazy.value(() => {
return bscript.decompile(a.input!); return bscript.decompile(a.input!);
}); }) as StackFunction;
const _redeem = lazy.value(function(): Payment { const _redeem = lazy.value(
const chunks = _chunks(); (): Payment => {
return { const chunks = _chunks();
network, return {
output: <Buffer>chunks[chunks.length - 1], network,
input: bscript.compile(chunks.slice(0, -1)), output: chunks[chunks.length - 1] as Buffer,
witness: a.witness || [], input: bscript.compile(chunks.slice(0, -1)),
}; witness: a.witness || [],
}); };
},
) as PaymentFunction;
// output dependents // output dependents
lazy.prop(o, 'address', function() { lazy.prop(o, 'address', () => {
if (!o.hash) return; if (!o.hash) return;
const payload = Buffer.allocUnsafe(21); const payload = Buffer.allocUnsafe(21);
@ -79,32 +87,32 @@ export function p2sh(a: Payment, opts?: PaymentOpts): Payment {
o.hash.copy(payload, 1); o.hash.copy(payload, 1);
return bs58check.encode(payload); return bs58check.encode(payload);
}); });
lazy.prop(o, 'hash', function() { lazy.prop(o, 'hash', () => {
// in order of least effort // in order of least effort
if (a.output) return a.output.slice(2, 22); if (a.output) return a.output.slice(2, 22);
if (a.address) return _address().hash; if (a.address) return _address().hash;
if (o.redeem && o.redeem.output) return bcrypto.hash160(o.redeem.output); if (o.redeem && o.redeem.output) return bcrypto.hash160(o.redeem.output);
}); });
lazy.prop(o, 'output', function() { lazy.prop(o, 'output', () => {
if (!o.hash) return; if (!o.hash) return;
return bscript.compile([OPS.OP_HASH160, o.hash, OPS.OP_EQUAL]); return bscript.compile([OPS.OP_HASH160, o.hash, OPS.OP_EQUAL]);
}); });
// input dependents // input dependents
lazy.prop(o, 'redeem', function() { lazy.prop(o, 'redeem', () => {
if (!a.input) return; if (!a.input) return;
return _redeem(); return _redeem();
}); });
lazy.prop(o, 'input', function() { lazy.prop(o, 'input', () => {
if (!a.redeem || !a.redeem.input || !a.redeem.output) return; if (!a.redeem || !a.redeem.input || !a.redeem.output) return;
return bscript.compile( return bscript.compile(
(<Array<Buffer | number>>[]).concat( ([] as Stack).concat(
<Array<Buffer | number>>bscript.decompile(a.redeem.input), bscript.decompile(a.redeem.input) as Stack,
a.redeem.output, a.redeem.output,
), ),
); );
}); });
lazy.prop(o, 'witness', function() { lazy.prop(o, 'witness', () => {
if (o.redeem && o.redeem.witness) return o.redeem.witness; if (o.redeem && o.redeem.witness) return o.redeem.witness;
if (o.input) return []; if (o.input) return [];
}); });
@ -140,7 +148,7 @@ export function p2sh(a: Payment, opts?: PaymentOpts): Payment {
} }
// inlined to prevent 'no-inner-declarations' failing // inlined to prevent 'no-inner-declarations' failing
const checkRedeem = function(redeem: Payment): void { const checkRedeem = (redeem: Payment): void => {
// is the redeem output empty/invalid? // is the redeem output empty/invalid?
if (redeem.output) { if (redeem.output) {
const decompile = bscript.decompile(redeem.output); const decompile = bscript.decompile(redeem.output);
@ -161,9 +169,7 @@ export function p2sh(a: Payment, opts?: PaymentOpts): Payment {
if (hasInput && hasWitness) if (hasInput && hasWitness)
throw new TypeError('Input and witness provided'); throw new TypeError('Input and witness provided');
if (hasInput) { if (hasInput) {
const richunks = <Array<Buffer | number>>( const richunks = bscript.decompile(redeem.input) as Stack;
bscript.decompile(redeem.input)
);
if (!bscript.isPushOnly(richunks)) if (!bscript.isPushOnly(richunks))
throw new TypeError('Non push-only scriptSig'); throw new TypeError('Non push-only scriptSig');
} }

1
types/payments/index.d.ts

@ -23,6 +23,7 @@ export interface Payment {
redeem?: Payment; redeem?: Payment;
witness?: Buffer[]; witness?: Buffer[];
} }
export declare type PaymentFunction = () => Payment;
export interface PaymentOpts { export interface PaymentOpts {
validate?: boolean; validate?: boolean;
allowIncomplete?: boolean; allowIncomplete?: boolean;

Loading…
Cancel
Save