@@ -141,13 +141,12 @@ Taproot Address: bc1p5utaw0g77graev5yw575c3jnzh8j88ezzw39lgr250ghppwpyccsvjkvyp
141141 GenPoint = NewJacobianPoint (genPointX , genPointY , one )
142142 precomputes = getPrecomputes ()
143143 IdentityPoint = NewJacobianPoint (zero , zero , zero )
144- addressTypes = []string {"legacy" , "nested" , "segwit" }
145- headers = [5 ][4 ]byte {
144+ addressTypes = []string {"legacy" , "nested" , "segwit" , "taproot" }
145+ headers = [4 ][4 ]byte {
146146 {0x1b , 0x1c , 0x1d , 0x1e }, // 27 - 30 P2PKH uncompressed
147147 {0x1f , 0x20 , 0x21 , 0x22 }, // 31 - 34 P2PKH compressed
148148 {0x23 , 0x24 , 0x25 , 0x26 }, // 35 - 38 P2WPKH-P2SH compressed (BIP-137)
149149 {0x27 , 0x28 , 0x29 , 0x2a }, // 39 - 42 P2WPKH compressed (BIP-137)
150- {0x2b , 0x2c , 0x2d , 0x2e }, // TODO 43 - 46 P2TR
151150 }
152151 pool = sync.Pool {
153152 New : func () any {
@@ -511,7 +510,6 @@ func generate(scalar *ModNScalar) {
511510 }
512511 scalar .Zero ()
513512 }
514- 515513}
516514
517515// NewPrivateKey generates a new privatekey object.
@@ -868,7 +866,6 @@ func DoubleSHA256(b []byte) []byte {
868866 panic (err )
869867 }
870868 return h2 .Sum (nil )
871- 872869}
873870
874871// Ripemd160SHA256 computes the RIPEMD160 hash of the SHA-256 hash of the input byte slice.
@@ -1266,11 +1263,11 @@ func rfcSign(msg []byte, privKey *ModNScalar) *Signature {
12661263//
12671264// Parameters:
12681265// - pubKey: a byte slice representing the public key.
1269- // - addrType: a string representing the address type. Valid values are "legacy", "nested", and "segwit ".
1266+ // - addrType: a string representing the address type. Valid values are "legacy", "nested", "segwit" and "taproot ".
12701267//
12711268// Returns:
12721269// - a string representing the Bitcoin address.
1273- // - an integer representing the address type. 0 for legacy, 1 for nested, and 2 for segwit.
1270+ // - an integer representing the address type.
12741271// - an error if the address type is invalid.
12751272func deriveAddress (pubKey []byte , addrType string ) (addr string , ver int , err error ) {
12761273 prefix := pubKey [0 ]
@@ -1289,6 +1286,9 @@ func deriveAddress(pubKey []byte, addrType string) (addr string, ver int, err er
12891286 if addrType == "segwit" {
12901287 return createNativeSegwit (pubKey ), 3 , nil
12911288 }
1289+ if addrType == "taproot" {
1290+ return createTaproot (pubKey ), 4 , nil
1291+ }
12921292 return "" , 0 , & SignatureError {Message : "invalid address type" }
12931293
12941294}
@@ -1304,7 +1304,7 @@ func deriveAddress(pubKey []byte, addrType string) (addr string, ver int, err er
13041304// - s: a pointer to a ModNScalar representing the s value of the signature.
13051305func splitSignature (sig []byte ) (byte , * ModNScalar , * ModNScalar , error ) {
13061306 header := sig [0 ]
1307- if header < headers [0 ][0 ] || header > headers [4 ][3 ] {
1307+ if header < headers [0 ][0 ] || header > headers [3 ][3 ] {
13081308 return 0 , nil , nil , & SignatureError {Message : "header byte out of range" }
13091309 }
13101310 var (
@@ -1323,17 +1323,17 @@ func splitSignature(sig []byte) (byte, *ModNScalar, *ModNScalar, error) {
13231323 return header , & r , & s , nil
13241324}
13251325
1326- // VerifyMessage verifies a signed message using the provided address, message, signature, and electrum flag .
1326+ // VerifyMessage verifies a signed message.
13271327//
13281328// Parameters:
1329- // - address: the address used to sign the message.
1330- // - message: the message to be verified.
1331- // - signature: the signature to verify the message.
1329+ // - message (*BitcoinMessage): The signed message to verify.
13321330// - electrum: a flag indicating whether to use the electrum signature format.
13331331//
13341332// Returns:
13351333// - a pointer to a VerifyMessageResult struct containing the verification result and the hex-encoded public key.
13361334// - error: an error if any occurred during the verification process.
1335+ //
1336+ // https://github.com/bitcoin/bips/blob/master/bip-0137.mediawiki
13371337func VerifyMessage (message * BitcoinMessage , electrum bool ) (* VerifyMessageResult , error ) {
13381338 dsig := make ([]byte , base64 .StdEncoding .DecodedLen (len (message .signature )))
13391339 n , err := base64 .StdEncoding .Decode (dsig , message .signature )
@@ -1349,10 +1349,7 @@ func VerifyMessage(message *BitcoinMessage, electrum bool) (*VerifyMessageResult
13491349 }
13501350 uncompressed := false
13511351 addrType := "legacy"
1352- if header >= 43 {
1353- header -= 16
1354- addrType = ""
1355- } else if header >= 39 {
1352+ if header >= 39 {
13561353 header -= 12
13571354 addrType = "segwit"
13581355 } else if header >= 35 {
@@ -1414,9 +1411,6 @@ func VerifyMessage(message *BitcoinMessage, electrum bool) (*VerifyMessageResult
14141411 PubKey : hex .EncodeToString (pubKey ),
14151412 Message : "message failed to verify" }, nil
14161413 }
1417- if addrType == "" {
1418- return nil , & SignatureError {Message : "unknown address type" }
1419- }
14201414 addr , _ , err := deriveAddress (pubKey , addrType )
14211415 if err != nil {
14221416 return nil , err
@@ -1450,6 +1444,8 @@ func VerifyMessage(message *BitcoinMessage, electrum bool) (*VerifyMessageResult
14501444// Returns:
14511445// - A pointer to a BitcoinMessage struct representing the signed message.
14521446// - An error if there was a problem signing the message.
1447+ //
1448+ // https://github.com/bitcoin/bips/blob/master/bip-0137.mediawiki
14531449func SignMessage (pk * privatekey , addrType , message string , deterministic , electrum bool ) (* BitcoinMessage , error ) {
14541450 var (
14551451 sig * Signature
0 commit comments