2017-01-03 12:45:54 +01:00
|
|
|
// OP_RETURN {aa21a9ed} {commitment}
|
2016-12-13 12:03:07 +01:00
|
|
|
|
2017-05-23 07:14:08 +02:00
|
|
|
var Buffer = require('safe-buffer').Buffer
|
2016-12-13 12:03:07 +01:00
|
|
|
var bscript = require('../../script')
|
|
|
|
var types = require('../../types')
|
|
|
|
var typeforce = require('typeforce')
|
|
|
|
var OPS = require('bitcoin-ops')
|
|
|
|
|
2017-04-19 09:24:58 +02:00
|
|
|
var HEADER = Buffer.from('aa21a9ed', 'hex')
|
2017-01-03 12:45:54 +01:00
|
|
|
|
2016-12-13 12:03:07 +01:00
|
|
|
function check (script) {
|
|
|
|
var buffer = bscript.compile(script)
|
|
|
|
|
|
|
|
return buffer.length > 37 &&
|
|
|
|
buffer[0] === OPS.OP_RETURN &&
|
|
|
|
buffer[1] === 0x24 &&
|
2017-01-03 12:45:54 +01:00
|
|
|
buffer.slice(2, 6).equals(HEADER)
|
2016-12-13 12:03:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
check.toJSON = function () { return 'Witness commitment output' }
|
|
|
|
|
|
|
|
function encode (commitment) {
|
|
|
|
typeforce(types.Hash256bit, commitment)
|
|
|
|
|
2017-04-19 09:24:58 +02:00
|
|
|
var buffer = Buffer.allocUnsafe(36)
|
|
|
|
HEADER.copy(buffer, 0)
|
|
|
|
commitment.copy(buffer, 4)
|
|
|
|
|
|
|
|
return bscript.compile([OPS.OP_RETURN, buffer])
|
2016-12-13 12:03:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function decode (buffer) {
|
|
|
|
typeforce(check, buffer)
|
|
|
|
|
|
|
|
return bscript.decompile(buffer)[1].slice(4, 36)
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
check: check,
|
|
|
|
decode: decode,
|
|
|
|
encode: encode
|
|
|
|
}
|