@@ -6,11 +6,17 @@ import (
66 "crypto/rand"
77 "crypto/x509"
88 "crypto/x509/pkix"
9+ "encoding/base64"
10+ "encoding/hex"
11+ "encoding/json"
912 "encoding/pem"
13+ "fmt"
1014 "math/big"
1115 "time"
12- )
1316
17+ "github.com/ton-connect/bridge3/internal/models"
18+ "golang.org/x/crypto/nacl/box"
19+ // GenerateSelfSignedCertificate generates a self-signed X.509 certificate and private key
1420func GenerateSelfSignedCertificate () ([]byte , []byte , error ) {
1521 privateKey , err := ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
1622 if err != nil {
@@ -50,3 +56,31 @@ func GenerateSelfSignedCertificate() ([]byte, []byte, error) {
5056
5157 return certPEM , keyPEM , nil
5258}
59+
60+ // EncryptRequestSourceWithWalletID encrypts the request source metadata using the wallet's Curve25519 public key
61+ func EncryptRequestSourceWithWalletID (requestSource models.BridgeRequestSource , walletID string ) (string , error ) {
62+ data , err := json .Marshal (requestSource )
63+ if err != nil {
64+ return "" , fmt .Errorf ("failed to marshal request source: %w" , err )
65+ }
66+
67+ publicKeyBytes , err := hex .DecodeString (walletID )
68+ if err != nil {
69+ return "" , fmt .Errorf ("failed to decode wallet ID: %w" , err )
70+ }
71+
72+ if len (publicKeyBytes ) != 32 {
73+ return "" , fmt .Errorf ("invalid public key length: expected 32 bytes, got %d" , len (publicKeyBytes ))
74+ }
75+
76+ // Convert to Curve25519 public key format
77+ var recipientPublicKey [32 ]byte
78+ copy (recipientPublicKey [:], publicKeyBytes )
79+
80+ encrypted , err := box .SealAnonymous (nil , data , & recipientPublicKey , rand .Reader )
81+ if err != nil {
82+ return "" , fmt .Errorf ("failed to encrypt data: %w" , err )
83+ }
84+
85+ return base64 .StdEncoding .EncodeToString (encrypted ), nil
86+ }
0 commit comments