Skip to content

Commit

Permalink
Feature/go encryption (#26)
Browse files Browse the repository at this point in the history
* working for go

* use published package for example
  • Loading branch information
glitch003 authored Jan 9, 2025
1 parent 0c066ad commit 0b183f8
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 0 deletions.
54 changes: 54 additions & 0 deletions go/examples/basic_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,58 @@ func main() {
}

fmt.Printf("Signature: %v\n", signResult["signature"])

// Example of string encryption and decryption
fmt.Println("\nTesting string encryption and decryption:")
testString := "Hello, World!"
fmt.Printf("Original string: %s\n", testString)

// Encrypt the string
encryptResult, err := client.EncryptString(lit.EncryptStringParams{
DataToEncrypt: testString,
AccessControlConditions: []interface{}{
map[string]interface{}{
"contractAddress": "",
"standardContractType": "",
"chain": "ethereum",
"method": "",
"parameters": []string{":userAddress"},
"returnValueTest": map[string]interface{}{
"comparator": "=",
"value": walletAddress,
},
},
},
})
if err != nil {
log.Fatalf("Failed to encrypt string: %v", err)
}

fmt.Printf("Encrypted data: %v\n", encryptResult)

// Decrypt the string
decryptResult, err := client.DecryptString(lit.DecryptStringParams{
Chain: "ethereum",
Ciphertext: encryptResult["ciphertext"].(string),
DataToEncryptHash: encryptResult["dataToEncryptHash"].(string),
AccessControlConditions: []interface{}{
map[string]interface{}{
"contractAddress": "",
"standardContractType": "",
"chain": "ethereum",
"method": "",
"parameters": []string{":userAddress"},
"returnValueTest": map[string]interface{}{
"comparator": "=",
"value": walletAddress,
},
},
},
SessionSigs: sessionSigs,
})
if err != nil {
log.Fatalf("Failed to decrypt string: %v", err)
}

fmt.Printf("Decrypted string: %s\n", decryptResult["decryptedString"])
}
74 changes: 74 additions & 0 deletions go/lit_go_sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,72 @@ mintResult, err := client.MintWithAuth(lit_go_sdk.MintWithAuthParams{
})
```

## String Encryption and Decryption

The SDK provides functionality to encrypt and decrypt strings with access control conditions. Here's how to use it:

```go
// First, get session signatures
sessionSigsResult, err := client.GetSessionSigs(lit_go_sdk.SessionSigsParams{
Chain: "ethereum",
Expiration: time.Now().Add(10 * time.Minute).Format(time.RFC3339),
ResourceAbilityRequests: []interface{}{
map[string]interface{}{
"resource": map[string]interface{}{
"resource": "*",
"resourcePrefix": "lit-litaction",
},
"ability": "lit-action-execution",
},
},
})
sessionSigs := sessionSigsResult["sessionSigs"].(map[string]interface{})

// Encrypt a string
testString := "Hello, World!"
encryptResult, err := client.EncryptString(lit_go_sdk.EncryptStringParams{
DataToEncrypt: testString,
AccessControlConditions: []interface{}{
map[string]interface{}{
"contractAddress": "",
"standardContractType": "",
"chain": "ethereum",
"method": "",
"parameters": []string{":userAddress"},
"returnValueTest": map[string]interface{}{
"comparator": "=",
"value": "your-eth-wallet-address",
},
},
},
})

// Decrypt the string
decryptResult, err := client.DecryptString(lit_go_sdk.DecryptStringParams{
Chain: "ethereum",
Ciphertext: encryptResult["ciphertext"].(string),
DataToEncryptHash: encryptResult["dataToEncryptHash"].(string),
AccessControlConditions: []interface{}{
map[string]interface{}{
"contractAddress": "",
"standardContractType": "",
"chain": "ethereum",
"method": "",
"parameters": []string{":userAddress"},
"returnValueTest": map[string]interface{}{
"comparator": "=",
"value": "your-eth-wallet-address",
},
},
},
SessionSigs: sessionSigs,
})

