|
| 1 | +// RGB standard library for working with smart contracts on Bitcoin & Lightning |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +// Written in 2024 by |
| 6 | +// Zoe Faltibà <[email protected]> |
| 7 | +// Rewritten in 2024 by |
| 8 | +// Dr Maxim Orlovsky <[email protected]> |
| 9 | +// |
| 10 | +// Copyright (C) 2024 LNP/BP Standards Association. All rights reserved. |
| 11 | +// |
| 12 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 13 | +// you may not use this file except in compliance with the License. |
| 14 | +// You may obtain a copy of the License at |
| 15 | +// |
| 16 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +// |
| 18 | +// Unless required by applicable law or agreed to in writing, software |
| 19 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | +// See the License for the specific language governing permissions and |
| 22 | +// limitations under the License. |
| 23 | + |
| 24 | +use std::iter; |
| 25 | +use std::num::NonZeroU32; |
| 26 | + |
| 27 | +use bp::{ConsensusDecode, Tx, Txid}; |
| 28 | +use electrum::{Client, ElectrumApi, Param}; |
| 29 | +pub use electrum::{Config, ConfigBuilder, Error, Socks5Config}; |
| 30 | +use rgbcore::vm::{WitnessOrd, WitnessPos}; |
| 31 | +use rgbcore::ChainNet; |
| 32 | + |
| 33 | +use super::RgbResolver; |
| 34 | + |
| 35 | +macro_rules! check { |
| 36 | + ($e:expr) => { |
| 37 | + $e.map_err(|e| e.to_string())? |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +impl RgbResolver for Client { |
| 42 | + fn check_chain_net(&self, chain_net: ChainNet) -> Result<(), String> { |
| 43 | + // check the electrum server is for the correct network |
| 44 | + let block_hash = check!(self.block_header(0)).block_hash(); |
| 45 | + if chain_net.genesis_block_hash() != block_hash { |
| 46 | + return Err(s!("resolver is for a network different from the wallet's one")); |
| 47 | + } |
| 48 | + // check the electrum server has the required functionality (verbose |
| 49 | + // transactions) |
| 50 | + let txid = match chain_net { |
| 51 | + ChainNet::BitcoinMainnet => { |
| 52 | + "33e794d097969002ee05d336686fc03c9e15a597c1b9827669460fac98799036" |
| 53 | + } |
| 54 | + ChainNet::BitcoinTestnet3 => { |
| 55 | + "5e6560fd518aadbed67ee4a55bdc09f19e619544f5511e9343ebba66d2f62653" |
| 56 | + } |
| 57 | + ChainNet::BitcoinTestnet4 => { |
| 58 | + "7aa0a7ae1e223414cb807e40cd57e667b718e42aaf9306db9102fe28912b7b4e" |
| 59 | + } |
| 60 | + ChainNet::BitcoinSignet => { |
| 61 | + "8153034f45e695453250a8fb7225a5e545144071d8ed7b0d3211efa1f3c92ad8" |
| 62 | + } |
| 63 | + ChainNet::BitcoinRegtest => { |
| 64 | + "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b" |
| 65 | + } |
| 66 | + _ => return Err(s!("only bitcoin is supported")), |
| 67 | + }; |
| 68 | + if let Err(e) = self.raw_call("blockchain.transaction.get", vec![ |
| 69 | + Param::String(txid.to_string()), |
| 70 | + Param::Bool(true), |
| 71 | + ]) { |
| 72 | + if !e |
| 73 | + .to_string() |
| 74 | + .contains("genesis block coinbase is not considered an ordinary transaction") |
| 75 | + { |
| 76 | + return Err(s!( |
| 77 | + "verbose transactions are unsupported by the provided electrum service" |
| 78 | + )); |
| 79 | + } |
| 80 | + } |
| 81 | + Ok(()) |
| 82 | + } |
| 83 | + |
| 84 | + fn resolve_pub_witness_ord(&self, txid: Txid) -> Result<WitnessOrd, String> { |
| 85 | + // We get the height of the tip of blockchain |
| 86 | + let header = check!(self.block_headers_subscribe()); |
| 87 | + |
| 88 | + // Now we get and parse transaction information to get the number of |
| 89 | + // confirmations |
| 90 | + let tx_details = match self.raw_call("blockchain.transaction.get", vec![ |
| 91 | + Param::String(txid.to_string()), |
| 92 | + Param::Bool(true), |
| 93 | + ]) { |
| 94 | + Err(e) |
| 95 | + if e.to_string() |
| 96 | + .contains("No such mempool or blockchain transaction") => |
| 97 | + { |
| 98 | + return Ok(WitnessOrd::Archived); |
| 99 | + } |
| 100 | + Err(e) => return Err(e.to_string()), |
| 101 | + Ok(v) => v, |
| 102 | + }; |
| 103 | + let forward = iter::from_fn(|| self.block_headers_pop().ok().flatten()).count() as isize; |
| 104 | + |
| 105 | + let Some(confirmations) = tx_details.get("confirmations") else { |
| 106 | + return Ok(WitnessOrd::Tentative); |
| 107 | + }; |
| 108 | + let confirmations = check!(confirmations |
| 109 | + .as_u64() |
| 110 | + .and_then(|x| u32::try_from(x).ok()) |
| 111 | + .ok_or(Error::InvalidResponse(tx_details.clone()))); |
| 112 | + if confirmations == 0 { |
| 113 | + return Ok(WitnessOrd::Tentative); |
| 114 | + } |
| 115 | + let block_time = check!(tx_details |
| 116 | + .get("blocktime") |
| 117 | + .and_then(|v| v.as_i64()) |
| 118 | + .ok_or(Error::InvalidResponse(tx_details.clone()))); |
| 119 | + |
| 120 | + let tip_height = u32::try_from(header.height).map_err(|_| s!("impossible height value"))?; |
| 121 | + let height: isize = (tip_height - confirmations) as isize; |
| 122 | + const SAFETY_MARGIN: isize = 1; |
| 123 | + // first check from expected min to max height |
| 124 | + let get_merkle_res = (1..=forward + 1) |
| 125 | + // we need this under assumption that electrum was lying due to "DB desynchronization" |
| 126 | + // since this have a very low probability we do that after everything else |
| 127 | + .chain((1..=SAFETY_MARGIN).flat_map(|i| [i + forward + 1, 1 - i])) |
| 128 | + .find_map(|offset| self.transaction_get_merkle(&txid, (height + offset) as usize).ok()) |
| 129 | + .ok_or_else(|| s!("transaction can't be located in the blockchain"))?; |
| 130 | + |
| 131 | + let tx_height = u32::try_from(get_merkle_res.block_height) |
| 132 | + .map_err(|_| s!("impossible height value"))?; |
| 133 | + |
| 134 | + let height = |
| 135 | + check!(NonZeroU32::new(tx_height).ok_or(Error::InvalidResponse(tx_details.clone()))); |
| 136 | + let pos = check!(WitnessPos::bitcoin(height, block_time) |
| 137 | + .ok_or(Error::InvalidResponse(tx_details.clone()))); |
| 138 | + |
| 139 | + Ok(WitnessOrd::Mined(pos)) |
| 140 | + } |
| 141 | + |
| 142 | + fn resolve_pub_witness(&self, txid: Txid) -> Result<Option<Tx>, String> { |
| 143 | + self.transaction_get_raw(&txid) |
| 144 | + .map_err(|e| e.to_string()) |
| 145 | + .and_then(|raw_tx| { |
| 146 | + Tx::consensus_deserialize(raw_tx) |
| 147 | + .map_err(|e| format!("cannot deserialize raw TX - {e}")) |
| 148 | + }) |
| 149 | + .map(Some) |
| 150 | + .or_else(|e| { |
| 151 | + if e.contains("No such mempool or blockchain transaction") { |
| 152 | + Ok(None) |
| 153 | + } else { |
| 154 | + Err(e) |
| 155 | + } |
| 156 | + }) |
| 157 | + } |
| 158 | +} |
0 commit comments