bitcoinjs-lib/src/script.ts

205 lines
5.3 KiB
TypeScript
Raw Normal View History

2018-12-28 16:53:54 +01:00
import * as types from './types'
2018-06-25 08:25:12 +02:00
const Buffer = require('safe-buffer').Buffer
const bip66 = require('bip66')
2018-06-25 08:24:37 +02:00
const ecc = require('tiny-secp256k1')
2018-06-25 08:25:12 +02:00
const pushdata = require('pushdata-bitcoin')
const typeforce = require('typeforce')
const scriptNumber = require('./script_number')
2018-06-25 08:25:12 +02:00
const OPS = require('bitcoin-ops')
const REVERSE_OPS = require('bitcoin-ops/map')
const OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
function isOPInt (value:number): boolean {
return types.Number(value) &&
((value === OPS.OP_0) ||
(value >= OPS.OP_1 && value <= OPS.OP_16) ||
(value === OPS.OP_1NEGATE))
}
function isPushOnlyChunk (value: number | Buffer): boolean {
return types.Buffer(value) || isOPInt(<number>value)
2016-11-14 06:03:06 +01:00
}
export function isPushOnly (value: Array<number | Buffer>) {
2016-11-14 06:03:06 +01:00
return types.Array(value) && value.every(isPushOnlyChunk)
}
function asMinimalOP (buffer: Buffer): number | void {
if (buffer.length === 0) return OPS.OP_0
if (buffer.length !== 1) return
if (buffer[0] >= 1 && buffer[0] <= 16) return OP_INT_BASE + buffer[0]
if (buffer[0] === 0x81) return OPS.OP_1NEGATE
}
function chunksIsBuffer(buf: Buffer | Array<number | Buffer>): buf is Buffer {
return Buffer.isBuffer(buf)
}
function chunksIsArray(buf: Buffer | Array<number | Buffer>): buf is Array<number | Buffer> {
return types.Array(buf)
}
function singleChunkIsBuffer(buf: number | Buffer): buf is Buffer {
return Buffer.isBuffer(buf)
}
export function compile (chunks: Buffer | Array<number | Buffer>): Buffer {
2015-08-14 03:16:17 +02:00
// TODO: remove me
if (chunksIsBuffer(chunks)) return chunks
2015-08-14 03:16:17 +02:00
typeforce(types.Array, chunks)
const bufferSize = chunks.reduce(function (accum: number, chunk) {
2015-08-14 03:16:17 +02:00
// data chunk
if (singleChunkIsBuffer(chunk)) {
// adhere to BIP62.3, minimal push policy
if (chunk.length === 1 && asMinimalOP(chunk) !== undefined) {
return accum + 1
}
return accum + pushdata.encodingLength(chunk.length) + chunk.length
2015-08-14 03:16:17 +02:00
}
// opcode
return accum + 1
}, 0.0)
const buffer = Buffer.allocUnsafe(bufferSize)
let offset = 0
2015-08-14 03:16:17 +02:00
chunks.forEach(function (chunk) {
// data chunk
if (singleChunkIsBuffer(chunk)) {
// adhere to BIP62.3, minimal push policy
const opcode = asMinimalOP(chunk)
if (opcode !== undefined) {
buffer.writeUInt8(opcode, offset)
offset += 1
return
}
offset += pushdata.encode(buffer, chunk.length, offset)
2015-08-14 03:16:17 +02:00
chunk.copy(buffer, offset)
offset += chunk.length
// opcode
} else {
buffer.writeUInt8(chunk, offset)
offset += 1
}
})
if (offset !== buffer.length) throw new Error('Could not decode chunks')
return buffer
}
export function decompile (buffer: Buffer | Array<number | Buffer>): Array<number | Buffer> {
2015-08-14 03:16:17 +02:00
// TODO: remove me
if (chunksIsArray(buffer)) return buffer
2015-08-14 03:16:17 +02:00
typeforce(types.Buffer, buffer)
2018-12-28 16:53:54 +01:00
const chunks: Array<number | Buffer> = []
let i = 0
2015-08-14 03:16:17 +02:00
while (i < buffer.length) {
const opcode = buffer[i]
2015-08-14 03:16:17 +02:00
// data chunk
if ((opcode > OPS.OP_0) && (opcode <= OPS.OP_PUSHDATA4)) {
const d = pushdata.decode(buffer, i)
2015-08-14 03:16:17 +02:00
// did reading a pushDataInt fail?
if (d === null) return null
2015-08-14 03:16:17 +02:00
i += d.size
// attempt to read too much data?
if (i + d.number > buffer.length) return null
2015-08-14 03:16:17 +02:00
const data = buffer.slice(i, i + d.number)
2015-08-14 03:16:17 +02:00
i += d.number
// decompile minimally
const op = asMinimalOP(data)
if (op !== undefined) {
2018-12-28 16:53:54 +01:00
chunks.push(<number>op)
} else {
chunks.push(data)
}
2015-08-14 03:16:17 +02:00
// opcode
} else {
chunks.push(opcode)
i += 1
}
}
return chunks
}
export function toASM (chunks: Buffer | Array<number | Buffer>): string {
if (chunksIsBuffer(chunks)) {
chunks = decompile(chunks)
}
return chunks.map(function (chunk) {
// data?
if (singleChunkIsBuffer(chunk)) {
const op = asMinimalOP(chunk)
if (op === undefined) return chunk.toString('hex')
chunk = <number>op
}
// opcode!
return REVERSE_OPS[chunk]
}).join(' ')
}
export function fromASM (asm: string): Buffer {
typeforce(types.String, asm)
return compile(asm.split(' ').map(function (chunkStr) {
// opcode?
if (OPS[chunkStr] !== undefined) return OPS[chunkStr]
typeforce(types.Hex, chunkStr)
// data!
2017-04-19 09:24:58 +02:00
return Buffer.from(chunkStr, 'hex')
}))
}
export function toStack (chunks: Buffer | Array<number | Buffer>): Array<Buffer> {
chunks = decompile(chunks)
2016-11-14 06:03:06 +01:00
typeforce(isPushOnly, chunks)
return chunks.map(function (op) {
if (singleChunkIsBuffer(op)) return op
2017-04-19 09:24:58 +02:00
if (op === OPS.OP_0) return Buffer.allocUnsafe(0)
return scriptNumber.encode(op - OP_INT_BASE)
})
}
export function isCanonicalPubKey (buffer: Buffer): boolean {
return ecc.isPoint(buffer)
2014-06-24 09:32:23 +02:00
}
export function isDefinedHashType (hashType: number): boolean {
const hashTypeMod = hashType & ~0x80
2018-05-29 03:37:03 +02:00
// return hashTypeMod > SIGHASH_ALL && hashTypeMod < SIGHASH_SINGLE
return hashTypeMod > 0x00 && hashTypeMod < 0x04
}
export function isCanonicalScriptSignature (buffer: Buffer): boolean {
if (!Buffer.isBuffer(buffer)) return false
if (!isDefinedHashType(buffer[buffer.length - 1])) return false
return bip66.check(buffer.slice(0, -1))
}
export const number = require('./script_number')
export const signature = require('./script_signature')