@@ -21,13 +21,11 @@ import (
2121 "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc/types"
2222 ourbtcutil "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/btc/util"
2323 "github.com/BitBoxSwiss/bitbox-wallet-app/backend/signing"
24- "github.com/BitBoxSwiss/bitbox-wallet-app/util/errp"
2524 "github.com/btcsuite/btcd/btcec/v2"
2625 "github.com/btcsuite/btcd/btcec/v2/schnorr"
2726 "github.com/btcsuite/btcd/btcutil"
2827 "github.com/btcsuite/btcd/chaincfg"
2928 "github.com/btcsuite/btcd/txscript"
30- "github.com/btcsuite/btcd/wire"
3129 "github.com/sirupsen/logrus"
3230)
3331
@@ -38,12 +36,13 @@ type AccountAddress struct {
3836
3937 // AccountConfiguration is the account level configuration from which this address was derived.
4038 AccountConfiguration * signing.Configuration
41- // publicKey is the public key of a single-sig address.
42- publicKey * btcec.PublicKey
39+ // PublicKey is the public key of a single-sig address.
40+ PublicKey * btcec.PublicKey
4341 Derivation types.Derivation
4442
45- // redeemScript stores the redeem script of a BIP16 P2SH output or nil if address type is P2PKH.
46- redeemScript []byte
43+ // redeemScript stores the redeem script of a BIP16 P2SH output or nil if address type is not
44+ // P2SH.
45+ RedeemScript []byte
4746
4847 log * logrus.Entry
4948}
@@ -116,9 +115,9 @@ func NewAccountAddress(
116115 return & AccountAddress {
117116 Address : address ,
118117 AccountConfiguration : accountConfiguration ,
119- publicKey : publicKey ,
118+ PublicKey : publicKey ,
120119 Derivation : derivation ,
121- redeemScript : redeemScript ,
120+ RedeemScript : redeemScript ,
122121 log : log ,
123122 }
124123}
@@ -128,23 +127,6 @@ func (address *AccountAddress) ID() string {
128127 return string (address .PubkeyScriptHashHex ())
129128}
130129
131- // BIP352Pubkey returns the pubkey used for silent payments:
132- // - 33 byte compressed public key for p2pkh, p2wpkh, p2wpkh-p2sh.
133- // - 32 byte x-only public key for p2tr
134- // See https://github.com/bitcoin/bips/blob/master/bip-0352.mediawiki#user-content-Inputs_For_Shared_Secret_Derivation.
135- func (address * AccountAddress ) BIP352Pubkey () ([]byte , error ) {
136- publicKey := address .publicKey
137- switch address .AccountConfiguration .ScriptType () {
138- case signing .ScriptTypeP2PKH , signing .ScriptTypeP2WPKHP2SH , signing .ScriptTypeP2WPKH :
139- return publicKey .SerializeCompressed (), nil
140- case signing .ScriptTypeP2TR :
141- outputKey := txscript .ComputeTaprootKeyNoScript (publicKey )
142- return schnorr .SerializePubKey (outputKey ), nil
143- default :
144- return nil , errp .New ("unsupported script type for silent payments" )
145- }
146- }
147-
148130// EncodeForHumans implements accounts.Address.
149131func (address * AccountAddress ) EncodeForHumans () string {
150132 return address .EncodeAddress ()
@@ -180,59 +162,11 @@ func (address *AccountAddress) ScriptForHashToSign() (bool, []byte) {
180162 case signing .ScriptTypeP2PKH :
181163 return false , address .PubkeyScript ()
182164 case signing .ScriptTypeP2WPKHP2SH :
183- return true , address .redeemScript
165+ return true , address .RedeemScript
184166 case signing .ScriptTypeP2WPKH :
185167 return true , address .PubkeyScript ()
186168 default :
187169 address .log .Panic ("Unrecognized address type." )
188170 }
189171 panic ("The end of the function cannot be reached." )
190172}
191-
192- // SignatureScript returns the signature script (and witness) needed to spend from this address.
193- // The signatures have to be provided in the order of the configuration (and some can be nil).
194- func (address * AccountAddress ) SignatureScript (
195- signature types.Signature ,
196- ) ([]byte , wire.TxWitness ) {
197- publicKey := address .publicKey
198- switch address .AccountConfiguration .ScriptType () {
199- case signing .ScriptTypeP2PKH :
200- signatureScript , err := txscript .NewScriptBuilder ().
201- AddData (append (signature .SerializeDER (), byte (txscript .SigHashAll ))).
202- AddData (publicKey .SerializeCompressed ()).
203- Script ()
204- if err != nil {
205- address .log .WithError (err ).Panic ("Failed to build signature script for P2PKH." )
206- }
207- return signatureScript , nil
208- case signing .ScriptTypeP2WPKHP2SH :
209- signatureScript , err := txscript .NewScriptBuilder ().
210- AddData (address .redeemScript ).
211- Script ()
212- if err != nil {
213- address .log .WithError (err ).Panic ("Failed to build segwit signature script." )
214- }
215- txWitness := wire.TxWitness {
216- append (signature .SerializeDER (), byte (txscript .SigHashAll )),
217- publicKey .SerializeCompressed (),
218- }
219- return signatureScript , txWitness
220- case signing .ScriptTypeP2WPKH :
221- txWitness := wire.TxWitness {
222- append (signature .SerializeDER (), byte (txscript .SigHashAll )),
223- publicKey .SerializeCompressed (),
224- }
225- return []byte {}, txWitness
226- case signing .ScriptTypeP2TR :
227- // We assume SIGHASH_DEFAULT, which defaults to SIGHASH_ALL without needing to explicitly
228- // append it to the signature. See:
229- // https://github.com/bitcoin/bips/blob/97e02b2223b21753acefa813a4e59dbb6e849e77/bip-0341.mediawiki#taproot-key-path-spending-signature-validation
230- txWitness := wire.TxWitness {
231- signature .SerializeCompact (),
232- }
233- return []byte {}, txWitness
234- default :
235- address .log .Panic ("Unrecognized address type." )
236- }
237- panic ("The end of the function cannot be reached." )
238- }
0 commit comments