bitcoinjs-lib/ts_src/templates/witnesscommitment/output.ts

37 lines
950 B
TypeScript
Raw Normal View History

// OP_RETURN {aa21a9ed} {commitment}
2018-12-28 07:09:51 +01:00
import * as bscript from '../../script'
import * as types from '../../types'
2018-06-25 08:25:12 +02:00
const typeforce = require('typeforce')
import { OPS } from '../../script'
2018-12-28 07:09:51 +01:00
const HEADER: Buffer = Buffer.from('aa21a9ed', 'hex')
2018-12-28 07:09:51 +01:00
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' }
2018-12-28 07:09:51 +01:00
export function encode (commitment: Buffer): Buffer {
typeforce(types.Hash256bit, commitment)
const buffer = Buffer.allocUnsafe(36)
2017-04-19 09:24:58 +02:00
HEADER.copy(buffer, 0)
commitment.copy(buffer, 4)
return bscript.compile([OPS.OP_RETURN, buffer])
}
2018-12-28 07:09:51 +01:00
export function decode (buffer: Buffer): Buffer {
typeforce(check, buffer)
return (<Buffer>bscript.decompile(buffer)![1]).slice(4, 36)
}