fmt.Printf("Decrypted string: %s\n", decryptResult["decryptedString"])
```

The encryption is tied to access control conditions, which means only users who meet those conditions (like owning a specific wallet address) can decrypt the data.

## API Reference

### NewLitNodeClient() (\*LitNodeClient, error)
Expand Down Expand Up @@ -163,6 +229,14 @@ Generates an authentication signature.

Mints a new PKP with authentication.

### EncryptString(params EncryptStringParams) (map[string]interface{}, error)

Encrypts a string with access control conditions.

### DecryptString(params DecryptStringParams) (map[string]interface{}, error)

Decrypts a string using session signatures and access control conditions.

### Close() error

Closes the client and stops the Node.js server.
Expand Down
120 changes: 120 additions & 0 deletions go/lit_go_sdk/client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,123 @@ func TestIntegration_GetLogs(t *testing.T) {
t.Error("Expected logs to be non-empty")
}
}

func TestIntegration_EncryptAndDecryptString(t *testing.T) {
// Derive wallet address from private key
privateKeyHex := os.Getenv("LIT_POLYGLOT_SDK_TEST_PRIVATE_KEY")
if privateKeyHex == "" {
t.Fatal("LIT_POLYGLOT_SDK_TEST_PRIVATE_KEY environment variable not set")
}

privateKeyBytes, err := hex.DecodeString(strings.TrimPrefix(privateKeyHex, "0x"))
if err != nil {
t.Fatalf("Failed to decode private key: %v", err)
}

privateKey, err := crypto.ToECDSA(privateKeyBytes)
if err != nil {
t.Fatalf("Failed to convert private key: %v", err)
}

publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
t.Fatal("Failed to get public key")
}

address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()

// Get session signatures
sessionSigsResult, err := integrationClient.GetSessionSigs(SessionSigsParams{
Chain: "ethereum",
Expiration: time.Now().Add(10 * time.Minute).Format(time.RFC3339),
ResourceAbilityRequests: []interface{}{
map[string]interface{}{
"resource": map[string]interface{}{
"resource": "*",
"resourcePrefix": "lit-litaction",
},
"ability": "lit-action-execution",
},
map[string]interface{}{
"resource": map[string]interface{}{
"resource": "*",
"resourcePrefix": "lit-pkp",
},
"ability": "pkp-signing",
},
},
})
if err != nil {
t.Fatalf("GetSessionSigs() error = %v", err)
}

sessionSigs := sessionSigsResult["sessionSigs"].(map[string]interface{})

// Test string to encrypt
testString := "Hello, World!"

// Test encryption
encryptResult, err := integrationClient.EncryptString(EncryptStringParams{
DataToEncrypt: testString,
AccessControlConditions: []interface{}{
map[string]interface{}{
"contractAddress": "",
"standardContractType": "",
"chain": "ethereum",
"method": "",
"parameters": []string{":userAddress"},
"returnValueTest": map[string]interface{}{
"comparator": "=",
"value": address,
},
},
},
})
if err != nil {
t.Fatalf("EncryptString() error = %v", err)
}

ciphertext, ok := encryptResult["ciphertext"].(string)
if !ok {
t.Fatal("Expected ciphertext in response")
}

dataToEncryptHash, ok := encryptResult["dataToEncryptHash"].(string)
if !ok {
t.Fatal("Expected dataToEncryptHash in response")
}

// Test decryption
decryptResult, err := integrationClient.DecryptString(DecryptStringParams{
Chain: "ethereum",
Ciphertext: ciphertext,
DataToEncryptHash: dataToEncryptHash,
AccessControlConditions: []interface{}{
map[string]interface{}{
"contractAddress": "",
"standardContractType": "",
"chain": "ethereum",
"method": "",
"parameters": []string{":userAddress"},
"returnValueTest": map[string]interface{}{
"comparator": "=",
"value": address,
},
},
},
SessionSigs: sessionSigs,
})
if err != nil {
t.Fatalf("DecryptString() error = %v", err)
}

decryptedString, ok := decryptResult["decryptedString"].(string)
if !ok {
t.Fatal("Expected decryptedString in response")
}

if decryptedString != testString {
t.Errorf("Decrypted string does not match original. got = %v, want = %v", decryptedString, testString)
}
}
31 changes: 31 additions & 0 deletions go/lit_go_sdk/lit_node_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,34 @@ func (c *LitNodeClient) CreateSiweMessage(params CreateSiweMessageParams) (map[s
func (c *LitNodeClient) GenerateAuthSig(toSign string) (map[string]interface{}, error) {
return c.post("/authHelpers/generateAuthSig", map[string]string{"toSign": toSign})
}

// EncryptStringParams represents the parameters for encrypting a string
type EncryptStringParams struct {
DataToEncrypt string `json:"dataToEncrypt"`
AccessControlConditions []interface{} `json:"accessControlConditions,omitempty"`
EvmContractConditions []interface{} `json:"evmContractConditions,omitempty"`
SolRpcConditions []interface{} `json:"solRpcConditions,omitempty"`
UnifiedAccessControlConditions []interface{} `json:"unifiedAccessControlConditions,omitempty"`
}

// DecryptStringParams represents the parameters for decrypting a string
type DecryptStringParams struct {
Ciphertext string `json:"ciphertext"`
DataToEncryptHash string `json:"dataToEncryptHash"`
AccessControlConditions []interface{} `json:"accessControlConditions,omitempty"`
EvmContractConditions []interface{} `json:"evmContractConditions,omitempty"`
SolRpcConditions []interface{} `json:"solRpcConditions,omitempty"`
UnifiedAccessControlConditions []interface{} `json:"unifiedAccessControlConditions,omitempty"`
SessionSigs map[string]interface{} `json:"sessionSigs"`
Chain string `json:"chain"`
}

// EncryptString encrypts a string using Lit Protocol
func (c *LitNodeClient) EncryptString(params EncryptStringParams) (map[string]interface{}, error) {
return c.post("/litNodeClient/encryptString", params)
}

// DecryptString decrypts a string using Lit Protocol
func (c *LitNodeClient) DecryptString(params DecryptStringParams) (map[string]interface{}, error) {
return c.post("/litNodeClient/decryptString", params)
}

0 comments on commit 0b183f8

Please sign in to comment.