Fix lint payments p2pkh
This commit is contained in:
parent
8d5d78431c
commit
db0e3f1203
2 changed files with 30 additions and 30 deletions
|
@ -1,9 +1,9 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
const bscript = require("../script");
|
|
||||||
const bcrypto = require("../crypto");
|
const bcrypto = require("../crypto");
|
||||||
const lazy = require("./lazy");
|
|
||||||
const networks_1 = require("../networks");
|
const networks_1 = require("../networks");
|
||||||
|
const bscript = require("../script");
|
||||||
|
const lazy = require("./lazy");
|
||||||
const typef = require('typeforce');
|
const typef = require('typeforce');
|
||||||
const OPS = bscript.OPS;
|
const OPS = bscript.OPS;
|
||||||
const ecc = require('tiny-secp256k1');
|
const ecc = require('tiny-secp256k1');
|
||||||
|
@ -23,18 +23,18 @@ function p2pkh(a, opts) {
|
||||||
signature: typef.maybe(bscript.isCanonicalScriptSignature),
|
signature: typef.maybe(bscript.isCanonicalScriptSignature),
|
||||||
input: typef.maybe(typef.Buffer),
|
input: typef.maybe(typef.Buffer),
|
||||||
}, a);
|
}, a);
|
||||||
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 network = a.network || networks_1.bitcoin;
|
const network = a.network || networks_1.bitcoin;
|
||||||
const o = { network };
|
const o = { network };
|
||||||
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);
|
||||||
|
@ -42,7 +42,7 @@ function p2pkh(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', () => {
|
||||||
if (a.output)
|
if (a.output)
|
||||||
return a.output.slice(3, 23);
|
return a.output.slice(3, 23);
|
||||||
if (a.address)
|
if (a.address)
|
||||||
|
@ -50,7 +50,7 @@ function p2pkh(a, opts) {
|
||||||
if (a.pubkey || o.pubkey)
|
if (a.pubkey || o.pubkey)
|
||||||
return bcrypto.hash160(a.pubkey || o.pubkey);
|
return bcrypto.hash160(a.pubkey || o.pubkey);
|
||||||
});
|
});
|
||||||
lazy.prop(o, 'output', function () {
|
lazy.prop(o, 'output', () => {
|
||||||
if (!o.hash)
|
if (!o.hash)
|
||||||
return;
|
return;
|
||||||
return bscript.compile([
|
return bscript.compile([
|
||||||
|
@ -61,24 +61,24 @@ function p2pkh(a, opts) {
|
||||||
OPS.OP_CHECKSIG,
|
OPS.OP_CHECKSIG,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
lazy.prop(o, 'pubkey', function () {
|
lazy.prop(o, 'pubkey', () => {
|
||||||
if (!a.input)
|
if (!a.input)
|
||||||
return;
|
return;
|
||||||
return _chunks()[1];
|
return _chunks()[1];
|
||||||
});
|
});
|
||||||
lazy.prop(o, 'signature', function () {
|
lazy.prop(o, 'signature', () => {
|
||||||
if (!a.input)
|
if (!a.input)
|
||||||
return;
|
return;
|
||||||
return _chunks()[0];
|
return _chunks()[0];
|
||||||
});
|
});
|
||||||
lazy.prop(o, 'input', function () {
|
lazy.prop(o, 'input', () => {
|
||||||
if (!a.pubkey)
|
if (!a.pubkey)
|
||||||
return;
|
return;
|
||||||
if (!a.signature)
|
if (!a.signature)
|
||||||
return;
|
return;
|
||||||
return bscript.compile([a.signature, a.pubkey]);
|
return bscript.compile([a.signature, a.pubkey]);
|
||||||
});
|
});
|
||||||
lazy.prop(o, 'witness', function () {
|
lazy.prop(o, 'witness', () => {
|
||||||
if (!o.input)
|
if (!o.input)
|
||||||
return;
|
return;
|
||||||
return [];
|
return [];
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import { Payment, PaymentOpts } from './index';
|
|
||||||
import * as bscript from '../script';
|
|
||||||
import * as bcrypto from '../crypto';
|
import * as bcrypto from '../crypto';
|
||||||
import * as lazy from './lazy';
|
|
||||||
import { bitcoin as BITCOIN_NETWORK } from '../networks';
|
import { bitcoin as BITCOIN_NETWORK } from '../networks';
|
||||||
|
import * as bscript from '../script';
|
||||||
|
import { Payment, PaymentOpts, StackFunction } from './index';
|
||||||
|
import * as lazy from './lazy';
|
||||||
const typef = require('typeforce');
|
const typef = require('typeforce');
|
||||||
const OPS = bscript.OPS;
|
const OPS = bscript.OPS;
|
||||||
const ecc = require('tiny-secp256k1');
|
const ecc = require('tiny-secp256k1');
|
||||||
|
@ -30,20 +30,20 @@ export function p2pkh(a: Payment, opts?: PaymentOpts): Payment {
|
||||||
a,
|
a,
|
||||||
);
|
);
|
||||||
|
|
||||||
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 network = a.network || BITCOIN_NETWORK;
|
const network = a.network || BITCOIN_NETWORK;
|
||||||
const o: Payment = { network };
|
const o: Payment = { network };
|
||||||
|
|
||||||
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);
|
||||||
|
@ -51,12 +51,12 @@ export function p2pkh(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', () => {
|
||||||
if (a.output) return a.output.slice(3, 23);
|
if (a.output) return a.output.slice(3, 23);
|
||||||
if (a.address) return _address().hash;
|
if (a.address) return _address().hash;
|
||||||
if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey! || o.pubkey!);
|
if (a.pubkey || o.pubkey) return bcrypto.hash160(a.pubkey! || o.pubkey!);
|
||||||
});
|
});
|
||||||
lazy.prop(o, 'output', function() {
|
lazy.prop(o, 'output', () => {
|
||||||
if (!o.hash) return;
|
if (!o.hash) return;
|
||||||
return bscript.compile([
|
return bscript.compile([
|
||||||
OPS.OP_DUP,
|
OPS.OP_DUP,
|
||||||
|
@ -66,20 +66,20 @@ export function p2pkh(a: Payment, opts?: PaymentOpts): Payment {
|
||||||
OPS.OP_CHECKSIG,
|
OPS.OP_CHECKSIG,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
lazy.prop(o, 'pubkey', function() {
|
lazy.prop(o, 'pubkey', () => {
|
||||||
if (!a.input) return;
|
if (!a.input) return;
|
||||||
return <Buffer>_chunks()[1];
|
return _chunks()[1] as Buffer;
|
||||||
});
|
});
|
||||||
lazy.prop(o, 'signature', function() {
|
lazy.prop(o, 'signature', () => {
|
||||||
if (!a.input) return;
|
if (!a.input) return;
|
||||||
return <Buffer>_chunks()[0];
|
return _chunks()[0] as Buffer;
|
||||||
});
|
});
|
||||||
lazy.prop(o, 'input', function() {
|
lazy.prop(o, 'input', () => {
|
||||||
if (!a.pubkey) return;
|
if (!a.pubkey) return;
|
||||||
if (!a.signature) return;
|
if (!a.signature) return;
|
||||||
return bscript.compile([a.signature, a.pubkey]);
|
return bscript.compile([a.signature, a.pubkey]);
|
||||||
});
|
});
|
||||||
lazy.prop(o, 'witness', function() {
|
lazy.prop(o, 'witness', () => {
|
||||||
if (!o.input) return;
|
if (!o.input) return;
|
||||||
return [];
|
return [];
|
||||||
});
|
});
|
||||||
|
@ -127,17 +127,17 @@ export function p2pkh(a: Payment, opts?: PaymentOpts): Payment {
|
||||||
if (a.input) {
|
if (a.input) {
|
||||||
const chunks = _chunks();
|
const chunks = _chunks();
|
||||||
if (chunks.length !== 2) throw new TypeError('Input is invalid');
|
if (chunks.length !== 2) throw new TypeError('Input is invalid');
|
||||||
if (!bscript.isCanonicalScriptSignature(<Buffer>chunks[0]))
|
if (!bscript.isCanonicalScriptSignature(chunks[0] as Buffer))
|
||||||
throw new TypeError('Input has invalid signature');
|
throw new TypeError('Input has invalid signature');
|
||||||
if (!ecc.isPoint(chunks[1]))
|
if (!ecc.isPoint(chunks[1]))
|
||||||
throw new TypeError('Input has invalid pubkey');
|
throw new TypeError('Input has invalid pubkey');
|
||||||
|
|
||||||
if (a.signature && !a.signature.equals(<Buffer>chunks[0]))
|
if (a.signature && !a.signature.equals(chunks[0] as Buffer))
|
||||||
throw new TypeError('Signature mismatch');
|
throw new TypeError('Signature mismatch');
|
||||||
if (a.pubkey && !a.pubkey.equals(<Buffer>chunks[1]))
|
if (a.pubkey && !a.pubkey.equals(chunks[1] as Buffer))
|
||||||
throw new TypeError('Pubkey mismatch');
|
throw new TypeError('Pubkey mismatch');
|
||||||
|
|
||||||
const pkh = bcrypto.hash160(<Buffer>chunks[1]);
|
const pkh = bcrypto.hash160(chunks[1] as Buffer);
|
||||||
if (hash.length > 0 && !hash.equals(pkh))
|
if (hash.length > 0 && !hash.equals(pkh))
|
||||||
throw new TypeError('Hash mismatch');
|
throw new TypeError('Hash mismatch');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue