Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
magic op hash(ns_id,script_pubkey,reveal_addr) consensus hash token fee (big-endian)
output 0: namespace preorder code
output 1: change address
otuput 2: burn address
*/
const burnAmount = asAmountV2(burn)
if (burnAmount.units !== 'BTC' && burnAmount.units !== 'STACKS') {
throw new Error(`Invalid burnUnits ${burnAmount.units}`)
}
const network = config.network
const burnAddress = network.getDefaultBurnAddress()
const namespaceIDBuff = Buffer.from(decodeB40(namespaceID), 'hex') // base40
const scriptPublicKey = bjsAddress.toOutputScript(preorderAddress, network.layer1)
const registerBuff = Buffer.from(registerAddress, 'ascii')
const dataBuffers = [namespaceIDBuff, scriptPublicKey, registerBuff]
const dataBuff = Buffer.concat(dataBuffers)
const hashed = hash160(dataBuff)
let btcBurnAmount = DUST_MINIMUM
let opReturnBufferLen = 39
if (burnAmount.units === 'STACKS') {
opReturnBufferLen = 47
} else {
btcBurnAmount = burnAmount.amount.toNumber()
}
const opReturnBuffer = Buffer.alloc(opReturnBufferLen)
// 3. the BURN
//
// 0 2 3 23 39 47 66
// |-----|--|--------------------------------------|--------------|-----------|-------------|
// magic op hash160(fqn,scriptPubkey,registerAddr) consensus hash token burn token type
// (optional) (optional)
//
// output 0: name preorder code
// output 1: preorder address
// output 2: burn address
//
// Returns an unsigned serialized transaction.
const burnAmount = asAmountV2(burn)
const network = config.network
const nameBuff = Buffer.from(decodeB40(fullyQualifiedName), 'hex') // base40
const scriptPublicKey = bjsAddress.toOutputScript(preorderAddress, network.layer1)
const dataBuffers = [nameBuff, scriptPublicKey]
if (!!registerAddress) {
const registerBuff = Buffer.from(registerAddress, 'ascii')
dataBuffers.push(registerBuff)
}
const dataBuff = Buffer.concat(dataBuffers)
const hashed = hash160(dataBuff)
const opReturnBufferLen = burnAmount.units === 'BTC' ? 39 : 66
const opReturnBuffer = Buffer.alloc(opReturnBufferLen)
opReturnBuffer.write(opEncode('?'), 0, 3, 'ascii')
hashed.copy(opReturnBuffer, 3)
const emsg = __.vldAlphNum((hsh || '').trim(), { strict: true, min, max })
if (emsg) return 'Invalid address'
hsh = hsh.trim()
if (hsh.startsWith('xpub') || hsh.startsWith('ltub')) {
return 'xpub/ltub addresses are not supported ' +
'(but will be in the near future)'
} else {
try {
bjsAddr.toOutputScript(this.toAddrHsh(hsh), this.net)
} catch (e) {
if (!hsh.startsWith('3')) return 'Invalid address'
try {
// fix me: workaround to prevent 'has no matching script' error:
// validate against bitcoin instead of litecoin network
// https://github.com/litecoin-project/litecoin/issues/312
bjsAddr.toOutputScript(this.toAddrHsh(hsh), this.net)
} catch (e) {
return 'Invalid address'
}
}
}
}
}
}
if (!feeRate || feeRate < 0) {
throw Error(`Must specify a transaction fee rate`)
}
let tx = new Transaction()
// add inputs from intermediate tx
for (let output of inputs) {
let txhashhex = output.txid.match(/.{2}/g).reverse().join('') // Reverse the hex string
let txid = Buffer(txhashhex, 'hex')
tx.addInput(txid, output.vout)
}
// pay to exodus address, spendable by Cosmos developers
let payToExodus = address.toOutputScript(EXODUS_ADDRESS)
tx.addOutput(payToExodus, inputAmount)
// OP_RETURN data output to specify user's Cosmos address
// this output has a value of 0. we set the address
// when we sign the transaction
let cosmosAddressScript = script.nullDataOutput(Buffer(20).fill(0))
tx.addOutput(cosmosAddressScript, 0)
// deduct fee from exodus output
let txLength = tx.byteLength() + tx.ins.length * 107 // account for input scripts
let feeAmount = txLength * feeRate
if (tx.outs[0].value - feeAmount < 0) {
throw Error(`Not enough coins given to pay fee.
tx length=${txLength}
fee rate=${feeRate} satoshi/byte
fee amount=${feeAmount} satoshis
vldAddrHsh (hsh) {
const emsg = super.vldAddrHsh(hsh)
if (emsg) return emsg
hsh = hsh.trim()
const typ = hsh.slice(0, 4) // e.g. for BTC: xpub or ypub
if (this.slip132[typ]) {
try {
this.getNode({ hsh: this.toHdAddrHshs(hsh)[1] })
} catch (e) {
return 'Invalid HD address. ' +
'Please note: Only public addresses are valid'
}
} else {
try {
bjsAddr.toOutputScript(this.toAddrHsh(hsh), this.net)
} catch (e) {
return 'Invalid address'
}
}
}
}
export const isOnchain = (input, chain, network) => {
if (!input || !chain || !network) {
return false
}
try {
address.toOutputScript(input, networks[chain][network])
return true
} catch (e) {
return false
}
}
const validateBTC = (addr) => {
try {
address.toOutputScript(addr, networks.bitcoin)
return true
} catch (error) {
return false
}
}