UNPKG

filepay

Version:

Post data and upload files to bitcoin sv

1,526 lines (1,386 loc) 1.24 MB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.filepay = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ (function (Buffer){ /* Originally forked from Unwriter's Datapay https://github.com/unwriter/datapay Copied by MatterCloud (Matter Web Services Inc.) as base for 'filepay' development. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ const mingo = require("mingo") const _Buffer = require('buffer/') const bitcoin = require('bsv'); const axios = require('axios'); const textEncoder = require('text-encoder'); const bsvCoinselect = require('bsv-coinselect'); const defaults = { api_key: 'abc', // https://www.mattercloud.net rpc: "https://api.mattercloud.net", fee: 400, feeb: 1.4, } /** * Will return the associated input scriptSig address information object * @return {Address|boolean} */ var isPublicKeyHashIn = function (script) { if (script.chunks.length === 2) { var signatureBuf = script.chunks[0].buf var pubkeyBuf = script.chunks[1].buf if (signatureBuf && signatureBuf.length && signatureBuf[0] === 0x30 && pubkeyBuf && pubkeyBuf.length ) { var version = pubkeyBuf[0] if ((version === 0x04 || version === 0x06 || version === 0x07) && pubkeyBuf.length === 65) { return true } else if ((version === 0x03 || version === 0x02) && pubkeyBuf.length === 33) { return true } } } return false } /** * @returns {boolean} if this is a pay to public key input script */ var isPublicKeyIn = function (script) { if (script.chunks.length === 1) { var signatureBuf = script.chunks[0].buf if (signatureBuf && signatureBuf.length && signatureBuf[0] === 0x30) { return true } } return false } /** * Builds a scriptSig (a script for an input) that signs a public key output script. * * @param {Signature|Buffer} signature - a Signature object, or the signature in DER canonical encoding * @param {number=} sigtype - the type of the signature (defaults to SIGHASH_ALL) */ var buildPublicKeyIn = function (signature, sigtype) { // $.checkArgument(signature instanceof Signature || Buffer.isBuffer(signature)) // $.checkArgument(_.isUndefined(sigtype) || _.isNumber(sigtype)) if (signature instanceof bitcoin.Signature) { signature = signature.toBuffer() } var script = new Script() script.add(Buffer.concat([ signature, Buffer.from([(sigtype || bitcoin.Signature.SIGHASH_ALL) & 0xff]) ])) return script } /** * @returns {boolean} if this is a public key output script */ var isPublicKeyOut = function (script) { if (script.chunks.length === 2 && script.chunks[0].buf && script.chunks[0].buf.length && script.chunks[1].opcodenum === bitcoin.Opcode.OP_CHECKSIG) { var pubkeyBuf = script.chunks[0].buf var version = pubkeyBuf[0] var isVersion = false if ((version === 0x04 || version === 0x06 || version === 0x07) && pubkeyBuf.length === 65) { isVersion = true } else if ((version === 0x03 || version === 0x02) && pubkeyBuf.length === 33) { isVersion = true } if (isVersion) { return PublicKey.isValid(pubkeyBuf) } } return false } /** * @returns {boolean} if this is a pay to pubkey hash output script */ var isPublicKeyHashOut = function (script) { return !!(script.chunks.length === 5 && script.chunks[0].opcodenum === bitcoin.Opcode.OP_DUP && script.chunks[1].opcodenum === bitcoin.Opcode.OP_HASH160 && script.chunks[2].buf && script.chunks[2].buf.length === 20 && script.chunks[3].opcodenum === bitcoin.Opcode.OP_EQUALVERIFY && script.chunks[4].opcodenum === bitcoin.Opcode.OP_CHECKSIG) } /** * Builds a scriptSig (a script for an input) that signs a public key hash * output script. * * @param {Buffer|string|PublicKey} publicKey * @param {Signature|Buffer} signature - a Signature object, or the signature in DER canonical encoding * @param {number=} sigtype - the type of the signature (defaults to SIGHASH_ALL) */ var buildPublicKeyHashIn = function (publicKey, signature, sigtype) { // $.checkArgument(signature instanceof Signature || Buffer.isBuffer(signature)) // $.checkArgument(_.isUndefined(sigtype) || _.isNumber(sigtype)) // Assume Signature object hack //if (signature instanceof bitcoin.Signature) { signature = signature.toBuffer() //} var script = new bitcoin.Script() .add(Buffer.concat([ signature, Buffer.from([(sigtype || bitcoin.Signature.SIGHASH_ALL) & 0xff]) ])) .add(new bitcoin.PublicKey(publicKey).toBuffer()) return script } /** * Detect and sign p2pkh or p2pk inputs * * @param {*} tx Transaction for signature * @param {*} index Input index * @param {*} satoshis Satoshi for input * @param {*} script Script for input * @param {*} key Signing key */ var signStandard = function(tx, index, satoshis, script, key) { let unlockingScript; const privKey = new bitcoin.PrivateKey(key); const pubKey = privKey.publicKey; const sigtype = bitcoin.crypto.Signature.SIGHASH_ALL | bitcoin.crypto.Signature.SIGHASH_FORKID; const flags = bitcoin.Script.Interpreter.SCRIPT_VERIFY_MINIMALDATA | bitcoin.Script.Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID | bitcoin.Script.Interpreter.SCRIPT_ENABLE_MAGNETIC_OPCODES | bitcoin.Script.Interpreter.SCRIPT_ENABLE_MONOLITH_OPCODES; const signature = bitcoin.Transaction.sighash.sign(tx, privKey, sigtype, index, script, new bitcoin.crypto.BN(satoshis), flags); if (isPublicKeyOut(script)) { unlockingScript = buildPublicKeyIn(signature, sigtype); } else if (isPublicKeyHashOut(script)) { unlockingScript = buildPublicKeyHashIn(pubKey, signature, sigtype); } else { throw new Error('Non-standard script'); } return unlockingScript; } /** * sign p2pkh-like input. Convenience function * * @param {*} tx Transaction for signature * @param {*} index Input index * @param {*} satoshis Satoshi for input * @param {*} script Script for input * @param {*} key Signing key */ var signStandardLike = function(tx, index, satoshis, script, key) { const privKey = new bitcoin.PrivateKey(key); const pubKey = privKey.publicKey; const sigtype = bitcoin.crypto.Signature.SIGHASH_ALL | bitcoin.crypto.Signature.SIGHASH_FORKID; const flags = bitcoin.Script.Interpreter.SCRIPT_VERIFY_MINIMALDATA | bitcoin.Script.Interpreter.SCRIPT_ENABLE_SIGHASH_FORKID | bitcoin.Script.Interpreter.SCRIPT_ENABLE_MAGNETIC_OPCODES | bitcoin.Script.Interpreter.SCRIPT_ENABLE_MONOLITH_OPCODES; const signature = bitcoin.Transaction.sighash.sign(tx, privKey, sigtype, index, script, new bitcoin.crypto.BN(satoshis), flags); return buildPublicKeyHashIn(pubKey, signature, sigtype); } /** * Custom signing of a transaction similar to bsv.js 1.5.3. * * The difference is that we first "sign" the inputs with `unlockingScript` function return. * After the inputs with a provided `unlockingScript` are paired with the unlocking scripts, then * attempt to apply the provided `key` against all other inputs (checking script types p2pkh, p2pk, etc) * * This provides enough functionality to use any custom locking script UTXO as inputs. * * @param {*} tx Transaction to sign * @param {*} txoutMapToUnlockingScript - Map `txid-outputIndex` -> function(tx, prevTxId, outputIndex): Script * @param {*} key Private key to sign any inputs that do not have a manually provided unlocking script */ var signCustom = function(tx, txoutMapToUnlockingScript, key) { // Apply any manual unlocking scripts if there utxos with unlockingScript function for (let i = 0; i < tx.inputs.length; i++) { const prevTxId = tx.inputs[i].prevTxId.toString('hex'); const outputIndex = tx.inputs[i].outputIndex; const hasUnlockingScript = txoutMapToUnlockingScript.get(`${prevTxId}-${outputIndex}`); if (!hasUnlockingScript) { continue; } tx.inputs[i].setScript(hasUnlockingScript(tx, i, tx.inputs[i].output.satoshis, tx.inputs[i].output.script, key)); } // Apply the defaults if any for (let i = 0; i < tx.inputs.length; i++) { const prevTxId = tx.inputs[i].prevTxId.toString('hex'); const outputIndex = tx.inputs[i].outputIndex; const hasUnlockingScript = txoutMapToUnlockingScript.get(`${prevTxId}-${outputIndex}`); // Ignore if there is a locking script if (hasUnlockingScript) { continue; } tx.inputs[i].setScript(signStandard(tx, i, tx.inputs[i].output.satoshis, tx.inputs[i].output.script, key)); } return tx; } var dedupUtxosPreserveRequiredIfFound = function(inputs) { // Standardize up the inputs bcause some libriares need 'amount' and some 'value' let modifiedInputs = []; // Add all required inputs first let map = new Map(); for (const input of inputs) { if (input.required) { modifiedInputs.push(input); map.set(input.txid + '-' + input.outputIndex, true); } } // Add all non-required inputs for (const input of inputs) { // Skip duplicate utxos if (map.get(input.txid + '-' + input.outputIndex)) { continue; } if (!input.required) { modifiedInputs.push(input); map.set(input.txid + '-' + input.outputIndex, true); } } return modifiedInputs; } var calculateFee = function(inputs, outputs) { let inputSum = 0; let outputSum = 0; if (inputs) { for (const input of inputs) { inputSum += input.value; } } if (outputs) { for (const output of outputs) { outputSum += output.value; } } return inputSum - outputSum; } var buildTransactionInputsOutputs = function(inputs, outputs) { // Standardize up the inputs bcause some libriares need 'amount' and some 'value' let modifiedInputs = []; let map = new Map(); for (const input of inputs) { // Skip duplicate utxos if (map.get(input.txid + '-' + input.outputIndex)) { continue; } modifiedInputs.push(Object.assign({}, input, { amount: input.value / 100000000, satoshis: input.value })); map.set(input.txid + '-' + input.outputIndex, true); } let tx = new bitcoin.Transaction().from(modifiedInputs); if (outputs) { for (const output of outputs) { if (output.script) { const a = (new bitcoin.Script(output.script)).toString(); tx.addOutput(new bitcoin.Transaction.Output({ script: a, satoshis: output.value })); } } } // Safety check to ensure fee never goes greater than 0.1 BSV let sumInputValues = 0; let sumOutputValues = 0; for (const input of tx.inputs) { sumInputValues += input.output.satoshis; } for (const output of tx.outputs) { sumOutputValues += output.satoshis; } // -------------------------------------------------- // The fee would be greater than 0.1 BSV // There's no need for that big of a fee since miners are mining 10MB tx max size at 0.5 sat/byte if (!isNaN(sumInputValues) && !isNaN(sumOutputValues) && sumInputValues >= 0 && sumOutputValues >= 0 && (sumInputValues - sumOutputValues <= 10000000)) { return tx; } // Limit can be changed in future. For now throw Error throw new Error('Too large fee error'); } var selectCoins = function(utxos, outputs, feeRate, changeScript) { return bsvCoinselect(utxos, outputs, feeRate, changeScript); } // The end goal of 'build' is to create a hex formated transaction object // therefore this function must end with _tx() for all cases // and return a hex formatted string of either a tranaction or a script var build = function(options, callback) { let script = null; if (options.tx) { // if tx exists, check to see if it's already been signed. // if it's a signed transaction // and the request is trying to override using 'data' or 'pay', // we should throw an error let tx = new bitcoin.Transaction(options.tx) // transaction is already signed if (tx.inputs.length > 0 && tx.inputs[0].script) { if (options.pay || options.data) { callback(new Error("the transaction is already signed and cannot be modified")) return; } } return tx; } else { // construct script only if transaction doesn't exist // if a 'transaction' attribute exists, the 'data' should be ignored to avoid confusion if (options.data) { script = _script(options) } } // Instantiate pay if (options.pay && options.pay.key) { // key exists => create a signed transaction let key = options.pay.key; const privateKey = new bitcoin.PrivateKey(key); const address = privateKey.toAddress(); /** * * Construct a transaction for the utxos, taking care to include required=true utxos if needed. * * @param {*} err Whether error happened, if so then callback immediately * @param {*} utxos - Provided utxos manual and discovered. * @param {*} innerCallback Callback on error or success */ const processWithUtxos = function(err, utxos, innerCallback) { if (err) { innerCallback ? innerCallback(err, null, null) : ''; return; } if (!utxos || !utxos.length) { innerCallback ? innerCallback('Error: No available utxos', null, null) : ''; return; } if (options.pay.filter && options.pay.filter.q && options.pay.filter.q.find) { let f = new mingo.Query(options.pay.filter.q.find) utxos = utxos.filter(function(item) { return f.test(item) }) } const desiredOutputs = []; if (script) { desiredOutputs.push({ script: script.toHex(), value: 0 }); } // Handle multiple outputs of varying types if (options.pay.to && Array.isArray(options.pay.to)) { options.pay.to.forEach(function(receiver) { if (receiver.address) { desiredOutputs.push({ script: bitcoin.Script.fromAddress(receiver.address).toHex(), value: receiver.value }); } else if (receiver.data) { desiredOutputs.push({ script: _script({ data: receiver.data }).toHex(), value: receiver.value }); } else if (receiver.script) { desiredOutputs.push({ script: receiver.script, value: receiver.value }); } else { throw new Error('Invalid to. Required script and value'); } }); } // select coins and provide a changeScript let feeb = (options.pay && options.pay.feeb) ? options.pay.feeb : 0.5; // My default the payment key is the change script let changeScriptHex = bitcoin.Script.fromAddress(address).toHex(); // Allow the user to override it with their own address if (options.pay.changeAddress) { changeScriptHex = bitcoin.Script.fromAddress(options.pay.changeAddress).toHex(); } // Also allow the user to override it with a changeScript (in hex) if (options.pay.changeScript) { // Sanity check to make sure it's well formed valid script changeScriptHex = bitcoin.Script.fromHex(options.pay.changeScript).toHex(); } const coinSelectedBuiltTx = selectCoins(dedupUtxosPreserveRequiredIfFound(utxos), desiredOutputs, feeb, changeScriptHex); if (!coinSelectedBuiltTx.inputs || !coinSelectedBuiltTx.outputs) { innerCallback('Insufficient input utxo', null, null); return; } let tx = buildTransactionInputsOutputs(coinSelectedBuiltTx.inputs, coinSelectedBuiltTx.outputs); let actualFee = calculateFee(coinSelectedBuiltTx.inputs, coinSelectedBuiltTx.outputs); // Track a mapping of txid+index -> unlockingScript functions const utxoUnlockingMap = new Map(); for (const utxo of utxos) { if (utxo.unlockingScript) { utxoUnlockingMap.set(utxo.txid + '-' + utxo.outputIndex, utxo.unlockingScript); } } // Do not use bitcoin.Transaction.sign because it makes assumptions about the inputs // Instead we sign each input in turn using sensible defaults and calling back to unlockingScript functions if provided. let transaction = signCustom(tx, utxoUnlockingMap, key); innerCallback(null, transaction, actualFee); } // If custom inputs are provided, then attempt to use them if (options.pay.inputs && Array.isArray(options.pay.inputs)) { processWithUtxos(null, options.pay.inputs, function(err, tx, fee) { // No error and the tx is valid given just the manual inputs // Therefore return because we had everything we needed with the manual inputs to fulfil total fee/outputs if (!err && tx) { callback(null, tx, fee); return; } // On the other hand, the manual inputs are inadequate, then lookup extra utxos connect(options).getUnspentUtxos(address, function(err, utxos) { if (err) { callback(err, null, null); return; } // Merge the provided utxos with the retrieved ones then let mergedUtxos = utxos.concat(options.pay.inputs); processWithUtxos(null, mergedUtxos, function(err, tx, fee) { callback(err, tx, fee); return; }); }) .catch((ex) => { console.log('Filepay build ex', ex); callback(ex, null, null); }); }); } else { // No manual utxos provided, lookup all of them connect(options).getUnspentUtxos(address, function(err, utxos) { processWithUtxos(err, utxos, function(err, tx, fee) { callback(err, tx, fee); }); }) .catch((ex) => { console.log('Filepay build ex', ex); callback(ex, null, null); }); } } else { const desiredOutputs = []; if (script) { desiredOutputs.push({ script: script, value: 0 }); } // Handle multiple outputs of varying types if (options.pay && options.pay.to && Array.isArray(options.pay.to)) { options.pay.to.forEach(function(receiver) { if (receiver.address) { desiredOutputs.push({ script: bitcoin.Script.fromAddress(receiver.address).toHex(), value: receiver.value }); } else if (receiver.data) { desiredOutputs.push({ script: _script({ data: receiver.data }).toHex(), value: receiver.value }); } else if (receiver.script) { desiredOutputs.push({ script: receiver.script, value: receiver.value }); } }); } // key doesn't exist => create an unsigned transaction let fee = (options.pay && options.pay.fee) ? options.pay.fee : defaults.fee; let tx = new bitcoin.Transaction(options.tx).fee(fee); for (const out of desiredOutputs) { tx.addOutput(new bitcoin.Transaction.Output({ script: out.script, satoshis: out.value })); } callback(null, tx, null) } } var buildHeader = function(options) { if (!options) { return { headers: { 'Content-Type': 'application/json' } }; } if (options && options.pay && options.pay.api_key) { return { headers: { 'Content-Type': 'application/json', api_key: options.pay.api_key } }; } if (options && options.api_key) { return { headers: { 'Content-Type': 'application/json', api_key: options.api_key } }; } return {} }; var send = function(options, callback) { if (!callback) { callback = function() {}; } build(options, function(err, tx, fee) { if (err) { callback(err); return; } let rpcaddr = (options.pay && options.pay.rpc) ? options.pay.rpc : defaults.rpc; axios.post(`${rpcaddr}/api/v3/main/merchants/tx/broadcast`, { rawtx: tx.toString() }, buildHeader(options), ).then((res) => { callback(null, res.data.result.txid, fee, tx.toString()); }).catch((ex) => { console.log('filepay ex', ex); callback(ex, tx.hash, fee, tx.toString()); }); }) } // compose script var _script = function(options) { var s = null; if (options.data) { if (Array.isArray(options.data)) { s = new bitcoin.Script(); if (!options.hasOwnProperty("safe")) { options.safe = true; } if (options.safe) { s.add(bitcoin.Opcode.OP_FALSE); } // Add op_return s.add(bitcoin.Opcode.OP_RETURN); options.data.forEach(function(item) { // add push data if (item.constructor.name === 'ArrayBuffer') { let buffer = _Buffer.Buffer.from(item) s.add(buffer) } else if (item.constructor.name === 'Buffer') { s.add(item) } else if (typeof item === 'string') { if (/^0x/i.test(item)) { // ex: 0x6d02 s.add(Buffer.from(item.slice(2), "hex")) } else { // ex: "hello" s.add(Buffer.from(item)) } } else if (typeof item === 'object' && item.hasOwnProperty('op')) { s.add({ opcodenum: item.op }) } }) } else if (typeof options.data === 'string') { // Exported transaction s = bitcoin.Script.fromHex(options.data); } } return s; } /** * Get unspent utxos for an address wrapper * * @param {*} options */ var apiClient = function(options) { return { getUnspentUtxos: async (address, callback) => { let rpcaddr = (options && options.pay && options.pay.rpc) ? options.pay.rpc : defaults.rpc; return axios.get(`${rpcaddr}/api/v3/main/address/${address}/utxo`, buildHeader(options), ) .then((response) => { if (callback) { callback(null, response.data) } return response.data; }).catch((err) => { console.log(err); if (callback) { callback(err); } return err; }) }, }; } var callbackAndResolve = function (resolveOrReject, data, callback) { if (callback) { callback(data); } if (resolveOrReject) { return resolveOrReject(data) } } var hexEncode = function(str) { function buf2hex(buffer) { const hexStr = Array.prototype.map.call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2)).join(''); return hexStr.toLowerCase(); } const checkHexPrefixRegex = /^0x(.*)/i; const match = checkHexPrefixRegex.exec(str); if (match) { return str; } else { let enc = new textEncoder.TextEncoder().encode(str); return buf2hex(enc) } } var hexEncodeIfNeeded = function(data) { if (!data) { return '0x00'; } const checkHexPrefixRegex = /^0x(.*)/i; const match = checkHexPrefixRegex.exec(data); if (match && match[1]) { return data.toLowerCase(); } return '0x' + hexEncode(data).toLowerCase(); } var isUtf8 = function(encoding) { if (!encoding || /\s*/i.test(encoding)) { return true; } return /utf\-?8$/i.test(encoding); } var buildFile = function(request, callback) { return new Promise((resolve, reject) => { if (!request.file) { return callbackAndResolve(resolve, { success: false, message: 'file required' }, callback); } if (!request.file.content) { return callbackAndResolve(resolve, { success: false, message: 'content required' }, callback); } if (!request.file.contentType) { return callbackAndResolve(resolve, { success: false, message: 'contentType required' }, callback); } try { let encoding = request.file.encoding ? request.file.encoding : 'utf-8'; if (isUtf8(encoding)) { encoding = 'utf-8'; } let args = [ '0x' + Buffer.from("19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut").toString('hex'), hexEncodeIfNeeded(request.file.content), hexEncodeIfNeeded(request.file.contentType), hexEncodeIfNeeded(encoding) ]; const hasFileName = request.file.name && request.file.name !== ''; let filename = request.file.name ? request.file.name : '0x00'; args.push(hexEncodeIfNeeded(filename)); if (request.file.tags) { request.file.tags.map((tag) => args.push(hexEncodeIfNeeded(tag))); } // Attach signatures if they are provided if (request.signatures && Array.isArray(request.signatures)) { for (const signatureKey of request.signatures) { if (!signatureKey.key || /^\s*$/.test(signatureKey.key)) { return callbackAndResolve(resolve, { success: false, message: 'signature key required' }, callback); } const identityPrivateKey = new filepay.bsv.PrivateKey(signatureKey.key); const identityAddress = identityPrivateKey.toAddress().toLegacyAddress(); args.push('0x' + Buffer.from('|').toString('hex')); const opReturnHexArray = Utils.buildAuthorIdentity({ args: args, address: identityAddress, key: signatureKey.key, indexes: signatureKey.indexes ? signatureKey.indexes : undefined }); args = args.concat(opReturnHexArray); } } return callbackAndResolve(resolve, { success: true, data: args, }, callback); } catch (ex) { console.log('ex', ex); callbackAndResolve(resolve, { success: false, message: ex.message ? ex.message : ex.toString() }, callback) } }); } var putFile = async (request, callback) => { if (!request.pay || !request.pay.key || request.pay.key === '') { return new Promise((resolve) => { return callbackAndResolve(resolve, { success: false, message: 'key required' }, request.callback); }); } const buildResult = await buildFile(request); if (!buildResult.success) { return new Promise((resolve) => { callbackAndResolve(resolve, { success: false, message: buildResult.message }, request.callback); }); } const newArgs = []; for (const i of buildResult.data) { const checkHexPrefixRegex = /^0x(.*)/i; const match = checkHexPrefixRegex.exec(i); if (match && match[1]) { newArgs.push(i); } else { newArgs.push('0x' + i); } } return send({ safe: true, data: newArgs, pay: request.pay, rpc: request.rpc, api_key: request.api_key }, callback); } /** * Queue a request to cache the file on BitcoinFiles.org and settle on BSV blockchain after payment is received. * The response contains a 'payment_sats_needed' field and an 'payment_address` that can be used to pay for queuing into a tx. * @param {*} requestRequest { name: 'filename', data: '0933923...', encoding: 'hex' } * @param {*} callback */ var queueFile = async (request, callback) => { var formData = new FormData(); formData.append("file", Buffer.from(request.data, request.encoding)); axios.post(`${rpcaddr}/upload`, formData, { headers: { 'Content-Type': 'multipart/form-data' } } ).then((res) => { callback(null, res.data); }).catch((ex) => { callback(ex, null); }); } var connect = function(options) { return apiClient(options); } var data2script = function(scriptArray) { if (!scriptArray || !Array.isArray(scriptArray)) { return ''; } var s = new bitcoin.Script(); s.add(bitcoin.Opcode.OP_FALSE); s.add(bitcoin.Opcode.OP_RETURN); scriptArray.forEach(function(item) { // add push data if (item.constructor.name === 'ArrayBuffer') { let buffer = _Buffer.Buffer.from(item) s.add(buffer) } else if (item.constructor.name === 'Buffer') { s.add(item) } else if (typeof item === 'string') { if (/^0x/i.test(item)) { // ex: 0x6d02 s.add(Buffer.from(item.slice(2), "hex")) } else { // ex: "hello" s.add(Buffer.from(item)) } } else if (typeof item === 'object' && item.hasOwnProperty('op')) { s.add({ opcodenum: item.op }) } }) return s; } module.exports = { putFile: putFile, queueFile: queueFile, build: build, send: send, bsv: bitcoin, connect: connect, data2script: data2script, coinselect: bsvCoinselect, signStandard: signStandard, signStandardLike: signStandardLike } /* // Post File or object require('filepay').putFile({ file: { content: 'Hello world!', contentType: 'text/plain', encoding: 'utf8', name: 'hello.txt' }, pay: { key: "58Jd09..." } }); // Post OP_RETURN strings or hex data require('filepay').send({ data: [ "0x6d02", "Hello world!"], pay: { api_key: "goes here", key: "58Jd09...", } }); */ /* filepay.send({ data: ["0x6d02", "hello from filepay"], pay: { key: "....", to: [ // Attach another OP_RETURN for a text file { "data": ["19HxigV4QyBv3tHpQVcUEQyq1pzZVdoAut", "Hello from inside a text file", "text/plain"], "value": 0 }, // Pay a public address { "address": "131xY3twRUJ1Y9Z9jJFKGLUa4SAdRJppcW", "value": 546 }, // Pay arbitrary script { "script": "OP_DUP OP_HASH160 20 0x717ff56bc729556b30b456e91b68faec709993ac OP_EQUALVERIFY OP_CHECKSIG", "value": 546 } ] } }); */ // Example: https://whatsonchain.com/tx/25418da84000051d43776370cc671278241177dcff424c7618fc9dc5b6fa7fdf /* filepay.send({ data: ["hello world"], pay: { key: privKey, inputs: [ { "txid": "19b99a8b4a8c8c1d2e3130945aeda7d8070104af2ff9320667d95fd1a311ea12", "value": 786, "script": "76a914161e9c31fbec37d9ecb297bf4b814c6e189dbe5288ac", "outputIndex": 2, "required": true, "unlockingScript": function(tx, index, satoshis, script, key) { // Optional. Provide a acustom unlocking script for this custom utxo // ... // Convenience method 'filepay.signStandard' provided for standard p2pkh/p2pk // return filepay.signStandard(tx, index, satoshis, script, key); return bsv.Script(); } }, { "txid": "2f65137399213afad9804662329cf2351e46a624f9ab61a3a9e45adedb1cebbe", "value": 9305, "script": "76a914161e9c31fbec37d9ecb297bf4b814c6e189dbe5288ac", "outputIndex": 2, "required": true // assumes filepay.signStandard is used for unlockingScript } ] } }); */ /* filepay.send({ data: ["hello world"], pay: { key: privKey, inputs: [ { "txid": "19b99a8b4a8c8c1d2e3130945aeda7d8070104af2ff9320667d95fd1a311ea12", "value": 786, "script": "76a914161e9c31fbec37d9ecb297bf4b814c6e189dbe5288ac", "outputIndex": 2, "required": true, "unlockingScript": function(tx, index, satoshis, script, key) { // Optional. Provide a acustom unlocking script for this custom utxo // ... // Convenience method 'filepay.signStandard' provided for standard p2pkh/p2pk return filepay.signStandard(tx, index, satoshis, script, key); } } ] } }); */ /* filepay.send({ data: ["hello world"], pay: { key: privKey, inputs: [ { "txid": "19b99a8b4a8c8c1d2e3130945aeda7d8070104af2ff9320667d95fd1a311ea12", "value": 786, "script": "76a914161e9c31fbec37d9ecb297bf4b814c6e189dbe5288ac", "outputIndex": 2, "required": true, "unlockingScript": function(tx, index, satoshis, script, key) { // Optional. Provide a acustom unlocking script for this custom utxo // ... // Convenience method 'filepay.signStandard' provided for standard p2pkh/p2pk // return filepay.signStandard(tx, index, satoshis, script, key); // ---------- CUSTOM UNLOCKING EXAMPLE --------- // To unlock any custom p2pkh-like locking script.... return filepay.signStandardLike(tx, index, satoshis, script, key); } } ] } }); */ }).call(this,require("buffer").Buffer) },{"axios":2,"bsv":36,"bsv-coinselect":34,"buffer":176,"buffer/":83,"mingo":120,"text-encoder":125}],2:[function(require,module,exports){ module.exports = require('./lib/axios'); },{"./lib/axios":4}],3:[function(require,module,exports){ 'use strict'; var utils = require('./../utils'); var settle = require('./../core/settle'); var buildURL = require('./../helpers/buildURL'); var buildFullPath = require('../core/buildFullPath'); var parseHeaders = require('./../helpers/parseHeaders'); var isURLSameOrigin = require('./../helpers/isURLSameOrigin'); var createError = require('../core/createError'); module.exports = function xhrAdapter(config) { return new Promise(function dispatchXhrRequest(resolve, reject) { var requestData = config.data; var requestHeaders = config.headers; if (utils.isFormData(requestData)) { delete requestHeaders['Content-Type']; // Let the browser set it } var request = new XMLHttpRequest(); // HTTP basic authentication if (config.auth) { var username = config.auth.username || ''; var password = config.auth.password || ''; requestHeaders.Authorization = 'Basic ' + btoa(username + ':' + password); } var fullPath = buildFullPath(config.baseURL, config.url); request.open(config.method.toUpperCase(), buildURL(fullPath, config.params, config.paramsSerializer), true); // Set the request timeout in MS request.timeout = config.timeout; // Listen for ready state request.onreadystatechange = function handleLoad() { if (!request || request.readyState !== 4) { return; } // The request errored out and we didn't get a response, this will be // handled by onerror instead // With one exception: request that using file: protocol, most browsers // will return status as 0 even though it's a successful request if (request.status === 0 && !(request.responseURL && request.responseURL.indexOf('file:') === 0)) { return; } // Prepare the response var responseHeaders = 'getAllResponseHeaders' in request ? parseHeaders(request.getAllResponseHeaders()) : null; var responseData = !config.responseType || config.responseType === 'text' ? request.responseText : request.response; var response = { data: responseData, status: request.status, statusText: request.statusText, headers: responseHeaders, config: config, request: request }; settle(resolve, reject, response); // Clean up request request = null; }; // Handle browser request cancellation (as opposed to a manual cancellation) request.onabort = function handleAbort() { if (!request) { return; } reject(createError('Request aborted', config, 'ECONNABORTED', request)); // Clean up request request = null; }; // Handle low level network errors request.onerror = function handleError() { // Real errors are hidden from us by the browser // onerror should only fire if it's a network error reject(createError('Network Error', config, null, request)); // Clean up request request = null; }; // Handle timeout request.ontimeout = function handleTimeout() { var timeoutErrorMessage = 'timeout of ' + config.timeout + 'ms exceeded'; if (config.timeoutErrorMessage) { timeoutErrorMessage = config.timeoutErrorMessage; } reject(createError(timeoutErrorMessage, config, 'ECONNABORTED', request)); // Clean up request request = null; }; // Add xsrf header // This is only done if running in a standard browser environment. // Specifically not if we're in a web worker, or react-native. if (utils.isStandardBrowserEnv()) { var cookies = require('./../helpers/cookies'); // Add xsrf header var xsrfValue = (config.withCredentials || isURLSameOrigin(fullPath)) && config.xsrfCookieName ? cookies.read(config.xsrfCookieName) : undefined; if (xsrfValue) { requestHeaders[config.xsrfHeaderName] = xsrfValue; } } // Add headers to the request if ('setRequestHeader' in request) { utils.forEach(requestHeaders, function setRequestHeader(val, key) { if (typeof requestData === 'undefined' && key.toLowerCase() === 'content-type') { // Remove Content-Type if data is undefined delete requestHeaders[key]; } else { // Otherwise add header to the request request.setRequestHeader(key, val); } }); } // Add withCredentials to request if needed if (!utils.isUndefined(config.withCredentials)) { request.withCredentials = !!config.withCredentials; } // Add responseType to request if needed if (config.responseType) { try { request.responseType = config.responseType; } catch (e) { // Expected DOMException thrown by browsers not compatible XMLHttpRequest Level 2. // But, this can be suppressed for 'json' type as it can be parsed by default 'transformResponse' function. if (config.responseType !== 'json') { throw e; } } } // Handle progress if needed if (typeof config.onDownloadProgress === 'function') { request.addEventListener('progress', config.onDownloadProgress); } // Not all browsers support upload events if (typeof config.onUploadProgress === 'function' && request.upload) { request.upload.addEventListener('progress', config.onUploadProgress); } if (config.cancelToken) { // Handle cancellation config.cancelToken.promise.then(function onCanceled(cancel) { if (!request) { return; } request.abort(); reject(cancel); // Clean up request request = null; }); } if (requestData === undefined) { requestData = null; } // Send the request request.send(requestData); }); }; },{"../core/buildFullPath":10,"../core/createError":11,"./../core/settle":15,"./../helpers/buildURL":19,"./../helpers/cookies":21,"./../helpers/isURLSameOrigin":23,"./../helpers/parseHeaders":25,"./../utils":27}],4:[function(require,module,exports){ 'use strict'; var utils = require('./utils'); var bind = require('./helpers/bind'); var Axios = require('./core/Axios'); var mergeConfig = require('./core/mergeConfig'); var defaults = require('./defaults'); /** * Create an instance of Axios * * @param {Object} defaultConfig The default config for the instance * @return {Axios} A new instance of Axios */ function createInstance(defaultConfig) { var context = new Axios(defaultConfig); var instance = bind(Axios.prototype.request, context); // Copy axios.prototype to instance utils.extend(instance, Axios.prototype, context); // Copy context to instance utils.extend(instance, context); return instance; } // Create the default instance to be exported var axios = createInstance(defaults); // Expose Axios class to allow class inheritance axios.Axios = Axios; // Factory for creating new instances axios.create = function create(instanceConfig) { return createInstance(mergeConfig(axios.defaults, instanceConfig)); }; // Expose Cancel & CancelToken axios.Cancel = require('./cancel/Cancel'); axios.CancelToken = require('./cancel/CancelToken'); axios.isCancel = require('./cancel/isCancel'); // Expose all/spread axios.all = function all(promises) { return Promise.all(promises); }; axios.spread = require('./helpers/spread'); module.exports = axios; // Allow use of default import syntax in TypeScript module.exports.default = axios; },{"./cancel/Cancel":5,"./cancel/CancelToken":6,"./cancel/isCancel":7,"./core/Axios":8,"./core/mergeConfig":14,"./defaults":17,"./helpers/bind":18,"./helpers/spread":26,"./utils":27}],5:[function(require,module,exports){ 'use strict'; /** * A `Cancel` is an object that is thrown when an operation is canceled. * * @class * @param {string=} message The message. */ function Cancel(message) { this.message = message; } Cancel.prototype.toString = function toString() { return 'Cancel' + (this.message ? ': ' + this.message : ''); }; Cancel.prototype.__CANCEL__ = true; module.exports = Cancel; },{}],6:[function(require,module,exports){ 'use strict'; var Cancel = require('./Cancel'); /** * A `CancelToken` is an object that can be used to request cancellation of an operation. * * @class * @param {Function} executor The executor function. */ function CancelToken(executor) { if (typeof executor !== 'function') { throw new TypeError('executor must be a function.'); } var resolvePromise; this.promise = new Promise(function promiseExecutor(resolve) { resolvePromise = resolve; }); var token = this; executor(function cancel(message) { if (token.reason) { // Cancellation has already been requested return; } token.reason = new Cancel(message); resolvePromise(token.reason); }); } /** * Throws a `Cancel` if cancellation has been requested. */ CancelToken.prototype.throwIfRequested = function throwIfRequested() { if (this.reason) { throw this.reason; } }; /** * Returns an object that contains a new `CancelToken` and a function that, when called, * cancels the `CancelToken`. */ CancelToken.source = function source() { var cancel; var token = new CancelToken(function executor(c) { cancel = c; }); return { token: token, cancel: cancel }; }; module.exports = CancelToken; },{"./Cancel":5}],7:[function(require,module,exports){ 'use strict'; module.exports = function isCancel(value) { return !!(value && value.__CANCEL__); }; },{}],8:[function(require,module,exports){ 'use strict'; var utils = require('./../utils'); var buildURL = require('../helpers/buildURL'); var InterceptorManager = require('./InterceptorManager'); var dispatchRequest = require('./dispatchRequest'); var mergeConfig = require('./mergeConfig'); /** * Create a new instance of Axios * * @param {Object} instanceConfig The default config for the instance */ function Axios(instanceConfig) { this.defaults = instanceConfig; this.interceptors = { request: new InterceptorManager(), response: new InterceptorManager() }; } /** * Dispatch a request * * @param {Object} config The config specific for this request (merged with this.defaults) */ Axios.prototype.request = function request(config) { /*eslint no-param-reassign:0*/ // Allow for axios('example/url'[, config]) a la fetch API if (typeof config === 'string') { config = arguments[1] || {}; config.url = arguments[0]; } else { config = config || {}; } config = mergeConfig(this.defaults, config); // Set config.method if (config.method) { config.method = config.method.toLowerCase(); } else if (this.defaults.method) { config.method = this.defaults.method.toLowerCase(); } else { config.method = 'get'; } // Hook up interceptors middleware var chain = [dispatchRequest, undefined]; var promise = Promise.resolve(config); this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) { chain.unshift(interceptor.fulfilled, interceptor.rejected); }); this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) { chain.push(interceptor.fulfilled, interceptor.rejected); }); while (chain.length) { promise = promise.then(chain.shift(), chain.shift()); } return promise; }; Axios.prototype.getUri = function getUri(config) { config = mergeConfig(this.defaults, config); return buildURL(config.url, config.params, config.paramsSerializer).replace(/^\?/, ''); }; // Provide aliases for supported request methods utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) { /*eslint func-names:0*/ Axios.prototype[method] = function(url, config) { return this.request(utils.merge(config || {}, { method: method, url: url })); }; }); utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) { /*eslint func-names:0*/ Axios.prototype[method] = function(url, data, config) { return this.request(utils.merge(config || {}, { method: method, url: url, data: data })); }; }); module.exports = Axios; },{"../helpers/buildURL":19,"./../utils":27,"./InterceptorManager":9,"./dispatchRequest":12,"./mergeConfig":14}],9:[function(require,module,exports){ 'use strict'; var utils = require('./../utils'); function InterceptorManager() { this.handlers = []; } /** * Add a new interceptor to the stack * * @param {Function} fulfilled The function to handle `then` for a `Promise` * @param {Function} rejected The function to handle `reject` for a `Promise` * * @return {Number} An ID used to remove interceptor later */ InterceptorManager.prototype.use = function use(fulfilled, rejected) { this.handlers.push({ fulfilled: fulfilled, rejected: rejected }); return this.handlers.length - 1; }; /** * Remove an interceptor from the stack * * @param {Number} id The ID that was returned by `use` */ InterceptorManager.prototype.eject = function eject(id) { if (this.handlers[id]) { this.handlers[id] = null; } }; /** * Iterate over all the registered interceptors * * This method is particularly useful for skipping over any * interceptors that may have become `null` calling `eject`. * * @param {Function} fn The function to call for each interceptor */ InterceptorManager.prototype.forEach = function forEach(fn) { utils.forEach(this.handlers, function forEachHandler(h) { if (h !== null) { fn(h); } }); }; module.exports = InterceptorManager; },{"./../utils":27}],10:[function(require,module,exports){ 'use strict'; var isAbsoluteURL = require('../helpers/isAbsoluteURL'); var combineURLs = require('../helpers/combineURLs'); /** * Creates a new URL by combining the baseURL with the requestedURL, * only when the requestedURL is not already an absolute URL. * If the requestURL is absolute, this function returns the requestedURL untouched. * * @param {string} baseURL The base URL * @param {string} requestedURL Absolute or relative URL to combine * @returns {string} The combined full path */ module.exports = function buildFullPath(baseURL, requestedURL) { if (baseURL && !isAbsoluteURL(requestedURL)) { return combineURLs(baseURL, requestedURL); } return requestedURL; }; },{"../helpers/combineURLs":20,"../helpers/isAbsoluteURL":22}],11:[function(require,module,exports){ 'use strict'; var enhanceError = require('./enhanceError'); /** * Create an Error with the specified message, config, error code, request and response. * * @param {string} message The error message. * @param {Object} config The config. * @param {string} [code] The error code (for example, 'ECONNABORTED'). * @param {Object} [request] The request. * @param {Object} [response] The response. * @returns {Error} The created error. */ module.exports = function createError(message, config, code, request, response) { var error = new Error(message); return enhanceError(error, config, code, request, response); }; },{"./enhanceError":13}],12:[function(require,module,exports){ 'use strict'; var