Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
} else {
msg.setOutputsList([currentTx.getOutputsList()[txRequest.getDetails().getRequestIndex()]])
msg.setOutputsCnt(1)
}
txAck = new TxAck()
txAck.setTx(msg)
let message = await transport.call(MessageType.MESSAGETYPE_TXACK, txAck, LONG_TIMEOUT, /*omitLock=*/true) as Event // 5 Minute timeout
responseType = message.message_enum
response = message.proto
continue
}
if (txRequest.getRequestType() === RequestType.TXEXTRADATA) {
let offset = txRequest.getDetails().getExtraDataOffset()
let length = txRequest.getDetails().getExtraDataLen()
msg = new TransactionType()
msg.setExtraData(currentTx.getExtraData_asU8().slice(offset, offset + length))
txAck = new TxAck()
txAck.setTx(msg)
let message = await transport.call(MessageType.MESSAGETYPE_TXACK, txAck, LONG_TIMEOUT, /*omitLock=*/true) as Event // 5 Minute timeout
responseType = message.message_enum
response = message.proto
continue
}
}
} catch (error) {
console.error({ error })
throw new Error('Failed to sign BTC transaction')
}
if (signatures.includes(null)) {
throw new Error('Some signatures are missing!')
break
}
let currentTx: TransactionType = null
let msg: TransactionType = null
let txAck: TxAck = null
// Device asked for one more information, let's process it.
if (txRequest.hasDetails() && !txRequest.getDetails().hasTxHash()) {
currentTx = txmap['unsigned']
} else {
currentTx = txmap[toHexString(txRequest.getDetails().getTxHash_asU8())]
}
if (txRequest.getRequestType() === RequestType.TXMETA) {
msg = new TransactionType()
msg.setVersion(currentTx.getVersion())
msg.setLockTime(currentTx.getLockTime())
msg.setInputsCnt(currentTx.getInputsCnt())
if (txRequest.getDetails().hasTxHash()) {
msg.setOutputsCnt(currentTx.getBinOutputsList().length)
} else {
msg.setOutputsCnt(currentTx.getOutputsList().length)
}
if (currentTx.hasExtraData()) {
msg.setExtraDataLen(currentTx.getExtraData_asU8().length)
} else {
msg.setExtraDataLen(0)
}
txAck = new TxAck()
txAck.setTx(msg)
let message = await transport.call(MessageType.MESSAGETYPE_TXACK, txAck, LONG_TIMEOUT, /*omitLock=*/true) as Event // 5 Minute timeout
continue
}
if (txRequest.getRequestType() === RequestType.TXINPUT) {
msg = new TransactionType()
msg.setInputsList([currentTx.getInputsList()[txRequest.getDetails().getRequestIndex()]])
txAck = new TxAck()
txAck.setTx(msg)
let message = await transport.call(MessageType.MESSAGETYPE_TXACK, txAck, LONG_TIMEOUT, /*omitLock=*/true) as Event // 5 Minute timeout
responseType = message.message_enum
response = message.proto
continue
}
if (txRequest.getRequestType() === RequestType.TXOUTPUT) {
msg = new TransactionType()
if (txRequest.getDetails().hasTxHash()) {
msg.setBinOutputsList([currentTx.getBinOutputsList()[txRequest.getDetails().getRequestIndex()]])
} else {
msg.setOutputsList([currentTx.getOutputsList()[txRequest.getDetails().getRequestIndex()]])
msg.setOutputsCnt(1)
}
txAck = new TxAck()
txAck.setTx(msg)
let message = await transport.call(MessageType.MESSAGETYPE_TXACK, txAck, LONG_TIMEOUT, /*omitLock=*/true) as Event // 5 Minute timeout
responseType = message.message_enum
response = message.proto
continue
}
if (txRequest.getRequestType() === RequestType.TXEXTRADATA) {
let offset = txRequest.getDetails().getExtraDataOffset()
}
if (currentTx.hasExtraData()) {
msg.setExtraDataLen(currentTx.getExtraData_asU8().length)
} else {
msg.setExtraDataLen(0)
}
txAck = new TxAck()
txAck.setTx(msg)
let message = await transport.call(MessageType.MESSAGETYPE_TXACK, txAck, LONG_TIMEOUT, /*omitLock=*/true) as Event // 5 Minute timeout
responseType = message.message_enum
response = message.proto
continue
}
if (txRequest.getRequestType() === RequestType.TXINPUT) {
msg = new TransactionType()
msg.setInputsList([currentTx.getInputsList()[txRequest.getDetails().getRequestIndex()]])
txAck = new TxAck()
txAck.setTx(msg)
let message = await transport.call(MessageType.MESSAGETYPE_TXACK, txAck, LONG_TIMEOUT, /*omitLock=*/true) as Event // 5 Minute timeout
responseType = message.message_enum
response = message.proto
continue
}
if (txRequest.getRequestType() === RequestType.TXOUTPUT) {
msg = new TransactionType()
if (txRequest.getDetails().hasTxHash()) {
msg.setBinOutputsList([currentTx.getBinOutputsList()[txRequest.getDetails().getRequestIndex()]])
} else {
msg.setOutputsList([currentTx.getOutputsList()[txRequest.getDetails().getRequestIndex()]])
msg.setOutputsCnt(1)
function prepareSignTx (
coin: Coin,
inputs: Array,
outputs: Array,
): any {
const unsignedTx = new TransactionType()
unsignedTx.setInputsCnt(inputs.length)
unsignedTx.setOutputsCnt(outputs.length)
inputs.forEach((input, i) => {
const utxo = new TxInputType()
utxo.setPrevHash(fromHexString(input.txid))
utxo.setPrevIndex(input.vout)
if (input.sequence !== undefined) utxo.setSequence(input.sequence)
utxo.setScriptType(translateInputScriptType(input.scriptType))
utxo.setAddressNList(input.addressNList)
utxo.setAmount(Number(input.amount))
unsignedTx.addInputs(utxo, i)
})
outputs.forEach((o, k) => {
const output: BTCSignTxOutput = o
inputs.forEach(inputTx => {
if (inputTx.txid in txmap)
return
if (inputTx.scriptType === BTCInputScriptType.SpendP2SHWitness ||
inputTx.scriptType === BTCInputScriptType.SpendWitness ||
inputTx.scriptType === BTCInputScriptType.External)
return
if (!inputTx.tx)
throw new Error("non-segwit inputs must have the associated prev tx")
const tx = new TransactionType()
tx.setVersion(inputTx.tx.version)
tx.setLockTime(inputTx.tx.locktime)
tx.setInputsCnt(inputTx.tx.vin.length)
tx.setOutputsCnt(inputTx.tx.vout.length)
inputTx.tx.vin.forEach((vin, i) => {
const txInput = new TxInputType()
if (vin.coinbase !== undefined) {
txInput.setPrevHash(fromHexString("\0".repeat(64)))
txInput.setPrevIndex(0xffffffff)
txInput.setScriptSig(fromHexString(vin.coinbase))
txInput.setSequence(vin.sequence)
} else {
txInput.setPrevHash(fromHexString(vin.txid))
txInput.setPrevIndex(vin.vout)
txInput.setScriptSig(fromHexString(vin.scriptSig.hex))