Fix response codes for various error states
This commit is contained in:
		
							parent
							
								
									90db8c15f2
								
							
						
					
					
						commit
						9b1fc1e000
					
				@ -252,9 +252,11 @@ class BitcoinRoutes {
 | 
			
		||||
   */
 | 
			
		||||
  private async postPsbtCompletion(req: Request, res: Response) {
 | 
			
		||||
    res.setHeader('content-type', 'text/plain');
 | 
			
		||||
    const notFoundError = `Couldn't get transaction hex for parent of input`;
 | 
			
		||||
    try {
 | 
			
		||||
      let psbt: bitcoinjs.Psbt;
 | 
			
		||||
      let format: 'hex' | 'base64';
 | 
			
		||||
      let isModified = false;
 | 
			
		||||
      try {
 | 
			
		||||
        psbt = bitcoinjs.Psbt.fromBase64(req.body);
 | 
			
		||||
        format = 'base64';
 | 
			
		||||
@ -269,21 +271,45 @@ class BitcoinRoutes {
 | 
			
		||||
      for (const [index, input] of psbt.data.inputs.entries()) {
 | 
			
		||||
        if (!input.nonWitnessUtxo) {
 | 
			
		||||
          // Buffer.from ensures it won't be modified in place by reverse()
 | 
			
		||||
          const txid = Buffer.from(psbt.txInputs[index].hash).reverse().toString('hex');
 | 
			
		||||
          const transaction: IEsploraApi.Transaction = await bitcoinApi.$getRawTransaction(txid, true);
 | 
			
		||||
          const txid = Buffer.from(psbt.txInputs[index].hash)
 | 
			
		||||
            .reverse()
 | 
			
		||||
            .toString('hex');
 | 
			
		||||
 | 
			
		||||
          let transaction: IEsploraApi.Transaction;
 | 
			
		||||
          // If missing transaction, return 404 status error
 | 
			
		||||
          try {
 | 
			
		||||
            transaction = await bitcoinApi.$getRawTransaction(txid, true);
 | 
			
		||||
            if (!transaction.hex) {
 | 
			
		||||
            throw new Error(`Couldn't get transaction hex for ${txid}`);
 | 
			
		||||
              throw new Error('');
 | 
			
		||||
            }
 | 
			
		||||
          } catch (err) {
 | 
			
		||||
            throw new Error(`${notFoundError} #${index} @ ${txid}`);
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          psbt.updateInput(index, {
 | 
			
		||||
            nonWitnessUtxo: Buffer.from(transaction.hex, 'hex'),
 | 
			
		||||
          });
 | 
			
		||||
          if (!isModified) {
 | 
			
		||||
            isModified = true;
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      if (isModified) {
 | 
			
		||||
        res.send(format === 'hex' ? psbt.toHex() : psbt.toBase64());
 | 
			
		||||
      } else {
 | 
			
		||||
        // Not modified
 | 
			
		||||
        // 422 Unprocessable Entity
 | 
			
		||||
        // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
 | 
			
		||||
        res.status(422).send(`Psbt had no missing nonUtxoWitnesses.`);
 | 
			
		||||
      }
 | 
			
		||||
    } catch (e: any) {
 | 
			
		||||
      if (e instanceof Error && new RegExp(notFoundError).test(e.message)) {
 | 
			
		||||
        res.status(404).send(e.message);
 | 
			
		||||
      } else {
 | 
			
		||||
        res.status(500).send(e instanceof Error ? e.message : e);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private async getTransactionStatus(req: Request, res: Response) {
 | 
			
		||||
    try {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user