UNPKG

@scrypt-inc/bitcoinjs-lib

Version:
76 lines (75 loc) 2.93 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.p2data = p2data; const networks_js_1 = require('../networks.cjs'); const bscript = __importStar(require('../script.cjs')); const types_js_1 = require('../types.cjs'); const lazy = __importStar(require('./lazy.cjs')); const v = __importStar(require("valibot")); const OPS = bscript.OPS; // output: OP_RETURN ... /** * Embeds data in a Bitcoin payment. * @param a - The payment object. * @param opts - Optional payment options. * @returns The modified payment object. * @throws {TypeError} If there is not enough data or if the output is invalid. */ function p2data(a, opts) { if (!a.data && !a.output) throw new TypeError('Not enough data'); opts = Object.assign({ validate: true }, opts || {}); v.parse(v.partial(v.object({ network: v.object({}), output: types_js_1.BufferSchema, data: v.array(types_js_1.BufferSchema), })), a); const network = a.network || networks_js_1.bitcoin; const o = { name: 'embed', network }; 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(chunk => v.is(types_js_1.BufferSchema, chunk))) throw new TypeError('Output is invalid'); if (a.data && !(0, types_js_1.stacksEqual)(a.data, o.data)) throw new TypeError('Data mismatch'); } } return Object.assign(o, a); }