Skip to content

Commit 11d6d99

Browse files
authored
Merge pull request #523 from interlay/interlay-non-shared
fix: revert shared wallet
2 parents 1065cfe + 2c2b92c commit 11d6d99

14 files changed

Lines changed: 190 additions & 624 deletions

File tree

bitcoin/src/electrs/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ impl ElectrsClient {
105105
Ok(ret)
106106
}
107107

108-
pub async fn is_tx_output_spent(&self, txid: &Txid, vout: u32) -> Result<bool, Error> {
109-
let spending_value: SpendingValue = self.get_and_decode(&format!("/tx/{txid}/outspend/{vout}")).await?;
110-
Ok(spending_value.spent)
111-
}
112-
113108
pub async fn get_blocks_tip_height(&self) -> Result<u32, Error> {
114109
Ok(self.get("/blocks/tip/height").await?.parse()?)
115110
}

bitcoin/src/electrs/types.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct TransactionStatus {
1515
}
1616

1717
// https://github.com/Blockstream/electrs/blob/adedee15f1fe460398a7045b292604df2161adc0/src/rest.rs#L167-L189
18-
#[derive(Deserialize, Clone)]
18+
#[derive(Deserialize)]
1919
pub struct TxInValue {
2020
pub txid: Txid,
2121
pub vout: u32,
@@ -33,7 +33,7 @@ pub struct TxInValue {
3333
}
3434

3535
// https://github.com/Blockstream/electrs/blob/adedee15f1fe460398a7045b292604df2161adc0/src/rest.rs#L239-L270
36-
#[derive(Deserialize, Clone)]
36+
#[derive(Deserialize)]
3737
pub struct TxOutValue {
3838
pub scriptpubkey: ScriptBuf,
3939
pub scriptpubkey_asm: String,
@@ -66,15 +66,3 @@ pub struct UtxoValue {
6666
pub status: TransactionStatus,
6767
pub value: u64,
6868
}
69-
70-
// https://github.com/Blockstream/electrs/blob/adedee15f1fe460398a7045b292604df2161adc0/src/rest.rs#L448-L457
71-
#[derive(Deserialize)]
72-
pub struct SpendingValue {
73-
pub spent: bool,
74-
#[serde(skip_serializing_if = "Option::is_none")]
75-
pub txid: Option<Txid>,
76-
#[serde(skip_serializing_if = "Option::is_none")]
77-
pub vin: Option<u32>,
78-
#[serde(skip_serializing_if = "Option::is_none")]
79-
pub status: Option<TransactionStatus>,
80-
}

bitcoin/src/iter.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,15 @@ mod tests {
207207
async fn wait_for_block(&self, height: u32, num_confirmations: u32) -> Result<Block, Error>;
208208
fn get_balance(&self, min_confirmations: Option<u32>) -> Result<Amount, Error>;
209209
fn list_transactions(&self, max_count: Option<usize>) -> Result<Vec<json::ListTransactionResult>, Error>;
210-
fn list_addresses(&self) -> Result<Vec<Address>, Error>;
211210
async fn get_block_count(&self) -> Result<u64, Error>;
212211
async fn get_raw_tx(&self, txid: &Txid, block_hash: &BlockHash) -> Result<Vec<u8>, Error>;
213212
async fn get_transaction(&self, txid: &Txid, block_hash: Option<BlockHash>) -> Result<Transaction, Error>;
214213
async fn get_proof(&self, txid: Txid, block_hash: &BlockHash) -> Result<Vec<u8>, Error>;
215214
async fn get_block_hash(&self, height: u32) -> Result<BlockHash, Error>;
216215
async fn get_new_address(&self) -> Result<Address, Error>;
217-
async fn get_new_sweep_address(&self) -> Result<Address, Error>;
218-
async fn get_last_sweep_height(&self) -> Result<Option<u32>, Error>;
219216
async fn get_new_public_key(&self) -> Result<PublicKey, Error>;
220-
fn dump_private_key(&self, address: &Address) -> Result<PrivateKey, Error>;
221-
fn import_private_key(&self, private_key: &PrivateKey, is_derivation_key: bool) -> Result<(), Error>;
217+
fn dump_derivation_key(&self, public_key: &PublicKey) -> Result<PrivateKey, Error>;
218+
fn import_derivation_key(&self, private_key: &PrivateKey) -> Result<(), Error>;
222219
async fn add_new_deposit_key(
223220
&self,
224221
public_key: PublicKey,
@@ -253,7 +250,6 @@ mod tests {
253250
fee_rate: SatPerVbyte,
254251
num_confirmations: u32,
255252
) -> Result<TransactionMetadata, Error>;
256-
async fn sweep_funds(&self, address: Address) -> Result<Txid, Error>;
257253
async fn create_or_load_wallet(&self) -> Result<(), Error>;
258254
async fn rescan_blockchain(&self, start_height: usize, end_height: usize) -> Result<(), Error>;
259255
async fn rescan_electrs_for_addresses(

bitcoin/src/lib.rs

Lines changed: 12 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ pub use sp_core::H256;
5353
use std::{
5454
convert::TryInto,
5555
future::Future,
56-
str::FromStr,
5756
sync::Arc,
5857
time::{Duration, Instant},
5958
};
@@ -101,8 +100,6 @@ const RANDOMIZATION_FACTOR: f64 = 0.25;
101100
const DERIVATION_KEY_LABEL: &str = "derivation-key";
102101
const DEPOSIT_LABEL: &str = "deposit";
103102

104-
const SWEEP_ADDRESS: &str = "sweep-address";
105-
106103
fn get_exponential_backoff() -> ExponentialBackoff {
107104
ExponentialBackoff {
108105
current_interval: INITIAL_INTERVAL,
@@ -150,8 +147,6 @@ pub trait BitcoinCoreApi {
150147

151148
fn list_transactions(&self, max_count: Option<usize>) -> Result<Vec<json::ListTransactionResult>, Error>;
152149

153-
fn list_addresses(&self) -> Result<Vec<Address>, Error>;
154-
155150
async fn get_raw_tx(&self, txid: &Txid, block_hash: &BlockHash) -> Result<Vec<u8>, Error>;
156151

157152
async fn get_transaction(&self, txid: &Txid, block_hash: Option<BlockHash>) -> Result<Transaction, Error>;
@@ -162,15 +157,11 @@ pub trait BitcoinCoreApi {
162157

163158
async fn get_new_address(&self) -> Result<Address, Error>;
164159

165-
async fn get_new_sweep_address(&self) -> Result<Address, Error>;
166-
167-
async fn get_last_sweep_height(&self) -> Result<Option<u32>, Error>;
168-
169160
async fn get_new_public_key(&self) -> Result<PublicKey, Error>;
170161

171-
fn dump_private_key(&self, address: &Address) -> Result<PrivateKey, Error>;
162+
fn dump_derivation_key(&self, public_key: &PublicKey) -> Result<PrivateKey, Error>;
172163

173-
fn import_private_key(&self, private_key: &PrivateKey, is_derivation_key: bool) -> Result<(), Error>;
164+
fn import_derivation_key(&self, private_key: &PrivateKey) -> Result<(), Error>;
174165

175166
async fn add_new_deposit_key(&self, public_key: PublicKey, secret_key: Vec<u8>) -> Result<(), Error>;
176167

@@ -213,8 +204,6 @@ pub trait BitcoinCoreApi {
213204
num_confirmations: u32,
214205
) -> Result<TransactionMetadata, Error>;
215206

216-
async fn sweep_funds(&self, address: Address) -> Result<Txid, Error>;
217-
218207
async fn create_or_load_wallet(&self) -> Result<(), Error>;
219208

220209
async fn rescan_blockchain(&self, start_height: usize, end_height: usize) -> Result<(), Error>;
@@ -370,7 +359,7 @@ impl BitcoinCoreBuilder {
370359

371360
#[derive(Clone)]
372361
pub struct BitcoinCore {
373-
pub rpc: Arc<Client>,
362+
rpc: Arc<Client>,
374363
wallet_name: Option<String>,
375364
network: Network,
376365
transaction_creation_lock: Arc<Mutex<()>>,
@@ -482,6 +471,7 @@ impl BitcoinCore {
482471
signed_funded_raw_tx.complete,
483472
signed_funded_raw_tx.errors
484473
);
474+
485475
return Err(Error::TransactionSigningError);
486476
}
487477

@@ -724,27 +714,6 @@ impl BitcoinCoreApi for BitcoinCore {
724714
.list_transactions(None, max_count.or(Some(DEFAULT_MAX_TX_COUNT)), None, None)?)
725715
}
726716

727-
// TODO: remove this once the wallet migration has completed
728-
fn list_addresses(&self) -> Result<Vec<Address>, Error> {
729-
// Lists groups of addresses which have had their common ownership
730-
// made public by common use as inputs or as the resulting change
731-
// in past transactions
732-
let groupings: Vec<Vec<Vec<serde_json::Value>>> = self.rpc.call("listaddressgroupings", &[])?;
733-
let addresses = groupings
734-
.into_iter()
735-
.flatten()
736-
.filter_map(|group| {
737-
group
738-
.get(0)
739-
.and_then(|v| v.as_str())
740-
.map(Address::from_str)?
741-
.and_then(|x| x.require_network(self.network))
742-
.ok()
743-
})
744-
.collect::<Vec<_>>();
745-
Ok(addresses)
746-
}
747-
748717
/// Get the raw transaction identified by `Txid` and stored
749718
/// in the specified block.
750719
///
@@ -799,26 +768,6 @@ impl BitcoinCoreApi for BitcoinCore {
799768
.require_network(self.network)?)
800769
}
801770

802-
async fn get_new_sweep_address(&self) -> Result<Address, Error> {
803-
Ok(self
804-
.rpc
805-
.get_new_address(Some(SWEEP_ADDRESS), Some(AddressType::Bech32))?
806-
.require_network(self.network)?)
807-
}
808-
809-
async fn get_last_sweep_height(&self) -> Result<Option<u32>, Error> {
810-
Ok(self
811-
.rpc
812-
.list_transactions(Some(SWEEP_ADDRESS), Some(DEFAULT_MAX_TX_COUNT), None, None)?
813-
.into_iter()
814-
// we want to return None if there is no sweep tx for full nodes or new
815-
// pruned nodes and we should return an error if any tx is still in the mempool
816-
.map(|tx| tx.info.blockheight.ok_or(Error::ConfirmationError))
817-
.collect::<Result<Vec<_>, _>>()?
818-
.into_iter()
819-
.min())
820-
}
821-
822771
/// Gets a new public key for an address in the wallet
823772
async fn get_new_public_key(&self) -> Result<PublicKey, Error> {
824773
let address = self
@@ -830,16 +779,15 @@ impl BitcoinCoreApi for BitcoinCore {
830779
Ok(public_key)
831780
}
832781

833-
fn dump_private_key(&self, address: &Address) -> Result<PrivateKey, Error> {
834-
Ok(self.rpc.dump_private_key(address)?)
782+
fn dump_derivation_key(&self, public_key: &PublicKey) -> Result<PrivateKey, Error> {
783+
let address = Address::p2wpkh(public_key, self.network).map_err(ConversionError::from)?;
784+
Ok(self.rpc.dump_private_key(&address)?)
835785
}
836786

837-
fn import_private_key(&self, private_key: &PrivateKey, is_derivation_key: bool) -> Result<(), Error> {
838-
Ok(self.rpc.import_private_key(
839-
private_key,
840-
is_derivation_key.then_some(DERIVATION_KEY_LABEL),
841-
Some(false),
842-
)?)
787+
fn import_derivation_key(&self, private_key: &PrivateKey) -> Result<(), Error> {
788+
Ok(self
789+
.rpc
790+
.import_private_key(private_key, Some(DERIVATION_KEY_LABEL), Some(false))?)
843791
}
844792

845793
/// Derive and import the private key for the master public key and public secret
@@ -1063,66 +1011,6 @@ impl BitcoinCoreApi for BitcoinCore {
10631011
.await?)
10641012
}
10651013

1066-
async fn sweep_funds(&self, address: Address) -> Result<Txid, Error> {
1067-
let unspent = self.rpc.list_unspent(Some(0), None, None, None, None)?;
1068-
1069-
let mut amount = Amount::ZERO;
1070-
let mut utxos = Vec::<json::CreateRawTransactionInput>::new();
1071-
1072-
for entry in unspent {
1073-
if self.electrs_client.is_tx_output_spent(&entry.txid, entry.vout).await? {
1074-
log::info!("{}:{} already spent", entry.txid, entry.vout);
1075-
// skip if already spent
1076-
continue;
1077-
}
1078-
amount += entry.amount;
1079-
utxos.push(json::CreateRawTransactionInput {
1080-
txid: entry.txid,
1081-
vout: entry.vout,
1082-
sequence: None,
1083-
})
1084-
}
1085-
1086-
log::info!("Sweeping {} from {} utxos", amount, utxos.len());
1087-
let mut outputs = serde_json::Map::<String, serde_json::Value>::new();
1088-
outputs.insert(address.to_string(), serde_json::Value::from(amount.to_btc()));
1089-
1090-
let args = [
1091-
serde_json::to_value::<&[json::CreateRawTransactionInput]>(&utxos)?,
1092-
serde_json::to_value(outputs)?,
1093-
serde_json::to_value(0i64)?, /* locktime - default 0: see https://developer.bitcoin.org/reference/rpc/createrawtransaction.html */
1094-
serde_json::to_value(true)?, // BIP125-replaceable, aka Replace By Fee (RBF)
1095-
];
1096-
let raw_tx: String = self.rpc.call("createrawtransaction", &args)?;
1097-
1098-
let funding_opts = FundRawTransactionOptions {
1099-
fee_rate: None,
1100-
add_inputs: Some(false),
1101-
subtract_fee_from_outputs: Some(vec![0]),
1102-
..Default::default()
1103-
};
1104-
let funded_raw_tx = self.rpc.fund_raw_transaction(raw_tx, Some(&funding_opts), None)?;
1105-
1106-
let signed_funded_raw_tx =
1107-
self.rpc
1108-
.sign_raw_transaction_with_wallet(&funded_raw_tx.transaction()?, None, None)?;
1109-
1110-
if signed_funded_raw_tx.errors.is_some() {
1111-
log::warn!(
1112-
"Received bitcoin funding errors (complete={}): {:?}",
1113-
signed_funded_raw_tx.complete,
1114-
signed_funded_raw_tx.errors
1115-
);
1116-
return Err(Error::TransactionSigningError);
1117-
}
1118-
1119-
let transaction = signed_funded_raw_tx.transaction()?;
1120-
let txid = self.rpc.send_raw_transaction(&transaction)?;
1121-
log::info!("Sent sweep tx: {txid}");
1122-
1123-
Ok(txid)
1124-
}
1125-
11261014
/// Create or load a wallet on Bitcoin Core.
11271015
async fn create_or_load_wallet(&self) -> Result<(), Error> {
11281016
let wallet_name = if let Some(ref wallet_name) = self.wallet_name {
@@ -1185,7 +1073,7 @@ impl BitcoinCoreApi for BitcoinCore {
11851073
// filter to only import
11861074
// a) payments in the blockchain (not in mempool), and
11871075
// b) payments TO the address (as bitcoin core will already know about transactions spending FROM it)
1188-
let confirmed_payments_to = all_transactions.iter().filter(|tx| {
1076+
let confirmed_payments_to = all_transactions.into_iter().filter(|tx| {
11891077
if let Some(status) = &tx.status {
11901078
if !status.confirmed {
11911079
return false;

bitcoin/src/light/mod.rs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,6 @@ impl BitcoinCoreApi for BitcoinLight {
137137
Ok(Default::default())
138138
}
139139

140-
// TODO: remove this later
141-
fn list_addresses(&self) -> Result<Vec<Address>, BitcoinError> {
142-
// don't need to migrate keys
143-
Ok(Default::default())
144-
}
145-
146140
async fn get_raw_tx(&self, txid: &Txid, _block_hash: &BlockHash) -> Result<Vec<u8>, BitcoinError> {
147141
Ok(self.electrs.get_raw_tx(txid).await?)
148142
}
@@ -168,29 +162,15 @@ impl BitcoinCoreApi for BitcoinLight {
168162
Ok(self.get_change_address()?)
169163
}
170164

171-
async fn get_new_sweep_address(&self) -> Result<Address, BitcoinError> {
172-
Ok(self.get_change_address()?)
173-
}
174-
175-
async fn get_last_sweep_height(&self) -> Result<Option<u32>, BitcoinError> {
176-
Ok(None)
177-
}
178-
179165
async fn get_new_public_key(&self) -> Result<PublicKey, BitcoinError> {
180166
Ok(self.private_key.public_key(&self.secp_ctx))
181167
}
182168

183-
fn dump_private_key(&self, address: &Address) -> Result<PrivateKey, BitcoinError> {
184-
self.wallet
185-
.key_store
186-
.read()
187-
.map_err(Into::<Error>::into)?
188-
.get(address)
189-
.ok_or(Error::NoPrivateKey.into())
190-
.cloned()
169+
fn dump_derivation_key(&self, _public_key: &PublicKey) -> Result<PrivateKey, BitcoinError> {
170+
Ok(self.private_key)
191171
}
192172

193-
fn import_private_key(&self, _private_key: &PrivateKey, _is_derivation_key: bool) -> Result<(), BitcoinError> {
173+
fn import_derivation_key(&self, _private_key: &PrivateKey) -> Result<(), BitcoinError> {
194174
// nothing to do
195175
Ok(())
196176
}
@@ -339,10 +319,6 @@ impl BitcoinCoreApi for BitcoinLight {
339319
.await?)
340320
}
341321

342-
async fn sweep_funds(&self, _address: Address) -> Result<Txid, BitcoinError> {
343-
Ok(Txid::all_zeros())
344-
}
345-
346322
async fn create_or_load_wallet(&self) -> Result<(), BitcoinError> {
347323
// nothing to do
348324
Ok(())

0 commit comments

Comments
 (0)