2019-04-21 21:30:21 +09:00
|
|
|
'use strict';
|
|
|
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
const networks_1 = require('../networks');
|
|
|
|
const bscript = require('../script');
|
|
|
|
const lazy = require('./lazy');
|
2019-01-04 18:33:02 +09:00
|
|
|
const typef = require('typeforce');
|
|
|
|
const OPS = bscript.OPS;
|
|
|
|
function stacksEqual(a, b) {
|
2019-04-21 21:30:21 +09:00
|
|
|
if (a.length !== b.length) return false;
|
|
|
|
return a.every((x, i) => {
|
|
|
|
return x.equals(b[i]);
|
|
|
|
});
|
2019-01-04 18:33:02 +09:00
|
|
|
}
|
|
|
|
// output: OP_RETURN ...
|
|
|
|
function p2data(a, opts) {
|
2019-04-21 21:30:21 +09:00
|
|
|
if (!a.data && !a.output) throw new TypeError('Not enough data');
|
|
|
|
opts = Object.assign({ validate: true }, opts || {});
|
|
|
|
typef(
|
|
|
|
{
|
|
|
|
network: typef.maybe(typef.Object),
|
|
|
|
output: typef.maybe(typef.Buffer),
|
|
|
|
data: typef.maybe(typef.arrayOf(typef.Buffer)),
|
|
|
|
},
|
|
|
|
a,
|
|
|
|
);
|
|
|
|
const network = a.network || networks_1.bitcoin;
|
2019-07-11 14:49:26 +09:00
|
|
|
const o = { name: 'embed', network };
|
2019-04-21 21:30:21 +09:00
|
|
|
lazy.prop(o, 'output', () => {
|
|
|
|
if (!a.data) return;
|
|
|
|
return bscript.compile([OPS.OP_RETURN].concat(a.data));
|
|
|
|
});
|
|
|
|
lazy.prop(o, 'data', () => {
|
|
|
|
if (!a.output) return;
|
|
|
|
return bscript.decompile(a.output).slice(1);
|
|
|
|
});
|
|
|
|
// extended validation
|
|
|
|
if (opts.validate) {
|
|
|
|
if (a.output) {
|
|
|
|
const chunks = bscript.decompile(a.output);
|
|
|
|
if (chunks[0] !== OPS.OP_RETURN) throw new TypeError('Output is invalid');
|
|
|
|
if (!chunks.slice(1).every(typef.Buffer))
|
|
|
|
throw new TypeError('Output is invalid');
|
|
|
|
if (a.data && !stacksEqual(a.data, o.data))
|
|
|
|
throw new TypeError('Data mismatch');
|
2019-01-04 18:33:02 +09:00
|
|
|
}
|
2019-04-21 21:30:21 +09:00
|
|
|
}
|
|
|
|
return Object.assign(o, a);
|
2019-01-04 18:33:02 +09:00
|
|
|
}
|
|
|
|
exports.p2data = p2data;
|