Programmable Key Pairs: A New Way to Manage Data Access and Privacy

Data is the new oil, but how can we ensure that we have full control over our own data, and who can access it? How can we share data securely and selectively with different parties without compromising our privacy or security? How can we leverage the power of cryptography and blockchain to create decentralized applications that respect our data sovereignty?

These are some of the questions that Programmable Key Pairs (PKPs) aim to answer. PKPs are a new tool that allows developers to create and manage data access policies using smart contracts on the LIT Protocol, a decentralized network for data access and privacy. PKPs enable users to encrypt their data with their own public keys, and grant or revoke access to their data with their own private keys. PKPs also allow users to specify conditions and logic for data access, such as time limits, payment requirements, or verification steps.

How do PKPs work?

PKPs are based on the concept of asymmetric encryption, which uses a pair of keys: a public key and a private key. The public key can be used to encrypt data, while the private key can be used to decrypt it. Only the owner of the private key can decrypt the data encrypted with the corresponding public key.

PKPs extend this concept by allowing users to program their keys with smart contracts. A smart contract is a piece of code that runs on a blockchain and executes predefined rules and logic. By programming their keys with smart contracts, users can create custom data access policies that define who can access their data, when, how, and under what conditions.

For example, a user can create a PKP that encrypts their medical records with their public key, and grants access to their doctor only for a limited period of time, or only after verifying their identity with a biometric scan. Alternatively, a user can create a PKP that encrypts their social media posts with their public key, and grants access to their friends only if they pay a small fee in cryptocurrency, or only if they agree to share their own posts in return.

What are the use cases for PKPs?

PKPs can be used to support a variety of use cases that require secure and selective data sharing across different domains and platforms. Some examples are:

How to get started with PKPs?

If you are a developer interested in getting started with PKPs, you can follow these steps:

  1. Install the LIT SDK: The LIT SDK is a software development kit that allows you to interact with the LIT Protocol and create PKPs. You can install it using npm or yarn:

npm install @litprotocol/lit-js-sdk

or

yarn add @litprotocol/lit-js-sdk
  1. Connect to the LIT Network: The LIT Network is a decentralized network of nodes that run the LIT Protocol and store the PKPs. You can connect to the network using the LIT SDK:

import { LitNodeClient } from '@litprotocol/lit-js-sdk';

// Create a client instance const client = new LitNodeClient();

// Connect to the network await client.connect();

  1. Create a PKP: To create a PKP, you need to specify a public key, a private key, and a smart contract. You can use the LIT SDK to generate keys and compile contracts:

import { LitNodeClient } from '@litprotocol/lit-js-sdk';

// Create a client instance const client = new LitNodeClient();

// Connect to the network await client.connect();

// Generate keys const { publicKey, privateKey } = await client.generateKeyPair();

// Write a smart contract const contract = ` pragma solidity ^0.8.0;

contract MyPKP {
  // Define your logic here
}
`;

// Compile the contract const { abi, bytecode } = await client.compileContract(contract);

  1. Deploy the PKP: To deploy the PKP, you need to send a transaction to the LIT Network with the public key, the private key, and the smart contract. You can use the LIT SDK to sign and send transactions:

import { LitNodeClient } from '@litprotocol/lit-js-sdk';

// Create a client instance const client = new LitNodeClient();

// Connect to the network await client.connect();

// Generate keys const { publicKey, privateKey } = await client.generateKeyPair();

// Write a smart contract const contract = ` pragma solidity ^0.8.0;

contract MyPKP {
  // Define your logic here
}
`;

// Compile the contract const { abi, bytecode } = await client.compileContract(contract);

// Create a transaction const tx = { publicKey, privateKey, abi, bytecode, };

// Sign the transaction const signedTx = await client.signTransaction(tx);

// Send the transaction const receipt = await client.sendTransaction(signedTx);

  1. Encrypt and decrypt data: To encrypt and decrypt data, you need to use the public key and the private key of the PKP. You can use the LIT SDK to encrypt and decrypt data:

import { LitNodeClient } from '@litprotocol/lit-js-sdk';

// Create a client instance const client = new LitNodeClient();

// Connect to the network await client.connect();

// Generate keys const { publicKey, privateKey } = await client.generateKeyPair();

// Write a smart contract const contract = ` pragma solidity ^0.8.0;

contract MyPKP {
  // Define your logic here
}
`;

// Compile the contract const { abi, bytecode } = await client.compileContract(contract);

// Create a transaction const tx = { publicKey, privateKey, abi, bytecode, };

// Sign the transaction const signedTx = await client.signTransaction(tx);

// Send the transaction const receipt = await client.sendTransaction(signedTx);

// Encrypt some data const data = 'Hello world!'; const encryptedData = await client.encryptData(data, publicKey);

// Decrypt the data const decryptedData = await client.decryptData(encryptedData, privateKey);

  1. Grant and revoke access: To grant and revoke access to your data, you need to use the smart contract of the PKP. You can use the LIT SDK to call the smart contract functions:

import { LitNodeClient } from '@litprotocol/lit-js-sdk';

// Create a client instance const client = new LitNodeClient();

// Connect to the network await client.connect();

// Generate keys const { publicKey, privateKey } = await client.generateKeyPair();

// Write a smart contract const contract = ` pragma solidity ^0.8.0;

contract MyPKP {
  // Define your logic here

// Grant access to an address function grantAccess(address _grantee) public { // Implement your logic here }

// Revoke access from an address function revokeAccess(address _grantee) public { // Implement your logic here } } `;

// Compile the contract const { abi, bytecode } = await client.compileContract(contract);

// Create a transaction const tx = { publicKey, privateKey, abi, bytecode, };

// Sign the transaction const signedTx = await client.signTransaction(tx);

// Send the transaction const receipt = await client.sendTransaction(signedTx);

// Get the PKP address from the receipt const pkpAddress = receipt.pkpAddress;

// Grant access to someone await client.callContractFunction(pkpAddress, 'grantAccess', [granteeAddress]);

// Revoke access from someone await client.callContractFunction(pkpAddress, 'revokeAccess', [granteeAddress]);

Conclusion

Programmable Key Pairs are a new way to manage data access and privacy using cryptography and blockchain. They allow users to encrypt their data with their own keys, and grant or revoke access to their data with their own rules. They enable developers to create decentralized applications that respect user data sovereignty and enable secure and selective data sharing across different domains and platforms.

If you want to learn more about PKPs and how to use them, you can visit the following resources: