Skip to content

Commit 66be34c

Browse files
committed
Upgrade secp to 0.32.0-beta.0
Based on the release tracking PR rust-bitcoin/rust-secp256k1#843 Just do the build errors, lint warnings done next.
1 parent c0e4458 commit 66be34c

25 files changed

+299
-442
lines changed

Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@ members = ["addresses", "base58", "bitcoin", "chacha20_poly1305", "consensus_enc
33
exclude = ["benches"]
44
resolver = "2"
55

6-
# Keep this patch for hashes because secp256k1 depends on bitcoin-hashes via crates.io
7-
# This allows testing changes to hashes with secp256k1
8-
# See https://github.com/rust-bitcoin/rust-bitcoin/pull/4284#pullrequestreview-2714442229
9-
[patch.crates-io.bitcoin_hashes]
10-
path = "hashes"
6+
[patch.crates-io.secp256k1]
7+
path = "../rust-secp256k1"

bitcoin/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ hex = { package = "hex-conservative", version = "0.3.0", default-features = fals
3333
internals = { package = "bitcoin-internals", path = "../internals", features = ["alloc", "hex"] }
3434
io = { package = "bitcoin-io", path = "../io", default-features = false, features = ["alloc", "hashes"] }
3535
primitives = { package = "bitcoin-primitives", path = "../primitives", default-features = false, features = ["alloc", "hex"] }
36-
<<<<<<< Side #1 (Conflict 1 of 1)
37-
secp256k1 = { version = "0.30.0", default-features = false, features = ["hashes", "alloc"] }
38-
secp256k1 = { version = "0.31.1", default-features = false, features = ["hashes", "alloc", "rand"] }
36+
secp256k1 = { version = "0.32.0-beta.0", default-features = false, features = ["alloc", "rand"] }
3937
units = { package = "bitcoin-units", path = "../units", default-features = false, features = ["alloc"] }
4038

4139
arbitrary = { version = "1.4.1", optional = true }

bitcoin/embedded/src/main.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use core::panic::PanicInfo;
1212

1313
use alloc_cortex_m::CortexMHeap;
1414
use bitcoin::secp256k1::ffi::types::AlignedType;
15-
use bitcoin::secp256k1::Secp256k1;
1615
// use panic_halt as _;
1716
use bitcoin::{Address, Network, PrivateKey};
1817
use cortex_m_rt::entry;
@@ -30,19 +29,15 @@ fn main() -> ! {
3029

3130
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }
3231

33-
let size = Secp256k1::preallocate_size();
34-
hprintln!("secp buf size {}", size * 16).unwrap();
35-
3632
// Load a private key
3733
let raw = "L1HKVVLHXiUhecWnwFYF6L3shkf1E12HUmuZTESvBXUdx3yqVP1D";
3834
let pk = PrivateKey::from_wif(raw).unwrap();
3935
hprintln!("Seed WIF: {}", pk).unwrap();
4036

4137
let mut buf_ful = vec![AlignedType::zeroed(); size];
42-
let secp = Secp256k1::preallocated_new(&mut buf_ful).unwrap();
4338

4439
// Derive address
45-
let pubkey = pk.public_key(&secp).try_into().unwrap();
40+
let pubkey = pk.public_key().try_into().unwrap();
4641
let address = Address::p2wpkh(pubkey, Network::Bitcoin);
4742
hprintln!("Address: {}", address).unwrap();
4843

bitcoin/examples/bip32.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use bitcoin::address::{Address, KnownHrp};
44
use bitcoin::bip32::{ChildNumber, DerivationPath, Xpriv, Xpub};
55
use bitcoin::hex::FromHex;
66
use bitcoin::secp256k1::ffi::types::AlignedType;
7-
use bitcoin::secp256k1::Secp256k1;
87
use bitcoin::{CompressedPublicKey, NetworkKind};
98

109
fn main() {
@@ -27,26 +26,21 @@ fn main() {
2726

2827
let seed = Vec::from_hex(seed_hex).unwrap();
2928

30-
// we need secp256k1 context for key derivation
31-
let mut buf: Vec<AlignedType> = Vec::new();
32-
buf.resize(Secp256k1::preallocate_size(), AlignedType::zeroed());
33-
let secp = Secp256k1::preallocated_new(buf.as_mut_slice()).unwrap();
34-
3529
// calculate root key from seed
3630
let root = Xpriv::new_master(NetworkKind::Main, &seed);
3731
println!("Root key: {root}");
3832

3933
// derive child xpub
4034
let path = "84h/0h/0h".parse::<DerivationPath>().unwrap();
41-
let child = root.derive_xpriv(&secp, &path).expect("only deriving three steps");
35+
let child = root.derive_xpriv(&path).expect("only deriving three steps");
4236
println!("Child at {path}: {child}");
43-
let xpub = Xpub::from_xpriv(&secp, &child);
37+
let xpub = Xpub::from_xpriv(&child);
4438
println!("Public key at {path}: {xpub}");
4539

4640
// generate first receiving address at m/0/0
4741
// manually creating indexes this time
4842
let zero = ChildNumber::ZERO_NORMAL;
49-
let public_key = xpub.derive_xpub(&secp, [zero, zero]).unwrap().public_key;
43+
let public_key = xpub.derive_xpub([zero, zero]).unwrap().public_key;
5044
let address = Address::p2wpkh(CompressedPublicKey(public_key), KnownHrp::Mainnet);
5145
println!("First receiving address: {address}");
5246
}

bitcoin/examples/create-p2wpkh-address.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
use bitcoin::secp256k1::{rand, Secp256k1};
1+
use bitcoin::secp256k1::rand;
22
use bitcoin::{Address, CompressedPublicKey, Network, PrivateKey};
33

44
/// Generate a P2WPKH (pay-to-witness-public-key-hash) address and print it
55
/// along with the associated private key needed to transact.
66
fn main() {
7-
// Create new secp256k1 instance.
8-
let secp = Secp256k1::new();
9-
107
// Generate secp256k1 public and private key pair.
11-
let (secret_key, public_key) = secp.generate_keypair(&mut rand::rng());
8+
let (secret_key, public_key) = secp256k1::generate_keypair(&mut rand::rng());
129

1310
// Create a Bitcoin private key to be used on the Bitcoin mainnet.
1411
let private_key = PrivateKey::new(secret_key, Network::Bitcoin);

bitcoin/examples/ecdsa-psbt

Lines changed: 0 additions & 1 deletion
This file was deleted.

bitcoin/examples/ecdsa-psbt-simple.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use bitcoin::ext::*;
2929
use bitcoin::key::WPubkeyHash;
3030
use bitcoin::locktime::absolute;
3131
use bitcoin::psbt::Input;
32-
use bitcoin::secp256k1::{Secp256k1, Signing};
3332
use bitcoin::{
3433
consensus, transaction, Address, Amount, EcdsaSighashType, Network, OutPoint, Psbt,
3534
RedeemScriptBuf, ScriptPubKeyBuf, ScriptSigBuf, Sequence, Transaction, TxIn, TxOut, Txid,
@@ -55,35 +54,33 @@ const SPEND_AMOUNT: Amount = Amount::from_sat_u32(25_000_000);
5554
const CHANGE_AMOUNT: Amount = Amount::from_sat_u32(4_990_000); // 10_000 sat fee.
5655

5756
// Derive the external address xpriv.
58-
fn get_external_address_xpriv<C: Signing>(
59-
secp: &Secp256k1<C>,
57+
fn get_external_address_xpriv(
6058
master_xpriv: Xpriv,
6159
index: u32,
6260
) -> Xpriv {
6361
let derivation_path =
6462
BIP84_DERIVATION_PATH.into_derivation_path().expect("valid derivation path");
6563
let child_xpriv =
66-
master_xpriv.derive_xpriv(secp, &derivation_path).expect("only deriving three steps");
64+
master_xpriv.derive_xpriv(&derivation_path).expect("only deriving three steps");
6765
let external_index = ChildNumber::ZERO_NORMAL;
6866
let idx = ChildNumber::from_normal_idx(index).expect("valid index number");
6967

70-
child_xpriv.derive_xpriv(secp, [external_index, idx]).expect("only deriving two more steps")
68+
child_xpriv.derive_xpriv([external_index, idx]).expect("only deriving two more steps")
7169
}
7270

7371
// Derive the internal address xpriv.
74-
fn get_internal_address_xpriv<C: Signing>(
75-
secp: &Secp256k1<C>,
72+
fn get_internal_address_xpriv(
7673
master_xpriv: Xpriv,
7774
index: u32,
7875
) -> Xpriv {
7976
let derivation_path =
8077
BIP84_DERIVATION_PATH.into_derivation_path().expect("valid derivation path");
8178
let child_xpriv =
82-
master_xpriv.derive_xpriv(secp, &derivation_path).expect("only deriving three steps");
79+
master_xpriv.derive_xpriv(&derivation_path).expect("only deriving three steps");
8380
let internal_index = ChildNumber::ONE_NORMAL;
8481
let idx = ChildNumber::from_normal_idx(index).expect("valid index number");
8582

86-
child_xpriv.derive_xpriv(secp, [internal_index, idx]).expect("only deriving two more steps")
83+
child_xpriv.derive_xpriv([internal_index, idx]).expect("only deriving two more steps")
8784
}
8885

8986
// The address to send to.
@@ -128,19 +125,17 @@ fn dummy_unspent_transaction_outputs() -> Vec<(OutPoint, TxOut)> {
128125
}
129126

130127
fn main() {
131-
let secp = Secp256k1::new();
132-
133128
// Get the individual xprivs we control. In a real application these would come from a stored secret.
134129
let master_xpriv = XPRIV.parse::<Xpriv>().expect("valid xpriv");
135-
let xpriv_input_1 = get_external_address_xpriv(&secp, master_xpriv, 0);
136-
let xpriv_input_2 = get_internal_address_xpriv(&secp, master_xpriv, 0);
137-
let xpriv_change = get_internal_address_xpriv(&secp, master_xpriv, 1);
130+
let xpriv_input_1 = get_external_address_xpriv(master_xpriv, 0);
131+
let xpriv_input_2 = get_internal_address_xpriv(master_xpriv, 0);
132+
let xpriv_change = get_internal_address_xpriv(master_xpriv, 1);
138133

139134
// Get the PKs
140-
let pk_input_1 = Xpub::from_xpriv(&secp, &xpriv_input_1).to_public_key();
141-
let pk_input_2 = Xpub::from_xpriv(&secp, &xpriv_input_2).to_public_key();
135+
let pk_input_1 = Xpub::from_xpriv(&xpriv_input_1).to_public_key();
136+
let pk_input_2 = Xpub::from_xpriv(&xpriv_input_2).to_public_key();
142137
let pk_inputs = [pk_input_1, pk_input_2];
143-
let pk_change = Xpub::from_xpriv(&secp, &xpriv_change).to_public_key();
138+
let pk_change = Xpub::from_xpriv(&xpriv_change).to_public_key();
144139

145140
// Get the Witness Public Key Hashes (WPKHs)
146141
let wpkhs: Vec<WPubkeyHash> = pk_inputs.iter().map(|pk| pk.wpubkey_hash()).collect();
@@ -218,7 +213,7 @@ fn main() {
218213
];
219214

220215
// Step 3: Signer role; that signs the PSBT.
221-
psbt.sign(&master_xpriv, &secp).expect("valid signature");
216+
psbt.sign(&master_xpriv).expect("valid signature");
222217

223218
// Step 4: Finalizer role; that finalizes the PSBT.
224219
println!("PSBT Inputs: {:#?}", psbt.inputs);

bitcoin/examples/ecdsa-psbt.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use bitcoin::consensus::encode;
3636
use bitcoin::ext::*;
3737
use bitcoin::locktime::absolute;
3838
use bitcoin::psbt::{self, Input, Psbt, PsbtSighashType};
39-
use bitcoin::secp256k1::{Secp256k1, Signing, Verification};
4039
use bitcoin::{
4140
transaction, Address, Amount, CompressedPublicKey, Network, OutPoint, RedeemScriptBuf,
4241
ScriptPubKeyBuf, ScriptSigBuf, Sequence, Transaction, TxIn, TxOut, Witness,
@@ -66,17 +65,15 @@ const CHANGE_AMOUNT_BTC: &str = "48.99999 BTC"; // 1000 sat transaction fee.
6665
const NETWORK: Network = Network::Regtest;
6766

6867
fn main() -> Result<()> {
69-
let secp = Secp256k1::new();
70-
7168
let (offline, fingerprint, account_0_xpub, input_xpub) =
72-
ColdStorage::new(&secp, EXTENDED_MASTER_PRIVATE_KEY)?;
69+
ColdStorage::new(EXTENDED_MASTER_PRIVATE_KEY)?;
7370

7471
let online = WatchOnly::new(account_0_xpub, input_xpub, fingerprint);
7572

76-
let created = online.create_psbt(&secp)?;
73+
let created = online.create_psbt()?;
7774
let updated = online.update_psbt(created)?;
7875

79-
let signed = offline.sign_psbt(&secp, updated)?;
76+
let signed = offline.sign_psbt(updated)?;
8077

8178
let finalized = online.finalize_psbt(signed)?;
8279

@@ -90,6 +87,7 @@ fn main() -> Result<()> {
9087
Ok(())
9188
}
9289

90+
// TODO: This comment is stale now, re-visit.
9391
// We cache the pubkeys for convenience because it requires a secp context to convert the private key.
9492
/// An example of an offline signer i.e., a cold-storage device.
9593
struct ColdStorage {
@@ -109,20 +107,20 @@ impl ColdStorage {
109107
/// # Returns
110108
///
111109
/// The newly created signer along with the data needed to configure a watch-only wallet.
112-
fn new<C: Signing>(secp: &Secp256k1<C>, xpriv: &str) -> Result<ExportData> {
110+
fn new(xpriv: &str) -> Result<ExportData> {
113111
let master_xpriv = xpriv.parse::<Xpriv>()?;
114-
let master_xpub = Xpub::from_xpriv(secp, &master_xpriv);
112+
let master_xpub = Xpub::from_xpriv(&master_xpriv);
115113

116114
// Hardened children require secret data to derive.
117115

118116
let path = "84h/0h/0h".into_derivation_path()?;
119117
let account_0_xpriv =
120-
master_xpriv.derive_xpriv(secp, &path).expect("derivation path is short");
121-
let account_0_xpub = Xpub::from_xpriv(secp, &account_0_xpriv);
118+
master_xpriv.derive_xpriv(&path).expect("derivation path is short");
119+
let account_0_xpub = Xpub::from_xpriv(&account_0_xpriv);
122120

123121
let path = INPUT_UTXO_DERIVATION_PATH.into_derivation_path()?;
124-
let input_xpriv = master_xpriv.derive_xpriv(secp, &path).expect("derivation path is short");
125-
let input_xpub = Xpub::from_xpriv(secp, &input_xpriv);
122+
let input_xpriv = master_xpriv.derive_xpriv(&path).expect("derivation path is short");
123+
let input_xpub = Xpub::from_xpriv(&input_xpriv);
126124

127125
let wallet = ColdStorage { master_xpriv, master_xpub };
128126
let fingerprint = wallet.master_fingerprint();
@@ -134,12 +132,11 @@ impl ColdStorage {
134132
fn master_fingerprint(&self) -> Fingerprint { self.master_xpub.fingerprint() }
135133

136134
/// Signs `psbt` with this signer.
137-
fn sign_psbt<C: Signing + Verification>(
135+
fn sign_psbt(
138136
&self,
139-
secp: &Secp256k1<C>,
140137
mut psbt: Psbt,
141138
) -> Result<Psbt> {
142-
match psbt.sign(&self.master_xpriv, secp) {
139+
match psbt.sign(&self.master_xpriv) {
143140
Ok(keys) => assert_eq!(keys.len(), 1),
144141
Err((_, e)) => {
145142
let e = e.get(&0).expect("at least one error");
@@ -173,12 +170,12 @@ impl WatchOnly {
173170
}
174171

175172
/// Creates the PSBT, in BIP-0174 parlance this is the 'Creator'.
176-
fn create_psbt<C: Verification>(&self, secp: &Secp256k1<C>) -> Result<Psbt> {
173+
fn create_psbt(&self) -> Result<Psbt> {
177174
let to_address =
178175
RECEIVE_ADDRESS.parse::<Address<_>>()?.require_network(Network::Regtest)?;
179176
let to_amount = OUTPUT_AMOUNT_BTC.parse::<Amount>()?;
180177

181-
let (_, change_address, _) = self.change_address(secp)?;
178+
let (_, change_address, _) = self.change_address()?;
182179
let change_amount = CHANGE_AMOUNT_BTC.parse::<Amount>()?;
183180

184181
let tx = Transaction {
@@ -253,12 +250,11 @@ impl WatchOnly {
253250
/// "m/84h/0h/0h/1/0"). A real wallet would have access to the chain so could determine if an
254251
/// address has been used or not. We ignore this detail and just re-use the first change address
255252
/// without loss of generality.
256-
fn change_address<C: Verification>(
253+
fn change_address(
257254
&self,
258-
secp: &Secp256k1<C>,
259255
) -> Result<(CompressedPublicKey, Address, DerivationPath)> {
260256
let path = [ChildNumber::ONE_NORMAL, ChildNumber::ZERO_NORMAL];
261-
let derived = self.account_0_xpub.derive_xpub(secp, path)?;
257+
let derived = self.account_0_xpub.derive_xpub(path)?;
262258

263259
let pk = derived.to_public_key();
264260
let addr = Address::p2wpkh(pk, NETWORK);

bitcoin/examples/sighash.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ fn compute_sighash_p2wpkh(raw_tx: &[u8], inp_idx: usize, amount: Amount) {
4848
println!("SegWit p2wpkh sighash: {sighash:x}");
4949
let msg = secp256k1::Message::from(sighash);
5050
println!("Message is {msg:x}");
51-
let secp = secp256k1::Secp256k1::verification_only();
52-
pk.verify(&secp, msg, sig).unwrap()
51+
pk.verify(msg, sig).unwrap()
5352
}
5453

5554
/// Computes sighash for a legacy multisig transaction input that spends either a p2sh or a p2ms output.

bitcoin/examples/sign-tx-segwit-v0.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use bitcoin::ext::*;
66
use bitcoin::key::WPubkeyHash;
77
use bitcoin::locktime::absolute;
8-
use bitcoin::secp256k1::{rand, Message, Secp256k1, SecretKey, Signing};
8+
use bitcoin::secp256k1::{rand, Message, SecretKey};
99
use bitcoin::sighash::{EcdsaSighashType, SighashCache};
1010
use bitcoin::{
1111
transaction, Address, Amount, Network, OutPoint, ScriptPubKeyBuf, ScriptSigBuf, Sequence,
@@ -17,11 +17,9 @@ const SPEND_AMOUNT: Amount = Amount::from_sat_u32(5_000_000);
1717
const CHANGE_AMOUNT: Amount = Amount::from_sat_u32(14_999_000); // 1000 sat fee.
1818

1919
fn main() {
20-
let secp = Secp256k1::new();
21-
2220
// Get a secret key we control and the pubkeyhash of the associated pubkey.
2321
// In a real application these would come from a stored secret.
24-
let (sk, wpkh) = senders_keys(&secp);
22+
let (sk, wpkh) = senders_keys();
2523

2624
// Get an address to send to.
2725
let address = receivers_address();
@@ -70,11 +68,11 @@ fn main() {
7068

7169
// Sign the sighash using the secp256k1 library (exported by rust-bitcoin).
7270
let msg = Message::from(sighash);
73-
let signature = secp.sign_ecdsa(msg, &sk);
71+
let signature = secp256k1::ecdsa::sign(msg, &sk);
7472

7573
// Update the witness stack.
7674
let signature = bitcoin::ecdsa::Signature { signature, sighash_type };
77-
let pk = sk.public_key(&secp);
75+
let pk = sk.public_key();
7876
*sighasher.witness_mut(input_index).unwrap() = Witness::p2wpkh(signature, pk);
7977

8078
// Get the signed transaction.
@@ -87,9 +85,9 @@ fn main() {
8785
/// An example of keys controlled by the transaction sender.
8886
///
8987
/// In a real application these would be actual secrets.
90-
fn senders_keys<C: Signing>(secp: &Secp256k1<C>) -> (SecretKey, WPubkeyHash) {
88+
fn senders_keys() -> (SecretKey, WPubkeyHash) {
9189
let sk = SecretKey::new(&mut rand::rng());
92-
let pk = bitcoin::PublicKey::new(sk.public_key(secp));
90+
let pk = bitcoin::PublicKey::new(sk.public_key());
9391
let wpkh = pk.wpubkey_hash().expect("key is compressed");
9492

9593
(sk, wpkh)

0 commit comments

Comments
 (0)