@@ -36,7 +36,6 @@ use bitcoin::consensus::encode;
36
36
use bitcoin:: ext:: * ;
37
37
use bitcoin:: locktime:: absolute;
38
38
use bitcoin:: psbt:: { self , Input , Psbt , PsbtSighashType } ;
39
- use bitcoin:: secp256k1:: { Secp256k1 , Signing , Verification } ;
40
39
use bitcoin:: {
41
40
transaction, Address , Amount , CompressedPublicKey , Network , OutPoint , RedeemScriptBuf ,
42
41
ScriptPubKeyBuf , ScriptSigBuf , Sequence , Transaction , TxIn , TxOut , Witness ,
@@ -66,17 +65,15 @@ const CHANGE_AMOUNT_BTC: &str = "48.99999 BTC"; // 1000 sat transaction fee.
66
65
const NETWORK : Network = Network :: Regtest ;
67
66
68
67
fn main ( ) -> Result < ( ) > {
69
- let secp = Secp256k1 :: new ( ) ;
70
-
71
68
let ( offline, fingerprint, account_0_xpub, input_xpub) =
72
- ColdStorage :: new ( & secp , EXTENDED_MASTER_PRIVATE_KEY ) ?;
69
+ ColdStorage :: new ( EXTENDED_MASTER_PRIVATE_KEY ) ?;
73
70
74
71
let online = WatchOnly :: new ( account_0_xpub, input_xpub, fingerprint) ;
75
72
76
- let created = online. create_psbt ( & secp ) ?;
73
+ let created = online. create_psbt ( ) ?;
77
74
let updated = online. update_psbt ( created) ?;
78
75
79
- let signed = offline. sign_psbt ( & secp , updated) ?;
76
+ let signed = offline. sign_psbt ( updated) ?;
80
77
81
78
let finalized = online. finalize_psbt ( signed) ?;
82
79
@@ -90,6 +87,7 @@ fn main() -> Result<()> {
90
87
Ok ( ( ) )
91
88
}
92
89
90
+ // TODO: This comment is stale now, re-visit.
93
91
// We cache the pubkeys for convenience because it requires a secp context to convert the private key.
94
92
/// An example of an offline signer i.e., a cold-storage device.
95
93
struct ColdStorage {
@@ -109,20 +107,20 @@ impl ColdStorage {
109
107
/// # Returns
110
108
///
111
109
/// 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 > {
113
111
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) ;
115
113
116
114
// Hardened children require secret data to derive.
117
115
118
116
let path = "84h/0h/0h" . into_derivation_path ( ) ?;
119
117
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) ;
122
120
123
121
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) ;
126
124
127
125
let wallet = ColdStorage { master_xpriv, master_xpub } ;
128
126
let fingerprint = wallet. master_fingerprint ( ) ;
@@ -134,12 +132,11 @@ impl ColdStorage {
134
132
fn master_fingerprint ( & self ) -> Fingerprint { self . master_xpub . fingerprint ( ) }
135
133
136
134
/// Signs `psbt` with this signer.
137
- fn sign_psbt < C : Signing + Verification > (
135
+ fn sign_psbt (
138
136
& self ,
139
- secp : & Secp256k1 < C > ,
140
137
mut psbt : Psbt ,
141
138
) -> Result < Psbt > {
142
- match psbt. sign ( & self . master_xpriv , secp ) {
139
+ match psbt. sign ( & self . master_xpriv ) {
143
140
Ok ( keys) => assert_eq ! ( keys. len( ) , 1 ) ,
144
141
Err ( ( _, e) ) => {
145
142
let e = e. get ( & 0 ) . expect ( "at least one error" ) ;
@@ -173,12 +170,12 @@ impl WatchOnly {
173
170
}
174
171
175
172
/// 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 > {
177
174
let to_address =
178
175
RECEIVE_ADDRESS . parse :: < Address < _ > > ( ) ?. require_network ( Network :: Regtest ) ?;
179
176
let to_amount = OUTPUT_AMOUNT_BTC . parse :: < Amount > ( ) ?;
180
177
181
- let ( _, change_address, _) = self . change_address ( secp ) ?;
178
+ let ( _, change_address, _) = self . change_address ( ) ?;
182
179
let change_amount = CHANGE_AMOUNT_BTC . parse :: < Amount > ( ) ?;
183
180
184
181
let tx = Transaction {
@@ -253,12 +250,11 @@ impl WatchOnly {
253
250
/// "m/84h/0h/0h/1/0"). A real wallet would have access to the chain so could determine if an
254
251
/// address has been used or not. We ignore this detail and just re-use the first change address
255
252
/// without loss of generality.
256
- fn change_address < C : Verification > (
253
+ fn change_address (
257
254
& self ,
258
- secp : & Secp256k1 < C > ,
259
255
) -> Result < ( CompressedPublicKey , Address , DerivationPath ) > {
260
256
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) ?;
262
258
263
259
let pk = derived. to_public_key ( ) ;
264
260
let addr = Address :: p2wpkh ( pk, NETWORK ) ;
0 commit comments