Commit js, ts, and definitions in separate folders

This commit is contained in:
junderw 2019-01-04 18:33:02 +09:00
parent e7ac2b9a4e
commit bc28949056
No known key found for this signature in database
GPG key ID: B256185D3A971908
148 changed files with 3850 additions and 39 deletions

View file

@ -0,0 +1,7 @@
import * as input from './input'
import * as output from './output'
export {
input,
output,
}

View file

@ -0,0 +1,21 @@
// OP_0 [signatures ...]
import * as bscript from '../../script'
import { OPS } from '../../script'
function partialSignature (value: number | Buffer): boolean {
return value === OPS.OP_0 || bscript.isCanonicalScriptSignature(<Buffer>value)
}
export function check (script: Buffer | Array<number | Buffer>, allowIncomplete?: boolean): boolean {
const chunks = <Array<number | Buffer>>bscript.decompile(script)
if (chunks.length < 2) return false
if (chunks[0] !== OPS.OP_0) return false
if (allowIncomplete) {
return chunks.slice(1).every(partialSignature)
}
return (<Array<Buffer>>chunks.slice(1)).every(bscript.isCanonicalScriptSignature)
}
check.toJSON = function () { return 'multisig input' }

View file

@ -0,0 +1,27 @@
// m [pubKeys ...] n OP_CHECKMULTISIG
import * as bscript from '../../script'
import * as types from '../../types'
import { OPS } from '../../script'
const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
export function check (script: Buffer | Array<number | Buffer>, allowIncomplete?: boolean): boolean {
const chunks = <Array<number | Buffer>>bscript.decompile(script)
if (chunks.length < 4) return false
if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG) return false
if (!types.Number(chunks[0])) return false
if (!types.Number(chunks[chunks.length - 2])) return false
const m = <number>chunks[0] - OP_INT_BASE
const n = <number>chunks[chunks.length - 2] - OP_INT_BASE
if (m <= 0) return false
if (n > 16) return false
if (m > n) return false
if (n !== chunks.length - 3) return false
if (allowIncomplete) return true
const keys = <Array<Buffer>> chunks.slice(1, -2)
return keys.every(bscript.isCanonicalPubKey)
}
check.toJSON = function () { return 'multi-sig output' }

View file

@ -0,0 +1,17 @@
// OP_RETURN {data}
import * as bscript from '../script'
const OPS = bscript.OPS
export function check (script: Buffer | Array<number | Buffer>): boolean {
const buffer = bscript.compile(script)
return buffer.length > 1 &&
buffer[0] === OPS.OP_RETURN
}
check.toJSON = function () { return 'null data output' }
const output = { check }
export {
output
}

View file

@ -0,0 +1,7 @@
import * as input from './input'
import * as output from './output'
export {
input,
output,
}

View file

@ -0,0 +1,11 @@
// {signature}
import * as bscript from '../../script'
export function check (script: Buffer | Array<number | Buffer>): boolean {
const chunks = <Array<number | Buffer>>bscript.decompile(script)
return chunks.length === 1 &&
bscript.isCanonicalScriptSignature(<Buffer>chunks[0])
}
check.toJSON = function () { return 'pubKey input' }

View file

@ -0,0 +1,13 @@
// {pubKey} OP_CHECKSIG
import * as bscript from '../../script'
import { OPS } from '../../script'
export function check (script: Buffer | Array<number | Buffer>): boolean {
const chunks = <Array<number | Buffer>>bscript.decompile(script)
return chunks.length === 2 &&
bscript.isCanonicalPubKey(<Buffer>chunks[0]) &&
chunks[1] === OPS.OP_CHECKSIG
}
check.toJSON = function () { return 'pubKey output' }

View file

@ -0,0 +1,7 @@
import * as input from './input'
import * as output from './output'
export {
input,
output,
}

View file

@ -0,0 +1,12 @@
// {signature} {pubKey}
import * as bscript from '../../script'
export function check (script: Buffer | Array<number | Buffer>): boolean {
const chunks = <Array<number | Buffer>>bscript.decompile(script)
return chunks.length === 2 &&
bscript.isCanonicalScriptSignature(<Buffer>chunks[0]) &&
bscript.isCanonicalPubKey(<Buffer>chunks[1])
}
check.toJSON = function () { return 'pubKeyHash input' }

View file

@ -0,0 +1,16 @@
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
import * as bscript from '../../script'
import { OPS } from '../../script'
export function check (script: Buffer | Array<number | Buffer>): boolean {
const buffer = bscript.compile(script)
return buffer.length === 25 &&
buffer[0] === OPS.OP_DUP &&
buffer[1] === OPS.OP_HASH160 &&
buffer[2] === 0x14 &&
buffer[23] === OPS.OP_EQUALVERIFY &&
buffer[24] === OPS.OP_CHECKSIG
}
check.toJSON = function () { return 'pubKeyHash output' }

View file

@ -0,0 +1,7 @@
import * as input from './input'
import * as output from './output'
export {
input,
output,
}

View file

