Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
createSwapScript (recipientAddress, refundAddress, secretHash, expiration) {
recipientAddress = remove0x(addressToString(recipientAddress))
refundAddress = remove0x(addressToString(refundAddress))
const dataSizeBase = 112
const redeemDestinationBase = 66
const refundDestinationBase = 89
const expirationHex = expiration.toString(16)
const expirationEncoded = padHexStart(expirationHex) // Pad with 0
const expirationSize = expirationEncoded.length / 2
const expirationPushOpcode = (0x60 - 1 + expirationSize).toString(16)
const redeemDestinationEncoded = (redeemDestinationBase + expirationSize).toString(16)
const refundDestinationEncoded = (refundDestinationBase + expirationSize).toString(16)
const dataSizeEncoded = (dataSizeBase + expirationSize).toString(16)
return [
// Constructor
'60', dataSizeEncoded, // PUSH1 {dataSizeEncoded}
swapVout.txid = initiationTxHash
swapVout.vSat = swapVout.value * 1e8
if (swapVout.vSat - txfee < 0) {
throw new Error('Transaction amount does not cover fee.')
}
const txb = new bitcoin.TransactionBuilder(network)
if (!isRedeem) txb.setLockTime(expiration)
const prevOutScript = paymentVariant.output
txb.addInput(swapVout.txid, swapVout.n, 0, prevOutScript)
txb.addOutput(addressToString(address), swapVout.vSat - txfee)
const tx = txb.buildIncomplete()
const isSegwit = paymentVariantName === 'p2wsh' || paymentVariantName === 'p2shSegwit'
const sig = await this.getMethod('signP2SHTransaction')(
initiationTxRaw, // TODO: Why raw? can't it be a bitcoinjs-lib TX like the next one?
tx,
address,
swapVout,
isSegwit ? swapPaymentVariants.p2wsh.redeem.output : swapPaymentVariants.p2sh.redeem.output,
isRedeem ? 0 : expiration,
isSegwit
)
const walletAddress = await this.getMethod('getWalletAddress')(address)
createScript (address) {
address = addressToString(address)
const type = base58.decode(address).toString('hex').substring(0, 2).toUpperCase()
const pubKeyHash = addressToPubKeyHash(address)
if (type === networks.bitcoin_testnet.pubKeyHash) {
return [
'76', // OP_DUP
'a9', // OP_HASH160
'14', // data size to be pushed
pubKeyHash, //
'88', // OP_EQUALVERIFY
'ac' // OP_CHECKSIG
].join('')
} else if (type === networks.bitcoin_testnet.scriptHash) {
return [
'a9', // OP_HASH160
'14', // data size to be pushed
pubKeyHash, //
let blockNumber = startBlock || await this.getMethod('getBlockHeight')() // TODO: Are mempool TXs possible?
let swapTransaction = null
let arrivedAtTip = false
while (!swapTransaction) {
let block
try {
block = await this.getMethod('getBlockByNumber')(blockNumber, true)
} catch (e) {
arrivedAtTip = true
}
if (block) {
swapTransaction = block.transactions.find(predicate)
blockNumber++
}
if (arrivedAtTip) {
await sleep(5000)
}
}
return swapTransaction
}
async signMessage (message) {
const hex = Buffer.from(message).toString('hex')
const addresses = await this.getAddresses()
const address = addressToString(addresses[0])
return this.metamask('personal_sign', ensure0x(hex), ensure0x(address))
}
}, BigNumber(0)).toNumber()
const unusedAddress = await this.getUnusedAddress(true)
const { inputs, change } = await this.getInputsForAmount(totalValue)
if (change) {
outputs.push({
to: unusedAddress,
value: change.value
})
}
const txb = new bitcoin.TransactionBuilder(network)
for (const output of outputs) {
txb.addOutput(addressToString(output.to), output.value)
}
const prevOutScriptType = this.getScriptType()
for (let i = 0; i < inputs.length; i++) {
const wallet = await this.getWalletAddress(inputs[i].address)
const keyPair = await this.keyPair(wallet.derivationPath)
const paymentVariant = this.getPaymentVariantFromPublicKey(keyPair.publicKey)
txb.addInput(inputs[i].txid, inputs[i].vout, 0, paymentVariant.output)
}
for (let i = 0; i < inputs.length; i++) {
const wallet = await this.getWalletAddress(inputs[i].address)
const keyPair = await this.keyPair(wallet.derivationPath)
const paymentVariant = this.getPaymentVariantFromPublicKey(keyPair.publicKey)
async buildTransaction (to, value, data) {
const derivationPath = this._derivationPath + '0/0'
const hdKey = await this.hdKey(derivationPath)
const addresses = await this.getAddresses()
const address = addresses[0]
const from = addressToString(address)
const txData = {
to: to ? ensure0x(to) : null,
from: ensure0x(from),
value: ensure0x(BigNumber(value).toString(16)),
data: data ? ensure0x(data) : undefined
}
const [ nonce, gasPrice, gasLimit ] = await Promise.all([
this.getMethod('getTransactionCount')(remove0x(from)),
this.getMethod('getGasPrice')(),
this.getMethod('estimateGas')(txData)
])
txData.nonce = nonce
txData.gasPrice = gasPrice
async signMessage (message) {
const hex = Buffer.from(message).toString('hex')
const addresses = await this.getAddresses()
const address = addressToString(addresses[0])
return this.metamask('personal_sign', ensure0x(hex), ensure0x(address))
}
async getUnspentTransactions (address) {
address = addressToString(address)
return this.jsonrpc('getaddressutxos', { 'addresses': [address] })
}
async sendBatchTransaction (transactions) {
let outputs = {}
for (const tx of transactions) {
outputs[addressToString(tx.to)] = BigNumber(tx.value).dividedBy(1e8).toNumber()
}
const rawTxOutputs = await this.createRawTransaction([], outputs)
const rawTxFunded = await this.fundRawTransaction(rawTxOutputs)
const rawTxSigned = await this.signRawTransaction(rawTxFunded.hex)
return this.sendRawTransaction(rawTxSigned.hex)
}