@@ -13,10 +13,7 @@ import (
1313 c "github.com/bitcoin-sv/go-sdk/primitives/hash"
1414)
1515
16- //
17- // ECIES encryption/decryption methods; AES-128-CBC with PKCS7 is used as the cipher; hmac-sha256 is used as the mac
18- //
19-
16+ // EncryptSingle is a helper that uses Electrum ECIES method to encrypt a message
2017func EncryptSingle (message string , privateKey * ec.PrivateKey ) (string , error ) {
2118 messageBytes := []byte (message )
2219 if privateKey == nil {
@@ -26,6 +23,7 @@ func EncryptSingle(message string, privateKey *ec.PrivateKey) (string, error) {
2623 return base64 .StdEncoding .EncodeToString (decryptedBytes ), nil
2724}
2825
26+ // DecryptSingle is a helper that uses Electrum ECIES method to decrypt a message
2927func DecryptSingle (encryptedData string , privateKey * ec.PrivateKey ) (string , error ) {
3028 encryptedBytes , err := base64 .StdEncoding .DecodeString (encryptedData )
3129 if err != nil {
@@ -38,6 +36,7 @@ func DecryptSingle(encryptedData string, privateKey *ec.PrivateKey) (string, err
3836 return string (plainBytes ), nil
3937}
4038
39+ // EncryptShared is a helper that uses Electrum ECIES method to encrypt a message for a target public key
4140func EncryptShared (message string , toPublicKey * ec.PublicKey , fromPrivateKey * ec.PrivateKey ) (string , error ) {
4241 messageBytes := []byte (message )
4342 decryptedBytes , err := ElectrumEncrypt (messageBytes , toPublicKey , fromPrivateKey , false )
@@ -47,6 +46,7 @@ func EncryptShared(message string, toPublicKey *ec.PublicKey, fromPrivateKey *ec
4746 return base64 .StdEncoding .EncodeToString (decryptedBytes ), nil
4847}
4948
49+ // DecryptShared is a helper that uses Electrum ECIES method to decrypt a message from a target public key
5050func DecryptShared (encryptedData string , toPrivateKey * ec.PrivateKey , fromPublicKey * ec.PublicKey ) (string , error ) {
5151 encryptedBytes , err := base64 .StdEncoding .DecodeString (encryptedData )
5252 if err != nil {
@@ -59,6 +59,7 @@ func DecryptShared(encryptedData string, toPrivateKey *ec.PrivateKey, fromPublic
5959 return string (plainBytes ), nil
6060}
6161
62+ // ElectrumEncrypt encrypts a message using ECIES using Electrum encryption method
6263func ElectrumEncrypt (message []byte ,
6364 toPublicKey * ec.PublicKey ,
6465 fromPrivateKey * ec.PrivateKey ,
@@ -100,6 +101,8 @@ func ElectrumEncrypt(message []byte,
100101
101102 return append (encrypted , mac ... ), nil
102103}
104+
105+ // ElectrumDecrypt decrypts a message using ECIES using Electrum decryption method
103106func ElectrumDecrypt (encryptedData []byte , toPrivateKey * ec.PrivateKey , fromPublicKey * ec.PublicKey ) ([]byte , error ) {
104107
105108 if len (encryptedData ) < 52 { // Minimum length: 4 (magic) + 16 (min cipher) + 32 (mac)
@@ -152,7 +155,7 @@ func ElectrumDecrypt(encryptedData []byte, toPrivateKey *ec.PrivateKey, fromPubl
152155 return plain , nil
153156}
154157
155- // BitcoreEncrypt encrypts a message using ECIES
158+ // BitcoreEncrypt encrypts a message using ECIES using Bitcore encryption method
156159func BitcoreEncrypt (message []byte ,
157160 toPublicKey * ec.PublicKey ,
158161 fromPrivateKey * ec.PrivateKey ,
@@ -187,6 +190,7 @@ func BitcoreEncrypt(message []byte,
187190 return encBuf , nil
188191}
189192
193+ // BitcoreDecrypt decrypts a message using ECIES using Bitcore decryption method
190194func BitcoreDecrypt (encryptedMessage []byte , toPrivatKey * ec.PrivateKey ) ([]byte , error ) {
191195
192196 fromPublicKey , err := ec .ParsePubKey (encryptedMessage [:33 ])
0 commit comments