@ -0,0 +1,46 @@
// <scriptSig> {serialized scriptPubKey script}
import * as bscript from '../../script'
import * as p2ms from '../multisig'
import * as p2pk from '../pubkey'
import * as p2pkh from '../pubkeyhash'
import * as p2wpkho from '../witnesspubkeyhash/output'
import * as p2wsho from '../witnessscripthash/output'
export function check (script: Buffer | Array<number | Buffer>, allowIncomplete?: boolean): boolean {
const chunks = <Array<number | Buffer>>bscript.decompile(script)
if (chunks.length < 1) return false
const lastChunk = chunks[chunks.length - 1]
if (!Buffer.isBuffer(lastChunk)) return false
const scriptSigChunks = <Array<number | Buffer>>bscript.decompile(bscript.compile(chunks.slice(0, -1)))
const redeemScriptChunks = bscript.decompile(<Buffer>lastChunk)
// is redeemScript a valid script?
if (!redeemScriptChunks) return false
// is redeemScriptSig push only?
if (!bscript.isPushOnly(scriptSigChunks)) return false
// is witness?
if (chunks.length === 1) {
return p2wsho.check(redeemScriptChunks) ||
p2wpkho.check(redeemScriptChunks)
}
// match types
if (p2pkh.input.check(scriptSigChunks) &&
p2pkh.output.check(redeemScriptChunks)) return true
if (p2ms.input.check(scriptSigChunks, allowIncomplete) &&
p2ms.output.check(redeemScriptChunks)) return true
if (p2pk.input.check(scriptSigChunks) &&
p2pk.output.check(redeemScriptChunks)) return true
return false
}
check.toJSON = function () { return 'scriptHash input' }

View file

@ -0,0 +1,14 @@
// OP_HASH160 {scriptHash} OP_EQUAL
import * as bscript from '../../script'
import { OPS } from '../../script'
export function check (script: Buffer | Array<number | Buffer>): boolean {
const buffer = bscript.compile(script)
return buffer.length === 23 &&
buffer[0] === OPS.OP_HASH160 &&
buffer[1] === 0x14 &&
buffer[22] === OPS.OP_EQUAL
}
check.toJSON = function () { return 'scriptHash output' }

View file

@ -0,0 +1,5 @@
import * as output from './output'
export {
output,
}

View file

@ -0,0 +1,36 @@
// OP_RETURN {aa21a9ed} {commitment}
import * as bscript from '../../script'
import * as types from '../../types'
const typeforce = require('typeforce')
import { OPS } from '../../script'
const HEADER: Buffer = Buffer.from('aa21a9ed', 'hex')
export function check (script: Buffer | Array<number | Buffer>): boolean {
const buffer = bscript.compile(script)
return buffer.length > 37 &&
buffer[0] === OPS.OP_RETURN &&
buffer[1] === 0x24 &&
buffer.slice(2, 6).equals(HEADER)
}
check.toJSON = function () { return 'Witness commitment output' }
export function encode (commitment: Buffer): Buffer {
typeforce(types.Hash256bit, commitment)
const buffer = Buffer.allocUnsafe(36)
HEADER.copy(buffer, 0)
commitment.copy(buffer, 4)
return bscript.compile([OPS.OP_RETURN, buffer])
}
export function decode (buffer: Buffer): Buffer {
typeforce(check, buffer)
return (<Buffer>(<Array<number | Buffer>>bscript.decompile(buffer))[1]).slice(4, 36)
}

View file

@ -0,0 +1,7 @@
import * as input from './input'
import * as output from './output'
export {
input,
output,
}

View file

@ -0,0 +1,16 @@
// {signature} {pubKey}
import * as bscript from '../../script'
function isCompressedCanonicalPubKey (pubKey: Buffer): boolean {
return bscript.isCanonicalPubKey(pubKey) && pubKey.length === 33
}
export function check (script: Buffer | Array<number | Buffer>): boolean {
const chunks = <Array<number | Buffer>>bscript.decompile(script)
return chunks.length === 2 &&
bscript.isCanonicalScriptSignature(<Buffer>chunks[0]) &&
isCompressedCanonicalPubKey(<Buffer>chunks[1])
}
check.toJSON = function () { return 'witnessPubKeyHash input' }

View file

@ -0,0 +1,13 @@
// OP_0 {pubKeyHash}
import * as bscript from '../../script'
import { OPS } from '../../script'
export function check (script: Buffer | Array<number | Buffer>): boolean {
const buffer = bscript.compile(script)
return buffer.length === 22 &&
buffer[0] === OPS.OP_0 &&
buffer[1] === 0x14
}
check.toJSON = function () { return 'Witness pubKeyHash output' }

View file

@ -0,0 +1,7 @@
import * as input from './input'
import * as output from './output'
export {
input,
output,
}

View file

@ -0,0 +1,36 @@
// <scriptSig> {serialized scriptPubKey script}
import * as bscript from '../../script'
const typeforce = require('typeforce')
import * as p2ms from '../multisig'
import * as p2pk from '../pubkey'
import * as p2pkh from '../pubkeyhash'
export function check (chunks: Array<Buffer>, allowIncomplete?: boolean): boolean {
typeforce(typeforce.Array, chunks)
if (chunks.length < 1) return false
const witnessScript = chunks[chunks.length - 1]
if (!Buffer.isBuffer(witnessScript)) return false
const witnessScriptChunks = bscript.decompile(witnessScript)
// is witnessScript a valid script?
if (!witnessScriptChunks || witnessScriptChunks.length === 0) return false
const witnessRawScriptSig = bscript.compile(chunks.slice(0, -1))
// match types
if (p2pkh.input.check(witnessRawScriptSig) &&
p2pkh.output.check(witnessScriptChunks)) return true
if (p2ms.input.check(witnessRawScriptSig, allowIncomplete) &&
p2ms.output.check(witnessScriptChunks)) return true
if (p2pk.input.check(witnessRawScriptSig) &&
p2pk.output.check(witnessScriptChunks)) return true
return false
}
check.toJSON = function () { return 'witnessScriptHash input' }

View file

@ -0,0 +1,13 @@
// OP_0 {scriptHash}
import * as bscript from '../../script'
import { OPS } from '../../script'
export function check (script: Buffer | Array<number | Buffer>): boolean {
const buffer = bscript.compile(script)
return buffer.length === 34 &&
buffer[0] === OPS.OP_0 &&
buffer[1] === 0x20
}
check.toJSON = function () { return 'Witness scriptHash output' }