Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
Show First 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | const parseTxData = txData => { | ||||
continue; | continue; | ||||
} | } | ||||
parsedTx.confirmations = tx.confirmations; | parsedTx.confirmations = tx.confirmations; | ||||
parsedTx.blocktime = tx.blocktime; | parsedTx.blocktime = tx.blocktime; | ||||
let amountSent = 0; | let amountSent = 0; | ||||
let amountReceived = 0; | let amountReceived = 0; | ||||
let opReturnMessage = ''; | let opReturnMessage = ''; | ||||
let isCashtabMessage = false; | |||||
// Assume an incoming transaction | // Assume an incoming transaction | ||||
let outgoingTx = false; | let outgoingTx = false; | ||||
let tokenTx = false; | let tokenTx = false; | ||||
// If vin includes tx address, this is an outgoing tx | // If vin includes tx address, this is an outgoing tx | ||||
// Note that with bch-input data, we do not have input amounts | // Note that with bch-input data, we do not have input amounts | ||||
for (let j = 0; j < tx.vin.length; j += 1) { | for (let j = 0; j < tx.vin.length; j += 1) { | ||||
const thisInput = tx.vin[j]; | const thisInput = tx.vin[j]; | ||||
Show All 14 Lines | const parseTxData = txData => { | ||||
if (asm.includes('OP_RETURN 5262419')) { | if (asm.includes('OP_RETURN 5262419')) { | ||||
// assume this is an eToken tx for now | // assume this is an eToken tx for now | ||||
// future diffs will add additional NFT parsing logic in this segment | // future diffs will add additional NFT parsing logic in this segment | ||||
tokenTx = true; | tokenTx = true; | ||||
} else { | } else { | ||||
// if this is not an eToken tx and does not contain addresses, then assume encoded message | // if this is not an eToken tx and does not contain addresses, then assume encoded message | ||||
asm = removeOpReturnPrefixes(asm); | asm = removeOpReturnPrefixes(asm); | ||||
let msgBody = asm.substr(asm.indexOf(' ') + 1); // extract everything after the OP_RETURN opcode | let msgBody = asm.substr(asm.indexOf(' ') + 1); // extract everything after the OP_RETURN opcode | ||||
if (msgBody.split(' ')[0] === '3158065') { | |||||
// this is a Cashtab.com generated message | |||||
opReturnMessage = Buffer.from( | |||||
msgBody.substr(msgBody.indexOf(' ') + 1), | |||||
'hex', | |||||
); // extract everything after the Cashtab prefix | |||||
isCashtabMessage = true; | |||||
} else { | |||||
opReturnMessage = Buffer.from(msgBody, 'hex'); | opReturnMessage = Buffer.from(msgBody, 'hex'); | ||||
} | } | ||||
} | |||||
continue; // skipping the remainder of tx data parsing logic in both token and OP_RETURN tx cases | continue; // skipping the remainder of tx data parsing logic in both token and OP_RETURN tx cases | ||||
} | } | ||||
if ( | if ( | ||||
thisOutput.scriptPubKey.addresses && | thisOutput.scriptPubKey.addresses && | ||||
thisOutput.scriptPubKey.addresses[0] === tx.address | thisOutput.scriptPubKey.addresses[0] === tx.address | ||||
) { | ) { | ||||
if (outgoingTx) { | if (outgoingTx) { | ||||
// This amount is change | // This amount is change | ||||
continue; | continue; | ||||
} | } | ||||
amountReceived += thisOutput.value; | amountReceived += thisOutput.value; | ||||
} else if (outgoingTx) { | } else if (outgoingTx) { | ||||
amountSent += thisOutput.value; | amountSent += thisOutput.value; | ||||
// Assume there's only one destination address, i.e. it was sent by a Cashtab wallet | // Assume there's only one destination address, i.e. it was sent by a Cashtab wallet | ||||
destinationAddress = thisOutput.scriptPubKey.addresses[0]; | destinationAddress = thisOutput.scriptPubKey.addresses[0]; | ||||
} | } | ||||
} | } | ||||
// Construct parsedTx | // Construct parsedTx | ||||
parsedTx.amountSent = amountSent; | parsedTx.amountSent = amountSent; | ||||
parsedTx.amountReceived = amountReceived; | parsedTx.amountReceived = amountReceived; | ||||
parsedTx.tokenTx = tokenTx; | parsedTx.tokenTx = tokenTx; | ||||
parsedTx.outgoingTx = outgoingTx; | parsedTx.outgoingTx = outgoingTx; | ||||
parsedTx.destinationAddress = destinationAddress; | parsedTx.destinationAddress = destinationAddress; | ||||
parsedTx.opReturnMessage = opReturnMessage; | parsedTx.opReturnMessage = opReturnMessage; | ||||
parsedTx.isCashtabMessage = isCashtabMessage; | |||||
parsedTxHistory.push(parsedTx); | parsedTxHistory.push(parsedTx); | ||||
} | } | ||||
return parsedTxHistory; | return parsedTxHistory; | ||||
}; | }; | ||||
const getTxHistory = async (BCH, addresses) => { | const getTxHistory = async (BCH, addresses) => { | ||||
let txHistoryResponse; | let txHistoryResponse; | ||||
try { | try { | ||||
▲ Show 20 Lines • Show All 752 Lines • ▼ Show 20 Lines | ) => { | ||||
// Start of building the OP_RETURN output. | // Start of building the OP_RETURN output. | ||||
// only build the OP_RETURN output if the user supplied it | // only build the OP_RETURN output if the user supplied it | ||||
if ( | if ( | ||||
typeof optionalOpReturnMsg !== 'undefined' && | typeof optionalOpReturnMsg !== 'undefined' && | ||||
optionalOpReturnMsg.trim() !== '' | optionalOpReturnMsg.trim() !== '' | ||||
) { | ) { | ||||
const script = [ | const script = [ | ||||
BCH.Script.opcodes.OP_RETURN, | BCH.Script.opcodes.OP_RETURN, | ||||
Buffer.from('6d02', 'hex'), | Buffer.from('6d02', 'hex'), | ||||
bytesofman: Should be `6a04` per spec | |||||
Buffer.from('313030', 'hex'), // Cashtab's '100' prefix | |||||
bytesofmanUnsubmitted Done Inline Actions
bytesofman: 1. Add the Cashtab prefix as an app-wide paramater in the `currency` object of `Ticker.js`… | |||||
Buffer.from(optionalOpReturnMsg), | Buffer.from(optionalOpReturnMsg), | ||||
]; | ]; | ||||
const data = BCH.Script.encode(script); | const data = BCH.Script.encode(script); | ||||
transactionBuilder.addOutput(data, 0); | transactionBuilder.addOutput(data, 0); | ||||
} | } | ||||
// End of building the OP_RETURN output. | // End of building the OP_RETURN output. | ||||
let originalAmount = new BigNumber(0); | let originalAmount = new BigNumber(0); | ||||
▲ Show 20 Lines • Show All 135 Lines • Show Last 20 Lines |
Should be 6a04 per spec