2017-01-03 12:45:54 +01:00
|
|
|
// OP_RETURN {aa21a9ed} {commitment}
|
2016-12-13 12:03:07 +01:00
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
import * as bscript from '../../script';
|
2019-03-07 06:16:45 +01:00
|
|
|
import { OPS } from '../../script';
|
2019-03-06 13:38:36 +01:00
|
|
|
import * as types from '../../types';
|
2018-12-29 13:39:19 +01:00
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
const typeforce = require('typeforce');
|
2016-12-13 12:03:07 +01:00
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
const HEADER: Buffer = Buffer.from('aa21a9ed', 'hex');
|
2017-01-03 12:45:54 +01:00
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
export function check(script: Buffer | Array<number | Buffer>): boolean {
|
|
|
|
const buffer = bscript.compile(script);
|
2016-12-13 12:03:07 +01:00
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
return (
|
|
|
|
buffer.length > 37 &&
|
2016-12-13 12:03:07 +01:00
|
|
|
buffer[0] === OPS.OP_RETURN &&
|
|
|
|
buffer[1] === 0x24 &&
|
2017-01-03 12:45:54 +01:00
|
|
|
buffer.slice(2, 6).equals(HEADER)
|
2019-03-06 13:38:36 +01:00
|
|
|
);
|
2016-12-13 12:03:07 +01:00
|
|
|
}
|
|
|
|
|
2019-03-21 16:15:37 +01:00
|
|
|
check.toJSON = (): string => {
|
2019-03-06 13:38:36 +01:00
|
|
|
return 'Witness commitment output';
|
|
|
|
};
|
2016-12-13 12:03:07 +01:00
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
export function encode(commitment: Buffer): Buffer {
|
|
|
|
typeforce(types.Hash256bit, commitment);
|
2016-12-13 12:03:07 +01:00
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
const buffer = Buffer.allocUnsafe(36);
|
|
|
|
HEADER.copy(buffer, 0);
|
|
|
|
commitment.copy(buffer, 4);
|
2017-04-19 09:24:58 +02:00
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
return bscript.compile([OPS.OP_RETURN, buffer]);
|
2016-12-13 12:03:07 +01:00
|
|
|
}
|
|
|
|
|
2019-03-06 13:38:36 +01:00
|
|
|
export function decode(buffer: Buffer): Buffer {
|
|
|
|
typeforce(check, buffer);
|
2016-12-13 12:03:07 +01:00
|
|
|
|
2019-03-07 06:16:45 +01:00
|
|
|
return (bscript.decompile(buffer)![1] as Buffer).slice(4, 36);
|
2016-12-13 12:03:07 +01:00
|
|
|
}
|