Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should send txs', async () => {
const rawtx = '01000000012ad543bf57024014458cf524385248b479d396709e511def541952b718111b91010000006b483045022100ed160167df04b7bd63ebdc4e48b43501ce789ef9a82849245ca2e6687a0306f602201097253115885d35e7d52c6fa7f2b77d6a063dd7c4fd5253eb093c53f577de454121020e7ab2251bb7b40fb25a84906577310de5c7e510ffa376492ccf3fa2e91deb2cffffffff02da940200000000001976a9148f881918cd3589d7ff585a0e8456fa48ea4fd30d88acac841200000000001976a91482a1a3c8458bed0a6cecbd7adcd39edfc11934d888ac00000000';
const tx = new bsv.Transaction(rawtx);
console.log('tx', tx.toJSON());
// decode raw transaction hex
await (async () => {
try {
let decodeRawTransaction = await BITBOX.RawTransactions.decodeRawTransaction(rawtx);
console.log(JSON.stringify(decodeRawTransaction));
} catch(error) {
console.error(error)
}})();
const cb = '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff1c03e8db082f7376706f6f6c2e636f6d2f0923a65738686a6ade010080ffffffff018419854a000000001976a914492558fb8ca71a3591316d095afc0f20ef7d42f788ac00000000';
await (async () => {
try {
if (options.file && !options.type) {
console.log('You must specify a mime type for this file, for example, --type "image/jpeg"')
process.exit(1)
}
const p = options.path
const parts = p.split('/')
parts.shift() // Skip the first element of the path
const name = parts.shift()
const data = getData(name)
let parentKey = null
let parentPath = null
const masterPrivateKey = bsv.HDPrivateKey(data.xprv)
if (parts.length === 1) {
if (parts[0] !== '0') {
throw new Error('Only one root not is allowed.')
}
} else {
parentPath = parts.slice(0, -1).join('/')
parentKey = masterPrivateKey.deriveChild('m/' + parentPath)
}
const childPath = parts.join('/')
const childKey = masterPrivateKey.deriveChild('m/' + childPath)
const fundingKey = getFundingKey()
const oprParts = []
oprParts.push('OP_RETURN')
// generate a wallet.json file
// remember to fund the wallet using the address generated
// the easiest way to do that is go to https://www.moneybutton.com/test
// and use withdraw button
// then review the tx on a block explorer e.g. https://bchsvexplorer.com
const bsv = require('bsv')
const fs = require('fs');
var pk = bsv.PrivateKey()
console.log(pk)
var address = new bsv.Address(pk.publicKey, bsv.Networks.mainnet)
console.log(address.toLegacyAddress())
wallet = {
"wif": pk.toWIF(),
"address": address.toLegacyAddress()
}
const sWallet = JSON.stringify(wallet, null, 2);
console.log(sWallet)
fs.writeFile('wallet.json', sWallet, 'utf8', function(err) {
if(err) {
return console.log(err);
}
callback(new Error("the transaction is already signed and cannot be modified"))
return;
}
}
} 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();
const insight = new explorer.Insight(rpcaddr)
insight.getUnspentUtxos(address, function (err, res) {
if (err) {
callback(err);
return;
}
if (options.pay.filter && options.pay.filter.q && options.pay.filter.q.find) {
let f = new mingo.Query(options.pay.filter.q.find)
res = res.filter(function(item) {
return f.test(item)
})
}
let tx = new bitcoin.Transaction(options.tx).from(res);
.then(utxos => {
const tx = new bsv.Transaction().change(process.env.ADDRESS),
script = new bsv.Script();
let fee = 0;
// Add OP_RETURN output
script.add(bsv.Opcode.OP_RETURN);
data.forEach(item => {
// Hex string
if (typeof item === 'string' && /^0x/i.test(item)) {
script.add(Buffer.from(item.slice(2), 'hex'))
// Opcode number
} else if (typeof item === 'number') {
script.add(item)
// Opcode
} else if (typeof item === 'object' && item.hasOwnProperty('op')) {
script.add({ opcodenum: item.op })
// All else
const result = await sendTX(tx.toString())
utxos = [bsv.Transaction.UnspentOutput({
address: parentKey.publicKey.toAddress().toString(),
txId: result,
outputIndex: 0,
satoshis: tx.outputs[0].satoshis,
scriptPubKey: tx.outputs[0].script.toHex()
})]
} else {
utxos = filterUTXOs(utxos, feeForMetanetNode)
}
// Create metanet node
// First, estimate the fee for the metanet node transaction
let metaTX = new bsv.Transaction().from(utxos)
metaTX.addOutput(new bsv.Transaction.Output({ script, satoshis: 0 }))
if (parentKey === null) {
metaTX.fee(feeForMetanetNode)
metaTX.change(fundingKey.publicKey.toAddress())
metaTX.sign(fundingKey.privateKey)
} else {
metaTX.sign(parentKey.privateKey)
}
const result = await sendTX(metaTX.toString())
// console.log(result, 'metaTX:', metaTX.toString())
return result
}
var _script = function(options) {
var s = null;
if (options.data) {
if (Array.isArray(options.data)) {
s = new bitcoin.Script();
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"))
_buildScript(data) {
const script = new bsv.Script()
script.add(bsv.Opcode.OP_RETURN)
data.forEach(item => {
// Hex string
if (typeof item === 'string' && /^0x/i.test(item)) {
script.add(_Buffer.from(item.slice(2), 'hex'))
// Opcode number
} else if (typeof item === 'number' || item === null) {
script.add(item || 0)
// Opcode
} else if (typeof item === 'object' && item.hasOwnProperty('op')) {
script.add({ opcodenum: item.op })
// All else
} else {
script.add(_Buffer.from(item))
}
})
.then(utxos => {
const tx = new bsv.Transaction().change(process.env.ADDRESS),
script = new bsv.Script();
let fee = 0;
// Add OP_RETURN output
script.add(bsv.Opcode.OP_RETURN);
data.forEach(item => {
// Hex string
if (typeof item === 'string' && /^0x/i.test(item)) {
script.add(Buffer.from(item.slice(2), 'hex'))
// Opcode number
} else if (typeof item === 'number') {
script.add(item)
// Opcode
} else if (typeof item === 'object' && item.hasOwnProperty('op')) {
script.add({ opcodenum: item.op })
// All else
} else {
if (index < 0) {
return {valid: false, message: "field index out of bounds < 0" };
}
if (index >= args.length) {
return {valid: false, message: "field index out of bounds > length" };
}
fieldsToSign.push(args[index]);
}
const bufWriter = new bsv.encoding.BufferWriter();
for (const fieldToSign of fieldsToSign) {
let bf = Buffer.from(fieldToSign, 'hex');
bufWriter.write(bf);
}
const appData = bufWriter.toBuffer();
//try {
const verified = bsv.Message(appData).verify(address, signature);
if (verified) {
return {
valid: true,
address: address,
signature: signature,
pos: pos,
fieldIndexesForSignature: fieldIndexesForSignature
};
}
// } catch (ex) {
// Fail silently
// todo: add debug/verbose mode in future
// console.log('ex', ex);
/// }
return {valid: false, message: 'signature not match'};
}